Skip to contentSkip to conversation

x86 emulator: Add call near absolute (opcode 0xff rm/2)

PATCH New Mohammed Gamal 1 participant 1 message Aug 20, 2008

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

PATCH 1/1 x86 emulator: Add call near absolute (opcode 0xff rm/2)

This adds the near call with absolute address instruction.

---
 arch/x86/kvm/x86_emulate.c |   55 +++++++++++++++++++++++++++++--------------
 1 files changed, 37 insertions(+), 18 deletions(-)
arch/x86/kvm/x86_emulate.c +37 -18
index b97e80e..1707056 100644
@@ -1080,6 +1080,37 @@ static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
 					       c->regs[VCPU_REGS_RSP]);
 }
 
+static int emulate_near_call(struct x86_emulate_ctxt *ctxt,
+			 struct x86_emulate_ops *ops,
+			 bool absolute)
+{
+	struct decode_cache *c = &ctxt->decode;
+	long int offset;
+	int rc = 0;
+
+	switch (c->op_bytes) {
+	case 2:
+		offset = insn_fetch(s16, 2, c->eip);
+		break;
+	case 4:
+		offset = insn_fetch(s32, 4, c->eip);
+		break;
+	default:
+		DPRINTF("Call: Invalid op_bytes\n");
+		rc = 1;
+		goto done;
+	}
+	c->src.val = (unsigned long) c->eip;
+	if (absolute)
+		c->eip = offset;
+	else
+		jmp_rel(c, offset);
+	c->op_bytes = c->ad_bytes;
+	emulate_push(ctxt);
+done:
+	return rc;
+}
+
 static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
 				struct x86_emulate_ops *ops)
 {
@@ -1162,6 +1193,8 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
 	case 1:	/* dec */
 		emulate_1op("dec", c->dst, ctxt->eflags);
 		break;
+	case 2: /* call near abs */
+		return emulate_near_call(ctxt, ops, 1);
 	case 4: /* jmp abs */
 		c->eip = c->src.val;
 		break;
@@ -1710,25 +1743,11 @@ special_insn:
 		port = insn_fetch(u8, 1, c->eip);
 		io_direction = 0;
 		goto do_io;
-	case 0xe8: /* call (near) */ {
-		long int rel;
-		switch (c->op_bytes) {
-		case 2:
-			rel = insn_fetch(s16, 2, c->eip);
-			break;
-		case 4:
-			rel = insn_fetch(s32, 4, c->eip);
-			break;
-		default:
-			DPRINTF("Call: Invalid op_bytes\n");
-			goto cannot_emulate;
-		}
-		c->src.val = (unsigned long) c->eip;
-		jmp_rel(c, rel);
-		c->op_bytes = c->ad_bytes;
-		emulate_push(ctxt);
+	case 0xe8: /* call (near) */
+		rc = emulate_near_call(ctxt, ops, 0);
+		if (rc != 0)
+			goto done;
 		break;
-	}
 	case 0xe9: /* jmp rel */
 		goto jmp;
 	case 0xea: /* jmp far */ {
Signature · 1 line
1.5.4.3