summaryrefslogtreecommitdiff
path: root/DuetPkg/DxeIpl/Ia32/Paging.c
blob: ff38dc33c41b853445612eaa0df4f22be73aeed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/** @file

Copyright (c) 2006 - 2007, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:
  Paging.c

Abstract:

Revision History:

**/

#include "DxeIpl.h"
#include "HobGeneration.h"
#include "VirtualMemory.h"
#include "Debug.h"

#define EFI_PAGE_SIZE_4K      0x1000
#define EFI_PAGE_SIZE_4M      0x400000

//
// Create 4G 4M-page table
// PDE (31:22)   :  1024 entries
//
#define EFI_MAX_ENTRY_NUM     1024

#define EFI_PDE_ENTRY_NUM     EFI_MAX_ENTRY_NUM

#define EFI_PDE_PAGE_NUM      1

#define EFI_PAGE_NUMBER_4M    (EFI_PDE_PAGE_NUM)

//
// Create 4M 4K-page table
// PTE (21:12)   :  1024 entries
//
#define EFI_PTE_ENTRY_NUM     EFI_MAX_ENTRY_NUM
#define EFI_PTE_PAGE_NUM      1

#define EFI_PAGE_NUMBER_4K    (EFI_PTE_PAGE_NUM)

#define EFI_PAGE_NUMBER       (EFI_PAGE_NUMBER_4M + EFI_PAGE_NUMBER_4K)

VOID
EnableNullPointerProtection (
  UINT8 *PageTable
  )
{
  IA32_PAGE_TABLE_ENTRY_4K                      *PageTableEntry4KB;

  PageTableEntry4KB = (IA32_PAGE_TABLE_ENTRY_4K *)((UINTN)PageTable + EFI_PAGE_NUMBER_4M * EFI_PAGE_SIZE_4K);

  //
  // Fill in the Page Table entries
  // Mark 0~4K as not present
  //
  PageTableEntry4KB->Bits.Present = 0;

  return ;
}

VOID
Ia32Create4KPageTables (
  UINT8 *PageTable
  )
{
  UINT64                                        PageAddress;
  UINTN                                         PTEIndex;
  IA32_PAGE_DIRECTORY_ENTRY_4K                  *PageDirectoryEntry4KB;
  IA32_PAGE_TABLE_ENTRY_4K                      *PageTableEntry4KB;

  PageAddress = 0;

  //
  //  Page Table structure 2 level 4K.
  //
  //  Page Table 4K  : PageDirectoryEntry4K      : bits 31-22
  //                   PageTableEntry            : bits 21-12
  //

  PageTableEntry4KB = (IA32_PAGE_TABLE_ENTRY_4K *)((UINTN)PageTable + EFI_PAGE_NUMBER_4M * EFI_PAGE_SIZE_4K);
  PageDirectoryEntry4KB = (IA32_PAGE_DIRECTORY_ENTRY_4K *)((UINTN)PageTable);

  PageDirectoryEntry4KB->Uint32 = (UINT32)(UINTN)PageTableEntry4KB;
  PageDirectoryEntry4KB->Bits.ReadWrite = 0;
  PageDirectoryEntry4KB->Bits.Present = 1;
  PageDirectoryEntry4KB->Bits.MustBeZero = 1;

  for (PTEIndex = 0; PTEIndex < EFI_PTE_ENTRY_NUM; PTEIndex++, PageTableEntry4KB++) {
    //
    // Fill in the Page Table entries
    //
    PageTableEntry4KB->Uint32 = (UINT32)PageAddress;
    PageTableEntry4KB->Bits.ReadWrite = 1;
    PageTableEntry4KB->Bits.Present = 1;

    PageAddress += EFI_PAGE_SIZE_4K;
  }

  return ;
}

VOID
Ia32Create4MPageTables (
  UINT8 *PageTable
  )
{
  UINT32                                        PageAddress;
  UINT8                                         *TempPageTable;
  UINTN                                         PDEIndex;
  IA32_PAGE_TABLE_ENTRY_4M                      *PageDirectoryEntry4MB;

  TempPageTable = PageTable;

  PageAddress = 0;

  //
  //  Page Table structure 1 level 4MB.
  //
  //  Page Table 4MB : PageDirectoryEntry4M      : bits 31-22
  //

  PageDirectoryEntry4MB = (IA32_PAGE_TABLE_ENTRY_4M *)TempPageTable;

  for (PDEIndex = 0; PDEIndex < EFI_PDE_ENTRY_NUM; PDEIndex++, PageDirectoryEntry4MB++) {
    //
    // Fill in the Page Directory entries
    //
    PageDirectoryEntry4MB->Uint32 = (UINT32)PageAddress;
    PageDirectoryEntry4MB->Bits.ReadWrite = 1;
    PageDirectoryEntry4MB->Bits.Present = 1;
    PageDirectoryEntry4MB->Bits.MustBe1 = 1;

    PageAddress += EFI_PAGE_SIZE_4M;
  }

  return ;
}

VOID *
PreparePageTable (
  VOID  *PageNumberTop,
  UINT8 SizeOfMemorySpace 
  )
/*++
Description:
  Generate pagetable below PageNumberTop, 
  and return the bottom address of pagetable for putting other things later.
--*/
{
  VOID *PageNumberBase;

  PageNumberBase = (VOID *)((UINTN)PageNumberTop - EFI_PAGE_NUMBER * EFI_PAGE_SIZE_4K);
  ZeroMem (PageNumberBase, EFI_PAGE_NUMBER * EFI_PAGE_SIZE_4K);

  Ia32Create4MPageTables (PageNumberBase);
  Ia32Create4KPageTables (PageNumberBase);
  //
  // Not enable NULL Pointer Protection if using INTX call
  //
//  EnableNullPointerProtection (PageNumberBase);

  return PageNumberBase;
}