-
Notifications
You must be signed in to change notification settings - Fork 12
/
mmu.c
2651 lines (2337 loc) · 76.5 KB
/
mmu.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
/*===- mmu.c - SVA Execution Engine =-------------------------------------===
*
* Secure Virtual Architecture
*
* This file was developed by the LLVM research group and is distributed under
* the University of Illinois Open Source License. See LICENSE.TXT for details.
*
*===----------------------------------------------------------------------===
*
* Note: We try to use the term "frame" to refer to a page of physical memory
* and a "page" to refer to the virtual addresses mapped to the page of
* physical memory.
*
*===----------------------------------------------------------------------===
*/
#include <string.h>
#include <sys/types.h>
#include "sva/callbacks.h"
#include "sva/config.h"
#include "sva/mmu.h"
#include "sva/mmu_intrinsics.h"
#include "sva/stack.h"
#include "sva/state.h"
#include "sva/svamem.h"
#include "sva/util.h"
#include "sva/x86.h"
/*
* Defines for #if #endif blocks for commenting out lines of code
*/
/* Used to denote unimplemented code */
#define NOT_YET_IMPLEMENTED 0
/* Used to denote obsolete code that hasn't been deleted yet */
#define OBSOLETE 0
/* Define whether to enable DEBUG blocks #if statements */
#define DEBUG 0
/* Define whether or not the mmu_init code assumes virtual addresses */
#define USE_VIRT 0
/* Temporary debug macro */
#undef NKDEBUG /* undef it, just in case */
#define NKDEBUG(fname, fmt, args...) \
printf("___Nested Kernel___ <%s> ", #fname); \
printf(fmt, ## args); \
printf("\n")
#undef NKDEBUGG
#define NKDEBUGG(fname, fmt, args...) /* nothing: it's a placeholder */
/*
*****************************************************************************
* Function prototype declarations.
*****************************************************************************
*/
/*
* Function prototypes for finding the virtual address of page table components
*/
static inline page_entry_t * get_pgeVaddr (uintptr_t vaddr);
static inline pml4e_t * get_pml4eVaddr (unsigned char * cr3, uintptr_t vaddr);
static inline pdpte_t * get_pdpteVaddr (pml4e_t * pml4e, uintptr_t vaddr);
static inline pde_t * get_pdeVaddr (pdpte_t * pdpte, uintptr_t vaddr);
static inline pte_t * get_pteVaddr (pde_t * pde, uintptr_t vaddr);
/*
* Function prototypes for returning the physical address of page table pages.
*/
static inline uintptr_t get_pml4ePaddr (unsigned char * cr3, uintptr_t vaddr);
static inline uintptr_t get_pdptePaddr (pml4e_t * pml4e, uintptr_t vaddr);
static inline uintptr_t get_pdePaddr (pdpte_t * pdpte, uintptr_t vaddr);
static inline uintptr_t get_ptePaddr (pde_t * pde, uintptr_t vaddr);
/*
* Private local mapping update function prototypes.
*/
static inline void __update_mapping (pte_t * pageEntryPtr, page_entry_t val);
/*
*****************************************************************************
* Define paging structures and related constants local to this source file
*****************************************************************************
*/
/* Flags whether the MMU has been initialized */
static unsigned char mmuIsInitialized = 0;
/*
* Struct: PTInfo
*
* Description:
* This structure contains information on pages fetched from the OS that are
* used for page table pages that the SVA VM creates for its own purposes
* (e.g., secure memory).
*/
struct PTInfo {
/* Virtual address of page provided by the OS */
unsigned char * vosaddr;
/* Physical address to which the virtual address is mapped. */
uintptr_t paddr;
/* Number of uses in this page table page */
unsigned short uses;
/* Flags whether this entry is used */
unsigned char valid;
};
/*
* Structure: PTPages
*
* Description:
* This table records information on pages fetched from the operating system
* that the SVA VM will use for its own purposes.
*/
struct PTInfo PTPages[1024] SVAMEM;
/* Array describing the physical pages */
/* The index is the physical page number */
static page_desc_t page_desc[numPageDescEntries] SVAMEM;
typedef unsigned lock_t;
static lock_t MMULock SVAMEM;
static inline void init_MMULock() {
MMULock = 0;
}
static inline char MMULock_Try() {
return __sync_lock_test_and_set(&MMULock, 1) == 0;
}
static inline void MMULock_Acquire() {
while (!MMULock_Try()) {
panic("MMULock contended!");
while(MMULock) {
// yield
}
}
}
static inline void MMULock_Release() {
SVA_ASSERT(MMULock != 0, "Attempt to unlock unheld MMU lock!");
__sync_lock_release(&MMULock);
}
/*
* Description:
* Given a page table entry value, return the page description associate with
* the frame being addressed in the mapping.
*
* Inputs:
* mapping: the mapping with the physical address of the referenced frame
*
* Return:
* Pointer to the page_desc for this frame
*/
page_desc_t * getPageDescPtr(unsigned long mapping) {
unsigned long frameIndex = (mapping & PG_FRAME) / pageSize;
if (frameIndex >= numPageDescEntries)
panic ("SVA: getPageDescPtr: %lx %lx\n", frameIndex, numPageDescEntries);
return page_desc + frameIndex;
}
void
printPageType (unsigned char * p) {
printf ("SVA: page type: %p: %x\n", p, getPageDescPtr(getPhysicalAddr(p))->type);
return;
}
/*
* Function: init_mmu
*
* Description:
* Initialize MMU data structures.
*/
void
init_mmu () {
/* Initialize the page descriptor array */
memset (page_desc, 0, sizeof (struct page_desc_t) * numPageDescEntries);
return;
}
/*
*****************************************************************************
* Define helper functions for MMU operations
*****************************************************************************
*/
/* Functions for aiding in declare and updating of page tables */
/*
* Function: page_entry_store
*
* Description:
* This function takes a pointer to a page table entry and updates its value
* to the new value provided.
*
* Assumptions:
* - This function assumes that write protection is enabled in CR0 (WP bit set
* to 1).
*
* Inputs:
* *page_entry -: A pointer to the page entry to store the new value to, a
* valid VA for accessing the page_entry.
* newVal -: The new value to store, including the address of the
* referenced page.
*
* Side Effect:
* - This function enables system wide write protection in CR0.
*
*
*/
static inline void
page_entry_store (unsigned long *page_entry, page_entry_t newVal) {
/* Write the new value to the page_entry */
*page_entry = newVal;
/* TODO: Add a check here to make sure the value matches the one passed in */
}
/*
*****************************************************************************
* Page table page index and entry lookups
*****************************************************************************
*/
/*
* Function: pt_update_is_valid()
*
* Description:
* This function assesses a potential page table update for a valid mapping.
*
* NOTE: This function assumes that the page being mapped in has already been
* declared and has its intial page metadata captured as defined in the
* initial mapping of the page.
*
* Inputs:
* *page_entry - VA pointer to the page entry being modified
* newVal - Representes the new value to write including the reference
* to the underlying mapping.
*
* Return:
* 0 - The update is not valid and should not be performed.
* 1 - The update is valid but should disable write access.
* 2 - The update is valid and can be performed.
*/
static inline unsigned char
pt_update_is_valid (page_entry_t *page_entry, page_entry_t newVal) {
/* Collect associated information for the existing mapping */
unsigned long origPA = *page_entry & PG_FRAME;
unsigned long origFrame = origPA >> PAGESHIFT;
uintptr_t origVA = (uintptr_t) getVirtual(origPA);
page_desc_t *origPG = getPageDescPtr(origPA);
/* Get associated information for the new page being mapped */
unsigned long newPA = newVal & PG_FRAME;
unsigned long newFrame = newPA >> PAGESHIFT;
uintptr_t newVA = (uintptr_t) getVirtual(newPA);
page_desc_t *newPG = getPageDescPtr(newVal);
/* Get the page table page descriptor. The page_entry is the viratu */
uintptr_t ptePAddr = getPhysicalAddr (page_entry);
page_desc_t *ptePG = getPageDescPtr(ptePAddr);
/* Return value */
unsigned char retValue = 2;
/*
* Determine if the page table pointer is within the direct map. If not,
* then it's an error.
*
* TODO: This check can cause a panic because the SVA VM does not set
* up the direct map before starting the kernel. As a result, we get
* page table addresses that don't fall into the direct map.
*/
#if OBSOLETE // nk doesn't require DMAP only aliases
SVA_NOOP_ASSERT (isDirectMap (page_entry), "SVA: MMU: Not direct map\n");
#endif
#if OBSOLETE
/*
* Verify that we're not trying to modify the PML4E entry that controls the
* ghost address space.
*/
if (vg) {
if ((ptePG->type == PG_L4) && ((ptePAddr & PG_FRAME) == secmemOffset)) {
panic ("SVA: MMU: Trying to modify ghost memory pml4e!\n");
}
}
/*
* Verify that we're not modifying any of the page tables that control
* the ghost virtual address space. Ensuring that the page that we're
* writing into isn't a ghost page table (along with the previous check)
* should suffice.
*/
if (vg) {
SVA_ASSERT (!isGhostPTP(ptePG), "SVA: MMU: Kernel modifying ghost memory!\n");
}
#endif
/*
* Add check that the direct map is not being modified.
*
* TODO: This should be a check to make sure that we are updating a PTP page.
*/
if ((PG_DML1 <= ptePG->type) && (ptePG->type <= PG_DML4)) {
panic ("SVA: MMU: Modifying direct map!\n");
}
/*
* If we aren't mapping a new page then we can skip several checks, and in
* some cases we must, otherwise, the checks will fail. For example if this
* is a mapping in a page table page then we allow a zero mapping.
*/
if (newVal & PG_V) {
/*
* If the new mapping references a secure memory page, then silently
* ignore the request. This reduces porting effort because the kernel
* can try to map a ghost page, and the mapping will just never happen.
*/
if (vg && isGhostPG(newPG)) {
return 0;
}
/* If the new mapping references a secure memory page fail */
if (vg) SVA_ASSERT (!isGhostPTP(newPG), "MMU: Kernel mapping a ghost PTP");
#if OBSOLETE
/* If the mapping is to an SVA page then fail */
SVA_ASSERT (!isSVAPg(newPG), "Kernel attempted to map an SVA page");
#endif
/*
* New mappings to code pages are permitted as long as they are either
* for user-space pages or do not permit write access.
*/
if (isCodePg (newPG)) {
if ((newVal & (PG_RW | PG_U)) == (PG_RW)) {
panic ("SVA: Making kernel code writeable: %lx %lx\n", newVA, newVal);
}
}
/*
* If the new page is a page table page, then we verify some page table
* page specific checks.
*/
if (isPTP(newPG)) {
/*
* If we have a page table page being mapped in and it currently
* has a mapping to it, then we verify that the new VA from the new
* mapping matches the existing currently mapped VA.
*
* This guarantees that we each page table page (and the translations
* within it) maps a singular region of the address space.
*
* Otherwise, this is the first mapping of the page, and we should record
* in what virtual address it is being placed.
*/
#if 0
if (pgRefCount(newPG) > 1) {
if (newPG->pgVaddr != page_entry) {
panic ("SVA: PG: %lx %lx: type=%x\n", newPG->pgVaddr, page_entry, newPG->type);
}
SVA_ASSERT (newPG->pgVaddr == page_entry, "MMU: Map PTP to second VA");
} else {
newPG->pgVaddr = page_entry;
}
#endif
}
/*
* Verify that that the mapping matches the correct type of page
* allowed to be mapped into this page table. Verify that the new
* PTP is of the correct type given the page level of the page
* entry.
*/
switch (ptePG->type) {
case PG_L1:
if (!isFramePg(newPG)) {
/* If it is a ghost frame, stop with an error */
if (vg && isGhostPG (newPG)) panic ("SVA: MMU: Mapping ghost page!\n");
/*
* If it is a page table page, just ensure that it is not writeable.
* The kernel may be modifying the direct map, and we will permit
* that as long as it doesn't make page tables writeable.
*
* Note: The SVA VM really should have its own direct map that the
* kernel cannot use or modify, but that is too much work, so
* we make this compromise.
*/
if ((newPG->type >= PG_L1) && (newPG->type <= PG_L4)) {
retValue = 1;
} else {
panic ("SVA: MMU: Map bad page type into L1: %x\n", newPG->type);
}
}
break;
case PG_L2:
if (newVal & PG_PS) {
if (!isFramePg(newPG)) {
/* If it is a ghost frame, stop with an error */
if (vg && isGhostPG (newPG)) panic ("SVA: MMU: Mapping ghost page!\n");
/*
* If it is a page table page, just ensure that it is not writeable.
* The kernel may be modifying the direct map, and we will permit
* that as long as it doesn't make page tables writeable.
*
* Note: The SVA VM really should have its own direct map that the
* kernel cannot use or modify, but that is too much work, so
* we make this compromise.
*/
if ((newPG->type >= PG_L1) && (newPG->type <= PG_L4)) {
retValue = 1;
} else {
panic ("SVA: MMU: Map bad page type into L2: %x\n", newPG->type);
}
}
} else {
SVA_ASSERT (isL1Pg(newPG), "MMU: Mapping non-L1 page into L2.");
}
break;
case PG_L3:
if (newVal & PG_PS) {
if (!isFramePg(newPG)) {
/* If it is a ghost frame, stop with an error */
if (vg && isGhostPG (newPG)) panic ("SVA: MMU: Mapping ghost page!\n");
/*
* If it is a page table page, just ensure that it is not writeable.
* The kernel may be modifying the direct map, and we will permit
* that as long as it doesn't make page tables writeable.
*
* Note: The SVA VM really should have its own direct map that the
* kernel cannot use or modify, but that is too much work, so
* we make this compromise.
*/
if ((newPG->type >= PG_L1) && (newPG->type <= PG_L4)) {
retValue = 1;
} else {
panic ("SVA: MMU: Map bad page type into L2: %x\n", newPG->type);
}
}
} else {
SVA_ASSERT (isL2Pg(newPG), "MMU: Mapping non-L2 page into L3.");
}
break;
case PG_L4:
/*
* FreeBSD inserts a self mapping into the pml4, therefore it is
* valid to map in an L4 page into the L4.
*
* TODO: Consider the security implications of allowing an L4 to map
* an L4.
*/
SVA_ASSERT (isL3Pg(newPG) || isL4Pg(newPG),
"MMU: Mapping non-L3/L4 page into L4.");
break;
default:
break;
}
}
/*
* If the new mapping is set for user access, but the VA being used is to
* kernel space, fail. Also capture in this check is if the new mapping is
* set for super user access, but the VA being used is to user space, fail.
*
* 3 things to assess for matches:
* - U/S Flag of new mapping
* - Type of the new mapping frame
* - Type of the PTE frame
*
* Ensures the new mapping U/S flag matches the PT page frame type and the
* mapped in frame's page type, as well as no mapping kernel code pages
* into userspace.
*/
/*
* If the original PA is not equivalent to the new PA then we are creating
* an entirely new mapping, thus make sure that this is a valid new page
* reference. Also verify that the reference counts to the old page are
* sane, i.e., there is at least a current count of 1 to it.
*/
if (origPA != newPA) {
/*
* If the old mapping was to a code page then we know we shouldn't be
* pointing this entry to another code page, thus fail.
*/
if (isCodePg (origPG)) {
SVA_ASSERT ((*page_entry & PG_U),
"Kernel attempting to modify code page mapping");
}
}
return retValue;
}
/*
* Function: updateNewPageData
*
* Description:
* This function is called whenever we are inserting a new mapping into a page
* entry. The goal is to manage any SVA page data that needs to be set for
* tracking the new mapping with the existing page data. This is essential to
* enable the MMU verification checks.
*
* Inputs:
* mapping - The new mapping to be inserted in x86_64 page table format.
*/
static inline void
updateNewPageData(page_entry_t mapping) {
uintptr_t newPA = mapping & PG_FRAME;
unsigned long newFrame = newPA >> PAGESHIFT;
uintptr_t newVA = (uintptr_t) getVirtual(newPA);
page_desc_t *newPG = getPageDescPtr(mapping);
/*
* If the new mapping is valid, update the counts for it.
*/
if (mapping & PG_V) {
#if 0
/*
* If the new page is to a page table page and this is the first reference
* to the page, we need to set the VA mapping this page so that the
* verification routine can enforce that this page is only mapped
* to a single VA. Note that if we have gotten here, we know that
* we currently do not have a mapping to this page already, which
* means this is the first mapping to the page.
*/
if (isPTP(newPG)) {
newPG->pgVaddr = newVA;
}
#endif
/*
* Update the reference count for the new page frame. Check that we aren't
* overflowing the counter.
*/
SVA_ASSERT (pgRefCount(newPG) < ((1u << 13) - 1),
"MMU: overflow for the mapping count");
newPG->count++;
/*
* Set the VA of this entry if it is the first mapping to a page
* table page.
*/
}
return;
}
/*
* Function: updateOrigPageData
*
* Description:
* This function updates the metadata for a page that is being removed from
* the mapping.
*
* Inputs:
* mapping - An x86_64 page table entry describing the old mapping of the page
*/
static inline void
updateOrigPageData(page_entry_t mapping) {
page_desc_t *origPG = getPageDescPtr(mapping);
/*
* Only decrement the mapping count if the page has an existing valid
* mapping. Ensure that we don't drop the reference count below zero.
*/
if ((mapping & PG_V) && (origPG->count)) {
--(origPG->count);
}
return;
}
/*
* Function: __do_mmu_update
*
* Description:
* If the update has been validated, this function manages metadata by
* updating the internal SVA reference counts for pages and then performs the
* actual update.
*
* Inputs:
* *page_entry - VA pointer to the page entry being modified
* newVal - Representes the mapping to insert into the page_entry
*/
static inline void
__do_mmu_update (pte_t * pteptr, page_entry_t mapping) {
uintptr_t origPA = *pteptr & PG_FRAME;
uintptr_t newPA = mapping & PG_FRAME;
/*
* If we have a new mapping as opposed to just changing the flags of an
* existing mapping, then update the SVA meta data for the pages. We know
* that we have passed the validation checks so these updates have been
* vetted.
*/
if (newPA != origPA) {
updateOrigPageData(*pteptr);
updateNewPageData(mapping);
} else if ((*pteptr & PG_V) && ((mapping & PG_V) == 0)) {
/*
* If the old mapping is marked valid but the new mapping is not, then
* decrement the reference count of the old page.
*/
updateOrigPageData(*pteptr);
} else if (((*pteptr & PG_V) == 0) && (mapping & PG_V)) {
/*
* Contrariwise, if the old mapping is invalid but the new mapping is valid,
* then increment the reference count of the new page.
*/
updateNewPageData(mapping);
}
/* Perform the actual write to into the page table entry */
page_entry_store ((page_entry_t *) pteptr, mapping);
return;
}
/*
* Function: initDeclaredPage
*
* Description:
* This function zeros out the physical page pointed to by frameAddr and
* changes the permissions of the page in the direct map to read-only.
* This function is agnostic as to which level page table entry we are
* modifying because the format of the entry is the same in all cases.
*
* Assumption: This function should only be called by a declare intrinsic.
* Otherwise it has side effects that may break the system.
*
* Inputs:
* frameAddr: represents the physical address of this frame
*
* *page_entry: A pointer to a page table entry that will be used to
* initialize the mapping to this newly created page as read only. Note
* that the address of the page_entry must be a virtually accessible
* address.
*/
static inline void
initDeclaredPage (unsigned long frameAddr) {
/*
* Get the direct map virtual address of the physical address.
*/
unsigned char * vaddr = getVirtual (frameAddr);
/*
* Initialize the contents of the page to zero. This will ensure that no
* existing page translations which have not been vetted exist within the
* page.
*/
memset (vaddr, 0, X86_PAGE_SIZE);
/*
* Get a pointer to the page table entry that maps the physical page into the
* direct map.
*/
page_entry_t * page_entry = get_pgeVaddr (vaddr);
if (page_entry) {
/*
* Make the direct map entry for the page read-only to ensure that the OS
* goes through SVA to make page table changes. Also be sure to flush the
* TLBs for the direct map address to ensure that it's made read-only
* right away.
*/
if (((*page_entry) & PG_PS) == 0) {
page_entry_store (page_entry, setMappingReadOnly(*page_entry));
sva_mm_flush_tlb (vaddr);
}
}
return;
}
/*
* Function: __update_mapping
*
* Description:
* Mapping update function that is agnostic to the level of page table. Most
* of the verification code is consistent regardless of which level page
* update we are doing.
*
* Inputs:
* - pageEntryPtr : reference to the page table entry to insert the mapping
* into
* - val : new entry value
*/
static inline void
__update_mapping (pte_t * pageEntryPtr, page_entry_t val) {
/*
* If the given page update is valid then store the new value to the page
* table entry, else raise an error.
*/
switch (pt_update_is_valid((page_entry_t *) pageEntryPtr, val)) {
case 1:
// Kernel thinks these should be RW, since it wants to write to them.
// Convert to read-only and carry on.
val = setMappingReadOnly (val);
__do_mmu_update ((page_entry_t *) pageEntryPtr, val);
break;
case 2:
__do_mmu_update ((page_entry_t *) pageEntryPtr, val);
break;
case 0:
/* Silently ignore the request */
panic("Invalid mmu update requested!\n");
return;
default:
panic("##### SVA invalid page update!!!\n");
}
return;
}
/* Functions for finding the virtual address of page table components */
/*
* Function: get_pgeVaddr
*
* Description:
* This function does page walk to find the entry controlling access to the
* specified address. The function takes into consideration the potential use
* of larger page sizes.
*
* Inputs:
* vaddr - Virtual Address to find entry for
*
* Return value:
* 0 - There is no mapping for this virtual address.
* Otherwise, a pointer to the PTE that controls the mapping of this virtual
* address is returned.
*/
static inline page_entry_t *
get_pgeVaddr (uintptr_t vaddr) {
/* Pointer to the page table entry for the virtual address */
page_entry_t *pge = 0;
/* Get the base of the pml4 to traverse */
uintptr_t cr3 = get_pagetable();
if ((cr3 & 0xfffffffffffff000u) == 0)
return 0;
/* Get the VA of the pml4e for this vaddr */
pml4e_t *pml4e = get_pml4eVaddr (cr3, vaddr);
if (*pml4e & PG_V) {
/* Get the VA of the pdpte for this vaddr */
pdpte_t *pdpte = get_pdpteVaddr (pml4e, vaddr);
if (*pdpte & PG_V) {
/*
* The PDPE can be configurd in large page mode. If it is then we have the
* entry corresponding to the given vaddr If not then we go deeper in the
* page walk.
*/
if (*pdpte & PG_PS) {
pge = pdpte;
} else {
/* Get the pde associated with this vaddr */
pde_t *pde = get_pdeVaddr (pdpte, vaddr);
if (*pde & PG_V) {
/*
* As is the case with the pdpte, if the pde is configured for large
* page size then we have the corresponding entry. Otherwise we need
* to traverse one more level, which is the last.
*/
if (*pde & PG_PS) {
pge = pde;
} else {
pge = get_pteVaddr (pde, vaddr);
}
}
}
}
}
/* Return the entry corresponding to this vaddr */
return pge;
}
static inline pml4e_t *
get_pml4eVaddr (unsigned char * cr3, uintptr_t vaddr) {
/* Offset into the page table */
uintptr_t offset = (vaddr >> (39 - 3)) & vmask;
return (pml4e_t *) getVirtual (((uintptr_t)cr3) | offset);
}
static inline pdpte_t *
get_pdpteVaddr (pml4e_t * pml4e, uintptr_t vaddr) {
uintptr_t base = (*pml4e) & 0x000ffffffffff000u;
uintptr_t offset = (vaddr >> (30 - 3)) & vmask;
return (pdpte_t *) getVirtual (base | offset);
}
static inline pde_t *
get_pdeVaddr (pdpte_t * pdpte, uintptr_t vaddr) {
uintptr_t base = (*pdpte) & 0x000ffffffffff000u;
uintptr_t offset = (vaddr >> (21 - 3)) & vmask;
return (pde_t *) getVirtual (base | offset);
}
static inline pte_t *
get_pteVaddr (pde_t * pde, uintptr_t vaddr) {
uintptr_t base = (*pde) & 0x000ffffffffff000u;
uintptr_t offset = (vaddr >> (12 - 3)) & vmask;
return (pte_t *) getVirtual (base | offset);
}
/*
* Functions for returing the physical address of page table pages.
*/
static inline uintptr_t
get_pml4ePaddr (unsigned char * cr3, uintptr_t vaddr) {
/* Offset into the page table */
uintptr_t offset = ((vaddr >> 39) << 3) & vmask;
return (((uintptr_t)cr3) | offset);
}
static inline uintptr_t
get_pdptePaddr (pml4e_t * pml4e, uintptr_t vaddr) {
uintptr_t offset = ((vaddr >> 30) << 3) & vmask;
return ((*pml4e & 0x000ffffffffff000u) | offset);
}
static inline uintptr_t
get_pdePaddr (pdpte_t * pdpte, uintptr_t vaddr) {
uintptr_t offset = ((vaddr >> 21) << 3) & vmask;
return ((*pdpte & 0x000ffffffffff000u) | offset);
}
static inline uintptr_t
get_ptePaddr (pde_t * pde, uintptr_t vaddr) {
uintptr_t offset = ((vaddr >> 12) << 3) & vmask;
return ((*pde & 0x000ffffffffff000u) | offset);
}
/* Functions for querying information about a page table entry */
static inline unsigned char
isPresent (uintptr_t * pte) {
return (*pte & 0x1u) ? 1u : 0u;
}
/*
* Function: getPhysicalAddr()
*
* Description:
* Find the physical page number of the specified virtual address.
*/
uintptr_t
getPhysicalAddr (void * v) {
/* Mask to get the proper number of bits from the virtual address */
static const uintptr_t vmask = 0x0000000000000fffu;
/* Virtual address to convert */
uintptr_t vaddr = ((uintptr_t) v);
/* Offset into the page table */
uintptr_t offset = 0;
/*
* Get the currently active page table.
*/
unsigned char * cr3 = get_pagetable();
/*
* Get the address of the PML4e.
*/
pml4e_t * pml4e = get_pml4eVaddr (cr3, vaddr);
/*
* Use the PML4E to get the address of the PDPTE.
*/
pdpte_t * pdpte = get_pdpteVaddr (pml4e, vaddr);
/*
* Determine if the PDPTE has the PS flag set. If so, then it's pointing to
* a 1 GB page; return the physical address of that page.
*/
if ((*pdpte) & PTE_PS) {
return (*pdpte & 0x000fffffffffffffu) >> 30;
}
/*
* Find the page directory entry table from the PDPTE value.
*/
pde_t * pde = get_pdeVaddr (pdpte, vaddr);
/*
* Determine if the PDE has the PS flag set. If so, then it's pointing to a
* 2 MB page; return the physical address of that page.
*/
if ((*pde) & PTE_PS) {
return (*pde & 0x000fffffffe00000u) + (vaddr & 0x1fffffu);
}
/*
* Find the PTE pointed to by this PDE.
*/
pte_t * pte = get_pteVaddr (pde, vaddr);
/*
* Compute the physical address.
*/
offset = vaddr & vmask;
uintptr_t paddr = (*pte & 0x000ffffffffff000u) + offset;
return paddr;
}
/* Cache of page table pages */
extern unsigned char
SVAPTPages[1024][X86_PAGE_SIZE] SVAMEM;
/*
* Function: allocPTPage()
*
* Description:
* This function allocates a page table page, initializes it, and returns it
* to the caller.
*/
static unsigned int
allocPTPage (void) {
/* Index into the page table information array */
unsigned int ptindex;
/* Pointer to newly allocated memory */
unsigned char * p;
/*
* Find an empty page table array entry to record information about this page
* table page. Note that we're a multi-processor system, so use an atomic to
* keep things valid.
*
* Note that we leave the first entry reserved. This permits us to use a
* zero index to denote an invalid index.
*/
for (ptindex = 1; ptindex < 1024; ++ptindex) {
if (__sync_bool_compare_and_swap (&(PTPages[ptindex].valid), 0, 1)) {
break;
}
}
if (ptindex == 1024)
panic ("SVA: allocPTPage: No more table space!\n");
/*
* Ask the system software for a page of memory.
*/
if ((p = SVAPTPages[ptindex]) != 0) {
/*
* Initialize the memory.
*/
memset (p, 0, X86_PAGE_SIZE);
/*
* Record the information about the page in the page table page array.
* We'll need the virtual address by which the system software knows the
* page as well as the physical address so that the SVA VM can unmap it
* later.
*/
PTPages[ptindex].vosaddr = p;
PTPages[ptindex].paddr = getPhysicalAddr (p);
/*
* Return the index in the table.
*/
return ptindex;
}
/*
* Set the type of the page to be a ghost page table page.
*/
getPageDescPtr(getPhysicalAddr (p))->ghostPTP = 1;
return 0;
}
/*
* Function: freePTPage()