make nested virtualization support optional
cover make nested virtualization support optional
Here're a couple of patches that introduce a Kconfig control for user to be able to remove nested HVM code from the build and hence to tune their system in case nested VMs aren't required.
The first two patches are minor fixes and not directly related to nested HVM, but since they also modify the same code I've decided to include them too.
-Sergiy
Sergiy Kibrik (4):
x86:svm: use nsvm_efer_svm_enabled() to check guest's EFER.SVME x86:svm: rename svm/nestedhvm.h -> svm/nestedsvm.h x86:hvm: guard calls to nestedhvm routines
x86:hvm: make nestedhvm support optional
xen/arch/x86/Kconfig | 5 +++++
xen/arch/x86/hvm/Makefile | 2 +-
xen/arch/x86/hvm/hvm.c | 6 ++++--
xen/arch/x86/hvm/svm/Makefile | 2 +-
xen/arch/x86/hvm/svm/asid.c | 2 +-
xen/arch/x86/hvm/svm/entry.S | 2 ++
xen/arch/x86/hvm/svm/intr.c | 2 +-
xen/arch/x86/hvm/svm/nestedsvm.c | 2 +-
xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} | 0
xen/arch/x86/hvm/svm/svm.c | 14 +++++++++-----
xen/arch/x86/hvm/vmx/Makefile | 2 +-
xen/arch/x86/hvm/vmx/entry.S | 2 ++
xen/arch/x86/hvm/vmx/vmcs.c | 8 +++++---
xen/arch/x86/hvm/vmx/vmx.c | 10 +++++++++-
xen/arch/x86/include/asm/hvm/nestedhvm.h | 10 +++++++++-
15 files changed, 51 insertions(+), 18 deletions(-)
rename xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} (100%)Signature · 1 line
2.25.1
PATCH 1/4 x86:svm: use nsvm_efer_svm_enabled() to check guest's EFER.SVME
There's a macro for this, might improve readability a bit & save a bit of space.
--- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1616,7 +1616,7 @@ void svm_nested_features_on_efer_update(struct vcpu *v)* Need state for transfering the nested gif status so only write on * the hvm_vcpu EFER.SVME changing. */ -if ( v->arch.hvm.guest_efer &EFER_SVME ) +if (nsvm_efer_svm_enabled (v) ) { if ( !vmcb->virt_ext.fields.vloadsave_enable && paging_mode_hap(v->domain) &&
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -253,7 +253,7 @@ static void cf_check svm_update_guest_efer(struct vcpu *v)vmcb_set_efer (vmcb, guest_efer);ASSERT (nestedhvm_enabled (v->domain) || - !(v->arch.hvm.guest_efer &EFER_SVME )); + !nsvm_efer_svm_enabled (v));if (nestedhvm_enabled (v->domain) )svm_nested_features_on_efer_update (v);
Signature · 1 line
2.25.1
PATCH 2/4 x86:svm: rename svm/nestedhvm.h -> svm/nestedsvm.h
In reply to Sergiy KibrikThis is private header for SVM code, comment in header and file's changelog suggest that intended name was svm/nestedsvm.h, so perhaps a small mistake here.
---
xen/arch/x86/hvm/svm/intr.c | 2 +-
xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} | 0
xen/arch/x86/hvm/svm/svm.c | 2 +-
3 files changed, 2 insertions(+), 2 deletions(-)
rename xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} (100%)xen/arch/x86/hvm/svm/intr.c +1 -1
@@ -25,7 +25,7 @@#include <public/hvm/ioreq.h> #include <xen/domain_page.h> -#include "nestedhvm.h" +#include "nestedsvm.h" static void svm_inject_nmi (struct vcpu *v) {
xen/arch/x86/hvm/svm/nestedsvm.h renamed from xen/arch/x86/hvm/svm/nestedhvm.h +0 -0
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -41,7 +41,7 @@#include <public/sched.h> -#include "nestedhvm.h" +#include "nestedsvm.h" #include "svm.h" void noreturnsvm_asm_do_resume (void );
Signature · 1 line
2.25.1
PATCH 3/4 x86:hvm: guard calls to nestedhvm routines
In reply to Sergiy KibrikCheck whether nested HVM is enabled for domain before calling nestedhvm_vcpu_*() and other not already guarded nestedhvm functions.
--- xen/arch/x86/hvm/hvm.c | 6 ++++-- xen/arch/x86/hvm/svm/asid.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -2
@@ -1658,7 +1658,8 @@ int hvm_vcpu_initialise(struct vcpu *v)return 0 ; fail6: -nestedhvm_vcpu_destroy (v); +if (nestedhvm_enabled (d) ) +nestedhvm_vcpu_destroy (v); fail5:free_compat_arg_xlat (v); fail4:
@@ -1682,7 +1683,8 @@ void hvm_vcpu_destroy(struct vcpu *v)if (hvm_altp2m_supported () )altp2m_vcpu_destroy (v); -nestedhvm_vcpu_destroy (v); +if (nestedhvm_enabled (v->domain) ) +nestedhvm_vcpu_destroy (v);free_compat_arg_xlat (v);
xen/arch/x86/hvm/svm/asid.c +1 -1
@@ -30,7 +30,7 @@ void svm_asid_handle_vmrun(void)struct vcpu *curr = current;struct vmcb_struct *vmcb = curr->arch.hvm.svm.vmcb;struct hvm_vcpu_asid *p_asid = -nestedhvm_vcpu_in_guestmode (curr) + (nestedhvm_enabled (curr->domain) &&nestedhvm_vcpu_in_guestmode (curr) ) ? &vcpu_nestedhvm (curr).nv_n2asid : &curr->arch.hvm.n1asid;bool need_flush =hvm_asid_handle_vmenter (p_asid);
xen/arch/x86/hvm/svm/svm.c +3 -3
@@ -795,7 +795,7 @@ static void cf_check svm_set_tsc_offset(struct vcpu *v, u64 offset, u64 at_tsc) n1vmcb =vcpu_nestedhvm (v).nv_n1vmcx; n2vmcb =vcpu_nestedhvm (v).nv_n2vmcx; -if (nestedhvm_vcpu_in_guestmode (v) ) +if (nestedhvm_enabled (d) &&nestedhvm_vcpu_in_guestmode (v) ) { n2_tsc_offset =vmcb_get_tsc_offset (n2vmcb) -vmcb_get_tsc_offset (n1vmcb);
@@ -2172,7 +2172,7 @@ svm_vmexit_do_vmrun(struct cpu_user_regs *regs,return ; } -if ( !nestedsvm_vmcb_map (v, vmcbaddr) ) +if (nestedhvm_enabled (v->domain) && !nestedsvm_vmcb_map (v, vmcbaddr) ) {gdprintk (XENLOG_ERR ,"VMRUN: mapping vmcb failed, injecting #GP\n" );hvm_inject_hw_exception (X86_EXC_GP ,0 );
@@ -2190,7 +2190,7 @@ nsvm_get_nvmcb_page(struct vcpu *v, uint64_t vmcbaddr)struct page_info *page;struct nestedvcpu *nv = &vcpu_nestedhvm (v); -if ( !nestedsvm_vmcb_map (v, vmcbaddr) ) +if (nestedhvm_enabled (v->domain) && !nestedsvm_vmcb_map (v, vmcbaddr) )return NULL ;/* Need to translate L1-GPA to MPA */
Signature · 1 line
2.25.1
PATCH 4/4 x86:hvm: make nestedhvm support optional
In reply to Sergiy KibrikIntroduce NESTEDHVM config option that controls nested virtualization in both SVM & VMX code. The option is for reduction of dead code on systems that aren't intended to run in nested mode.
--- xen/arch/x86/Kconfig | 5 +++++ xen/arch/x86/hvm/Makefile | 2 +- xen/arch/x86/hvm/svm/Makefile | 2 +- xen/arch/x86/hvm/svm/entry.S | 2 ++ xen/arch/x86/hvm/svm/svm.c | 4 ++++ xen/arch/x86/hvm/vmx/Makefile | 2 +- xen/arch/x86/hvm/vmx/entry.S | 2 ++ xen/arch/x86/hvm/vmx/vmcs.c | 8 +++++--- xen/arch/x86/hvm/vmx/vmx.c | 10 +++++++++- xen/arch/x86/include/asm/hvm/nestedhvm.h | 10 +++++++++- 10 files changed, 39 insertions(+), 8 deletions(-)
xen/arch/x86/Kconfig +5 -0
@@ -144,6 +144,11 @@ config INTEL_VMX If your system includes a processor with Intel VT-x support, say Y. If in doubt, say Y. +config NESTEDHVM + bool "Support nested virtualization" if EXPERT + depends on HVM + default y + config XEN_SHSTK bool "Supervisor Shadow Stacks" depends on HAS_AS_CET_SS
xen/arch/x86/hvm/Makefile +1 -1
@@ -17,7 +17,7 @@ obj-y += ioreq.o obj-y += irq.o obj-y += monitor.o obj-y += mtrr.o -obj-y += nestedhvm.o +obj-$(CONFIG_NESTEDHVM) += nestedhvm.o obj-y += pmtimer.o obj-y += quirks.o obj-y += rtc.o
xen/arch/x86/hvm/svm/Makefile +1 -1
@@ -2,7 +2,7 @@ obj-y += asid.o obj-y += emulate.o obj-bin-y += entry.o obj-y += intr.o -obj-y += nestedsvm.o +obj-$(CONFIG_NESTEDHVM) += nestedsvm.o obj-y += svm.o obj-y += svmdebug.o obj-y += vmcb.o
xen/arch/x86/hvm/svm/entry.S +2 -0
@@ -28,7 +28,9 @@ FUNC(svm_asm_do_resume) GET_CURRENT(bx) .Lsvm_do_resume: call svm_intr_assist +#ifdef CONFIG_NESTEDHVM call nsvm_vcpu_switch +#endif ASSERT_NOT_IN_ATOMIC mov VCPU_processor(%rbx),%eax
xen/arch/x86/hvm/svm/svm.c +4 -0
@@ -2460,6 +2460,7 @@ static struct hvm_function_table __initdata_cf_clobber svm_function_table = { .set_descriptor_access_exiting = svm_set_descriptor_access_exiting, .get_insn_bytes = svm_get_insn_bytes, +#ifdef CONFIG_NESTEDHVM .nhvm_vcpu_initialise = nsvm_vcpu_initialise, .nhvm_vcpu_destroy = nsvm_vcpu_destroy, .nhvm_vcpu_reset = nsvm_vcpu_reset,
@@ -2469,6 +2470,7 @@ static struct hvm_function_table __initdata_cf_clobber svm_function_table = { .nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled, .nhvm_intr_blocked = nsvm_intr_blocked, .nhvm_hap_walk_L1_p2m = nsvm_hap_walk_L1_p2m, +#endif .get_reg = svm_get_reg, .set_reg = svm_set_reg,
@@ -2991,12 +2993,14 @@ void asmlinkage svm_vmexit_handler(void)case VMEXIT_VMSAVE :svm_vmexit_do_vmsave (vmcb, regs, v, regs->rax);break ; +#ifdef CONFIG_NESTEDHVM case VMEXIT_STGI :svm_vmexit_do_stgi (regs, v);break ;case VMEXIT_CLGI :svm_vmexit_do_clgi (regs, v);break ; +#endif case VMEXIT_XSETBV :if (vmcb_get_cpl (vmcb) )
xen/arch/x86/hvm/vmx/Makefile +1 -1
@@ -3,4 +3,4 @@ obj-y += intr.o obj-y += realmode.o obj-y += vmcs.o obj-y += vmx.o -obj-y += vvmx.o +obj-$(CONFIG_NESTEDHVM) += vvmx.o
xen/arch/x86/hvm/vmx/entry.S +2 -0
@@ -86,7 +86,9 @@ FUNC(vmx_asm_vmexit_handler) .Lvmx_do_vmentry: call vmx_intr_assist +#ifdef CONFIG_NESTEDHVM call nvmx_switch_guest +#endif ASSERT_NOT_IN_ATOMIC mov VCPU_processor(%rbx),%eax
xen/arch/x86/hvm/vmx/vmcs.c +5 -3
@@ -674,7 +674,7 @@ int cf_check vmx_cpu_up_prepare(unsigned int cpu)* If nvmx_cpu_up_prepare() failed, do not return failure and just fallback * to legacy mode for vvmcs synchronization. */ -if (nvmx_cpu_up_prepare (cpu) !=0 ) +if (IS_ENABLED (CONFIG_NESTEDHVM ) &&nvmx_cpu_up_prepare (cpu) !=0 )printk ("CPU%d: Could not allocate virtual VMCS buffer.\n" , cpu);if (per_cpu (vmxon_region, cpu) )
@@ -685,7 +685,8 @@ int cf_check vmx_cpu_up_prepare(unsigned int cpu)return 0 ;printk ("CPU%d: Could not allocate host VMCS\n" , cpu); -nvmx_cpu_dead (cpu); +if (IS_ENABLED (CONFIG_NESTEDHVM ) ) +nvmx_cpu_dead (cpu);return -ENOMEM ; }
@@ -693,7 +694,8 @@ void cf_check vmx_cpu_dead(unsigned int cpu) {vmx_free_vmcs (per_cpu (vmxon_region, cpu));per_cpu (vmxon_region, cpu) =0 ; -nvmx_cpu_dead (cpu); +if (IS_ENABLED (CONFIG_NESTEDHVM ) ) +nvmx_cpu_dead (cpu);vmx_pi_desc_fixup (cpu); }
xen/arch/x86/hvm/vmx/vmx.c +9 -1
@@ -1959,6 +1959,7 @@ static void nvmx_enqueue_n2_exceptions(struct vcpu *v, nvmx->intr.intr_info, nvmx->intr.error_code); } +#ifdef CONFIG_NESTEDHVM static int cf_check nvmx_vmexit_event(struct vcpu *v,const struct x86_event *event) {
@@ -1966,6 +1967,7 @@ static int cf_check nvmx_vmexit_event( hvm_intsrc_none); return NESTEDHVM_VMEXIT_DONE; } +#endif static void __vmx_inject_exception (int trap,int type,int error_code) {
@@ -2851,6 +2853,7 @@ static struct hvm_function_table __initdata_cf_clobber vmx_function_table = { .handle_cd = vmx_handle_cd, .set_info_guest = vmx_set_info_guest, .set_rdtsc_exiting = vmx_set_rdtsc_exiting, +#ifdef CONFIG_NESTEDHVM .nhvm_vcpu_initialise = nvmx_vcpu_initialise, .nhvm_vcpu_destroy = nvmx_vcpu_destroy, .nhvm_vcpu_reset = nvmx_vcpu_reset,
@@ -2860,8 +2863,9 @@ static struct hvm_function_table __initdata_cf_clobber vmx_function_table = { .nhvm_vcpu_vmexit_event = nvmx_vmexit_event, .nhvm_intr_blocked = nvmx_intr_blocked, .nhvm_domain_relinquish_resources = nvmx_domain_relinquish_resources, - .update_vlapic_mode = vmx_vlapic_msr_changed, .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m, +#endif + .update_vlapic_mode = vmx_vlapic_msr_changed, .enable_msr_interception = vmx_enable_msr_interception, .altp2m_vcpu_update_p2m = vmx_vcpu_update_eptp, .altp2m_vcpu_update_vmfunc_ve = vmx_vcpu_update_vmfunc_ve,
@@ -3465,10 +3469,12 @@ static int cf_check vmx_msr_read_intercept(__vmread (GUEST_IA32_DEBUGCTL , msr_content);break ; +#ifdef CONFIG_NESTEDHVM case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_VMFUNC:if ( !nvmx_msr_read_intercept (msr, msr_content) ) goto gp_fault;break ; +#endif case MSR_IA32_MISC_ENABLE :rdmsrl (MSR_IA32_MISC_ENABLE , *msr_content);/* Debug Trace Store is not supported. */
@@ -4631,6 +4637,7 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs *regs) }break ; +#ifdef CONFIG_NESTEDHVM case EXIT_REASON_VMXOFF :case EXIT_REASON_VMXON :case EXIT_REASON_VMCLEAR :
@@ -4645,6 +4652,7 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs *regs)if (nvmx_handle_vmx_insn (regs, exit_reason) ==X86EMUL_OKAY )update_guest_eip ();break ; +#endif case EXIT_REASON_VMFUNC :if (vmx_vmfunc_intercept (regs) !=X86EMUL_OKAY )
xen/arch/x86/include/asm/hvm/nestedhvm.h +9 -1
@@ -25,14 +25,22 @@ enum nestedhvm_vmexits {/* Nested HVM on/off per domain */ static inline bool nestedhvm_enabled (const struct domain *d) { -return IS_ENABLED (CONFIG_HVM ) && (d->options & XEN_DOMCTL_CDF_nested_virt); +return IS_ENABLED (CONFIG_NESTEDHVM ) && + (d->options & XEN_DOMCTL_CDF_nested_virt); }/* Nested VCPU */ int nestedhvm_vcpu_initialise (struct vcpu *v);void nestedhvm_vcpu_destroy (struct vcpu *v);void nestedhvm_vcpu_reset (struct vcpu *v); +#ifdef CONFIG_NESTEDHVM bool nestedhvm_vcpu_in_guestmode (struct vcpu *v); +#else +static inline bool nestedhvm_vcpu_in_guestmode (struct vcpu *v) +{ +return false; +} +#endif #define nestedhvm_vcpu_enter_guestmode (v) \ vcpu_nestedhvm(v).nv_guestmode = 1#define nestedhvm_vcpu_exit_guestmode (v) \
Signature · 1 line
2.25.1
Re: [XEN PATCH v1 4/4] x86:hvm: make nestedhvm support optional
Quoted 5 lines · Sergiy Kibrik
Introduce NESTEDHVM config option that controls nested virtualization in both SVM & VMX code. The option is for reduction of dead code on systems that aren't intended to run in nested mode.
Signed-off-by Sergiy Kibrik <[email protected]>
I know you're trying to remove unused code, but you're #ifdef-ing out non-dead code which reachable by guests.
You cannot #ifdef out any of the VMExit or MSR handling. They still need handling if the guest does them, and falling to defaults doesn't work. e.g. we'll now crash the VM if it executes an STGI instruction, rather than injecting #UD.
~Andrew
Re: [XEN PATCH v1 1/4] x86:svm: use nsvm_efer_svm_enabled() to check guest's EFER.SVME
In reply to Sergiy KibrikQuoted 3 lines · Sergiy Kibrik
There's a macro for this, might improve readability a bit & save a bit of space.
Signed-off-by Sergiy Kibrik <[email protected]>
Re: [XEN PATCH v1 2/4] x86:svm: rename svm/nestedhvm.h -> svm/nestedsvm.h
In reply to Sergiy KibrikQuoted 10 lines · Sergiy Kibrik
This is private header for SVM code, comment in header and file's changelog suggest that intended name was
svm/nestedsvm.h, so perhaps a small mistake here.Signed-off-by Sergiy Kibrik <[email protected]>--- xen/arch/x86/hvm/svm/intr.c | 2 +- xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} | 0 xen/arch/x86/hvm/svm/svm.c | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename xen/arch/x86/hvm/svm/{nestedhvm.h => nestedsvm.h} (100%)
Yet then, if already we rename it, why not to just svm/nested.h?
Jan
Re: [XEN PATCH v1 3/4] x86:hvm: guard calls to nestedhvm routines
In reply to Sergiy KibrikQuoted 9 lines · Sergiy Kibrik
Check whether nested HVM is enabled for domain before calling nestedhvm_vcpu_*() and other not already guarded nestedhvm functions.
Signed-off-by Sergiy Kibrik <[email protected]>--- xen/arch/x86/hvm/hvm.c | 6 ++++-- xen/arch/x86/hvm/svm/asid.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-)
Afaics common and VMX code have quite a few more references to nestedhvm_vcpu_in_guestmode(), without nestedhvm_enabled(). Are they all fine to keep as is, while the respective adjustments here are strictly necessary? In fact I wonder whether (a) nestedhvm_vcpu_in_guestmode() couldn't be made constant-false when nested is build-time disabled, which ought to eliminate the need for those changes below. Or whether
(b) nestedhvm_vcpu_in_guestmode() wouldn't better include a nestedhvm_enabled() check.
xen/arch/x86/hvm/svm/asid.c void svm_asid_handle_vmrun(void) · line 33
Quoted 8 lines
xen/arch/x86/hvm/svm/asid.c +1
-1@@ -30,7 +30,7 @@ void svm_asid_handle_vmrun(void)struct vcpu *curr = current;struct vmcb_struct *vmcb = curr->arch.hvm.svm.vmcb;struct hvm_vcpu_asid *p_asid = -nestedhvm_vcpu_in_guestmode (curr) + (nestedhvm_enabled (curr->domain) &&nestedhvm_vcpu_in_guestmode (curr) )
Nit: No blanks inside parentheses like these. (There's no real need for parentheses here in the first place.)
Jan
Re: [XEN PATCH v1 4/4] x86:hvm: make nestedhvm support optional
In reply to Sergiy KibrikHi,
Quoted 29 lines · Sergiy Kibrik
Introduce NESTEDHVM config option that controls nested virtualization in both SVM & VMX code. The option is for reduction of dead code on systems that aren't intended to run in nested mode.
Signed-off-by Sergiy Kibrik [email protected]---
xen/arch/x86/Kconfig | 5 +++++ xen/arch/x86/hvm/Makefile | 2 +- xen/arch/x86/hvm/svm/Makefile | 2 +-xen/arch/x86/hvm/svm/entry.S| 2 ++xen/arch/x86/hvm/svm/svm.c| 4 ++++ xen/arch/x86/hvm/vmx/Makefile | 2 +-xen/arch/x86/hvm/vmx/entry.S| 2 ++xen/arch/x86/hvm/vmx/vmcs.c| 8 +++++---xen/arch/x86/hvm/vmx/vmx.c| 10 +++++++++-xen/arch/x86/include/asm/hvm/nestedhvm.h| 10 +++++++++-10 files changed, 39 insertions(+), 8 deletions(-)xen/arch/x86/Kconfig +0
-0@@ -144,6 +144,11 @@ config INTEL_VMXIf your system includes a processor with Intel VT-x support, say Y. If in doubt, say Y.
Quoted 4 lines
+config NESTEDHVM
+ bool "Support nested virtualization" if EXPERT
+ depends on HVM
+ default y
There's dedicated Kconfig for HVM build-time configuration on staging at:
arch/x86/hvm/Kconfig
Suggest plumbing new knob there.
Thanks,
Denis