KVM: x86 emulator: access GPRs on demand
PATCH v3 1/1 KVM: x86 emulator: access GPRs on demand
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
---
v3:
fix misplaced parentheses in em_loop() and em_jcxz(), unbreaking those instructions.
v2:
add APIs for managing the register cache. This reduces the potential for confusion
between ctxt->regs_dirty and vcpu->arch.regs_dirty.
move cache management to the entry points
add missing writebacks to int and task switch emulation
arch/x86/include/asm/kvm_emulate.h | 20 ++- arch/x86/kvm/emulate.c | 305 ++++++++++++++++++++++--------------- arch/x86/kvm/x86.c | 45 +++--- 3 files changed, 223 insertions(+), 147 deletions(-)
arch/x86/include/asm/kvm_emulate.h +19 -1
@@ -86,6 +86,19 @@ struct x86_instruction_info {struct x86_emulate_ops {/* +* read_gpr: read a general purpose register (rax - r15) +* +* @reg: gpr number. +*/ +ulong (*read_gpr)(struct x86_emulate_ctxt *ctxt,unsigned reg); +/* +* write_gpr: write a general purpose register (rax - r15) +* +* @reg: gpr number. +* @val: value to write. +*/ +void (*write_gpr)(struct x86_emulate_ctxt *ctxt,unsigned reg,ulong val); +/* * read_std: Read bytes of standard (non-emulated/special) memory. * Used for descriptor reading. * @addr: [IN ] Linear address from which to read.
@@ -281,8 +294,10 @@ struct x86_emulate_ctxt {bool rip_relative;unsigned long _eip;struct operand memop; +u32 regs_valid;/* bitmaps of registers in _regs[] that can be read */ +u32 regs_dirty;/* bitmaps of registers in _regs[] that have been written */ /* Fields above regs are cleared together. */ -unsigned long regs[NR_VCPU_REGS ]; +unsigned long _regs[NR_VCPU_REGS ];struct operand *memopp;struct fetch_cache fetch;struct read_cache io_read;
@@ -394,4 +409,7 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, u16 tss_selector, int idt_index, int reason, bool has_error_code, u32 error_code);int emulate_int_real (struct x86_emulate_ctxt *ctxt,int irq); +void emulator_invalidate_register_cache (struct x86_emulate_ctxt *ctxt); +void emulator_writeback_register_cache (struct x86_emulate_ctxt *ctxt); +#endif /* _ASM_X86_KVM_X86_EMULATE_H */
arch/x86/kvm/emulate.c +186 -119
680 lines not on this page · raw message
arch/x86/kvm/x86.c +18 -27
114 lines not on this page · raw message
Signature · 1 line
1.7.11.3
Quoted 33 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
Signed-off-by Avi Kivity <[email protected]>---
v3:
fix misplaced parentheses inem_loop()andem_jcxz(), unbreaking those instructions.v2:
add APIs for managing the register cache. This reduces the potential for confusionbetween ctxt->regs_dirty and vcpu->arch.regs_dirty.move cache management to the entry points
add missing writebacks to int and task switch emulationarch/x86/include/asm/kvm_emulate.h | 20 ++- arch/x86/kvm/emulate.c | 305 ++++++++++++++++++++++--------------- arch/x86/kvm/x86.c | 45 +++--- 3 files changed, 223 insertions(+), 147 deletions(-)arch/x86/kvm/emulate.c +1
-0@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 10 lines
arch/x86/kvm/emulate.c +3
-1has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }
No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Maybe move invalidate() to init_emulate_ctxt?
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }
Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a memset() that clears everything up to _regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose the memset() or not, it is rather efficient.
Quoted 6 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Maybe move
invalidate()toinit_emulate_ctxt?
See the memset() in init_decode_cache().
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 41 lines · Avi Kivity
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a
memset()that clears everything up to_regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose thememset()or not, it is rather efficient.Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Maybe move
invalidate()toinit_emulate_ctxt?See the
memset()ininit_decode_cache().
Right. Applied, thanks.
Quoted 45 lines · Marcelo Tosatti
Quoted 44 lines · Avi Kivity
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a
memset()that clears everything up to_regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose thememset()or not, it is rather efficient.Quoted 4 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Maybe move
invalidate()toinit_emulate_ctxt?See the
memset()ininit_decode_cache().Right. Applied, thanks.
Actually, had to revert because autotest was failing.
Now it rejects:
4 out of 49 hunks FAILED -- saving rejects to file arch/x86/kvm/emulate.c.rej
Please regenerate.
Quoted 46 lines · Marcelo Tosatti
Quoted 45 lines · Marcelo Tosatti
Quoted 44 lines · Avi Kivity
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a
memset()that clears everything up to_regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose thememset()or not, it is rather efficient.Quoted 4 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Maybe move
invalidate()toinit_emulate_ctxt?See the
memset()ininit_decode_cache().Right. Applied, thanks.
Actually, had to revert because autotest was failing.
Was it failing because of this patch? Or what?
Quoted 7 lines
Now it rejects:
4 out of 49 hunks FAILED -- saving rejects to file
arch/x86/kvm/emulate.c.rejPlease regenerate.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
Quoted 50 lines · Avi Kivity
Quoted 49 lines · Marcelo Tosatti
Quoted 48 lines · Marcelo Tosatti
Quoted 47 lines · Avi Kivity
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a
memset()that clears everything up to_regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose thememset()or not, it is rather efficient.Quoted 6 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Maybe move
invalidate()toinit_emulate_ctxt?
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
See the
memset()ininit_decode_cache().Right. Applied, thanks.
Actually, had to revert because autotest was failing.
Was it failing because of this patch? Or what?
I see, the rsp mask fix.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
Quoted 50 lines · Avi Kivity
Quoted 49 lines · Marcelo Tosatti
Quoted 48 lines · Marcelo Tosatti
Quoted 47 lines · Avi Kivity
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Avi Kivity
Instead of populating the the entire register file, read in registers as they are accessed, and write back only the modified ones. This saves a VMREAD and VMWRITE on Intel (for rsp, since it is not usually used during emulation), and a two 128-byte copies for the registers.
arch/x86/kvm/emulate.c +4
-1@@ -2715,14 +2764,17 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, {int rc; +invalidate_registers (ctxt); ctxt->_eip = ctxt->eip; ctxt->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (rc ==X86EMUL_CONTINUE ) +if (rc ==X86EMUL_CONTINUE ) { ctxt->eip = ctxt->_eip; +writeback_registers (ctxt); + }Quoted 8 lines
arch/x86/kvm/emulate.c int emulator_task_switch(struct x86_emulate_ctxt *ctxt, · line 2780
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; }No clear point when emulator register cache is active, when it is not (AFAICS this patch does not invalidate registers on emulation start (the above being one of the exceptions) does not clear valid bit on writeback-to-vcpu-cache on emulation exit).
It is cleared when emulation starts. For the non-insn-emulation entry points, there is an explicit invalidate. For the emulation entry point, there is a
memset()that clears everything up to_regs, which includes the cache. This discrepancy isn't nice, but it preexists. I don't know whether we should decompose thememset()or not, it is rather efficient.Quoted 6 lines
Concern is that emulator can start with cached registers marked as valid but in fact are invalid from previous emulation round.
Maybe move
invalidate()toinit_emulate_ctxt?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
See the
memset()ininit_decode_cache().Right. Applied, thanks.
Actually, had to revert because autotest was failing.
Was it failing because of this patch? Or what?
No, due to lack of stack size fix.
With the fix, it rejects. About to merge the big real mode patchset, so its not a bad idea to wait for that before resending.
Quoted 3 lines · Marcelo Tosatti
With the fix, it rejects. About to merge the big real mode patchset, so its not a bad idea to wait for that before resending.
Nevermind this sentence.