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