Re: [PATCH v4 2/3] selftests: KVM: AMD Nested test infrastructure

From: Auger Eric
Date: Fri Feb 07 2020 - 09:16:38 EST


Hi Krish,

On 2/6/20 11:57 PM, Krish Sadhukhan wrote:
>
>
> On 02/06/2020 02:47 AM, Eric Auger wrote:
>> Add the basic infrastructure needed to test AMD nested SVM.
>> This is largely copied from the KVM unit test infrastructure.
>>
>> Signed-off-by: Eric Auger <eric.auger@xxxxxxxxxx>
>>
>> ---
>>
>> v3 -> v4:
>> - just keep the 16 GPRs in gpr64_regs struct
>> - vm* instructions do not take any param
>> - add comments
>>
>> v2 -> v3:
>> - s/regs/gp_regs64
>> - Split the header into 2 parts: svm.h is a copy of
>> ÂÂ arch/x86/include/asm/svm.h whereas svm_util.h contains
>> ÂÂ testing add-ons
>> - use get_gdt/dt() and remove sgdt/sidt
>> - use get_es/ss/ds/cs
>> - fix clobber for dr6 & dr7
>> - use u64 instead of ulong
>> ---
>> Â tools/testing/selftests/kvm/MakefileÂÂÂÂÂÂÂÂÂ |ÂÂ 2 +-
>>  .../selftests/kvm/include/x86_64/processor.h | 20 ++
>> Â .../selftests/kvm/include/x86_64/svm.hÂÂÂÂÂÂÂ | 297 ++++++++++++++++++
>> Â .../selftests/kvm/include/x86_64/svm_util.hÂÂ |Â 38 +++
>>  tools/testing/selftests/kvm/lib/x86_64/svm.c | 161 ++++++++++
>> Â 5 files changed, 517 insertions(+), 1 deletion(-)
>> Â create mode 100644 tools/testing/selftests/kvm/include/x86_64/svm.h
>> Â create mode 100644
>> tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> Â create mode 100644 tools/testing/selftests/kvm/lib/x86_64/svm.c
>>
>> diff --git a/tools/testing/selftests/kvm/Makefile
>> b/tools/testing/selftests/kvm/Makefile
>> index 608fa835c764..2e770f554cae 100644
>> --- a/tools/testing/selftests/kvm/Makefile
>> +++ b/tools/testing/selftests/kvm/Makefile
>> @@ -8,7 +8,7 @@ KSFT_KHDR_INSTALL := 1
>> Â UNAME_M := $(shell uname -m)
>> Â Â LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c
>> lib/sparsebit.c
>> -LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c
>> lib/x86_64/ucall.c
>> +LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c
>> lib/x86_64/svm.c lib/x86_64/ucall.c
>> Â LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
>> Â LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c
>> Â diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h
>> b/tools/testing/selftests/kvm/include/x86_64/processor.h
>> index 6f7fffaea2e8..12475047869f 100644
>> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
>> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
>> @@ -56,6 +56,26 @@ enum x86_register {
>> ÂÂÂÂÂ R15,
>> Â };
>> Â +/* General Registers in 64-Bit Mode */
>> +struct gpr64_regs {
>> +ÂÂÂ u64 rax;
>> +ÂÂÂ u64 rcx;
>> +ÂÂÂ u64 rdx;
>> +ÂÂÂ u64 rbx;
>> +ÂÂÂ u64 rsp;
>> +ÂÂÂ u64 rbp;
>> +ÂÂÂ u64 rsi;
>> +ÂÂÂ u64 rdi;
>> +ÂÂÂ u64 r8;
>> +ÂÂÂ u64 r9;
>> +ÂÂÂ u64 r10;
>> +ÂÂÂ u64 r11;
>> +ÂÂÂ u64 r12;
>> +ÂÂÂ u64 r13;
>> +ÂÂÂ u64 r14;
>> +ÂÂÂ u64 r15;
>> +};
>> +
>> Â struct desc64 {
>> ÂÂÂÂÂ uint16_t limit0;
>> ÂÂÂÂÂ uint16_t base0;
>> diff --git a/tools/testing/selftests/kvm/include/x86_64/svm.h
>> b/tools/testing/selftests/kvm/include/x86_64/svm.h
>> new file mode 100644
>> index 000000000000..f4ea2355dbc2
>> --- /dev/null
>> +++ b/tools/testing/selftests/kvm/include/x86_64/svm.h
>> @@ -0,0 +1,297 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/*
>> + * tools/testing/selftests/kvm/include/x86_64/svm.h
>> + * This is a copy of arch/x86/include/asm/svm.h
>> + *
>> + */
>> +
>> +#ifndef SELFTEST_KVM_SVM_H
>> +#define SELFTEST_KVM_SVM_H
>> +
>> +enum {
>> +ÂÂÂ INTERCEPT_INTR,
>> +ÂÂÂ INTERCEPT_NMI,
>> +ÂÂÂ INTERCEPT_SMI,
>> +ÂÂÂ INTERCEPT_INIT,
>> +ÂÂÂ INTERCEPT_VINTR,
>> +ÂÂÂ INTERCEPT_SELECTIVE_CR0,
>> +ÂÂÂ INTERCEPT_STORE_IDTR,
>> +ÂÂÂ INTERCEPT_STORE_GDTR,
>> +ÂÂÂ INTERCEPT_STORE_LDTR,
>> +ÂÂÂ INTERCEPT_STORE_TR,
>> +ÂÂÂ INTERCEPT_LOAD_IDTR,
>> +ÂÂÂ INTERCEPT_LOAD_GDTR,
>> +ÂÂÂ INTERCEPT_LOAD_LDTR,
>> +ÂÂÂ INTERCEPT_LOAD_TR,
>> +ÂÂÂ INTERCEPT_RDTSC,
>> +ÂÂÂ INTERCEPT_RDPMC,
>> +ÂÂÂ INTERCEPT_PUSHF,
>> +ÂÂÂ INTERCEPT_POPF,
>> +ÂÂÂ INTERCEPT_CPUID,
>> +ÂÂÂ INTERCEPT_RSM,
>> +ÂÂÂ INTERCEPT_IRET,
>> +ÂÂÂ INTERCEPT_INTn,
>> +ÂÂÂ INTERCEPT_INVD,
>> +ÂÂÂ INTERCEPT_PAUSE,
>> +ÂÂÂ INTERCEPT_HLT,
>> +ÂÂÂ INTERCEPT_INVLPG,
>> +ÂÂÂ INTERCEPT_INVLPGA,
>> +ÂÂÂ INTERCEPT_IOIO_PROT,
>> +ÂÂÂ INTERCEPT_MSR_PROT,
>> +ÂÂÂ INTERCEPT_TASK_SWITCH,
>> +ÂÂÂ INTERCEPT_FERR_FREEZE,
>> +ÂÂÂ INTERCEPT_SHUTDOWN,
>> +ÂÂÂ INTERCEPT_VMRUN,
>> +ÂÂÂ INTERCEPT_VMMCALL,
>> +ÂÂÂ INTERCEPT_VMLOAD,
>> +ÂÂÂ INTERCEPT_VMSAVE,
>> +ÂÂÂ INTERCEPT_STGI,
>> +ÂÂÂ INTERCEPT_CLGI,
>> +ÂÂÂ INTERCEPT_SKINIT,
>> +ÂÂÂ INTERCEPT_RDTSCP,
>> +ÂÂÂ INTERCEPT_ICEBP,
>> +ÂÂÂ INTERCEPT_WBINVD,
>> +ÂÂÂ INTERCEPT_MONITOR,
>> +ÂÂÂ INTERCEPT_MWAIT,
>> +ÂÂÂ INTERCEPT_MWAIT_COND,
>> +ÂÂÂ INTERCEPT_XSETBV,
>> +ÂÂÂ INTERCEPT_RDPRU,
>> +};
>> +
>> +
>> +struct __attribute__ ((__packed__)) vmcb_control_area {
>> +ÂÂÂ u32 intercept_cr;
>> +ÂÂÂ u32 intercept_dr;
>> +ÂÂÂ u32 intercept_exceptions;
>> +ÂÂÂ u64 intercept;
>> +ÂÂÂ u8 reserved_1[40];
>> +ÂÂÂ u16 pause_filter_thresh;
>> +ÂÂÂ u16 pause_filter_count;
>> +ÂÂÂ u64 iopm_base_pa;
>> +ÂÂÂ u64 msrpm_base_pa;
>> +ÂÂÂ u64 tsc_offset;
>> +ÂÂÂ u32 asid;
>> +ÂÂÂ u8 tlb_ctl;
>> +ÂÂÂ u8 reserved_2[3];
>> +ÂÂÂ u32 int_ctl;
>> +ÂÂÂ u32 int_vector;
>> +ÂÂÂ u32 int_state;
>> +ÂÂÂ u8 reserved_3[4];
>> +ÂÂÂ u32 exit_code;
>> +ÂÂÂ u32 exit_code_hi;
>> +ÂÂÂ u64 exit_info_1;
>> +ÂÂÂ u64 exit_info_2;
>> +ÂÂÂ u32 exit_int_info;
>> +ÂÂÂ u32 exit_int_info_err;
>> +ÂÂÂ u64 nested_ctl;
>> +ÂÂÂ u64 avic_vapic_bar;
>> +ÂÂÂ u8 reserved_4[8];
>> +ÂÂÂ u32 event_inj;
>> +ÂÂÂ u32 event_inj_err;
>> +ÂÂÂ u64 nested_cr3;
>> +ÂÂÂ u64 virt_ext;
>> +ÂÂÂ u32 clean;
>> +ÂÂÂ u32 reserved_5;
>> +ÂÂÂ u64 next_rip;
>> +ÂÂÂ u8 insn_len;
>> +ÂÂÂ u8 insn_bytes[15];
>> +ÂÂÂ u64 avic_backing_page;ÂÂÂ /* Offset 0xe0 */
>> +ÂÂÂ u8 reserved_6[8];ÂÂÂ /* Offset 0xe8 */
>> +ÂÂÂ u64 avic_logical_id;ÂÂÂ /* Offset 0xf0 */
>> +ÂÂÂ u64 avic_physical_id;ÂÂÂ /* Offset 0xf8 */
>> +ÂÂÂ u8 reserved_7[768];
>> +};
>> +
>> +
>> +#define TLB_CONTROL_DO_NOTHING 0
>> +#define TLB_CONTROL_FLUSH_ALL_ASID 1
>> +#define TLB_CONTROL_FLUSH_ASID 3
>> +#define TLB_CONTROL_FLUSH_ASID_LOCAL 7
>> +
>> +#define V_TPR_MASK 0x0f
>> +
>> +#define V_IRQ_SHIFT 8
>> +#define V_IRQ_MASK (1 << V_IRQ_SHIFT)
>> +
>> +#define V_GIF_SHIFT 9
>> +#define V_GIF_MASK (1 << V_GIF_SHIFT)
>> +
>> +#define V_INTR_PRIO_SHIFT 16
>> +#define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
>> +
>> +#define V_IGN_TPR_SHIFT 20
>> +#define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
>> +
>> +#define V_INTR_MASKING_SHIFT 24
>> +#define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
>> +
>> +#define V_GIF_ENABLE_SHIFT 25
>> +#define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
>> +
>> +#define AVIC_ENABLE_SHIFT 31
>> +#define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
>> +
>> +#define LBR_CTL_ENABLE_MASK BIT_ULL(0)
>> +#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
>> +
>> +#define SVM_INTERRUPT_SHADOW_MASK 1
>> +
>> +#define SVM_IOIO_STR_SHIFT 2
>> +#define SVM_IOIO_REP_SHIFT 3
>> +#define SVM_IOIO_SIZE_SHIFT 4
>> +#define SVM_IOIO_ASIZE_SHIFT 7
>> +
>> +#define SVM_IOIO_TYPE_MASK 1
>> +#define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
>> +#define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
>> +#define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
>> +#define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
>> +
>> +#define SVM_VM_CR_VALID_MASKÂÂÂ 0x001fULL
>> +#define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
>> +#define SVM_VM_CR_SVM_DIS_MASKÂ 0x0010ULL
>> +
>> +#define SVM_NESTED_CTL_NP_ENABLEÂÂÂ BIT(0)
>> +#define SVM_NESTED_CTL_SEV_ENABLEÂÂÂ BIT(1)
>> +
>> +struct __attribute__ ((__packed__)) vmcb_seg {
>> +ÂÂÂ u16 selector;
>> +ÂÂÂ u16 attrib;
>> +ÂÂÂ u32 limit;
>> +ÂÂÂ u64 base;
>> +};
>> +
>> +struct __attribute__ ((__packed__)) vmcb_save_area {
>> +ÂÂÂ struct vmcb_seg es;
>> +ÂÂÂ struct vmcb_seg cs;
>> +ÂÂÂ struct vmcb_seg ss;
>> +ÂÂÂ struct vmcb_seg ds;
>> +ÂÂÂ struct vmcb_seg fs;
>> +ÂÂÂ struct vmcb_seg gs;
>> +ÂÂÂ struct vmcb_seg gdtr;
>> +ÂÂÂ struct vmcb_seg ldtr;
>> +ÂÂÂ struct vmcb_seg idtr;
>> +ÂÂÂ struct vmcb_seg tr;
>> +ÂÂÂ u8 reserved_1[43];
>> +ÂÂÂ u8 cpl;
>> +ÂÂÂ u8 reserved_2[4];
>> +ÂÂÂ u64 efer;
>> +ÂÂÂ u8 reserved_3[112];
>> +ÂÂÂ u64 cr4;
>> +ÂÂÂ u64 cr3;
>> +ÂÂÂ u64 cr0;
>> +ÂÂÂ u64 dr7;
>> +ÂÂÂ u64 dr6;
>> +ÂÂÂ u64 rflags;
>> +ÂÂÂ u64 rip;
>> +ÂÂÂ u8 reserved_4[88];
>> +ÂÂÂ u64 rsp;
>> +ÂÂÂ u8 reserved_5[24];
>> +ÂÂÂ u64 rax;
>> +ÂÂÂ u64 star;
>> +ÂÂÂ u64 lstar;
>> +ÂÂÂ u64 cstar;
>> +ÂÂÂ u64 sfmask;
>> +ÂÂÂ u64 kernel_gs_base;
>> +ÂÂÂ u64 sysenter_cs;
>> +ÂÂÂ u64 sysenter_esp;
>> +ÂÂÂ u64 sysenter_eip;
>> +ÂÂÂ u64 cr2;
>> +ÂÂÂ u8 reserved_6[32];
>> +ÂÂÂ u64 g_pat;
>> +ÂÂÂ u64 dbgctl;
>> +ÂÂÂ u64 br_from;
>> +ÂÂÂ u64 br_to;
>> +ÂÂÂ u64 last_excp_from;
>> +ÂÂÂ u64 last_excp_to;
>> +};
>> +
>> +struct __attribute__ ((__packed__)) vmcb {
>> +ÂÂÂ struct vmcb_control_area control;
>> +ÂÂÂ struct vmcb_save_area save;
>> +};
>> +
>> +#define SVM_CPUID_FUNC 0x8000000a
>> +
>> +#define SVM_VM_CR_SVM_DISABLE 4
>> +
>> +#define SVM_SELECTOR_S_SHIFT 4
>> +#define SVM_SELECTOR_DPL_SHIFT 5
>> +#define SVM_SELECTOR_P_SHIFT 7
>> +#define SVM_SELECTOR_AVL_SHIFT 8
>> +#define SVM_SELECTOR_L_SHIFT 9
>> +#define SVM_SELECTOR_DB_SHIFT 10
>> +#define SVM_SELECTOR_G_SHIFT 11
>> +
>> +#define SVM_SELECTOR_TYPE_MASK (0xf)
>> +#define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
>> +#define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
>> +#define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
>> +#define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
>> +#define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
>> +#define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
>> +#define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
>> +
>> +#define SVM_SELECTOR_WRITE_MASK (1 << 1)
>> +#define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
>> +#define SVM_SELECTOR_CODE_MASK (1 << 3)
>> +
>> +#define INTERCEPT_CR0_READÂÂÂ 0
>> +#define INTERCEPT_CR3_READÂÂÂ 3
>> +#define INTERCEPT_CR4_READÂÂÂ 4
>> +#define INTERCEPT_CR8_READÂÂÂ 8
>> +#define INTERCEPT_CR0_WRITEÂÂÂ (16 + 0)
>> +#define INTERCEPT_CR3_WRITEÂÂÂ (16 + 3)
>> +#define INTERCEPT_CR4_WRITEÂÂÂ (16 + 4)
>> +#define INTERCEPT_CR8_WRITEÂÂÂ (16 + 8)
>> +
>> +#define INTERCEPT_DR0_READÂÂÂ 0
>> +#define INTERCEPT_DR1_READÂÂÂ 1
>> +#define INTERCEPT_DR2_READÂÂÂ 2
>> +#define INTERCEPT_DR3_READÂÂÂ 3
>> +#define INTERCEPT_DR4_READÂÂÂ 4
>> +#define INTERCEPT_DR5_READÂÂÂ 5
>> +#define INTERCEPT_DR6_READÂÂÂ 6
>> +#define INTERCEPT_DR7_READÂÂÂ 7
>> +#define INTERCEPT_DR0_WRITEÂÂÂ (16 + 0)
>> +#define INTERCEPT_DR1_WRITEÂÂÂ (16 + 1)
>> +#define INTERCEPT_DR2_WRITEÂÂÂ (16 + 2)
>> +#define INTERCEPT_DR3_WRITEÂÂÂ (16 + 3)
>> +#define INTERCEPT_DR4_WRITEÂÂÂ (16 + 4)
>> +#define INTERCEPT_DR5_WRITEÂÂÂ (16 + 5)
>> +#define INTERCEPT_DR6_WRITEÂÂÂ (16 + 6)
>> +#define INTERCEPT_DR7_WRITEÂÂÂ (16 + 7)
>> +
>> +#define SVM_EVTINJ_VEC_MASK 0xff
>> +
>> +#define SVM_EVTINJ_TYPE_SHIFT 8
>> +#define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
>> +
>> +#define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
>> +#define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
>> +#define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
>> +#define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
>> +
>> +#define SVM_EVTINJ_VALID (1 << 31)
>> +#define SVM_EVTINJ_VALID_ERR (1 << 11)
>> +
>> +#define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
>> +#define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
>> +
>> +#defineÂÂÂ SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
>> +#defineÂÂÂ SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
>> +#defineÂÂÂ SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
>> +#defineÂÂÂ SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
>> +
>> +#define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
>> +#define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
>> +
>> +#define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
>> +#define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
>> +#define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
>> +
>> +#define SVM_EXITINFO_REG_MASK 0x0F
>> +
>> +#define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
>> +
>> +#endif /* SELFTEST_KVM_SVM_H */
>> diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> new file mode 100644
>> index 000000000000..cd037917fece
>> --- /dev/null
>> +++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> @@ -0,0 +1,38 @@
>> +/* SPDX-License-Identifier: GPL-2.0-only */
>> +/*
>> + * tools/testing/selftests/kvm/include/x86_64/svm_utils.h
>> + * Header for nested SVM testing
>> + *
>> + * Copyright (C) 2020, Red Hat, Inc.
>> + */
>> +
>> +#ifndef SELFTEST_KVM_SVM_UTILS_H
>> +#define SELFTEST_KVM_SVM_UTILS_H
>> +
>> +#include <stdint.h>
>> +#include "svm.h"
>> +#include "processor.h"
>> +
>> +#define CPUID_SVM_BITÂÂÂÂÂÂÂ 2
>> +#define CPUID_SVMÂÂÂÂÂÂÂ BIT_ULL(CPUID_SVM_BIT)
>> +
>> +#define SVM_EXIT_VMMCALLÂÂÂ 0x081
>> +
>> +struct svm_test_data {
>> +ÂÂÂ /* VMCB */
>> +ÂÂÂ struct vmcb *vmcb; /* gva */
>> +ÂÂÂ void *vmcb_hva;
>> +ÂÂÂ uint64_t vmcb_gpa;
>> +
>> +ÂÂÂ /* host state-save area */
>> +ÂÂÂ struct vmcb_save_area *save_area; /* gva */
>> +ÂÂÂ void *save_area_hva;
>> +ÂÂÂ uint64_t save_area_gpa;
>> +};
> Looks like vmcb_hva and save_area_hva haven't been used anywhere. Do we
> need them ?
Yes I can remove them for now.
>> +
>> +struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t
>> *p_svm_gva);
>> +void generic_svm_setup(struct svm_test_data *svm, void *guest_rip,
>> void *guest_rsp);
>> +void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
>> +void nested_svm_check_supported(void);
>> +
>> +#endif /* SELFTEST_KVM_SVM_UTILS_H */
>> diff --git a/tools/testing/selftests/kvm/lib/x86_64/svm.c
>> b/tools/testing/selftests/kvm/lib/x86_64/svm.c
>> new file mode 100644
>> index 000000000000..6e05a8fc3fe0
>> --- /dev/null
>> +++ b/tools/testing/selftests/kvm/lib/x86_64/svm.c
>> @@ -0,0 +1,161 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * tools/testing/selftests/kvm/lib/x86_64/svm.c
>> + * Helpers used for nested SVM testing
>> + * Largely inspired from KVM unit test svm.c
>> + *
>> + * Copyright (C) 2020, Red Hat, Inc.
>> + */
>> +
>> +#include "test_util.h"
>> +#include "kvm_util.h"
>> +#include "../kvm_util_internal.h"
>> +#include "processor.h"
>> +#include "svm_util.h"
>> +
>> +struct gpr64_regs guest_regs;
>> +u64 rflags;
>> +
>> +/* Allocate memory regions for nested SVM tests.
>> + *
>> + * Input Args:
>> + *ÂÂ vm - The VM to allocate guest-virtual addresses in.
>> + *
>> + * Output Args:
>> + *ÂÂ p_svm_gva - The guest virtual address for the struct svm_test_data.
>> + *
>> + * Return:
>> + *ÂÂ Pointer to structure with the addresses of the SVM areas.
>> + */
>> +struct svm_test_data *
>> +vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva)
>> +{
>> +ÂÂÂ vm_vaddr_t svm_gva = vm_vaddr_alloc(vm, getpagesize(),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 0x10000, 0, 0);
>> +ÂÂÂ struct svm_test_data *svm = addr_gva2hva(vm, svm_gva);
>> +
>> +ÂÂÂ svm->vmcb = (void *)vm_vaddr_alloc(vm, getpagesize(),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 0x10000, 0, 0);
>> +ÂÂÂ svm->vmcb_hva = addr_gva2hva(vm, (uintptr_t)svm->vmcb);
>> +ÂÂÂ svm->vmcb_gpa = addr_gva2gpa(vm, (uintptr_t)svm->vmcb);
>> +
>> +ÂÂÂ svm->save_area = (void *)vm_vaddr_alloc(vm, getpagesize(),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 0x10000, 0, 0);
>> +ÂÂÂ svm->save_area_hva = addr_gva2hva(vm, (uintptr_t)svm->save_area);
>> +ÂÂÂ svm->save_area_gpa = addr_gva2gpa(vm, (uintptr_t)svm->save_area);
>> +
>> +ÂÂÂ *p_svm_gva = svm_gva;
>> +ÂÂÂ return svm;
>> +}
>> +
>> +static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂ u64 base, u32 limit, u32 attr)
>> +{
>> +ÂÂÂ seg->selector = selector;
>> +ÂÂÂ seg->attrib = attr;
>> +ÂÂÂ seg->limit = limit;
>> +ÂÂÂ seg->base = base;
>> +}
>> +
>> +void generic_svm_setup(struct svm_test_data *svm, void *guest_rip,
>> void *guest_rsp)
>> +{
>> +ÂÂÂ struct vmcb *vmcb = svm->vmcb;
>> +ÂÂÂ uint64_t vmcb_gpa = svm->vmcb_gpa;
>> +ÂÂÂ struct vmcb_save_area *save = &vmcb->save;
>> +ÂÂÂ struct vmcb_control_area *ctrl = &vmcb->control;
>> +ÂÂÂ u32 data_seg_attr = 3 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_P_MASK
>> +ÂÂÂÂÂÂÂÂÂ | SVM_SELECTOR_DB_MASK | SVM_SELECTOR_G_MASK;
>> +ÂÂÂ u32 code_seg_attr = 9 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_P_MASK
>> +ÂÂÂÂÂÂÂ | SVM_SELECTOR_L_MASK | SVM_SELECTOR_G_MASK;
>> +ÂÂÂ uint64_t efer;
>> +
>> +ÂÂÂ efer = rdmsr(MSR_EFER);
>> +ÂÂÂ wrmsr(MSR_EFER, efer | EFER_SVME);
>> +ÂÂÂ wrmsr(MSR_VM_HSAVE_PA, svm->save_area_gpa);
>> +
>> +ÂÂÂ memset(vmcb, 0, sizeof(*vmcb));
>> +ÂÂÂ asm volatile ("vmsave\n\t" : : "a" (vmcb_gpa) : "memory");
>> +ÂÂÂ vmcb_set_seg(&save->es, get_es(), 0, -1U, data_seg_attr);
>> +ÂÂÂ vmcb_set_seg(&save->cs, get_cs(), 0, -1U, code_seg_attr);
>> +ÂÂÂ vmcb_set_seg(&save->ss, get_ss(), 0, -1U, data_seg_attr);
>> +ÂÂÂ vmcb_set_seg(&save->ds, get_ds(), 0, -1U, data_seg_attr);
>> +ÂÂÂ vmcb_set_seg(&save->gdtr, 0, get_gdt().address, get_gdt().size, 0);
>> +ÂÂÂ vmcb_set_seg(&save->idtr, 0, get_idt().address, get_idt().size, 0);
>> +
>> +ÂÂÂ ctrl->asid = 1;
>> +ÂÂÂ save->cpl = 0;
>> +ÂÂÂ save->efer = rdmsr(MSR_EFER);
>> +ÂÂÂ asm volatile ("mov %%cr4, %0" : "=r"(save->cr4) : : "memory");
>> +ÂÂÂ asm volatile ("mov %%cr3, %0" : "=r"(save->cr3) : : "memory");
>> +ÂÂÂ asm volatile ("mov %%cr0, %0" : "=r"(save->cr0) : : "memory");
>> +ÂÂÂ asm volatile ("mov %%dr7, %0" : "=r"(save->dr7) : : "memory");
>> +ÂÂÂ asm volatile ("mov %%dr6, %0" : "=r"(save->dr6) : : "memory");
>> +ÂÂÂ asm volatile ("mov %%cr2, %0" : "=r"(save->cr2) : : "memory");
>> +ÂÂÂ save->g_pat = rdmsr(MSR_IA32_CR_PAT);
>> +ÂÂÂ save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
>> +ÂÂÂ ctrl->intercept = (1ULL << INTERCEPT_VMRUN) |
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (1ULL << INTERCEPT_VMMCALL);
>> +
>> +ÂÂÂ vmcb->save.rip = (u64)guest_rip;
>> +ÂÂÂ vmcb->save.rsp = (u64)guest_rsp;
>> +ÂÂÂ guest_regs.rdi = (u64)svm;
>> +}
>> +
>> +/*
>> + * save/restore 64-bit general registers except rax, rip, rsp
>> + * which are directly handed through the VMCB guest processor state
>> + */
>> +#define SAVE_GPR_CÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
>> +ÂÂÂ "xchg %%rbx, guest_regs+0x20\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%rcx, guest_regs+0x10\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%rdx, guest_regs+0x18\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%rbp, guest_regs+0x30\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%rsi, guest_regs+0x38\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%rdi, guest_regs+0x40\n\t"ÂÂÂ \
>> + "xchg %%r8, guest_regs+0x48\n\t" \
>> + "xchg %%r9, guest_regs+0x50\n\t" \
>> +ÂÂÂ "xchg %%r10, guest_regs+0x58\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%r11, guest_regs+0x60\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%r12, guest_regs+0x68\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%r13, guest_regs+0x70\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%r14, guest_regs+0x78\n\t"ÂÂÂ \
>> +ÂÂÂ "xchg %%r15, guest_regs+0x80\n\t"
>> +
>> +#define LOAD_GPR_CÂÂÂÂÂ SAVE_GPR_C
>> +
>> +/*
>> + * selftests do not use interrupts so we dropped clgi/sti/cli/stgi
>> + * for now. registers involved in LOAD/SAVE_GPR_C are eventually
>> + * unmodified so they do not need to be in the clobber list.
>> + */
>> +void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa)
>> +{
>> +ÂÂÂ asm volatile (
>> +ÂÂÂÂÂÂÂ "vmload\n\t"
> Don't we need to set %rax before calling vmload ?
>
> ÂÂÂÂÂÂÂÂÂÂÂ "mov %[vmcb_gpa], %%rax \n\t"
> ÂÂÂÂÂÂÂÂÂÂÂ "vmload %%rax\n\t"
>
>> +ÂÂÂÂÂÂÂ "mov rflags, %%r15\n\t"ÂÂÂ // rflags
>> +ÂÂÂÂÂÂÂ "mov %%r15, 0x170(%[vmcb])\n\t"
>> +ÂÂÂÂÂÂÂ "mov guest_regs, %%r15\n\t"ÂÂÂ // rax
>> +ÂÂÂÂÂÂÂ "mov %%r15, 0x1f8(%[vmcb])\n\t"
>> +ÂÂÂÂÂÂÂ LOAD_GPR_C
>> +ÂÂÂÂÂÂÂ "vmrun\n\t"
>> +ÂÂÂÂÂÂÂ SAVE_GPR_C
>> +ÂÂÂÂÂÂÂ "mov 0x170(%[vmcb]), %%r15\n\t"ÂÂÂ // rflags
>> +ÂÂÂÂÂÂÂ "mov %%r15, rflags\n\t"
>> +ÂÂÂÂÂÂÂ "mov 0x1f8(%[vmcb]), %%r15\n\t"ÂÂÂ // rax
>> +ÂÂÂÂÂÂÂ "mov %%r15, guest_regs\n\t"
>> +ÂÂÂÂÂÂÂ "vmsave\n\t"
>> +ÂÂÂÂÂÂÂ : : [vmcb] "r" (vmcb), [vmcb_gpa] "a" (vmcb_gpa)
as explained by Vitaly "a" (vmcb_gpa) does the job
>> +ÂÂÂÂÂÂÂ : "r15", "memory");
>> +}
>> +
>> +void nested_svm_check_supported(void)
>> +{
>> +ÂÂÂ struct kvm_cpuid_entry2 *entry =
>> +ÂÂÂÂÂÂÂ kvm_get_supported_cpuid_entry(0x80000001);
>> +
>> +ÂÂÂ if (!(entry->ecx & CPUID_SVM)) {
>> +ÂÂÂÂÂÂÂ fprintf(stderr, "nested SVM not enabled, skipping test\n");
> I think a better message would be:
>
> ÂÂÂ "nested SVM not supported on this CPU, skipping test\n"
>
> Also, the function should ideally return a boolean and let the callers
> print whatever they want.
This is inspired from nested_vmx_check_supported(). I think this can be
done later on and if we do we can change both messages/proto at the same
time.

Thank you for the review!

Best Regards

Eric
>
>> +ÂÂÂÂÂÂÂ exit(KSFT_SKIP);
>> +ÂÂÂ }
>> +}
>> +
>