Skip to contentSkip to conversation

x86 emulator: Add IRET instruction

PATCH In review Mohammed Gamal 4 participants 9 messages Jul 25, 2010 – Jul 26, 2010

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

PATCH 1/1 x86 emulator: Add IRET instruction

Ths patch adds IRET instruction (opcode 0xcf).
Currently, only IRET in real mode is emulated. Protected mode support is to be added later if needed.

[Fixed a typo]

---
 arch/x86/kvm/emulate.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +61 -0
index b38bd8b..4f18b9d 100644
@@ -1778,6 +1778,61 @@ static int emulate_popa(struct x86_emulate_ctxt *ctxt,
 	return rc;
 }
 
+static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
+			     struct x86_emulate_ops *ops)
+{
+	struct decode_cache *c = &ctxt->decode;
+	int rc = X86EMUL_CONTINUE;
+	unsigned long temp_eip = 0;
+	unsigned long temp_eflags = 0;
+	/* TODO: Add stack limit check */
+
+	rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
+
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+
+	if (temp_eip & ~0xffff) {
+		emulate_gp(ctxt, 0);
+		return X86EMUL_PROPAGATE_FAULT;
+	}
+
+	c->eip = temp_eip;
+
+	rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_CS);
+
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+
+	rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
+
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+
+	if (c->op_bytes == 4)
+		temp_eflags = ((temp_eflags & 0x257fd5) | (ctxt->eflags & 0x1a0000));
+	
+	ctxt->eflags = temp_eflags;
+
+	return rc;
+}
+
+static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
+				    struct x86_emulate_ops* ops)
+{
+	switch(ctxt->mode) {
+	case X86EMUL_MODE_REAL:
+		return emulate_iret_real(ctxt, ops);
+	case X86EMUL_MODE_VM86:
+	case X86EMUL_MODE_PROT16:
+	case X86EMUL_MODE_PROT32:
+	case X86EMUL_MODE_PROT64:
+	default:
+		/* iret from protected mode unimplemented yet */
+		return X86EMUL_UNHANDLEABLE;		
+	}
+}
+
 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
 				struct x86_emulate_ops *ops)
 {
@@ -2910,6 +2965,12 @@ special_insn:
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		break;
+	case 0xcf:		/* iret */
+		rc = emulate_iret(ctxt, ops);
+
+		if (rc != X86EMUL_CONTINUE)
+			goto done;
+		break;
 	case 0xd0 ... 0xd1:	/* Grp2 */
 		c->src.val = 1;
 		emulate_grp2(ctxt);
Signature · 1 line
1.7.0.4
maintainer
#1 lore raw

Review

Quoted 2 lines · Mohammed Gamal
(quoted diff) +2 -0
+	if (c->op_bytes == 4)
+		temp_eflags = ((temp_eflags&  0x257fd5) | (ctxt->eflags&  0x1a0000));

Should this do also

if (c->op_bytes == 2)

    temp_eflags = ((temp_eflags & 0x7fd5) | (ctxt->eflags & ~0xffffL));

?

Or better, extract a new function computing the mask from emulate_popf, which would do something similar to what I wrote above.

Paolo

#2 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Mohammed Gamal

+       if (c->op_bytes == 4)
+               temp_eflags = ((temp_eflags&  0x257fd5) | (ctxt->eflags& 0x1a0000));

Quoted 6 lines

Should this do also

if (c->op_bytes == 2)
   temp_eflags = ((temp_eflags & 0x7fd5) | (ctxt->eflags & ~0xffffL));

?

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

Quoted 6 lines

Or better, extract a new function computing the mask from emulate_popf, which would do something similar to what I wrote above.

Paolo

#3 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Mohammed Gamal

Review

Quoted 5 lines
(quoted diff) +2 -0
+       if (c->op_bytes == 4)
+               temp_eflags = ((temp_eflags&  0x257fd5) | (ctxt->eflags&
 0x1a0000));

Should this do also

Quoted 9 lines
Quoted 5 lines

if (c->op_bytes == 2)
temp_eflags = ((temp_eflags & 0x7fd5) | (ctxt->eflags & ~0xffffL));

?

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

Intel manual says:

EFLAGS[15:0] ← Pop();

Quoted 14 lines
Quoted 7 lines

Or better, extract a new function computing the mask from emulate_popf, which would do something similar to what I wrote above.

Paolo

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#4 lore raw
In reply to Mohammed Gamal
Quoted 9 lines · Mohammed Gamal
Quoted 8 lines · Paolo Bonzini

Review

Quoted 4 lines · Mohammed Gamal
(quoted diff) +2 -0
+       if (c->op_bytes == 4)
+               temp_eflags = ((temp_eflags & 0x257fd5) | (ctxt->eflags&
  0x1a0000));

Should this do also

Quoted 8 lines
Quoted 4 lines

if (c->op_bytes == 2)

    temp_eflags = ((temp_eflags & 0x7fd5) | (ctxt->eflags & ~0xffffL));

?

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

That's fine, but please make sure that

mov %sp, %bp
orw $2, 4(%bp)
iret

followed at return site by

pushf
popw %ax

does not set bit 1 in %ax. That's the important point (also see how emulate_popf avoids magic hex constants).

Paolo

#5 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 3 lines

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

That's fine, but please make sure that

mov %sp, %bp
orw $2, 4(%bp)
iret

Quoted 6 lines

followed at return site by

pushf
popw %ax

does not set bit 1 in %ax. That's the important point (also see how emulate_popf avoids magic hex constants).

Moreover, vmx will fail the next entry if this is not done. 23.3.1.4 says:

Quoted 4 lines

RFLAGS.
— Reserved bits 63:22 (bits 31:22 on processors that do not support Intel 64 architecture), bit 15, bit 5 and bit 3 must be 0 in the field, and reserved bit 1 must be 1.

Looks like a note is missing in the manual. I'll alert the authors.

Signature · 1 line
error compiling committee.c: too many arguments to function
maintainer
#6 lore raw
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Paolo Bonzini
Quoted 3 lines

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

That's fine, but please make sure that

mov %sp, %bp
orw $2, 4(%bp)
iret

followed at return site by

Quoted 12 lines
Quoted 5 lines

pushf
popw %ax

does not set bit 1 in %ax. That's the important point (also see how emulate_popf avoids magic hex constants).

Moreover, vmx will fail the next entry if this is not done. 23.3.1.4 says:

Quoted 3 lines

RFLAGS. — Reserved bits 63:22 (bits 31:22 on processors that do not support Intel 64 architecture), bit 15, bit 5 and bit 3 must be 0 in the field, and reserved bit 1 must be 1.

(I remembered one bit had to be 1, but failed to recall which one. I should have looked up SAHF in the manual). This means that my code actually should be

mov %sp, %bp
orw $8, 4(%bp)
iret

followed by testing bit 3.

The emulate_popf approach that explicitly lists bits taken from the stack seems more robust. For example, Mohammed's "if (c->op_bytes == 4)" code leaves bit 1 cleared:

    temp_eflags = ((temp_eflags & 0x257fd5)
          | (ctxt->eflags & 0x1a0000));

(But then it is probably never used since only a 32-bit code segment in unreal mode would trigger it).

Paolo

maintainer
#7 lore raw
In reply to Avi Kivity
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Paolo Bonzini
Quoted 3 lines

I don't think this is needed. The temp_eflags value is assigned directly to eflags if we're operand size is 16 bits. At least that's what the Intel manual says!

That's fine, but please make sure that

mov %sp, %bp
orw $2, 4(%bp)
iret

followed at return site by

Quoted 12 lines
Quoted 5 lines

pushf
popw %ax

does not set bit 1 in %ax. That's the important point (also see how emulate_popf avoids magic hex constants).

Moreover, vmx will fail the next entry if this is not done. 23.3.1.4 says:

Quoted 3 lines

RFLAGS. — Reserved bits 63:22 (bits 31:22 on processors that do not support Intel 64 architecture), bit 15, bit 5 and bit 3 must be 0 in the field, and reserved bit 1 must be 1.

(I remembered one bit had to be 1, but failed to recall which one. I should have looked up SAHF in the manual). This means that my code actually should be

mov %sp, %bp
orw $8, 4(%bp)
iret

followed by testing bit 3.

The emulate_popf approach that explicitly lists bits taken from the stack seems more robust. For example, Mohammed's "if (c->op_bytes == 4)" code leaves bit 1 cleared:

    temp_eflags = ((temp_eflags & 0x257fd5)
          | (ctxt->eflags & 0x1a0000));

(But then it is probably never used since only a 32-bit code segment in unreal mode would trigger it).

Paolo

#8 lore raw
In reply to Mohammed Gamal
Quoted 3 lines · Mohammed Gamal

Ths patch adds IRET instruction (opcode 0xcf).
Currently, only IRET in real mode is emulated. Protected mode support is to be added later if needed.

Please post a unit test with the next iteration.

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