Skip to content

Commit 801d342

Browse files
Nadav Har'Elavikivity
authored andcommitted
KVM: nVMX: Add "nested" module option to kvm_intel
This patch adds to kvm_intel a module option "nested". This option controls whether the guest can use VMX instructions, i.e., whether we allow nested virtualization. A similar, but separate, option already exists for the SVM module. This option currently defaults to 0, meaning that nested VMX must be explicitly enabled by giving nested=1. When nested VMX matures, the default should probably be changed to enable nested VMX by default - just like nested SVM is currently enabled by default. Signed-off-by: Nadav Har'El <nyh@il.ibm.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent b5c9ff7 commit 801d342

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

arch/x86/kvm/vmx.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ module_param(vmm_exclusive, bool, S_IRUGO);
7474
static int __read_mostly yield_on_hlt = 1;
7575
module_param(yield_on_hlt, bool, S_IRUGO);
7676

77+
/*
78+
* If nested=1, nested virtualization is supported, i.e., guests may use
79+
* VMX and be a hypervisor for its own guests. If nested=0, guests may not
80+
* use VMX instructions.
81+
*/
82+
static int __read_mostly nested = 0;
83+
module_param(nested, bool, S_IRUGO);
84+
7785
#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
7886
(X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
7987
#define KVM_GUEST_CR0_MASK \
@@ -1292,6 +1300,23 @@ static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
12921300
return target_tsc - native_read_tsc();
12931301
}
12941302

1303+
static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1304+
{
1305+
struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1306+
return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1307+
}
1308+
1309+
/*
1310+
* nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1311+
* instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1312+
* all guests if the "nested" module option is off, and can also be disabled
1313+
* for a single guest by disabling its VMX cpuid bit.
1314+
*/
1315+
static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1316+
{
1317+
return nested && guest_cpuid_has_vmx(vcpu);
1318+
}
1319+
12951320
/*
12961321
* Reads an msr value (of 'msr_index') into 'pdata'.
12971322
* Returns 0 on success, non-0 otherwise.

0 commit comments

Comments
 (0)