-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathptlxen-memory.cpp
More file actions
2084 lines (1766 loc) · 74.3 KB
/
ptlxen-memory.cpp
File metadata and controls
2084 lines (1766 loc) · 74.3 KB
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
//
// PTLsim: Cycle Accurate x86-64 Simulator
// Memory functions for Xen subsystem
//
// Copyright 2005-2008 Matt T. Yourst <yourst@yourst.com>
//
#include <globals.h>
#include <superstl.h>
#include <ptlxen.h>
#include <mm.h>
#include <ptlsim.h>
#include <stats.h>
//
// Debugging
//
void print_page_table_tree(ostream& os, mfn_t mfn, int level = 0, int maxlevel = 3) {
Level1PTE* ptes = (Level1PTE*)phys_to_mapped_virt(mfn << 12);
foreach (w, 2*level) os << " ";
W64 pfn = mfn_to_linear_pfn(mfn);
os << " L", (4-level), " mfn ", mfn, " (pfn ", pfn, "):", endl;
foreach (i, 512) {
Level1PTE pte = ptes[i];
if likely (!pte.p) continue;
W64 leafpfn = mfn_to_linear_pfn(pte.mfn);
foreach (w, level) os << " ";
os << " [ ", intstring(i, -3), " ] ", pte, " pfn ", intstring(leafpfn, 10), endl;
if unlikely ((level == 2) & Level2PTE(pte).psz) {
foreach (w, level) os << " ";
os << " (huge page)", endl;
} else if unlikely ((level == 0) && inrange(int(i), 256, 271)) {
os << " (hypervisor space)", endl;
} else if unlikely (level < maxlevel) {
print_page_table_tree(os, pte.mfn, level+1, maxlevel);
}
}
}
//
// PTLsim internal page table management
//
mmu_update_t mmuqueue[1024];
int mmuqueue_count = 0;
int do_commit_page_table_updates() {
static const bool DEBUG = 0;
if (DEBUG) logfile << "Page table update commit of ", mmuqueue_count, " entries:", endl, flush;
foreach (i, mmuqueue_count) {
mmu_update_t& mmu = mmuqueue[i];
W64 virt = mmu.ptr;
if likely (virt_is_inside_ptlsim(mmu.ptr)) {
mmu.ptr = ptl_virt_to_phys((void*)mmu.ptr);
} else if likely (virt_is_inside_physmap(mmu.ptr)) {
mmu.ptr = mapped_virt_to_phys((void*)mmu.ptr);
} else {
// invalid update
mmu.ptr = 0;
}
if (DEBUG) logfile << " virt 0x", hexstring(virt, 64), ", phys 0x", hexstring(mmu.ptr, 64), " (mfn ", intstring(mmu.ptr >> 12, 8),
" offset ", intstring(lowbits(mmu.ptr, 12) / 8, 8), ") <= ", Level1PTE(mmu.val), endl, flush;
}
int update_count = 0;
int rc = HYPERVISOR_mmu_update(mmuqueue, mmuqueue_count, &update_count, DOMID_SELF);
if unlikely (rc) {
logfile << "Page table update commit failed for ", mmuqueue_count, " entries (completed ", update_count, " entries):", endl, flush;
foreach (i, mmuqueue_count) {
logfile << " phys 0x", hexstring(mmuqueue[i].ptr, 64), " (mfn ", intstring(mmuqueue[i].ptr >> 12, 8),
" offset ", intstring(lowbits(mmuqueue[i].ptr, 12) / 8, 8), ") <= ", Level1PTE(mmuqueue[i].val), endl, flush;
}
}
mmuqueue_count = 0;
return rc;
}
// Update a PTE by its physical address
template <typename T>
int update_phys_pte(Waddr dest, const T& src) {
mmu_update_t u;
u.ptr = dest;
u.val = (W64)src;
return HYPERVISOR_mmu_update(&u, 1, NULL, DOMID_SELF);
}
template int update_phys_pte(Waddr dest, const Level1PTE& src);
template int update_phys_pte(Waddr dest, const Level2PTE& src);
template int update_phys_pte(Waddr dest, const Level3PTE& src);
template int update_phys_pte(Waddr dest, const Level4PTE& src);
int update_ptl_virt(void* ptr, const Level1PTE& pte) {
return HYPERVISOR_update_va_mapping(Waddr(ptr), pte, UVMF_INVLPG);
}
int invalidate_ptl_virt(void* ptr) {
mmuext_op op;
op.cmd = MMUEXT_INVLPG_LOCAL;
op.arg1.linear_addr = Waddr(ptr);
op.arg2.nr_ents = 1;
int success_count = 0;
return HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF);
}
int flush_tlb() {
mmuext_op op;
op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
op.arg1.linear_addr = 0;
op.arg2.nr_ents = 0;
int success_count = 0;
return HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF);
}
int flush_cache() {
PTLsimHostCall call;
call.op = PTLSIM_HOST_FLUSH_CACHE;
call.ready = 0;
return synchronous_host_call(call);
}
int pin_page_table_page(void* virt, int level) {
return 0;
assert(inrange(level, 0, 4));
// Was it in PTLsim space?
mfn_t mfn = ptl_virt_to_mfn(virt);
if unlikely (mfn == INVALID_MFN) return -1;
int level_to_function[5] = {MMUEXT_UNPIN_TABLE, MMUEXT_PIN_L1_TABLE, MMUEXT_PIN_L2_TABLE, MMUEXT_PIN_L3_TABLE, MMUEXT_PIN_L4_TABLE};
int func = level_to_function[level];
int rc = 0;
mmuext_op op;
op.cmd = func;
op.arg1.mfn = mfn;
int success_count = 0;
return HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF);
}
int query_pages(page_type_t* pt, int count) {
mmuext_op op;
op.cmd = MMUEXT_QUERY_PAGES;
op.arg1.linear_addr = (Waddr)pt;
op.arg2.nr_ents = count;
int success_count = 0;
return HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF);
}
page_type_t query_page(mfn_t mfn) {
unmap_phys_page(mfn);
page_type_t pt;
pt.in.mfn = mfn;
mmuext_op op;
op.cmd = MMUEXT_QUERY_PAGES;
op.arg1.linear_addr = (Waddr)&pt;
op.arg2.nr_ents = 1;
int success_count = 0;
assert(HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF) == 0);
return pt;
}
//
// Physical memory map (physmap)
//
//
// Dummy page for speculative faults: this page of all zeros
// is mapped in whenever we try to access physical memory
// that doesn't exist, isn't ours, or is part of Xen itself.
//
void* zeropage;
Level4PTE ptlsim_pml4_entry;
Level4PTE physmap_pml4_entry;
struct Level1PTE* phys_pagedir;
struct Level2PTE* phys_level2_pagedir;
struct Level3PTE* phys_level3_pagedir;
mfn_t phys_level3_pagedir_mfn;
W64 phys_pagedir_mfn_count;
//
// Reverse mapping from sim MFNs to host MFNs
//
W64 sim_mfn_to_host_mfn_map_size = 0;
W32* sim_mfn_to_host_mfn_map = null;
//
// Build page tables for the 1:1 mapping of physical memory.
//
// Since we don't know which pages a domain can access until later,
// and the accessibility may change at any time, we only build levels
// L2 and L3, but leave L1 to be constructed on demand (we still do
// allocate L1, we just don't fill it).
//
// On return, PML4 slot 508 (0xfffffe0000000000) should be set to
// ptl_virt_to_mfn(phys_level3_pagedir).
//
void build_physmap_page_tables() {
static const bool DEBUG = 0;
if (DEBUG) cerr << "Building physical page map for ", bootinfo.total_machine_pages, " pages (",
(pages_to_kb(bootinfo.total_machine_pages) / 1024), " MB)", " of memory:", endl, flush;
zeropage = ptl_mm_alloc_private_page();
ptl_mm_zero_private_page(zeropage);
Waddr physmap_level1_page_count = ceil(bootinfo.total_machine_pages, PTES_PER_PAGE) / PTES_PER_PAGE;
phys_pagedir = (Level1PTE*)ptl_mm_alloc_private_pages(physmap_level1_page_count * PAGE_SIZE);
memset(phys_pagedir, 0, physmap_level1_page_count * PAGE_SIZE);
if (DEBUG) cerr << " L1 page table at virt ", (void*)phys_pagedir, " (", bootinfo.total_machine_pages, " entries, ",
physmap_level1_page_count, " pages, ", (bootinfo.total_machine_pages * sizeof(Level1PTE)), " bytes)", endl, flush;
//
// Construct L2 page tables, pointing to fill-on-demand L1 tables:
//
Waddr physmap_level2_page_count = ceil(physmap_level1_page_count, PTES_PER_PAGE) / PTES_PER_PAGE;
phys_level2_pagedir = (Level2PTE*)ptl_mm_try_alloc_private_pages(physmap_level2_page_count * PAGE_SIZE);
if (!phys_level2_pagedir) {
cerr << "phys_level2_pagedir not allocated", endl, flush;
ptl_mm_dump(cerr);
ptl_mm_validate();
}
if (DEBUG) cerr << " L2 page table at virt ", (void*)phys_level2_pagedir, " (", physmap_level1_page_count, " entries, ",
physmap_level2_page_count, " pages, ", (physmap_level1_page_count * sizeof(Level1PTE)), " bytes)", endl, flush;
foreach (i, physmap_level1_page_count) {
Level2PTE& pte = phys_level2_pagedir[i];
pte = 0;
pte.p = 1; // let PTLsim fill it in on demand
pte.rw = 1; // sub-pages are writable unless overridden
pte.us = 1; // both user and supervisor (PTLsim itself will check protections)
pte.a = 1; // accessed
pte.mfn = ptl_virt_to_mfn(phys_pagedir + (i * PTES_PER_PAGE));
// cerr << " Slot ", intstring(i, 6), " = ", pte, endl, flush;
pte.p = 0;
}
// Clear out leftover slots: we may not care, but Xen will complain:
if ((physmap_level1_page_count & (PTES_PER_PAGE-1)) > 0) {
foreach (i, PTES_PER_PAGE - (physmap_level1_page_count & (PTES_PER_PAGE-1))) {
// cerr << " Slot ", intstring(physmap_level1_page_count + i, 6), " is left over", endl, flush;
phys_level2_pagedir[physmap_level1_page_count + i] = 0;
}
}
//
// Construct L3 page table (just one page covers 2^39 bit phys addr space):
//
// PTLmon has pre-allocated an mfn for this page and it is already mapped
// into every address space. Therefore, we need to do a batched update
// on the page.
//
assert(physmap_level2_page_count < PTES_PER_PAGE);
phys_level3_pagedir = (Level3PTE*)ptl_mm_alloc_private_page();
ptl_mm_zero_private_page(phys_level3_pagedir);
if (DEBUG) cerr << " L3 page table at mfn ", phys_level3_pagedir_mfn, ", virt ", (void*)phys_level3_pagedir, " (", physmap_level2_page_count, " entries, ",
1, " pages, ", (physmap_level2_page_count * sizeof(Level1PTE)), " bytes)", endl, flush;
foreach (i, physmap_level2_page_count) {
Level3PTE& pte = phys_level3_pagedir[i];
Level3PTE newpte = 0;
newpte.p = 1; // pre-filled
newpte.rw = 1; // sub-pages are writable unless overridden
newpte.us = 1; // both user and supervisor (PTLsim itself will check protections)
newpte.a = 1; // accessed
// Link back to L2 tables:
Level2PTE* ptvirt = phys_level2_pagedir + (i * PTES_PER_PAGE);
newpte.mfn = ptl_virt_to_mfn(ptvirt);
// cerr << " Slot ", intstring(i, 6), " = ", pte, endl, flush;
assert(make_ptl_page_writable(ptvirt, false) == 0);
pte = newpte;
}
//
// Remap and pin L3 page
//
if (DEBUG) cerr << " Final L3 page table page at virt ", phys_level3_pagedir,
" (mfn ", ptl_virt_to_mfn(phys_level3_pagedir), ")", endl, flush;
assert(make_ptl_page_writable(phys_level3_pagedir, false) == 0);
//
// Build PTLsim PML4 entry 510:
//
ptlsim_pml4_entry = 0;
ptlsim_pml4_entry.p = 1;
ptlsim_pml4_entry.rw = 1;
ptlsim_pml4_entry.us = 1;
ptlsim_pml4_entry.a = 1;
ptlsim_pml4_entry.mfn = ptl_virt_to_mfn(bootinfo.ptl_level3_map);
//
// Build physmap PML4 entry 508:
//
physmap_pml4_entry = 0;
physmap_pml4_entry.p = 1;
physmap_pml4_entry.rw = 1;
physmap_pml4_entry.us = 1;
physmap_pml4_entry.a = 1;
physmap_pml4_entry.mfn = ptl_virt_to_mfn(phys_level3_pagedir);
//
// Inject the physmap entry into the current page table,
// which must be PTLsim's initial page table:
//
mfn_t top_mfn = bootinfo.toplevel_page_table_mfn;
assert(top_mfn == get_cr3_mfn());
int physmap_slot = VirtAddr(PHYS_VIRT_BASE).lm.level4;
assert(update_phys_pte_mfn_and_slot(top_mfn, physmap_slot, physmap_pml4_entry) == 0);
//
// Build P2M (sim to host) MFN mapping table
//
sim_mfn_to_host_mfn_map_size = bootinfo.max_pages;
sim_mfn_to_host_mfn_map = (W32*)ptl_mm_alloc_private_pages(bootinfo.max_pages * sizeof(W32));
foreach (i, sim_mfn_to_host_mfn_map_size) {
sim_mfn_to_host_mfn_map[i] = 0xffffffff;
}
}
#define DETERMINISTIC_PHYSADDRS
//
// Take a host MFN that varies between domain invocations
// depending on the random pages assigned to the domain,
// and convert it to a fully deterministic simulation MFN
// that's used in all other parts of PTLsim. This enables
// repeatable cache behavior modeling and so on.
//
// Simulation MFNs are always used by Context.* methods
// and by loadphys() and storemask().
//
W64 host_mfn_to_sim_mfn(W64 hostmfn) {
#ifdef DETERMINISTIC_PHYSADDRS
if unlikely (hostmfn == bootinfo.shared_info_mfn) {
return (PHYSADDR_TYPE_SHINFO << PHYSADDR_TYPE_MFN_SHIFT);
}
W64 simmfn = mfn_to_linear_pfn(hostmfn);
// SD: If simmfn was not found, try RAW mapping.
// NOTE: This is scary, and would perhaps need more assertions and checks!
if unlikely ( (simmfn == INVALID_MFN) && (hostmfn <= bootinfo.total_machine_pages)) {
logfile << "host_mfn_to_sim_mfn(", hostmfn, " vs total machine pages ", bootinfo.total_machine_pages,
"): m2p sim mfn is out of range (", simmfn, " vs limit ", sim_mfn_to_host_mfn_map_size, "); caller ",
getcaller(), " at cycle ", sim_cycle, "=> Assuming RAW mapping for granted pages.", endl;
return (PHYSADDR_TYPE_RAW << PHYSADDR_TYPE_MFN_SHIFT) | hostmfn;
}
if unlikely (simmfn >= sim_mfn_to_host_mfn_map_size) {
logfile << "host_mfn_to_sim_mfn(", hostmfn, " vs total machine pages ", bootinfo.total_machine_pages,
"): m2p sim mfn is out of range (", simmfn, " vs limit ", sim_mfn_to_host_mfn_map_size, "); caller ",
getcaller(), " at cycle ", sim_cycle, endl;
#if(0)
// SD: This is a costly check to perhaps find the original mapping!
foreach (i, sim_mfn_to_host_mfn_map_size) {
if (sim_mfn_to_host_mfn_map[i] == hostmfn)
logfile << "Found reverse matching mapping: sim_mfn_to_host_mfn_map[", i, ",] == ", hostmfn,endl;
}
#endif
return INVALID_MFN;
}
sim_mfn_to_host_mfn_map[simmfn] = hostmfn;
return (PHYSADDR_TYPE_DRAM << PHYSADDR_TYPE_MFN_SHIFT) | simmfn;
// return (PHYSADDR_TYPE_DRAM << PHYSADDR_TYPE_SHIFT) | (hostmfn ^ 0xffffff);
#else
return (PHYSADDR_TYPE_DRAM << PHYSADDR_TYPE_MFN_SHIFT) | hostmfn;
#endif
}
//
// Turn a simulation MFN back into a host (physical) MFN.
// This does the inverse transform of host_mfn_to_sim_mfn().
//
W64 sim_mfn_to_host_mfn(W64 simmfn) {
W64 orig_simmfn = simmfn;
#ifdef DETERMINISTIC_PHYSADDRS
//int type = (simmfn >> (PHYSADDR_TYPE_SHIFT-12));
//simmfn = lowbits(simmfn, (PHYSADDR_TYPE_SHIFT-12));
int type = (simmfn >> PHYSADDR_TYPE_MFN_SHIFT);
simmfn = lowbits(simmfn, PHYSADDR_TYPE_MFN_SHIFT);
if unlikely (type == PHYSADDR_TYPE_SHINFO) {
return ptl_virt_to_phys(&sshinfo) >> 12;
}
if (type != PHYSADDR_TYPE_DRAM) logfile << "sim_mfn_to_host_mfn(", orig_simmfn, "): simmfn = ", simmfn, ", type = ", type, ", caller ", getcaller(), endl;
assert(type == PHYSADDR_TYPE_DRAM);
if unlikely (simmfn >= sim_mfn_to_host_mfn_map_size) {
logfile << "sim_mfn_to_host_mfn(", simmfn, "): p2m sim mfn is out of range (", simmfn, " vs limit ", sim_mfn_to_host_mfn_map_size, "); caller ", getcaller(), endl;
assert(false);
}
W64 hostmfn = sim_mfn_to_host_mfn_map[simmfn];
if unlikely (hostmfn >= bootinfo.total_machine_pages) {
logfile << "sim_mfn_to_host_mfn(", simmfn, "): reverse mapping from sim mfn ", simmfn, " to host mfn ", hostmfn, " is unknown; caller ", getcaller(), endl;
assert(false);
}
// W64 hostmfn = simmfn ^ 0xffffff;
// W64 hostmfn = simmfn;
return hostmfn;
// return mfn_to_linear_pfn(xenmfn);
//++MTY TODO
#else
int type = (simmfn >> PHYSADDR_TYPE_MFN_SHIFT);
simmfn = lowbits(simmfn, PHYSADDR_TYPE_MFN_SHIFT);
if (type != PHYSADDR_TYPE_DRAM) logfile << "sim_mfn_to_host_mfn(", orig_simmfn, "): simmfn = ", simmfn, ", type = ", type, ", caller ", getcaller(), endl;
assert(type == PHYSADDR_TYPE_DRAM);
return simmfn;
#endif
}
//
// Page fault handling logic:
//
// By default, PTLsim maps physical pages as writable the first time
// they are referenced. Since we call unmap_address_space() before
// passing through any hypercalls that could collide with our now
// removed writable mappings, this is not a problem.
//
// If Xen refuses to update the physmap PTE with a writable mapping,
// this means some live page table is pinning it to read-only. In
// this case, for loads at least, we simply make it a read only
// mapping, which is always allowed.
//
// When we attempt to commit user stores, we check the pte.rw
// bit for the mapped page and if it's 0, we let Xen validate
// and commit the store for us.
//
//
// Map physical pages, building page tables as we go
//
static const bool force_page_fault_logging = 0;
bool force_readonly_physmap = 0;
Waddr last_virtaddr_triggering_walk = 0;
Waddr last_guest_rip_triggering_walk = 0;
Waddr last_guest_uuid_triggering_walk = 0;
int map_phys_page(mfn_t mfn, Waddr rip) {
int level2_slot_index = mfn / PTES_PER_PAGE;
W64 faultaddr = mfn << 12;
Level2PTE& l2pte = phys_level2_pagedir[level2_slot_index];
Level1PTE& l1pte = phys_pagedir[mfn];
if unlikely (!l2pte.p) {
//
// Level 2 PTE was not present: either this is the first
// access or it was fast cleared by unmap_address_space().
// In any case, re-establish it after clearing any old
// PTEs from the corresponding L1 page.
//
Level1PTE* l1page = floorptr(&l1pte, PAGE_SIZE);
assert(make_ptl_page_writable(l1page, 1) == 0);
ptl_mm_zero_private_page(l1page);
assert(make_ptl_page_writable(l1page, 0) == 0);
assert(update_ptl_pte(l2pte, l2pte.P(1)) == 0);
if (logable(9) | force_page_fault_logging) {
logfile << "[PTLsim Page Fault Handler from rip ", (void*)rip, "] ",
(void*)faultaddr, ": added L2 PTE slot ", level2_slot_index, " (L1 mfn ",
l2pte.mfn, ") to PTLsim physmap; toplevel cr3 mfn ", get_cr3_mfn(), endl;
}
}
//
// Page was not present: try to map the page read-write
//
Level1PTE pte = 0;
pte.p = 1;
pte.rw = 1;
pte.us = 1;
pte.mfn = mfn;
int rc = (force_readonly_physmap) ? -EINVAL : update_ptl_pte(l1pte, pte);
if unlikely (rc) {
//
// It's a special page and must be marked read-only:
//
pte.rw = 0;
rc = update_ptl_pte(l1pte, pte);
if unlikely (rc) {
//
// We still can't map the page! Most likely we got here after
// attempting to follow a virtaddr in the Xen reserved area,
// and we can't map some Xen-internal page table page that
// the native processor can see but the domain cannot.
//
// This can happen on speculative out-of-order accesses
// that never make it to the architectural state but
// nonetheless still must have *some* page to access
// or PTLsim will deadlock.
//
// Map in a zero page (to terminate all page table walks)
// and print a warning in the log. This is the same
// behavior as if invalid physical memory were accessed
// (but in that case the page is all 1's, not all 0's).
//
if unlikely (logable(2) | force_page_fault_logging) {
logfile << "[PTLsim Page Fault Handler from rip ", (void*)rip, "] ",
(void*)faultaddr, ": added dummy zero PTE for guest mfn ", mfn,
" (", sim_cycle, " cycles, ", total_user_insns_committed, " commits)", endl;
logfile << "Warning: failed to map mfn ", mfn, " (for virt ", (void*)faultaddr, ", requested by rip ", (void*)rip, " at cycle ", sim_cycle, ")", endl;
logfile << " Either it doesn't exist, isn't ours, or is part of Xen itself.", endl;
logfile << " Mapping a zero page in its place and hoping the access is speculative.", endl;
logfile << " The last page table walk was for virtual address ", (void*)last_virtaddr_triggering_walk, endl;
logfile << flush;
}
pte.mfn = ptl_virt_to_mfn(zeropage);
rc = update_ptl_pte(l1pte, pte);
assert(rc == 0);
return 3;
} else {
if unlikely (logable(9) | force_page_fault_logging) {
logfile << "[PTLsim Page Fault Handler from rip ", (void*)rip, "] ",
(void*)faultaddr, ": added read-only L1 PTE for guest mfn ", mfn,
" (", sim_cycle, " cycles, ", total_user_insns_committed, " commits)", endl;
}
return 2;
}
} else {
if unlikely (logable(9) | force_page_fault_logging) {
logfile << "[PTLsim Page Fault Handler from rip ", (void*)rip,
"] ", (void*)faultaddr, ": added L1 PTE for guest mfn ", mfn,
", toplevel cr3 mfn ", get_cr3_mfn(), " (", sim_cycle, " cycles, ", total_user_insns_committed, " commits)", endl;
}
return 1;
}
return 0;
}
void unmap_phys_page(mfn_t mfn) {
update_ptl_virt(phys_to_mapped_virt(mfn << 12), Level1PTE(0));
/*
Level1PTE& pte = phys_pagedir[mfn];
if unlikely (!pte.p) return;
assert(update_ptl_pte(pte, Level1PTE(0)) == 0);
*/
}
//
// Unmap an entire tree of physical pages rooted
// at the specified L4 mfn. This must be done
// before passing a pin hypercall or new_baseptr
// hypercall up to Xen. We may have read/write
// refs to some of these pages, which are currently
// normal pages (updated by the guest kernel) but
// which will become read-only page table pages
// once Xen tries to pin the entire tree. We only
// need to unmap L4/L3/L2 pages; L1 pages (i.e.
// the actual data pages) are not relevant.
//
// Only those pages with read/write mappings are
// unmapped. Levels 4/3/2 of the page table are
// recursively traversed and unmapped from the leaves
// on up, so we do not accidentally touch a page and
// re-map it on our way back to the root.
//
static const bool debug_unmap_phys_page_tree = 1;
inline void unmap_level1_page_tree(mfn_t mfn) {
// No need to unmap actual leaf physical pages - those are just data pages
Level1PTE& physpte = phys_pagedir[mfn];
if unlikely (debug_unmap_phys_page_tree & logable(100)) logfile << " L1: mfn ", intstring(mfn, 8), ((physpte.p & physpte.rw) ? " (unmap)" : ""), endl;
if unlikely (physpte.p & physpte.rw) physpte <= physpte.P(0);
}
inline void unmap_level2_page_tree(mfn_t mfn) {
Level2PTE* ptes = (Level2PTE*)phys_to_mapped_virt(mfn << 12);
foreach (i, PTES_PER_PAGE) if unlikely (ptes[i].p) unmap_level1_page_tree(ptes[i].mfn);
Level1PTE& physpte = phys_pagedir[mfn];
if unlikely (debug_unmap_phys_page_tree & logable(100)) logfile << " L2: mfn ", intstring(mfn, 8), ((physpte.p & physpte.rw) ? " (unmap)" : ""), endl;
if unlikely (physpte.p & physpte.rw) physpte <= physpte.P(0);
}
void unmap_level3_page_tree(mfn_t mfn) {
Level3PTE* ptes = (Level3PTE*)phys_to_mapped_virt(mfn << 12);
foreach (i, PTES_PER_PAGE) if unlikely (ptes[i].p) unmap_level2_page_tree(ptes[i].mfn);
Level1PTE& physpte = phys_pagedir[mfn];
if unlikely (debug_unmap_phys_page_tree & logable(100)) logfile << " L3: mfn ", intstring(mfn, 8), ((physpte.p & physpte.rw) ? " (unmap)" : ""), endl;
if unlikely (physpte.p & physpte.rw) physpte <= physpte.P(0);
}
void unmap_level4_page_tree(mfn_t mfn) {
Level4PTE* ptes = (Level4PTE*)phys_to_mapped_virt(mfn << 12);
foreach (i, PTES_PER_PAGE) if unlikely (ptes[i].p) unmap_level3_page_tree(ptes[i].mfn);
Level1PTE& physpte = phys_pagedir[mfn];
if unlikely (debug_unmap_phys_page_tree & logable(100)) logfile << " L4: mfn ", intstring(mfn, 8), ((physpte.p & physpte.rw) ? " (unmap)" : ""), endl;
if unlikely (physpte.p & physpte.rw) physpte <= physpte.P(0);
}
void unmap_phys_page_tree(mfn_t root) {
if (logable(100)) logfile << "Unmapping page tree starting at root mfn ", root, endl;
unmap_level4_page_tree(root);
commit_page_table_updates();
}
//
// This is required before switching back to native mode, since we may have
// read/write maps of pages that the guest kernel thinks are read-only
// everywhere; this will cause later pin operations to fail.
//
// We scan the physmap L2 page table, looking for L1 pages that were filled
// in on demand by PTLsim's page fault handler. If the present bit was set,
// we first clear the L2 PTE's present bit, then unpin the L1 page.
//
void unmap_address_space() {
Waddr physmap_level1_pages = ceil(bootinfo.total_machine_pages, PTES_PER_PAGE) / PTES_PER_PAGE;
int n = 0;
if (logable(100)) logfile << "unmap_address_space: check ", physmap_level1_pages, " PTEs:", endl, flush;
foreach (i, physmap_level1_pages) {
Level2PTE& l2pte = phys_level2_pagedir[i];
if unlikely (l2pte.p) {
l2pte <= l2pte.P(0);
if (logable(100)) logfile << " update ", intstring(n, 6), ": pte ", intstring(i, 6), " <= not present", endl;
n++;
}
}
commit_page_table_updates();
}
//
// Xen puts the virtualized page fault virtual address in arch.cr2
// instead of the machine's CR2 register.
//
static inline Waddr read_cr2() { return shinfo.vcpu_info[0].arch.cr2; }
static int page_fault_in_progress = 0;
asmlinkage void do_page_fault(W64* regs) {
int rc;
Waddr faultaddr = read_cr2();
mfn_t toplevel = get_cr3_mfn();
//
// If we are already handling a page fault, and got another one
// that means we faulted in pagetable walk. Continuing here would cause
// a recursive fault.
//
PageFaultErrorCode pfec = regs[REG_ar1];
if unlikely (page_fault_in_progress) {
cerr << "PTLsim Internal Error: recursive page fault @ rip ", (void*)regs[REG_rip], " while accessing ", (void*)faultaddr, " (error code ", pfec, ")", endl, flush;
cerr << "Registers:", endl;
print_regs(cerr, regs);
print_stack(cerr, regs[REG_rsp]);
cerr.flush();
logfile.flush();
shutdown(SHUTDOWN_crash);
}
page_fault_in_progress = 1;
if likely (inrange(faultaddr, PHYS_VIRT_BASE, (PHYS_VIRT_BASE + ((Waddr)bootinfo.total_machine_pages * PAGE_SIZE) - 1))) {
mfn_t mfn = (faultaddr - (Waddr)PHYS_VIRT_BASE) >> 12;
map_phys_page(mfn, regs[REG_rip]);
} else {
cerr << "PTLsim Internal Error: page fault @ rip ", (void*)regs[REG_rip], " while accessing ", (void*)faultaddr, " (error code ", pfec, "); rsp ", get_rsp(), endl;
cerr << "Registers:", endl;
print_regs(cerr, regs);
print_stack(cerr, regs[REG_rsp]);
cerr.flush();
if (logfile) {
logfile << "Cycle: ", sim_cycle, " PTLsim Internal Error: page fault @ rip ", (void*)regs[REG_rip], " while accessing ", (void*)faultaddr, " (error code ", pfec, "); rsp ", get_rsp(), endl;
logfile << "Registers:", endl;
print_regs(logfile, regs);
print_stack(logfile, regs[REG_rsp]);
PTLsimMachine* machine = PTLsimMachine::getcurrent();
if (machine) machine->dump_state(logfile); else logfile << "(No current machine model)", endl;
logfile.close();
}
logfile.flush();
logfile.close();
shutdown(SHUTDOWN_crash);
asm("ud2a");
}
page_fault_in_progress = 0;
}
ostream& operator <<(ostream& os, const page_type_t& pagetype) {
static const char* page_type_names[] = {"none", "L1", "L2", "L3", "L4", "GDT", "LDT", "write"};
const char* page_type_name =
(pagetype.out.type == PAGE_TYPE_INVALID_MFN) ? "inv" :
(pagetype.out.type == PAGE_TYPE_INACCESSIBLE) ? "inacc" :
(pagetype.out.type < lengthof(page_type_names)) ? page_type_names[pagetype.out.type] :
"???";
os << padstring(page_type_name, -5), " ", (pagetype.out.pinned ? "pin" : " "), " ",
intstring(pagetype.out.total_count, 5), " total, ", intstring(pagetype.out.type_count, 5), " by type";
return os;
}
//
// Debugging helper function to track down stray refs to a page
//
void find_all_mappings_of_mfn(mfn_t mfn) {
// Start with an empty mapping
unmap_address_space();
int pagetype_bytes_allocated = bootinfo.total_machine_pages * sizeof(page_type_t);
page_type_t* pagetypes = (page_type_t*)ptl_mm_alloc_private_pages(pagetype_bytes_allocated);
assert(pagetypes);
foreach (i, bootinfo.total_machine_pages) {
pagetypes[i].in.mfn = i;
}
logfile << "Finding all mappings of mfn ", mfn, ":", endl, flush;
int rc = query_pages(pagetypes, bootinfo.total_machine_pages);
logfile << "rc = ", rc, endl, flush;
force_readonly_physmap = 1;
foreach (i, bootinfo.total_machine_pages) {
const page_type_t& pt = pagetypes[i];
if (pt.out.type == PAGE_TYPE_INACCESSIBLE) continue;
if (inrange(pt.out.type, (byte)PAGE_TYPE_L1, (byte)PAGE_TYPE_L4)) {
const Level1PTE* pte = (const Level1PTE*)phys_to_mapped_virt(i << 12);
foreach (j, PTES_PER_PAGE) {
if (pte->mfn == mfn) {
logfile << " Page table page mfn ", intstring(i, 6), " index ", intstring(j, 3), " references target mfn ", intstring(mfn, 6), ": ", *pte, endl;
}
pte++;
}
}
}
ptl_mm_free_private_pages(pagetypes, pagetype_bytes_allocated);
force_readonly_physmap = 0;
unmap_address_space();
}
//
// Page table control
//
//
// Set the real page table on the PTLsim primary VCPU.
//
// Inject the PTLsim toplevel page table entries (PML 510 and PML 508)
// into the specified user mfn before switching, so as to ensure a
// seamless transition. The page must already be pinned.
//
void inject_ptlsim_into_toplevel(mfn_t mfn) {
int ptlsim_slot = VirtAddr(PTLSIM_VIRT_BASE).lm.level4;
int physmap_slot = VirtAddr(PHYS_VIRT_BASE).lm.level4;
assert(update_phys_pte_mfn_and_slot(mfn, ptlsim_slot, ptlsim_pml4_entry) == 0);
assert(update_phys_pte_mfn_and_slot(mfn, physmap_slot, physmap_pml4_entry) == 0);
}
void switch_page_table(mfn_t mfn) {
unmap_phys_page(mfn);
inject_ptlsim_into_toplevel(mfn);
mmuext_op op;
op.cmd = MMUEXT_NEW_BASEPTR;
op.arg1.mfn = mfn;
int success_count = 0;
assert(HYPERVISOR_mmuext_op(&op, 1, &success_count, DOMID_SELF) == 0);
}
ostream& print_page_table_with_types(ostream& os, Level1PTE* ptes) {
page_type_t pagetypes[512];
foreach (i, 512) {
pagetypes[i].in.mfn = ptes[i].mfn;
}
assert(query_pages(pagetypes, lengthof(pagetypes)) == 0);
foreach (i, 512) {
os << " ", intstring(i, 3), ": ", ptes[i], " type ", pagetypes[i], endl;
}
return os;
}
//
// Page Table Walks
//
Level1PTE Context::virt_to_host_pte(W64 rawvirt) {
bool DEBUG = 0; // (signext64(floor(rawvirt, 4096), 48) == 0xffffffffff5f9000);
int slot = lowbits(rawvirt >> 12, log2(PTE_CACHE_SIZE));
if unlikely (cached_pte_virt[slot] != floor(rawvirt, PAGE_SIZE)) {
cached_pte_virt[slot] = floor(rawvirt, PAGE_SIZE);
if (DEBUG) logfile << "Doing page table walk for ", (void*)rawvirt, " into slot ", slot, " @ cycle ", sim_cycle, endl;
cached_pte[slot] = page_table_walk(rawvirt, cr3 >> 12);
if (DEBUG) logfile << "Result of page table walk for ", (void*)rawvirt, " into slot ", slot, " = ", cached_pte[slot], " @ cycle ", sim_cycle, endl;
} else {
if (DEBUG) logfile << "Already have slot ", slot, " for virt ", (void*)rawvirt, ": ", cached_pte[slot], " @ cycle ", sim_cycle, endl;
}
return cached_pte[slot];
}
Level1PTE Context::virt_to_pte(W64 rawvirt) {
Level1PTE pte = virt_to_host_pte(rawvirt);
if (!pte.p) return pte;
mfn_t simmfn = host_mfn_to_sim_mfn(pte.mfn);
pte.mfn = simmfn;
if unlikely (simmfn == INVALID_MFN) {
logfile << "virt_to_pte(", (void*)rawvirt, ") on vcpu ", vcpuid, ": pte ", pte, " => invalid mfn ", simmfn, " (called from ", getcaller(), ")", endl;
logfile << "Mini-TLB:", endl;
foreach (i, lengthof(cached_pte_virt)) {
logfile << " 0x", hexstring(cached_pte_virt[i], 64), " => ", cached_pte[i], endl;
}
PTLsimMachine* machine = PTLsimMachine::getcurrent();
if (machine) machine->dump_state(logfile); else logfile << "(No current machine model)", endl;
if (vcpuid == 0)
assert(simmfn != INVALID_MFN);
else {
logfile << "Trying to fix up and continue with mfn 0", endl;
pte.mfn = 0;
}
}
return pte;
}
//
// Walk the page table tree, accumulating the relevant permissions
// as we go, according to x86 rules (specifically, p, rw, us, nx).
//
// The A (accessed) and D (dirty) bits in the returned PTE have
// special meaning. We do not actually update these bits unless
// the instruction causing the PT walk successfully commits.
// Therefore, if the returned A is *not* set, this means one or
// more PT levels need to have their A bits refreshed. If D is
// *not* set, AND the intended access is for a store, the D bits
// also need to be refreshed at the final PT level (level 2 or 1).
// This is done at commit time by page_table_acc_dirty_update().
//
Waddr xen_m2p_map_end;
Level1PTE page_table_walk(W64 rawvirt, W64 toplevel_mfn, bool do_special_translations) {
bool DEBUG = 0; // (signext64(floor(rawvirt, 4096), 48) == 0xffffffffff5f9000);
VirtAddr virt(rawvirt);
last_virtaddr_triggering_walk = rawvirt;
bool acc_bit_up_to_date = 0;
if unlikely ((rawvirt >= HYPERVISOR_VIRT_START) & (rawvirt < xen_m2p_map_end)) {
//
// The access is inside Xen's address space. Xen will not let us even access the
// page table entries it injects into every top-level page table page, and we
// cannot map M2P pages like we do other physical pages. Because Xen does not
// allow its internal page tables to be mapped by guests at all, we have to
// special-case these virtual addresses.
//
// We cheat by biasing the returned physical address such that we have
// (HYPERVISOR_VIRT_START - PHYS_VIRT_BASE) + PHYS_VIRT_BASE == HYPERVISOR_VIRT_START
// when other parts of PTLsim use ptl_phys_to_virt to access the memory.
//
const Waddr hypervisor_space_mask = (HYPERVISOR_VIRT_END - HYPERVISOR_VIRT_START)-1;
Waddr pseudo_phys = (HYPERVISOR_VIRT_START - PHYS_VIRT_BASE) + (rawvirt & hypervisor_space_mask);
Level1PTE pte = 0;
pte.mfn = pseudo_phys >> 12;
pte.p = 1;
pte.rw = 0;
pte.us = 1;
pte.a = 1; // don't try to update accessed bits again
pte.d = 0;
if unlikely (DEBUG) logfile << "Hypervisor space translation for virt ", (void*)rawvirt, " => ", pte, endl;
return pte;
}
if unlikely ((rawvirt >= PTLSIM_RESERVED_VIRT_BASE) & (rawvirt <= PTLSIM_RESERVED_VIRT_END)) {
// PTLsim space is inaccessible to the guest
Level1PTE pte = 0;
return pte;
if unlikely (DEBUG) logfile << "PTLsim space translation for virt ", (void*)rawvirt, " => ", pte, endl;
}
Level4PTE& level4 = ((Level4PTE*)phys_to_mapped_virt(toplevel_mfn << 12))[virt.lm.level4];
Level1PTE final = (W64)level4;
if unlikely (DEBUG) logfile << "level4 PTE = ", level4, ", final = ", final, endl;
if unlikely (!level4.p) return final;
acc_bit_up_to_date = level4.a;
Level3PTE& level3 = ((Level3PTE*)phys_to_mapped_virt(level4.mfn << 12))[virt.lm.level3];
final.accum(level3);
if unlikely (DEBUG) logfile << "level3 PTE = ", level3, ", final = ", final, endl;
if unlikely (!level3.p) return final;
acc_bit_up_to_date &= level3.a;
Level2PTE& level2 = ((Level2PTE*)phys_to_mapped_virt(level3.mfn << 12))[virt.lm.level2];
final.accum(level2);
if unlikely (DEBUG) logfile << "level2 PTE = ", level2, ", final = ", final, endl;
if (unlikely(!level2.p)) return final;
acc_bit_up_to_date &= level2.a;
if unlikely (level2.psz) {
final.mfn = level2.mfn;
final.pwt = level2.pwt;
final.pcd = level2.pcd;
acc_bit_up_to_date &= level2.a;
final.a = acc_bit_up_to_date;
final.d = level2.d;
if unlikely (DEBUG) logfile << "level2 PTE = ", level2, ", final = ", final, ", page size set", endl;
return final;
}
Level1PTE& level1 = ((Level1PTE*)phys_to_mapped_virt(level2.mfn << 12))[virt.lm.level1];
final.accum(level1);
if unlikely (!level1.p) {
if unlikely (DEBUG) logfile << "level1 PTE = ", level1, ", final = ", final, ", not present", endl;
return final;
}
acc_bit_up_to_date &= level1.a;
final.mfn = level1.mfn;
final.g = level1.g;
final.pat = level1.pat;
final.pwt = level1.pwt;
final.pcd = level1.pcd;
final.a = acc_bit_up_to_date;
final.d = level1.d;
#if 0
if unlikely ((final.mfn == bootinfo.shared_info_mfn) && do_special_translations) {
final.mfn = (Waddr)ptl_virt_to_phys(&sshinfo) >> 12;
}
#endif
if unlikely (DEBUG) logfile << "level1 PTE = ", level1, ", final = ", final, ", returning", " @ cycle ", sim_cycle, endl;
return final;
}
//
// Walk the page table, but return the physical address of the PTE itself
// that maps the specified virtual address