This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 882
/
Copy pathvcpu.c
3936 lines (3484 loc) · 119 KB
/
vcpu.c
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
/*
* Copyright (c) 2009 Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "../include/hax.h"
#include "include/ia32.h"
#include "include/vcpu.h"
#include "include/mtrr.h"
#include "include/vmx.h"
#include "include/cpu.h"
#include "include/vm.h"
#include "include/debug.h"
#include "include/dump_vmcs.h"
#include "include/intr.h"
#include "include/vtlb.h"
#include "include/ept.h"
#include "include/paging.h"
#include "include/hax_core_interface.h"
#include "include/hax_driver.h"
uint64 gmsr_list[NR_GMSR] = {
IA32_STAR,
IA32_LSTAR,
IA32_CSTAR,
IA32_SF_MASK,
IA32_KERNEL_GS_BASE
};
uint64 hmsr_list[NR_HMSR] = {
IA32_EFER,
IA32_STAR,
IA32_LSTAR,
IA32_CSTAR,
IA32_SF_MASK,
IA32_KERNEL_GS_BASE
};
uint64 emt64_msr[NR_EMT64MSR] = {
IA32_STAR,
IA32_LSTAR,
IA32_CSTAR,
IA32_SF_MASK,
IA32_KERNEL_GS_BASE
};
extern uint32 pw_reserved_bits_high_mask;
static void vcpu_init(struct vcpu_t *vcpu);
static void vcpu_prepare(struct vcpu_t *vcpu);
static void vcpu_init_emulator(struct vcpu_t *vcpu);
static void vmread_cr(struct vcpu_t *vcpu);
static void vmwrite_cr(struct vcpu_t *vcpu);
static int exit_exc_nmi(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_interrupt(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_triple_fault(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_interrupt_window(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_taskswitch(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_cpuid(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_hlt(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_invlpg(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_rdtsc(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_cr_access(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_dr_access(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_io_access(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_msr_read(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_msr_write(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int exit_invalid_guest_state(struct vcpu_t *vcpu,
struct hax_tunnel *htun);
static int exit_ept_misconfiguration(struct vcpu_t *vcpu,
struct hax_tunnel *htun);
static int exit_ept_violation(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static int null_handler(struct vcpu_t *vcpu, struct hax_tunnel *hun);
static void advance_rip(struct vcpu_t *vcpu);
static void handle_machine_check(struct vcpu_t *vcpu);
static void handle_cpuid_virtual(struct vcpu_t *vcpu, uint32 eax, uint32 ecx);
static void handle_mem_fault(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static void check_flush(struct vcpu_t *vcpu, uint32 bits);
static void vmwrite_efer(struct vcpu_t *vcpu);
static int handle_msr_read(struct vcpu_t *vcpu, uint32 msr, uint64 *val);
static int handle_msr_write(struct vcpu_t *vcpu, uint32 msr, uint64 val);
static void handle_cpuid(struct vcpu_t *vcpu, struct hax_tunnel *htun);
static void vcpu_dump(struct vcpu_t *vcpu, uint32 mask, const char *caption);
static void vcpu_state_dump(struct vcpu_t *vcpu);
static void vcpu_enter_fpu_state(struct vcpu_t *vcpu);
static int vcpu_set_apic_base(struct vcpu_t *vcpu, uint64 val);
static bool vcpu_is_bsp(struct vcpu_t *vcpu);
static uint32 get_seg_present(uint32 seg)
{
mword ldtr_base;
struct seg_desc_t *seg_desc;
struct hstate *hstate = &get_cpu_data(hax_cpuid())->hstate;
ldtr_base = get_kernel_ldtr_base();
seg_desc = (struct seg_desc_t *)ldtr_base + (seg >> 3);
if (seg_desc->_present) {
hstate->fake_gs = seg_desc->_raw;
}
return seg_desc->_present;
}
static void fake_seg_gs_entry(struct hstate *hstate)
{
mword ldtr_base;
struct seg_desc_t *seg_desc = NULL;
uint16 seg = hstate->gs;
ldtr_base = get_kernel_ldtr_base();
seg_desc = (struct seg_desc_t *)ldtr_base + (seg >> 3);
if (seg_desc->_present == 0)
seg_desc->_raw = hstate->fake_gs;
set_kernel_gs(seg);
ia32_wrmsr(IA32_GS_BASE, hstate->gs_base);
seg_desc->_raw = 0;
}
static void get_segment_desc_t(segment_desc_t *sdt, uint32 s, uint64 b,
uint32 l, uint32 a)
{
sdt->selector = s;
sdt->base = b;
sdt->limit = l;
sdt->ar = a;
}
static inline void set_gdt(struct vcpu_state_t *state, uint64 base,
uint64 limit)
{
state->_gdt.base = base;
state->_gdt.limit = limit;
}
static inline void set_idt(struct vcpu_state_t *state, uint64 base,
uint64 limit)
{
state->_idt.base = base;
state->_idt.limit = limit;
}
static uint64 vcpu_read_cr(struct vcpu_state_t *state, uint32 n)
{
uint64 val = 0;
switch (n) {
case 0: {
val = state->_cr0;
break;
}
case 2: {
val = state->_cr2;
break;
}
case 3: {
val = state->_cr3;
break;
}
case 4: {
val = state->_cr4;
break;
}
default: {
hax_error("Unsupported CR%d access\n", n);
break;
}
}
hax_debug("vcpu_read_cr cr %x val %llx\n", n, val);
return val;
}
static void vcpu_write_cr(struct vcpu_state_t *state, uint32 n, uint64 val)
{
hax_debug("vcpu_write_cr cr %x val %llx\n", n, val);
switch (n) {
case 0: {
state->_cr0 = val;
break;
}
case 2: {
state->_cr2 = val;
break;
}
case 3: {
state->_cr3 = val;
break;
}
case 4: {
state->_cr4 = val;
break;
}
default: {
hax_error("write_cr: Unsupported CR%d access\n", n);
break;
}
}
}
void * vcpu_vmcs_va(struct vcpu_t *vcpu)
{
return hax_page_va(vcpu->vmcs_page);
}
paddr_t vcpu_vmcs_pa(struct vcpu_t *vcpu)
{
return hax_page_pa(vcpu->vmcs_page);
}
static void set_activity_state(struct vcpu_state_t *vcpu_state, uint state)
{
vcpu_state->_activity_state = state;
}
static uint get_activity_state(struct vcpu_state_t *state)
{
return state->_activity_state;
}
void * get_vcpu_host(struct vcpu_t *vcpu)
{
return vcpu ? vcpu->vcpu_host : NULL;
}
int set_vcpu_host(struct vcpu_t *vcpu, void *vcpu_host)
{
if (!vcpu || (vcpu->vcpu_host && vcpu->vcpu_host != vcpu_host))
return -1;
vcpu->vcpu_host = vcpu_host;
return 0;
}
int set_vcpu_tunnel(struct vcpu_t *vcpu, struct hax_tunnel *tunnel,
uint8 *iobuf)
{
if (!vcpu || (vcpu->tunnel && tunnel && vcpu->tunnel != tunnel) ||
(vcpu->io_buf && iobuf && vcpu->io_buf != iobuf))
return -1;
vcpu->tunnel = tunnel;
vcpu->io_buf = iobuf;
return 0;
}
struct hax_tunnel * get_vcpu_tunnel(struct vcpu_t *vcpu)
{
return vcpu ? vcpu->tunnel : NULL;
}
/*
* vcpu_vpid_alloc()
*
* Allocate a non-zero unique VPID for virtual processor.
* The allocated VPID is stored in vcpu->vpid.
* In the case of allocating failure, the vcpu->vpid will keeps zero, which
* means "do not enable VPID feature".
*
* Param: vcpu - specify the virtual processor who will get the VPID
* Return Value: 0 - means success. Negative value - means failure.
*/
static int vcpu_vpid_alloc(struct vcpu_t *vcpu)
{
uint32 vpid_seed_bits = sizeof(vcpu->vm->vpid_seed) * 8;
uint8 bit, max_bit;
max_bit = vpid_seed_bits > 0xff ? 0xff : vpid_seed_bits;
if (0 != vcpu->vpid) {
hax_warning("vcpu_vpid_alloc: vcpu %u in vm %d already has a valid "
"VPID 0x%x.\n", vcpu->vcpu_id, vcpu->vm->vm_id, vcpu->vpid);
return -1;
}
for (bit = 0; bit < max_bit; bit++) {
if (!hax_test_and_set_bit(bit, (uint64 *)vcpu->vm->vpid_seed))
break;
}
if (bit == max_bit) {
// No available VPID resource
hax_error("vcpu_vpid_alloc: no available vpid resource. vcpu: %u, "
"vm: %d\n", vcpu->vcpu_id, vcpu->vm->vm_id);
return -2;
}
/*
* We split vpid as high byte and low byte, the vpid seed is used to
* generate low byte. We use the index of first zero bit in vpid seed plus 1
* as the value of low_byte, and use vcpu->vm->vm_id as the value of high
* byte.
* Note: vpid can't be zero.
*/
vcpu->vpid = (uint16)(vcpu->vm->vm_id << 8) + (uint16)(bit + 1);
hax_info("vcpu_vpid_alloc: succeed! vpid: 0x%x. vcpu_id: %u, vm_id: %d.\n",
vcpu->vpid, vcpu->vcpu_id, vcpu->vm->vm_id);
return 0;
}
/*
* vcpu_vpid_free()
*
* Free the VPID that stored in vcpu->vpid for virtual processor.
* The value of vcpu->vpid will be reset to zero after freeing.
*
* Param: vcpu - specify the virtual processor whose VPID will be freed.
* Return Value: 0 - means success.
* Negative value - means vcpu->vpid has been already freed.
*/
static int vcpu_vpid_free(struct vcpu_t *vcpu)
{
uint8 bit = (vcpu->vpid & 0xff) - 1;
if (0 == vcpu->vpid) {
hax_warning("vcpu_vpid_free: vcpu %u in vm %d does not have a valid "
"VPID.\n", vcpu->vcpu_id, vcpu->vm->vm_id);
return -1;
}
hax_info("vcpu_vpid_free: Clearing bit: 0x%x, vpid_seed: 0x%llx. "
"vcpu_id: %u, vm_id: %d.\n", bit, *(uint64 *)vcpu->vm->vpid_seed,
vcpu->vcpu_id, vcpu->vm->vm_id);
if (0 != hax_test_and_clear_bit(bit, (uint64 *)(vcpu->vm->vpid_seed))) {
hax_warning("vcpu_vpid_free: bit for vpid 0x%x of vcpu %u in vm %d was "
"already clear.\n", vcpu->vpid, vcpu->vcpu_id,
vcpu->vm->vm_id);
}
vcpu->vpid = 0;
return 0;
}
static int (*handler_funcs[])(struct vcpu_t *vcpu, struct hax_tunnel *htun) = {
exit_exc_nmi,
exit_interrupt,
exit_triple_fault,
0, 0, 0, 0,
exit_interrupt_window, // Interrupt window
exit_interrupt_window, // NMI window
0,
exit_cpuid,
0,
exit_hlt,
0,
exit_invlpg,
0,
exit_rdtsc,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17 ... 27
exit_cr_access,
exit_dr_access,
exit_io_access,
exit_msr_read,
exit_msr_write,
exit_invalid_guest_state,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 34 ... 47
exit_ept_violation,
exit_ept_misconfiguration,
0, 0, 0, 0, 0, 0 // 50 ... 55
};
static int nr_handlers = ARRAY_ELEMENTS(handler_funcs);
struct vcpu_t *vcpu_create(struct vm_t *vm, void *vm_host, int vcpu_id)
{
struct hax_tunnel_info info;
struct vcpu_t *vcpu;
hax_debug("vcpu_create vcpu_id %x\n", vcpu_id);
if (!valid_vcpu_id(vcpu_id))
return NULL;
vcpu = (struct vcpu_t *)hax_vmalloc(sizeof(struct vcpu_t), HAX_MEM_NONPAGE);
if (!vcpu)
goto fail_0;
hax_clear_panic_log(vcpu);
memset(vcpu, 0, sizeof(struct vcpu_t));
if (hax_vcpu_setup_hax_tunnel(vcpu, &info) < 0) {
hax_error("HAX: cannot setup hax_tunnel for vcpu.\n");
goto fail_1;
}
vcpu->vmcs_page = (struct hax_page *)hax_alloc_page(0, 1);
if (!vcpu->vmcs_page)
goto fail_2;
vcpu->gstate.gfxpage = (struct hax_page *)hax_alloc_page(0, 1);
if (!vcpu->gstate.gfxpage)
goto fail_3;
hax_clear_page(vcpu->gstate.gfxpage);
hax_clear_page(vcpu->vmcs_page);
vcpu->state = (struct vcpu_state_t *)hax_vmalloc(
sizeof(struct vcpu_state_t), HAX_MEM_NONPAGE);
if (!vcpu->state)
goto fail_4;
memset(vcpu->state, 0, sizeof(struct vcpu_state_t));
vcpu->tmutex = hax_mutex_alloc_init();
if (!vcpu->tmutex)
goto fail_5;
if (!vcpu_vtlb_alloc(vcpu))
goto fail_6;
if (hax_vcpu_create_host(vcpu, vm_host, vm->vm_id, vcpu_id))
goto fail_7;
vcpu->cpu_id = hax_cpuid();
vcpu->vcpu_id = vcpu_id;
vcpu->is_running = 0;
vcpu->vm = vm;
// Must ensure it is called before fill_common_vmcs is called
vcpu_vpid_alloc(vcpu);
// Prepare guest environment
vcpu_init(vcpu);
// First time vmclear/vmptrld on current CPU
vcpu_prepare(vcpu);
// Init IA32_APIC_BASE MSR
vcpu->gstate.apic_base = APIC_BASE_DEFAULT_ADDR | APIC_BASE_ENABLE;
if (vcpu_is_bsp(vcpu)) {
vcpu->gstate.apic_base |= APIC_BASE_BSP;
}
// Publish the vcpu
hax_mutex_lock(vm->vm_lock);
hax_list_add(&vcpu->vcpu_list, &vm->vcpu_list);
// The caller should get_vm thus no race with vm destroy
hax_atomic_add(&vm->ref_count, 1);
hax_mutex_unlock(vm->vm_lock);
// Initialize emulator
vcpu_init_emulator(vcpu);
hax_debug("HAX: vcpu %d is created.\n", vcpu->vcpu_id);
return vcpu;
fail_7:
vcpu_vtlb_free(vcpu);
fail_6:
hax_mutex_free(vcpu->tmutex);
fail_5:
hax_vfree(vcpu->state, sizeof(struct vcpu_state_t));
fail_4:
if (vcpu->gstate.gfxpage) {
hax_free_pages(vcpu->gstate.gfxpage);
}
fail_3:
hax_free_pages(vcpu->vmcs_page);
fail_2:
hax_vcpu_destroy_hax_tunnel(vcpu);
fail_1:
hax_vfree(vcpu, sizeof(struct vcpu_t));
fail_0:
hax_error("HAX: Cannot allocate memory to create vcpu.\n");
return NULL;
}
/*
* We don't need corresponding vcpu_core_close because once closed, the VM will
* be destroyed
*/
int hax_vcpu_core_open(struct vcpu_t *vcpu)
{
if (!vcpu)
return -ENODEV;
if (hax_test_and_set_bit(VCPU_STATE_FLAGS_OPENED, &(vcpu->flags)))
return -EBUSY;
return 0;
}
static int _vcpu_teardown(struct vcpu_t *vcpu)
{
int vcpu_id = vcpu->vcpu_id;
#ifdef CONFIG_HAX_EPT2
if (vcpu->mmio_fetch.kva) {
gpa_space_unmap_page(&vcpu->vm->gpa_space, &vcpu->mmio_fetch.kmap);
}
#endif // CONFIG_HAX_EPT2
// TODO: we should call invvpid after calling vcpu_vpid_free().
vcpu_vpid_free(vcpu);
if (vcpu->gstate.gfxpage) {
hax_free_pages(vcpu->gstate.gfxpage);
}
hax_free_pages(vcpu->vmcs_page);
hax_vfree(vcpu->state, sizeof(struct vcpu_state_t));
vcpu_vtlb_free(vcpu);
hax_mutex_free(vcpu->tmutex);
hax_vfree(vcpu, sizeof(struct vcpu_t));
hax_info("HAX: vcpu %d is teardown.\n", vcpu_id);
return 0;
}
int vcpu_teardown(struct vcpu_t *vcpu)
{
struct vm_t *vm = vcpu->vm;
int ret;
hax_mutex_lock(vm->vm_lock);
hax_list_del(&vcpu->vcpu_list);
hax_mutex_unlock(vm->vm_lock);
ret = _vcpu_teardown(vcpu);
// Should not hold the vmlock here
// Trying to put vm again
hax_put_vm(vm);
return ret;
}
static void vcpu_init(struct vcpu_t *vcpu)
{
// TODO: Need to decide which mode guest will start
struct vcpu_state_t *state = vcpu->state;
hax_mutex_lock(vcpu->tmutex);
// TODO: mtrr ?
vcpu->cr_pat = 0x0007040600070406ULL;
vcpu->cpuid_features_flag_mask = 0xffffffffffffffffULL;
vcpu->cur_state = GS_VALID;
vmx(vcpu, entry_exception_vector) = ~0u;
vmx(vcpu, cr0_mask) = 0;
vmx(vcpu, cr0_shadow) = 0;
vmx(vcpu, cr4_mask) = 0;
vmx(vcpu, cr4_shadow) = 0;
vcpu->ref_count = 1;
vcpu->tsc_offset = 0ULL - rdtsc();
// Prepare the vcpu state to Power-up
state->_rflags = 2;
state->_rip = 0x0000fff0;
state->_cr0 = 0x60000010;
get_segment_desc_t(&state->_cs, 0xf000, 0xffff0000, 0xffff, 0x93);
get_segment_desc_t(&state->_ss, 0, 0, 0xffff, 0x93);
get_segment_desc_t(&state->_ds, 0, 0, 0xffff, 0x93);
get_segment_desc_t(&state->_es, 0, 0, 0xffff, 0x93);
get_segment_desc_t(&state->_fs, 0, 0, 0xffff, 0x93);
get_segment_desc_t(&state->_gs, 0, 0, 0xffff, 0x93);
set_gdt(state, 0, 0xffff);
set_idt(state, 0, 0xffff);
get_segment_desc_t(&state->_ldt, 0, 0, 0xffff, 0x82);
get_segment_desc_t(&state->_tr, 0, 0, 0xffff, 0x83);
state->_dr0 = state->_dr1 = state->_dr2 = state->_dr3 = 0x0;
state->_dr6 = 0xffff0ff0;
state->_dr7 = 0x00000400;
hax_mutex_unlock(vcpu->tmutex);
}
#ifdef DEBUG_HOST_STATE
static int check_panic(void)
{
char *kernel_panic = NULL;
return 0;
}
// Code to check the host state between vmluanch and vmexit
static uint32 get_seg_avail(uint32 seg)
{
mword gdtr_base;
struct seg_desc_t *sd;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
return sd->_avl;
}
static void dump_segment(uint32 seg)
{
struct seg_desc_t *sd;
mword gdtr_base;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
hax_debug("seg %x value %llx\n", seg, sd->_raw);
}
static int check_cs(uint32 seg)
{
mword gdtr_base;
mword desc_base;
struct seg_desc_t *sd;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
if (sd->_base0 != 0 || sd->_base1 != 0)
return 1;
if (sd->_limit1 != 0xf || sd->_limit0 != 0xffff)
return 1;
if (sd->_type != 0xb)
return 1;
if (sd->_s != 1)
return 1;
if (sd->_dpl != 0)
return 1;
if (sd->_present != 1)
return 1;
if (sd->_longmode != 0x1)
return 1;
if (sd->_d != 0x0)
return 1;
if (sd->_granularity != 1)
return 1;
return 0;
}
static int check_data_seg(uint32 seg)
{
mword gdtr_base;
mword desc_base;
struct seg_desc_t *sd;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
if (sd->_base0 != 0 || sd->_base1 != 0)
return 1;
if (sd->_limit1 != 0xf || sd->_limit0 != 0xffff)
return 1;
if (sd->_type != 0x3)
return 1;
if (sd->_s != 1)
return 1;
if (sd->_dpl != 0) {
// The DPL is wrong for Mac return 0 now
return 0;
}
if (sd->_present != 1)
return 1;
// if (sd->_longmode != 0x1)
// return 1;
if (sd->_d != 0x1)
return 1;
if (sd->_granularity != 1)
return 1;
return 0;
}
static int check_stack_seg(uint32 seg)
{
mword gdtr_base;
mword desc_base;
struct seg_desc_t *sd;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
if (sd->_base0 != 0 || sd->_base1 != 0)
return 1;
if (sd->_limit1 != 0xf || sd->_limit0 != 0xffff)
return 1;
if (sd->_type != 0x3)
return 1;
if (sd->_s != 1)
return 1;
if (sd->_dpl != 0)
return 1;
if (sd->_present != 1)
return 1;
// if (sd->_longmode != 0x1)
// return 1;
if (sd->_d != 0x1)
return 1;
if (sd->_granularity != 1)
return 1;
return 0;
}
static int check_tr_seg(uint32 seg)
{
mword gdtr_base;
struct seg_desc_t *sd;
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
// if (sd->_base0 != 0 || sd->_base1 != 0)
// return 1;
if (sd->_limit1 != 0x0 || sd->_limit0 != 0x67)
return 1;
if (sd->_type != 0xb)
return 1;
if (sd->_s != 0)
return 1;
if (sd->_dpl != 0)
return 1;
if (sd->_present != 1)
return 1;
// if (sd->_longmode != 0x1)
// return 1;
if (sd->_d != 0x0)
return 1;
if (sd->_granularity != 0)
return 1;
return 0;
}
static int check_fgs_seg(uint32 seg, uint fs)
{
mword gdtr_base;
mword desc_base;
struct seg_desc_t *sd;
mword base;
if (seg == 0) {
hax_debug("fgs_seg seg is %x fs %x\n", seg, fs);
return 0;
}
gdtr_base = get_kernel_gdtr_base();
sd = (struct seg_desc_t *)gdtr_base + (seg >> 3);
if (fs) {
base = ia32_rdmsr(IA32_FS_BASE);
} else {
base = ia32_rdmsr(IA32_GS_BASE);
}
if ((base & 0xffffff) != sd->_base0 ||
((base >> 24) & 0xff) != sd->_base1) {
// hax_debug("%s base address mismatch base %llx sd %llx\n",
// fs ? "fs" : "gs", base, sd->_raw);
// return 1;
return 0;
}
if (sd->_base0 != 0 || sd->_base1 != 0)
return 1;
if (sd->_limit1 != 0xf || sd->_limit0 != 0xffff)
return 1;
if (sd->_type != 0x3)
return 1;
if (sd->_s != 1)
return 1;
if (sd->_dpl != 0)
return 1;
if (sd->_present != 1)
return 1;
// if (sd->_longmode != 0x1)
// return 1;
if (sd->_d != 0x1)
return 1;
if (sd->_granularity != 1)
return 1;
return 0;
}
int vcpu_get_host_state(struct vcpu_t *vcpu, int pre)
{
uint64 value;
struct host_state_compare *hsc;
hsc = pre ? &vcpu->hsc_pre : &vcpu->hsc_post;
memset(hsc, 0, sizeof(struct host_state_compare));
hsc->cr0 = get_cr0();
hsc->cr2 = get_cr2();
hsc->cr3 = get_cr3();
hsc->cr4 = get_cr4();
// Check segmentation
hsc->cs = get_kernel_cs();
hsc->cs_avail = get_seg_avail(hsc->cs);
hsc->ds = get_kernel_ds();
hsc->ds_avail = get_seg_avail(hsc->ds);
hsc->es = get_kernel_es();
hsc->es_avail = get_seg_avail(hsc->es);
hsc->ss = get_kernel_ss();
hsc->ss_avail = get_seg_avail(hsc->ss);
hsc->fs = get_kernel_fs();
hsc->fs_avail = get_seg_avail(hsc->fs);
hsc->gs = get_kernel_gs();
hsc->gs_avail = get_seg_avail(hsc->gs);
hsc->tr = get_kernel_tr_selector();
hsc->tr_avail = get_seg_avail(hsc->tr);
hsc->ldt = get_kernel_ldt();
hsc->efer = ia32_rdmsr(IA32_EFER);
hsc->sysenter_cs = ia32_rdmsr(IA32_SYSENTER_CS);
hsc->sysenter_eip = ia32_rdmsr(IA32_SYSENTER_EIP);
hsc->sysenter_esp = ia32_rdmsr(IA32_SYSENTER_ESP);
hsc->pat_msr = ia32_rdmsr(IA32_CR_PAT);
hsc->fs_msr = ia32_rdmsr(IA32_FS_BASE);
hsc->gs_msr = ia32_rdmsr(IA32_GS_BASE);
hsc->rflags = get_kernel_rflags();
if (pre) {
if (check_cs(hsc->cs)) {
hax_debug("CS does not pass the check.\n");
dump_segment(hsc->cs);
// check_panic();
}
if (check_stack_seg(hsc->ss)) {
hax_debug("SS does not pass the check.\n");
dump_segment(hsc->ss);
// check_panic();
}
if (check_fgs_seg(hsc->fs, 1)) {
hax_debug("FS does not pass the check.\n");
dump_segment(hsc->fs);
// check_panic();
}
if (check_fgs_seg(hsc->gs, 0)) {
hax_debug("GS does not pass the check.\n");
dump_segment(hsc->gs);
// check_panic();
}
if (check_data_seg(hsc->ds) || check_data_seg(hsc->es)) {
hax_debug("DS or ES does not pass the check.\n");
dump_segment(hsc->ds);
dump_segment(hsc->es);
// check_panic();
}
if (check_tr_seg(hsc->tr)) {
hax_debug("TR does not pass the check.\n");
dump_segment(hsc->tr);
// check_panic();
}
}
return 0;
}
static int dump_hsc_state(struct host_state_compare *hsc)
{
return 0;
}
int compare_host_state(struct vcpu_t *vcpu)
{
struct host_state_compare *pre, *post;
pre = &vcpu->hsc_pre;
post = &vcpu->hsc_post;
if (memcmp(pre, post, sizeof(struct host_state_compare))) {
hax_debug("The previous and next is not same.\n");
dump_hsc_state(pre);
dump_hsc_state(post);
check_panic();
}
return 0;
}
#endif
static int is_emt64_msr(uint64 entry)
{
int i = 0;
for (i = 0; i < NR_EMT64MSR; i++) {
if (entry == emt64_msr[i])
return 1;
}
return 0;
}
void save_guest_msr(struct vcpu_t *vcpu)
{
int i;
struct gstate *gstate = &vcpu->gstate;
for (i = 0; i < NR_GMSR; i++) {
gstate->gmsr[i].entry = gmsr_list[i];
if (cpu_has_emt64_support() || !is_emt64_msr(gmsr_list[i])) {
gstate->gmsr[i].value = ia32_rdmsr(gstate->gmsr[i].entry);
}
}
if (!hax->apm_version)
return;
// APM v1: save IA32_PMCx and IA32_PERFEVTSELx
for (i = 0; i < (int)hax->apm_general_count; i++) {
uint32 msr = (uint32)(IA32_PMC0 + i);
gstate->apm_pmc_msrs[i] = ia32_rdmsr(msr);
msr = (uint32)(IA32_PERFEVTSEL0 + i);
gstate->apm_pes_msrs[i] = ia32_rdmsr(msr);
}
}
void load_guest_msr(struct vcpu_t *vcpu)
{
int i;
struct gstate *gstate = &vcpu->gstate;
for (i = 0; i < NR_GMSR; i++) {
if (cpu_has_emt64_support() || !is_emt64_msr(gstate->gmsr[i].entry)) {
ia32_wrmsr(gstate->gmsr[i].entry, gstate->gmsr[i].value);
}
}
if (!hax->apm_version)
return;
// APM v1: restore IA32_PMCx and IA32_PERFEVTSELx
for (i = 0; i < (int)hax->apm_general_count; i++) {
uint32 msr = (uint32)(IA32_PMC0 + i);
ia32_wrmsr(msr, gstate->apm_pmc_msrs[i]);
msr = (uint32)(IA32_PERFEVTSEL0 + i);
ia32_wrmsr(msr, gstate->apm_pes_msrs[i]);
}
}
static void save_host_msr(struct vcpu_t *vcpu)
{
int i;
struct hstate *hstate = &get_cpu_data(vcpu->cpu_id)->hstate;
for (i = 0; i < NR_HMSR; i++) {
hstate->hmsr[i].entry = hmsr_list[i];
if (cpu_has_emt64_support() || !is_emt64_msr(hmsr_list[i])) {
hstate->hmsr[i].value = ia32_rdmsr(hstate->hmsr[i].entry);
}
}
if (!hax->apm_version)
return;
// APM v1: save IA32_PMCx and IA32_PERFEVTSELx
for (i = 0; i < (int)hax->apm_general_count; i++) {
uint32 msr = (uint32)(IA32_PMC0 + i);
hstate->apm_pmc_msrs[i] = ia32_rdmsr(msr);
msr = (uint32)(IA32_PERFEVTSEL0 + i);
hstate->apm_pes_msrs[i] = ia32_rdmsr(msr);
}
}
static void load_host_msr(struct vcpu_t *vcpu)
{
int i;
struct hstate *hstate = &get_cpu_data(vcpu->cpu_id)->hstate;
for (i = 0; i < NR_HMSR; i++) {
if (cpu_has_emt64_support() || !is_emt64_msr(hstate->hmsr[i].entry)) {
ia32_wrmsr(hstate->hmsr[i].entry, hstate->hmsr[i].value);
}
}
if (!hax->apm_version)
return;
// APM v1: restore IA32_PMCx and IA32_PERFEVTSELx
for (i = 0; i < (int)hax->apm_general_count; i++) {
uint32 msr = (uint32)(IA32_PMC0 + i);
ia32_wrmsr(msr, hstate->apm_pmc_msrs[i]);
msr = (uint32)(IA32_PERFEVTSEL0 + i);
ia32_wrmsr(msr, hstate->apm_pes_msrs[i]);