Emulator exception improvements
cover Emulator exception improvements
This boring patchset introduces a struct x86_exception to represent an exception, and uses it to communicate between the emulator and the rest of kvm. The primary benefit is that we can now pass a #GP from kvm into the emulator, not just #PFs.
I'd like to also fold vcpu->arch.fault into struct x86_exception, but that's more difficult, so deferred until later.
Avi Kivity (7):
KVM: x86 emulator: introduce struct x86_exception to communicate
faults
KVM: x86 emulator: make emulator memory callbacks return full
exception
KVM: x86 emulator: drop dead pf injection in emulate_popf()
KVM: x86 emulator: tighen up ->read_std() and ->write_std() error
checks
KVM: x86 emulator: simplify exception generation
KVM: Push struct x86_exception info the various gva_to_gpa variants
KVM: Push struct x86_exception into walk_addr()
arch/x86/include/asm/kvm_emulate.h | 26 +++-- arch/x86/include/asm/kvm_host.h | 14 ++- arch/x86/kvm/emulate.c | 243 ++++++++++++++---------------------- arch/x86/kvm/mmu.c | 13 +- arch/x86/kvm/paging_tmpl.h | 31 +++-- arch/x86/kvm/x86.c | 97 ++++++++------- 6 files changed, 195 insertions(+), 229 deletions(-)
PATCH 1/7 KVM: x86 emulator: introduce struct x86_exception to communicate faults
Introduce a structure that can contain an exception to be passed back to main kvm code.
--- arch/x86/include/asm/kvm_emulate.h | 11 ++++++++--- arch/x86/kvm/emulate.c | 26 +++++++++++++++++++++++--- arch/x86/kvm/x86.c | 13 +++++++------ 3 files changed, 38 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_emulate.h +8 -3
@@ -15,6 +15,12 @@struct x86_emulate_ctxt ; +struct x86_exception { +u8 vector; +bool error_code_valid; +u16 error_code; +}; +/* * x86_emulate_ops: *
@@ -229,9 +235,8 @@ struct x86_emulate_ctxt {bool perm_ok;/* do not check permissions if true */ -int exception;/* exception that happens during emulation or -1 */ -u32 error_code;/* error code for exception */ -bool error_code_valid; +bool have_exception; +struct x86_exception exception;/* decode cache */ struct decode_cache decode;
arch/x86/kvm/emulate.c +23 -3
@@ -469,9 +469,9 @@ static ulong linear(struct x86_emulate_ctxt *ctxt,static void emulate_exception (struct x86_emulate_ctxt *ctxt,int vec,u32 error,bool valid) { - ctxt->exception = vec; - ctxt->error_code = error; - ctxt->error_code_valid = valid; + ctxt->exception.vector = vec; + ctxt->exception.error_code = error; + ctxt->exception.error_code_valid = valid; }static void emulate_gp (struct x86_emulate_ctxt *ctxt,int err)
@@ -3015,23 +3015,27 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)if (ctxt->mode ==X86EMUL_MODE_PROT64 && (c->d & No64)) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }/* LOCK prefix is allowed only with some instructions */ if (c->lock_prefix && (!(c->d & Lock) || c->dst.type !=OP_MEM )) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }if ((c->d & SrcMask) == SrcMemFAddr && c->src.type !=OP_MEM ) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }/* Privileged instruction can be executed only in CPL=0 */ if ((c->d & Priv) && ops->cpl (ctxt->vcpu)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }
@@ -3210,6 +3214,7 @@ special_insn:case 0x8c :/* mov r/m, sreg */ if (c->modrm_reg >VCPU_SREG_GS ) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } c->dst.val = ops->get_segment_selector (c->modrm_reg, ctxt->vcpu);
@@ -3225,6 +3230,7 @@ special_insn:if (c->modrm_reg ==VCPU_SREG_CS || c->modrm_reg >VCPU_SREG_GS ) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }
@@ -3357,6 +3363,7 @@ special_insn: c->dst.bytes =min (c->dst.bytes,4u );if (!emulator_io_permited (ctxt, ops, c->src.val, c->dst.bytes)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
@@ -3371,6 +3378,7 @@ special_insn:if (!emulator_io_permited (ctxt, ops, c->dst.val, c->src.bytes)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } ops->pio_out_emulated(c->src.bytes, c->dst.val,
@@ -3396,6 +3404,7 @@ special_insn:case 0xfa :/* cli */ if (emulator_bad_iopl (ctxt, ops)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }else ctxt->eflags &= ~X86_EFLAGS_IF ;
@@ -3403,6 +3412,7 @@ special_insn:case 0xfb :/* sti */ if (emulator_bad_iopl (ctxt, ops)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }else { ctxt->interruptibility =KVM_X86_SHADOW_INT_STI ;
@@ -3475,6 +3485,8 @@ writeback: ctxt->eip = c->eip; done: +if (rc ==X86EMUL_PROPAGATE_FAULT ) + ctxt->have_exception = true;return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; twobyte_insn:
@@ -3537,6 +3549,7 @@ twobyte_insn:break ;case 5 :/* not defined */ emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done;case 7 :/* invlpg*/ emulate_invlpg(ctxt->vcpu,
@@ -3567,6 +3580,7 @@ twobyte_insn: case 5 ... 7: case 9 ... 15:emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } c->dst.val = ops->get_cr (c->modrm_reg, ctxt->vcpu);
@@ -3575,6 +3589,7 @@ twobyte_insn:if ((ops->get_cr (4 , ctxt->vcpu) &X86_CR4_DE ) && (c->modrm_reg ==4 || c->modrm_reg ==5 )) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } ops->get_dr (c->modrm_reg, &c->dst.val, ctxt->vcpu);
@@ -3582,6 +3597,7 @@ twobyte_insn:case 0x22 :/* mov reg, cr */ if (ops->set_cr (c->modrm_reg, c->src.val, ctxt->vcpu)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } c->dst.type =OP_NONE ;
@@ -3590,6 +3606,7 @@ twobyte_insn:if ((ops->get_cr (4 , ctxt->vcpu) &X86_CR4_DE ) && (c->modrm_reg ==4 || c->modrm_reg ==5 )) {emulate_ud (ctxt); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }
@@ -3598,6 +3615,7 @@ twobyte_insn: ~0ULL : ~0U), ctxt->vcpu) < 0) {/* #UD condition is already handled by the code above */ emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }
@@ -3609,6 +3627,7 @@ twobyte_insn: | ((u64)c->regs[VCPU_REGS_RDX] << 32);if (ops->set_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], msr_data)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; } rc =X86EMUL_CONTINUE ;
@@ -3617,6 +3636,7 @@ twobyte_insn:/* rdmsr */ if (ops->get_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], &msr_data)) {emulate_gp (ctxt,0 ); + rc =X86EMUL_PROPAGATE_FAULT ; goto done; }else { c->regs[VCPU_REGS_RAX ] = (u32 )msr_data;
arch/x86/kvm/x86.c +7 -6
@@ -4259,12 +4259,13 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)static void inject_emulated_exception (struct kvm_vcpu *vcpu) {struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; -if (ctxt->exception ==PF_VECTOR ) +if (ctxt->exception.vector ==PF_VECTOR )kvm_propagate_fault (vcpu); -else if (ctxt->error_code_valid) -kvm_queue_exception_e (vcpu, ctxt->exception, ctxt->error_code); +else if (ctxt->exception.error_code_valid) +kvm_queue_exception_e (vcpu, ctxt->exception.vector, + ctxt->exception.error_code);else -kvm_queue_exception (vcpu, ctxt->exception); +kvm_queue_exception (vcpu, ctxt->exception.vector); }static void init_emulate_ctxt (struct kvm_vcpu *vcpu)
@@ -4376,7 +4377,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); vcpu->arch.emulate_ctxt.interruptibility =0 ; - vcpu->arch.emulate_ctxt.exception =-1 ; + vcpu->arch.emulate_ctxt.have_exception = false; vcpu->arch.emulate_ctxt.perm_ok = false; r =x86_decode_insn (&vcpu->arch.emulate_ctxt);
@@ -4442,7 +4443,7 @@ restart: } done: -if (vcpu->arch.emulate_ctxt.exception >=0 ) { +if (vcpu->arch.emulate_ctxt.have_exception) {inject_emulated_exception (vcpu); r =EMULATE_DONE ; }else if (vcpu->arch.pio.count) {
Signature · 1 line
1.7.1
PATCH 2/7 KVM: x86 emulator: make emulator memory callbacks return full exception
In reply to Avi KivityThis way, they can return #GP, not just #PF.
--- arch/x86/include/asm/kvm_emulate.h | 15 ++++-- arch/x86/kvm/emulate.c | 89 ++++++++++++----------------------- arch/x86/kvm/x86.c | 76 ++++++++++++++++++------------- 3 files changed, 84 insertions(+), 96 deletions(-)
arch/x86/include/asm/kvm_emulate.h +9 -6
@@ -70,7 +70,8 @@ struct x86_emulate_ops {* @bytes: [IN ] Number of bytes to read from memory. */ int (*read_std)(unsigned long addr,void *val, -unsigned int bytes,struct kvm_vcpu *vcpu,u32 *error); +unsigned int bytes,struct kvm_vcpu *vcpu, +struct x86_exception *fault);/* * write_std: Write bytes of standard (non-emulated/special) memory.
@@ -80,7 +81,8 @@ struct x86_emulate_ops {* @bytes: [IN ] Number of bytes to write to memory. */ int (*write_std)(unsigned long addr,void *val, -unsigned int bytes,struct kvm_vcpu *vcpu,u32 *error); +unsigned int bytes,struct kvm_vcpu *vcpu, +struct x86_exception *fault);/* * fetch: Read bytes of standard (non-emulated/special) memory. * Used for instruction fetch.
@@ -89,7 +91,8 @@ struct x86_emulate_ops {* @bytes: [IN ] Number of bytes to read from memory. */ int (*fetch)(unsigned long addr,void *val, -unsigned int bytes,struct kvm_vcpu *vcpu,u32 *error); +unsigned int bytes,struct kvm_vcpu *vcpu, +struct x86_exception *fault);/* * read_emulated: Read bytes from emulated/special memory area.
@@ -100,7 +103,7 @@ struct x86_emulate_ops {int (*read_emulated)(unsigned long addr,void *val,unsigned int bytes, -unsigned int *error, +struct x86_exception *fault,struct kvm_vcpu *vcpu);/*
@@ -113,7 +116,7 @@ struct x86_emulate_ops {int (*write_emulated)(unsigned long addr,const void *val,unsigned int bytes, -unsigned int *error, +struct x86_exception *fault,struct kvm_vcpu *vcpu);/*
@@ -128,7 +131,7 @@ struct x86_emulate_ops {const void *old, const void *new, unsigned int bytes, - unsigned int *error, + struct x86_exception *fault, struct kvm_vcpu *vcpu); int (*pio_in_emulated)(int size, unsigned short port, void *val,
arch/x86/kvm/emulate.c +31 -58
231 lines not on this page · raw message
arch/x86/kvm/x86.c +44 -32
199 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 3/7 KVM: x86 emulator: drop dead pf injection in emulate_popf()
In reply to Avi KivityIf rc == X86EMUL_PROPAGATE_FAULT, we would have returned earlier.
--- arch/x86/kvm/emulate.c | 8 -------- 1 files changed, 0 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +0 -8
@@ -479,11 +479,6 @@ static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)emulate_exception (ctxt,GP_VECTOR , err, true); } -static void emulate_pf (struct x86_emulate_ctxt *ctxt) -{ -emulate_exception (ctxt,PF_VECTOR ,0 , true); -} -static void emulate_ud (struct x86_emulate_ctxt *ctxt) {emulate_exception (ctxt,UD_VECTOR ,0 , false);
@@ -1184,9 +1179,6 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt, *(unsigned long *)dest = (ctxt->eflags & ~change_mask) | (val & change_mask); -if (rc ==X86EMUL_PROPAGATE_FAULT ) -emulate_pf (ctxt); -return rc; }
Signature · 1 line
1.7.1
PATCH 4/7 KVM: x86 emulator: tighen up ->read_std() and ->write_std() error checks
In reply to Avi KivityInstead of checking for X86EMUL_PROPAGATE_FAULT, check for any error, making the callers more reliable.
--- arch/x86/kvm/emulate.c | 16 ++++++++-------- 1 files changed, 8 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +8 -8
@@ -1915,7 +1915,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, ret = ops->read_std (old_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret;
@@ -1923,13 +1923,13 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, ret = ops->write_std (old_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret; ret = ops->read_std (new_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret;
@@ -1940,7 +1940,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, &tss_seg.prev_task_link,sizeof tss_seg.prev_task_link, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret; }
@@ -2049,7 +2049,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, ret = ops->read_std (old_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret;
@@ -2057,13 +2057,13 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, ret = ops->write_std (old_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret; ret = ops->read_std (new_tss_base, &tss_seg,sizeof tss_seg, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret;
@@ -2074,7 +2074,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, &tss_seg.prev_task_link,sizeof tss_seg.prev_task_link, ctxt->vcpu, &ctxt->exception); -if (ret ==X86EMUL_PROPAGATE_FAULT ) +if (ret !=X86EMUL_CONTINUE )/* FIXME: need to provide precise fault address */ return ret; }
Signature · 1 line
1.7.1
PATCH 5/7 KVM: x86 emulator: simplify exception generation
In reply to Avi KivityImmediately after we generate an exception, we want a X86EMUL_PROPAGATE_FAULT constant, so return it from the generation functions.
--- arch/x86/kvm/emulate.c | 140 +++++++++++++++++------------------------------- 1 files changed, 50 insertions(+), 90 deletions(-)
arch/x86/kvm/emulate.c +50 -90
@@ -466,33 +466,33 @@ static ulong linear(struct x86_emulate_ctxt *ctxt,return la; } -static void emulate_exception (struct x86_emulate_ctxt *ctxt,int vec, -u32 error,bool valid) +static int emulate_exception (struct x86_emulate_ctxt *ctxt,int vec, +u32 error,bool valid) { ctxt->exception.vector = vec; ctxt->exception.error_code = error; ctxt->exception.error_code_valid = valid; +return X86EMUL_PROPAGATE_FAULT ; } -static void emulate_gp (struct x86_emulate_ctxt *ctxt,int err) +static int emulate_gp (struct x86_emulate_ctxt *ctxt,int err) { -emulate_exception (ctxt,GP_VECTOR , err, true); +return emulate_exception (ctxt,GP_VECTOR , err, true); } -static void emulate_ud (struct x86_emulate_ctxt *ctxt) +static int emulate_ud (struct x86_emulate_ctxt *ctxt) { -emulate_exception (ctxt,UD_VECTOR ,0 , false); +return emulate_exception (ctxt,UD_VECTOR ,0 , false); } -static void emulate_ts (struct x86_emulate_ctxt *ctxt,int err) +static int emulate_ts (struct x86_emulate_ctxt *ctxt,int err) { -emulate_exception (ctxt,TS_VECTOR , err, true); +return emulate_exception (ctxt,TS_VECTOR , err, true); }static int emulate_de (struct x86_emulate_ctxt *ctxt) { -emulate_exception (ctxt,DE_VECTOR ,0 , false); -return X86EMUL_PROPAGATE_FAULT ; +return emulate_exception (ctxt,DE_VECTOR ,0 , false); } static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
@@ -898,10 +898,8 @@ static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,get_descriptor_table_ptr (ctxt, ops, selector, &dt); -if (dt.size < index *8 +7 ) { -emulate_gp (ctxt, selector &0xfffc ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (dt.size < index *8 +7 ) +return emulate_gp (ctxt, selector &0xfffc ); addr = dt.address + index *8 ; ret = ops->read_std (addr, desc,sizeof *desc, ctxt->vcpu, &ctxt->exception);
@@ -921,10 +919,8 @@ static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,get_descriptor_table_ptr (ctxt, ops, selector, &dt); -if (dt.size < index *8 +7 ) { -emulate_gp (ctxt, selector &0xfffc ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (dt.size < index *8 +7 ) +return emulate_gp (ctxt, selector &0xfffc ); addr = dt.address + index *8 ; ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
@@ -1165,10 +1161,8 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt, change_mask |=EFLG_IF ;break ;case X86EMUL_MODE_VM86 : -if (iopl <3 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (iopl <3 ) +return emulate_gp (ctxt,0 ); change_mask |=EFLG_IF ;break ;default :/* real mode */
@@ -1347,10 +1341,8 @@ static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,if (rc !=X86EMUL_CONTINUE )return rc; -if (temp_eip & ~0xffff ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (temp_eip & ~0xffff ) +return emulate_gp (ctxt,0 ); rc =emulate_pop (ctxt, ops, &cs, c->op_bytes);
@@ -1601,10 +1593,8 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)/* syscall is not available in real mode */ if (ctxt->mode ==X86EMUL_MODE_REAL || - ctxt->mode ==X86EMUL_MODE_VM86 ) { -emulate_ud (ctxt); -return X86EMUL_PROPAGATE_FAULT ; - } + ctxt->mode ==X86EMUL_MODE_VM86 ) +return emulate_ud (ctxt);setup_syscalls_segments (ctxt, ops, &cs, &ss);
@@ -1655,34 +1645,26 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)u16 cs_sel, ss_sel;/* inject #GP if in real mode */ -if (ctxt->mode ==X86EMUL_MODE_REAL ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (ctxt->mode ==X86EMUL_MODE_REAL ) +return emulate_gp (ctxt,0 );/* XXX sysenter/sysexit have not been tested in 64bit mode. * Therefore, we inject an #UD. */ -if (ctxt->mode ==X86EMUL_MODE_PROT64 ) { -emulate_ud (ctxt); -return X86EMUL_PROPAGATE_FAULT ; - } +if (ctxt->mode ==X86EMUL_MODE_PROT64 ) +return emulate_ud (ctxt);setup_syscalls_segments (ctxt, ops, &cs, &ss); ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data);switch (ctxt->mode) {case X86EMUL_MODE_PROT32 : -if ((msr_data &0xfffc ) ==0x0 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if ((msr_data &0xfffc ) ==0x0 ) +return emulate_gp (ctxt,0 );break ;case X86EMUL_MODE_PROT64 : -if (msr_data ==0x0 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (msr_data ==0x0 ) +return emulate_gp (ctxt,0 );break ; }
@@ -1722,10 +1704,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)/* inject #GP if in real mode or Virtual 8086 mode */ if (ctxt->mode ==X86EMUL_MODE_REAL || - ctxt->mode ==X86EMUL_MODE_VM86 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } + ctxt->mode ==X86EMUL_MODE_VM86 ) +return emulate_gp (ctxt,0 );setup_syscalls_segments (ctxt, ops, &cs, &ss);
@@ -1740,18 +1720,14 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)switch (usermode) {case X86EMUL_MODE_PROT32 : cs_sel = (u16)(msr_data +16 ); -if ((msr_data &0xfffc ) ==0x0 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if ((msr_data &0xfffc ) ==0x0 ) +return emulate_gp (ctxt,0 ); ss_sel = (u16)(msr_data +24 );break ;case X86EMUL_MODE_PROT64 : cs_sel = (u16)(msr_data +32 ); -if (msr_data ==0x0 ) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (msr_data ==0x0 ) +return emulate_gp (ctxt,0 ); ss_sel = cs_sel +8 ; cs.d =0 ; cs.l =1 ;
@@ -1982,10 +1958,8 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,struct decode_cache *c = &ctxt->decode;int ret; -if (ops->set_cr (3 , tss->cr3, ctxt->vcpu)) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (ops->set_cr (3 , tss->cr3, ctxt->vcpu)) +return emulate_gp (ctxt,0 ); c->eip = tss->eip; ctxt->eflags = tss->eflags |2 ; c->regs[VCPU_REGS_RAX ] = tss->eax;
@@ -2107,10 +2081,8 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,if (reason !=TASK_SWITCH_IRET ) {if ((tss_selector &3 ) > next_tss_desc.dpl || - ops->cpl (ctxt->vcpu) > next_tss_desc.dpl) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } + ops->cpl (ctxt->vcpu) > next_tss_desc.dpl) +return emulate_gp (ctxt,0 ); } desc_limit =desc_limit_scaled (&next_tss_desc);
@@ -2331,10 +2303,8 @@ static int em_rdtsc(struct x86_emulate_ctxt *ctxt)struct decode_cache *c = &ctxt->decode;u64 tsc =0 ; -if (cpl >0 && (ctxt->ops->get_cr (4 , ctxt->vcpu) &X86_CR4_TSD )) { -emulate_gp (ctxt,0 ); -return X86EMUL_PROPAGATE_FAULT ; - } +if (cpl >0 && (ctxt->ops->get_cr (4 , ctxt->vcpu) &X86_CR4_TSD )) +return emulate_gp (ctxt,0 ); ctxt->ops->get_msr (ctxt->vcpu,MSR_IA32_TSC , &tsc); c->regs[VCPU_REGS_RAX ] = (u32 )tsc; c->regs[VCPU_REGS_RDX ] = tsc >>32 ;
@@ -2979,28 +2949,24 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) ctxt->decode.mem_read.pos =0 ;if (ctxt->mode ==X86EMUL_MODE_PROT64 && (c->d & No64)) { -emulate_ud (ctxt); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_ud (ctxt); goto done; }/* LOCK prefix is allowed only with some instructions */ if (c->lock_prefix && (!(c->d & Lock) || c->dst.type !=OP_MEM )) { -emulate_ud (ctxt); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_ud (ctxt); goto done; }if ((c->d & SrcMask) == SrcMemFAddr && c->src.type !=OP_MEM ) { -emulate_ud (ctxt); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_ud (ctxt); goto done; }/* Privileged instruction can be executed only in CPL=0 */ if ((c->d & Priv) && ops->cpl (ctxt->vcpu)) { -emulate_gp (ctxt,0 ); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_gp (ctxt,0 ); goto done; }
@@ -3178,8 +3144,7 @@ special_insn:break ;case 0x8c :/* mov r/m, sreg */ if (c->modrm_reg >VCPU_SREG_GS ) { -emulate_ud (ctxt); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_ud (ctxt); goto done; } c->dst.val = ops->get_segment_selector (c->modrm_reg, ctxt->vcpu);
@@ -3194,8 +3159,7 @@ special_insn:if (c->modrm_reg ==VCPU_SREG_CS || c->modrm_reg >VCPU_SREG_GS ) { -emulate_ud (ctxt); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_ud (ctxt); goto done; }
@@ -3327,8 +3291,7 @@ special_insn: do_io_in: c->dst.bytes =min (c->dst.bytes,4u );if (!emulator_io_permited (ctxt, ops, c->src.val, c->dst.bytes)) { -emulate_gp (ctxt,0 ); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_gp (ctxt,0 ); goto done; } if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
@@ -3342,8 +3305,7 @@ special_insn: c->src.bytes =min (c->src.bytes,4u );if (!emulator_io_permited (ctxt, ops, c->dst.val, c->src.bytes)) { -emulate_gp (ctxt,0 ); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_gp (ctxt,0 ); goto done; } ops->pio_out_emulated(c->src.bytes, c->dst.val,
@@ -3368,16 +3330,14 @@ special_insn:break ;case 0xfa :/* cli */ if (emulator_bad_iopl (ctxt, ops)) { -emulate_gp (ctxt,0 ); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_gp (ctxt,0 ); goto done; }else ctxt->eflags &= ~X86_EFLAGS_IF ;break ;case 0xfb :/* sti */ if (emulator_bad_iopl (ctxt, ops)) { -emulate_gp (ctxt,0 ); - rc =X86EMUL_PROPAGATE_FAULT ; + rc =emulate_gp (ctxt,0 ); goto done; }else { ctxt->interruptibility =KVM_X86_SHADOW_INT_STI ;
Signature · 1 line
1.7.1
PATCH 6/7 KVM: Push struct x86_exception info the various gva_to_gpa variants
In reply to Avi Kivity--- arch/x86/include/asm/kvm_host.h | 14 ++++++--- arch/x86/kvm/mmu.c | 13 +++++---- arch/x86/kvm/paging_tmpl.h | 19 +++++++++---- arch/x86/kvm/x86.c | 52 ++++++++++++++++---------------------- 4 files changed, 51 insertions(+), 47 deletions(-)
arch/x86/include/asm/kvm_host.h +9 -5
@@ -245,7 +245,7 @@ struct kvm_mmu {void (*inject_page_fault)(struct kvm_vcpu *vcpu);void (*free)(struct kvm_vcpu *vcpu);gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,gva_t gva,u32 access, -u32 *error); +struct x86_exception *exception);gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu,gpa_t gpa,u32 access);void (*prefetch_page)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *page);
@@ -707,10 +707,14 @@ void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);int kvm_mmu_load (struct kvm_vcpu *vcpu);void kvm_mmu_unload (struct kvm_vcpu *vcpu);void kvm_mmu_sync_roots (struct kvm_vcpu *vcpu); -gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva,u32 *error); -gpa_t kvm_mmu_gva_to_gpa_fetch (struct kvm_vcpu *vcpu,gva_t gva,u32 *error); -gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,u32 *error); -gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva,u32 *error); +gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva, +struct x86_exception *exception); +gpa_t kvm_mmu_gva_to_gpa_fetch (struct kvm_vcpu *vcpu,gva_t gva, +struct x86_exception *exception); +gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva, +struct x86_exception *exception); +gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, +struct x86_exception *exception);int kvm_emulate_hypercall (struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu.c +7 -6
@@ -2568,18 +2568,19 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) }static gpa_t nonpaging_gva_to_gpa (struct kvm_vcpu *vcpu,gva_t vaddr, -u32 access,u32 *error) +u32 access,struct x86_exception *exception) { -if (error) - *error =0 ; +if (exception) + exception->error_code =0 ;return vaddr; }static gpa_t nonpaging_gva_to_gpa_nested (struct kvm_vcpu *vcpu,gva_t vaddr, -u32 access,u32 *error) +u32 access, +struct x86_exception *exception) { -if (error) - *error =0 ; +if (exception) + exception->error_code =0 ;return vcpu->arch.nested_mmu.translate_gpa (vcpu, vaddr, access); }
arch/x86/kvm/paging_tmpl.h +13 -6
@@ -670,7 +670,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva) }static gpa_t FNAME (gva_to_gpa )(struct kvm_vcpu *vcpu,gva_t vaddr,u32 access, -u32 *error) +struct x86_exception *exception) {struct guest_walker walker;gpa_t gpa =UNMAPPED_GVA ;
@@ -681,14 +681,18 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,if (r) { gpa =gfn_to_gpa (walker.gfn); gpa |= vaddr & ~PAGE_MASK ; - }else if (error) - *error = walker.error_code; + }else if (exception) { + exception->vector =PF_VECTOR ; + exception->error_code_valid = true; + exception->error_code = walker.error_code; + }return gpa; }static gpa_t FNAME (gva_to_gpa_nested )(struct kvm_vcpu *vcpu,gva_t vaddr, -u32 access,u32 *error) +u32 access, +struct x86_exception *exception) {struct guest_walker walker;gpa_t gpa =UNMAPPED_GVA ;
@@ -699,8 +703,11 @@ static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,if (r) { gpa =gfn_to_gpa (walker.gfn); gpa |= vaddr & ~PAGE_MASK ; - }else if (error) - *error = walker.error_code; + }else if (exception) { + exception->vector =PF_VECTOR ; + exception->error_code_valid = true; + exception->error_code = walker.error_code; + }return gpa; }
arch/x86/kvm/x86.c +22 -30
137 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 7/7 KVM: Push struct x86_exception into walk_addr()
In reply to Avi Kivity--- arch/x86/kvm/paging_tmpl.h | 32 ++++++++++++++------------------ 1 files changed, 14 insertions(+), 18 deletions(-)
arch/x86/kvm/paging_tmpl.h +14 -18
@@ -72,7 +72,7 @@ struct guest_walker {unsigned pt_access;unsigned pte_access;gfn_t gfn; -u32 error_code; +struct x86_exception fault; };static gfn_t gpte_to_gfn_lvl (pt_element_t gpte,int lvl)
@@ -266,21 +266,23 @@ walk:return 1 ; error: - walker->error_code =0 ; + walker->fault.vector =PF_VECTOR ; + walker->fault.error_code_valid = true; + walker->fault.error_code =0 ;if (present) - walker->error_code |=PFERR_PRESENT_MASK ; + walker->fault.error_code |=PFERR_PRESENT_MASK ; - walker->error_code |= write_fault | user_fault; + walker->fault.error_code |= write_fault | user_fault;if (fetch_fault && mmu->nx) - walker->error_code |=PFERR_FETCH_MASK ; + walker->fault.error_code |=PFERR_FETCH_MASK ;if (rsvd_fault) - walker->error_code |=PFERR_RSVD_MASK ; + walker->fault.error_code |=PFERR_RSVD_MASK ; vcpu->arch.fault.address = addr; - vcpu->arch.fault.error_code = walker->error_code; + vcpu->arch.fault.error_code = walker->fault.error_code; -trace_kvm_mmu_walker_error (walker->error_code); +trace_kvm_mmu_walker_error (walker->fault.error_code);return 0 ; }
@@ -681,11 +683,8 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,if (r) { gpa =gfn_to_gpa (walker.gfn); gpa |= vaddr & ~PAGE_MASK ; - }else if (exception) { - exception->vector =PF_VECTOR ; - exception->error_code_valid = true; - exception->error_code = walker.error_code; - } + }else if (exception) + *exception = walker.fault;return gpa; }
@@ -703,11 +702,8 @@ static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,if (r) { gpa =gfn_to_gpa (walker.gfn); gpa |= vaddr & ~PAGE_MASK ; - }else if (exception) { - exception->vector =PF_VECTOR ; - exception->error_code_valid = true; - exception->error_code = walker.error_code; - } + }else if (exception) + *exception = walker.fault;return gpa; }
Signature · 1 line
1.7.1
Marcelo Tosatti
AppliedQuoted 20 lines · Avi Kivity
This boring patchset introduces a struct
x86_exceptionto represent an exception, and uses it to communicate between the emulator and the rest of kvm. The primary benefit is that we can now pass a #GP from kvm into the emulator, not just #PFs.I'd like to also fold
vcpu->arch.faultinto structx86_exception, but that's more difficult, so deferred until later.Avi Kivity (7):
KVM: x86 emulator: introduce structx86_exceptionto communicate
faults
KVM: x86 emulator: make emulator memory callbacks return full
exception
KVM: x86 emulator: drop dead pf injection inemulate_popf()
KVM: x86 emulator: tighen up ->read_std() and ->write_std() error
checks
KVM: x86 emulator: simplify exception generation
KVM: Push structx86_exceptioninfo the variousgva_to_gpavariants
KVM: Push structx86_exceptionintowalk_addr()
Quoted 7 lines
arch/x86/include/asm/kvm_emulate.h | 26 +++-- arch/x86/include/asm/kvm_host.h | 14 ++- arch/x86/kvm/emulate.c | 243 ++++++++++++++---------------------- arch/x86/kvm/mmu.c | 13 +- arch/x86/kvm/paging_tmpl.h | 31 +++-- arch/x86/kvm/x86.c | 97 ++++++++------- 6 files changed, 195 insertions(+), 229 deletions(-)
Applied, thanks.