KVM: x86 emulator: Disable writeback for CMP emulation
PATCH v2 1/3 KVM: x86 emulator: Disable writeback for CMP emulation
From: Takuya Yoshikawa <[email protected]>
This stops "CMP r/m, reg" to write back the data into memory. Pointed out by Avi.
The writeback suppression now covers CMP, CMPS, SCAS.
---
Changelog v1->v2: moved the line just after the cmp label.
arch/x86/kvm/emulate.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +1 -1
@@ -3671,6 +3671,7 @@ special_insn: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 */
@@ -3797,7 +3798,6 @@ special_insn: rc =emulate_popf (ctxt, ops, &c->dst.val, c->op_bytes);break ; case 0xa6 ... 0xa7: /* cmps */ - c->dst.type =OP_NONE ;/* Disable writeback. */ goto cmp; case 0xa8 ... 0xa9: /* test ax, imm */ goto test;
Signature · 1 line
1.7.1
PATCH v2 2/3 KVM: x86 emulator: Make emulate_push() store the value directly
From: Takuya Yoshikawa <[email protected]>
PUSH emulation stores the value by calling writeback() after setting the dst operand appropriately in emulate_push().
This writeback() using dst is not needed at all because we know the target is the stack. So this patch makes emulate_push() call, newly introduced, segmented_write() directly.
By this, many inlined writeback()'s are removed.
---
Changelog v1->v2: used segmented_write().
arch/x86/kvm/emulate.c | 87 +++++++++++++++++++----------------------------- 1 files changed, 34 insertions(+), 53 deletions(-)
arch/x86/kvm/emulate.c +34 -53
@@ -1345,17 +1345,19 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; } -static inline void emulate_push (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops) +static int emulate_push (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode; +struct segmented_address addr; - c->dst.type =OP_MEM ; - c->dst.bytes = c->op_bytes; - c->dst.val = c->src.val;register_address_increment (c, &c->regs[VCPU_REGS_RSP ], -c->op_bytes); - c->dst.addr.mem.ea =register_address (c, c->regs[VCPU_REGS_RSP ]); - c->dst.addr.mem.seg =VCPU_SREG_SS ; + addr.ea =register_address (c, c->regs[VCPU_REGS_RSP ]); + addr.seg =VCPU_SREG_SS ; + +/* Disable writeback. */ + c->dst.type =OP_NONE ; +return segmented_write (ctxt, addr, &c->src.val, c->op_bytes); } static int emulate_pop(struct x86_emulate_ctxt *ctxt,
@@ -1417,14 +1419,14 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt,return rc; } -static void emulate_push_sreg (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops,int seg) +static int emulate_push_sreg (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops,int seg) {struct decode_cache *c = &ctxt->decode; c->src.val = ops->get_segment_selector (seg, ctxt->vcpu); -emulate_push (ctxt, ops); +return emulate_push (ctxt, ops); } static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
@@ -1454,18 +1456,13 @@ static int emulate_pusha(struct x86_emulate_ctxt *ctxt, (reg ==VCPU_REGS_RSP ) ? (c->src.val = old_esp) : (c->src.val = c->regs[reg]); -emulate_push (ctxt, ops); - - rc =writeback (ctxt, ops); + rc =emulate_push (ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc; ++reg; } -/* Disable writeback. */ - c->dst.type =OP_NONE ; -return rc; }
@@ -1503,27 +1500,22 @@ int emulate_int_real(struct x86_emulate_ctxt *ctxt,/* TODO: Add limit checks */ c->src.val = ctxt->eflags; -emulate_push (ctxt, ops); - rc =writeback (ctxt, ops); + rc =emulate_push (ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc; ctxt->eflags &= ~(EFLG_IF |EFLG_TF |EFLG_AC ); c->src.val = ops->get_segment_selector (VCPU_SREG_CS , ctxt->vcpu); -emulate_push (ctxt, ops); - rc =writeback (ctxt, ops); + rc =emulate_push (ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc; c->src.val = c->eip; -emulate_push (ctxt, ops); - rc =writeback (ctxt, ops); + rc =emulate_push (ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc; - c->dst.type =OP_NONE ; - ops->get_idt (&dt, ctxt->vcpu); eip_addr = dt.address + (irq <<2 );
@@ -1713,6 +1705,7 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode; +int rc =X86EMUL_CONTINUE ;switch (c->modrm_reg) {case 0 :/* inc */
@@ -1726,17 +1719,17 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, old_eip = c->eip; c->eip = c->src.val; c->src.val = old_eip; -emulate_push (ctxt, ops); + rc =emulate_push (ctxt, ops);break ; }case 4 :/* jmp abs */ c->eip = c->src.val;break ;case 6 :/* push */ -emulate_push (ctxt, ops); + rc =emulate_push (ctxt, ops);break ; } -return X86EMUL_CONTINUE ; +return rc; } static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
@@ -2380,7 +2373,7 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, c->op_bytes = c->ad_bytes = (next_tss_desc.type &8 ) ?4 :2 ; c->lock_prefix =0 ; c->src.val = (unsigned long ) error_code; -emulate_push (ctxt, ops); + ret =emulate_push (ctxt, ops); }return ret;
@@ -2400,11 +2393,8 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, rc =emulator_do_task_switch (ctxt, ops, tss_selector, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) { - rc =writeback (ctxt, ops); -if (rc ==X86EMUL_CONTINUE ) - ctxt->eip = c->eip; - } +if (rc ==X86EMUL_CONTINUE ) + ctxt->eip = c->eip;return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }
@@ -2422,8 +2412,7 @@ static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,static int em_push (struct x86_emulate_ctxt *ctxt) { -emulate_push (ctxt, ctxt->ops); -return X86EMUL_CONTINUE ; +return emulate_push (ctxt, ctxt->ops); }static int em_das (struct x86_emulate_ctxt *ctxt)
@@ -2483,20 +2472,12 @@ static int em_call_far(struct x86_emulate_ctxt *ctxt)memcpy (&c->eip, c->src.valptr, c->op_bytes); c->src.val = old_cs; -emulate_push (ctxt, ctxt->ops); - rc =writeback (ctxt, ctxt->ops); + rc =emulate_push (ctxt, ctxt->ops);if (rc !=X86EMUL_CONTINUE )return rc; c->src.val = old_eip; -emulate_push (ctxt, ctxt->ops); - rc =writeback (ctxt, ctxt->ops); -if (rc !=X86EMUL_CONTINUE ) -return rc; - - c->dst.type =OP_NONE ; - -return X86EMUL_CONTINUE ; +return emulate_push (ctxt, ctxt->ops); }static int em_ret_near_imm (struct x86_emulate_ctxt *ctxt)
@@ -3625,7 +3606,7 @@ special_insn:emulate_2op_SrcV ("add" , c->src, c->dst, ctxt->eflags);break ;case 0x06 :/* push es */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_ES ); + rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_ES );break ;case 0x07 :/* pop es */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_ES );
@@ -3635,14 +3616,14 @@ special_insn:emulate_2op_SrcV ("or" , c->src, c->dst, ctxt->eflags);break ;case 0x0e :/* push cs */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_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 */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_SS ); + rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_SS );break ;case 0x17 :/* pop ss */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_SS );
@@ -3652,7 +3633,7 @@ special_insn:emulate_2op_SrcV ("sbb" , c->src, c->dst, ctxt->eflags);break ;case 0x1e :/* push ds */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_DS ); + rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_DS );break ;case 0x1f :/* pop ds */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_DS );
@@ -3789,7 +3770,7 @@ special_insn:break ;case 0x9c :/* pushf */ c->src.val = (unsigned long ) ctxt->eflags; -emulate_push (ctxt, ops); + rc =emulate_push (ctxt, ops);break ;case 0x9d :/* popf */ c->dst.type =OP_REG ;
@@ -3864,7 +3845,7 @@ special_insn:long int rel = c->src.val; c->src.val = (unsigned long ) c->eip;jmp_rel (c, rel); -emulate_push (ctxt, ops); + rc =emulate_push (ctxt, ops);break ; }case 0xe9 :/* jmp rel */
@@ -4157,7 +4138,7 @@ twobyte_insn: c->dst.val =test_cc (c->b, ctxt->eflags);break ;case 0xa0 :/* push fs */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_FS ); + rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_FS );break ;case 0xa1 :/* pop fs */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_FS );
@@ -4174,7 +4155,7 @@ twobyte_insn:emulate_2op_cl ("shld" , c->src2, c->src, c->dst, ctxt->eflags);break ;case 0xa8 :/* push gs */ -emulate_push_sreg (ctxt, ops,VCPU_SREG_GS ); + rc =emulate_push_sreg (ctxt, ops,VCPU_SREG_GS );break ;case 0xa9 :/* pop gs */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_GS );
Signature · 1 line
1.7.1
PATCH 3/3 KVM: x86 emulator: Use em_push() instead of emulate_push()
In reply to Takuya YoshikawaFrom: Takuya Yoshikawa <[email protected]>
em_push() is a simple wrapper of emulate_push(). So this patch replaces emulate_push() with em_push() and removes the unnecessary former.
In addition, the unused ops arguments are removed from emulate_pusha() and emulate_grp45().
--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++------------------------- 1 files changed, 17 insertions(+), 25 deletions(-)
arch/x86/kvm/emulate.c +17 -25
@@ -1345,8 +1345,7 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; } -static int emulate_push (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops) +static int em_push (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;struct segmented_address addr;
@@ -1426,7 +1425,7 @@ static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, c->src.val = ops->get_segment_selector (seg, ctxt->vcpu); -return emulate_push (ctxt, ops); +return em_push (ctxt); } static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
@@ -1444,8 +1443,7 @@ static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,return rc; } -static int emulate_pusha (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops) +static int emulate_pusha (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;unsigned long old_esp = c->regs[VCPU_REGS_RSP ];
@@ -1456,7 +1454,7 @@ static int emulate_pusha(struct x86_emulate_ctxt *ctxt, (reg ==VCPU_REGS_RSP ) ? (c->src.val = old_esp) : (c->src.val = c->regs[reg]); - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )return rc;
@@ -1500,19 +1498,19 @@ int emulate_int_real(struct x86_emulate_ctxt *ctxt,/* TODO: Add limit checks */ c->src.val = ctxt->eflags; - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )return rc; ctxt->eflags &= ~(EFLG_IF |EFLG_TF |EFLG_AC ); c->src.val = ops->get_segment_selector (VCPU_SREG_CS , ctxt->vcpu); - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )return rc; c->src.val = c->eip; - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )return rc;
@@ -1701,8 +1699,7 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; } -static inline int emulate_grp45 (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops) +static int emulate_grp45 (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;int rc =X86EMUL_CONTINUE ;
@@ -1719,14 +1716,14 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, old_eip = c->eip; c->eip = c->src.val; c->src.val = old_eip; - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);break ; }case 4 :/* jmp abs */ c->eip = c->src.val;break ;case 6 :/* push */ - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);break ; }return rc;
@@ -2373,7 +2370,7 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, c->op_bytes = c->ad_bytes = (next_tss_desc.type &8 ) ?4 :2 ; c->lock_prefix =0 ; c->src.val = (unsigned long ) error_code; - ret =emulate_push (ctxt, ops); + ret =em_push (ctxt); }return ret;
@@ -2410,11 +2407,6 @@ static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg, op->addr.mem.seg = seg; } -static int em_push (struct x86_emulate_ctxt *ctxt) -{ -return emulate_push (ctxt, ctxt->ops); -} -static int em_das (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode;
@@ -2472,12 +2464,12 @@ static int em_call_far(struct x86_emulate_ctxt *ctxt)memcpy (&c->eip, c->src.valptr, c->op_bytes); c->src.val = old_cs; - rc =emulate_push (ctxt, ctxt->ops); + rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )return rc; c->src.val = old_eip; -return emulate_push (ctxt, ctxt->ops); +return em_push (ctxt); }static int em_ret_near_imm (struct x86_emulate_ctxt *ctxt)
@@ -3666,7 +3658,7 @@ special_insn: rc =emulate_pop (ctxt, ops, &c->dst.val, c->op_bytes);break ;case 0x60 :/* pusha */ - rc =emulate_pusha (ctxt, ops); + rc =emulate_pusha (ctxt);break ;case 0x61 :/* popa */ rc =emulate_popa (ctxt, ops);
@@ -3770,7 +3762,7 @@ special_insn:break ;case 0x9c :/* pushf */ c->src.val = (unsigned long ) ctxt->eflags; - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);break ;case 0x9d :/* popf */ c->dst.type =OP_REG ;
@@ -3845,7 +3837,7 @@ special_insn:long int rel = c->src.val; c->src.val = (unsigned long ) c->eip;jmp_rel (c, rel); - rc =emulate_push (ctxt, ops); + rc =em_push (ctxt);break ; }case 0xe9 :/* jmp rel */
@@ -3923,7 +3915,7 @@ special_insn:break ;case 0xfe :/* Grp4 */ grp45: - rc =emulate_grp45 (ctxt, ops); + rc =emulate_grp45 (ctxt);break ;case 0xff :/* Grp5 */ if (c->modrm_reg ==5 )
Signature · 1 line
1.7.1
Quoted 7 lines · Takuya Yoshikawa
From: Takuya Yoshikawa<[email protected]>
This stops "CMP r/m, reg" to write back the data into memory. Pointed out by Avi.
The writeback suppression now covers CMP, CMPS, SCAS.
Patchset looks good, nice cleanup.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 11 lines · Avi Kivity
Quoted 7 lines · Takuya Yoshikawa
From: Takuya Yoshikawa<[email protected]>
This stops "CMP r/m, reg" to write back the data into memory. Pointed out by Avi.
The writeback suppression now covers CMP, CMPS, SCAS.
Patchset looks good, nice cleanup.
Now applied, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function