]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/ResetVector/Ia32/PageTables64.asm
OvmfPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / OvmfPkg / ResetVector / Ia32 / PageTables64.asm
CommitLineData
c90e37b5
JJ
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
b26f0cf9 6; SPDX-License-Identifier: BSD-2-Clause-Patent\r
c90e37b5
JJ
7;\r
8;------------------------------------------------------------------------------\r
9\r
10BITS 32\r
11\r
12%define PAGE_PRESENT 0x01\r
13%define PAGE_READ_WRITE 0x02\r
14%define PAGE_USER_SUPERVISOR 0x04\r
15%define PAGE_WRITE_THROUGH 0x08\r
16%define PAGE_CACHE_DISABLE 0x010\r
17%define PAGE_ACCESSED 0x020\r
18%define PAGE_DIRTY 0x040\r
19%define PAGE_PAT 0x080\r
20%define PAGE_GLOBAL 0x0100\r
21%define PAGE_2M_MBO 0x080\r
22%define PAGE_2M_PAT 0x01000\r
23\r
24%define PAGE_2M_PDE_ATTR (PAGE_2M_MBO + \\r
c90e37b5
JJ
25 PAGE_ACCESSED + \\r
26 PAGE_DIRTY + \\r
27 PAGE_READ_WRITE + \\r
28 PAGE_PRESENT)\r
29\r
98f378a7 30%define PAGE_PDP_ATTR (PAGE_ACCESSED + \\r
c90e37b5
JJ
31 PAGE_READ_WRITE + \\r
32 PAGE_PRESENT)\r
33\r
e60af8a1
BS
34; Check if Secure Encrypted Virtualization (SEV) feature is enabled\r
35;\r
36; If SEV is enabled then EAX will be at least 32\r
37; If SEV is disabled then EAX will be zero.\r
38;\r
39CheckSevFeature:\r
40 ; Check if we have a valid (0x8000_001F) CPUID leaf\r
41 mov eax, 0x80000000\r
42 cpuid\r
43\r
44 ; This check should fail on Intel or Non SEV AMD CPUs. In future if\r
45 ; Intel CPUs supports this CPUID leaf then we are guranteed to have exact\r
46 ; same bit definition.\r
47 cmp eax, 0x8000001f\r
48 jl NoSev\r
49\r
50 ; Check for memory encryption feature:\r
51 ; CPUID Fn8000_001F[EAX] - Bit 1\r
52 ;\r
53 mov eax, 0x8000001f\r
54 cpuid\r
55 bt eax, 1\r
56 jnc NoSev\r
57\r
58 ; Check if memory encryption is enabled\r
59 ; MSR_0xC0010131 - Bit 0 (SEV enabled)\r
60 mov ecx, 0xc0010131\r
61 rdmsr\r
62 bt eax, 0\r
63 jnc NoSev\r
64\r
65 ; Get pte bit position to enable memory encryption\r
66 ; CPUID Fn8000_001F[EBX] - Bits 5:0\r
67 ;\r
68 mov eax, ebx\r
69 and eax, 0x3f\r
70 jmp SevExit\r
71\r
72NoSev:\r
73 xor eax, eax\r
74\r
75SevExit:\r
76 OneTimeCallRet CheckSevFeature\r
c90e37b5
JJ
77\r
78;\r
e60af8a1 79; Modified: EAX, EBX, ECX, EDX\r
c90e37b5
JJ
80;\r
81SetCr3ForPageTables64:\r
82\r
e60af8a1
BS
83 OneTimeCall CheckSevFeature\r
84 xor edx, edx\r
85 test eax, eax\r
86 jz SevNotActive\r
87\r
88 ; If SEV is enabled, C-bit is always above 31\r
89 sub eax, 32\r
90 bts edx, eax\r
91\r
92SevNotActive:\r
93\r
c90e37b5 94 ;\r
73d66c58
MH
95 ; For OVMF, build some initial page tables at\r
96 ; PcdOvmfSecPageTablesBase - (PcdOvmfSecPageTablesBase + 0x6000).\r
b382ede3 97 ;\r
73d66c58
MH
98 ; This range should match with PcdOvmfSecPageTablesSize which is\r
99 ; declared in the FDF files.\r
c90e37b5
JJ
100 ;\r
101 ; At the end of PEI, the pages tables will be rebuilt into a\r
102 ; more permanent location by DxeIpl.\r
103 ;\r
104\r
105 mov ecx, 6 * 0x1000 / 4\r
106 xor eax, eax\r
107clearPageTablesMemoryLoop:\r
73d66c58 108 mov dword[ecx * 4 + PT_ADDR (0) - 4], eax\r
c90e37b5
JJ
109 loop clearPageTablesMemoryLoop\r
110\r
111 ;\r
112 ; Top level Page Directory Pointers (1 * 512GB entry)\r
113 ;\r
73d66c58 114 mov dword[PT_ADDR (0)], PT_ADDR (0x1000) + PAGE_PDP_ATTR\r
e60af8a1 115 mov dword[PT_ADDR (4)], edx\r
c90e37b5
JJ
116\r
117 ;\r
118 ; Next level Page Directory Pointers (4 * 1GB entries => 4GB)\r
119 ;\r
73d66c58 120 mov dword[PT_ADDR (0x1000)], PT_ADDR (0x2000) + PAGE_PDP_ATTR\r
e60af8a1 121 mov dword[PT_ADDR (0x1004)], edx\r
73d66c58 122 mov dword[PT_ADDR (0x1008)], PT_ADDR (0x3000) + PAGE_PDP_ATTR\r
e60af8a1 123 mov dword[PT_ADDR (0x100C)], edx\r
73d66c58 124 mov dword[PT_ADDR (0x1010)], PT_ADDR (0x4000) + PAGE_PDP_ATTR\r
e60af8a1 125 mov dword[PT_ADDR (0x1014)], edx\r
73d66c58 126 mov dword[PT_ADDR (0x1018)], PT_ADDR (0x5000) + PAGE_PDP_ATTR\r
e60af8a1 127 mov dword[PT_ADDR (0x101C)], edx\r
c90e37b5
JJ
128\r
129 ;\r
130 ; Page Table Entries (2048 * 2MB entries => 4GB)\r
131 ;\r
132 mov ecx, 0x800\r
133pageTableEntriesLoop:\r
134 mov eax, ecx\r
135 dec eax\r
136 shl eax, 21\r
137 add eax, PAGE_2M_PDE_ATTR\r
73d66c58 138 mov [ecx * 8 + PT_ADDR (0x2000 - 8)], eax\r
e60af8a1 139 mov [(ecx * 8 + PT_ADDR (0x2000 - 8)) + 4], edx\r
c90e37b5
JJ
140 loop pageTableEntriesLoop\r
141\r
142 ;\r
143 ; Set CR3 now that the paging structures are available\r
144 ;\r
73d66c58 145 mov eax, PT_ADDR (0)\r
c90e37b5
JJ
146 mov cr3, eax\r
147\r
148 OneTimeCallRet SetCr3ForPageTables64\r