Skip to contentSkip to conversation

x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef)

PATCH RESEND In review Mohammed Gamal 2 participants 5 messages Aug 18, 2008 – Aug 20, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 x86 emulator: Add in/out instructions (opcodes 0xe4-0xe7, 0xec-0xef)

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.

---
 arch/x86/kvm/x86_emulate.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
arch/x86/kvm/x86_emulate.c +33 -2
index 005f1db..dfc1090 100644
@@ -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;
Signature · 1 line
1.5.4.3
#1 lore raw
Quoted 5 lines · Mohammed Gamal

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.

We still don't have a testcase for this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Mohammed Gamal

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.

We still don't have a testcase for this.

To be clear, do you want an emulator test case (in user/test/x86/emulator.c) for every instruction added to the emulator?

#3 lore raw
Quoted 10 lines · Mohammed Gamal
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Mohammed Gamal

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.

Quoted 6 lines
Quoted 3 lines

We still don't have a testcase for this.

To be clear, do you want an emulator test case (in user/test/x86/emulator.c) for every instruction added to the emulator?

Yes. We've been bitten by rarely-used instructions being broken.

I've now added a simple harness to test these instructions. It works by jumping into big real mode, forcing kvm to emulate (if emulate_invalid_guest_state=1) and returning.

Please review it as well as the few fixes I've made in kvm to support it. It's in the 'realmode' branch of both kvm.git and kvm-userspace.git. Once it passes regression testing, I'll merge it into master.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 10 lines · Avi Kivity
Quoted 3 lines

To be clear, do you want an emulator test case (in user/test/x86/emulator.c) for every instruction added to the emulator?

Yes. We've been bitten by rarely-used instructions being broken.

I've now added a simple harness to test these instructions. It works by jumping into big real mode, forcing kvm to emulate (if emulate_invalid_guest_state=1) and returning.

Please review it as well as the few fixes I've made in kvm to support it. It's in the 'realmode' branch of both kvm.git and kvm-userspace.git. Once it passes regression testing, I'll merge it into master.

The harness is in a new file, user/test/x86/realmode.c, btw. emulate.c will be used for 64-bit instructions.

Signature · 1 line
error compiling committee.c: too many arguments to function