public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86 emulator: Add in/out instructions, opcodes (0xe4-0xe7, 0xec-0xef)
@ 2008-08-04 19:57 Mohammed Gamal
  2008-08-11 11:45 ` Avi Kivity
  0 siblings, 1 reply; 4+ messages in thread
From: Mohammed Gamal @ 2008-08-04 19:57 UTC (permalink / raw)
  To: kvm; +Cc: avi, riel

This patch adds instructions 'in' and 'out' to the x86 emulator.

Signed-off-by: Mohammed Gamal <m.gamal005@gmail.com>

---
 arch/x86/kvm/x86_emulate.c |   36 ++++++++++++++++++++++++++++++++++--
 1 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86_emulate.c b/arch/x86/kvm/x86_emulate.c
index 4c7ca7f..a73e702 100644
--- a/arch/x86/kvm/x86_emulate.c
+++ b/arch/x86/kvm/x86_emulate.c
@@ -172,11 +172,14 @@ static u16 opcode_table[256] = {
 	/* 0xD8 - 0xDF */
 	0, 0, 0, 0, 0, 0, 0, 0,
 	/* 0xE0 - 0xE7 */
-	0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, 
+	SrcNone | ByteOp | ImplicitOps, SrcNone |ImplicitOps,
 	/* 0xE8 - 0xEF */
 	ImplicitOps | Stack, SrcImm | ImplicitOps,
 	ImplicitOps, SrcImmByte | ImplicitOps,
-	0, 0, 0, 0,
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, 
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
 	/* 0xF0 - 0xF7 */
 	0, 0, 0, 0,
 	ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
@@ -1254,6 +1257,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	u64 msr_data;
 	unsigned long saved_eip = 0;
 	struct decode_cache *c = &ctxt->decode;
+	unsigned int port;
 	int rc = 0;
 
 	/* Shadow copy of register state. Committed on successful emulation.
@@ -1682,6 +1686,14 @@ special_insn:
 		c->src.val = c->regs[VCPU_REGS_RCX];
 		emulate_grp2(ctxt);
 		break;
+	case 0xe4: 	/* inb */
+	case 0xe5: 	/* in */
+		port = insn_fetch(u8, 1, c->eip);
+		goto in;
+	case 0xe6: /* outb */
+	case 0xe7: /* out */
+		port = insn_fetch(u8, 1, c->eip);
+		goto out;
 	case 0xe8: /* call (near) */ {
 		long int rel;
 		switch (c->op_bytes) {
@@ -1732,6 +1744,26 @@ special_insn:
 		jmp_rel(c, c->src.val);
 		c->dst.type = OP_NONE; /* Disable writeback. */
 		break;
+	case 0xec: /* in al,dx */
+	case 0xed: /* in (e/r)ax,dx */
+		port = c->regs[VCPU_REGS_RDX];
+	in:	if(kvm_emulate_pio(ctxt->vcpu, NULL, 1,
+				   (c->d & ByteOp) ? 1 : c->op_bytes,
+				   port) != 0) { 
+			c->eip = saved_eip;
+			return -1;
+		}
+		return 0;
+	case 0xee: /* out al,dx */
+	case 0xef: /* out (e/r)ax,dx */
+		port = c->regs[VCPU_REGS_RDX];
+	out:	if(kvm_emulate_pio(ctxt->vcpu, NULL, 0,
+				   (c->d & ByteOp) ? 1 : c->op_bytes,
+				   port) != 0) { 
+			c->eip = saved_eip;
+			return -1;
+		}
+		return 0;
 	case 0xf4:              /* hlt */
 		ctxt->vcpu->arch.halt_request = 1;
 		break;
-- 
1.5.4.3

 


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86 emulator: Add in/out instructions, opcodes (0xe4-0xe7, 0xec-0xef)
  2008-08-04 19:57 [PATCH] x86 emulator: Add in/out instructions, opcodes (0xe4-0xe7, 0xec-0xef) Mohammed Gamal
@ 2008-08-11 11:45 ` Avi Kivity
  0 siblings, 0 replies; 4+ messages in thread
From: Avi Kivity @ 2008-08-11 11:45 UTC (permalink / raw)
  To: Mohammed Gamal; +Cc: kvm, riel

Mohammed Gamal wrote:
> This patch adds instructions 'in' and 'out' to the x86 emulator.
> 

> +	SrcNone | ByteOp | ImplicitOps, SrcNone |ImplicitOps,

Missing blank.



>  	case 0xe8: /* call (near) */ {
>  		long int rel;
>  		switch (c->op_bytes) {
> @@ -1732,6 +1744,26 @@ special_insn:
>  		jmp_rel(c, c->src.val);
>  		c->dst.type = OP_NONE; /* Disable writeback. */
>  		break;
> +	case 0xec: /* in al,dx */
> +	case 0xed: /* in (e/r)ax,dx */
> +		port = c->regs[VCPU_REGS_RDX];
> +	in:	if(kvm_emulate_pio(ctxt->vcpu, NULL, 1,

missing blank.

> +				   (c->d & ByteOp) ? 1 : c->op_bytes,
> +				   port) != 0) { 
> +			c->eip = saved_eip;
> +			return -1;
> +		}
> +		return 0;
> +	case 0xee: /* out al,dx */
> +	case 0xef: /* out (e/r)ax,dx */
> +		port = c->regs[VCPU_REGS_RDX];
> +	out:	if(kvm_emulate_pio(ctxt->vcpu, NULL, 0,
> +				   (c->d & ByteOp) ? 1 : c->op_bytes,
> +				   port) != 0) { 
> +			c->eip = saved_eip;
> +			return -1;


goto cannot_emulate?

> +		}
> +		return 0;

It would be simpler to fold the in and out implementations together (and 
extract the direction flag from the instruction byte).



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef)
@ 2008-09-06 14:22 Mohammed Gamal
  2008-09-07  7:27 ` Avi Kivity
  0 siblings, 1 reply; 4+ messages in thread
From: Mohammed Gamal @ 2008-09-06 14:22 UTC (permalink / raw)
  To: kvm; +Cc: avi

The patch adds in/out instructions to the x86 emulator.

The instruction was encountered while running the BIOS while using
the invalid guest state emulation patch.

Signed-off-by: Mohammed Gamal <m.gamal005@gmail.com>
---
 arch/x86/kvm/x86_emulate.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86_emulate.c b/arch/x86/kvm/x86_emulate.c
index 005f1db..dfc1090 100644
--- a/arch/x86/kvm/x86_emulate.c
+++ b/arch/x86/kvm/x86_emulate.c
@@ -170,11 +170,14 @@ static u16 opcode_table[256] = {
 	/* 0xD8 - 0xDF */
 	0, 0, 0, 0, 0, 0, 0, 0,
 	/* 0xE0 - 0xE7 */
-	0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0,
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
 	/* 0xE8 - 0xEF */
 	ImplicitOps | Stack, SrcImm | ImplicitOps,
 	ImplicitOps, SrcImmByte | ImplicitOps,
-	0, 0, 0, 0,
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
+	SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps,
 	/* 0xF0 - 0xF7 */
 	0, 0, 0, 0,
 	ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3,
@@ -1252,6 +1255,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	u64 msr_data;
 	unsigned long saved_eip = 0;
 	struct decode_cache *c = &ctxt->decode;
+	unsigned int port;
+	int io_direction;
 	int rc = 0;
 
 	/* Shadow copy of register state. Committed on successful emulation.
@@ -1680,6 +1685,16 @@ special_insn:
 		c->src.val = c->regs[VCPU_REGS_RCX];
 		emulate_grp2(ctxt);
 		break;
+	case 0xe4: 	/* inb */
+	case 0xe5: 	/* in */
+		port = insn_fetch(u8, 1, c->eip);
+		io_direction = 1;
+		goto do_io;
+	case 0xe6: /* outb */
+	case 0xe7: /* out */
+		port = insn_fetch(u8, 1, c->eip);
+		io_direction = 0;
+		goto do_io;
 	case 0xe8: /* call (near) */ {
 		long int rel;
 		switch (c->op_bytes) {
@@ -1730,6 +1745,22 @@ special_insn:
 		jmp_rel(c, c->src.val);
 		c->dst.type = OP_NONE; /* Disable writeback. */
 		break;
+	case 0xec: /* in al,dx */
+	case 0xed: /* in (e/r)ax,dx */
+		port = c->regs[VCPU_REGS_RDX];
+		io_direction = 1;
+		goto do_io;
+	case 0xee: /* out al,dx */
+	case 0xef: /* out (e/r)ax,dx */
+		port = c->regs[VCPU_REGS_RDX];
+		io_direction = 0;
+	do_io:	if (kvm_emulate_pio(ctxt->vcpu, NULL, io_direction,
+				   (c->d & ByteOp) ? 1 : c->op_bytes,
+				   port) != 0) {
+			c->eip = saved_eip;
+			goto cannot_emulate;
+		}
+		return 0;
 	case 0xf4:              /* hlt */
 		ctxt->vcpu->arch.halt_request = 1;
 		break;
-- 
1.5.4.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef)
  2008-09-06 14:22 [PATCH] x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef) Mohammed Gamal
@ 2008-09-07  7:27 ` Avi Kivity
  0 siblings, 0 replies; 4+ messages in thread
From: Avi Kivity @ 2008-09-07  7:27 UTC (permalink / raw)
  To: Mohammed Gamal; +Cc: kvm

Mohammed Gamal wrote:
> The patch adds in/out instructions to the x86 emulator.
>
> The instruction was encountered while running the BIOS while using
> the invalid guest state emulation patch.
>
>   

Applied, thanks.

> @@ -1252,6 +1255,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
>  	u64 msr_data;
>  	unsigned long saved_eip = 0;
>  	struct decode_cache *c = &ctxt->decode;
> +	unsigned int port;
> +	int io_direction;
>  	int rc = 0;
>  
>  	/* Shadow copy of register state. Committed on successful emulation.
>   

I renamed io_direction to io_dir_in as it isn't immediately clear what 0 
and 1 mean with the original name.

-- 
error compiling committee.c: too many arguments to function


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-09-07  7:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-04 19:57 [PATCH] x86 emulator: Add in/out instructions, opcodes (0xe4-0xe7, 0xec-0xef) Mohammed Gamal
2008-08-11 11:45 ` Avi Kivity
  -- strict thread matches above, loose matches on Subject: below --
2008-09-06 14:22 [PATCH] x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef) Mohammed Gamal
2008-09-07  7:27 ` Avi Kivity

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox