KVM: x86: Allow userspace to disable the emulator
cover KVM: x86: Allow userspace to disable the emulator
The primary intent of this series is to dynamically allocate the emulator and get KVM to a state where the emulator *could* be disabled at some point in the future. Actually allowing userspace to disable the emulator was a minor change at that point, so I threw it in.
Dynamically allocating the emulator shrinks the size of x86 vcpus by ~2.5k bytes, which is important because 'struct vcpu_vmx' has once again fattened up and squeaked past the PAGE_ALLOC_COSTLY_ORDER threshold. Moving the emulator to its own allocation gives us some breathing room for the near future, and has some other nice side effects.
As for disabling the emulator... in the not-too-distant future, I expect there will be use cases that can truly disable KVM's emulator, e.g. for security (KVM's and/or the guests). I don't have a strong opinion on whether or not KVM should actually allow userspace to disable the emulator without a concrete use case, which is why that part is done in its own tiny patch. I also expect that any real use case would want "no emulator" to be a per-VM toggle, but since this is basically an experimental feature I added it as a module param.
Running without an emulator has been "tested" in the sense that the selftests that don't require emulation continue to pass, and everything else fails with the expected "emulation error".
Sean Christopherson (13):
KVM: x86: Refactor I/O emulation helpers to provide vcpu-only variant
KVM: x86: Explicitly pass an exception struct to check_intercept
KVM: x86: Move emulation-only helpers to emulate.c
KVM: x86: Refactor R/W page helper to take the emulation context
KVM: x86: Refactor emulated exception injection to take the emul
context
KVM: x86: Refactor emulate tracepoint to explicitly take context
KVM: x86: Refactor init_emulate_ctxt() to explicitly take context
KVM: x86: Dynamically allocate per-vCPU emulation context
KVM: x86: Move kvm_emulate.h into KVM's private directory
KVM: x86: Shrink the usercopy region of the emulation context
KVM: x86: Add helper to "handle" internal emulation error
KVM: x86: Add variable to control existence of emulator
KVM: x86: Allow userspace to disable the kernel's emulator
arch/x86/include/asm/kvm_host.h | 12 +-
arch/x86/kvm/emulate.c | 17 +-
arch/x86/{include/asm => kvm}/kvm_emulate.h | 9 +-
arch/x86/kvm/mmu/mmu.c | 1 +
arch/x86/kvm/svm.c | 5 +-
arch/x86/kvm/trace.h | 22 +--
arch/x86/kvm/vmx/vmx.c | 15 +-
arch/x86/kvm/x86.c | 194 ++++++++++++++------
arch/x86/kvm/x86.h | 16 +-
9 files changed, 189 insertions(+), 102 deletions(-)
rename arch/x86/{include/asm => kvm}/kvm_emulate.h (99%)Signature · 1 line
2.24.0
PATCH 1/13 KVM: x86: Refactor I/O emulation helpers to provide vcpu-only variant
Add new helpers for I/O helpers to provide a variant that takes a vCPU instead of the emulation context. This will eventually allow KVM to constrain use of the emulation context to the full emulation path.
--- arch/x86/kvm/x86.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-)
arch/x86/kvm/x86.c +24 -15
@@ -5887,11 +5887,9 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,return 0 ; } -static int emulator_pio_in_emulated (struct x86_emulate_ctxt *ctxt, -int size,unsigned short port,void *val, -unsigned int count) +static int emulator_pio_in (struct kvm_vcpu *vcpu,int size, +unsigned short port,void *val,unsigned int count) { -struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt);int ret;if (vcpu->arch.pio.count)
@@ -5911,17 +5909,30 @@ static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,return 0 ; } -static int emulator_pio_out_emulated (struct x86_emulate_ctxt *ctxt, -int size,unsigned short port, -const void *val,unsigned int count) +static int emulator_pio_in_emulated (struct x86_emulate_ctxt *ctxt, +int size,unsigned short port,void *val, +unsigned int count) { -struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); +return emulator_pio_in (emul_to_vcpu (ctxt), size, port, val, count); +} + +static int emulator_pio_out (struct kvm_vcpu *vcpu,int size, +unsigned short port,const void *val, +unsigned int count) +{memcpy (vcpu->arch.pio_data, val, size * count);trace_kvm_pio (KVM_PIO_OUT , port, size, count, vcpu->arch.pio_data);return emulator_pio_in_out (vcpu, size, port, (void *)val, count, false); } +static int emulator_pio_out_emulated (struct x86_emulate_ctxt *ctxt, +int size,unsigned short port, +const void *val,unsigned int count) +{ +return emulator_pio_out (emul_to_vcpu (ctxt), size, port, val, count); +} +static unsigned long get_segment_base (struct kvm_vcpu *vcpu,int seg) {return kvm_x86_ops->get_segment_base (vcpu, seg);
@@ -6842,8 +6853,8 @@ static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port) {unsigned long val =kvm_rax_read (vcpu); -int ret =emulator_pio_out_emulated (&vcpu->arch.emulate_ctxt, - size, port, &val,1 ); +int ret =emulator_pio_out (vcpu, size, port, &val,1 ); +if (ret)return ret;
@@ -6879,11 +6890,10 @@ static int complete_fast_pio_in(struct kvm_vcpu *vcpu) val = (vcpu->arch.pio.size <4 ) ?kvm_rax_read (vcpu) :0 ;/* -* Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform +* Since vcpu->arch.pio.count == 1 let emulator_pio_in perform * the copy and tracing */ -emulator_pio_in_emulated (&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size, - vcpu->arch.pio.port, &val,1 ); +emulator_pio_in (vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val,1 );kvm_rax_write (vcpu, val);return kvm_skip_emulated_instruction (vcpu);
@@ -6898,8 +6908,7 @@ static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,/* For size less than 4 we merge, else we zero extend */ val = (size <4 ) ?kvm_rax_read (vcpu) :0 ; - ret =emulator_pio_in_emulated (&vcpu->arch.emulate_ctxt, size, port, - &val,1 ); + ret =emulator_pio_in (vcpu, size, port, &val,1 );if (ret) {kvm_rax_write (vcpu, val);return ret;
Signature · 1 line
2.24.0
PATCH 2/13 KVM: x86: Explicitly pass an exception struct to check_intercept
In reply to Sean ChristophersonExplicitly pass an exception struct when checking for intercept from the emulator, which elimates the last reference to arch.emulate_ctxt in vendor specific code.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/svm.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 8 ++++---- arch/x86/kvm/x86.c | 3 ++- 4 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -1139,7 +1139,8 @@ struct kvm_x86_ops {int (*check_intercept)(struct kvm_vcpu *vcpu,struct x86_instruction_info *info, -enum x86_intercept_stage stage); +enum x86_intercept_stage stage, +struct x86_exception *exception);void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu);bool (*mpx_supported)(void );bool (*xsaves_supported)(void );
arch/x86/kvm/svm.c +2 -1
@@ -6074,7 +6074,8 @@ static const struct __x86_intercept {static int svm_check_intercept (struct kvm_vcpu *vcpu,struct x86_instruction_info *info, -enum x86_intercept_stage stage) +enum x86_intercept_stage stage, +struct x86_exception *exception) {struct vcpu_svm *svm =to_svm (vcpu);int vmexit, ret =X86EMUL_CONTINUE ;
arch/x86/kvm/vmx/vmx.c +4 -4
@@ -7152,10 +7152,10 @@ static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)static int vmx_check_intercept (struct kvm_vcpu *vcpu,struct x86_instruction_info *info, -enum x86_intercept_stage stage) +enum x86_intercept_stage stage, +struct x86_exception *exception) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;/* * RDPID causes #UD if disabled through secondary execution controls.
@@ -7163,8 +7163,8 @@ static int vmx_check_intercept(struct kvm_vcpu *vcpu,*/ if (info->intercept == x86_intercept_rdtscp && !nested_cpu_has2 (vmcs12,SECONDARY_EXEC_RDTSCP )) { - ctxt->exception.vector =UD_VECTOR ; - ctxt->exception.error_code_valid = false; + exception->vector =UD_VECTOR ; + exception->error_code_valid = false;return X86EMUL_PROPAGATE_FAULT ; }
arch/x86/kvm/x86.c +2 -1
@@ -6195,7 +6195,8 @@ static int emulator_intercept(struct x86_emulate_ctxt *ctxt,struct x86_instruction_info *info, enum x86_intercept_stage stage) { - return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage); + return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage, + &ctxt->exception); } static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
Signature · 1 line
2.24.0
PATCH 3/13 KVM: x86: Move emulation-only helpers to emulate.c
In reply to Sean ChristophersonMove ctxt_virt_addr_bits() and emul_is_noncanonical_address() from x86.h to emulate.c. This eliminates all references to struct x86_emulate_ctxt from x86.h, and sets the stage for a future patch to stop including kvm_emulate.h in asm/kvm_host.h.
--- arch/x86/kvm/emulate.c | 15 +++++++++++++++ arch/x86/kvm/x86.h | 15 --------------- 2 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/kvm/emulate.c +15 -0
@@ -670,6 +670,21 @@ static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); } +static inline u8 ctxt_virt_addr_bits (struct x86_emulate_ctxt *ctxt) +{ +return (ctxt->ops->get_cr (ctxt,4 ) &X86_CR4_LA57 ) ?57 :48 ; +} + +static inline bool emul_is_noncanonical_address (u64 la, +struct x86_emulate_ctxt *ctxt) +{ +#ifdef CONFIG_X86_64 +return get_canonical (la,ctxt_virt_addr_bits (ctxt)) != la; +#else +return false; +#endif +} +/* * x86 defines three classes of vector instructions: explicitly * aligned, explicitly unaligned, and the rest, which change behaviour
arch/x86/kvm/x86.h +0 -15
@@ -154,11 +154,6 @@ static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)return kvm_read_cr4_bits (vcpu,X86_CR4_LA57 ) ?57 :48 ; } -static inline u8 ctxt_virt_addr_bits (struct x86_emulate_ctxt *ctxt) -{ -return (ctxt->ops->get_cr (ctxt,4 ) &X86_CR4_LA57 ) ?57 :48 ; -} -static inline u64 get_canonical (u64 la,u8 vaddr_bits) {return ((int64_t )la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
@@ -173,16 +168,6 @@ static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)#endif } -static inline bool emul_is_noncanonical_address (u64 la, -struct x86_emulate_ctxt *ctxt) -{ -#ifdef CONFIG_X86_64 -return get_canonical (la,ctxt_virt_addr_bits (ctxt)) != la; -#else -return false; -#endif -} -static inline void vcpu_cache_mmio_info (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn,unsigned access) {
Signature · 1 line
2.24.0
PATCH 4/13 KVM: x86: Refactor R/W page helper to take the emulation context
In reply to Sean ChristophersonInvert the vcpu->context derivation in emulator_read_write_onepage() in preparation for dynamically allocating the emulation context.
--- arch/x86/kvm/x86.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +4 -4
@@ -5648,14 +5648,14 @@ static const struct read_write_emulator_ops write_emultor = {static int emulator_read_write_onepage (unsigned long addr,void *val,unsigned int bytes,struct x86_exception *exception, -struct kvm_vcpu *vcpu, +struct x86_emulate_ctxt *ctxt,const struct read_write_emulator_ops *ops) {gpa_t gpa;int handled, ret;bool write = ops->write;struct kvm_mmio_fragment *frag; -struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; +struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt);/* * If the exit was due to a NPF we may already have a GPA.
@@ -5719,7 +5719,7 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt, now = -addr & ~PAGE_MASK ; rc =emulator_read_write_onepage (addr, val, now, exception, - vcpu, ops); + ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc;
@@ -5731,7 +5731,7 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt, } rc =emulator_read_write_onepage (addr, val, bytes, exception, - vcpu, ops); + ctxt, ops);if (rc !=X86EMUL_CONTINUE )return rc;
Signature · 1 line
2.24.0
PATCH 5/13 KVM: x86: Refactor emulated exception injection to take the emul context
In reply to Sean ChristophersonInvert the vcpu->context derivation in inject_emulated_exception() in preparation for dynamically allocating the emulation context.
--- arch/x86/kvm/x86.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +5 -4
@@ -6309,9 +6309,10 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) } } -static bool inject_emulated_exception (struct kvm_vcpu *vcpu) +static bool inject_emulated_exception (struct x86_emulate_ctxt *ctxt) { -struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; +struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); +if (ctxt->exception.vector ==PF_VECTOR )return kvm_propagate_fault (vcpu, &ctxt->exception);
@@ -6718,7 +6719,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,*/ WARN_ON_ONCE (ctxt->exception.vector ==UD_VECTOR ||exception_type (ctxt->exception.vector) ==EXCPT_TRAP ); -inject_emulated_exception (vcpu); +inject_emulated_exception (ctxt);return 1 ; }return handle_emulation_failure (vcpu, emulation_type);
@@ -6772,7 +6773,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (ctxt->have_exception) { r =1 ; -if (inject_emulated_exception (vcpu)) +if (inject_emulated_exception (ctxt))return r; }else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) {
Signature · 1 line
2.24.0
PATCH 6/13 KVM: x86: Refactor emulate tracepoint to explicitly take context
In reply to Sean ChristophersonExplicitly pass the emulation context to the emulate tracepoint in preparation of dynamically allocation the emulation context.
--- arch/x86/kvm/trace.h | 22 +++++++++++----------- arch/x86/kvm/x86.c | 13 ++++++++----- 2 files changed, 19 insertions(+), 16 deletions(-)
arch/x86/kvm/trace.h +11 -11
@@ -731,8 +731,9 @@ TRACE_EVENT(kvm_skinit, }) TRACE_EVENT(kvm_emulate_insn, - TP_PROTO(struct kvm_vcpu *vcpu, __u8 failed), - TP_ARGS(vcpu, failed), + TP_PROTO(struct kvm_vcpu *vcpu, struct x86_emulate_ctxt *ctxt, + __u8 failed), + TP_ARGS(vcpu, ctxt, failed), TP_STRUCT__entry( __field( __u64, rip )
@@ -745,13 +746,10 @@ TRACE_EVENT(kvm_emulate_insn,TP_fast_assign ( __entry->csbase = kvm_x86_ops->get_segment_base(vcpu, VCPU_SREG_CS); - __entry->len = vcpu->arch.emulate_ctxt.fetch.ptr - - vcpu->arch.emulate_ctxt.fetch.data; - __entry->rip = vcpu->arch.emulate_ctxt._eip - __entry->len; -memcpy (__entry->insn, - vcpu->arch.emulate_ctxt.fetch.data, -15 ); - __entry->flags =kei_decode_mode (vcpu->arch.emulate_ctxt.mode); + __entry->len = ctxt->fetch.ptr - ctxt->fetch.data; + __entry->rip = ctxt->_eip - __entry->len; +memcpy (__entry->insn, ctxt->fetch.data,15 ); + __entry->flags =kei_decode_mode (ctxt->mode); __entry->failed = failed; ),
@@ -764,8 +762,10 @@ TRACE_EVENT(kvm_emulate_insn, ) ); -#define trace_kvm_emulate_insn_start (vcpu) trace_kvm_emulate_insn(vcpu, 0) -#define trace_kvm_emulate_insn_failed (vcpu) trace_kvm_emulate_insn(vcpu, 1) +#define trace_kvm_emulate_insn_start (vcpu, ctxt) \ + trace_kvm_emulate_insn(vcpu, ctxt, 0) +#define trace_kvm_emulate_insn_failed (vcpu, ctxt) \ + trace_kvm_emulate_insn(vcpu, ctxt, 1) TRACE_EVENT( vcpu_match_mmio,
arch/x86/kvm/x86.c +8 -5
@@ -6370,10 +6370,13 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) }EXPORT_SYMBOL_GPL (kvm_inject_realmode_interrupt); -static int handle_emulation_failure (struct kvm_vcpu *vcpu,int emulation_type) +static int handle_emulation_failure (struct x86_emulate_ctxt *ctxt, +int emulation_type) { +struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); + ++vcpu->stat.insn_emulation_fail; -trace_kvm_emulate_insn_failed (vcpu); +trace_kvm_emulate_insn_failed (vcpu, ctxt);if (emulation_type &EMULTYPE_VMWARE_GP ) {kvm_queue_exception_e (vcpu,GP_VECTOR ,0 );
@@ -6701,7 +6704,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, r =x86_decode_insn (ctxt, insn, insn_len); -trace_kvm_emulate_insn_start (vcpu); +trace_kvm_emulate_insn_start (vcpu, ctxt); ++vcpu->stat.insn_emulation;if (r !=EMULATION_OK ) { if ((emulation_type & EMULTYPE_TRAP_UD) ||
@@ -6722,7 +6725,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,inject_emulated_exception (ctxt);return 1 ; } -return handle_emulation_failure (vcpu, emulation_type); +return handle_emulation_failure (ctxt, emulation_type); } }
@@ -6768,7 +6771,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, emulation_type))return 1 ; -return handle_emulation_failure (vcpu, emulation_type); +return handle_emulation_failure (ctxt, emulation_type); }if (ctxt->have_exception) {
Signature · 1 line
2.24.0
PATCH 7/13 KVM: x86: Refactor init_emulate_ctxt() to explicitly take context
In reply to Sean ChristophersonExplicitly pass the emulation context when initializing said context in preparation of dynamically allocation the emulation context.
--- arch/x86/kvm/x86.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +5 -5
@@ -6324,9 +6324,9 @@ static bool inject_emulated_exception(struct x86_emulate_ctxt *ctxt)return false; } -static void init_emulate_ctxt (struct kvm_vcpu *vcpu) +static void init_emulate_ctxt (struct x86_emulate_ctxt *ctxt) { -struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; +struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt);int cs_db, cs_l; kvm_x86_ops->get_cs_db_l_bits (vcpu, &cs_db, &cs_l);
@@ -6353,7 +6353,7 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;int ret; -init_emulate_ctxt (vcpu); +init_emulate_ctxt (ctxt); ctxt->op_bytes =2 ; ctxt->ad_bytes =2 ;
@@ -6683,7 +6683,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,kvm_clear_exception_queue (vcpu);if (!(emulation_type &EMULTYPE_NO_DECODE )) { -init_emulate_ctxt (vcpu); +init_emulate_ctxt (ctxt);/* * We will reenter on the same instruction since
@@ -8787,7 +8787,7 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;int ret; -init_emulate_ctxt (vcpu); +init_emulate_ctxt (ctxt); ret =emulator_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code);
Signature · 1 line
2.24.0
PATCH 8/13 KVM: x86: Dynamically allocate per-vCPU emulation context
In reply to Sean ChristophersonAllocate the emulation context instead of embedding it in struct kvm_vcpu_arch.
Dynamic allocation provides several benefits:
- Shrinks the size x86 vcpus by ~2.5k bytes, dropping them back below the PAGE_ALLOC_COSTLY_ORDER threshold.
- Allows for dropping the include of kvm_emulate.h from asm/kvm_host.h and moving kvm_emulate.h into KVM's private directory.
- Allows a reducing KVM's attack surface by shrinking the amount of vCPU data that is exposed to usercopy.
- Allows a future patch to disable the emulator entirely, which may or may not be a realistic endeavor.
Mark the entire struct as valid for usercopy to maintain existing behavior with respect to hardened usercopy. Future patches can shrink the usercopy range to cover only what is necessary.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 62 ++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -289,6 +289,7 @@ enum x86emul_mode {#define X86EMUL_SMM_INSIDE_NMI_MASK (1 << 7)struct x86_emulate_ctxt { +void *vcpu;const struct x86_emulate_ops *ops;/* Register state before/after emulation. */
arch/x86/include/asm/kvm_host.h +1 -1
@@ -671,7 +671,7 @@ struct kvm_vcpu_arch {/* emulate context */ -struct x86_emulate_ctxt emulate_ctxt; +struct x86_emulate_ctxt *emulate_ctxt;bool emulate_regs_need_sync_to_vcpu;bool emulate_regs_need_sync_from_vcpu;int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +55 -7
143 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH 9/13 KVM: x86: Move kvm_emulate.h into KVM's private directory
In reply to Sean ChristophersonNow that the emulation context is dynamically allocated and not embedded in struct kvm_vcpu, move its header, kvm_emulate.h, out of the public asm directory and into KVM's private x86 directory.
---
arch/x86/include/asm/kvm_host.h | 5 ++++-
arch/x86/kvm/emulate.c | 2 +-
arch/x86/{include/asm => kvm}/kvm_emulate.h | 0
arch/x86/kvm/mmu/mmu.c | 1 +
arch/x86/kvm/x86.c | 1 +
arch/x86/kvm/x86.h | 1 +
6 files changed, 8 insertions(+), 2 deletions(-)
rename arch/x86/{include/asm => kvm}/kvm_emulate.h (100%)arch/x86/include/asm/kvm_host.h +4 -1
@@ -175,7 +175,10 @@ enum { VCPU_SREG_LDTR, }; -#include <asm/kvm_emulate.h> +struct x86_emulate_ctxt ; +struct x86_exception ; +enum x86_intercept ; +enum x86_intercept_stage ;#define KVM_NR_MEM_OBJS 40
arch/x86/kvm/emulate.c +1 -1
@@ -20,7 +20,7 @@#include <linux/kvm_host.h> #include "kvm_cache_regs.h" -#include <asm/kvm_emulate.h> +#include "kvm_emulate.h" #include <linux/stringify.h> #include <asm/debugreg.h> #include <asm/nospec-branch.h>
arch/x86/kvm/kvm_emulate.h renamed from arch/x86/include/asm/kvm_emulate.h +0 -0
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -19,6 +19,7 @@#include "mmu.h" #include "x86.h" #include "kvm_cache_regs.h" +#include "kvm_emulate.h" #include "cpuid.h" #include <linux/kvm_host.h>
arch/x86/kvm/x86.c +1 -0
@@ -22,6 +22,7 @@#include "i8254.h" #include "tss.h" #include "kvm_cache_regs.h" +#include "kvm_emulate.h" #include "x86.h" #include "cpuid.h" #include "pmu.h"
arch/x86/kvm/x86.h +1 -0
@@ -5,6 +5,7 @@#include <linux/kvm_host.h> #include <asm/pvclock.h> #include "kvm_cache_regs.h" +#include "kvm_emulate.h" #define KVM_DEFAULT_PLE_GAP 128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
Signature · 1 line
2.24.0
PATCH 10/13 KVM: x86: Shrink the usercopy region of the emulation context
In reply to Sean ChristophersonShuffle a few operand structs to the end of struct x86_emulate_ctxt and update the cache creation to whitelist only the region of the emulation context that is expected to be copied to/from user memory, e.g. the instruction operands, registers, and fetch/io/mem caches.
--- arch/x86/kvm/kvm_emulate.h | 8 +++++--- arch/x86/kvm/x86.c | 12 ++++++------ 2 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/kvm/kvm_emulate.h +5 -3
@@ -318,9 +318,6 @@ struct x86_emulate_ctxt {u8 intercept;u8 op_bytes;u8 ad_bytes; -struct operand src; -struct operand src2; -struct operand dst;int (*execute)(struct x86_emulate_ctxt *ctxt);int (*check_perm)(struct x86_emulate_ctxt *ctxt);/*
@@ -345,6 +342,11 @@ struct x86_emulate_ctxt {u8 seg_override;u64 d;unsigned long _eip; + +/* Here begins the usercopy section. */ +struct operand src; +struct operand src2; +struct operand dst;struct operand memop;/* Fields above regs are cleared together. */ unsigned long _regs[NR_VCPU_REGS ];
arch/x86/kvm/x86.c +6 -6
@@ -231,13 +231,13 @@ static struct kmem_cache *x86_emulator_cache;static struct kmem_cache *kvm_alloc_emulator_cache (void ) { -return kmem_cache_create_usercopy ("x86_emulator" , -sizeof (struct x86_emulate_ctxt ), +unsigned int useroffset = offsetof(struct x86_emulate_ctxt , src); +unsigned int size =sizeof (struct x86_emulate_ctxt ); + +return kmem_cache_create_usercopy ("x86_emulator" , size, __alignof__(struct x86_emulate_ctxt ), -SLAB_ACCOUNT , -0 , -sizeof (struct x86_emulate_ctxt ), -NULL ); +SLAB_ACCOUNT , useroffset, + size - useroffset,NULL ); }static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt);
Signature · 1 line
2.24.0
PATCH 11/13 KVM: x86: Add helper to "handle" internal emulation error
In reply to Sean ChristophersonAdd a helper to set the appropriate error codes in vcpu->run when emulation fails (future patches will add additional failure scenarios).
--- arch/x86/kvm/x86.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)
arch/x86/kvm/x86.c +14 -18
@@ -6401,6 +6401,14 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) }EXPORT_SYMBOL_GPL (kvm_inject_realmode_interrupt); +static int internal_emulation_error (struct kvm_vcpu *vcpu) +{ + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; + vcpu->run->internal.ndata =0 ; +return 0 ; +} +static int handle_emulation_failure (struct x86_emulate_ctxt *ctxt,int emulation_type) {
@@ -6414,21 +6422,13 @@ static int handle_emulation_failure(struct x86_emulate_ctxt *ctxt,return 1 ; } -if (emulation_type &EMULTYPE_SKIP ) { - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ; -return 0 ; - } +if (emulation_type &EMULTYPE_SKIP ) +return internal_emulation_error (vcpu);kvm_queue_exception (vcpu,UD_VECTOR ); -if (!is_guest_mode (vcpu) && kvm_x86_ops->get_cpl (vcpu) ==0 ) { - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ; -return 0 ; - } +if (!is_guest_mode (vcpu) && kvm_x86_ops->get_cpl (vcpu) ==0 ) +return internal_emulation_error (vcpu);return 1 ; }
@@ -8830,12 +8830,8 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, ret =emulator_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code); -if (ret) { - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ; -return 0 ; - } +if (ret) +return internal_emulation_error (vcpu);kvm_rip_write (vcpu, ctxt->eip);kvm_set_rflags (vcpu, ctxt->eflags);
Signature · 1 line
2.24.0
PATCH 12/13 KVM: x86: Add variable to control existence of emulator
In reply to Sean ChristophersonAdd a global variable to control whether or not the emulator is enabled, and make all necessary changes to gracefully handle reaching emulation paths with the emulator disabled.
Running with VMX's unrestricted guest disabled requires special consideration due to its use of kvm_inject_realmode_interrupt(). When unrestricted guest is disabled, KVM emulates interrupts and exceptions when the processor is in real mode, but does so without going through the standard emulator loop. Ideally, kvm_inject_realmode_interrupt() would only log the interrupt and defer actual emulation to the standard run loop, but that is a non-trivial change and a waste of resources given that unrestricted guest is supported on all CPUs shipped within the last decade. Similarly, dirtying up the event injection stack for such a legacy feature is undesirable. To avoid the conundrum, prevent disabling both the emulator and unrestricted guest.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 7 ++++++- arch/x86/kvm/x86.c | 18 +++++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1031,7 +1031,7 @@ struct kvm_x86_ops {int (*hardware_enable)(void );void (*hardware_disable)(void );int (*check_processor_compatibility)(void );/* __init */ -int (*hardware_setup)(void );/* __init */ +int (*hardware_setup)(bool enable_emulator);/* __init */ void (*hardware_unsetup)(void );/* __exit */ bool (*cpu_has_accelerated_tpr)(void );bool (*has_emulated_msr)(int index);
arch/x86/kvm/svm.c +1 -1
@@ -1307,7 +1307,7 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu) } } -static __init int svm_hardware_setup (void ) +static __init int svm_hardware_setup (bool enable_emulator) {int cpu;struct page *iopm_pages;
arch/x86/kvm/vmx/vmx.c +6 -1
@@ -7600,7 +7600,7 @@ static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)return to_vmx (vcpu)->nested.vmxon; } -static __init int hardware_setup (void ) +static __init int hardware_setup (bool enable_emulator) {unsigned long host_bndcfgs;struct desc_ptr dt;
@@ -7647,6 +7647,11 @@ static __init int hardware_setup(void)if (!cpu_has_virtual_nmis ()) enable_vnmi =0 ; +if (!enable_emulator && !enable_unrestricted_guest) { +pr_warn ("kvm: unrestricted guest disabled, emulator must be enabled\n" ); +return -EIO ; + } +/* * set_apic_access_page_addr() is used to reload apic access * page upon invalidation. No need to do anything if not
arch/x86/kvm/x86.c +15 -3
@@ -155,6 +155,8 @@ EXPORT_SYMBOL_GPL(enable_vmware_backdoor);static bool __read_mostly force_emulation_prefix = false;module_param (force_emulation_prefix, bool,S_IRUGO ); +static const bool enable_emulator = true; +int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR );
@@ -6384,6 +6386,9 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;int ret; +if (WARN_ON_ONCE (!ctxt)) +return ; +init_emulate_ctxt (ctxt); ctxt->op_bytes =2 ;
@@ -6704,6 +6709,9 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,bool writeback = true;bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; +if (!ctxt) +return internal_emulation_error (vcpu); + vcpu->arch.l1tf_flush_l1d = true;/*
@@ -8633,7 +8641,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)static void __get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs) { -if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { +if (vcpu->arch.emulate_regs_need_sync_to_vcpu && + !(WARN_ON_ONCE (!vcpu->arch.emulate_ctxt))) {/* * We are here if userspace calls get_regs() in the middle of * instruction emulation. Registers state needs to be copied
@@ -8826,6 +8835,9 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;int ret; +if (!ctxt) +return internal_emulation_error (vcpu); +init_emulate_ctxt (ctxt); ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
@@ -9164,7 +9176,7 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,if (IS_ERR (vcpu))return vcpu; -if (!alloc_emulate_ctxt (vcpu)) { +if (enable_emulator && !alloc_emulate_ctxt (vcpu)) {kvm_arch_vcpu_destroy (vcpu);return ERR_PTR (-ENOMEM ); }
@@ -9410,7 +9422,7 @@ int kvm_arch_hardware_setup(void) {int r; - r = kvm_x86_ops->hardware_setup (); + r = kvm_x86_ops->hardware_setup (enable_emulator);if (r !=0 )return r;
Signature · 1 line
2.24.0
PATCH 13/13 KVM: x86: Allow userspace to disable the kernel's emulator
In reply to Sean ChristophersonExpose the emulator control to userspace via a module param.
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -155,7 +155,8 @@ EXPORT_SYMBOL_GPL(enable_vmware_backdoor);static bool __read_mostly force_emulation_prefix = false;module_param (force_emulation_prefix, bool,S_IRUGO ); -static const bool enable_emulator = true; +static bool __read_mostly enable_emulator = true; +module_param_named (emulator, enable_emulator, bool,0440 );int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR );
Signature · 1 line
2.24.0