1 ;------------------------------------------------------------------------------
3 ; Sets the CR3 register for 64-bit paging
5 ; Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.<BR>
6 ; This program and the accompanying materials
7 ; are licensed and made available under the terms and conditions of the BSD License
8 ; which accompanies this distribution. The full text of the license may be found at
9 ; http://opensource.org/licenses/bsd-license.php
11 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 ;------------------------------------------------------------------------------
18 %define PAGE_PRESENT 0x01
19 %define PAGE_READ_WRITE 0x02
20 %define PAGE_USER_SUPERVISOR 0x04
21 %define PAGE_WRITE_THROUGH 0x08
22 %define PAGE_CACHE_DISABLE 0x010
23 %define PAGE_ACCESSED 0x020
24 %define PAGE_DIRTY 0x040
25 %define PAGE_PAT 0x080
26 %define PAGE_GLOBAL 0x0100
27 %define PAGE_2M_MBO 0x080
28 %define PAGE_2M_PAT 0x01000
30 %define PAGE_2M_PDE_ATTR (PAGE_2M_MBO + \
36 %define PAGE_PDP_ATTR (PAGE_ACCESSED + \
40 ; Check if Secure Encrypted Virtualization (SEV) feature is enabled
42 ; If SEV is enabled then EAX will be at least 32
43 ; If SEV is disabled then EAX will be zero.
46 ; Check if we have a valid (0x8000_001F) CPUID leaf
50 ; This check should fail on Intel or Non SEV AMD CPUs. In future if
51 ; Intel CPUs supports this CPUID leaf then we are guranteed to have exact
52 ; same bit definition.
56 ; Check for memory encryption feature:
57 ; CPUID Fn8000_001F[EAX] - Bit 1
64 ; Check if memory encryption is enabled
65 ; MSR_0xC0010131 - Bit 0 (SEV enabled)
71 ; Get pte bit position to enable memory encryption
72 ; CPUID Fn8000_001F[EBX] - Bits 5:0
82 OneTimeCallRet CheckSevFeature
85 ; Modified: EAX, EBX, ECX, EDX
87 SetCr3ForPageTables64:
89 OneTimeCall CheckSevFeature
94 ; If SEV is enabled, C-bit is always above 31
101 ; For OVMF, build some initial page tables at
102 ; PcdOvmfSecPageTablesBase - (PcdOvmfSecPageTablesBase + 0x6000).
104 ; This range should match with PcdOvmfSecPageTablesSize which is
105 ; declared in the FDF files.
107 ; At the end of PEI, the pages tables will be rebuilt into a
108 ; more permanent location by DxeIpl.
111 mov ecx, 6 * 0x1000 / 4
113 clearPageTablesMemoryLoop:
114 mov dword[ecx * 4 + PT_ADDR (0) - 4], eax
115 loop clearPageTablesMemoryLoop
118 ; Top level Page Directory Pointers (1 * 512GB entry)
120 mov dword[PT_ADDR (0)], PT_ADDR (0x1000) + PAGE_PDP_ATTR
121 mov dword[PT_ADDR (4)], edx
124 ; Next level Page Directory Pointers (4 * 1GB entries => 4GB)
126 mov dword[PT_ADDR (0x1000)], PT_ADDR (0x2000) + PAGE_PDP_ATTR
127 mov dword[PT_ADDR (0x1004)], edx
128 mov dword[PT_ADDR (0x1008)], PT_ADDR (0x3000) + PAGE_PDP_ATTR
129 mov dword[PT_ADDR (0x100C)], edx
130 mov dword[PT_ADDR (0x1010)], PT_ADDR (0x4000) + PAGE_PDP_ATTR
131 mov dword[PT_ADDR (0x1014)], edx
132 mov dword[PT_ADDR (0x1018)], PT_ADDR (0x5000) + PAGE_PDP_ATTR
133 mov dword[PT_ADDR (0x101C)], edx
136 ; Page Table Entries (2048 * 2MB entries => 4GB)
139 pageTableEntriesLoop:
143 add eax, PAGE_2M_PDE_ATTR
144 mov [ecx * 8 + PT_ADDR (0x2000 - 8)], eax
145 mov [(ecx * 8 + PT_ADDR (0x2000 - 8)) + 4], edx
146 loop pageTableEntriesLoop
149 ; Set CR3 now that the paging structures are available
154 OneTimeCallRet SetCr3ForPageTables64