3 Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include "HobGeneration.h"
23 #include "VirtualMemory.h"
26 #define EFI_PAGE_SIZE_4K 0x1000
27 #define EFI_PAGE_SIZE_4M 0x400000
30 // Create 4G 4M-page table
31 // PDE (31:22) : 1024 entries
33 #define EFI_MAX_ENTRY_NUM 1024
35 #define EFI_PDE_ENTRY_NUM EFI_MAX_ENTRY_NUM
37 #define EFI_PDE_PAGE_NUM 1
39 #define EFI_PAGE_NUMBER_4M (EFI_PDE_PAGE_NUM)
42 // Create 4M 4K-page table
43 // PTE (21:12) : 1024 entries
45 #define EFI_PTE_ENTRY_NUM EFI_MAX_ENTRY_NUM
46 #define EFI_PTE_PAGE_NUM 1
48 #define EFI_PAGE_NUMBER_4K (EFI_PTE_PAGE_NUM)
50 #define EFI_PAGE_NUMBER (EFI_PAGE_NUMBER_4M + EFI_PAGE_NUMBER_4K)
53 EnableNullPointerProtection (
57 IA32_PAGE_TABLE_ENTRY_4K
*PageTableEntry4KB
;
59 PageTableEntry4KB
= (IA32_PAGE_TABLE_ENTRY_4K
*)((UINTN
)PageTable
+ EFI_PAGE_NUMBER_4M
* EFI_PAGE_SIZE_4K
);
62 // Fill in the Page Table entries
63 // Mark 0~4K as not present
65 PageTableEntry4KB
->Bits
.Present
= 0;
71 Ia32Create4KPageTables (
77 IA32_PAGE_DIRECTORY_ENTRY_4K
*PageDirectoryEntry4KB
;
78 IA32_PAGE_TABLE_ENTRY_4K
*PageTableEntry4KB
;
83 // Page Table structure 2 level 4K.
85 // Page Table 4K : PageDirectoryEntry4K : bits 31-22
86 // PageTableEntry : bits 21-12
89 PageTableEntry4KB
= (IA32_PAGE_TABLE_ENTRY_4K
*)((UINTN
)PageTable
+ EFI_PAGE_NUMBER_4M
* EFI_PAGE_SIZE_4K
);
90 PageDirectoryEntry4KB
= (IA32_PAGE_DIRECTORY_ENTRY_4K
*)((UINTN
)PageTable
);
92 PageDirectoryEntry4KB
->Uint32
= (UINT32
)(UINTN
)PageTableEntry4KB
;
93 PageDirectoryEntry4KB
->Bits
.ReadWrite
= 0;
94 PageDirectoryEntry4KB
->Bits
.Present
= 1;
95 PageDirectoryEntry4KB
->Bits
.MustBeZero
= 1;
97 for (PTEIndex
= 0; PTEIndex
< EFI_PTE_ENTRY_NUM
; PTEIndex
++, PageTableEntry4KB
++) {
99 // Fill in the Page Table entries
101 PageTableEntry4KB
->Uint32
= (UINT32
)PageAddress
;
102 PageTableEntry4KB
->Bits
.ReadWrite
= 1;
103 PageTableEntry4KB
->Bits
.Present
= 1;
105 PageAddress
+= EFI_PAGE_SIZE_4K
;
112 Ia32Create4MPageTables (
117 UINT8
*TempPageTable
;
119 IA32_PAGE_TABLE_ENTRY_4M
*PageDirectoryEntry4MB
;
121 TempPageTable
= PageTable
;
126 // Page Table structure 1 level 4MB.
128 // Page Table 4MB : PageDirectoryEntry4M : bits 31-22
131 PageDirectoryEntry4MB
= (IA32_PAGE_TABLE_ENTRY_4M
*)TempPageTable
;
133 for (PDEIndex
= 0; PDEIndex
< EFI_PDE_ENTRY_NUM
; PDEIndex
++, PageDirectoryEntry4MB
++) {
135 // Fill in the Page Directory entries
137 PageDirectoryEntry4MB
->Uint32
= (UINT32
)PageAddress
;
138 PageDirectoryEntry4MB
->Bits
.ReadWrite
= 1;
139 PageDirectoryEntry4MB
->Bits
.Present
= 1;
140 PageDirectoryEntry4MB
->Bits
.MustBe1
= 1;
142 PageAddress
+= EFI_PAGE_SIZE_4M
;
151 UINT8 SizeOfMemorySpace
155 Generate pagetable below PageNumberTop,
156 and return the bottom address of pagetable for putting other things later.
159 VOID
*PageNumberBase
;
161 PageNumberBase
= (VOID
*)((UINTN
)PageNumberTop
- EFI_PAGE_NUMBER
* EFI_PAGE_SIZE_4K
);
162 ZeroMem (PageNumberBase
, EFI_PAGE_NUMBER
* EFI_PAGE_SIZE_4K
);
164 Ia32Create4MPageTables (PageNumberBase
);
165 Ia32Create4KPageTables (PageNumberBase
);
167 // Not enable NULL Pointer Protection if using INTX call
169 // EnableNullPointerProtection (PageNumberBase);
171 return PageNumberBase
;