1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2018, Google LLC. 4 */ 5 #ifndef SELFTEST_KVM_UTIL_H 6 #define SELFTEST_KVM_UTIL_H 7 8 #include "test_util.h" 9 10 #include <linux/compiler.h> 11 #include "linux/hashtable.h" 12 #include "linux/list.h" 13 #include <linux/kernel.h> 14 #include <linux/kvm.h> 15 #include "linux/rbtree.h" 16 #include <linux/types.h> 17 18 #include <asm/atomic.h> 19 #include <asm/kvm.h> 20 21 #include <sys/eventfd.h> 22 #include <sys/ioctl.h> 23 24 #include <pthread.h> 25 26 #include "kvm_util_arch.h" 27 #include "kvm_util_types.h" 28 #include "sparsebit.h" 29 30 #define KVM_DEV_PATH "/dev/kvm" 31 #define KVM_MAX_VCPUS 512 32 33 #define NSEC_PER_SEC 1000000000L 34 35 struct userspace_mem_region { 36 struct kvm_userspace_memory_region2 region; 37 struct sparsebit *unused_phy_pages; 38 struct sparsebit *protected_phy_pages; 39 int fd; 40 off_t offset; 41 enum vm_mem_backing_src_type backing_src_type; 42 void *host_mem; 43 void *host_alias; 44 void *mmap_start; 45 void *mmap_alias; 46 size_t mmap_size; 47 struct rb_node gpa_node; 48 struct rb_node hva_node; 49 struct hlist_node slot_node; 50 }; 51 52 struct kvm_binary_stats { 53 int fd; 54 struct kvm_stats_header header; 55 struct kvm_stats_desc *desc; 56 }; 57 58 struct kvm_vcpu { 59 struct list_head list; 60 uint32_t id; 61 int fd; 62 struct kvm_vm *vm; 63 struct kvm_run *run; 64 #ifdef __x86_64__ 65 struct kvm_cpuid2 *cpuid; 66 #endif 67 #ifdef __aarch64__ 68 struct kvm_vcpu_init init; 69 #endif 70 struct kvm_binary_stats stats; 71 struct kvm_dirty_gfn *dirty_gfns; 72 uint32_t fetch_index; 73 uint32_t dirty_gfns_count; 74 }; 75 76 struct userspace_mem_regions { 77 struct rb_root gpa_tree; 78 struct rb_root hva_tree; 79 DECLARE_HASHTABLE(slot_hash, 9); 80 }; 81 82 enum kvm_mem_region_type { 83 MEM_REGION_CODE, 84 MEM_REGION_DATA, 85 MEM_REGION_PT, 86 MEM_REGION_TEST_DATA, 87 NR_MEM_REGIONS, 88 }; 89 90 struct kvm_vm { 91 int mode; 92 unsigned long type; 93 int kvm_fd; 94 int fd; 95 unsigned int pgtable_levels; 96 unsigned int page_size; 97 unsigned int page_shift; 98 unsigned int pa_bits; 99 unsigned int va_bits; 100 uint64_t max_gfn; 101 struct list_head vcpus; 102 struct userspace_mem_regions regions; 103 struct sparsebit *vpages_valid; 104 struct sparsebit *vpages_mapped; 105 bool has_irqchip; 106 bool pgd_created; 107 vm_paddr_t ucall_mmio_addr; 108 vm_paddr_t pgd; 109 vm_vaddr_t handlers; 110 uint32_t dirty_ring_size; 111 uint64_t gpa_tag_mask; 112 113 struct kvm_vm_arch arch; 114 115 struct kvm_binary_stats stats; 116 117 /* 118 * KVM region slots. These are the default memslots used by page 119 * allocators, e.g., lib/elf uses the memslots[MEM_REGION_CODE] 120 * memslot. 121 */ 122 uint32_t memslots[NR_MEM_REGIONS]; 123 }; 124 125 struct vcpu_reg_sublist { 126 const char *name; 127 long capability; 128 int feature; 129 int feature_type; 130 bool finalize; 131 __u64 *regs; 132 __u64 regs_n; 133 __u64 *rejects_set; 134 __u64 rejects_set_n; 135 __u64 *skips_set; 136 __u64 skips_set_n; 137 }; 138 139 struct vcpu_reg_list { 140 char *name; 141 struct vcpu_reg_sublist sublists[]; 142 }; 143 144 #define for_each_sublist(c, s) \ 145 for ((s) = &(c)->sublists[0]; (s)->regs; ++(s)) 146 147 #define kvm_for_each_vcpu(vm, i, vcpu) \ 148 for ((i) = 0; (i) <= (vm)->last_vcpu_id; (i)++) \ 149 if (!((vcpu) = vm->vcpus[i])) \ 150 continue; \ 151 else 152 153 struct userspace_mem_region * 154 memslot2region(struct kvm_vm *vm, uint32_t memslot); 155 156 static inline struct userspace_mem_region *vm_get_mem_region(struct kvm_vm *vm, 157 enum kvm_mem_region_type type) 158 { 159 assert(type < NR_MEM_REGIONS); 160 return memslot2region(vm, vm->memslots[type]); 161 } 162 163 /* Minimum allocated guest virtual and physical addresses */ 164 #define KVM_UTIL_MIN_VADDR 0x2000 165 #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 166 167 #define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000 168 #define DEFAULT_STACK_PGS 5 169 170 enum vm_guest_mode { 171 VM_MODE_P52V48_4K, 172 VM_MODE_P52V48_16K, 173 VM_MODE_P52V48_64K, 174 VM_MODE_P48V48_4K, 175 VM_MODE_P48V48_16K, 176 VM_MODE_P48V48_64K, 177 VM_MODE_P40V48_4K, 178 VM_MODE_P40V48_16K, 179 VM_MODE_P40V48_64K, 180 VM_MODE_PXXV48_4K, /* For 48bits VA but ANY bits PA */ 181 VM_MODE_P47V64_4K, 182 VM_MODE_P44V64_4K, 183 VM_MODE_P36V48_4K, 184 VM_MODE_P36V48_16K, 185 VM_MODE_P36V48_64K, 186 VM_MODE_P47V47_16K, 187 VM_MODE_P36V47_16K, 188 NUM_VM_MODES, 189 }; 190 191 struct vm_shape { 192 uint32_t type; 193 uint8_t mode; 194 uint8_t pad0; 195 uint16_t pad1; 196 }; 197 198 kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); 199 200 #define VM_TYPE_DEFAULT 0 201 202 #define VM_SHAPE(__mode) \ 203 ({ \ 204 struct vm_shape shape = { \ 205 .mode = (__mode), \ 206 .type = VM_TYPE_DEFAULT \ 207 }; \ 208 \ 209 shape; \ 210 }) 211 212 #if defined(__aarch64__) 213 214 extern enum vm_guest_mode vm_mode_default; 215 216 #define VM_MODE_DEFAULT vm_mode_default 217 #define MIN_PAGE_SHIFT 12U 218 #define ptes_per_page(page_size) ((page_size) / 8) 219 220 #elif defined(__x86_64__) 221 222 #define VM_MODE_DEFAULT VM_MODE_PXXV48_4K 223 #define MIN_PAGE_SHIFT 12U 224 #define ptes_per_page(page_size) ((page_size) / 8) 225 226 #elif defined(__s390x__) 227 228 #define VM_MODE_DEFAULT VM_MODE_P44V64_4K 229 #define MIN_PAGE_SHIFT 12U 230 #define ptes_per_page(page_size) ((page_size) / 16) 231 232 #elif defined(__riscv) 233 234 #if __riscv_xlen == 32 235 #error "RISC-V 32-bit kvm selftests not supported" 236 #endif 237 238 #define VM_MODE_DEFAULT VM_MODE_P40V48_4K 239 #define MIN_PAGE_SHIFT 12U 240 #define ptes_per_page(page_size) ((page_size) / 8) 241 242 #elif defined(__loongarch__) 243 #define VM_MODE_DEFAULT VM_MODE_P47V47_16K 244 #define MIN_PAGE_SHIFT 12U 245 #define ptes_per_page(page_size) ((page_size) / 8) 246 247 #endif 248 249 #define VM_SHAPE_DEFAULT VM_SHAPE(VM_MODE_DEFAULT) 250 251 #define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) 252 #define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) 253 254 struct vm_guest_mode_params { 255 unsigned int pa_bits; 256 unsigned int va_bits; 257 unsigned int page_size; 258 unsigned int page_shift; 259 }; 260 extern const struct vm_guest_mode_params vm_guest_mode_params[]; 261 262 int __open_path_or_exit(const char *path, int flags, const char *enoent_help); 263 int open_path_or_exit(const char *path, int flags); 264 int open_kvm_dev_path_or_exit(void); 265 266 int kvm_get_module_param_integer(const char *module_name, const char *param); 267 bool kvm_get_module_param_bool(const char *module_name, const char *param); 268 269 static inline bool get_kvm_param_bool(const char *param) 270 { 271 return kvm_get_module_param_bool("kvm", param); 272 } 273 274 static inline int get_kvm_param_integer(const char *param) 275 { 276 return kvm_get_module_param_integer("kvm", param); 277 } 278 279 unsigned int kvm_check_cap(long cap); 280 281 static inline bool kvm_has_cap(long cap) 282 { 283 return kvm_check_cap(cap); 284 } 285 286 #define __KVM_SYSCALL_ERROR(_name, _ret) \ 287 "%s failed, rc: %i errno: %i (%s)", (_name), (_ret), errno, strerror(errno) 288 289 /* 290 * Use the "inner", double-underscore macro when reporting errors from within 291 * other macros so that the name of ioctl() and not its literal numeric value 292 * is printed on error. The "outer" macro is strongly preferred when reporting 293 * errors "directly", i.e. without an additional layer of macros, as it reduces 294 * the probability of passing in the wrong string. 295 */ 296 #define __KVM_IOCTL_ERROR(_name, _ret) __KVM_SYSCALL_ERROR(_name, _ret) 297 #define KVM_IOCTL_ERROR(_ioctl, _ret) __KVM_IOCTL_ERROR(#_ioctl, _ret) 298 299 #define kvm_do_ioctl(fd, cmd, arg) \ 300 ({ \ 301 kvm_static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) == _IOC_SIZE(cmd)); \ 302 ioctl(fd, cmd, arg); \ 303 }) 304 305 #define __kvm_ioctl(kvm_fd, cmd, arg) \ 306 kvm_do_ioctl(kvm_fd, cmd, arg) 307 308 #define kvm_ioctl(kvm_fd, cmd, arg) \ 309 ({ \ 310 int ret = __kvm_ioctl(kvm_fd, cmd, arg); \ 311 \ 312 TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ 313 }) 314 315 static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { } 316 317 #define __vm_ioctl(vm, cmd, arg) \ 318 ({ \ 319 static_assert_is_vm(vm); \ 320 kvm_do_ioctl((vm)->fd, cmd, arg); \ 321 }) 322 323 /* 324 * Assert that a VM or vCPU ioctl() succeeded, with extra magic to detect if 325 * the ioctl() failed because KVM killed/bugged the VM. To detect a dead VM, 326 * probe KVM_CAP_USER_MEMORY, which (a) has been supported by KVM since before 327 * selftests existed and (b) should never outright fail, i.e. is supposed to 328 * return 0 or 1. If KVM kills a VM, KVM returns -EIO for all ioctl()s for the 329 * VM and its vCPUs, including KVM_CHECK_EXTENSION. 330 */ 331 #define __TEST_ASSERT_VM_VCPU_IOCTL(cond, name, ret, vm) \ 332 do { \ 333 int __errno = errno; \ 334 \ 335 static_assert_is_vm(vm); \ 336 \ 337 if (cond) \ 338 break; \ 339 \ 340 if (errno == EIO && \ 341 __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)KVM_CAP_USER_MEMORY) < 0) { \ 342 TEST_ASSERT(errno == EIO, "KVM killed the VM, should return -EIO"); \ 343 TEST_FAIL("KVM killed/bugged the VM, check the kernel log for clues"); \ 344 } \ 345 errno = __errno; \ 346 TEST_ASSERT(cond, __KVM_IOCTL_ERROR(name, ret)); \ 347 } while (0) 348 349 #define TEST_ASSERT_VM_VCPU_IOCTL(cond, cmd, ret, vm) \ 350 __TEST_ASSERT_VM_VCPU_IOCTL(cond, #cmd, ret, vm) 351 352 #define vm_ioctl(vm, cmd, arg) \ 353 ({ \ 354 int ret = __vm_ioctl(vm, cmd, arg); \ 355 \ 356 __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ 357 }) 358 359 static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } 360 361 #define __vcpu_ioctl(vcpu, cmd, arg) \ 362 ({ \ 363 static_assert_is_vcpu(vcpu); \ 364 kvm_do_ioctl((vcpu)->fd, cmd, arg); \ 365 }) 366 367 #define vcpu_ioctl(vcpu, cmd, arg) \ 368 ({ \ 369 int ret = __vcpu_ioctl(vcpu, cmd, arg); \ 370 \ 371 __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm); \ 372 }) 373 374 /* 375 * Looks up and returns the value corresponding to the capability 376 * (KVM_CAP_*) given by cap. 377 */ 378 static inline int vm_check_cap(struct kvm_vm *vm, long cap) 379 { 380 int ret = __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)cap); 381 382 TEST_ASSERT_VM_VCPU_IOCTL(ret >= 0, KVM_CHECK_EXTENSION, ret, vm); 383 return ret; 384 } 385 386 static inline int __vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0) 387 { 388 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } }; 389 390 return __vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); 391 } 392 static inline void vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0) 393 { 394 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } }; 395 396 vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); 397 } 398 399 static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa, 400 uint64_t size, uint64_t attributes) 401 { 402 struct kvm_memory_attributes attr = { 403 .attributes = attributes, 404 .address = gpa, 405 .size = size, 406 .flags = 0, 407 }; 408 409 /* 410 * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows 411 * need significant enhancements to support multiple attributes. 412 */ 413 TEST_ASSERT(!attributes || attributes == KVM_MEMORY_ATTRIBUTE_PRIVATE, 414 "Update me to support multiple attributes!"); 415 416 vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); 417 } 418 419 420 static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa, 421 uint64_t size) 422 { 423 vm_set_memory_attributes(vm, gpa, size, KVM_MEMORY_ATTRIBUTE_PRIVATE); 424 } 425 426 static inline void vm_mem_set_shared(struct kvm_vm *vm, uint64_t gpa, 427 uint64_t size) 428 { 429 vm_set_memory_attributes(vm, gpa, size, 0); 430 } 431 432 void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size, 433 bool punch_hole); 434 435 static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t gpa, 436 uint64_t size) 437 { 438 vm_guest_mem_fallocate(vm, gpa, size, true); 439 } 440 441 static inline void vm_guest_mem_allocate(struct kvm_vm *vm, uint64_t gpa, 442 uint64_t size) 443 { 444 vm_guest_mem_fallocate(vm, gpa, size, false); 445 } 446 447 void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); 448 const char *vm_guest_mode_string(uint32_t i); 449 450 void kvm_vm_free(struct kvm_vm *vmp); 451 void kvm_vm_restart(struct kvm_vm *vmp); 452 void kvm_vm_release(struct kvm_vm *vmp); 453 void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename); 454 int kvm_memfd_alloc(size_t size, bool hugepages); 455 456 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent); 457 458 static inline void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log) 459 { 460 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot }; 461 462 vm_ioctl(vm, KVM_GET_DIRTY_LOG, &args); 463 } 464 465 static inline void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log, 466 uint64_t first_page, uint32_t num_pages) 467 { 468 struct kvm_clear_dirty_log args = { 469 .dirty_bitmap = log, 470 .slot = slot, 471 .first_page = first_page, 472 .num_pages = num_pages 473 }; 474 475 vm_ioctl(vm, KVM_CLEAR_DIRTY_LOG, &args); 476 } 477 478 static inline uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm) 479 { 480 return __vm_ioctl(vm, KVM_RESET_DIRTY_RINGS, NULL); 481 } 482 483 static inline void kvm_vm_register_coalesced_io(struct kvm_vm *vm, 484 uint64_t address, 485 uint64_t size, bool pio) 486 { 487 struct kvm_coalesced_mmio_zone zone = { 488 .addr = address, 489 .size = size, 490 .pio = pio, 491 }; 492 493 vm_ioctl(vm, KVM_REGISTER_COALESCED_MMIO, &zone); 494 } 495 496 static inline void kvm_vm_unregister_coalesced_io(struct kvm_vm *vm, 497 uint64_t address, 498 uint64_t size, bool pio) 499 { 500 struct kvm_coalesced_mmio_zone zone = { 501 .addr = address, 502 .size = size, 503 .pio = pio, 504 }; 505 506 vm_ioctl(vm, KVM_UNREGISTER_COALESCED_MMIO, &zone); 507 } 508 509 static inline int vm_get_stats_fd(struct kvm_vm *vm) 510 { 511 int fd = __vm_ioctl(vm, KVM_GET_STATS_FD, NULL); 512 513 TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, KVM_GET_STATS_FD, fd, vm); 514 return fd; 515 } 516 517 static inline int __kvm_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd, 518 uint32_t flags) 519 { 520 struct kvm_irqfd irqfd = { 521 .fd = eventfd, 522 .gsi = gsi, 523 .flags = flags, 524 .resamplefd = -1, 525 }; 526 527 return __vm_ioctl(vm, KVM_IRQFD, &irqfd); 528 } 529 530 static inline void kvm_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd, 531 uint32_t flags) 532 { 533 int ret = __kvm_irqfd(vm, gsi, eventfd, flags); 534 535 TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_IRQFD, ret, vm); 536 } 537 538 static inline void kvm_assign_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd) 539 { 540 kvm_irqfd(vm, gsi, eventfd, 0); 541 } 542 543 static inline void kvm_deassign_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd) 544 { 545 kvm_irqfd(vm, gsi, eventfd, KVM_IRQFD_FLAG_DEASSIGN); 546 } 547 548 static inline int kvm_new_eventfd(void) 549 { 550 int fd = eventfd(0, 0); 551 552 TEST_ASSERT(fd >= 0, __KVM_SYSCALL_ERROR("eventfd()", fd)); 553 return fd; 554 } 555 556 static inline void read_stats_header(int stats_fd, struct kvm_stats_header *header) 557 { 558 ssize_t ret; 559 560 ret = pread(stats_fd, header, sizeof(*header), 0); 561 TEST_ASSERT(ret == sizeof(*header), 562 "Failed to read '%lu' header bytes, ret = '%ld'", 563 sizeof(*header), ret); 564 } 565 566 struct kvm_stats_desc *read_stats_descriptors(int stats_fd, 567 struct kvm_stats_header *header); 568 569 static inline ssize_t get_stats_descriptor_size(struct kvm_stats_header *header) 570 { 571 /* 572 * The base size of the descriptor is defined by KVM's ABI, but the 573 * size of the name field is variable, as far as KVM's ABI is 574 * concerned. For a given instance of KVM, the name field is the same 575 * size for all stats and is provided in the overall stats header. 576 */ 577 return sizeof(struct kvm_stats_desc) + header->name_size; 578 } 579 580 static inline struct kvm_stats_desc *get_stats_descriptor(struct kvm_stats_desc *stats, 581 int index, 582 struct kvm_stats_header *header) 583 { 584 /* 585 * Note, size_desc includes the size of the name field, which is 586 * variable. i.e. this is NOT equivalent to &stats_desc[i]. 587 */ 588 return (void *)stats + index * get_stats_descriptor_size(header); 589 } 590 591 void read_stat_data(int stats_fd, struct kvm_stats_header *header, 592 struct kvm_stats_desc *desc, uint64_t *data, 593 size_t max_elements); 594 595 void kvm_get_stat(struct kvm_binary_stats *stats, const char *name, 596 uint64_t *data, size_t max_elements); 597 598 #define __get_stat(stats, stat) \ 599 ({ \ 600 uint64_t data; \ 601 \ 602 kvm_get_stat(stats, #stat, &data, 1); \ 603 data; \ 604 }) 605 606 #define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat) 607 #define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat) 608 609 static inline bool read_smt_control(char *buf, size_t buf_size) 610 { 611 FILE *f = fopen("/sys/devices/system/cpu/smt/control", "r"); 612 bool ret; 613 614 if (!f) 615 return false; 616 617 ret = fread(buf, sizeof(*buf), buf_size, f) > 0; 618 fclose(f); 619 620 return ret; 621 } 622 623 static inline bool is_smt_possible(void) 624 { 625 char buf[16]; 626 627 if (read_smt_control(buf, sizeof(buf)) && 628 (!strncmp(buf, "forceoff", 8) || !strncmp(buf, "notsupported", 12))) 629 return false; 630 631 return true; 632 } 633 634 static inline bool is_smt_on(void) 635 { 636 char buf[16]; 637 638 if (read_smt_control(buf, sizeof(buf)) && !strncmp(buf, "on", 2)) 639 return true; 640 641 return false; 642 } 643 644 void vm_create_irqchip(struct kvm_vm *vm); 645 646 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, 647 uint64_t flags) 648 { 649 struct kvm_create_guest_memfd guest_memfd = { 650 .size = size, 651 .flags = flags, 652 }; 653 654 return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &guest_memfd); 655 } 656 657 static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, 658 uint64_t flags) 659 { 660 int fd = __vm_create_guest_memfd(vm, size, flags); 661 662 TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd)); 663 return fd; 664 } 665 666 void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, 667 uint64_t gpa, uint64_t size, void *hva); 668 int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, 669 uint64_t gpa, uint64_t size, void *hva); 670 void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, 671 uint64_t gpa, uint64_t size, void *hva, 672 uint32_t guest_memfd, uint64_t guest_memfd_offset); 673 int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, 674 uint64_t gpa, uint64_t size, void *hva, 675 uint32_t guest_memfd, uint64_t guest_memfd_offset); 676 677 void vm_userspace_mem_region_add(struct kvm_vm *vm, 678 enum vm_mem_backing_src_type src_type, 679 uint64_t guest_paddr, uint32_t slot, uint64_t npages, 680 uint32_t flags); 681 void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, 682 uint64_t guest_paddr, uint32_t slot, uint64_t npages, 683 uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset); 684 685 #ifndef vm_arch_has_protected_memory 686 static inline bool vm_arch_has_protected_memory(struct kvm_vm *vm) 687 { 688 return false; 689 } 690 #endif 691 692 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); 693 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); 694 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); 695 struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id); 696 void vm_populate_vaddr_bitmap(struct kvm_vm *vm); 697 vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); 698 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); 699 vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, 700 enum kvm_mem_region_type type); 701 vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, 702 vm_vaddr_t vaddr_min, 703 enum kvm_mem_region_type type); 704 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); 705 vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, 706 enum kvm_mem_region_type type); 707 vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); 708 709 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, 710 unsigned int npages); 711 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); 712 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); 713 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); 714 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); 715 716 #ifndef vcpu_arch_put_guest 717 #define vcpu_arch_put_guest(mem, val) do { (mem) = (val); } while (0) 718 #endif 719 720 static inline vm_paddr_t vm_untag_gpa(struct kvm_vm *vm, vm_paddr_t gpa) 721 { 722 return gpa & ~vm->gpa_tag_mask; 723 } 724 725 void vcpu_run(struct kvm_vcpu *vcpu); 726 int _vcpu_run(struct kvm_vcpu *vcpu); 727 728 static inline int __vcpu_run(struct kvm_vcpu *vcpu) 729 { 730 return __vcpu_ioctl(vcpu, KVM_RUN, NULL); 731 } 732 733 void vcpu_run_complete_io(struct kvm_vcpu *vcpu); 734 struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu); 735 736 static inline void vcpu_enable_cap(struct kvm_vcpu *vcpu, uint32_t cap, 737 uint64_t arg0) 738 { 739 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } }; 740 741 vcpu_ioctl(vcpu, KVM_ENABLE_CAP, &enable_cap); 742 } 743 744 static inline void vcpu_guest_debug_set(struct kvm_vcpu *vcpu, 745 struct kvm_guest_debug *debug) 746 { 747 vcpu_ioctl(vcpu, KVM_SET_GUEST_DEBUG, debug); 748 } 749 750 static inline void vcpu_mp_state_get(struct kvm_vcpu *vcpu, 751 struct kvm_mp_state *mp_state) 752 { 753 vcpu_ioctl(vcpu, KVM_GET_MP_STATE, mp_state); 754 } 755 static inline void vcpu_mp_state_set(struct kvm_vcpu *vcpu, 756 struct kvm_mp_state *mp_state) 757 { 758 vcpu_ioctl(vcpu, KVM_SET_MP_STATE, mp_state); 759 } 760 761 static inline void vcpu_regs_get(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 762 { 763 vcpu_ioctl(vcpu, KVM_GET_REGS, regs); 764 } 765 766 static inline void vcpu_regs_set(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 767 { 768 vcpu_ioctl(vcpu, KVM_SET_REGS, regs); 769 } 770 static inline void vcpu_sregs_get(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 771 { 772 vcpu_ioctl(vcpu, KVM_GET_SREGS, sregs); 773 774 } 775 static inline void vcpu_sregs_set(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 776 { 777 vcpu_ioctl(vcpu, KVM_SET_SREGS, sregs); 778 } 779 static inline int _vcpu_sregs_set(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 780 { 781 return __vcpu_ioctl(vcpu, KVM_SET_SREGS, sregs); 782 } 783 static inline void vcpu_fpu_get(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 784 { 785 vcpu_ioctl(vcpu, KVM_GET_FPU, fpu); 786 } 787 static inline void vcpu_fpu_set(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 788 { 789 vcpu_ioctl(vcpu, KVM_SET_FPU, fpu); 790 } 791 792 static inline int __vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id, void *addr) 793 { 794 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)addr }; 795 796 return __vcpu_ioctl(vcpu, KVM_GET_ONE_REG, ®); 797 } 798 static inline int __vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val) 799 { 800 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val }; 801 802 return __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®); 803 } 804 static inline uint64_t vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id) 805 { 806 uint64_t val; 807 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val }; 808 809 TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id); 810 811 vcpu_ioctl(vcpu, KVM_GET_ONE_REG, ®); 812 return val; 813 } 814 static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val) 815 { 816 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val }; 817 818 TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id); 819 820 vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®); 821 } 822 823 #ifdef __KVM_HAVE_VCPU_EVENTS 824 static inline void vcpu_events_get(struct kvm_vcpu *vcpu, 825 struct kvm_vcpu_events *events) 826 { 827 vcpu_ioctl(vcpu, KVM_GET_VCPU_EVENTS, events); 828 } 829 static inline void vcpu_events_set(struct kvm_vcpu *vcpu, 830 struct kvm_vcpu_events *events) 831 { 832 vcpu_ioctl(vcpu, KVM_SET_VCPU_EVENTS, events); 833 } 834 #endif 835 #ifdef __x86_64__ 836 static inline void vcpu_nested_state_get(struct kvm_vcpu *vcpu, 837 struct kvm_nested_state *state) 838 { 839 vcpu_ioctl(vcpu, KVM_GET_NESTED_STATE, state); 840 } 841 static inline int __vcpu_nested_state_set(struct kvm_vcpu *vcpu, 842 struct kvm_nested_state *state) 843 { 844 return __vcpu_ioctl(vcpu, KVM_SET_NESTED_STATE, state); 845 } 846 847 static inline void vcpu_nested_state_set(struct kvm_vcpu *vcpu, 848 struct kvm_nested_state *state) 849 { 850 vcpu_ioctl(vcpu, KVM_SET_NESTED_STATE, state); 851 } 852 #endif 853 static inline int vcpu_get_stats_fd(struct kvm_vcpu *vcpu) 854 { 855 int fd = __vcpu_ioctl(vcpu, KVM_GET_STATS_FD, NULL); 856 857 TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, KVM_CHECK_EXTENSION, fd, vcpu->vm); 858 return fd; 859 } 860 861 int __kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr); 862 863 static inline void kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr) 864 { 865 int ret = __kvm_has_device_attr(dev_fd, group, attr); 866 867 TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno); 868 } 869 870 int __kvm_device_attr_get(int dev_fd, uint32_t group, uint64_t attr, void *val); 871 872 static inline void kvm_device_attr_get(int dev_fd, uint32_t group, 873 uint64_t attr, void *val) 874 { 875 int ret = __kvm_device_attr_get(dev_fd, group, attr, val); 876 877 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_GET_DEVICE_ATTR, ret)); 878 } 879 880 int __kvm_device_attr_set(int dev_fd, uint32_t group, uint64_t attr, void *val); 881 882 static inline void kvm_device_attr_set(int dev_fd, uint32_t group, 883 uint64_t attr, void *val) 884 { 885 int ret = __kvm_device_attr_set(dev_fd, group, attr, val); 886 887 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); 888 } 889 890 static inline int __vcpu_has_device_attr(struct kvm_vcpu *vcpu, uint32_t group, 891 uint64_t attr) 892 { 893 return __kvm_has_device_attr(vcpu->fd, group, attr); 894 } 895 896 static inline void vcpu_has_device_attr(struct kvm_vcpu *vcpu, uint32_t group, 897 uint64_t attr) 898 { 899 kvm_has_device_attr(vcpu->fd, group, attr); 900 } 901 902 static inline int __vcpu_device_attr_get(struct kvm_vcpu *vcpu, uint32_t group, 903 uint64_t attr, void *val) 904 { 905 return __kvm_device_attr_get(vcpu->fd, group, attr, val); 906 } 907 908 static inline void vcpu_device_attr_get(struct kvm_vcpu *vcpu, uint32_t group, 909 uint64_t attr, void *val) 910 { 911 kvm_device_attr_get(vcpu->fd, group, attr, val); 912 } 913 914 static inline int __vcpu_device_attr_set(struct kvm_vcpu *vcpu, uint32_t group, 915 uint64_t attr, void *val) 916 { 917 return __kvm_device_attr_set(vcpu->fd, group, attr, val); 918 } 919 920 static inline void vcpu_device_attr_set(struct kvm_vcpu *vcpu, uint32_t group, 921 uint64_t attr, void *val) 922 { 923 kvm_device_attr_set(vcpu->fd, group, attr, val); 924 } 925 926 int __kvm_test_create_device(struct kvm_vm *vm, uint64_t type); 927 int __kvm_create_device(struct kvm_vm *vm, uint64_t type); 928 929 static inline int kvm_create_device(struct kvm_vm *vm, uint64_t type) 930 { 931 int fd = __kvm_create_device(vm, type); 932 933 TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_DEVICE, fd)); 934 return fd; 935 } 936 937 void *vcpu_map_dirty_ring(struct kvm_vcpu *vcpu); 938 939 /* 940 * VM VCPU Args Set 941 * 942 * Input Args: 943 * vm - Virtual Machine 944 * num - number of arguments 945 * ... - arguments, each of type uint64_t 946 * 947 * Output Args: None 948 * 949 * Return: None 950 * 951 * Sets the first @num input parameters for the function at @vcpu's entry point, 952 * per the C calling convention of the architecture, to the values given as 953 * variable args. Each of the variable args is expected to be of type uint64_t. 954 * The maximum @num can be is specific to the architecture. 955 */ 956 void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...); 957 958 void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level); 959 int _kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level); 960 961 #define KVM_MAX_IRQ_ROUTES 4096 962 963 struct kvm_irq_routing *kvm_gsi_routing_create(void); 964 void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing, 965 uint32_t gsi, uint32_t pin); 966 int _kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing); 967 void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing); 968 969 const char *exit_reason_str(unsigned int exit_reason); 970 971 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, 972 uint32_t memslot); 973 vm_paddr_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, 974 vm_paddr_t paddr_min, uint32_t memslot, 975 bool protected); 976 vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); 977 978 static inline vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, 979 vm_paddr_t paddr_min, uint32_t memslot) 980 { 981 /* 982 * By default, allocate memory as protected for VMs that support 983 * protected memory, as the majority of memory for such VMs is 984 * protected, i.e. using shared memory is effectively opt-in. 985 */ 986 return __vm_phy_pages_alloc(vm, num, paddr_min, memslot, 987 vm_arch_has_protected_memory(vm)); 988 } 989 990 /* 991 * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also 992 * loads the test binary into guest memory and creates an IRQ chip (x86 only). 993 * __vm_create() does NOT create vCPUs, @nr_runnable_vcpus is used purely to 994 * calculate the amount of memory needed for per-vCPU data, e.g. stacks. 995 */ 996 struct kvm_vm *____vm_create(struct vm_shape shape); 997 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, 998 uint64_t nr_extra_pages); 999 1000 static inline struct kvm_vm *vm_create_barebones(void) 1001 { 1002 return ____vm_create(VM_SHAPE_DEFAULT); 1003 } 1004 1005 static inline struct kvm_vm *vm_create_barebones_type(unsigned long type) 1006 { 1007 const struct vm_shape shape = { 1008 .mode = VM_MODE_DEFAULT, 1009 .type = type, 1010 }; 1011 1012 return ____vm_create(shape); 1013 } 1014 1015 static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) 1016 { 1017 return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0); 1018 } 1019 1020 struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus, 1021 uint64_t extra_mem_pages, 1022 void *guest_code, struct kvm_vcpu *vcpus[]); 1023 1024 static inline struct kvm_vm *vm_create_with_vcpus(uint32_t nr_vcpus, 1025 void *guest_code, 1026 struct kvm_vcpu *vcpus[]) 1027 { 1028 return __vm_create_with_vcpus(VM_SHAPE_DEFAULT, nr_vcpus, 0, 1029 guest_code, vcpus); 1030 } 1031 1032 1033 struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape, 1034 struct kvm_vcpu **vcpu, 1035 uint64_t extra_mem_pages, 1036 void *guest_code); 1037 1038 /* 1039 * Create a VM with a single vCPU with reasonable defaults and @extra_mem_pages 1040 * additional pages of guest memory. Returns the VM and vCPU (via out param). 1041 */ 1042 static inline struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, 1043 uint64_t extra_mem_pages, 1044 void *guest_code) 1045 { 1046 return __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, vcpu, 1047 extra_mem_pages, guest_code); 1048 } 1049 1050 static inline struct kvm_vm *vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, 1051 void *guest_code) 1052 { 1053 return __vm_create_with_one_vcpu(vcpu, 0, guest_code); 1054 } 1055 1056 static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shape, 1057 struct kvm_vcpu **vcpu, 1058 void *guest_code) 1059 { 1060 return __vm_create_shape_with_one_vcpu(shape, vcpu, 0, guest_code); 1061 } 1062 1063 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm); 1064 1065 void kvm_set_files_rlimit(uint32_t nr_vcpus); 1066 1067 int __pin_task_to_cpu(pthread_t task, int cpu); 1068 1069 static inline void pin_task_to_cpu(pthread_t task, int cpu) 1070 { 1071 int r; 1072 1073 r = __pin_task_to_cpu(task, cpu); 1074 TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu); 1075 } 1076 1077 static inline int pin_task_to_any_cpu(pthread_t task) 1078 { 1079 int cpu = sched_getcpu(); 1080 1081 pin_task_to_cpu(task, cpu); 1082 return cpu; 1083 } 1084 1085 static inline void pin_self_to_cpu(int cpu) 1086 { 1087 pin_task_to_cpu(pthread_self(), cpu); 1088 } 1089 1090 static inline int pin_self_to_any_cpu(void) 1091 { 1092 return pin_task_to_any_cpu(pthread_self()); 1093 } 1094 1095 void kvm_print_vcpu_pinning_help(void); 1096 void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[], 1097 int nr_vcpus); 1098 1099 unsigned long vm_compute_max_gfn(struct kvm_vm *vm); 1100 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size); 1101 unsigned int vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages); 1102 unsigned int vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages); 1103 static inline unsigned int 1104 vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages) 1105 { 1106 unsigned int n; 1107 n = vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages)); 1108 #ifdef __s390x__ 1109 /* s390 requires 1M aligned guest sizes */ 1110 n = (n + 255) & ~255; 1111 #endif 1112 return n; 1113 } 1114 1115 #define sync_global_to_guest(vm, g) ({ \ 1116 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ 1117 memcpy(_p, &(g), sizeof(g)); \ 1118 }) 1119 1120 #define sync_global_from_guest(vm, g) ({ \ 1121 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ 1122 memcpy(&(g), _p, sizeof(g)); \ 1123 }) 1124 1125 /* 1126 * Write a global value, but only in the VM's (guest's) domain. Primarily used 1127 * for "globals" that hold per-VM values (VMs always duplicate code and global 1128 * data into their own region of physical memory), but can be used anytime it's 1129 * undesirable to change the host's copy of the global. 1130 */ 1131 #define write_guest_global(vm, g, val) ({ \ 1132 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ 1133 typeof(g) _val = val; \ 1134 \ 1135 memcpy(_p, &(_val), sizeof(g)); \ 1136 }) 1137 1138 void assert_on_unhandled_exception(struct kvm_vcpu *vcpu); 1139 1140 void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, 1141 uint8_t indent); 1142 1143 static inline void vcpu_dump(FILE *stream, struct kvm_vcpu *vcpu, 1144 uint8_t indent) 1145 { 1146 vcpu_arch_dump(stream, vcpu, indent); 1147 } 1148 1149 /* 1150 * Adds a vCPU with reasonable defaults (e.g. a stack) 1151 * 1152 * Input Args: 1153 * vm - Virtual Machine 1154 * vcpu_id - The id of the VCPU to add to the VM. 1155 */ 1156 struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id); 1157 void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code); 1158 1159 static inline struct kvm_vcpu *vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, 1160 void *guest_code) 1161 { 1162 struct kvm_vcpu *vcpu = vm_arch_vcpu_add(vm, vcpu_id); 1163 1164 vcpu_arch_set_entry_point(vcpu, guest_code); 1165 1166 return vcpu; 1167 } 1168 1169 /* Re-create a vCPU after restarting a VM, e.g. for state save/restore tests. */ 1170 struct kvm_vcpu *vm_arch_vcpu_recreate(struct kvm_vm *vm, uint32_t vcpu_id); 1171 1172 static inline struct kvm_vcpu *vm_vcpu_recreate(struct kvm_vm *vm, 1173 uint32_t vcpu_id) 1174 { 1175 return vm_arch_vcpu_recreate(vm, vcpu_id); 1176 } 1177 1178 void vcpu_arch_free(struct kvm_vcpu *vcpu); 1179 1180 void virt_arch_pgd_alloc(struct kvm_vm *vm); 1181 1182 static inline void virt_pgd_alloc(struct kvm_vm *vm) 1183 { 1184 virt_arch_pgd_alloc(vm); 1185 } 1186 1187 /* 1188 * VM Virtual Page Map 1189 * 1190 * Input Args: 1191 * vm - Virtual Machine 1192 * vaddr - VM Virtual Address 1193 * paddr - VM Physical Address 1194 * memslot - Memory region slot for new virtual translation tables 1195 * 1196 * Output Args: None 1197 * 1198 * Return: None 1199 * 1200 * Within @vm, creates a virtual translation for the page starting 1201 * at @vaddr to the page starting at @paddr. 1202 */ 1203 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr); 1204 1205 static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) 1206 { 1207 virt_arch_pg_map(vm, vaddr, paddr); 1208 } 1209 1210 1211 /* 1212 * Address Guest Virtual to Guest Physical 1213 * 1214 * Input Args: 1215 * vm - Virtual Machine 1216 * gva - VM virtual address 1217 * 1218 * Output Args: None 1219 * 1220 * Return: 1221 * Equivalent VM physical address 1222 * 1223 * Returns the VM physical address of the translated VM virtual 1224 * address given by @gva. 1225 */ 1226 vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva); 1227 1228 static inline vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva) 1229 { 1230 return addr_arch_gva2gpa(vm, gva); 1231 } 1232 1233 /* 1234 * Virtual Translation Tables Dump 1235 * 1236 * Input Args: 1237 * stream - Output FILE stream 1238 * vm - Virtual Machine 1239 * indent - Left margin indent amount 1240 * 1241 * Output Args: None 1242 * 1243 * Return: None 1244 * 1245 * Dumps to the FILE stream given by @stream, the contents of all the 1246 * virtual translation tables for the VM given by @vm. 1247 */ 1248 void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent); 1249 1250 static inline void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) 1251 { 1252 virt_arch_dump(stream, vm, indent); 1253 } 1254 1255 1256 static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) 1257 { 1258 return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0); 1259 } 1260 1261 /* 1262 * Arch hook that is invoked via a constructor, i.e. before exeucting main(), 1263 * to allow for arch-specific setup that is common to all tests, e.g. computing 1264 * the default guest "mode". 1265 */ 1266 void kvm_selftest_arch_init(void); 1267 1268 void kvm_arch_vm_post_create(struct kvm_vm *vm, unsigned int nr_vcpus); 1269 void kvm_arch_vm_finalize_vcpus(struct kvm_vm *vm); 1270 void kvm_arch_vm_release(struct kvm_vm *vm); 1271 1272 bool vm_is_gpa_protected(struct kvm_vm *vm, vm_paddr_t paddr); 1273 1274 uint32_t guest_get_vcpuid(void); 1275 1276 #endif /* SELFTEST_KVM_UTIL_H */ 1277