ReactOS  0.4.13-dev-544-gede3fdd
memory.c File Reference
#include <hal.h>
#include <debug.h>
Include dependency graph for memory.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define MM_HAL_HEAP_START   (PVOID)(MM_HAL_VA_START + (1024 * 1024))
 

Functions

ULONG64 NTAPI HalpAllocPhysicalMemory (IN PLOADER_PARAMETER_BLOCK LoaderBlock, IN ULONG64 MaxAddress, IN PFN_NUMBER PageCount, IN BOOLEAN Aligned)
 
PVOID NTAPI HalpMapPhysicalMemory64 (IN PHYSICAL_ADDRESS PhysicalAddress, IN PFN_COUNT PageCount)
 
VOID NTAPI HalpUnmapVirtualAddress (IN PVOID VirtualAddress, IN PFN_COUNT PageCount)
 
PVOID NTAPI HalpMapPhysicalMemory64Vista (IN PHYSICAL_ADDRESS PhysicalAddress, IN PFN_COUNT PageCount, IN BOOLEAN FlushCurrentTLB)
 
VOID NTAPI HalpUnmapVirtualAddressVista (IN PVOID VirtualAddress, IN PFN_COUNT PageCount, IN BOOLEAN FlushCurrentTLB)
 

Variables

ULONG HalpUsedAllocDescriptors
 
MEMORY_ALLOCATION_DESCRIPTOR HalpAllocationDescriptorArray [64]
 
PVOID HalpHeapStart = MM_HAL_HEAP_START
 

Macro Definition Documentation

◆ MM_HAL_HEAP_START

#define MM_HAL_HEAP_START   (PVOID)(MM_HAL_VA_START + (1024 * 1024))

Definition at line 16 of file memory.c.

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file memory.c.

Function Documentation

◆ HalpAllocPhysicalMemory()

ULONG64 NTAPI HalpAllocPhysicalMemory ( IN PLOADER_PARAMETER_BLOCK  LoaderBlock,
IN ULONG64  MaxAddress,
IN PFN_NUMBER  PageCount,
IN BOOLEAN  Aligned 
)

Definition at line 29 of file memory.c.

33 {
34  ULONG UsedDescriptors;
36  PFN_NUMBER MaxPage, BasePage, Alignment;
37  PLIST_ENTRY NextEntry;
38  PMEMORY_ALLOCATION_DESCRIPTOR MdBlock, NewBlock, FreeBlock;
39 
40  /* Highest page we'll go */
41  MaxPage = MaxAddress >> PAGE_SHIFT;
42 
43  /* We need at least two blocks */
44  if ((HalpUsedAllocDescriptors + 2) > 64) return 0;
45 
46  /* Remember how many we have now */
47  UsedDescriptors = HalpUsedAllocDescriptors;
48 
49  /* Loop the loader block memory descriptors */
50  NextEntry = LoaderBlock->MemoryDescriptorListHead.Flink;
51  while (NextEntry != &LoaderBlock->MemoryDescriptorListHead)
52  {
53  /* Get the block */
54  MdBlock = CONTAINING_RECORD(NextEntry,
56  ListEntry);
57 
58  /* No alignment by default */
59  Alignment = 0;
60 
61  /* Unless requested, in which case we use a 64KB block alignment */
62  if (Aligned) Alignment = ((MdBlock->BasePage + 0x0F) & ~0x0F) - MdBlock->BasePage;
63 
64  /* Search for free memory */
65  if ((MdBlock->MemoryType == LoaderFree) ||
66  (MdBlock->MemoryType == LoaderFirmwareTemporary))
67  {
68  /* Make sure the page is within bounds, including alignment */
69  BasePage = MdBlock->BasePage;
70  if ((BasePage) &&
71  (MdBlock->PageCount >= PageCount + Alignment) &&
72  (BasePage + PageCount + Alignment < MaxPage))
73  {
74  /* We found an address */
75  PhysicalAddress = ((ULONG64)BasePage + Alignment) << PAGE_SHIFT;
76  break;
77  }
78  }
79 
80  /* Keep trying */
81  NextEntry = NextEntry->Flink;
82  }
83 
84  /* If we didn't find anything, get out of here */
85  if (NextEntry == &LoaderBlock->MemoryDescriptorListHead) return 0;
86 
87  /* Okay, now get a descriptor */
89  NewBlock->PageCount = (ULONG)PageCount;
90  NewBlock->BasePage = MdBlock->BasePage + Alignment;
92 
93  /* Update count */
94  UsedDescriptors++;
95  HalpUsedAllocDescriptors = UsedDescriptors;
96 
97  /* Check if we had any alignment */
98  if (Alignment)
99  {
100  /* Check if we had leftovers */
101  if (MdBlock->PageCount > (PageCount + Alignment))
102  {
103  /* Get the next descriptor */
104  FreeBlock = &HalpAllocationDescriptorArray[UsedDescriptors];
105  FreeBlock->PageCount = MdBlock->PageCount - Alignment - (ULONG)PageCount;
106  FreeBlock->BasePage = MdBlock->BasePage + Alignment + (ULONG)PageCount;
107 
108  /* One more */
110 
111  /* Insert it into the list */
112  InsertHeadList(&MdBlock->ListEntry, &FreeBlock->ListEntry);
113  }
114 
115  /* Trim the original block to the alignment only */
116  MdBlock->PageCount = Alignment;
117 
118  /* Insert the descriptor after the original one */
119  InsertHeadList(&MdBlock->ListEntry, &NewBlock->ListEntry);
120  }
121  else
122  {
123  /* Consume memory from this block */
124  MdBlock->BasePage += (ULONG)PageCount;
125  MdBlock->PageCount -= (ULONG)PageCount;
126 
127  /* Insert the descriptor before the original one */
128  InsertTailList(&MdBlock->ListEntry, &NewBlock->ListEntry);
129 
130  /* Remove the entry if the whole block was allocated */
131  if (MdBlock->PageCount == 0) RemoveEntryList(&MdBlock->ListEntry);
132  }
133 
134  /* Return the address */
135  return PhysicalAddress;
136 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
ULONG HalpUsedAllocDescriptors
Definition: memory.c:20
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
#define InsertTailList(ListHead, Entry)
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
TYPE_OF_MEMORY MemoryType
Definition: arc.h:193
ULONG PFN_NUMBER
Definition: ke.h:8
union Alignment_ Alignment
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
unsigned __int64 ULONG64
Definition: imports.h:198
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1060
Definition: typedefs.h:117
unsigned int ULONG
Definition: retypes.h:1
MEMORY_ALLOCATION_DESCRIPTOR HalpAllocationDescriptorArray[64]
Definition: memory.c:21

Referenced by HalpAcpiCopyBiosTable(), HalpAcpiFindRsdtPhase0(), and HalpSetupAcpiPhase0().

◆ HalpMapPhysicalMemory64()

PVOID NTAPI HalpMapPhysicalMemory64 ( IN PHYSICAL_ADDRESS  PhysicalAddress,
IN PFN_COUNT  PageCount 
)

Definition at line 140 of file memory.c.

142 {
144 }
PVOID NTAPI HalpMapPhysicalMemory64Vista(IN PHYSICAL_ADDRESS PhysicalAddress, IN PFN_COUNT PageCount, IN BOOLEAN FlushCurrentTLB)
Definition: memory.c:156
#define TRUE
Definition: types.h:120
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1060

◆ HalpMapPhysicalMemory64Vista()

PVOID NTAPI HalpMapPhysicalMemory64Vista ( IN PHYSICAL_ADDRESS  PhysicalAddress,
IN PFN_COUNT  PageCount,
IN BOOLEAN  FlushCurrentTLB 
)

Definition at line 156 of file memory.c.

159 {
160  PHARDWARE_PTE PointerPte;
161  PFN_NUMBER UsedPages = 0;
163 
164  /* Start at the current HAL heap base */
167 
168  /* Loop until we have all the pages required */
169  while (UsedPages < PageCount)
170  {
171  /* If this overflows past the HAL heap, it means there's no space */
172  if (VirtualAddress == NULL) return NULL;
173 
174  /* Get the PTE for this address */
175  PointerPte = HalAddressToPte(VirtualAddress);
176 
177  /* Go to the next page */
179 
180  /* Check if the page is available */
181  if (PointerPte->Valid)
182  {
183  /* PTE has data, skip it and start with a new base address */
185  UsedPages = 0;
186  continue;
187  }
188 
189  /* PTE is available, keep going on this run */
190  UsedPages++;
191  }
192 
193  /* Take the base address of the page plus the actual offset in the address */
196 
197  /* If we are starting at the heap, move the heap */
198  if (BaseAddress == HalpHeapStart)
199  {
200  /* Past this allocation */
201  HalpHeapStart = (PVOID)((ULONG_PTR)BaseAddress + (PageCount * PAGE_SIZE));
202  }
203 
204  /* Loop pages that can be mapped */
205  while (UsedPages--)
206  {
207  /* Fill out the PTE */
208  PointerPte = HalAddressToPte(BaseAddress);
210  PointerPte->Valid = 1;
211  PointerPte->Write = 1;
212 
213  /* Move to the next address */
216  }
217 
218  /* Flush the TLB and return the address */
219  if (FlushCurrentTLB)
220  HalpFlushTLB();
221 
222  return VirtualAddress;
223 }
ULONG64 Valid
Definition: mmtypes.h:66
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
VOID NTAPI HalpFlushTLB(VOID)
Definition: misc.c:163
#define HalAddressToPte(x)
Definition: halp.h:523
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1060
ULONG64 PageFrameNumber
Definition: mmtypes.h:78
ULONG LowPart
Definition: typedefs.h:104
#define PAGE_SIZE
Definition: env_spec_w32.h:49
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791
PVOID HalpHeapStart
Definition: memory.c:22
ULONG64 Write
Definition: mmtypes.h:67
#define BYTE_OFFSET(Va)
LONGLONG QuadPart
Definition: typedefs.h:112

Referenced by HalpMapPhysicalMemory64(), and HalpRegisterKdSupportFunctions().

◆ HalpUnmapVirtualAddress()

VOID NTAPI HalpUnmapVirtualAddress ( IN PVOID  VirtualAddress,
IN PFN_COUNT  PageCount 
)

Definition at line 148 of file memory.c.

150 {
152 }
#define TRUE
Definition: types.h:120
VOID NTAPI HalpUnmapVirtualAddressVista(IN PVOID VirtualAddress, IN PFN_COUNT PageCount, IN BOOLEAN FlushCurrentTLB)
Definition: memory.c:227
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791

◆ HalpUnmapVirtualAddressVista()

VOID NTAPI HalpUnmapVirtualAddressVista ( IN PVOID  VirtualAddress,
IN PFN_COUNT  PageCount,
IN BOOLEAN  FlushCurrentTLB 
)

Definition at line 227 of file memory.c.

230 {
231  PHARDWARE_PTE PointerPte;
232  ULONG i;
233 
234  /* Only accept valid addresses */
235  if (VirtualAddress < (PVOID)MM_HAL_VA_START) return;
236 
237  /* Align it down to page size */
239 
240  /* Loop PTEs */
241  PointerPte = HalAddressToPte(VirtualAddress);
242  for (i = 0; i < PageCount; i++)
243  {
244  *(PULONG)PointerPte = 0;
245  PointerPte++;
246  }
247 
248  /* Flush the TLB */
249  if (FlushCurrentTLB)
250  HalpFlushTLB();
251 
252  /* Put the heap back */
254 }
#define MM_HAL_VA_START
Definition: ketypes.h:256
VOID NTAPI HalpFlushTLB(VOID)
Definition: misc.c:163
#define HalAddressToPte(x)
Definition: halp.h:523
uint32_t ULONG_PTR
Definition: typedefs.h:63
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
void * PVOID
Definition: retypes.h:9
#define PAGE_SIZE
Definition: env_spec_w32.h:49
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791
PVOID HalpHeapStart
Definition: memory.c:22
unsigned int * PULONG
Definition: retypes.h:1
unsigned int ULONG
Definition: retypes.h:1

Referenced by HalpRegisterKdSupportFunctions(), and HalpUnmapVirtualAddress().

Variable Documentation

◆ HalpAllocationDescriptorArray

MEMORY_ALLOCATION_DESCRIPTOR HalpAllocationDescriptorArray[64]

Definition at line 21 of file memory.c.

Referenced by HalpAllocPhysicalMemory().

◆ HalpHeapStart

PVOID HalpHeapStart = MM_HAL_HEAP_START

Definition at line 22 of file memory.c.

Referenced by HalpMapPhysicalMemory64Vista(), and HalpUnmapVirtualAddressVista().

◆ HalpUsedAllocDescriptors

ULONG HalpUsedAllocDescriptors

Definition at line 20 of file memory.c.

Referenced by HalpAllocPhysicalMemory().