forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hvtest_x86.m
1640 lines (1279 loc) · 55.5 KB
/
hvtest_x86.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <darwintest.h>
#include <pthread.h>
#include <stdatomic.h>
#include <mach/mach.h>
#include <mach/vm_map.h>
#include <mach/vm_page_size.h>
#include <sys/sysctl.h>
#include "hvtest_x86_guest.h"
#include <Foundation/Foundation.h>
#include <Hypervisor/hv.h>
#include <Hypervisor/hv_vmx.h>
T_GLOBAL_META(
T_META_NAMESPACE("xnu.intel.hv"),
T_META_RUN_CONCURRENTLY(true),
T_META_REQUIRES_SYSCTL_NE("hw.optional.arm64", 1) // Don't run translated.
);
static bool
hv_support()
{
int hv_support;
size_t hv_support_size = sizeof(hv_support);
int err = sysctlbyname("kern.hv_support", &hv_support, &hv_support_size, NULL, 0);
if (err) {
return false;
} else {
return hv_support != 0;
}
}
static uint64_t get_reg(hv_vcpuid_t vcpu, hv_x86_reg_t reg)
{
uint64_t val;
T_QUIET; T_EXPECT_EQ(hv_vcpu_read_register(vcpu, reg, &val), HV_SUCCESS,
"get register");
return val;
}
static void set_reg(hv_vcpuid_t vcpu, hv_x86_reg_t reg, uint64_t value)
{
T_QUIET; T_EXPECT_EQ(hv_vcpu_write_register(vcpu, reg, value), HV_SUCCESS,
"set register");
}
static uint64_t get_vmcs(hv_vcpuid_t vcpu, uint32_t field)
{
uint64_t val;
T_QUIET; T_EXPECT_EQ(hv_vmx_vcpu_read_vmcs(vcpu, field, &val), HV_SUCCESS,
"get vmcs");
return val;
}
static void set_vmcs(hv_vcpuid_t vcpu, uint32_t field, uint64_t value)
{
T_QUIET; T_EXPECT_EQ(hv_vmx_vcpu_write_vmcs(vcpu, field, value), HV_SUCCESS,
"set vmcs");
}
static uint64_t get_cap(uint32_t field)
{
uint64_t val;
T_QUIET; T_ASSERT_EQ(hv_vmx_read_capability(field, &val), HV_SUCCESS,
"get capability");
return val;
}
static NSMutableDictionary *page_cache;
static NSMutableSet *allocated_phys_pages;
static pthread_mutex_t page_cache_lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t next_phys = 0x4000000;
/*
* Map a page into guest's physical address space, return gpa of the
* page. If *host_uva is NULL, a new host user page is allocated.
*/
static hv_gpaddr_t
map_guest_phys(void **host_uva)
{
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&page_cache_lock),
"acquire page lock");
hv_gpaddr_t gpa = next_phys;
next_phys += vm_page_size;
if (*host_uva == NULL) {
*host_uva = valloc(vm_page_size);
memset(*host_uva, 0, vm_page_size);
[allocated_phys_pages addObject:@((uintptr_t)*host_uva)];
}
T_QUIET; T_ASSERT_EQ(hv_vm_map(*host_uva, gpa, vm_page_size, HV_MEMORY_READ), HV_SUCCESS, "enter hv mapping");
[page_cache setObject:@((uintptr_t)*host_uva) forKey:@(gpa)];
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&page_cache_lock),
"release page lock");
return gpa;
}
static uint64_t *pml4;
static hv_gpaddr_t pml4_gpa;
/* Stolen from kern/bits.h, which cannot be included outside the kernel. */
#define BIT(b) (1ULL << (b))
#define mask(width) (width >= 64 ? (unsigned long long)-1 : (BIT(width) - 1))
#define extract(x, shift, width) ((((uint64_t)(x)) >> (shift)) & mask(width))
#define bits(x, hi, lo) extract((x), (lo), (hi) - (lo) + 1)
/*
* Enter a page in a level of long mode's PML4 paging structures.
* Helper for fault_in_page.
*/
static void *
enter_level(uint64_t *table, void *host_va, void *va, int hi, int lo) {
uint64_t * const te = &table[bits(va, hi, lo)];
const uint64_t present = 1;
const uint64_t rw = 2;
const uint64_t addr_mask = mask(47-12) << 12;
if (!(*te & present)) {
hv_gpaddr_t gpa = map_guest_phys(&host_va);
*te = (gpa & addr_mask) | rw | present;
} else {
NSNumber *num = [page_cache objectForKey:@(*te & addr_mask)];
T_QUIET; T_ASSERT_NOTNULL(num, "existing page is backed");
void *backing = (void*)[num unsignedLongValue];
if (host_va != 0) {
T_QUIET; T_ASSERT_EQ(va, backing, "backing page matches");
} else {
host_va = backing;
}
}
return host_va;
}
/*
* Enters a page both into the guest paging structures and the EPT
* (long mode PML4 only, real mode and protected mode support running
* without paging, and that's what they use instead.)
*/
static void *
map_page(void *host_va, void *va) {
uint64_t *pdpt = enter_level(pml4, NULL, va, 47, 39);
uint64_t *pd = enter_level(pdpt, NULL, va, 38, 30);
uint64_t *pt = enter_level(pd, NULL, va, 29, 21);
return enter_level(pt, host_va, va, 20, 12);
}
static void
fault_in_page(void *va) {
map_page(va, va);
}
static void free_page_cache(void)
{
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&page_cache_lock),
"acquire page lock");
for (NSNumber *uvaNumber in allocated_phys_pages) {
uintptr_t va = [uvaNumber unsignedLongValue];
free((void *)va);
}
[page_cache release];
[allocated_phys_pages release];
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&page_cache_lock),
"release page lock");
}
static uint64_t
run_to_next_vm_fault(hv_vcpuid_t vcpu, bool on_demand_paging)
{
bool retry;
uint64_t exit_reason, qual, gpa, gla, info, vector_info, error_code;
do {
retry = false;
do {
T_QUIET; T_ASSERT_EQ(hv_vcpu_run_until(vcpu, ~(uint64_t)0), HV_SUCCESS, "run VCPU");
exit_reason = get_vmcs(vcpu, VMCS_RO_EXIT_REASON);
} while (exit_reason == VMX_REASON_IRQ);
qual = get_vmcs(vcpu, VMCS_RO_EXIT_QUALIFIC);
gpa = get_vmcs(vcpu, VMCS_GUEST_PHYSICAL_ADDRESS);
gla = get_vmcs(vcpu, VMCS_RO_GUEST_LIN_ADDR);
info = get_vmcs(vcpu, VMCS_RO_VMEXIT_IRQ_INFO);
vector_info = get_vmcs(vcpu, VMCS_RO_IDT_VECTOR_INFO);
error_code = get_vmcs(vcpu, VMCS_RO_VMEXIT_IRQ_ERROR);
if (on_demand_paging) {
if (exit_reason == VMX_REASON_EXC_NMI &&
(info & 0x800003ff) == 0x8000030e &&
(error_code & 0x1) == 0) {
// guest paging fault
fault_in_page((void*)qual);
retry = true;
}
else if (exit_reason == VMX_REASON_EPT_VIOLATION) {
if ((qual & 0x86) == 0x82) {
// EPT write fault
T_QUIET; T_ASSERT_EQ(hv_vm_protect(gpa & ~(hv_gpaddr_t)PAGE_MASK, vm_page_size,
HV_MEMORY_READ | HV_MEMORY_WRITE),
HV_SUCCESS, "make page writable");
retry = true;
}
else if ((qual & 0x86) == 0x84) {
// EPT exec fault
T_QUIET; T_ASSERT_EQ(hv_vm_protect(gpa & ~(hv_gpaddr_t)PAGE_MASK, vm_page_size,
HV_MEMORY_READ | HV_MEMORY_EXEC),
HV_SUCCESS, "make page executable");
retry = true;
}
}
}
} while (retry);
// printf("reason: %lld, qualification: %llx\n", exit_reason, qual);
// printf("gpa: %llx, gla: %llx\n", gpa, gla);
// printf("RIP: %llx\n", get_reg(vcpu, HV_X86_RIP));
// printf("CR3: %llx\n", get_reg(vcpu, HV_X86_CR3));
// printf("info: %llx\n", info);
// printf("vector_info: %llx\n", vector_info);
// printf("error_code: %llx\n", error_code);
return exit_reason;
}
static uint64_t
expect_vmcall(hv_vcpuid_t vcpu, bool on_demand_paging)
{
uint64_t reason = run_to_next_vm_fault(vcpu, on_demand_paging);
T_ASSERT_EQ(reason, (uint64_t)VMX_REASON_VMCALL, "expect vmcall exit");
// advance RIP to after VMCALL
set_vmcs(vcpu, VMCS_GUEST_RIP, get_reg(vcpu, HV_X86_RIP)+get_vmcs(vcpu, VMCS_RO_VMEXIT_INSTR_LEN));
return get_reg(vcpu, HV_X86_RAX);
}
static uint64_t
expect_vmcall_with_value(hv_vcpuid_t vcpu, uint64_t rax, bool on_demand_paging)
{
uint64_t reason = run_to_next_vm_fault(vcpu, on_demand_paging);
T_QUIET; T_ASSERT_EQ(reason, (uint64_t)VMX_REASON_VMCALL, "check for vmcall exit");
T_ASSERT_EQ(get_reg(vcpu, HV_X86_RAX), rax, "vmcall exit with expected RAX value %llx", rax);
// advance RIP to after VMCALL
set_vmcs(vcpu, VMCS_GUEST_RIP, get_reg(vcpu, HV_X86_RIP)+get_vmcs(vcpu, VMCS_RO_VMEXIT_INSTR_LEN));
return reason;
}
typedef void (*vcpu_entry_function)(uint64_t);
typedef void *(*vcpu_monitor_function)(void *, hv_vcpuid_t);
struct test_vcpu {
hv_vcpuid_t vcpu;
vcpu_entry_function guest_func;
uint64_t guest_param;
vcpu_monitor_function monitor_func;
void *monitor_param;
};
static uint64_t
canonicalize(uint64_t ctrl, uint64_t mask)
{
return (ctrl | (mask & 0xffffffff)) & (mask >> 32);
}
static void
setup_real_mode(hv_vcpuid_t vcpu)
{
uint64_t pin_cap, proc_cap, proc2_cap, entry_cap, exit_cap;
pin_cap = get_cap(HV_VMX_CAP_PINBASED);
proc_cap = get_cap(HV_VMX_CAP_PROCBASED);
proc2_cap = get_cap(HV_VMX_CAP_PROCBASED2);
entry_cap = get_cap(HV_VMX_CAP_ENTRY);
exit_cap = get_cap(HV_VMX_CAP_EXIT);
set_vmcs(vcpu, VMCS_CTRL_PIN_BASED, canonicalize(0, pin_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED,
canonicalize(CPU_BASED_HLT | CPU_BASED_CR8_LOAD | CPU_BASED_CR8_STORE, proc_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED2, canonicalize(0, proc2_cap));
set_vmcs(vcpu, VMCS_CTRL_VMENTRY_CONTROLS, canonicalize(0, entry_cap));
set_vmcs(vcpu, VMCS_CTRL_VMEXIT_CONTROLS, canonicalize(0, exit_cap));
set_vmcs(vcpu, VMCS_GUEST_CR0, 0x20);
set_vmcs(vcpu, VMCS_CTRL_CR0_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR0_SHADOW, 0x20);
set_vmcs(vcpu, VMCS_GUEST_CR4, 0x2000);
set_vmcs(vcpu, VMCS_CTRL_CR4_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR4_SHADOW, 0x0000);
set_vmcs(vcpu, VMCS_GUEST_TR_AR, 0x83);
set_vmcs(vcpu, VMCS_GUEST_LDTR_AR, 0x10000);
set_vmcs(vcpu, VMCS_GUEST_SS, 0);
set_vmcs(vcpu, VMCS_GUEST_SS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_SS_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_SS_AR, 0x93);
set_vmcs(vcpu, VMCS_GUEST_CS, 0);
set_vmcs(vcpu, VMCS_GUEST_CS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_CS_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_CS_AR, 0x9b);
set_vmcs(vcpu, VMCS_GUEST_DS, 0);
set_vmcs(vcpu, VMCS_GUEST_DS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_DS_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_DS_AR, 0x93);
set_vmcs(vcpu, VMCS_GUEST_ES, 0);
set_vmcs(vcpu, VMCS_GUEST_ES_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_ES_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_ES_AR, 0x93);
set_vmcs(vcpu, VMCS_GUEST_FS, 0);
set_vmcs(vcpu, VMCS_GUEST_FS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_FS_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_FS_AR, 0x93);
set_vmcs(vcpu, VMCS_GUEST_GS, 0);
set_vmcs(vcpu, VMCS_GUEST_GS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GS_LIMIT, 0xffff);
set_vmcs(vcpu, VMCS_GUEST_GS_AR, 0x93);
set_vmcs(vcpu, VMCS_GUEST_GDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GDTR_LIMIT, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_LIMIT, 0);
set_vmcs(vcpu, VMCS_GUEST_RFLAGS, 0x2);
set_vmcs(vcpu, VMCS_CTRL_EXC_BITMAP, 0xffffffff);
}
static void
setup_protected_mode(hv_vcpuid_t vcpu)
{
uint64_t pin_cap, proc_cap, proc2_cap, entry_cap, exit_cap;
pin_cap = get_cap(HV_VMX_CAP_PINBASED);
proc_cap = get_cap(HV_VMX_CAP_PROCBASED);
proc2_cap = get_cap(HV_VMX_CAP_PROCBASED2);
entry_cap = get_cap(HV_VMX_CAP_ENTRY);
exit_cap = get_cap(HV_VMX_CAP_EXIT);
set_vmcs(vcpu, VMCS_CTRL_PIN_BASED, canonicalize(0, pin_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED,
canonicalize(CPU_BASED_HLT | CPU_BASED_CR8_LOAD | CPU_BASED_CR8_STORE, proc_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED2, canonicalize(0, proc2_cap));
set_vmcs(vcpu, VMCS_CTRL_VMENTRY_CONTROLS, canonicalize(0, entry_cap));
set_vmcs(vcpu, VMCS_CTRL_VMEXIT_CONTROLS, canonicalize(0, exit_cap));
set_vmcs(vcpu, VMCS_GUEST_CR0, 0x21);
set_vmcs(vcpu, VMCS_CTRL_CR0_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR0_SHADOW, 0x21);
set_vmcs(vcpu, VMCS_GUEST_CR3, 0);
set_vmcs(vcpu, VMCS_GUEST_CR4, 0x2000);
set_vmcs(vcpu, VMCS_CTRL_CR4_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR4_SHADOW, 0x0000);
set_vmcs(vcpu, VMCS_GUEST_TR, 0);
set_vmcs(vcpu, VMCS_GUEST_TR_AR, 0x8b);
set_vmcs(vcpu, VMCS_GUEST_LDTR, 0x0);
set_vmcs(vcpu, VMCS_GUEST_LDTR_AR, 0x10000);
set_vmcs(vcpu, VMCS_GUEST_SS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_SS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_SS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_SS_AR, 0xc093);
set_vmcs(vcpu, VMCS_GUEST_CS, 0x10);
set_vmcs(vcpu, VMCS_GUEST_CS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_CS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_CS_AR, 0xc09b);
set_vmcs(vcpu, VMCS_GUEST_DS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_DS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_DS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_DS_AR, 0xc093);
set_vmcs(vcpu, VMCS_GUEST_ES, 0x8);
set_vmcs(vcpu, VMCS_GUEST_ES_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_ES_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_ES_AR, 0xc093);
set_vmcs(vcpu, VMCS_GUEST_FS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_FS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_FS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_FS_AR, 0xc093);
set_vmcs(vcpu, VMCS_GUEST_GS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_GS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_GS_AR, 0xc093);
set_vmcs(vcpu, VMCS_GUEST_GDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GDTR_LIMIT, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_LIMIT, 0);
set_vmcs(vcpu, VMCS_GUEST_RFLAGS, 0x2);
set_vmcs(vcpu, VMCS_CTRL_EXC_BITMAP, 0xffffffff);
}
static void
setup_long_mode(hv_vcpuid_t vcpu)
{
uint64_t pin_cap, proc_cap, proc2_cap, entry_cap, exit_cap;
pin_cap = get_cap(HV_VMX_CAP_PINBASED);
proc_cap = get_cap(HV_VMX_CAP_PROCBASED);
proc2_cap = get_cap(HV_VMX_CAP_PROCBASED2);
entry_cap = get_cap(HV_VMX_CAP_ENTRY);
exit_cap = get_cap(HV_VMX_CAP_EXIT);
set_vmcs(vcpu, VMCS_CTRL_PIN_BASED, canonicalize(0, pin_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED,
canonicalize(CPU_BASED_HLT | CPU_BASED_CR8_LOAD | CPU_BASED_CR8_STORE, proc_cap));
set_vmcs(vcpu, VMCS_CTRL_CPU_BASED2, canonicalize(0, proc2_cap));
set_vmcs(vcpu, VMCS_CTRL_VMENTRY_CONTROLS, canonicalize(VMENTRY_GUEST_IA32E, entry_cap));
set_vmcs(vcpu, VMCS_CTRL_VMEXIT_CONTROLS, canonicalize(0, exit_cap));
set_vmcs(vcpu, VMCS_GUEST_CR0, 0x80000021L);
set_vmcs(vcpu, VMCS_CTRL_CR0_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR0_SHADOW, 0x80000021L);
set_vmcs(vcpu, VMCS_GUEST_CR4, 0x2020);
set_vmcs(vcpu, VMCS_CTRL_CR4_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR4_SHADOW, 0x2020);
set_vmcs(vcpu, VMCS_GUEST_IA32_EFER, 0x500);
T_QUIET; T_ASSERT_EQ(hv_vcpu_enable_native_msr(vcpu, MSR_IA32_KERNEL_GS_BASE, true), HV_SUCCESS, "enable native GS_BASE");
set_vmcs(vcpu, VMCS_GUEST_TR, 0);
set_vmcs(vcpu, VMCS_GUEST_TR_AR, 0x8b);
set_vmcs(vcpu, VMCS_GUEST_LDTR, 0x0);
set_vmcs(vcpu, VMCS_GUEST_LDTR_AR, 0x10000);
set_vmcs(vcpu, VMCS_GUEST_SS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_SS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_SS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_SS_AR, 0xa093);
set_vmcs(vcpu, VMCS_GUEST_CS, 0x10);
set_vmcs(vcpu, VMCS_GUEST_CS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_CS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_CS_AR, 0xa09b);
set_vmcs(vcpu, VMCS_GUEST_DS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_DS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_DS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_DS_AR, 0xa093);
set_vmcs(vcpu, VMCS_GUEST_ES, 0x8);
set_vmcs(vcpu, VMCS_GUEST_ES_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_ES_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_ES_AR, 0xa093);
set_vmcs(vcpu, VMCS_GUEST_FS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_FS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_FS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_FS_AR, 0xa093);
set_vmcs(vcpu, VMCS_GUEST_GS, 0x8);
set_vmcs(vcpu, VMCS_GUEST_GS_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GS_LIMIT, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_GS_AR, 0xa093);
set_vmcs(vcpu, VMCS_GUEST_RFLAGS, 0x2);
set_vmcs(vcpu, VMCS_CTRL_EXC_BITMAP, 0xffffffff);
set_vmcs(vcpu, VMCS_GUEST_CR3, pml4_gpa);
set_vmcs(vcpu, VMCS_GUEST_GDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_GDTR_LIMIT, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_BASE, 0);
set_vmcs(vcpu, VMCS_GUEST_IDTR_LIMIT, 0);
}
static void *
wrap_monitor(void *param)
{
struct test_vcpu *test = (struct test_vcpu *)param;
T_QUIET; T_ASSERT_EQ(hv_vcpu_create(&test->vcpu, HV_VCPU_DEFAULT), HV_SUCCESS,
"created vcpu");
const size_t stack_size = 0x4000;
void *stack_bottom = valloc(stack_size);
T_QUIET; T_ASSERT_NOTNULL(stack_bottom, "allocate VCPU stack");
vcpu_entry_function entry = test->guest_func;
set_vmcs(test->vcpu, VMCS_GUEST_RIP, (uintptr_t)entry);
set_vmcs(test->vcpu, VMCS_GUEST_RSP, (uintptr_t)stack_bottom + stack_size);
set_reg(test->vcpu, HV_X86_RDI, test->guest_param);
void *result = test->monitor_func(test->monitor_param, test->vcpu);
T_QUIET; T_ASSERT_EQ(hv_vcpu_destroy(test->vcpu), HV_SUCCESS, "Destroyed vcpu");
free(stack_bottom);
free(test);
return result;
}
static pthread_t
create_vcpu_thread(
vcpu_entry_function guest_function, uint64_t guest_param,
vcpu_monitor_function monitor_func, void *monitor_param)
{
pthread_t thread;
struct test_vcpu *test = malloc(sizeof(*test));
T_QUIET; T_ASSERT_NOTNULL(test, "malloc test params");
test->guest_func = guest_function;
test->guest_param = guest_param;
test->monitor_func = monitor_func;
test->monitor_param = monitor_param;
T_ASSERT_POSIX_SUCCESS(pthread_create(&thread, NULL, wrap_monitor, test),
"create vcpu pthread");
// ownership of test struct moves to the thread
test = NULL;
return thread;
}
static void
vm_setup()
{
T_SETUPBEGIN;
if (hv_support() < 1) {
T_SKIP("Running on non-HV target, skipping...");
return;
}
page_cache = [[NSMutableDictionary alloc] init];
allocated_phys_pages = [[NSMutableSet alloc] init];
T_ASSERT_EQ(hv_vm_create(HV_VM_DEFAULT), HV_SUCCESS, "Created vm");
// Set up root paging structures for long mode,
// where paging is mandatory.
pml4_gpa = map_guest_phys((void**)&pml4);
memset(pml4, 0, vm_page_size);
T_SETUPEND;
}
static void
vm_cleanup()
{
T_ASSERT_EQ(hv_vm_destroy(), HV_SUCCESS, "Destroyed vm");
free_page_cache();
pml4 = NULL;
pml4_gpa = 0;
}
static pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t vcpus_ready_lock = PTHREAD_MUTEX_INITIALIZER;
static uint32_t vcpus_initializing;
static pthread_mutex_t vcpus_hang_lock = PTHREAD_MUTEX_INITIALIZER;
static void *
multikill_vcpu_thread_function(void __unused *arg)
{
hv_vcpuid_t *vcpu = (hv_vcpuid_t*)arg;
T_QUIET; T_ASSERT_EQ(hv_vcpu_create(vcpu, HV_VCPU_DEFAULT), HV_SUCCESS,
"created vcpu");
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&vcpus_ready_lock),
"acquire vcpus_ready_lock");
T_QUIET; T_ASSERT_NE(vcpus_initializing, 0, "check for vcpus_ready underflow");
vcpus_initializing--;
if (vcpus_initializing == 0) {
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_signal(&ready_cond),
"signaling all VCPUs ready");
}
T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&vcpus_ready_lock),
"release vcpus_ready_lock");
// To cause the VCPU pointer to be cleared from the wrong thread, we need
// to get threads onto the thread deallocate queue. One way to accomplish
// this is to die while waiting for a lock.
T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&vcpus_hang_lock),
"acquire vcpus_hang_lock");
// Do not allow the thread to terminate. Exactly one thread will acquire
// the above lock successfully.
while (true) {
pause();
}
return NULL;
}
T_DECL(regression_55524541,
"kill task with multiple VCPU threads waiting for lock")
{
if (!hv_support()) {
T_SKIP("no HV support");
}
int pipedesc[2];
T_ASSERT_POSIX_SUCCESS(pipe(pipedesc), "create pipe");
pid_t child = fork();
if (child == 0) {
const uint32_t vcpu_count = 8;
pthread_t vcpu_threads[8];
T_ASSERT_EQ(hv_vm_create(HV_VM_DEFAULT), HV_SUCCESS, "created vm");
vcpus_initializing = vcpu_count;
for (uint32_t i = 0; i < vcpu_count; i++) {
hv_vcpuid_t vcpu;
T_ASSERT_POSIX_SUCCESS(pthread_create(&vcpu_threads[i], NULL,
multikill_vcpu_thread_function, (void *)&vcpu),
"create vcpu_threads[%u]", i);
}
T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&vcpus_ready_lock),
"acquire vcpus_ready_lock");
while (vcpus_initializing != 0) {
T_ASSERT_POSIX_SUCCESS(pthread_cond_wait(&ready_cond,
&vcpus_ready_lock), "wait for all threads ready");
}
T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&vcpus_ready_lock),
"release vcpus_ready_lock");
// Indicate readiness to die, meditiate peacefully.
uint8_t byte = 0;
T_ASSERT_EQ_LONG(write(pipedesc[1], &byte, 1), 1L, "notifying on pipe");
while (true) {
pause();
}
} else {
T_ASSERT_GT(child, 0, "successful fork");
// Wait for child to prepare.
uint8_t byte;
T_ASSERT_EQ_LONG(read(pipedesc[0], &byte, 1), 1L, "waiting on pipe");
T_ASSERT_POSIX_SUCCESS(kill(child, SIGTERM), "kill child");
// Hope for no panic...
T_ASSERT_POSIX_SUCCESS(wait(NULL), "reap child");
}
T_ASSERT_POSIX_SUCCESS(close(pipedesc[0]), "close pipedesc[0]");
T_ASSERT_POSIX_SUCCESS(close(pipedesc[1]), "close pipedesc[1]");
}
static void *
simple_long_mode_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_long_mode(vcpu);
expect_vmcall_with_value(vcpu, 0x33456, true);
return NULL;
}
T_DECL(simple_long_mode_guest, "simple long mode guest")
{
vm_setup();
pthread_t vcpu_thread = create_vcpu_thread(simple_long_mode_vcpu_entry, 0x10000, simple_long_mode_monitor, 0);
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu_thread, NULL), "join vcpu");
vm_cleanup();
}
static void *
smp_test_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_long_mode(vcpu);
uint64_t value = expect_vmcall(vcpu, true);
return (void *)(uintptr_t)value;
}
T_DECL(smp_sanity, "Multiple VCPUs in the same VM")
{
vm_setup();
// Use this region as shared memory between the VCPUs.
void *shared = NULL;
map_guest_phys((void**)&shared);
atomic_uint *count_word = (atomic_uint *)shared;
atomic_init(count_word, 0);
pthread_t vcpu1_thread = create_vcpu_thread(smp_vcpu_entry,
(uintptr_t)count_word, smp_test_monitor, count_word);
pthread_t vcpu2_thread = create_vcpu_thread(smp_vcpu_entry,
(uintptr_t)count_word, smp_test_monitor, count_word);
void *r1, *r2;
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu1_thread, &r1), "join vcpu1");
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu2_thread, &r2), "join vcpu2");
uint64_t v1 = (uint64_t)r1;
uint64_t v2 = (uint64_t)r2;
if (v1 == 0) {
T_ASSERT_EQ_ULLONG(v2, 1ULL, "check count");
} else if (v1 == 1) {
T_ASSERT_EQ_ULLONG(v2, 0ULL, "check count");
} else {
T_FAIL("unexpected count: %llu", v1);
}
vm_cleanup();
}
extern void *hvtest_begin;
extern void *hvtest_end;
static void *
simple_protected_mode_test_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_protected_mode(vcpu);
size_t guest_pages_size = round_page((uintptr_t)&hvtest_end - (uintptr_t)&hvtest_begin);
const size_t mem_size = 1 * 1024 * 1024;
uint8_t *guest_pages_shadow = valloc(mem_size);
bzero(guest_pages_shadow, mem_size);
memcpy(guest_pages_shadow+0x1000, &hvtest_begin, guest_pages_size);
T_ASSERT_EQ(hv_vm_map(guest_pages_shadow, 0x40000000, mem_size, HV_MEMORY_READ | HV_MEMORY_EXEC),
HV_SUCCESS, "map guest memory");
expect_vmcall_with_value(vcpu, 0x23456, false);
free(guest_pages_shadow);
return NULL;
}
T_DECL(simple_protected_mode_guest, "simple protected mode guest")
{
vm_setup();
pthread_t vcpu_thread = create_vcpu_thread((vcpu_entry_function)
(((uintptr_t)simple_protected_mode_vcpu_entry & PAGE_MASK) +
0x40000000 + 0x1000),
0, simple_protected_mode_test_monitor, 0);
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu_thread, NULL), "join vcpu");
vm_cleanup();
}
static void *
simple_real_mode_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_real_mode(vcpu);
size_t guest_pages_size = round_page((uintptr_t)&hvtest_end - (uintptr_t)&hvtest_begin);
const size_t mem_size = 1 * 1024 * 1024;
uint8_t *guest_pages_shadow = valloc(mem_size);
bzero(guest_pages_shadow, mem_size);
memcpy(guest_pages_shadow+0x1000, &hvtest_begin, guest_pages_size);
T_ASSERT_EQ(hv_vm_map(guest_pages_shadow, 0x0, mem_size, HV_MEMORY_READ | HV_MEMORY_EXEC), HV_SUCCESS,
"map guest memory");
expect_vmcall_with_value(vcpu, 0x23456, false);
free(guest_pages_shadow);
return NULL;
}
T_DECL(simple_real_mode_guest, "simple real mode guest")
{
vm_setup();
pthread_t vcpu_thread = create_vcpu_thread((vcpu_entry_function)
(((uintptr_t)simple_real_mode_vcpu_entry & PAGE_MASK) +
0x1000),
0, simple_real_mode_monitor, 0);
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu_thread, NULL), "join vcpu");
vm_cleanup();
}
static void *
radar61961809_monitor(void *gpaddr, hv_vcpuid_t vcpu)
{
uint32_t const gdt_template[] = {
0, 0, /* Empty */
0x0000ffff, 0x00cf9200, /* 0x08 CPL0 4GB writable data, 32bit */
0x0000ffff, 0x00cf9a00, /* 0x10 CPL0 4GB readable code, 32bit */
0x0000ffff, 0x00af9200, /* 0x18 CPL0 4GB writable data, 64bit */
0x0000ffff, 0x00af9a00, /* 0x20 CPL0 4GB readable code, 64bit */
};
// We start the test in protected mode.
setup_protected_mode(vcpu);
// SAVE_EFER makes untrapped CR0.PG work.
uint64_t exit_cap = get_cap(HV_VMX_CAP_EXIT);
set_vmcs(vcpu, VMCS_CTRL_VMEXIT_CONTROLS, canonicalize(VMEXIT_SAVE_EFER, exit_cap));
// Start with CR0.PG disabled.
set_vmcs(vcpu, VMCS_GUEST_CR0, 0x00000021);
set_vmcs(vcpu, VMCS_CTRL_CR0_SHADOW, 0x00000021);
/*
* Don't trap on modifying CR0.PG to reproduce the problem.
* Otherwise, we'd have to handle the switch ourselves, and would
* just do it right.
*/
set_vmcs(vcpu, VMCS_CTRL_CR0_MASK, ~0x80000000UL);
// PAE must be enabled for a switch into long mode to work.
set_vmcs(vcpu, VMCS_GUEST_CR4, 0x2020);
set_vmcs(vcpu, VMCS_CTRL_CR4_MASK, ~0u);
set_vmcs(vcpu, VMCS_CTRL_CR4_SHADOW, 0x2020);
// Will use the harness managed page tables in long mode.
set_vmcs(vcpu, VMCS_GUEST_CR3, pml4_gpa);
// Hypervisor fw wants this (for good, but unrelated reason).
T_QUIET; T_ASSERT_EQ(hv_vcpu_enable_native_msr(vcpu, MSR_IA32_KERNEL_GS_BASE, true), HV_SUCCESS, "enable native GS_BASE");
// Far pointer array for our far jumps.
uint32_t *far_ptr = NULL;
hv_gpaddr_t far_ptr_gpaddr = map_guest_phys((void**)&far_ptr);
map_page(far_ptr, (void*)far_ptr_gpaddr);
far_ptr[0] = (uint32_t)(((uintptr_t)&radar61961809_prepare - (uintptr_t)&hvtest_begin) + (uintptr_t)gpaddr);
far_ptr[1] = 0x0010; // 32bit CS
far_ptr[2] = (uint32_t)(((uintptr_t)&radar61961809_loop64 - (uintptr_t)&hvtest_begin) + (uintptr_t)gpaddr);
far_ptr[3] = 0x0020; // 64bit CS
set_reg(vcpu, HV_X86_RDI, far_ptr_gpaddr);
// Setup GDT.
uint32_t *gdt = valloc(vm_page_size);
hv_gpaddr_t gdt_gpaddr = 0x70000000;
map_page(gdt, (void*)gdt_gpaddr);
bzero(gdt, vm_page_size);
memcpy(gdt, gdt_template, sizeof(gdt_template));
set_vmcs(vcpu, VMCS_GUEST_GDTR_BASE, gdt_gpaddr);
set_vmcs(vcpu, VMCS_GUEST_GDTR_LIMIT, sizeof(gdt_template)+1);
// Map test code (because we start in protected mode without
// paging, we cannot use the harness's fault management yet.)
size_t guest_pages_size = round_page((uintptr_t)&hvtest_end - (uintptr_t)&hvtest_begin);
const size_t mem_size = 1 * 1024 * 1024;
uint8_t *guest_pages_shadow = valloc(mem_size);
bzero(guest_pages_shadow, mem_size);
memcpy(guest_pages_shadow, &hvtest_begin, guest_pages_size);
T_ASSERT_EQ(hv_vm_map(guest_pages_shadow, (hv_gpaddr_t)gpaddr, mem_size, HV_MEMORY_READ | HV_MEMORY_EXEC),
HV_SUCCESS, "map guest memory");
// Create entries in PML4.
uint8_t *host_va = guest_pages_shadow;
uint8_t *va = (uint8_t*)gpaddr;
for (unsigned long i = 0; i < guest_pages_size / vm_page_size; i++, va += vm_page_size, host_va += vm_page_size) {
map_page(host_va, va);
}
uint64_t reason = run_to_next_vm_fault(vcpu, false);
T_ASSERT_EQ(reason, (uint64_t)VMX_REASON_RDMSR, "check for rdmsr");
T_ASSERT_EQ(get_reg(vcpu, HV_X86_RCX), 0xc0000080LL, "expected EFER rdmsr");
set_reg(vcpu, HV_X86_RDX, 0);
set_reg(vcpu, HV_X86_RAX, 0);
set_vmcs(vcpu, VMCS_GUEST_RIP, get_reg(vcpu, HV_X86_RIP)+get_vmcs(vcpu, VMCS_RO_VMEXIT_INSTR_LEN));
reason = run_to_next_vm_fault(vcpu, false);
T_ASSERT_EQ(reason, (uint64_t)VMX_REASON_WRMSR, "check for wrmsr");
T_ASSERT_EQ(get_reg(vcpu, HV_X86_RCX), 0xc0000080LL, "expected EFER wrmsr");
T_ASSERT_EQ(get_reg(vcpu, HV_X86_RDX), 0x0LL, "expected EFER wrmsr higher bits 0");
T_ASSERT_EQ(get_reg(vcpu, HV_X86_RAX), 0x100LL, "expected EFER wrmsr lower bits LME");
set_vmcs(vcpu, VMCS_GUEST_IA32_EFER, 0x100);
set_vmcs(vcpu, VMCS_GUEST_RIP, get_reg(vcpu, HV_X86_RIP)+get_vmcs(vcpu, VMCS_RO_VMEXIT_INSTR_LEN));
// See assembly part of the test for checkpoints.
expect_vmcall_with_value(vcpu, 0x100, false /* PG disabled =>
* no PFs expected */);
expect_vmcall_with_value(vcpu, 0x1111, true /* PG now enabled */);
expect_vmcall_with_value(vcpu, 0x2222, true);
free(guest_pages_shadow);
free(gdt);
return NULL;
}
T_DECL(radar61961809_guest,
"rdar://61961809 (Unexpected guest faults with hv_vcpu_run_until, dropping out of long mode)")
{
vm_setup();
hv_gpaddr_t gpaddr = 0x80000000;
pthread_t vcpu_thread = create_vcpu_thread((vcpu_entry_function)
(((uintptr_t)radar61961809_entry & PAGE_MASK) +
gpaddr),
0, radar61961809_monitor, (void*)gpaddr);
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu_thread, NULL), "join vcpu");
vm_cleanup();
}
static void *
superpage_2mb_backed_guest_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_protected_mode(vcpu);
size_t guest_pages_size = round_page((uintptr_t)&hvtest_end - (uintptr_t)&hvtest_begin);
const size_t mem_size = 2 * 1024 * 1024;
uint8_t *guest_pages_shadow = mmap(NULL, mem_size,
PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
if (guest_pages_shadow == MAP_FAILED) {
/* Getting a 2MB superpage is hard in practice, because memory gets fragmented
* easily.
* T_META_REQUIRES_REBOOT in the T_DECL helps a lot in actually getting a page,
* but in the case that it still fails, we don't want the test to fail through
* no fault of the hypervisor.
*/
T_SKIP("Unable to attain a 2MB superpage. Skipping.");
}
bzero(guest_pages_shadow, mem_size);
memcpy(guest_pages_shadow+0x1000, &hvtest_begin, guest_pages_size);
T_ASSERT_EQ(hv_vm_map(guest_pages_shadow, 0x40000000, mem_size, HV_MEMORY_READ | HV_MEMORY_EXEC),
HV_SUCCESS, "map guest memory");
expect_vmcall_with_value(vcpu, 0x23456, false);
munmap(guest_pages_shadow, mem_size);
return NULL;
}
T_DECL(superpage_2mb_backed_guest, "guest backed by a 2MB superpage",
T_META_REQUIRES_REBOOT(true)) // Helps actually getting a superpage
{
vm_setup();
pthread_t vcpu_thread = create_vcpu_thread((vcpu_entry_function)
(((uintptr_t)simple_protected_mode_vcpu_entry & PAGE_MASK) +
0x40000000 + 0x1000),
0, superpage_2mb_backed_guest_monitor, 0);
T_ASSERT_POSIX_SUCCESS(pthread_join(vcpu_thread, NULL), "join vcpu");
vm_cleanup();
}
static void *
save_restore_regs_monitor(void *arg __unused, hv_vcpuid_t vcpu)
{
setup_long_mode(vcpu);
uint64_t rsp = get_reg(vcpu, HV_X86_RSP);
set_reg(vcpu, HV_X86_RAX, 0x0101010101010101);
set_reg(vcpu, HV_X86_RBX, 0x0202020202020202);
set_reg(vcpu, HV_X86_RCX, 0x0303030303030303);
set_reg(vcpu, HV_X86_RDX, 0x0404040404040404);
set_reg(vcpu, HV_X86_RSI, 0x0505050505050505);
set_reg(vcpu, HV_X86_RDI, 0x0606060606060606);
set_reg(vcpu, HV_X86_RBP, 0x0707070707070707);
set_reg(vcpu, HV_X86_R8, 0x0808080808080808);
set_reg(vcpu, HV_X86_R9, 0x0909090909090909);