diff mbox

[non-atomic-injection] x86 emulator: Add cwd/cdq/cqo instruction (opcode 0x99)

Message ID 1282227939-4378-1-git-send-email-m.gamal005@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Mohammed Gamal Aug. 19, 2010, 2:25 p.m. UTC
None
diff mbox

Patch

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index ee4bb69..fddf76d 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -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);