Skip to content

Commit cc440cd

Browse files
committed
KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE
Similar to VMX, the state that is captured through the currently available IOCTLs is a mix of L1 and L2 state, dependent on whether the L2 guest was running at the moment when the process was interrupted to save its state. In particular, the SVM-specific state for nested virtualization includes the L1 saved state (including the interrupt flag), the cached L2 controls, and the GIF. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 8ec107c commit cc440cd

6 files changed

Lines changed: 171 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,22 @@ struct kvm_sync_regs {
385385
#define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4)
386386

387387
#define KVM_STATE_NESTED_FORMAT_VMX 0
388-
#define KVM_STATE_NESTED_FORMAT_SVM 1 /* unused */
388+
#define KVM_STATE_NESTED_FORMAT_SVM 1
389389

390390
#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
391391
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
392392
#define KVM_STATE_NESTED_EVMCS 0x00000004
393393
#define KVM_STATE_NESTED_MTF_PENDING 0x00000008
394+
#define KVM_STATE_NESTED_GIF_SET 0x00000100
394395

395396
#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
396397
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
397398

398399
#define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
399400

401+
#define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000
402+
403+
400404
struct kvm_vmx_nested_state_data {
401405
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
402406
__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
@@ -411,6 +415,15 @@ struct kvm_vmx_nested_state_hdr {
411415
} smm;
412416
};
413417

418+
struct kvm_svm_nested_state_data {
419+
/* Save area only used if KVM_STATE_NESTED_RUN_PENDING. */
420+
__u8 vmcb12[KVM_STATE_NESTED_SVM_VMCB_SIZE];
421+
};
422+
423+
struct kvm_svm_nested_state_hdr {
424+
__u64 vmcb_pa;
425+
};
426+
414427
/* for KVM_CAP_NESTED_STATE */
415428
struct kvm_nested_state {
416429
__u16 flags;
@@ -419,6 +432,7 @@ struct kvm_nested_state {
419432

420433
union {
421434
struct kvm_vmx_nested_state_hdr vmx;
435+
struct kvm_svm_nested_state_hdr svm;
422436

423437
/* Pad the header to 128 bytes. */
424438
__u8 pad[120];
@@ -431,6 +445,7 @@ struct kvm_nested_state {
431445
*/
432446
union {
433447
struct kvm_vmx_nested_state_data vmx[0];
448+
struct kvm_svm_nested_state_data svm[0];
434449
} data;
435450
};
436451

‎arch/x86/kvm/cpuid.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,9 @@ static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature)
303303
kvm_cpu_cap_set(x86_feature);
304304
}
305305

306+
static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
307+
{
308+
return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
309+
}
310+
306311
#endif

‎arch/x86/kvm/svm/nested.c‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "trace.h"
2626
#include "mmu.h"
2727
#include "x86.h"
28+
#include "cpuid.h"
2829
#include "lapic.h"
2930
#include "svm.h"
3031

@@ -238,6 +239,8 @@ static void load_nested_vmcb_control(struct vcpu_svm *svm,
238239
{
239240
copy_vmcb_control_area(&svm->nested.ctl, control);
240241

242+
/* Copy it here because nested_svm_check_controls will check it. */
243+
svm->nested.ctl.asid = control->asid;
241244
svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
242245
svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
243246
}
@@ -930,6 +933,150 @@ int nested_svm_exit_special(struct vcpu_svm *svm)
930933
return NESTED_EXIT_CONTINUE;
931934
}
932935

936+
static int svm_get_nested_state(struct kvm_vcpu *vcpu,
937+
struct kvm_nested_state __user *user_kvm_nested_state,
938+
u32 user_data_size)
939+
{
940+
struct vcpu_svm *svm;
941+
struct kvm_nested_state kvm_state = {
942+
.flags = 0,
943+
.format = KVM_STATE_NESTED_FORMAT_SVM,
944+
.size = sizeof(kvm_state),
945+
};
946+
struct vmcb __user *user_vmcb = (struct vmcb __user *)
947+
&user_kvm_nested_state->data.svm[0];
948+
949+
if (!vcpu)
950+
return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
951+
952+
svm = to_svm(vcpu);
953+
954+
if (user_data_size < kvm_state.size)
955+
goto out;
956+
957+
/* First fill in the header and copy it out. */
958+
if (is_guest_mode(vcpu)) {
959+
kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb;
960+
kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
961+
kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
962+
963+
if (svm->nested.nested_run_pending)
964+
kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
965+
}
966+
967+
if (gif_set(svm))
968+
kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
969+
970+
if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
971+
return -EFAULT;
972+
973+
if (!is_guest_mode(vcpu))
974+
goto out;
975+
976+
/*
977+
* Copy over the full size of the VMCB rather than just the size
978+
* of the structs.
979+
*/
980+
if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
981+
return -EFAULT;
982+
if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
983+
sizeof(user_vmcb->control)))
984+
return -EFAULT;
985+
if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
986+
sizeof(user_vmcb->save)))
987+
return -EFAULT;
988+
989+
out:
990+
return kvm_state.size;
991+
}
992+
993+
static int svm_set_nested_state(struct kvm_vcpu *vcpu,
994+
struct kvm_nested_state __user *user_kvm_nested_state,
995+
struct kvm_nested_state *kvm_state)
996+
{
997+
struct vcpu_svm *svm = to_svm(vcpu);
998+
struct vmcb *hsave = svm->nested.hsave;
999+
struct vmcb __user *user_vmcb = (struct vmcb __user *)
1000+
&user_kvm_nested_state->data.svm[0];
1001+
struct vmcb_control_area ctl;
1002+
struct vmcb_save_area save;
1003+
u32 cr0;
1004+
1005+
if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1006+
return -EINVAL;
1007+
1008+
if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1009+
KVM_STATE_NESTED_RUN_PENDING |
1010+
KVM_STATE_NESTED_GIF_SET))
1011+
return -EINVAL;
1012+
1013+
/*
1014+
* If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1015+
* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1016+
*/
1017+
if (!(vcpu->arch.efer & EFER_SVME)) {
1018+
/* GIF=1 and no guest mode are required if SVME=0. */
1019+
if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1020+
return -EINVAL;
1021+
}
1022+
1023+
/* SMM temporarily disables SVM, so we cannot be in guest mode. */
1024+
if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1025+
return -EINVAL;
1026+
1027+
if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1028+
svm_leave_nested(svm);
1029+
goto out_set_gif;
1030+
}
1031+
1032+
if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1033+
return -EINVAL;
1034+
if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1035+
return -EINVAL;
1036+
if (copy_from_user(&ctl, &user_vmcb->control, sizeof(ctl)))
1037+
return -EFAULT;
1038+
if (copy_from_user(&save, &user_vmcb->save, sizeof(save)))
1039+
return -EFAULT;
1040+
1041+
if (!nested_vmcb_check_controls(&ctl))
1042+
return -EINVAL;
1043+
1044+
/*
1045+
* Processor state contains L2 state. Check that it is
1046+
* valid for guest mode (see nested_vmcb_checks).
1047+
*/
1048+
cr0 = kvm_read_cr0(vcpu);
1049+
if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1050+
return -EINVAL;
1051+
1052+
/*
1053+
* Validate host state saved from before VMRUN (see
1054+
* nested_svm_check_permissions).
1055+
* TODO: validate reserved bits for all saved state.
1056+
*/
1057+
if (!(save.cr0 & X86_CR0_PG))
1058+
return -EINVAL;
1059+
1060+
/*
1061+
* All checks done, we can enter guest mode. L1 control fields
1062+
* come from the nested save state. Guest state is already
1063+
* in the registers, the save area of the nested state instead
1064+
* contains saved L1 state.
1065+
*/
1066+
copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
1067+
hsave->save = save;
1068+
1069+
svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
1070+
load_nested_vmcb_control(svm, &ctl);
1071+
nested_prepare_vmcb_control(svm);
1072+
1073+
out_set_gif:
1074+
svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1075+
return 0;
1076+
}
1077+
9331078
struct kvm_x86_nested_ops svm_nested_ops = {
9341079
.check_events = svm_check_nested_events,
1080+
.get_state = svm_get_nested_state,
1081+
.set_state = svm_set_nested_state,
9351082
};

‎arch/x86/kvm/svm/svm.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
11931193
svm->avic_is_running = true;
11941194

11951195
svm->nested.hsave = page_address(hsave_page);
1196+
clear_page(svm->nested.hsave);
11961197

11971198
svm->msrpm = page_address(msrpm_pages);
11981199
svm_vcpu_init_msrpm(svm->msrpm);

‎arch/x86/kvm/vmx/nested.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,6 @@ static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
437437
}
438438
}
439439

440-
static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
441-
{
442-
return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
443-
}
444-
445440
static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
446441
struct vmcs12 *vmcs12)
447442
{

‎arch/x86/kvm/x86.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4628,7 +4628,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
46284628

46294629
if (kvm_state.flags &
46304630
~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4631-
| KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING))
4631+
| KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING
4632+
| KVM_STATE_NESTED_GIF_SET))
46324633
break;
46334634

46344635
/* nested_run_pending implies guest_mode. */

0 commit comments

Comments
 (0)