Basic KVM test for Intel VMX
cover Basic KVM test for Intel VMX
Since we already have some functional and CVE tests for AMD SVM, we should also add tests for Intel VMX. This patchset adds the necessary support code for creating nested VMs on Intel VMX and a simple functional test for VMREAD/VMWRITE instructions similar to kvm_svm04.
The changes include refactoring of existing tests so let's merge it after the upcoming LTP release.
Martin Doucha (10):
kvm_read_sregs(): Read the TR segment register kvm_svm_vmrun(): Simplify VM state save/load with macros
kvm_x86: Define CR0 flags and additional CPUID/MSR constants
KVM: Implement helper functions for setting x86 control registers
KVM: Add memcmp() helper function
KVM: Add helper functions for nested Intel VMX virtualization
lib: Add helper function for reloading kernel modules
lib: Add helper function for reading boolean sysconf files
kvm_pagefault01: Use library functions to reload KVM modules
KVM: Add functional test for emulated VMREAD/VMWRITE instructions
include/tst_module.h | 3 + include/tst_sys_conf.h | 2 + lib/tst_module.c | 28 ++ lib/tst_sys_conf.c | 35 ++ testcases/kernel/kvm/bootstrap_x86.S | 123 ++++- testcases/kernel/kvm/bootstrap_x86_64.S | 153 ++++-- testcases/kernel/kvm/include/kvm_guest.h | 2 + testcases/kernel/kvm/include/kvm_x86.h | 19 +- testcases/kernel/kvm/include/kvm_x86_vmx.h | 201 ++++++++ testcases/kernel/kvm/kvm_pagefault01.c | 59 +-- testcases/kernel/kvm/kvm_vmx01.c | 282 +++++++++++ testcases/kernel/kvm/lib_guest.c | 12 + testcases/kernel/kvm/lib_x86.c | 515 +++++++++++++++++++++ 13 files changed, 1322 insertions(+), 112 deletions(-) create mode 100644 testcases/kernel/kvm/include/kvm_x86_vmx.h create mode 100644 testcases/kernel/kvm/kvm_vmx01.c
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/10 kvm_read_sregs(): Read the TR segment register
--- testcases/kernel/kvm/bootstrap_x86.S | 2 ++ testcases/kernel/kvm/bootstrap_x86_64.S | 2 ++ testcases/kernel/kvm/include/kvm_x86.h | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-)
testcases/kernel/kvm/bootstrap_x86.S +2 -0
@@ -215,6 +215,8 @@ kvm_read_sregs: movw %ax, 8(%edi) mov %ss, %ax movw %ax, 10(%edi) + str %ax + movw %ax, 12(%edi) pop %edi ret
testcases/kernel/kvm/bootstrap_x86_64.S +2 -0
@@ -319,6 +319,8 @@ kvm_read_sregs: movw %ax, 8(%rdi) mov %ss, %ax movw %ax, 10(%rdi) + str %ax + movw %ax, 12(%rdi) retq handle_interrupt:
testcases/kernel/kvm/include/kvm_x86.h +1 -1
@@ -178,7 +178,7 @@ struct kvm_cregs { };struct kvm_sregs { -uint16_t cs, ds, es, fs, gs, ss; +uint16_t cs, ds, es, fs, gs, ss, tr; };struct kvm_regs64 {
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/10 kvm_svm_vmrun(): Simplify VM state save/load with macros
In reply to Martin Doucha--- testcases/kernel/kvm/bootstrap_x86.S | 57 +++++++++----- testcases/kernel/kvm/bootstrap_x86_64.S | 99 +++++++++++++++---------- 2 files changed, 98 insertions(+), 58 deletions(-)
testcases/kernel/kvm/bootstrap_x86.S +38 -19
@@ -361,6 +361,34 @@ kvm_svm_guest_entry: 1: hlt jmp 1b +/* vcpu structure address must be in %rdi */ +.macro load_vcpu_regs + movl 0x04(%edi), %eax + movl 0x0c(%edi), %ebx + movl 0x14(%edi), %ecx + movl 0x1c(%edi), %edx + /* save %edi last */ + movl 0x2c(%edi), %esi + movl 0x34(%edi), %ebp + /* skip %esp */ + movl 0x24(%edi), %edi +.endm + +/* vcpu structure address must be on top of the stack */ +.macro save_vcpu_regs + push %edi + movl 4(%esp), %edi + movl %eax, 0x04(%edi) + movl %ebx, 0x0c(%edi) + movl %ecx, 0x14(%edi) + movl %edx, 0x1c(%edi) + pop %eax + movl %eax, 0x24(%edi) + movl %esi, 0x2c(%edi) + movl %ebp, 0x34(%edi) + /* skip %esp */ +.endm + .global kvm_svm_vmrun kvm_svm_vmrun: push %edi
@@ -377,17 +405,11 @@ kvm_svm_vmrun: vmsave push %eax - /* Load guest registers */ push %edi - movl (%edi), %eax - /* %eax is loaded by vmrun from VMCB */ - movl 0x0c(%edi), %ebx - movl 0x14(%edi), %ecx - movl 0x1c(%edi), %edx - movl 0x2c(%edi), %esi - movl 0x34(%edi), %ebp - /* %esp is loaded by vmrun from VMCB */ - movl 0x24(%edi), %edi + load_vcpu_regs + /* %eax = vcpu->vmcb; */ + movl (%esp), %eax + movl (%eax), %eax vmload vmrun
@@ -395,8 +417,9 @@ kvm_svm_vmrun: /* Clear guest register buffer */ push %edi + push %eax push %ecx - movl 8(%esp), %edi + movl 12(%esp), %edi addl $4, %edi xorl %eax, %eax mov $32, %ecx
@@ -404,17 +427,13 @@ kvm_svm_vmrun: cld rep stosl popfl - - /* Save guest registers */ pop %ecx pop %eax pop %edi - movl %ebx, 0x0c(%edi) - movl %ecx, 0x14(%edi) - movl %edx, 0x1c(%edi) - movl %eax, 0x24(%edi) - movl %esi, 0x2c(%edi) - movl %ebp, 0x34(%edi) + + save_vcpu_regs + pop %edi + /* Copy %eax and %esp from VMCB */ movl (%edi), %esi movl 0x5f8(%esi), %eax
testcases/kernel/kvm/bootstrap_x86_64.S +60 -39
135 lines not on this page · raw message
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/10 kvm_x86: Define CR0 flags and additional CPUID/MSR constants
In reply to Martin Doucha--- testcases/kernel/kvm/include/kvm_x86.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
testcases/kernel/kvm/include/kvm_x86.h +14 -0
@@ -62,12 +62,14 @@/* CPUID constants */ +#define CPUID_GET_MODEL_INFO 0x1#define CPUID_GET_INPUT_RANGE 0x80000000#define CPUID_GET_EXT_FEATURES 0x80000001#define CPUID_GET_SVM_FEATURES 0x8000000a/* Model-specific CPU register constants */ +#define MSR_IA32_FEATURE_CONTROL 0x3a#define MSR_SYSENTER_CS 0x174#define MSR_SYSENTER_ESP 0x175#define MSR_SYSENTER_EIP 0x176
@@ -95,6 +97,18 @@#define VM_CR_SVMDIS (1 << 4)/* Control register constants */ +#define CR0_PE (1 << 0) +#define CR0_MP (1 << 1) +#define CR0_EM (1 << 2) +#define CR0_TS (1 << 3) +#define CR0_ET (1 << 4) +#define CR0_NE (1 << 5) +#define CR0_WP (1 << 16) +#define CR0_AM (1 << 18) +#define CR0_NW (1 << 29) +#define CR0_CD (1 << 30) +#define CR0_PG (1 << 31) +#define CR4_VME (1 << 0)#define CR4_PVI (1 << 1)#define CR4_TSD (1 << 2)
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 4/10 KVM: Implement helper functions for setting x86 control registers
In reply to Martin Doucha--- testcases/kernel/kvm/include/kvm_x86.h | 3 +++ testcases/kernel/kvm/lib_x86.c | 27 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
testcases/kernel/kvm/include/kvm_x86.h +3 -0
@@ -221,6 +221,9 @@ unsigned int kvm_create_stack_descriptor(struct segment_descriptor *table,void kvm_get_cpuid (unsigned int eax,unsigned int ecx,struct kvm_cpuid *buf);void kvm_read_cregs (struct kvm_cregs *buf);void kvm_read_sregs (struct kvm_sregs *buf); +void kvm_set_cr0 (unsigned long val); +void kvm_set_cr3 (unsigned long val); +void kvm_set_cr4 (unsigned long val);uint64_t kvm_rdmsr (unsigned int msr);void kvm_wrmsr (unsigned int msr,uint64_t value);
testcases/kernel/kvm/lib_x86.c +27 -0
@@ -214,6 +214,33 @@ void kvm_get_cpuid(unsigned int eax, unsigned int ecx, struct kvm_cpuid *buf) ); } +void kvm_set_cr0 (unsigned long val) +{ + asm ( +"mov %0, %%cr0\n" + : + :"r" (val) + ); +} + +void kvm_set_cr3 (unsigned long val) +{ + asm ( +"mov %0, %%cr3\n" + : + :"r" (val) + ); +} + +void kvm_set_cr4 (unsigned long val) +{ + asm ( +"mov %0, %%cr4\n" + : + :"r" (val) + ); +} +uint64_t kvm_rdmsr (unsigned int msr) {unsigned int ret_lo, ret_hi;
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 5/10 KVM: Add memcmp() helper function
In reply to Martin Doucha--- testcases/kernel/kvm/include/kvm_guest.h | 2 ++ testcases/kernel/kvm/lib_guest.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)
testcases/kernel/kvm/include/kvm_guest.h +2 -0
@@ -48,6 +48,8 @@ void *memset(void *dest, int val, size_t size);void *memzero (void *dest,size_t size);void *memcpy (void *dest,const void *src,size_t size); +int memcmp (const void *a,const void *b,size_t length); +char *strcpy (char *dest,const char *src);char *strcat (char *dest,const char *src);size_t strlen (const char *str);
testcases/kernel/kvm/lib_guest.c +12 -0
@@ -45,6 +45,18 @@ void *memcpy(void *dest, const void *src, size_t size)return dest; } +int memcmp (const void *a,const void *b,size_t length) +{ +const unsigned char *x = a, *y = b; + +for (; length; x++, y++, length--) { +if (*x != *y) +return (int )*x - (int )*y; + } + +return 0 ; +} +char *strcpy (char *dest,const char *src) {char *ret = dest;
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 6/10 KVM: Add helper functions for nested Intel VMX virtualization
In reply to Martin Doucha--- testcases/kernel/kvm/bootstrap_x86.S | 64 +++ testcases/kernel/kvm/bootstrap_x86_64.S | 54 +++ testcases/kernel/kvm/include/kvm_x86_vmx.h | 201 +++++++++ testcases/kernel/kvm/lib_x86.c | 488 +++++++++++++++++++++ 4 files changed, 807 insertions(+) create mode 100644 testcases/kernel/kvm/include/kvm_x86_vmx.h
testcases/kernel/kvm/bootstrap_x86.S +64 -0
@@ -11,6 +11,9 @@ .set MSR_VM_HSAVE_PA, 0xc0010117 +.set VMX_VMCS_HOST_RSP, 0x6c14 +.set VMX_VMCS_HOST_RIP, 0x6c16 + /* * This section will be allocated at address 0x1000 and * jumped to from the reset stub provided by kvm_run.
@@ -451,6 +454,67 @@ kvm_svm_vmrun: pop %edi ret +.global kvm_vmx_vmlaunch +kvm_vmx_vmlaunch: + push %edi + mov 8(%esp), %edi + push %ebx + push %esi + push %ebp + push %edi + + mov $VMX_VMCS_HOST_RSP, %eax + vmwrite %esp, %eax + jna vmx_vmwrite_error + mov $VMX_VMCS_HOST_RIP, %eax + lea vmx_vm_exit, %ebx + vmwrite %ebx, %eax + jna vmx_vmwrite_error + + load_vcpu_regs + vmlaunch + jmp vmx_vm_exit + +.global kvm_vmx_vmresume +kvm_vmx_vmresume: + push %edi + mov 8(%esp), %edi + push %ebx + push %esi + push %ebp + push %edi + + mov $VMX_VMCS_HOST_RSP, %eax + vmwrite %esp, %eax + jna vmx_vmwrite_error + mov $VMX_VMCS_HOST_RIP, %eax + lea vmx_vm_exit, %ebx + vmwrite %ebx, %eax + jna vmx_vmwrite_error + + load_vcpu_regs + vmresume + +vmx_vm_exit: + jna vmx_vmentry_error + save_vcpu_regs + xorl %eax, %eax + +vmx_vm_ret: + pop %edi + pop %ebp + pop %esi + pop %ebx + pop %edi + ret + +vmx_vmwrite_error: + movl $2, %eax + jmp vmx_vm_ret + +vmx_vmentry_error: + movl $1, %eax + jmp vmx_vm_ret .section .bss.pgtables, "aw", @nobits .global kvm_pagetable
testcases/kernel/kvm/bootstrap_x86_64.S +54 -0
66 lines not on this page · raw message
testcases/kernel/kvm/include/kvm_x86_vmx.h new file +201 -0
201 lines not on this page · raw message
testcases/kernel/kvm/lib_x86.c +488 -0
503 lines not on this page · raw message
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 7/10 lib: Add helper function for reloading kernel modules
In reply to Martin Doucha--- include/tst_module.h | 3 +++ lib/tst_module.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/tst_module.h +3 -0
@@ -47,4 +47,7 @@ static inline void tst_requires_module_signature_disabled(void) tst_requires_module_signature_disabled_(); } +void tst_modprobe (const char *mod_name,char *const argv[]); +void tst_module_reload (const char *mod_name,char *const argv[]); +#endif /* TST_MODULE_H */
lib/tst_module.c +28 -0
@@ -146,3 +146,31 @@ void tst_requires_module_signature_disabled_(void)if (tst_module_signature_enforced_ ())tst_brkm (TCONF ,NULL ,"module signature is enforced, skip test" ); } + +void tst_modprobe (const char *mod_name,char *const argv[]) +{ +const int offset =2 ;/* command name & module path */ +int i, size =0 ; + +while (argv && argv[size]) + ++size; + size += offset; + +const char *mod_argv[size +1 ];/* + NULL in the end */ + + mod_argv[size] =NULL ; + mod_argv[0 ] ="modprobe" ; + mod_argv[1 ] = mod_name; + +for (i = offset; i < size; ++i) + mod_argv[i] = argv[i - offset]; + +tst_cmd (NULL , mod_argv,NULL ,NULL ,0 ); +} + +void tst_module_reload (const char *mod_name,char *const argv[]) +{ +tst_resm (TINFO ,"Reloading kernel module %s" , mod_name); +tst_module_unload_ (NULL , mod_name); +tst_modprobe (mod_name, argv); +}
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 8/10 lib: Add helper function for reading boolean sysconf files
In reply to Martin Doucha--- include/tst_sys_conf.h | 2 ++ lib/tst_sys_conf.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
include/tst_sys_conf.h +2 -0
@@ -28,4 +28,6 @@ int tst_sys_conf_save(const struct tst_path_val *conf);void tst_sys_conf_restore (int verbose);void tst_sys_conf_dump (void ); +int tst_read_bool_sys_param (const char *filename); +#endif
lib/tst_sys_conf.c +35 -0
@@ -7,6 +7,7 @@#include <stdio.h> #include <unistd.h> #include <string.h> +#include <ctype.h> #define TST_NO_DEFAULT_MAIN #include "tst_test.h"
@@ -145,3 +146,37 @@ void tst_sys_conf_restore(int verbose) } } +int tst_read_bool_sys_param (const char *filename) +{ +char buf[PATH_MAX ]; +int i, fd, ret; + + fd =open (filename,O_RDONLY ); + +if (fd <0 ) +return -1 ; + + ret =read (fd, buf,PATH_MAX -1 ); +SAFE_CLOSE (fd); + +if (ret <1 ) +return -1 ; + + buf[ret] ='\0' ; + +for (i =0 ; buf[i] && !isspace (buf[i]); i++) + ; + + buf[i] ='\0' ; + +if (isdigit (buf[0 ])) { +tst_parse_int (buf, &ret,INT_MIN ,INT_MAX ); +return ret; + } + +if (!strcasecmp (buf,"N" )) +return 0 ; + +/* Assume that any other value than 0 or N means the param is enabled */ +return 1 ; +}
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 9/10 kvm_pagefault01: Use library functions to reload KVM modules
In reply to Martin Doucha--- testcases/kernel/kvm/kvm_pagefault01.c | 59 +++----------------------- 1 file changed, 5 insertions(+), 54 deletions(-)
testcases/kernel/kvm/kvm_pagefault01.c +5 -54
@@ -136,70 +136,21 @@ TST_TEST_TCONF("Test supported only on x86_64");#else /* COMPILE_PAYLOAD */ -#include <ctype.h> -#include <stdio.h> -#include <unistd.h> #include "tst_module.h" #define TDP_MMU_SYSFILE "/sys/module/kvm/parameters/tdp_mmu"#define TDP_AMD_SYSFILE "/sys/module/kvm_amd/parameters/npt"#define TDP_INTEL_SYSFILE "/sys/module/kvm_intel/parameters/ept" -#define BUF_SIZE 64 - -static int read_bool_sys_param (const char *filename) -{ -char buf[BUF_SIZE ]; -int i, fd, ret; - - fd =open (filename,O_RDONLY ); - -if (fd <0 ) -return -1 ; - - ret =read (fd, buf,BUF_SIZE -1 ); -SAFE_CLOSE (fd); - -if (ret <1 ) -return -1 ; - - buf[ret] ='\0' ; - -for (i =0 ; buf[i] && !isspace (buf[i]); i++) - ; - - buf[i] ='\0' ; - -if (isdigit (buf[0 ])) { -tst_parse_int (buf, &ret,INT_MIN ,INT_MAX ); -return ret; - } - -if (!strcasecmp (buf,"N" )) -return 0 ; - -/* Assume that any other value than 0 or N means the param is enabled */ -return 1 ; -} - -static void reload_module (const char *module,char *arg) -{ -const char *const argv[] = {"modprobe" , module, arg,NULL }; - -tst_res (TINFO ,"Reloading module %s with parameter %s" , module, arg); -tst_module_unload (module); -tst_cmd (argv,NULL ,NULL ,0 ); -} -static void disable_tdp (void ) { -if (read_bool_sys_param (TDP_AMD_SYSFILE ) >0 ) -reload_module ("kvm_amd" ,"npt=0" ); +if (tst_read_bool_sys_param (TDP_AMD_SYSFILE ) >0 ) +tst_module_reload ("kvm_amd" , (char *const []){"npt=0" ,NULL }); -if (read_bool_sys_param (TDP_INTEL_SYSFILE ) >0 ) -reload_module ("kvm_intel" ,"ept=0" ); +if (tst_read_bool_sys_param (TDP_INTEL_SYSFILE ) >0 ) +tst_module_reload ("kvm_intel" , (char *const []){"ept=0" ,NULL }); -if (read_bool_sys_param (TDP_MMU_SYSFILE ) >0 ) +if (tst_read_bool_sys_param (TDP_MMU_SYSFILE ) >0 )tst_res (TINFO ,"WARNING: tdp_mmu is enabled, beware of false negatives" ); }
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 10/10 KVM: Add functional test for emulated VMREAD/VMWRITE instructions
In reply to Martin Doucha--- testcases/kernel/kvm/kvm_vmx01.c | 282 +++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 testcases/kernel/kvm/kvm_vmx01.c
testcases/kernel/kvm/kvm_vmx01.c new file +282 -0
@@ -0,0 +1,282 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024 SUSE LLC <[email protected]> +*/ + +/*\ +* Basic functional test for VMREAD/VMWRITE instructions in KVM environment. +* Verify that VMWRITE instruction changes the contents of current VMCS and +* the values written into shadow VMCS can be read in both parent and nested +* VM. +*/ + +#include "kvm_test.h" + +#ifdef COMPILE_PAYLOAD +#if defined(__i386__) || defined(__x86_64__) + +#include "kvm_x86_vmx.h" + +#define GUEST_READ_ERROR 1 +#define GUEST_WRITE_ERROR 2 +#define SHADOW_DATA_LENGTH 37 +#define VMCS_FIELD (x) x, #x + +struct vmcs_field_table { +unsigned long field_id; +const char *name; +uint64_t value; +}; + +/* Data written into shadow VMCS by the parent VM and read by the nested VM */ +static struct vmcs_field_table host_data[SHADOW_DATA_LENGTH ] = { + {VMCS_FIELD (VMX_VMCS_GUEST_ES ),0xe5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS ),0xc5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS ),0x55 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS ),0xd5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS ),0xf5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS ),0x65 }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR ),0x1d72 }, + {VMCS_FIELD (VMX_VMCS_GUEST_TR ),0x72 }, + {VMCS_FIELD (VMX_VMCS_HOST_ES ),0x5e }, + {VMCS_FIELD (VMX_VMCS_HOST_CS ),0x5c }, + {VMCS_FIELD (VMX_VMCS_HOST_SS ),0x55 }, + {VMCS_FIELD (VMX_VMCS_HOST_DS ),0x5d }, + {VMCS_FIELD (VMX_VMCS_HOST_FS ),0x5f }, + {VMCS_FIELD (VMX_VMCS_HOST_GS ),0x56 }, + {VMCS_FIELD (VMX_VMCS_HOST_TR ),0x27 }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_LIMIT ),0xe51 }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_LIMIT ),0xc51 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_LIMIT ),0x551 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_LIMIT ),0xd51 }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_LIMIT ),0xf51 }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_LIMIT ),0x651 }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR_LIMIT ),0x1d721 }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_ACCESS ),0xa0e5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_ACCESS ),0xa0c5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_ACCESS ),0xa055 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_ACCESS ),0xa0d5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_ACCESS ),0xa0f5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_ACCESS ),0xa065 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SYSENTER_CS ),0x65c }, + {VMCS_FIELD (VMX_VMCS_HOST_SYSENTER_CS ),0x45c }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_BASE ),0xe5b }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_BASE ),0xc5b }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_BASE ),0x55b }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_BASE ),0xd5b }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_BASE ),0xf5b }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_BASE ),0x65b }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR_BASE ),0x1d72b } +}; + +/* Data written into shadow VMCS by the nested VM and read by the parent VM */ +static struct vmcs_field_table guest_data[SHADOW_DATA_LENGTH ] = { + {VMCS_FIELD (VMX_VMCS_GUEST_ES ),0x5e }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS ),0x5c }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS ),0x55 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS ),0x5d }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS ),0x5f }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS ),0x56 }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR ),0x721d }, + {VMCS_FIELD (VMX_VMCS_GUEST_TR ),0x27 }, + {VMCS_FIELD (VMX_VMCS_HOST_ES ),0xe5 }, + {VMCS_FIELD (VMX_VMCS_HOST_CS ),0xc5 }, + {VMCS_FIELD (VMX_VMCS_HOST_SS ),0x55 }, + {VMCS_FIELD (VMX_VMCS_HOST_DS ),0xd5 }, + {VMCS_FIELD (VMX_VMCS_HOST_FS ),0xf5 }, + {VMCS_FIELD (VMX_VMCS_HOST_GS ),0x65 }, + {VMCS_FIELD (VMX_VMCS_HOST_TR ),0x72 }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_LIMIT ),0x1e5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_LIMIT ),0x1c5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_LIMIT ),0x155 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_LIMIT ),0x1d5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_LIMIT ),0x1f5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_LIMIT ),0x165 }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR_LIMIT ),0x11d72 }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_ACCESS ),0xa05e }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_ACCESS ),0xa05c }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_ACCESS ),0xa055 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_ACCESS ),0xa05d }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_ACCESS ),0xa05f }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_ACCESS ),0xa056 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SYSENTER_CS ),0x5c6 }, + {VMCS_FIELD (VMX_VMCS_HOST_SYSENTER_CS ),0x5c4 }, + {VMCS_FIELD (VMX_VMCS_GUEST_ES_BASE ),0xbe5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_CS_BASE ),0xbc5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_SS_BASE ),0xb55 }, + {VMCS_FIELD (VMX_VMCS_GUEST_DS_BASE ),0xbd5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_FS_BASE ),0xbf5 }, + {VMCS_FIELD (VMX_VMCS_GUEST_GS_BASE ),0xb65 }, + {VMCS_FIELD (VMX_VMCS_GUEST_LDTR_BASE ),0xb1d72 } +}; + +static uint64_t vmread_buffer[SHADOW_DATA_LENGTH ]; + +int guest_main (void ) +{ +int i; + +/* kvm_vmx_vmread() calls tst_brk(), don't use it in nested VM */ +for (i =0 ; i <SHADOW_DATA_LENGTH ; i++) { + asm goto( +"vmread %1, (%0)\n" +"jna %l[read_error]\n" +"vmwrite %2, %3\n" +"jna %l[write_error]\n" + : + :"r" (&vmread_buffer[i]),"r" (host_data[i].field_id), +"r" (guest_data[i].value), +"r" (guest_data[i].field_id) + :"cc" ,"memory" + : read_error, write_error + ); + } + +return 0 ; + +read_error: +return GUEST_READ_ERROR ; + +write_error: +return GUEST_WRITE_ERROR ; +} + +void main (void ) +{ +struct kvm_vmx_vcpu *vcpu; +struct kvm_vmcs *shadow_vmcs; +char *vmcs_backup; +int i, errors; +uint64_t val; + +kvm_set_vmx_state (1 ); + +/* Check secondary VMCS execctl support */ +if (kvm_rdmsr (MSR_IA32_VMX_BASIC ) &IA32_VMXBASIC_USELESS_CTL_MASKS ) + val =kvm_rdmsr (MSR_IA32_VMX_EXECCTL_MASK2 ); +else + val =kvm_rdmsr (MSR_IA32_VMX_EXECCTL_MASK ); + +if (!((val >>32 ) &VMX_EXECCTL_ENABLE_CTL2 )) +tst_brk (TCONF ,"CPU does not support shadow VMCS" ); + +/* Create and configure guest VMCS */ + shadow_vmcs =kvm_alloc_vmcs (); +kvm_vmx_vmclear (shadow_vmcs); + shadow_vmcs->version |=VMX_SHADOW_VMCS ; + vcpu =kvm_create_vmx_vcpu (guest_main,1 ); +kvm_vmx_vmptrld (vcpu->vmcs); + val =kvm_vmx_vmread (VMX_VMCS_VMEXEC_CTL ); + val |=VMX_EXECCTL_ENABLE_CTL2 ; +kvm_vmx_vmwrite (VMX_VMCS_VMEXEC_CTL , val); + val =kvm_rdmsr (MSR_IA32_VMX_EXECCTL2_MASK ); + +if (!((val >>32 ) &VMX_EXECCTL2_SHADOW_VMCS )) +tst_brk (TCONF ,"CPU does not support shadow VMCS" ); + + val =VMX_EXECCTL2_SHADOW_VMCS | (uint32_t )val; +kvm_vmx_vmwrite (VMX_VMCS_VMEXEC_CTL2 , val); +kvm_vmx_vmwrite (VMX_VMCS_LINK_POINTER , (uintptr_t )shadow_vmcs); + +/* Configure shadow VMCS */ + vmcs_backup =tst_heap_alloc (sizeof (struct kvm_vmcs )); +memcpy (vmcs_backup, shadow_vmcs,sizeof (struct kvm_vmcs )); +kvm_vmx_vmptrld (shadow_vmcs); + +for (i =0 ; i <SHADOW_DATA_LENGTH ; i++) +kvm_vmx_vmwrite (host_data[i].field_id, host_data[i].value); + +/* Flush shadow VMCS just in case */ +kvm_vmx_vmptrld (vcpu->vmcs); + +if (!memcmp (vmcs_backup, shadow_vmcs,sizeof (struct kvm_vmcs ))) +tst_res (TFAIL ,"VMWRITE did not modify raw VMCS data" ); + +/* Run nested VM */ +memcpy (vmcs_backup, shadow_vmcs,sizeof (struct kvm_vmcs )); +kvm_vmx_vmrun (vcpu); + val =kvm_vmx_vmread (VMX_VMCS_EXIT_REASON ); + +if (val !=VMX_EXIT_HLT ) { +tst_res (TFAIL ,"Unexpected guest exit reason %llx" , val); +return ; + } + +if (vcpu->regs.rax ==GUEST_READ_ERROR ) { +tst_res (TFAIL ,"Guest failed to read shadow VMCS" ); +return ; + } + +if (vcpu->regs.rax ==GUEST_WRITE_ERROR ) { +tst_res (TFAIL ,"Guest failed to write shadow VMCS" ); +return ; + } + +if (!memcmp (vmcs_backup, shadow_vmcs,sizeof (struct kvm_vmcs ))) +tst_res (TFAIL ,"Nested VMWRITE did not modify raw VMCS data" ); + +/* Check values read by the nested VM from shadow VMCS */ +for (i =0 , errors =0 ; i <SHADOW_DATA_LENGTH ; i++) { +if (vmread_buffer[i] == host_data[i].value) +continue ; + + errors++; +tst_res (TFAIL ,"Shadow %s guest mismatch: %llx != %llx" , + host_data[i].name, vmread_buffer[i], + host_data[i].value); + } + +if (!errors) +tst_res (TPASS ,"Guest read correct values from shadow VMCS" ); + +/* Check values written by the nested VM to shadow VMCS */ +kvm_vmx_vmptrld (shadow_vmcs); + +for (i =0 , errors =0 ; i <SHADOW_DATA_LENGTH ; i++) { + val =kvm_vmx_vmread (guest_data[i].field_id); + +if (val == guest_data[i].value) +continue ; + + errors++; +tst_res (TFAIL ,"Shadow %s parent mismatch: %llx != %llx" , + guest_data[i].name, val, guest_data[i].value); + } + +if (!errors) +tst_res (TPASS ,"Parent read correct values from shadow VMCS" ); +} + +#else /* defined(__i386__) || defined(__x86_64__) */ +TST_TEST_TCONF ("Test supported only on x86" ); +#endif /* defined(__i386__) || defined(__x86_64__) */ + +#else /* COMPILE_PAYLOAD */ + +#include "tst_module.h" + +#define NESTED_INTEL_SYSFILE "/sys/module/kvm_intel/parameters/nested" + +static void setup (void ) +{ +if (!tst_read_bool_sys_param (NESTED_INTEL_SYSFILE )) { +tst_module_reload ("kvm_intel" , + (char *const []){"nested=1" ,NULL }); + } + +tst_kvm_setup (); +} + +static struct tst_test test = { + .test_all = tst_kvm_run, + .setup = setup, + .cleanup = tst_kvm_cleanup, + .needs_root =1 , + .supported_archs = (const char *const []) { +"x86_64" , +"x86" , +NULL + }, +}; + +#endif /* COMPILE_PAYLOAD */
Signature · 5 lines
2.47.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 10/10] KVM: Add functional test for emulated VMREAD/VMWRITE instructions
Hi Martin,
whole patchset merged, excellent work. Thanks!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp