Skip to content

Commit 8cab650

Browse files
vittyvkbonzini
authored andcommitted
x86/kvm/nVMX: nested state migration for Enlightened VMCS
Add support for get/set of nested state when Enlightened VMCS is in use. A new KVM_STATE_NESTED_EVMCS flag to indicate eVMCS on the vCPU was enabled is added. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 1e7ecd1 commit 8cab650

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

arch/x86/include/uapi/asm/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ struct kvm_sync_regs {
381381

382382
#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
383383
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
384+
#define KVM_STATE_NESTED_EVMCS 0x00000004
384385

385386
#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
386387
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002

arch/x86/kvm/vmx.c

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,8 @@ static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
16181618
* maximum supported version. KVM supports versions from 1 to
16191619
* KVM_EVMCS_VERSION.
16201620
*/
1621-
*vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
1621+
if (vmcs_version)
1622+
*vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
16221623

16231624
vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
16241625
vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
@@ -9338,7 +9339,8 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)
93389339
* This is an equivalent of the nested hypervisor executing the vmptrld
93399340
* instruction.
93409341
*/
9341-
static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu)
9342+
static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
9343+
bool from_launch)
93429344
{
93439345
struct vcpu_vmx *vmx = to_vmx(vcpu);
93449346
struct hv_vp_assist_page assist_page;
@@ -9389,8 +9391,9 @@ static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu)
93899391
* present in struct hv_enlightened_vmcs, ...). Make sure there
93909392
* are no leftovers.
93919393
*/
9392-
memset(vmx->nested.cached_vmcs12, 0,
9393-
sizeof(*vmx->nested.cached_vmcs12));
9394+
if (from_launch)
9395+
memset(vmx->nested.cached_vmcs12, 0,
9396+
sizeof(*vmx->nested.cached_vmcs12));
93949397

93959398
}
93969399
return 1;
@@ -11147,6 +11150,15 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
1114711150
}
1114811151

1114911152
if (vmx->nested.need_vmcs12_sync) {
11153+
/*
11154+
* hv_evmcs may end up being not mapped after migration (when
11155+
* L2 was running), map it here to make sure vmcs12 changes are
11156+
* properly reflected.
11157+
*/
11158+
if (vmx->nested.enlightened_vmcs_enabled &&
11159+
!vmx->nested.hv_evmcs)
11160+
nested_vmx_handle_enlightened_vmptrld(vcpu, false);
11161+
1115011162
if (vmx->nested.hv_evmcs) {
1115111163
copy_vmcs12_to_enlightened(vmx);
1115211164
/* All fields are clean */
@@ -13424,7 +13436,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
1342413436
if (!nested_vmx_check_permission(vcpu))
1342513437
return 1;
1342613438

13427-
if (!nested_vmx_handle_enlightened_vmptrld(vcpu))
13439+
if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
1342813440
return 1;
1342913441

1343013442
if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
@@ -14711,6 +14723,20 @@ static int enable_smi_window(struct kvm_vcpu *vcpu)
1471114723
return 0;
1471214724
}
1471314725

14726+
static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
14727+
{
14728+
struct vcpu_vmx *vmx = to_vmx(vcpu);
14729+
14730+
/*
14731+
* In case we do two consecutive get/set_nested_state()s while L2 was
14732+
* running hv_evmcs may end up not being mapped (we map it from
14733+
* nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
14734+
* have vmcs12 if it is true.
14735+
*/
14736+
return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
14737+
vmx->nested.hv_evmcs;
14738+
}
14739+
1471414740
static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
1471514741
struct kvm_nested_state __user *user_kvm_nested_state,
1471614742
u32 user_data_size)
@@ -14731,16 +14757,15 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
1473114757
vmx = to_vmx(vcpu);
1473214758
vmcs12 = get_vmcs12(vcpu);
1473314759

14734-
/* FIXME: Enlightened VMCS is currently unsupported */
14735-
if (vmx->nested.hv_evmcs)
14736-
return -ENOTSUPP;
14760+
if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
14761+
kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
1473714762

1473814763
if (nested_vmx_allowed(vcpu) &&
1473914764
(vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
1474014765
kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
1474114766
kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
1474214767

14743-
if (vmx->nested.current_vmptr != -1ull) {
14768+
if (vmx_has_valid_vmcs12(vcpu)) {
1474414769
kvm_state.size += VMCS12_SIZE;
1474514770

1474614771
if (is_guest_mode(vcpu) &&
@@ -14769,20 +14794,24 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
1476914794
if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1477014795
return -EFAULT;
1477114796

14772-
if (vmx->nested.current_vmptr == -1ull)
14797+
if (!vmx_has_valid_vmcs12(vcpu))
1477314798
goto out;
1477414799

1477514800
/*
1477614801
* When running L2, the authoritative vmcs12 state is in the
1477714802
* vmcs02. When running L1, the authoritative vmcs12 state is
14778-
* in the shadow vmcs linked to vmcs01, unless
14803+
* in the shadow or enlightened vmcs linked to vmcs01, unless
1477914804
* need_vmcs12_sync is set, in which case, the authoritative
1478014805
* vmcs12 state is in the vmcs12 already.
1478114806
*/
14782-
if (is_guest_mode(vcpu))
14807+
if (is_guest_mode(vcpu)) {
1478314808
sync_vmcs12(vcpu, vmcs12);
14784-
else if (enable_shadow_vmcs && !vmx->nested.need_vmcs12_sync)
14785-
copy_shadow_to_vmcs12(vmx);
14809+
} else if (!vmx->nested.need_vmcs12_sync) {
14810+
if (vmx->nested.hv_evmcs)
14811+
copy_enlightened_to_vmcs12(vmx);
14812+
else if (enable_shadow_vmcs)
14813+
copy_shadow_to_vmcs12(vmx);
14814+
}
1478614815

1478714816
if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
1478814817
return -EFAULT;
@@ -14810,6 +14839,9 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
1481014839
if (kvm_state->format != 0)
1481114840
return -EINVAL;
1481214841

14842+
if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
14843+
nested_enable_evmcs(vcpu, NULL);
14844+
1481314845
if (!nested_vmx_allowed(vcpu))
1481414846
return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
1481514847

@@ -14860,11 +14892,21 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
1486014892
if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
1486114893
return 0;
1486214894

14863-
if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14864-
!page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14865-
return -EINVAL;
14895+
if (kvm_state->vmx.vmcs_pa != -1ull) {
14896+
if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14897+
!page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14898+
return -EINVAL;
1486614899

14867-
set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14900+
set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14901+
} else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
14902+
/*
14903+
* Sync eVMCS upon entry as we may not have
14904+
* HV_X64_MSR_VP_ASSIST_PAGE set up yet.
14905+
*/
14906+
vmx->nested.need_vmcs12_sync = true;
14907+
} else {
14908+
return -EINVAL;
14909+
}
1486814910

1486914911
if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
1487014912
vmx->nested.smm.vmxon = true;

arch/x86/kvm/x86.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,11 +4068,13 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
40684068
break;
40694069

40704070
if (kvm_state.flags &
4071-
~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE))
4071+
~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4072+
| KVM_STATE_NESTED_EVMCS))
40724073
break;
40734074

40744075
/* nested_run_pending implies guest_mode. */
4075-
if (kvm_state.flags == KVM_STATE_NESTED_RUN_PENDING)
4076+
if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4077+
&& !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
40764078
break;
40774079

40784080
r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);

0 commit comments

Comments
 (0)