KVM: x86 emulator: Use opcode::execute series
v2 cover KVM: x86 emulator: Use opcode::execute series
Changelog v1->v2:
- Dropped push sreg part.
- Covered CMPS and SCAS
- Removed useless emulate_* functions
- Replaced D6ALU macro with I6ALU
- Various rebases.
I also updated the titles and changelogs to be consistent with Avi's recent patches.
Takuya
PATCH v2 1/4 KVM: x86 emulator: Use opcode::execute for Group 1, CMPS and SCAS
From: Takuya Yoshikawa <[email protected]>
The following instructions are changed to use opcode::execute.
Group 1 (80-83)
ADD (00-05), OR (08-0D), ADC (10-15), SBB (18-1D), AND (20-25), SUB (28-2D), XOR (30-35), CMP (38-3D)
CMPS (A6-A7), SCAS (AE-AF)
The last two do the same as CMP in the emulator, so em_cmp() is used.
--- arch/x86/kvm/emulate.c | 161 ++++++++++++++++++++++++++--------------------- 1 files changed, 89 insertions(+), 72 deletions(-)
arch/x86/kvm/emulate.c +89 -72
@@ -2512,6 +2512,72 @@ static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_add (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("add" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_or (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("or" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_adc (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("adc" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_sbb (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("sbb" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_and (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("and" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_sub (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("sub" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_xor (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("xor" , c->src, c->dst, ctxt->eflags); +return X86EMUL_CONTINUE ; +} + +static int em_cmp (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +emulate_2op_SrcV ("cmp" , c->src, c->dst, ctxt->eflags); +/* Disable writeback. */ + c->dst.type =OP_NONE ; +return X86EMUL_CONTINUE ; +} +static int em_imul (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;
@@ -2808,9 +2874,9 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)#define D2bvIP (_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)#define I2bv (_f, _e) I((_f) | ByteOp, _e), I(_f, _e) -#define D6ALU (_f) D2bv((_f) | DstMem | SrcReg | ModRM), \ - D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ - D2bv(((_f) & ~Lock) | DstAcc | SrcImm) +#define I6ALU (_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \ + I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ + I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)static struct opcode group7_rm1[] = {DI (SrcNone | ModRM | Priv, monitor),
@@ -2834,8 +2900,16 @@ static struct opcode group7_rm7[] = {DIP (SrcNone | ModRM, rdtscp, check_rdtsc), N, N, N, N, N, N, }; +static struct opcode group1[] = { -X7 (D (Lock)),N +I (Lock, em_add), +I (Lock, em_or), +I (Lock, em_adc), +I (Lock, em_sbb), +I (Lock, em_and), +I (Lock, em_sub), +I (Lock, em_xor), +I (0 , em_cmp), }; static struct opcode group1A[] = {
@@ -2905,25 +2979,25 @@ static struct gprefix pfx_0f_6f_0f_7f = {static struct opcode opcode_table[256 ] = {/* 0x00 - 0x07 */ -D6ALU (Lock), +I6ALU (Lock, em_add),D (ImplicitOps | Stack | No64),D (ImplicitOps | Stack | No64),/* 0x08 - 0x0F */ -D6ALU (Lock), +I6ALU (Lock, em_or),D (ImplicitOps | Stack | No64),N ,/* 0x10 - 0x17 */ -D6ALU (Lock), +I6ALU (Lock, em_adc),D (ImplicitOps | Stack | No64),D (ImplicitOps | Stack | No64),/* 0x18 - 0x1F */ -D6ALU (Lock), +I6ALU (Lock, em_sbb),D (ImplicitOps | Stack | No64),D (ImplicitOps | Stack | No64),/* 0x20 - 0x27 */ -D6ALU (Lock),N ,N , +I6ALU (Lock, em_and),N ,N ,/* 0x28 - 0x2F */ -D6ALU (Lock),N ,I (ByteOp | DstAcc | No64, em_das), +I6ALU (Lock, em_sub),N ,I (ByteOp | DstAcc | No64, em_das),/* 0x30 - 0x37 */ -D6ALU (Lock),N ,N , +I6ALU (Lock, em_xor),N ,N ,/* 0x38 - 0x3F */ -D6ALU (0 ),N ,N , +I6ALU (0 , em_cmp),N ,N ,/* 0x40 - 0x4F */ X16 (D (DstReg)),/* 0x50 - 0x57 */
@@ -2964,12 +3038,12 @@ static struct opcode opcode_table[256] = {I2bv (DstAcc | SrcMem | Mov | MemAbs, em_mov),I2bv (DstMem | SrcAcc | Mov | MemAbs, em_mov),I2bv (SrcSI | DstDI | Mov | String, em_mov), -D2bv (SrcSI | DstDI | String), +I2bv (SrcSI | DstDI | String, em_cmp),/* 0xA8 - 0xAF */ D2bv (DstAcc | SrcImm),I2bv (SrcAcc | DstDI | Mov | String, em_mov),I2bv (SrcSI | DstAcc | Mov | String, em_mov), -D2bv (SrcAcc | DstDI | String), +I2bv (SrcAcc | DstDI | String, em_cmp),/* 0xB0 - 0xB7 */ X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),/* 0xB8 - 0xBF */
@@ -3093,7 +3167,7 @@ static struct opcode twobyte_table[256] = {#undef D2bv#undef D2bvIP#undef I2bv -#undef D6ALU +#undef I6ALUstatic unsigned imm_size (struct decode_cache *c) {
@@ -3630,60 +3704,27 @@ special_insn: goto twobyte_insn;switch (c->b) { - case 0x00 ... 0x05: - add:/* add */ -emulate_2op_SrcV ("add" , c->src, c->dst, ctxt->eflags); -break ;case 0x06 :/* push es */ rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_ES );break ;case 0x07 :/* pop es */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_ES );break ; - case 0x08 ... 0x0d: - or:/* or */ -emulate_2op_SrcV ("or" , c->src, c->dst, ctxt->eflags); -break ;case 0x0e :/* push cs */ rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_CS );break ; - case 0x10 ... 0x15: - adc:/* adc */ -emulate_2op_SrcV ("adc" , c->src, c->dst, ctxt->eflags); -break ;case 0x16 :/* push ss */ rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_SS );break ;case 0x17 :/* pop ss */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_SS );break ; - case 0x18 ... 0x1d: - sbb:/* sbb */ -emulate_2op_SrcV ("sbb" , c->src, c->dst, ctxt->eflags); -break ;case 0x1e :/* push ds */ rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_DS );break ;case 0x1f :/* pop ds */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_DS );break ; - case 0x20 ... 0x25: - and:/* and */ -emulate_2op_SrcV ("and" , c->src, c->dst, ctxt->eflags); -break ; - case 0x28 ... 0x2d: - sub:/* sub */ -emulate_2op_SrcV ("sub" , c->src, c->dst, ctxt->eflags); -break ; - case 0x30 ... 0x35: - xor:/* xor */ -emulate_2op_SrcV ("xor" , c->src, c->dst, ctxt->eflags); -break ; - case 0x38 ... 0x3d: - cmp:/* cmp */ - c->dst.type =OP_NONE ;/* Disable writeback. */ -emulate_2op_SrcV ("cmp" , c->src, c->dst, ctxt->eflags); -break ; case 0x40 ... 0x47: /* inc r16/r32 */emulate_1op ("inc" , c->dst, ctxt->eflags);break ;
@@ -3718,26 +3759,6 @@ special_insn:if (test_cc (c->b, ctxt->eflags))jmp_rel (c, c->src.val);break ; - case 0x80 ... 0x83: /* Grp1 */ -switch (c->modrm_reg) { -case 0 : - goto add; -case 1 : - goto or; -case 2 : - goto adc; -case 3 : - goto sbb; -case 4 : - goto and; -case 5 : - goto sub; -case 6 : - goto xor; -case 7 : - goto cmp; - } -break ; case 0x84 ... 0x85: test:emulate_2op_SrcV ("test" , c->src, c->dst, ctxt->eflags);
@@ -3807,12 +3828,8 @@ special_insn: c->dst.bytes = c->op_bytes; rc =emulate_popf (ctxt, ops, &c->dst.val, c->op_bytes);break ; - case 0xa6 ... 0xa7: /* cmps */ - goto cmp; case 0xa8 ... 0xa9: /* test ax, imm */ goto test; - case 0xae ... 0xaf: /* scas */ - goto cmp; case 0xc0 ... 0xc1:emulate_grp2 (ctxt);break ;
Signature · 1 line
1.7.1
PATCH v2 2/4 KVM: x86 emulator: Use opcode::execute for POP reg (58-5F)
In reply to Takuya YoshikawaFrom: Takuya Yoshikawa <[email protected]>
In addition, the RET emulation is changed to call em_pop() to remove the pop_instruction label.
--- arch/x86/kvm/emulate.c | 16 ++++++++++------ 1 files changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/emulate.c +10 -6
@@ -1400,6 +1400,13 @@ static int emulate_pop(struct x86_emulate_ctxt *ctxt,return rc; } +static int em_pop (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + +return emulate_pop (ctxt, ctxt->ops, &c->dst.val, c->op_bytes); +} +static int emulate_popf (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops,void *dest,int len)
@@ -3003,7 +3010,7 @@ static struct opcode opcode_table[256] = {/* 0x50 - 0x57 */ X8 (I (SrcReg | Stack, em_push)),/* 0x58 - 0x5F */ -X8 (D (DstReg | Stack)), +X8 (I (DstReg | Stack, em_pop)),/* 0x60 - 0x67 */ D (ImplicitOps | Stack | No64),D (ImplicitOps | Stack | No64), N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
@@ -3731,10 +3738,6 @@ special_insn: case 0x48 ... 0x4f: /* dec r16/r32 */emulate_1op ("dec" , c->dst, ctxt->eflags);break ; - case 0x58 ... 0x5f: /* pop reg */ - pop_instruction: - rc =emulate_pop (ctxt, ops, &c->dst.val, c->op_bytes); -break ;case 0x60 :/* pusha */ rc =emulate_pusha (ctxt);break ;
@@ -3837,7 +3840,8 @@ special_insn: c->dst.type =OP_REG ; c->dst.addr.reg = &c->eip; c->dst.bytes = c->op_bytes; - goto pop_instruction; + rc =em_pop (ctxt); +break ;case 0xc4 :/* les */ rc =emulate_load_segment (ctxt, ops,VCPU_SREG_ES );break ;
Signature · 1 line
1.7.1
PATCH v2 3/4 KVM: x86 emulator: Use opcode::execute for PUSHA/POPA (60/61)
In reply to Takuya YoshikawaFrom: Takuya Yoshikawa <[email protected]>
For this, emulate_pusha/popa() are converted to em_pusha/popa().
--- arch/x86/kvm/emulate.c | 16 +++++----------- 1 files changed, 5 insertions(+), 11 deletions(-)
arch/x86/kvm/emulate.c +5 -11
@@ -1473,7 +1473,7 @@ static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,return rc; } -static int emulate_pusha (struct x86_emulate_ctxt *ctxt) +static int em_pusha (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;unsigned long old_esp = c->regs[VCPU_REGS_RSP ];
@@ -1494,8 +1494,7 @@ static int emulate_pusha(struct x86_emulate_ctxt *ctxt)return rc; } -static int emulate_popa (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops) +static int em_popa (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;int rc =X86EMUL_CONTINUE ;
@@ -1508,7 +1507,7 @@ static int emulate_popa(struct x86_emulate_ctxt *ctxt, --reg; } - rc =emulate_pop (ctxt, ops, &c->regs[reg], c->op_bytes); + rc =emulate_pop (ctxt, ctxt->ops, &c->regs[reg], c->op_bytes);if (rc !=X86EMUL_CONTINUE )break ; --reg;
@@ -3012,7 +3011,8 @@ static struct opcode opcode_table[256] = {/* 0x58 - 0x5F */ X8 (I (DstReg | Stack, em_pop)),/* 0x60 - 0x67 */ -D (ImplicitOps | Stack | No64),D (ImplicitOps | Stack | No64), +I (ImplicitOps | Stack | No64, em_pusha), +I (ImplicitOps | Stack | No64, em_popa),N ,D (DstReg | SrcMem32 | ModRM | Mov)/* movsxd (x86/64) */ , N, N, N, N,/* 0x68 - 0x6F */
@@ -3738,12 +3738,6 @@ special_insn: case 0x48 ... 0x4f: /* dec r16/r32 */emulate_1op ("dec" , c->dst, ctxt->eflags);break ; -case 0x60 :/* pusha */ - rc =emulate_pusha (ctxt); -break ; -case 0x61 :/* popa */ - rc =emulate_popa (ctxt, ops); -break ;case 0x63 :/* movsxd */ if (ctxt->mode !=X86EMUL_MODE_PROT64 ) goto cannot_emulate;
Signature · 1 line
1.7.1
PATCH v2 4/4 KVM: x86 emulator: Use opcode::execute for PUSHF/POPF (9C/9D)
In reply to Takuya YoshikawaFrom: Takuya Yoshikawa <[email protected]>
For this, em_pushf/popf() are introduced.
--- arch/x86/kvm/emulate.c | 31 ++++++++++++++++++++----------- 1 files changed, 20 insertions(+), 11 deletions(-)
arch/x86/kvm/emulate.c +20 -11
@@ -1448,6 +1448,16 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt,return rc; } +static int em_popf (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + + c->dst.type =OP_REG ; + c->dst.addr.reg = &ctxt->eflags; + c->dst.bytes = c->op_bytes; +return emulate_popf (ctxt, ctxt->ops, &c->dst.val, c->op_bytes); +} +static int emulate_push_sreg (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops,int seg) {
@@ -1494,6 +1504,14 @@ static int em_pusha(struct x86_emulate_ctxt *ctxt)return rc; } +static int em_pushf (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; + + c->src.val = (unsigned long )ctxt->eflags; +return em_push (ctxt); +} +static int em_popa (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;
@@ -3040,7 +3058,8 @@ static struct opcode opcode_table[256] = {/* 0x98 - 0x9F */ D (DstAcc | SrcNone),I (ImplicitOps | SrcAcc, em_cwd),I (SrcImmFAddr | No64, em_call_far),N , -DI (ImplicitOps | Stack, pushf),DI (ImplicitOps | Stack, popf),N ,N , +II (ImplicitOps | Stack, em_pushf, pushf), +II (ImplicitOps | Stack, em_popf, popf),N ,N ,/* 0xA0 - 0xA7 */ I2bv (DstAcc | SrcMem | Mov | MemAbs, em_mov), I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
@@ -3815,16 +3834,6 @@ special_insn:case 8 : c->dst.val = (s32 )c->dst.val;break ; }break ; -case 0x9c :/* pushf */ - c->src.val = (unsigned long ) ctxt->eflags; - rc =em_push (ctxt); -break ; -case 0x9d :/* popf */ - c->dst.type =OP_REG ; - c->dst.addr.reg = &ctxt->eflags; - c->dst.bytes = c->op_bytes; - rc =emulate_popf (ctxt, ops, &c->dst.val, c->op_bytes); -break ; case 0xa8 ... 0xa9: /* test ax, imm */ goto test; case 0xc0 ... 0xc1:
Signature · 1 line
1.7.1
Avi Kivity
AppliedQuoted 10 lines · Takuya Yoshikawa
Changelog
v1->v2:
- Dropped push sreg part.
- Covered CMPS and SCAS
- Removed useless emulate_* functions
- Replaced D6ALU macro with I6ALU
- Various rebases.I also updated the titles and changelogs to be consistent with Avi's recent patches.
Applied, thanks.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.