1*49cdfc7eSAndroid Build Coastguard WorkerLTP KVM Test API 2*49cdfc7eSAndroid Build Coastguard Worker================ 3*49cdfc7eSAndroid Build Coastguard Worker 4*49cdfc7eSAndroid Build Coastguard WorkerTesting KVM is more complex than other Linux features. Some KVM bugs allow 5*49cdfc7eSAndroid Build Coastguard Workeruserspace code running inside the virtual machine to bypass (emulated) hardware 6*49cdfc7eSAndroid Build Coastguard Workeraccess restrictions and elevate its privileges inside the guest operating 7*49cdfc7eSAndroid Build Coastguard Workersystem. The worst types of KVM bugs may even allow the guest code to crash or 8*49cdfc7eSAndroid Build Coastguard Workercompromise the physical host. KVM tests therefore need to be split into two 9*49cdfc7eSAndroid Build Coastguard Workercomponents – a KVM controller program running on the physical host and a guest 10*49cdfc7eSAndroid Build Coastguard Workerpayload program running inside the VM. The cooperation of these two components 11*49cdfc7eSAndroid Build Coastguard Workerallows testing even of bugs that somehow cross the virtualization boundary. 12*49cdfc7eSAndroid Build Coastguard Worker 13*49cdfc7eSAndroid Build Coastguard WorkerNOTE: See also 14*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines], 15*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/C-Test-Case-Tutorial[C Test Case Tutorial], 16*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API]. 17*49cdfc7eSAndroid Build Coastguard Worker 18*49cdfc7eSAndroid Build Coastguard Worker1. Basic KVM test structure 19*49cdfc7eSAndroid Build Coastguard Worker--------------------------- 20*49cdfc7eSAndroid Build Coastguard Worker 21*49cdfc7eSAndroid Build Coastguard WorkerKVM tests are simple C source files containing both the KVM controller code 22*49cdfc7eSAndroid Build Coastguard Workerand the guest payload code separated by `#ifdef COMPILE_PAYLOAD` preprocessor 23*49cdfc7eSAndroid Build Coastguard Workercondition. The file will be compiled twice. Once to compile the payload part, 24*49cdfc7eSAndroid Build Coastguard Workeronce to compile the KVM controller part and embed the payload binary inside. 25*49cdfc7eSAndroid Build Coastguard WorkerThe result is a single self-contained binary that'll execute the embedded 26*49cdfc7eSAndroid Build Coastguard Workerpayload inside a KVM virtual machine and print results in the same format as 27*49cdfc7eSAndroid Build Coastguard Workera normal LTP test. 28*49cdfc7eSAndroid Build Coastguard Worker 29*49cdfc7eSAndroid Build Coastguard WorkerA KVM test source should start with `#include "kvm_test.h"` instead of the 30*49cdfc7eSAndroid Build Coastguard Workerusual `tst_test.h`. The `kvm_test.h` header file will include the other basic 31*49cdfc7eSAndroid Build Coastguard Workerheaders appropriate for the current compilation pass. Everything else in the 32*49cdfc7eSAndroid Build Coastguard Workersource file should be enclosed in `#ifdef COMPILE_PAYLOAD ... #else ... #endif` 33*49cdfc7eSAndroid Build Coastguard Workercondition, including any other header file includes. Note that the standard 34*49cdfc7eSAndroid Build Coastguard WorkerLTP headers are not available in the payload compilation pass, only the KVM 35*49cdfc7eSAndroid Build Coastguard Workerguest library headers can be included. 36*49cdfc7eSAndroid Build Coastguard Worker 37*49cdfc7eSAndroid Build Coastguard Worker.Example KVM test 38*49cdfc7eSAndroid Build Coastguard Worker[source,c] 39*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 40*49cdfc7eSAndroid Build Coastguard Worker#include "kvm_test.h" 41*49cdfc7eSAndroid Build Coastguard Worker 42*49cdfc7eSAndroid Build Coastguard Worker#ifdef COMPILE_PAYLOAD 43*49cdfc7eSAndroid Build Coastguard Worker 44*49cdfc7eSAndroid Build Coastguard Worker/* Guest payload code */ 45*49cdfc7eSAndroid Build Coastguard Worker 46*49cdfc7eSAndroid Build Coastguard Workervoid main(void) 47*49cdfc7eSAndroid Build Coastguard Worker{ 48*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Hello, world!"); 49*49cdfc7eSAndroid Build Coastguard Worker} 50*49cdfc7eSAndroid Build Coastguard Worker 51*49cdfc7eSAndroid Build Coastguard Worker#else /* COMPILE_PAYLOAD */ 52*49cdfc7eSAndroid Build Coastguard Worker 53*49cdfc7eSAndroid Build Coastguard Worker/* KVM controller code */ 54*49cdfc7eSAndroid Build Coastguard Worker 55*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 56*49cdfc7eSAndroid Build Coastguard Worker .test_all = tst_kvm_run, 57*49cdfc7eSAndroid Build Coastguard Worker .setup = tst_kvm_setup, 58*49cdfc7eSAndroid Build Coastguard Worker .cleanup = tst_kvm_cleanup, 59*49cdfc7eSAndroid Build Coastguard Worker}; 60*49cdfc7eSAndroid Build Coastguard Worker 61*49cdfc7eSAndroid Build Coastguard Worker#endif /* COMPILE_PAYLOAD */ 62*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 63*49cdfc7eSAndroid Build Coastguard Worker 64*49cdfc7eSAndroid Build Coastguard WorkerThe KVM controller code is a normal LTP test and needs to define an instance 65*49cdfc7eSAndroid Build Coastguard Workerof `struct tst_test` with metadata and the usual setup, cleanup, and test 66*49cdfc7eSAndroid Build Coastguard Workerfunctions. Basic implementation of all three functions is provided by the KVM 67*49cdfc7eSAndroid Build Coastguard Workerhost library. 68*49cdfc7eSAndroid Build Coastguard Worker 69*49cdfc7eSAndroid Build Coastguard WorkerOn the other hand, the payload is essentially a tiny kernel that'll run 70*49cdfc7eSAndroid Build Coastguard Workeron bare virtual hardware. It cannot access any files, Linux syscalls, standard 71*49cdfc7eSAndroid Build Coastguard Workerlibrary functions, etc. except for the small subset provided by the KVM guest 72*49cdfc7eSAndroid Build Coastguard Workerlibrary. The payload code must define a `void main(void)` function which will 73*49cdfc7eSAndroid Build Coastguard Workerbe the VM entry point of the test. 74*49cdfc7eSAndroid Build Coastguard Worker 75*49cdfc7eSAndroid Build Coastguard Worker2. KVM host library 76*49cdfc7eSAndroid Build Coastguard Worker------------------- 77*49cdfc7eSAndroid Build Coastguard Worker 78*49cdfc7eSAndroid Build Coastguard WorkerThe KVM host library provides helper functions for creating and running 79*49cdfc7eSAndroid Build Coastguard Workera minimal KVM virtual machine. 80*49cdfc7eSAndroid Build Coastguard Worker 81*49cdfc7eSAndroid Build Coastguard Worker2.1 Data structures 82*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~ 83*49cdfc7eSAndroid Build Coastguard Worker 84*49cdfc7eSAndroid Build Coastguard Worker[source,c] 85*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 86*49cdfc7eSAndroid Build Coastguard Workerstruct tst_kvm_instance { 87*49cdfc7eSAndroid Build Coastguard Worker int vm_fd, vcpu_fd; 88*49cdfc7eSAndroid Build Coastguard Worker struct kvm_run *vcpu_info; 89*49cdfc7eSAndroid Build Coastguard Worker size_t vcpu_info_size; 90*49cdfc7eSAndroid Build Coastguard Worker struct kvm_userspace_memory_region ram[MAX_KVM_MEMSLOTS]; 91*49cdfc7eSAndroid Build Coastguard Worker struct tst_kvm_result *result; 92*49cdfc7eSAndroid Build Coastguard Worker}; 93*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 94*49cdfc7eSAndroid Build Coastguard Worker 95*49cdfc7eSAndroid Build Coastguard Worker`struct tst_kvm_instance` holds the file descriptors and memory buffers 96*49cdfc7eSAndroid Build Coastguard Workerof a single KVM virtual machine: 97*49cdfc7eSAndroid Build Coastguard Worker 98*49cdfc7eSAndroid Build Coastguard Worker- `int vm_fd` is the main VM file descriptor created by `ioctl(KVM_CREATE_VM)` 99*49cdfc7eSAndroid Build Coastguard Worker- `int vcpu_fd` is the virtual CPU filedescriptor created by 100*49cdfc7eSAndroid Build Coastguard Worker `ioctl(KVM_CREATE_VCPU)` 101*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_run *vcpu_info` is the VCPU state structure created by 102*49cdfc7eSAndroid Build Coastguard Worker `mmap(vcpu_fd)` 103*49cdfc7eSAndroid Build Coastguard Worker- `size_t vcpu_info_size` is the size of `vcpu_info` buffer 104*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_userspace_memory_region ram[MAX_KVM_MEMSLOTS]` is the list 105*49cdfc7eSAndroid Build Coastguard Worker of memory slots defined in this VM. Unused memory slots have zero 106*49cdfc7eSAndroid Build Coastguard Worker in the `userspace_addr` field. 107*49cdfc7eSAndroid Build Coastguard Worker- `struct tst_kvm_result *result` is a buffer for passing test result data 108*49cdfc7eSAndroid Build Coastguard Worker from the VM to the controller program, mainly `tst_res()`/`tst_brk()` flags 109*49cdfc7eSAndroid Build Coastguard Worker and messages. 110*49cdfc7eSAndroid Build Coastguard Worker 111*49cdfc7eSAndroid Build Coastguard Worker[source,c] 112*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 113*49cdfc7eSAndroid Build Coastguard Workerstruct tst_kvm_result { 114*49cdfc7eSAndroid Build Coastguard Worker int32_t result; 115*49cdfc7eSAndroid Build Coastguard Worker int32_t lineno; 116*49cdfc7eSAndroid Build Coastguard Worker uint64_t file_addr; 117*49cdfc7eSAndroid Build Coastguard Worker char message[0]; 118*49cdfc7eSAndroid Build Coastguard Worker}; 119*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 120*49cdfc7eSAndroid Build Coastguard Worker 121*49cdfc7eSAndroid Build Coastguard Worker`struct tst_kvm_result` is used to pass test results and synchronization data 122*49cdfc7eSAndroid Build Coastguard Workerbetween the KVM guest and the controller program. Most often, it is used 123*49cdfc7eSAndroid Build Coastguard Workerto pass `tst_res()` and `tst_brk()` messages from the VM, but special values 124*49cdfc7eSAndroid Build Coastguard Workercan also be used to send control flow requests both ways. 125*49cdfc7eSAndroid Build Coastguard Worker 126*49cdfc7eSAndroid Build Coastguard Worker- `int32_t result` is the message type, either one of the `TPASS`, `TFAIL`, 127*49cdfc7eSAndroid Build Coastguard Worker `TWARN`, `TBROK`, `TINFO` flags or a special control flow value. Errno flags 128*49cdfc7eSAndroid Build Coastguard Worker are not supported. 129*49cdfc7eSAndroid Build Coastguard Worker- `int32_t lineno` is the line number for `tst_res()`/`tst_brk()` messages. 130*49cdfc7eSAndroid Build Coastguard Worker- `uint64_t file_addr` is the VM address of the filename string for 131*49cdfc7eSAndroid Build Coastguard Worker `tst_res()`/`tst_brk()` messages. 132*49cdfc7eSAndroid Build Coastguard Worker- `char message[0]` is the buffer for arbitrary message data, most often used 133*49cdfc7eSAndroid Build Coastguard Worker to pass `tst_res()`/`tst_brk()` message strings. 134*49cdfc7eSAndroid Build Coastguard Worker 135*49cdfc7eSAndroid Build Coastguard Worker2.2 Working with virtual machines 136*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 137*49cdfc7eSAndroid Build Coastguard Worker 138*49cdfc7eSAndroid Build Coastguard WorkerThe KVM host library provides default implementation of the setup, cleanup 139*49cdfc7eSAndroid Build Coastguard Workerand test functions for `struct tst_test` in cases where you do not need 140*49cdfc7eSAndroid Build Coastguard Workerto customize the VM configuration. You can either assign these functions 141*49cdfc7eSAndroid Build Coastguard Workerto the `struct tst_test` instance directly or call them from your own function 142*49cdfc7eSAndroid Build Coastguard Workerthat does some additional steps. All three function must be used together. 143*49cdfc7eSAndroid Build Coastguard Worker 144*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_setup(void)` 145*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_run(void)` 146*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_cleanup(void)` 147*49cdfc7eSAndroid Build Coastguard Worker 148*49cdfc7eSAndroid Build Coastguard WorkerNote: `tst_kvm_run()` calls `tst_free_all()`. Calling it will free all 149*49cdfc7eSAndroid Build Coastguard Workerpreviously allocated guarded buffers. 150*49cdfc7eSAndroid Build Coastguard Worker 151*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_validate_result(int value)` – Validate whether the value 152*49cdfc7eSAndroid Build Coastguard Worker returned in `struct tst_kvm_result.result` can be safely passed 153*49cdfc7eSAndroid Build Coastguard Worker to `tst_res()` or `tst_brk()`. If the value is not valid, the controller 154*49cdfc7eSAndroid Build Coastguard Worker program will be terminated with error. 155*49cdfc7eSAndroid Build Coastguard Worker 156*49cdfc7eSAndroid Build Coastguard Worker- `uint64_t tst_kvm_get_phys_address(const struct tst_kvm_instance *inst, 157*49cdfc7eSAndroid Build Coastguard Worker uint64_t addr)` – Converts pointer value (virtual address) from KVM virtual 158*49cdfc7eSAndroid Build Coastguard Worker machine `inst` to the corresponding physical address. Returns 0 if 159*49cdfc7eSAndroid Build Coastguard Worker the virtual address is not mapped to any physical address. If virtual memory 160*49cdfc7eSAndroid Build Coastguard Worker mapping is not enabled in the VM or not available on the arch at all, this 161*49cdfc7eSAndroid Build Coastguard Worker function simply returns `addr` as is. 162*49cdfc7eSAndroid Build Coastguard Worker 163*49cdfc7eSAndroid Build Coastguard Worker- `int tst_kvm_find_phys_memslot(const struct tst_kvm_instance *inst, 164*49cdfc7eSAndroid Build Coastguard Worker uint64_t paddr)` – Returns index of the memory slot in KVM virtual machine 165*49cdfc7eSAndroid Build Coastguard Worker `inst` which contains the physical address `paddr`. If the address is not 166*49cdfc7eSAndroid Build Coastguard Worker backed by a memory buffer, returns -1. 167*49cdfc7eSAndroid Build Coastguard Worker 168*49cdfc7eSAndroid Build Coastguard Worker- `int tst_kvm_find_memslot(const struct tst_kvm_instance *inst, 169*49cdfc7eSAndroid Build Coastguard Worker uint64_t addr)` – Returns index of the memory slot in KVM virtual machine 170*49cdfc7eSAndroid Build Coastguard Worker `inst` which contains the virtual address `addr`. If the virtual address 171*49cdfc7eSAndroid Build Coastguard Worker is not mapped to a valid physical address backed by a memory buffer, 172*49cdfc7eSAndroid Build Coastguard Worker returns -1. 173*49cdfc7eSAndroid Build Coastguard Worker 174*49cdfc7eSAndroid Build Coastguard Worker- `void *tst_kvm_get_memptr(const struct tst_kvm_instance *inst, 175*49cdfc7eSAndroid Build Coastguard Worker uint64_t addr)` – Converts pointer value (virtual address) from KVM virtual 176*49cdfc7eSAndroid Build Coastguard Worker machine `inst` to host-side pointer. 177*49cdfc7eSAndroid Build Coastguard Worker 178*49cdfc7eSAndroid Build Coastguard Worker- `void *tst_kvm_alloc_memory(struct tst_kvm_instance *inst, unsigned int slot, 179*49cdfc7eSAndroid Build Coastguard Worker uint64_t baseaddr, size_t size, unsigned int flags)` – Allocates a guarded 180*49cdfc7eSAndroid Build Coastguard Worker buffer of given `size` in bytes and installs it into specified memory `slot` 181*49cdfc7eSAndroid Build Coastguard Worker of the KVM virtual machine `inst` at base address `baseaddr`. The buffer 182*49cdfc7eSAndroid Build Coastguard Worker will be automatically page aligned at both ends. See the kernel 183*49cdfc7eSAndroid Build Coastguard Worker documentation of `KVM_SET_USER_MEMORY_REGION` ioctl for list of valid 184*49cdfc7eSAndroid Build Coastguard Worker `flags`. Returns pointer to page-aligned beginning of the allocated buffer. 185*49cdfc7eSAndroid Build Coastguard Worker The actual requested `baseaddr` will be located at 186*49cdfc7eSAndroid Build Coastguard Worker `ret + baseaddr % pagesize`. 187*49cdfc7eSAndroid Build Coastguard Worker 188*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_cpuid2 *tst_kvm_get_cpuid(int sysfd)` – Get a list of supported 189*49cdfc7eSAndroid Build Coastguard Worker virtual CPU features returned by `ioctl(KVM_GET_SUPPORTED_CPUID)`. 190*49cdfc7eSAndroid Build Coastguard Worker The argument must be an open file descriptor returned by `open("/dev/kvm")`. 191*49cdfc7eSAndroid Build Coastguard Worker 192*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_create_instance(struct tst_kvm_instance *inst, 193*49cdfc7eSAndroid Build Coastguard Worker size_t ram_size)` – Creates and fully initializes a new KVM virtual machine 194*49cdfc7eSAndroid Build Coastguard Worker with at least `ram_size` bytes of memory. The VM instance info will be 195*49cdfc7eSAndroid Build Coastguard Worker stored in `inst`. 196*49cdfc7eSAndroid Build Coastguard Worker 197*49cdfc7eSAndroid Build Coastguard Worker- `int tst_kvm_run_instance(struct tst_kvm_instance *inst, int exp_errno)` – 198*49cdfc7eSAndroid Build Coastguard Worker Executes the program installed in KVM virtual machine `inst`. Any result 199*49cdfc7eSAndroid Build Coastguard Worker messages returned by the VM will be automatically printed to controller 200*49cdfc7eSAndroid Build Coastguard Worker program output. Returns zero. If `exp_errno` is non-zero, the VM execution 201*49cdfc7eSAndroid Build Coastguard Worker syscall is allowed to fail with the `exp_errno` error code and 202*49cdfc7eSAndroid Build Coastguard Worker `tst_kvm_run_instance()` will return -1 instead of terminating the test. 203*49cdfc7eSAndroid Build Coastguard Worker 204*49cdfc7eSAndroid Build Coastguard Worker- `void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)` – Deletes 205*49cdfc7eSAndroid Build Coastguard Worker the KVM virtual machine `inst`. Note that the guarded buffers assigned 206*49cdfc7eSAndroid Build Coastguard Worker to the VM by `tst_kvm_create_instance()` or `tst_kvm_alloc_memory()` will 207*49cdfc7eSAndroid Build Coastguard Worker not be freed. 208*49cdfc7eSAndroid Build Coastguard Worker 209*49cdfc7eSAndroid Build Coastguard WorkerThe KVM host library does not provide any way to reset a VM instance back 210*49cdfc7eSAndroid Build Coastguard Workerto initial state. Running multiple iterations of the test requires destroying 211*49cdfc7eSAndroid Build Coastguard Workerthe old VM instance and creating a new one. Otherwise the VM will exit 212*49cdfc7eSAndroid Build Coastguard Workerwithout reporting any results on the second iteration and the test will fail. 213*49cdfc7eSAndroid Build Coastguard WorkerThe `tst_kvm_run()` function handles this issue correctly. 214*49cdfc7eSAndroid Build Coastguard Worker 215*49cdfc7eSAndroid Build Coastguard Worker3. KVM guest library 216*49cdfc7eSAndroid Build Coastguard Worker-------------------- 217*49cdfc7eSAndroid Build Coastguard Worker 218*49cdfc7eSAndroid Build Coastguard WorkerThe KVM guest library provides a minimal implementation of both the LTP 219*49cdfc7eSAndroid Build Coastguard Workertest library and the standard C library functions. Do not try to include 220*49cdfc7eSAndroid Build Coastguard Workerthe usual LTP or C headers in guest payload code, it will not work. 221*49cdfc7eSAndroid Build Coastguard Worker 222*49cdfc7eSAndroid Build Coastguard Worker3.1 Standard C functions 223*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 224*49cdfc7eSAndroid Build Coastguard Worker 225*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_test.h"` 226*49cdfc7eSAndroid Build Coastguard Worker 227*49cdfc7eSAndroid Build Coastguard WorkerThe functions listed below are implemented according to the C standard: 228*49cdfc7eSAndroid Build Coastguard Worker 229*49cdfc7eSAndroid Build Coastguard Worker- `void *memset(void *dest, int val, size_t size)` 230*49cdfc7eSAndroid Build Coastguard Worker- `void *memzero(void *dest, size_t size)` 231*49cdfc7eSAndroid Build Coastguard Worker- `void *memcpy(void *dest, const void *src, size_t size)` 232*49cdfc7eSAndroid Build Coastguard Worker- `char *strcpy(char *dest, const char *src)` 233*49cdfc7eSAndroid Build Coastguard Worker- `char *strcat(char *dest, const char *src)` 234*49cdfc7eSAndroid Build Coastguard Worker- `size_t strlen(const char *str)` 235*49cdfc7eSAndroid Build Coastguard Worker 236*49cdfc7eSAndroid Build Coastguard Worker3.2 LTP library functions 237*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~ 238*49cdfc7eSAndroid Build Coastguard Worker 239*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_test.h"` 240*49cdfc7eSAndroid Build Coastguard Worker 241*49cdfc7eSAndroid Build Coastguard WorkerThe KVM guest library currently provides the LTP functions for reporting test 242*49cdfc7eSAndroid Build Coastguard Workerresults. All standard result flags except for `T*ERRNO` are supported 243*49cdfc7eSAndroid Build Coastguard Workerwith the same rules as usual. However, the printf-like formatting is not 244*49cdfc7eSAndroid Build Coastguard Workerimplemented yet. 245*49cdfc7eSAndroid Build Coastguard Worker 246*49cdfc7eSAndroid Build Coastguard Worker- `void tst_res(int result, const char *message)` 247*49cdfc7eSAndroid Build Coastguard Worker- `void tst_brk(int result, const char *message) __attribute__((noreturn))` 248*49cdfc7eSAndroid Build Coastguard Worker 249*49cdfc7eSAndroid Build Coastguard WorkerA handful of useful macros is also available: 250*49cdfc7eSAndroid Build Coastguard Worker 251*49cdfc7eSAndroid Build Coastguard Worker- `TST_TEST_TCONF(message)` – Generates a test program that will simply print 252*49cdfc7eSAndroid Build Coastguard Worker a `TCONF` message and exit. This is useful when the real test cannot be 253*49cdfc7eSAndroid Build Coastguard Worker built due to missing dependencies or arch limitations. 254*49cdfc7eSAndroid Build Coastguard Worker 255*49cdfc7eSAndroid Build Coastguard Worker- `ARRAY_SIZE(arr)` – Returns the number of items in statically allocated 256*49cdfc7eSAndroid Build Coastguard Worker array `arr`. 257*49cdfc7eSAndroid Build Coastguard Worker 258*49cdfc7eSAndroid Build Coastguard Worker- `LTP_ALIGN(x, a)` – Aligns integer `x` to be a multiple of `a`, which 259*49cdfc7eSAndroid Build Coastguard Worker must be a power of 2. 260*49cdfc7eSAndroid Build Coastguard Worker 261*49cdfc7eSAndroid Build Coastguard Worker3.3 Arch independent functions 262*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 263*49cdfc7eSAndroid Build Coastguard Worker 264*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_test.h"` 265*49cdfc7eSAndroid Build Coastguard Worker 266*49cdfc7eSAndroid Build Coastguard WorkerMemory management in KVM guest library currently uses only primitive linear 267*49cdfc7eSAndroid Build Coastguard Workerbuffer for memory allocation. There are no checks whether the VM can allocate 268*49cdfc7eSAndroid Build Coastguard Workermore memory and the already allocated memory cannot be freed. 269*49cdfc7eSAndroid Build Coastguard Worker 270*49cdfc7eSAndroid Build Coastguard Worker- `void *tst_heap_alloc(size_t size)` – Allocates a block of memory on the heap. 271*49cdfc7eSAndroid Build Coastguard Worker 272*49cdfc7eSAndroid Build Coastguard Worker- `void *tst_heap_alloc_aligned(size_t size, size_t align)` – Allocates 273*49cdfc7eSAndroid Build Coastguard Worker a block of memory on the heap with the starting address aligned to given 274*49cdfc7eSAndroid Build Coastguard Worker value. 275*49cdfc7eSAndroid Build Coastguard Worker 276*49cdfc7eSAndroid Build Coastguard Worker3.4 x86 specific functions 277*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~ 278*49cdfc7eSAndroid Build Coastguard Worker 279*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_test.h"` + 280*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_x86.h"` 281*49cdfc7eSAndroid Build Coastguard Worker 282*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_interrupt_frame` – Opaque arch-dependent structure which holds 283*49cdfc7eSAndroid Build Coastguard Worker interrupt frame information. Use the functions below to get individual values: 284*49cdfc7eSAndroid Build Coastguard Worker 285*49cdfc7eSAndroid Build Coastguard Worker- `uintptr_t kvm_get_interrupt_ip(const struct kvm_interrupt_frame *ifrm)` – 286*49cdfc7eSAndroid Build Coastguard Worker Get instruction pointer value from interrupt frame structure. This may be 287*49cdfc7eSAndroid Build Coastguard Worker the instruction which caused an interrupt or the one immediately after, 288*49cdfc7eSAndroid Build Coastguard Worker depending on the interrupt vector semantics. 289*49cdfc7eSAndroid Build Coastguard Worker 290*49cdfc7eSAndroid Build Coastguard Worker- `int (*tst_interrupt_callback)(void *userdata, 291*49cdfc7eSAndroid Build Coastguard Worker struct kvm_interrupt_frame *ifrm, unsigned long errcode)` – Interrupt handler 292*49cdfc7eSAndroid Build Coastguard Worker callback prototype. When an interrupt occurs, the assigned callback function 293*49cdfc7eSAndroid Build Coastguard Worker will be passed the `userdata` pointer that was given 294*49cdfc7eSAndroid Build Coastguard Worker to `tst_set_interrupt_callback()`, interrupt frame `ifrm` and the error 295*49cdfc7eSAndroid Build Coastguard Worker code `errcode` defined by the interrupt vector semantics. If the interrupt 296*49cdfc7eSAndroid Build Coastguard Worker vector does not generate an error code, `errcode` will be set to zero. 297*49cdfc7eSAndroid Build Coastguard Worker The callback function must return 0 if the interrupt was successfully 298*49cdfc7eSAndroid Build Coastguard Worker handled and test execution should resume. Non-zero return value means that 299*49cdfc7eSAndroid Build Coastguard Worker the interrupt could not be handled and the test will terminate with error. 300*49cdfc7eSAndroid Build Coastguard Worker 301*49cdfc7eSAndroid Build Coastguard Worker- `void tst_set_interrupt_callback(unsigned int vector, 302*49cdfc7eSAndroid Build Coastguard Worker tst_interrupt_callback func, void *userdata)` – Register new interrupt 303*49cdfc7eSAndroid Build Coastguard Worker handler callback function `func` for interrupt `vector`. The `userdata` 304*49cdfc7eSAndroid Build Coastguard Worker argument is an arbitrary pointer that will be passed to `func()` every time 305*49cdfc7eSAndroid Build Coastguard Worker it gets called. The previous interrupt handler callback will be removed. 306*49cdfc7eSAndroid Build Coastguard Worker Setting `func` to `NULL` will remove any existing interrupt handler 307*49cdfc7eSAndroid Build Coastguard Worker from `vector` and the interrupt will become fatal error. 308*49cdfc7eSAndroid Build Coastguard Worker 309*49cdfc7eSAndroid Build Coastguard Worker[source,c] 310*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 311*49cdfc7eSAndroid Build Coastguard Workerstruct page_table_entry_pae { 312*49cdfc7eSAndroid Build Coastguard Worker unsigned int present: 1; 313*49cdfc7eSAndroid Build Coastguard Worker unsigned int writable: 1; 314*49cdfc7eSAndroid Build Coastguard Worker unsigned int user_access: 1; 315*49cdfc7eSAndroid Build Coastguard Worker unsigned int write_through: 1; 316*49cdfc7eSAndroid Build Coastguard Worker unsigned int disable_cache: 1; 317*49cdfc7eSAndroid Build Coastguard Worker unsigned int accessed: 1; 318*49cdfc7eSAndroid Build Coastguard Worker unsigned int dirty: 1; 319*49cdfc7eSAndroid Build Coastguard Worker unsigned int page_type: 1; 320*49cdfc7eSAndroid Build Coastguard Worker unsigned int global: 1; 321*49cdfc7eSAndroid Build Coastguard Worker unsigned int padding: 3; 322*49cdfc7eSAndroid Build Coastguard Worker uint64_t address: 40; 323*49cdfc7eSAndroid Build Coastguard Worker unsigned int padding2: 7; 324*49cdfc7eSAndroid Build Coastguard Worker unsigned int prot_key: 4; 325*49cdfc7eSAndroid Build Coastguard Worker unsigned int noexec: 1; 326*49cdfc7eSAndroid Build Coastguard Worker} __attribute__((__packed__)); 327*49cdfc7eSAndroid Build Coastguard Worker 328*49cdfc7eSAndroid Build Coastguard Workerstruct kvm_cpuid { 329*49cdfc7eSAndroid Build Coastguard Worker unsigned int eax, ebx, ecx, edx; 330*49cdfc7eSAndroid Build Coastguard Worker}; 331*49cdfc7eSAndroid Build Coastguard Worker 332*49cdfc7eSAndroid Build Coastguard Workerstruct kvm_cregs { 333*49cdfc7eSAndroid Build Coastguard Worker unsigned long cr0, cr2, cr3, cr4; 334*49cdfc7eSAndroid Build Coastguard Worker}; 335*49cdfc7eSAndroid Build Coastguard Worker 336*49cdfc7eSAndroid Build Coastguard Workerstruct kvm_sregs { 337*49cdfc7eSAndroid Build Coastguard Worker uint16_t cs, ds, es, fs, gs, ss; 338*49cdfc7eSAndroid Build Coastguard Worker}; 339*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 340*49cdfc7eSAndroid Build Coastguard Worker 341*49cdfc7eSAndroid Build Coastguard Worker`struct page_table_entry_pae` is the page table entry structure for PAE and 342*49cdfc7eSAndroid Build Coastguard Worker64bit paging modes. See Intel(R) 64 and IA-32 Architectures Software 343*49cdfc7eSAndroid Build Coastguard WorkerDeveloper's Manual, Volume 3, Chapter 4 for explanation of the fields. 344*49cdfc7eSAndroid Build Coastguard Worker 345*49cdfc7eSAndroid Build Coastguard Worker- `uintptr_t kvm_get_page_address_pae(const struct page_table_entry_pae *entry)` 346*49cdfc7eSAndroid Build Coastguard Worker – Returns the physical address of the memory page referenced by the given 347*49cdfc7eSAndroid Build Coastguard Worker page table `entry`. Depending on memory mapping changes done by the test, 348*49cdfc7eSAndroid Build Coastguard Worker the physical address may not be a valid pointer. The caller must determine 349*49cdfc7eSAndroid Build Coastguard Worker whether the address points to another page table entry or a data page, using 350*49cdfc7eSAndroid Build Coastguard Worker the known position in page table hierarchy and `entry->page_type`. Returns 351*49cdfc7eSAndroid Build Coastguard Worker zero if the `entry` does not reference any memory page. 352*49cdfc7eSAndroid Build Coastguard Worker 353*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_set_segment_descriptor(struct segment_descriptor *dst, uint64_t baseaddr, uint32_t limit, unsigned int flags)` - 354*49cdfc7eSAndroid Build Coastguard Worker Fill the `dst` segment descriptor with given values. The maximum value 355*49cdfc7eSAndroid Build Coastguard Worker of `limit` is `0xfffff` (inclusive) regardless of `flags`. 356*49cdfc7eSAndroid Build Coastguard Worker 357*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_parse_segment_descriptor(struct segment_descriptor *src, uint64_t *baseaddr, uint32_t *limit, unsigned int *flags)` - 358*49cdfc7eSAndroid Build Coastguard Worker Parse data in the `src` segment descriptor and copy them to variables 359*49cdfc7eSAndroid Build Coastguard Worker pointed to by the other arguments. Any parameter except the first one can 360*49cdfc7eSAndroid Build Coastguard Worker be `NULL`. 361*49cdfc7eSAndroid Build Coastguard Worker 362*49cdfc7eSAndroid Build Coastguard Worker- `int kvm_find_free_descriptor(const struct segment_descriptor *table, size_t size)` - 363*49cdfc7eSAndroid Build Coastguard Worker Find the first segment descriptor in `table` which does not have 364*49cdfc7eSAndroid Build Coastguard Worker the `SEGFLAG_PRESENT` bit set. The function handles double-size descriptors 365*49cdfc7eSAndroid Build Coastguard Worker correctly. Returns index of the first available descriptor or -1 if all 366*49cdfc7eSAndroid Build Coastguard Worker `size` descriptors are taken. 367*49cdfc7eSAndroid Build Coastguard Worker 368*49cdfc7eSAndroid Build Coastguard Worker- `unsigned int kvm_create_stack_descriptor(struct segment_descriptor *table, size_t tabsize, void *stack_base)` - 369*49cdfc7eSAndroid Build Coastguard Worker Convenience function for registering a stack segment descriptor. It'll 370*49cdfc7eSAndroid Build Coastguard Worker automatically find a free slot in `table` and fill the necessary flags. 371*49cdfc7eSAndroid Build Coastguard Worker The `stack_base` pointer must point to the bottom of the stack. 372*49cdfc7eSAndroid Build Coastguard Worker 373*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_get_cpuid(unsigned int eax, unsigned int ecx, 374*49cdfc7eSAndroid Build Coastguard Worker struct kvm_cpuid *buf)` – Executes the CPUID instruction with the given 375*49cdfc7eSAndroid Build Coastguard Worker `eax` and `ecx` arguments and stores the results in `buf`. 376*49cdfc7eSAndroid Build Coastguard Worker 377*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_read_cregs(struct kvm_cregs *buf)` – Copies the current values 378*49cdfc7eSAndroid Build Coastguard Worker of control registers to `buf`. 379*49cdfc7eSAndroid Build Coastguard Worker 380*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_read_sregs(struct kvm_sregs *buf)` - Copies the current values 381*49cdfc7eSAndroid Build Coastguard Worker of segment registers to `buf`. 382*49cdfc7eSAndroid Build Coastguard Worker 383*49cdfc7eSAndroid Build Coastguard Worker- `uint64_t kvm_rdmsr(unsigned int msr)` – Returns the current value 384*49cdfc7eSAndroid Build Coastguard Worker of model-specific register `msr`. 385*49cdfc7eSAndroid Build Coastguard Worker 386*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_wrmsr(unsigned int msr, uint64_t value)` – Stores `value` 387*49cdfc7eSAndroid Build Coastguard Worker into model-specific register `msr`. 388*49cdfc7eSAndroid Build Coastguard Worker 389*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_exit(void) __attribute__((noreturn))` – Terminate the test. 390*49cdfc7eSAndroid Build Coastguard Worker Similar to calling `exit(0)` in a regular LTP test, although `kvm_exit()` 391*49cdfc7eSAndroid Build Coastguard Worker will terminate only one iteration of the test, not the whole host process. 392*49cdfc7eSAndroid Build Coastguard Worker 393*49cdfc7eSAndroid Build Coastguard WorkerSee Intel(R) 64 and IA-32 Architectures Software Developer's Manual 394*49cdfc7eSAndroid Build Coastguard Workerfor documentation of standard and model-specific x86 registers. 395*49cdfc7eSAndroid Build Coastguard Worker 396*49cdfc7eSAndroid Build Coastguard Worker3.5 AMD SVM helper functions 397*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 398*49cdfc7eSAndroid Build Coastguard Worker 399*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_test.h"` + 400*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_x86.h"` + 401*49cdfc7eSAndroid Build Coastguard Worker`#include "kvm_x86_svm.h"` 402*49cdfc7eSAndroid Build Coastguard Worker 403*49cdfc7eSAndroid Build Coastguard WorkerThe KVM guest library provides basic helper functions for creating and running 404*49cdfc7eSAndroid Build Coastguard Workernested virtual machines using the AMD SVM technology. 405*49cdfc7eSAndroid Build Coastguard Worker 406*49cdfc7eSAndroid Build Coastguard Worker.Example code to execute nested VM 407*49cdfc7eSAndroid Build Coastguard Worker[source,c] 408*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 409*49cdfc7eSAndroid Build Coastguard Workerint guest_main(void) 410*49cdfc7eSAndroid Build Coastguard Worker{ 411*49cdfc7eSAndroid Build Coastguard Worker ... 412*49cdfc7eSAndroid Build Coastguard Worker return 0; 413*49cdfc7eSAndroid Build Coastguard Worker} 414*49cdfc7eSAndroid Build Coastguard Worker 415*49cdfc7eSAndroid Build Coastguard Workervoid main(void) 416*49cdfc7eSAndroid Build Coastguard Worker{ 417*49cdfc7eSAndroid Build Coastguard Worker struct kvm_svm_vcpu *vm; 418*49cdfc7eSAndroid Build Coastguard Worker 419*49cdfc7eSAndroid Build Coastguard Worker kvm_init_svm(); 420*49cdfc7eSAndroid Build Coastguard Worker vm = kvm_create_svm_vcpu(guest_main, 1); 421*49cdfc7eSAndroid Build Coastguard Worker kvm_svm_vmrun(vm); 422*49cdfc7eSAndroid Build Coastguard Worker} 423*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 424*49cdfc7eSAndroid Build Coastguard Worker 425*49cdfc7eSAndroid Build Coastguard Worker- `int kvm_is_svm_supported(void)` - Returns non-zero value if the CPU 426*49cdfc7eSAndroid Build Coastguard Worker supports AMD SVM, otherwise returns 0. 427*49cdfc7eSAndroid Build Coastguard Worker 428*49cdfc7eSAndroid Build Coastguard Worker- `int kvm_get_svm_state(void)` - Returns non-zero value if SVM is currently 429*49cdfc7eSAndroid Build Coastguard Worker enabled, otherwise returns 0. 430*49cdfc7eSAndroid Build Coastguard Worker 431*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_set_svm_state(int enabled)` - Enable or disable SVM according 432*49cdfc7eSAndroid Build Coastguard Worker to argument. If SVM is disabled by host or not supported, the test will exit 433*49cdfc7eSAndroid Build Coastguard Worker with `TCONF`. 434*49cdfc7eSAndroid Build Coastguard Worker 435*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_init_svm(void)` - Enable and fully initialize SVM, including 436*49cdfc7eSAndroid Build Coastguard Worker allocating and setting up host save area VMCB. If SVM is disabled by host or 437*49cdfc7eSAndroid Build Coastguard Worker not supported, the test will exit with `TCONF`. 438*49cdfc7eSAndroid Build Coastguard Worker 439*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_vmcb *kvm_alloc_vmcb(void)` - Allocate new VMCB structure 440*49cdfc7eSAndroid Build Coastguard Worker with correct memory alignment and fill it with zeroes. 441*49cdfc7eSAndroid Build Coastguard Worker 442*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_vmcb_set_intercept(struct kvm_vmcb *vmcb, unsigned int id, unsigned int state)` - 443*49cdfc7eSAndroid Build Coastguard Worker Set SVM intercept bit `id` to given `state`. 444*49cdfc7eSAndroid Build Coastguard Worker 445*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_init_guest_vmcb(struct kvm_vmcb *vmcb, uint32_t asid, uint16_t ss, void *rsp, int (*guest_main)(void))` - 446*49cdfc7eSAndroid Build Coastguard Worker Initialize new SVM virtual machine. The `asid` parameter is the nested 447*49cdfc7eSAndroid Build Coastguard Worker page table ID. The `ss` and `rsp` parameters set the stack segment and stack 448*49cdfc7eSAndroid Build Coastguard Worker pointer values, respectively. The `guest_main` parameter sets the code entry 449*49cdfc7eSAndroid Build Coastguard Worker point of the virtual machine. All control registers, segment registers 450*49cdfc7eSAndroid Build Coastguard Worker (except stack segment register), GDTR and IDTR will be copied 451*49cdfc7eSAndroid Build Coastguard Worker from the current CPU state. 452*49cdfc7eSAndroid Build Coastguard Worker 453*49cdfc7eSAndroid Build Coastguard Worker- `struct kvm_svm_vcpu *kvm_create_svm_vcpu(int (*guest_main)(void), int alloc_stack)` - 454*49cdfc7eSAndroid Build Coastguard Worker Convenience function for allocating and initializing new SVM virtual CPU. 455*49cdfc7eSAndroid Build Coastguard Worker The `guest_main` parameter is passed to `kvm_init_guest_vmcb()`, 456*49cdfc7eSAndroid Build Coastguard Worker the `alloc_stack` parameter controls whether a new 8KB stack will be 457*49cdfc7eSAndroid Build Coastguard Worker allocated and registered in GDT. Interception will be enabled for `VMSAVE` 458*49cdfc7eSAndroid Build Coastguard Worker and `HLT` instructions. If you set `alloc_stack` to zero, you must configure 459*49cdfc7eSAndroid Build Coastguard Worker the stack segment register and stack pointer manually. 460*49cdfc7eSAndroid Build Coastguard Worker 461*49cdfc7eSAndroid Build Coastguard Worker- `void kvm_svm_vmrun(struct kvm_svm_vcpu *cpu)` - Start or continue execution 462*49cdfc7eSAndroid Build Coastguard Worker of a nested virtual machine. Beware that FPU state is not saved. Do not use 463*49cdfc7eSAndroid Build Coastguard Worker floating point types or values in nested guest code. Also do not use 464*49cdfc7eSAndroid Build Coastguard Worker `tst_res()` or `tst_brk()` functions in nested guest code. 465*49cdfc7eSAndroid Build Coastguard Worker 466*49cdfc7eSAndroid Build Coastguard WorkerSee AMD64 Architecture Programmer's Manual Volume 2 for documentation 467*49cdfc7eSAndroid Build Coastguard Workerof the Secure Virtual Machine (SVM) technology. 468*49cdfc7eSAndroid Build Coastguard Worker 469*49cdfc7eSAndroid Build Coastguard Worker4. KVM guest environment 470*49cdfc7eSAndroid Build Coastguard Worker------------------------ 471*49cdfc7eSAndroid Build Coastguard Worker 472*49cdfc7eSAndroid Build Coastguard WorkerKVM guest payload execution begins with bootstrap code which will perform 473*49cdfc7eSAndroid Build Coastguard Workerthe minimal guest environment setup required for running C code: 474*49cdfc7eSAndroid Build Coastguard Worker 475*49cdfc7eSAndroid Build Coastguard Worker- Activate the appropriate CPU execution mode (IA-32 protected mode 476*49cdfc7eSAndroid Build Coastguard Worker on 32-bit x86 or the 64-bit mode on x86_64). 477*49cdfc7eSAndroid Build Coastguard Worker- Create indentity mapping (virtual address = physical address) of the lower 478*49cdfc7eSAndroid Build Coastguard Worker 2GB memory region, even if parts of the region are not backed by any host 479*49cdfc7eSAndroid Build Coastguard Worker memory buffers. The memory region above 2GB threshold is left unmapped 480*49cdfc7eSAndroid Build Coastguard Worker except for one memory page reserved for the `struct tst_kvm_result` buffer. 481*49cdfc7eSAndroid Build Coastguard Worker- Initialize 8KB stack. 482*49cdfc7eSAndroid Build Coastguard Worker- Install default interrupt handlers for standard CPU exception vectors. 483*49cdfc7eSAndroid Build Coastguard Worker 484*49cdfc7eSAndroid Build Coastguard WorkerWhen the environment setup is complete, bootstrap will call `void main(void)` 485*49cdfc7eSAndroid Build Coastguard Workerfunction implemented by the test program. To finish execution of guest payload, 486*49cdfc7eSAndroid Build Coastguard Workerthe test can either return from the `main()` function or call `kvm_exit()` 487*49cdfc7eSAndroid Build Coastguard Workerat any point. 488