ReactOS  0.4.15-dev-1070-ge1a01de
patpge.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for patpge.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define PDE_BITS   10
 
#define PTE_BITS   10
 

Functions

INIT_FUNCTION ULONG_PTR NTAPI Ki386EnableGlobalPage (IN ULONG_PTR Context)
 
INIT_FUNCTION VOID NTAPI KiInitializePAT (VOID)
 
INIT_FUNCTION ULONG_PTR NTAPI Ki386EnableTargetLargePage (IN ULONG_PTR Context)
 
PVOID NTAPI Ki386AllocateContiguousMemory (PLARGE_IDENTITY_MAP IdentityMap, ULONG PagesCount, BOOLEAN InLower4Gb)
 
PHYSICAL_ADDRESS NTAPI Ki386BuildIdentityBuffer (PLARGE_IDENTITY_MAP IdentityMap, PVOID StartPtr, ULONG Length)
 
BOOLEAN NTAPI Ki386IdentityMapMakeValid (PLARGE_IDENTITY_MAP IdentityMap, PHARDWARE_PTE Pde, PHARDWARE_PTE *PageTable)
 
BOOLEAN NTAPI Ki386MapAddress (PLARGE_IDENTITY_MAP IdentityMap, ULONG_PTR VirtualPtr, PHYSICAL_ADDRESS PhysicalPtr)
 
VOID NTAPI Ki386ConvertPte (PHARDWARE_PTE Pte)
 
BOOLEAN NTAPI Ki386CreateIdentityMap (PLARGE_IDENTITY_MAP IdentityMap, PVOID StartPtr, ULONG PagesCount)
 
VOID NTAPI Ki386FreeIdentityMap (PLARGE_IDENTITY_MAP IdentityMap)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file patpge.c.

◆ PDE_BITS

#define PDE_BITS   10

Definition at line 15 of file patpge.c.

◆ PTE_BITS

#define PTE_BITS   10

Definition at line 16 of file patpge.c.

Function Documentation

◆ Ki386AllocateContiguousMemory()

PVOID NTAPI Ki386AllocateContiguousMemory ( PLARGE_IDENTITY_MAP  IdentityMap,
ULONG  PagesCount,
BOOLEAN  InLower4Gb 
)

Definition at line 82 of file patpge.c.

85 {
86  PHYSICAL_ADDRESS AddressMask;
87  PVOID Result;
88  ULONG SizeInBytes = PagesCount * PAGE_SIZE;
89 
90  /* Initialize acceptable address mask to any possible address */
91  AddressMask.LowPart = 0xFFFFFFFF;
92  AddressMask.HighPart = 0xFFFFFFFF;
93 
94  /* Mark out higher 4Gb if caller asked so */
95  if (InLower4Gb) AddressMask.HighPart = 0;
96 
97  /* Try to allocate the memory */
98  Result = MmAllocateContiguousMemory(SizeInBytes, AddressMask);
99  if (!Result) return NULL;
100 
101  /* Zero it out */
102  RtlZeroMemory(Result, SizeInBytes);
103 
104  /* Track allocated pages in the IdentityMap structure */
105  IdentityMap->PagesList[IdentityMap->PagesCount] = Result;
106  IdentityMap->PagesCount++;
107 
108  return Result;
109 }
ULONG PagesCount
Definition: ke.h:174
smooth NULL
Definition: ftsmooth.c:416
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
ULONG LowPart
Definition: typedefs.h:106
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PVOID PagesList[30]
Definition: ke.h:175
PVOID NTAPI MmAllocateContiguousMemory(IN SIZE_T NumberOfBytes, IN PHYSICAL_ADDRESS HighestAcceptableAddress)
Definition: contmem.c:626
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262

Referenced by Ki386IdentityMapMakeValid(), and Ki386MapAddress().

◆ Ki386BuildIdentityBuffer()

PHYSICAL_ADDRESS NTAPI Ki386BuildIdentityBuffer ( PLARGE_IDENTITY_MAP  IdentityMap,
PVOID  StartPtr,
ULONG  Length 
)

Definition at line 113 of file patpge.c.

116 {
117  // TODO: Check whether all pages are below 4GB boundary
118  return MmGetPhysicalAddress(StartPtr);
119 }
PHYSICAL_ADDRESS NTAPI MmGetPhysicalAddress(IN PVOID Address)
Definition: stubs.c:685

Referenced by Ki386CreateIdentityMap().

◆ Ki386ConvertPte()

VOID NTAPI Ki386ConvertPte ( PHARDWARE_PTE  Pte)

Definition at line 183 of file patpge.c.

184 {
185  PVOID VirtualPtr;
186  PHYSICAL_ADDRESS PhysicalPtr;
187 
188  /* Get virtual and physical addresses */
189  VirtualPtr = (PVOID)(Pte->PageFrameNumber << PAGE_SHIFT);
190  PhysicalPtr = MmGetPhysicalAddress(VirtualPtr);
191 
192  /* Map its physical address in the page table provided by the caller */
193  Pte->PageFrameNumber = (PFN_NUMBER)(PhysicalPtr.QuadPart >> PAGE_SHIFT);
194 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
ULONG PFN_NUMBER
Definition: ke.h:8
void * PVOID
Definition: retypes.h:9
ULONG64 PageFrameNumber
Definition: mmtypes.h:78
PHYSICAL_ADDRESS NTAPI MmGetPhysicalAddress(IN PVOID Address)
Definition: stubs.c:685
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by Ki386CreateIdentityMap().

◆ Ki386CreateIdentityMap()

BOOLEAN NTAPI Ki386CreateIdentityMap ( PLARGE_IDENTITY_MAP  IdentityMap,
PVOID  StartPtr,
ULONG  PagesCount 
)

Definition at line 198 of file patpge.c.

199 {
200  ULONG_PTR Ptr;
201  ULONG PteIndex;
202  PHYSICAL_ADDRESS IdentityPtr;
203 
204  /* Zero out the IdentityMap contents */
205  RtlZeroMemory(IdentityMap, sizeof(LARGE_IDENTITY_MAP));
206 
207  /* Get the pointer to the physical address and save it in the struct */
208  IdentityPtr = Ki386BuildIdentityBuffer(IdentityMap, StartPtr, PagesCount);
209  IdentityMap->StartAddress = IdentityPtr.LowPart;
210  if (IdentityMap->StartAddress == 0)
211  {
212  DPRINT1("Failed to get buffer for large pages identity mapping\n");
213  return FALSE;
214  }
215  DPRINT("IdentityMap->StartAddress %p\n", IdentityMap->StartAddress);
216 
217  /* Map all pages */
218  for (Ptr = (ULONG_PTR)StartPtr;
219  Ptr < (ULONG_PTR)StartPtr + PagesCount * PAGE_SIZE;
220  Ptr += PAGE_SIZE, IdentityPtr.QuadPart += PAGE_SIZE)
221  {
222  /* Map virtual address */
223  if (!Ki386MapAddress(IdentityMap, Ptr, IdentityPtr)) return FALSE;
224 
225  /* Map physical address */
226  if (!Ki386MapAddress(IdentityMap, IdentityPtr.LowPart, IdentityPtr)) return FALSE;
227  }
228 
229  /* Convert all PTEs in the page directory from virtual to physical,
230  because Ki386IdentityMapMakeValid mapped only virtual addresses */
231  for (PteIndex = 0; PteIndex < (PAGE_SIZE / sizeof(HARDWARE_PTE)); PteIndex++)
232  {
233  if (IdentityMap->TopLevelDirectory[PteIndex].Valid != 0)
234  Ki386ConvertPte(&IdentityMap->TopLevelDirectory[PteIndex]);
235  }
236 
237  /* Save the page directory address (allocated by Ki386MapAddress) */
238  IdentityMap->Cr3 = MmGetPhysicalAddress(IdentityMap->TopLevelDirectory).LowPart;
239 
240  DPRINT("IdentityMap->Cr3 0x%x\n", IdentityMap->Cr3);
241 
242  return TRUE;
243 }
ULONG64 Valid
Definition: mmtypes.h:66
ULONG_PTR StartAddress
Definition: ke.h:173
#define TRUE
Definition: types.h:120
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
VOID NTAPI Ki386ConvertPte(PHARDWARE_PTE Pte)
Definition: patpge.c:183
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOLEAN NTAPI Ki386MapAddress(PLARGE_IDENTITY_MAP IdentityMap, ULONG_PTR VirtualPtr, PHYSICAL_ADDRESS PhysicalPtr)
Definition: patpge.c:154
#define FALSE
Definition: types.h:117
struct _HARDWARE_PTE HARDWARE_PTE
void DPRINT(...)
Definition: polytest.cpp:61
ULONG LowPart
Definition: typedefs.h:106
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PHARDWARE_PTE TopLevelDirectory
Definition: ke.h:171
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
PHYSICAL_ADDRESS NTAPI Ki386BuildIdentityBuffer(PLARGE_IDENTITY_MAP IdentityMap, PVOID StartPtr, ULONG Length)
Definition: patpge.c:113
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ULONG_PTR
Definition: config.h:101
PHYSICAL_ADDRESS NTAPI MmGetPhysicalAddress(IN PVOID Address)
Definition: stubs.c:685
LONGLONG QuadPart
Definition: typedefs.h:114

◆ Ki386EnableGlobalPage()

INIT_FUNCTION ULONG_PTR NTAPI Ki386EnableGlobalPage ( IN ULONG_PTR  Context)

Definition at line 23 of file patpge.c.

24 {
25  //PLONG Count;
26 #if defined(_GLOBAL_PAGES_ARE_AWESOME_)
27  ULONG Cr4;
28 #endif
30 
31  /* Disable interrupts */
33 
34  /* Spin until other processors are ready */
35  //Count = (PLONG)Context;
36  //InterlockedDecrement(Count);
37  //while (*Count) YieldProcessor();
38 
39 #if defined(_GLOBAL_PAGES_ARE_AWESOME_)
40 
41  /* Get CR4 and ensure global pages are disabled */
42  Cr4 = __readcr4();
43  ASSERT(!(Cr4 & CR4_PGE));
44 
45  /* Reset CR3 to flush the TLB */
47 
48  /* Now enable PGE */
49  __writecr4(Cr4 | CR4_PGE);
50 
51 #endif
52 
53  /* Restore interrupts and return */
55  return 0;
56 }
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1694
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:140
__INTRIN_INLINE unsigned long __readcr3(void)
Definition: intrin_x86.h:1720
#define CR4_PGE
Definition: ketypes.h:91
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1689
unsigned char BOOLEAN
FORCEINLINE BOOLEAN KeDisableInterrupts(VOID)
Definition: ke.h:176
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1727
FORCEINLINE VOID KeRestoreInterrupts(BOOLEAN WereEnabled)
Definition: ke.h:191
unsigned int ULONG
Definition: retypes.h:1

Referenced by KiInitMachineDependent().

◆ Ki386EnableTargetLargePage()

INIT_FUNCTION ULONG_PTR NTAPI Ki386EnableTargetLargePage ( IN ULONG_PTR  Context)

Definition at line 70 of file patpge.c.

71 {
73 
74  /* Call helper function with the start address and temporary page table pointer */
75  Ki386EnableCurrentLargePage(IdentityMap->StartAddress, IdentityMap->Cr3);
76 
77  return 0;
78 }
ULONG_PTR StartAddress
Definition: ke.h:173
VOID NTAPI Ki386EnableCurrentLargePage(IN ULONG_PTR StartAddress, IN ULONG Cr3)
struct _LARGE_IDENTITY_MAP * PLARGE_IDENTITY_MAP

Referenced by KiInitMachineDependent().

◆ Ki386FreeIdentityMap()

VOID NTAPI Ki386FreeIdentityMap ( PLARGE_IDENTITY_MAP  IdentityMap)

Definition at line 247 of file patpge.c.

248 {
249  ULONG Page;
250 
251  DPRINT("Freeing %lu pages allocated for identity mapping\n", IdentityMap->PagesCount);
252 
253  /* Free all allocated pages, if any */
254  for (Page = 0; Page < IdentityMap->PagesCount; Page++)
255  MmFreeContiguousMemory(IdentityMap->PagesList[Page]);
256 }
VOID NTAPI MmFreeContiguousMemory(IN PVOID BaseAddress)
Definition: contmem.c:653
ULONG PagesCount
Definition: ke.h:174
void DPRINT(...)
Definition: polytest.cpp:61
PVOID PagesList[30]
Definition: ke.h:175
unsigned int ULONG
Definition: retypes.h:1

◆ Ki386IdentityMapMakeValid()

BOOLEAN NTAPI Ki386IdentityMapMakeValid ( PLARGE_IDENTITY_MAP  IdentityMap,
PHARDWARE_PTE  Pde,
PHARDWARE_PTE PageTable 
)

Definition at line 123 of file patpge.c.

126 {
127  ULONG NewPage;
128 
129  if (Pde->Valid == 0)
130  {
131  /* Invalid, so allocate a new page for it */
132  NewPage = (ULONG)Ki386AllocateContiguousMemory(IdentityMap, 1, FALSE);
133  if (!NewPage) return FALSE;
134 
135  /* Set PFN to its virtual address and mark it as valid */
136  Pde->PageFrameNumber = NewPage >> PAGE_SHIFT;
137  Pde->Valid = 1;
138 
139  /* Pass page table address to the caller */
140  if (PageTable) *PageTable = (PHARDWARE_PTE)NewPage;
141  }
142  else
143  {
144  /* Valid entry, just pass the page table address to the caller */
145  if (PageTable)
147  }
148 
149  return TRUE;
150 }
ULONG64 Valid
Definition: mmtypes.h:66
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
struct _HARDWARE_PTE * PHARDWARE_PTE
ULONG64 PageFrameNumber
Definition: mmtypes.h:78
PVOID NTAPI Ki386AllocateContiguousMemory(PLARGE_IDENTITY_MAP IdentityMap, ULONG PagesCount, BOOLEAN InLower4Gb)
Definition: patpge.c:82
unsigned int ULONG
Definition: retypes.h:1

Referenced by Ki386MapAddress().

◆ Ki386MapAddress()

BOOLEAN NTAPI Ki386MapAddress ( PLARGE_IDENTITY_MAP  IdentityMap,
ULONG_PTR  VirtualPtr,
PHYSICAL_ADDRESS  PhysicalPtr 
)

Definition at line 154 of file patpge.c.

157 {
158  PHARDWARE_PTE Pde, Pte;
160 
161  /* Allocate page directory on demand */
162  if (!IdentityMap->TopLevelDirectory)
163  {
164  IdentityMap->TopLevelDirectory = Ki386AllocateContiguousMemory(IdentityMap, 1, 1);
165  if (!IdentityMap->TopLevelDirectory) return FALSE;
166  }
167 
168  /* Get PDE of VirtualPtr and make it writable and valid */
169  Pde = &IdentityMap->TopLevelDirectory[(VirtualPtr >> 22) & ((1 << PDE_BITS) - 1)];
170  if (!Ki386IdentityMapMakeValid(IdentityMap, Pde, &PageTable)) return FALSE;
171  Pde->Write = 1;
172 
173  /* Get PTE of VirtualPtr, make it valid, and map PhysicalPtr there */
174  Pte = &PageTable[(VirtualPtr >> 12) & ((1 << PTE_BITS) - 1)];
175  Pte->Valid = 1;
176  Pte->PageFrameNumber = (PFN_NUMBER)(PhysicalPtr.QuadPart >> PAGE_SHIFT);
177 
178  return TRUE;
179 }
ULONG64 Valid
Definition: mmtypes.h:66
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
#define TRUE
Definition: types.h:120
ULONG PFN_NUMBER
Definition: ke.h:8
#define FALSE
Definition: types.h:117
#define PDE_BITS
Definition: patpge.c:15
ULONG64 PageFrameNumber
Definition: mmtypes.h:78
PVOID NTAPI Ki386AllocateContiguousMemory(PLARGE_IDENTITY_MAP IdentityMap, ULONG PagesCount, BOOLEAN InLower4Gb)
Definition: patpge.c:82
PHARDWARE_PTE TopLevelDirectory
Definition: ke.h:171
ULONG64 Write
Definition: mmtypes.h:67
#define PTE_BITS
Definition: patpge.c:16
BOOLEAN NTAPI Ki386IdentityMapMakeValid(PLARGE_IDENTITY_MAP IdentityMap, PHARDWARE_PTE Pde, PHARDWARE_PTE *PageTable)
Definition: patpge.c:123
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by Ki386CreateIdentityMap().

◆ KiInitializePAT()

INIT_FUNCTION VOID NTAPI KiInitializePAT ( VOID  )

Definition at line 61 of file patpge.c.

62 {
63  /* FIXME: Support this */
64  DPRINT("PAT support detected but not yet taken advantage of\n");
65 }
void DPRINT(...)
Definition: polytest.cpp:61

Referenced by KiInitMachineDependent().