Skip to contentSkip to conversation

x86 emulator: Add cwd/cdq/cqo instruction (opcode 0x99)

PATCH In review Mohammed Gamal 2 participants 2 messages Aug 19, 2010 – Aug 22, 2010

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

PATCH 1/1 x86 emulator: Add cwd/cdq/cqo instruction (opcode 0x99)

---
 arch/x86/kvm/emulate.c |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +15 -1
index ee4bb69..fddf76d 100644
@@ -2421,7 +2421,7 @@ static struct opcode opcode_table[256] = {
 	/* 0x90 - 0x97 */
 	X8(D(SrcAcc | DstReg)),
 	/* 0x98 - 0x9F */
-	D(DstAcc | SrcNone), N, D(SrcImmFAddr | No64), N,
+	D(DstAcc | SrcNone), D(ImplicitOps), D(SrcImmFAddr | No64), N,
 	D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
 	/* 0xA0 - 0xA7 */
 	D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
@@ -3161,6 +3161,20 @@ special_insn:
 		case 8: c->dst.val = (s32)c->dst.val; break;
 		}
 		break;
+	case 0x99: /* cwd/cdq/cqo */ {
+		unsigned long rax = c->regs[VCPU_REGS_RAX];
+		unsigned long sign_bit = 1 << ((c->op_bytes * 8) - 1);
+
+		if (rax & sign_bit) {
+			switch (c->op_bytes) {
+			case 2: c->regs[VCPU_REGS_RDX] = 0xffff; break;
+			case 4: c->regs[VCPU_REGS_RDX] = 0xffffffff; break;
+			case 8: c->regs[VCPU_REGS_RDX] = 0xffffffffffffffff; break;
+			}
+		} else 
+			c->regs[VCPU_REGS_RDX] = 0;
+		}
+		break;
 	case 0x9c: /* pushf */
 		c->src.val =  (unsigned long) ctxt->eflags;
 		emulate_push(ctxt, ops);
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 26 lines · Mohammed Gamal
---
  arch/x86/kvm/emulate.c |   16 +++++++++++++++-
  1 files changed, 15 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +5 -1
index ee4bb69..fddf76d 100644
@@ -2421,7 +2421,7 @@ static struct opcode opcode_table[256] = {
  	/* 0x90 - 0x97 */
  	X8(D(SrcAcc | DstReg)),
  	/* 0x98 - 0x9F */
-	D(DstAcc | SrcNone), N, D(SrcImmFAddr | No64), N,
+	D(DstAcc | SrcNone), D(ImplicitOps), D(SrcImmFAddr | No64), N,
  	D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
  	/* 0xA0 - 0xA7 */
  	D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
@@ -3161,6 +3161,20 @@ special_insn:
  		case 8: c->dst.val = (s32)c->dst.val; break;
  		}
  		break;
+	case 0x99: /* cwd/cdq/cqo */ {
+		unsigned long rax = c->regs[VCPU_REGS_RAX];
+		unsigned long sign_bit = 1<<  ((c->op_bytes * 8) - 1);
+

arch/x86/kvm/emulate.c special_insn: · line 3177

Quoted 10 lines
arch/x86/kvm/emulate.c +10 -0
+		if (rax&  sign_bit) {
+			switch (c->op_bytes) {
+			case 2: c->regs[VCPU_REGS_RDX] = 0xffff; break;
+			case 4: c->regs[VCPU_REGS_RDX] = 0xffffffff; break;
+			case 8: c->regs[VCPU_REGS_RDX] = 0xffffffffffffffff; break;
+			}
+		} else
+			c->regs[VCPU_REGS_RDX] = 0;
+		}
+		break;

Again I have something (a bit simpler) in my queue, will push it soon. I applied your test case.

We need to work out a way of not duplicating this work.

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