]>
Commit | Line | Data |
---|---|---|
1 | ;------------------------------------------------------------------------------\r | |
2 | ; @file\r | |
3 | ; Sets the CR3 register for 64-bit paging\r | |
4 | ;\r | |
5 | ; Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.<BR>\r | |
6 | ; Copyright (c) 2017 - 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>\r | |
7 | ; SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
8 | ;\r | |
9 | ;------------------------------------------------------------------------------\r | |
10 | \r | |
11 | BITS 32\r | |
12 | \r | |
13 | %define PAGE_PRESENT 0x01\r | |
14 | %define PAGE_READ_WRITE 0x02\r | |
15 | %define PAGE_USER_SUPERVISOR 0x04\r | |
16 | %define PAGE_WRITE_THROUGH 0x08\r | |
17 | %define PAGE_CACHE_DISABLE 0x010\r | |
18 | %define PAGE_ACCESSED 0x020\r | |
19 | %define PAGE_DIRTY 0x040\r | |
20 | %define PAGE_PAT 0x080\r | |
21 | %define PAGE_GLOBAL 0x0100\r | |
22 | %define PAGE_2M_MBO 0x080\r | |
23 | %define PAGE_2M_PAT 0x01000\r | |
24 | \r | |
25 | %define PAGE_4K_PDE_ATTR (PAGE_ACCESSED + \\r | |
26 | PAGE_DIRTY + \\r | |
27 | PAGE_READ_WRITE + \\r | |
28 | PAGE_PRESENT)\r | |
29 | \r | |
30 | %define PAGE_2M_PDE_ATTR (PAGE_2M_MBO + \\r | |
31 | PAGE_ACCESSED + \\r | |
32 | PAGE_DIRTY + \\r | |
33 | PAGE_READ_WRITE + \\r | |
34 | PAGE_PRESENT)\r | |
35 | \r | |
36 | %define PAGE_PDP_ATTR (PAGE_ACCESSED + \\r | |
37 | PAGE_READ_WRITE + \\r | |
38 | PAGE_PRESENT)\r | |
39 | \r | |
40 | %define TDX_BSP 1\r | |
41 | %define TDX_AP 2\r | |
42 | \r | |
43 | ;\r | |
44 | ; Modified: EAX, EBX, ECX, EDX\r | |
45 | ;\r | |
46 | SetCr3ForPageTables64:\r | |
47 | ; Check the TDX features.\r | |
48 | ; If it is TDX APs, then jump to SetCr3 directly.\r | |
49 | ; In TD guest the initialization is done by BSP, including building\r | |
50 | ; the page tables. APs will spin on until byte[TDX_WORK_AREA_PGTBL_READY]\r | |
51 | ; is set.\r | |
52 | OneTimeCall CheckTdxFeaturesBeforeBuildPagetables\r | |
53 | cmp eax, TDX_BSP\r | |
54 | je ClearOvmfPageTables\r | |
55 | cmp eax, TDX_AP\r | |
56 | je SetCr3\r | |
57 | \r | |
58 | ; Check whether the SEV is active and populate the SevEsWorkArea\r | |
59 | OneTimeCall CheckSevFeatures\r | |
60 | \r | |
61 | ; If SEV is enabled, the C-bit position is always above 31.\r | |
62 | ; The mask will be saved in the EDX and applied during the\r | |
63 | ; the page table build below.\r | |
64 | OneTimeCall GetSevCBitMaskAbove31\r | |
65 | \r | |
66 | ClearOvmfPageTables:\r | |
67 | ;\r | |
68 | ; For OVMF, build some initial page tables at\r | |
69 | ; PcdOvmfSecPageTablesBase - (PcdOvmfSecPageTablesBase + 0x6000).\r | |
70 | ;\r | |
71 | ; This range should match with PcdOvmfSecPageTablesSize which is\r | |
72 | ; declared in the FDF files.\r | |
73 | ;\r | |
74 | ; At the end of PEI, the pages tables will be rebuilt into a\r | |
75 | ; more permanent location by DxeIpl.\r | |
76 | ;\r | |
77 | \r | |
78 | mov ecx, 6 * 0x1000 / 4\r | |
79 | xor eax, eax\r | |
80 | clearPageTablesMemoryLoop:\r | |
81 | mov dword[ecx * 4 + PT_ADDR (0) - 4], eax\r | |
82 | loop clearPageTablesMemoryLoop\r | |
83 | \r | |
84 | ;\r | |
85 | ; Top level Page Directory Pointers (1 * 512GB entry)\r | |
86 | ;\r | |
87 | mov dword[PT_ADDR (0)], PT_ADDR (0x1000) + PAGE_PDP_ATTR\r | |
88 | mov dword[PT_ADDR (4)], edx\r | |
89 | \r | |
90 | ;\r | |
91 | ; Next level Page Directory Pointers (4 * 1GB entries => 4GB)\r | |
92 | ;\r | |
93 | mov dword[PT_ADDR (0x1000)], PT_ADDR (0x2000) + PAGE_PDP_ATTR\r | |
94 | mov dword[PT_ADDR (0x1004)], edx\r | |
95 | mov dword[PT_ADDR (0x1008)], PT_ADDR (0x3000) + PAGE_PDP_ATTR\r | |
96 | mov dword[PT_ADDR (0x100C)], edx\r | |
97 | mov dword[PT_ADDR (0x1010)], PT_ADDR (0x4000) + PAGE_PDP_ATTR\r | |
98 | mov dword[PT_ADDR (0x1014)], edx\r | |
99 | mov dword[PT_ADDR (0x1018)], PT_ADDR (0x5000) + PAGE_PDP_ATTR\r | |
100 | mov dword[PT_ADDR (0x101C)], edx\r | |
101 | \r | |
102 | ;\r | |
103 | ; Page Table Entries (2048 * 2MB entries => 4GB)\r | |
104 | ;\r | |
105 | mov ecx, 0x800\r | |
106 | pageTableEntriesLoop:\r | |
107 | mov eax, ecx\r | |
108 | dec eax\r | |
109 | shl eax, 21\r | |
110 | add eax, PAGE_2M_PDE_ATTR\r | |
111 | mov [ecx * 8 + PT_ADDR (0x2000 - 8)], eax\r | |
112 | mov [(ecx * 8 + PT_ADDR (0x2000 - 8)) + 4], edx\r | |
113 | loop pageTableEntriesLoop\r | |
114 | \r | |
115 | ; Clear the C-bit from the GHCB page if the SEV-ES is enabled.\r | |
116 | OneTimeCall SevClearPageEncMaskForGhcbPage\r | |
117 | \r | |
118 | ; TDX will do some PostBuildPages task, such as setting\r | |
119 | ; byte[TDX_WORK_AREA_PGTBL_READY].\r | |
120 | OneTimeCall TdxPostBuildPageTables\r | |
121 | \r | |
122 | SetCr3:\r | |
123 | ;\r | |
124 | ; Set CR3 now that the paging structures are available\r | |
125 | ;\r | |
126 | mov eax, PT_ADDR (0)\r | |
127 | mov cr3, eax\r | |
128 | \r | |
129 | OneTimeCallRet SetCr3ForPageTables64\r |