This source file includes following definitions.
- align
- kvm_check_cap
- vm_enable_cap
- vm_open
- _vm_create
- vm_create
- kvm_vm_restart
- kvm_vm_get_dirty_log
- kvm_vm_clear_dirty_log
- userspace_mem_region_find
- kvm_userspace_memory_region_find
- vcpu_find
- vm_vcpu_rm
- kvm_vm_release
- kvm_vm_free
- kvm_memcmp_hva_gva
- vm_userspace_mem_region_add
- memslot2region
- vm_mem_region_set_flags
- vcpu_mmap_sz
- vm_vcpu_add
- vm_vaddr_unused_gap
- vm_vaddr_alloc
- virt_map
- addr_gpa2hva
- addr_hva2gpa
- vm_create_irqchip
- vcpu_state
- vcpu_run
- _vcpu_run
- vcpu_run_complete_io
- vcpu_set_mp_state
- vcpu_regs_get
- vcpu_regs_set
- vcpu_events_get
- vcpu_events_set
- vcpu_nested_state_get
- vcpu_nested_state_set
- vcpu_sregs_get
- vcpu_sregs_set
- _vcpu_sregs_set
- vcpu_ioctl
- _vcpu_ioctl
- vm_ioctl
- vm_dump
- exit_reason_str
- vm_phy_pages_alloc
- vm_phy_page_alloc
- addr_gva2hva
- vm_is_unrestricted_guest
- vm_get_page_size
- vm_get_page_shift
- vm_get_max_gfn
1
2
3
4
5
6
7
8 #include "test_util.h"
9 #include "kvm_util.h"
10 #include "kvm_util_internal.h"
11 #include "processor.h"
12
13 #include <assert.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <linux/kernel.h>
18
19 #define KVM_UTIL_PGS_PER_HUGEPG 512
20 #define KVM_UTIL_MIN_PFN 2
21
22
23 static void *align(void *x, size_t size)
24 {
25 size_t mask = size - 1;
26 TEST_ASSERT(size != 0 && !(size & (size - 1)),
27 "size not a power of 2: %lu", size);
28 return (void *) (((size_t) x + mask) & ~mask);
29 }
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 int kvm_check_cap(long cap)
48 {
49 int ret;
50 int kvm_fd;
51
52 kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
53 if (kvm_fd < 0)
54 exit(KSFT_SKIP);
55
56 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
57 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
58 " rc: %i errno: %i", ret, errno);
59
60 close(kvm_fd);
61
62 return ret;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
78 {
79 int ret;
80
81 ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
82 TEST_ASSERT(ret == 0, "KVM_ENABLE_CAP IOCTL failed,\n"
83 " rc: %i errno: %i", ret, errno);
84
85 return ret;
86 }
87
88 static void vm_open(struct kvm_vm *vm, int perm)
89 {
90 vm->kvm_fd = open(KVM_DEV_PATH, perm);
91 if (vm->kvm_fd < 0)
92 exit(KSFT_SKIP);
93
94 if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) {
95 fprintf(stderr, "immediate_exit not available, skipping test\n");
96 exit(KSFT_SKIP);
97 }
98
99 vm->fd = ioctl(vm->kvm_fd, KVM_CREATE_VM, vm->type);
100 TEST_ASSERT(vm->fd >= 0, "KVM_CREATE_VM ioctl failed, "
101 "rc: %i errno: %i", vm->fd, errno);
102 }
103
104 const char * const vm_guest_mode_string[] = {
105 "PA-bits:52, VA-bits:48, 4K pages",
106 "PA-bits:52, VA-bits:48, 64K pages",
107 "PA-bits:48, VA-bits:48, 4K pages",
108 "PA-bits:48, VA-bits:48, 64K pages",
109 "PA-bits:40, VA-bits:48, 4K pages",
110 "PA-bits:40, VA-bits:48, 64K pages",
111 "PA-bits:ANY, VA-bits:48, 4K pages",
112 };
113 _Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
114 "Missing new mode strings?");
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
136 {
137 struct kvm_vm *vm;
138
139 DEBUG("Testing guest mode: %s\n", vm_guest_mode_string(mode));
140
141 vm = calloc(1, sizeof(*vm));
142 TEST_ASSERT(vm != NULL, "Insufficient Memory");
143
144 vm->mode = mode;
145 vm->type = 0;
146
147
148 switch (vm->mode) {
149 case VM_MODE_P52V48_4K:
150 vm->pgtable_levels = 4;
151 vm->pa_bits = 52;
152 vm->va_bits = 48;
153 vm->page_size = 0x1000;
154 vm->page_shift = 12;
155 break;
156 case VM_MODE_P52V48_64K:
157 vm->pgtable_levels = 3;
158 vm->pa_bits = 52;
159 vm->va_bits = 48;
160 vm->page_size = 0x10000;
161 vm->page_shift = 16;
162 break;
163 case VM_MODE_P48V48_4K:
164 vm->pgtable_levels = 4;
165 vm->pa_bits = 48;
166 vm->va_bits = 48;
167 vm->page_size = 0x1000;
168 vm->page_shift = 12;
169 break;
170 case VM_MODE_P48V48_64K:
171 vm->pgtable_levels = 3;
172 vm->pa_bits = 48;
173 vm->va_bits = 48;
174 vm->page_size = 0x10000;
175 vm->page_shift = 16;
176 break;
177 case VM_MODE_P40V48_4K:
178 vm->pgtable_levels = 4;
179 vm->pa_bits = 40;
180 vm->va_bits = 48;
181 vm->page_size = 0x1000;
182 vm->page_shift = 12;
183 break;
184 case VM_MODE_P40V48_64K:
185 vm->pgtable_levels = 3;
186 vm->pa_bits = 40;
187 vm->va_bits = 48;
188 vm->page_size = 0x10000;
189 vm->page_shift = 16;
190 break;
191 case VM_MODE_PXXV48_4K:
192 #ifdef __x86_64__
193 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
194 TEST_ASSERT(vm->va_bits == 48, "Linear address width "
195 "(%d bits) not supported", vm->va_bits);
196 vm->pgtable_levels = 4;
197 vm->page_size = 0x1000;
198 vm->page_shift = 12;
199 DEBUG("Guest physical address width detected: %d\n",
200 vm->pa_bits);
201 #else
202 TEST_ASSERT(false, "VM_MODE_PXXV48_4K not supported on "
203 "non-x86 platforms");
204 #endif
205 break;
206 default:
207 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
208 }
209
210 #ifdef __aarch64__
211 if (vm->pa_bits != 40)
212 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
213 #endif
214
215 vm_open(vm, perm);
216
217
218 vm->vpages_valid = sparsebit_alloc();
219 sparsebit_set_num(vm->vpages_valid,
220 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
221 sparsebit_set_num(vm->vpages_valid,
222 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
223 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
224
225
226 vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
227
228
229 vm->vpages_mapped = sparsebit_alloc();
230 if (phy_pages != 0)
231 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
232 0, 0, phy_pages, 0);
233
234 return vm;
235 }
236
237 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
238 {
239 return _vm_create(mode, phy_pages, perm);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
256 {
257 struct userspace_mem_region *region;
258
259 vm_open(vmp, perm);
260 if (vmp->has_irqchip)
261 vm_create_irqchip(vmp);
262
263 for (region = vmp->userspace_mem_region_head; region;
264 region = region->next) {
265 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
266 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
267 " rc: %i errno: %i\n"
268 " slot: %u flags: 0x%x\n"
269 " guest_phys_addr: 0x%lx size: 0x%lx",
270 ret, errno, region->region.slot,
271 region->region.flags,
272 region->region.guest_phys_addr,
273 region->region.memory_size);
274 }
275 }
276
277 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
278 {
279 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
280 int ret;
281
282 ret = ioctl(vm->fd, KVM_GET_DIRTY_LOG, &args);
283 TEST_ASSERT(ret == 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
284 strerror(-ret));
285 }
286
287 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
288 uint64_t first_page, uint32_t num_pages)
289 {
290 struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
291 .first_page = first_page,
292 .num_pages = num_pages };
293 int ret;
294
295 ret = ioctl(vm->fd, KVM_CLEAR_DIRTY_LOG, &args);
296 TEST_ASSERT(ret == 0, "%s: KVM_CLEAR_DIRTY_LOG failed: %s",
297 strerror(-ret));
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319 static struct userspace_mem_region *
320 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
321 {
322 struct userspace_mem_region *region;
323
324 for (region = vm->userspace_mem_region_head; region;
325 region = region->next) {
326 uint64_t existing_start = region->region.guest_phys_addr;
327 uint64_t existing_end = region->region.guest_phys_addr
328 + region->region.memory_size - 1;
329 if (start <= existing_end && end >= existing_start)
330 return region;
331 }
332
333 return NULL;
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 struct kvm_userspace_memory_region *
353 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
354 uint64_t end)
355 {
356 struct userspace_mem_region *region;
357
358 region = userspace_mem_region_find(vm, start, end);
359 if (!region)
360 return NULL;
361
362 return ®ion->region;
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
382 {
383 struct vcpu *vcpup;
384
385 for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
386 if (vcpup->id == vcpuid)
387 return vcpup;
388 }
389
390 return NULL;
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
407 {
408 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
409 int ret;
410
411 ret = munmap(vcpu->state, sizeof(*vcpu->state));
412 TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
413 "errno: %i", ret, errno);
414 close(vcpu->fd);
415 TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
416 "errno: %i", ret, errno);
417
418 if (vcpu->next)
419 vcpu->next->prev = vcpu->prev;
420 if (vcpu->prev)
421 vcpu->prev->next = vcpu->next;
422 else
423 vm->vcpu_head = vcpu->next;
424 free(vcpu);
425 }
426
427 void kvm_vm_release(struct kvm_vm *vmp)
428 {
429 int ret;
430
431 while (vmp->vcpu_head)
432 vm_vcpu_rm(vmp, vmp->vcpu_head->id);
433
434 ret = close(vmp->fd);
435 TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
436 " vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
437
438 close(vmp->kvm_fd);
439 TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
440 " vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
441 }
442
443
444
445
446 void kvm_vm_free(struct kvm_vm *vmp)
447 {
448 int ret;
449
450 if (vmp == NULL)
451 return;
452
453
454 while (vmp->userspace_mem_region_head) {
455 struct userspace_mem_region *region
456 = vmp->userspace_mem_region_head;
457
458 region->region.memory_size = 0;
459 ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
460 ®ion->region);
461 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
462 "rc: %i errno: %i", ret, errno);
463
464 vmp->userspace_mem_region_head = region->next;
465 sparsebit_free(®ion->unused_phy_pages);
466 ret = munmap(region->mmap_start, region->mmap_size);
467 TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
468 ret, errno);
469
470 free(region);
471 }
472
473
474 sparsebit_free(&vmp->vpages_valid);
475 sparsebit_free(&vmp->vpages_mapped);
476
477 kvm_vm_release(vmp);
478
479
480 free(vmp);
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
507 {
508 size_t amt;
509
510
511
512
513
514 for (uintptr_t offset = 0; offset < len; offset += amt) {
515 uintptr_t ptr1 = (uintptr_t)hva + offset;
516
517
518
519
520
521 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
522
523
524
525
526
527 amt = len - offset;
528 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
529 amt = vm->page_size - (ptr1 % vm->page_size);
530 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
531 amt = vm->page_size - (ptr2 % vm->page_size);
532
533 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
534 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
535
536
537
538
539
540
541 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
542 if (ret != 0)
543 return ret;
544 }
545
546
547
548
549
550 return 0;
551 }
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 void vm_userspace_mem_region_add(struct kvm_vm *vm,
576 enum vm_mem_backing_src_type src_type,
577 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
578 uint32_t flags)
579 {
580 int ret;
581 struct userspace_mem_region *region;
582 size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
583 size_t alignment;
584
585 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
586 "address not on a page boundary.\n"
587 " guest_paddr: 0x%lx vm->page_size: 0x%x",
588 guest_paddr, vm->page_size);
589 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
590 <= vm->max_gfn, "Physical range beyond maximum "
591 "supported physical address,\n"
592 " guest_paddr: 0x%lx npages: 0x%lx\n"
593 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
594 guest_paddr, npages, vm->max_gfn, vm->page_size);
595
596
597
598
599
600 region = (struct userspace_mem_region *) userspace_mem_region_find(
601 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
602 if (region != NULL)
603 TEST_ASSERT(false, "overlapping userspace_mem_region already "
604 "exists\n"
605 " requested guest_paddr: 0x%lx npages: 0x%lx "
606 "page_size: 0x%x\n"
607 " existing guest_paddr: 0x%lx size: 0x%lx",
608 guest_paddr, npages, vm->page_size,
609 (uint64_t) region->region.guest_phys_addr,
610 (uint64_t) region->region.memory_size);
611
612
613 for (region = vm->userspace_mem_region_head; region;
614 region = region->next) {
615 if (region->region.slot == slot)
616 break;
617 }
618 if (region != NULL)
619 TEST_ASSERT(false, "A mem region with the requested slot "
620 "already exists.\n"
621 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
622 " existing slot: %u paddr: 0x%lx size: 0x%lx",
623 slot, guest_paddr, npages,
624 region->region.slot,
625 (uint64_t) region->region.guest_phys_addr,
626 (uint64_t) region->region.memory_size);
627
628
629 region = calloc(1, sizeof(*region));
630 TEST_ASSERT(region != NULL, "Insufficient Memory");
631 region->mmap_size = npages * vm->page_size;
632
633 #ifdef __s390x__
634
635 alignment = 0x100000;
636 #else
637 alignment = 1;
638 #endif
639
640 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
641 alignment = max(huge_page_size, alignment);
642
643
644 if (alignment > 1)
645 region->mmap_size += alignment;
646
647 region->mmap_start = mmap(NULL, region->mmap_size,
648 PROT_READ | PROT_WRITE,
649 MAP_PRIVATE | MAP_ANONYMOUS
650 | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
651 -1, 0);
652 TEST_ASSERT(region->mmap_start != MAP_FAILED,
653 "test_malloc failed, mmap_start: %p errno: %i",
654 region->mmap_start, errno);
655
656
657 region->host_mem = align(region->mmap_start, alignment);
658
659
660 if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
661 ret = madvise(region->host_mem, npages * vm->page_size,
662 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
663 TEST_ASSERT(ret == 0, "madvise failed,\n"
664 " addr: %p\n"
665 " length: 0x%lx\n"
666 " src_type: %x",
667 region->host_mem, npages * vm->page_size, src_type);
668 }
669
670 region->unused_phy_pages = sparsebit_alloc();
671 sparsebit_set_num(region->unused_phy_pages,
672 guest_paddr >> vm->page_shift, npages);
673 region->region.slot = slot;
674 region->region.flags = flags;
675 region->region.guest_phys_addr = guest_paddr;
676 region->region.memory_size = npages * vm->page_size;
677 region->region.userspace_addr = (uintptr_t) region->host_mem;
678 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
679 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
680 " rc: %i errno: %i\n"
681 " slot: %u flags: 0x%x\n"
682 " guest_phys_addr: 0x%lx size: 0x%lx",
683 ret, errno, slot, flags,
684 guest_paddr, (uint64_t) region->region.memory_size);
685
686
687 if (vm->userspace_mem_region_head)
688 vm->userspace_mem_region_head->prev = region;
689 region->next = vm->userspace_mem_region_head;
690 vm->userspace_mem_region_head = region;
691 }
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708 struct userspace_mem_region *
709 memslot2region(struct kvm_vm *vm, uint32_t memslot)
710 {
711 struct userspace_mem_region *region;
712
713 for (region = vm->userspace_mem_region_head; region;
714 region = region->next) {
715 if (region->region.slot == memslot)
716 break;
717 }
718 if (region == NULL) {
719 fprintf(stderr, "No mem region with the requested slot found,\n"
720 " requested slot: %u\n", memslot);
721 fputs("---- vm dump ----\n", stderr);
722 vm_dump(stderr, vm, 2);
723 TEST_ASSERT(false, "Mem region not found");
724 }
725
726 return region;
727 }
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
744 {
745 int ret;
746 struct userspace_mem_region *region;
747
748 region = memslot2region(vm, slot);
749
750 region->region.flags = flags;
751
752 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
753
754 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
755 " rc: %i errno: %i slot: %u flags: 0x%x",
756 ret, errno, slot, flags);
757 }
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772 static int vcpu_mmap_sz(void)
773 {
774 int dev_fd, ret;
775
776 dev_fd = open(KVM_DEV_PATH, O_RDONLY);
777 if (dev_fd < 0)
778 exit(KSFT_SKIP);
779
780 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
781 TEST_ASSERT(ret >= sizeof(struct kvm_run),
782 "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
783 __func__, ret, errno);
784
785 close(dev_fd);
786
787 return ret;
788 }
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
805 {
806 struct vcpu *vcpu;
807
808
809 vcpu = vcpu_find(vm, vcpuid);
810 if (vcpu != NULL)
811 TEST_ASSERT(false, "vcpu with the specified id "
812 "already exists,\n"
813 " requested vcpuid: %u\n"
814 " existing vcpuid: %u state: %p",
815 vcpuid, vcpu->id, vcpu->state);
816
817
818 vcpu = calloc(1, sizeof(*vcpu));
819 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
820 vcpu->id = vcpuid;
821 vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
822 TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
823 vcpu->fd, errno);
824
825 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
826 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
827 vcpu_mmap_sz(), sizeof(*vcpu->state));
828 vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
829 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
830 TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
831 "vcpu id: %u errno: %i", vcpuid, errno);
832
833
834 if (vm->vcpu_head)
835 vm->vcpu_head->prev = vcpu;
836 vcpu->next = vm->vcpu_head;
837 vm->vcpu_head = vcpu;
838 }
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
861 vm_vaddr_t vaddr_min)
862 {
863 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
864
865
866 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
867 if ((pgidx_start * vm->page_size) < vaddr_min)
868 goto no_va_found;
869
870
871 if (!sparsebit_is_set_num(vm->vpages_valid,
872 pgidx_start, pages))
873 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
874 pgidx_start, pages);
875 do {
876
877
878
879
880
881
882 if (sparsebit_is_clear_num(vm->vpages_mapped,
883 pgidx_start, pages))
884 goto va_found;
885 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
886 pgidx_start, pages);
887 if (pgidx_start == 0)
888 goto no_va_found;
889
890
891
892
893
894 if (!sparsebit_is_set_num(vm->vpages_valid,
895 pgidx_start, pages)) {
896 pgidx_start = sparsebit_next_set_num(
897 vm->vpages_valid, pgidx_start, pages);
898 if (pgidx_start == 0)
899 goto no_va_found;
900 }
901 } while (pgidx_start != 0);
902
903 no_va_found:
904 TEST_ASSERT(false, "No vaddr of specified pages available, "
905 "pages: 0x%lx", pages);
906
907
908 return -1;
909
910 va_found:
911 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
912 pgidx_start, pages),
913 "Unexpected, invalid virtual page index range,\n"
914 " pgidx_start: 0x%lx\n"
915 " pages: 0x%lx",
916 pgidx_start, pages);
917 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
918 pgidx_start, pages),
919 "Unexpected, pages already mapped,\n"
920 " pgidx_start: 0x%lx\n"
921 " pages: 0x%lx",
922 pgidx_start, pages);
923
924 return pgidx_start * vm->page_size;
925 }
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
949 uint32_t data_memslot, uint32_t pgd_memslot)
950 {
951 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
952
953 virt_pgd_alloc(vm, pgd_memslot);
954
955
956
957
958
959 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
960
961
962 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
963 pages--, vaddr += vm->page_size) {
964 vm_paddr_t paddr;
965
966 paddr = vm_phy_page_alloc(vm,
967 KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
968
969 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
970
971 sparsebit_set(vm->vpages_mapped,
972 vaddr >> vm->page_shift);
973 }
974
975 return vaddr_start;
976 }
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
996 size_t size, uint32_t pgd_memslot)
997 {
998 size_t page_size = vm->page_size;
999 size_t npages = size / page_size;
1000
1001 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1002 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1003
1004 while (npages--) {
1005 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1006 vaddr += page_size;
1007 paddr += page_size;
1008 }
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1029 {
1030 struct userspace_mem_region *region;
1031 for (region = vm->userspace_mem_region_head; region;
1032 region = region->next) {
1033 if ((gpa >= region->region.guest_phys_addr)
1034 && (gpa <= (region->region.guest_phys_addr
1035 + region->region.memory_size - 1)))
1036 return (void *) ((uintptr_t) region->host_mem
1037 + (gpa - region->region.guest_phys_addr));
1038 }
1039
1040 TEST_ASSERT(false, "No vm physical memory at 0x%lx", gpa);
1041 return NULL;
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1062 {
1063 struct userspace_mem_region *region;
1064 for (region = vm->userspace_mem_region_head; region;
1065 region = region->next) {
1066 if ((hva >= region->host_mem)
1067 && (hva <= (region->host_mem
1068 + region->region.memory_size - 1)))
1069 return (vm_paddr_t) ((uintptr_t)
1070 region->region.guest_phys_addr
1071 + (hva - (uintptr_t) region->host_mem));
1072 }
1073
1074 TEST_ASSERT(false, "No mapping to a guest physical address, "
1075 "hva: %p", hva);
1076 return -1;
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091 void vm_create_irqchip(struct kvm_vm *vm)
1092 {
1093 int ret;
1094
1095 ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
1096 TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1097 "rc: %i errno: %i", ret, errno);
1098
1099 vm->has_irqchip = true;
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1118 {
1119 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1120 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1121
1122 return vcpu->state;
1123 }
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1140 {
1141 int ret = _vcpu_run(vm, vcpuid);
1142 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1143 "rc: %i errno: %i", ret, errno);
1144 }
1145
1146 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1147 {
1148 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1149 int rc;
1150
1151 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1152 do {
1153 rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1154 } while (rc == -1 && errno == EINTR);
1155 return rc;
1156 }
1157
1158 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1159 {
1160 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1161 int ret;
1162
1163 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1164
1165 vcpu->state->immediate_exit = 1;
1166 ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1167 vcpu->state->immediate_exit = 0;
1168
1169 TEST_ASSERT(ret == -1 && errno == EINTR,
1170 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1171 ret, errno);
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1190 struct kvm_mp_state *mp_state)
1191 {
1192 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1193 int ret;
1194
1195 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1196
1197 ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
1198 TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
1199 "rc: %i errno: %i", ret, errno);
1200 }
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1218 {
1219 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1220 int ret;
1221
1222 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1223
1224 ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1225 TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1226 ret, errno);
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1245 {
1246 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1247 int ret;
1248
1249 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1250
1251 ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1252 TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1253 ret, errno);
1254 }
1255
1256 #ifdef __KVM_HAVE_VCPU_EVENTS
1257 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1258 struct kvm_vcpu_events *events)
1259 {
1260 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1261 int ret;
1262
1263 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1264
1265 ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1266 TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1267 ret, errno);
1268 }
1269
1270 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1271 struct kvm_vcpu_events *events)
1272 {
1273 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1274 int ret;
1275
1276 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1277
1278 ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
1279 TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1280 ret, errno);
1281 }
1282 #endif
1283
1284 #ifdef __x86_64__
1285 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1286 struct kvm_nested_state *state)
1287 {
1288 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1289 int ret;
1290
1291 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1292
1293 ret = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, state);
1294 TEST_ASSERT(ret == 0,
1295 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1296 ret, errno);
1297 }
1298
1299 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1300 struct kvm_nested_state *state, bool ignore_error)
1301 {
1302 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1303 int ret;
1304
1305 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1306
1307 ret = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, state);
1308 if (!ignore_error) {
1309 TEST_ASSERT(ret == 0,
1310 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1311 ret, errno);
1312 }
1313
1314 return ret;
1315 }
1316 #endif
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1334 {
1335 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1336 int ret;
1337
1338 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1339
1340 ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1341 TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1342 ret, errno);
1343 }
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1361 {
1362 int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
1363 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1364 "rc: %i errno: %i", ret, errno);
1365 }
1366
1367 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1368 {
1369 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1370
1371 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1372
1373 return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1374 }
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1390 unsigned long cmd, void *arg)
1391 {
1392 int ret;
1393
1394 ret = _vcpu_ioctl(vm, vcpuid, cmd, arg);
1395 TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1396 cmd, ret, errno, strerror(errno));
1397 }
1398
1399 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1400 unsigned long cmd, void *arg)
1401 {
1402 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1403 int ret;
1404
1405 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1406
1407 ret = ioctl(vcpu->fd, cmd, arg);
1408
1409 return ret;
1410 }
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1425 {
1426 int ret;
1427
1428 ret = ioctl(vm->fd, cmd, arg);
1429 TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1430 cmd, ret, errno, strerror(errno));
1431 }
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1449 {
1450 struct userspace_mem_region *region;
1451 struct vcpu *vcpu;
1452
1453 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1454 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1455 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1456 fprintf(stream, "%*sMem Regions:\n", indent, "");
1457 for (region = vm->userspace_mem_region_head; region;
1458 region = region->next) {
1459 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1460 "host_virt: %p\n", indent + 2, "",
1461 (uint64_t) region->region.guest_phys_addr,
1462 (uint64_t) region->region.memory_size,
1463 region->host_mem);
1464 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1465 sparsebit_dump(stream, region->unused_phy_pages, 0);
1466 }
1467 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1468 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1469 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1470 vm->pgd_created);
1471 if (vm->pgd_created) {
1472 fprintf(stream, "%*sVirtual Translation Tables:\n",
1473 indent + 2, "");
1474 virt_dump(stream, vm, indent + 4);
1475 }
1476 fprintf(stream, "%*sVCPUs:\n", indent, "");
1477 for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
1478 vcpu_dump(stream, vm, vcpu->id, indent + 2);
1479 }
1480
1481
1482 static struct exit_reason {
1483 unsigned int reason;
1484 const char *name;
1485 } exit_reasons_known[] = {
1486 {KVM_EXIT_UNKNOWN, "UNKNOWN"},
1487 {KVM_EXIT_EXCEPTION, "EXCEPTION"},
1488 {KVM_EXIT_IO, "IO"},
1489 {KVM_EXIT_HYPERCALL, "HYPERCALL"},
1490 {KVM_EXIT_DEBUG, "DEBUG"},
1491 {KVM_EXIT_HLT, "HLT"},
1492 {KVM_EXIT_MMIO, "MMIO"},
1493 {KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
1494 {KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
1495 {KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
1496 {KVM_EXIT_INTR, "INTR"},
1497 {KVM_EXIT_SET_TPR, "SET_TPR"},
1498 {KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
1499 {KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
1500 {KVM_EXIT_S390_RESET, "S390_RESET"},
1501 {KVM_EXIT_DCR, "DCR"},
1502 {KVM_EXIT_NMI, "NMI"},
1503 {KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
1504 {KVM_EXIT_OSI, "OSI"},
1505 {KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
1506 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1507 {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
1508 #endif
1509 };
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526 const char *exit_reason_str(unsigned int exit_reason)
1527 {
1528 unsigned int n1;
1529
1530 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1531 if (exit_reason == exit_reasons_known[n1].reason)
1532 return exit_reasons_known[n1].name;
1533 }
1534
1535 return "Unknown";
1536 }
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1558 vm_paddr_t paddr_min, uint32_t memslot)
1559 {
1560 struct userspace_mem_region *region;
1561 sparsebit_idx_t pg, base;
1562
1563 TEST_ASSERT(num > 0, "Must allocate at least one page");
1564
1565 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1566 "not divisible by page size.\n"
1567 " paddr_min: 0x%lx page_size: 0x%x",
1568 paddr_min, vm->page_size);
1569
1570 region = memslot2region(vm, memslot);
1571 base = pg = paddr_min >> vm->page_shift;
1572
1573 do {
1574 for (; pg < base + num; ++pg) {
1575 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
1576 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
1577 break;
1578 }
1579 }
1580 } while (pg && pg != base + num);
1581
1582 if (pg == 0) {
1583 fprintf(stderr, "No guest physical page available, "
1584 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1585 paddr_min, vm->page_size, memslot);
1586 fputs("---- vm dump ----\n", stderr);
1587 vm_dump(stderr, vm, 2);
1588 abort();
1589 }
1590
1591 for (pg = base; pg < base + num; ++pg)
1592 sparsebit_clear(region->unused_phy_pages, pg);
1593
1594 return base * vm->page_size;
1595 }
1596
1597 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1598 uint32_t memslot)
1599 {
1600 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1601 }
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1616 {
1617 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1618 }
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632 bool vm_is_unrestricted_guest(struct kvm_vm *vm)
1633 {
1634 char val = 'N';
1635 size_t count;
1636 FILE *f;
1637
1638 if (vm == NULL) {
1639
1640 f = fopen(KVM_DEV_PATH, "r");
1641 TEST_ASSERT(f != NULL, "Error in opening KVM dev file: %d",
1642 errno);
1643 fclose(f);
1644 }
1645
1646 f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1647 if (f) {
1648 count = fread(&val, sizeof(char), 1, f);
1649 TEST_ASSERT(count == 1, "Unable to read from param file.");
1650 fclose(f);
1651 }
1652
1653 return val == 'Y';
1654 }
1655
1656 unsigned int vm_get_page_size(struct kvm_vm *vm)
1657 {
1658 return vm->page_size;
1659 }
1660
1661 unsigned int vm_get_page_shift(struct kvm_vm *vm)
1662 {
1663 return vm->page_shift;
1664 }
1665
1666 unsigned int vm_get_max_gfn(struct kvm_vm *vm)
1667 {
1668 return vm->max_gfn;
1669 }