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