Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

533 lines
14 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. xxmemory.c
  5. Abstract:
  6. Provides routines to allow the HAL to map physical memory.
  7. Author:
  8. John Vert (jvert) 3-Sep-1991
  9. Environment:
  10. Phase 0 initialization only.
  11. Revision History:
  12. --*/
  13. //
  14. // This module is compatible with PAE mode and therefore treats physical
  15. // addresses as 64-bit entities.
  16. //
  17. #if !defined(_PHYS64_)
  18. #define _PHYS64_
  19. #endif
  20. #include "halp.h"
  21. #ifdef ALLOC_PRAGMA
  22. #pragma alloc_text(INIT,HalpAllocPhysicalMemory)
  23. #endif
  24. #define EXTRA_ALLOCATION_DESCRIPTORS 64
  25. MEMORY_ALLOCATION_DESCRIPTOR
  26. HalpAllocationDescriptorArray[ EXTRA_ALLOCATION_DESCRIPTORS ];
  27. ULONG HalpUsedAllocDescriptors = 0;
  28. //
  29. // Almost all of the last 4Mb of memory are available to the HAL to map
  30. // physical memory. The kernel may use a couple of PTEs in this area for
  31. // special purposes, so skip any which are not zero.
  32. //
  33. // Note that the HAL's heap only uses the last 3Mb. This is so we can
  34. // reserve the first 1Mb for use if we have to return to real mode.
  35. // In order to return to real mode we need to identity-map the first 1Mb of
  36. // physical memory.
  37. //
  38. #define HAL_HEAP_START ((PVOID)(((ULONG_PTR)MM_HAL_RESERVED) + 1024 * 1024))
  39. PVOID HalpHeapStart=HAL_HEAP_START;
  40. PVOID
  41. HalpMapPhysicalMemory64(
  42. IN PHYSICAL_ADDRESS PhysicalAddress,
  43. IN ULONG NumberPages
  44. )
  45. /*++
  46. Routine Description:
  47. This routine maps physical memory into the area of virtual memory
  48. reserved for the HAL. It does this by directly inserting the PTE
  49. into the Page Table which the OS Loader has provided.
  50. N.B. This routine does *NOT* update the MemoryDescriptorList. The
  51. caller is responsible for either removing the appropriate
  52. physical memory from the list, or creating a new descriptor to
  53. describe it.
  54. Arguments:
  55. PhysicalAddress - Supplies the physical address of the start of the
  56. area of physical memory to be mapped.
  57. NumberPages - Supplies the number of pages contained in the area of
  58. physical memory to be mapped.
  59. Return Value:
  60. PVOID - Virtual address at which the requested block of physical memory
  61. was mapped
  62. NULL - The requested block of physical memory could not be mapped.
  63. --*/
  64. {
  65. PHARDWARE_PTE PTE;
  66. ULONG PagesMapped;
  67. PVOID VirtualAddress;
  68. PVOID RangeStart;
  69. //
  70. // The OS Loader sets up hyperspace for us, so we know that the Page
  71. // Tables are magically mapped starting at V.A. 0xC0000000.
  72. //
  73. PagesMapped = 0;
  74. RangeStart = HalpHeapStart;
  75. while (PagesMapped < NumberPages) {
  76. //
  77. // Look for enough consecutive free ptes to honor mapping
  78. //
  79. PagesMapped = 0;
  80. VirtualAddress = RangeStart;
  81. //
  82. // If RangeStart has wrapped, there are not enough free pages
  83. // available.
  84. //
  85. if (RangeStart == NULL) {
  86. return NULL;
  87. }
  88. while (PagesMapped < NumberPages) {
  89. PTE=MiGetPteAddress(VirtualAddress);
  90. if (HalpIsPteFree(PTE) == FALSE) {
  91. //
  92. // Pte is not free, skip up to the next pte and start over
  93. //
  94. RangeStart = (PVOID) ((ULONG_PTR)VirtualAddress + PAGE_SIZE);
  95. break;
  96. }
  97. VirtualAddress = (PVOID) ((ULONG_PTR)VirtualAddress + PAGE_SIZE);
  98. PagesMapped++;
  99. }
  100. }
  101. VirtualAddress = (PVOID) ((ULONG_PTR) RangeStart |
  102. BYTE_OFFSET (PhysicalAddress.LowPart));
  103. if (RangeStart == HalpHeapStart) {
  104. //
  105. // Push the start of heap beyond this range.
  106. //
  107. HalpHeapStart = (PVOID)((ULONG_PTR)RangeStart + (NumberPages * PAGE_SIZE));
  108. }
  109. while (PagesMapped) {
  110. PTE=MiGetPteAddress(RangeStart);
  111. HalpSetPageFrameNumber( PTE, PhysicalAddress.QuadPart >> PAGE_SHIFT );
  112. PTE->Valid = 1;
  113. PTE->Write = 1;
  114. PhysicalAddress.QuadPart += PAGE_SIZE;
  115. RangeStart = (PVOID)((ULONG_PTR)RangeStart + PAGE_SIZE);
  116. --PagesMapped;
  117. }
  118. //
  119. // Flush TLB
  120. //
  121. HalpFlushTLB ();
  122. return(VirtualAddress);
  123. }
  124. PVOID
  125. HalpMapPhysicalMemoryWriteThrough64(
  126. IN PHYSICAL_ADDRESS PhysicalAddress,
  127. IN ULONG NumberPages
  128. )
  129. /*++
  130. Routine Description:
  131. Maps a physical memory address into virtual space, same as
  132. HalpMapPhysicalMemory(). The difference is that this routine
  133. marks the pages as PCD/PWT so that writes to the memory mapped registers
  134. mapped here won't get delayed in the internal write-back caches.
  135. Arguments:
  136. PhysicalAddress - Supplies a physical address of the memory to be mapped
  137. NumberPages - Number of pages to map
  138. Return Value:
  139. Virtual address pointer to the requested physical address
  140. --*/
  141. {
  142. ULONG Index;
  143. PHARDWARE_PTE PTE;
  144. PVOID VirtualAddress;
  145. VirtualAddress = HalpMapPhysicalMemory(PhysicalAddress, NumberPages);
  146. PTE = MiGetPteAddress(VirtualAddress);
  147. for (Index = 0; Index < NumberPages; Index++, HalpIncrementPte(&PTE)) {
  148. PTE->CacheDisable = 1;
  149. PTE->WriteThrough = 1;
  150. }
  151. return VirtualAddress;
  152. }
  153. PVOID
  154. HalpRemapVirtualAddress64(
  155. IN PVOID VirtualAddress,
  156. IN PHYSICAL_ADDRESS PhysicalAddress,
  157. IN BOOLEAN WriteThrough
  158. )
  159. /*++
  160. Routine Description:
  161. This routine remaps a PTE to the physical memory address provided.
  162. Arguments:
  163. PhysicalAddress - Supplies the physical address of the area to be mapped
  164. VirtualAddress - Valid address to be remapped
  165. WriteThrough - Map as cachable or WriteThrough
  166. Return Value:
  167. PVOID - Virtual address at which the requested block of physical memory
  168. was mapped
  169. NULL - The requested block of physical memory could not be mapped.
  170. --*/
  171. {
  172. PHARDWARE_PTE PTE;
  173. PTE = MiGetPteAddress (VirtualAddress);
  174. HalpSetPageFrameNumber( PTE, PhysicalAddress.QuadPart >> PAGE_SHIFT );
  175. PTE->Valid = 1;
  176. PTE->Write = 1;
  177. if (WriteThrough) {
  178. PTE->CacheDisable = 1;
  179. PTE->WriteThrough = 1;
  180. }
  181. //
  182. // Flush TLB
  183. //
  184. HalpFlushTLB();
  185. return(VirtualAddress);
  186. }
  187. VOID
  188. HalpUnmapVirtualAddress(
  189. IN PVOID VirtualAddress,
  190. IN ULONG NumberPages
  191. )
  192. /*++
  193. Routine Description:
  194. This routine unmaps a PTE.
  195. Arguments:
  196. VirtualAddress - Valid address to be remapped
  197. NumberPages - No of pages to be unmapped
  198. Return Value:
  199. None.
  200. --*/
  201. {
  202. PHARDWARE_PTE Pte;
  203. PULONG PtePtr;
  204. ULONG Index;
  205. if (VirtualAddress < HAL_HEAP_START)
  206. return;
  207. VirtualAddress = (PVOID)((ULONG_PTR)VirtualAddress & ~(PAGE_SIZE - 1));
  208. Pte = MiGetPteAddress (VirtualAddress);
  209. for (Index = 0; Index < NumberPages; Index++, HalpIncrementPte(&Pte)) {
  210. HalpFreePte( Pte );
  211. }
  212. //
  213. // Flush TLB
  214. //
  215. HalpFlushTLB();
  216. //
  217. // Realign heap start so that VA space can be reused
  218. //
  219. if (HalpHeapStart > VirtualAddress) {
  220. HalpHeapStart = VirtualAddress;
  221. }
  222. }
  223. ULONG
  224. HalpAllocPhysicalMemory(
  225. IN PLOADER_PARAMETER_BLOCK LoaderBlock,
  226. IN ULONG MaxPhysicalAddress,
  227. IN ULONG NoPages,
  228. IN BOOLEAN bAlignOn64k
  229. )
  230. /*++
  231. Routine Description:
  232. Carves out N pages of physical memory from the memory descriptor
  233. list in the desired location. This function is to be called only
  234. during phase zero initialization. (ie, before the kernel's memory
  235. management system is running)
  236. Arguments:
  237. MaxPhysicalAddress - The max address where the physical memory can be
  238. NoPages - Number of pages to allocate
  239. bAlignOn64k - Whether caller wants resulting pages to be allocated
  240. on a 64k byte boundry
  241. Return Value:
  242. The physical address or NULL if the memory could not be obtained.
  243. --*/
  244. {
  245. PMEMORY_ALLOCATION_DESCRIPTOR Descriptor;
  246. PMEMORY_ALLOCATION_DESCRIPTOR NewDescriptor;
  247. PMEMORY_ALLOCATION_DESCRIPTOR TailDescriptor;
  248. PLIST_ENTRY NextMd;
  249. ULONG AlignmentOffset;
  250. ULONG MaxPageAddress;
  251. ULONG PhysicalAddress;
  252. MaxPageAddress = MaxPhysicalAddress >> PAGE_SHIFT;
  253. if ((HalpUsedAllocDescriptors + 2) > EXTRA_ALLOCATION_DESCRIPTORS) {
  254. //
  255. // This allocation will require one or more additional
  256. // descriptors, but we don't have that many in our static
  257. // array. Fail the request.
  258. //
  259. // Note: Depending on the state of the existing descriptor
  260. // list it is possible that this allocation would not
  261. // need an two additional descriptor blocks. However in
  262. // the interest of repeatability and ease of testing we
  263. // will fail the request now anyway, rather than a
  264. // smaller number of configuration-dependent failures.
  265. //
  266. ASSERT(FALSE);
  267. return 0;
  268. }
  269. //
  270. // Scan the memory allocation descriptors and allocate map buffers
  271. //
  272. NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
  273. while (NextMd != &LoaderBlock->MemoryDescriptorListHead) {
  274. Descriptor = CONTAINING_RECORD(NextMd,
  275. MEMORY_ALLOCATION_DESCRIPTOR,
  276. ListEntry);
  277. AlignmentOffset = bAlignOn64k ?
  278. ((Descriptor->BasePage + 0x0f) & ~0x0f) - Descriptor->BasePage :
  279. 0;
  280. //
  281. // Search for a block of memory which is contains a memory chuck
  282. // that is greater than size pages, and has a physical address less
  283. // than MAXIMUM_PHYSICAL_ADDRESS.
  284. //
  285. if ((Descriptor->MemoryType == LoaderFree ||
  286. Descriptor->MemoryType == MemoryFirmwareTemporary) &&
  287. (Descriptor->BasePage) &&
  288. (Descriptor->PageCount >= NoPages + AlignmentOffset) &&
  289. (Descriptor->BasePage + NoPages + AlignmentOffset < MaxPageAddress)) {
  290. PhysicalAddress =
  291. (Descriptor->BasePage + AlignmentOffset) << PAGE_SHIFT;
  292. break;
  293. }
  294. NextMd = NextMd->Flink;
  295. }
  296. //
  297. // Use the extra descriptor to define the memory at the end of the
  298. // original block.
  299. //
  300. ASSERT(NextMd != &LoaderBlock->MemoryDescriptorListHead);
  301. if (NextMd == &LoaderBlock->MemoryDescriptorListHead)
  302. return 0;
  303. //
  304. // The new descriptor will describe the memory being allocated as
  305. // having been reserved.
  306. //
  307. NewDescriptor =
  308. &HalpAllocationDescriptorArray[ HalpUsedAllocDescriptors];
  309. NewDescriptor->PageCount = NoPages;
  310. NewDescriptor->BasePage = Descriptor->BasePage + AlignmentOffset;
  311. NewDescriptor->MemoryType = LoaderHALCachedMemory;
  312. HalpUsedAllocDescriptors++;
  313. //
  314. // Adjust the existing memory descriptors and insert the new one
  315. // describing the allocation.
  316. //
  317. if (AlignmentOffset == 0) {
  318. //
  319. // Trim the source descriptor and insert the allocation
  320. // descriptor before it.
  321. //
  322. Descriptor->BasePage += NoPages;
  323. Descriptor->PageCount -= NoPages;
  324. InsertTailList(
  325. &Descriptor->ListEntry,
  326. &NewDescriptor->ListEntry
  327. );
  328. if (Descriptor->PageCount == 0) {
  329. //
  330. // The whole block was allocated,
  331. // Remove the entry from the list completely.
  332. //
  333. // NOTE: This descriptor can't be recycled or freed since
  334. // we don't know the allocator.
  335. //
  336. RemoveEntryList(&Descriptor->ListEntry);
  337. }
  338. } else {
  339. if (Descriptor->PageCount - NoPages - AlignmentOffset) {
  340. //
  341. // This allocation is coming out of the middle of a descriptor
  342. // block. We can use the existing descriptor block to describe
  343. // the head portion, but we will need a new one to describe the
  344. // tail.
  345. //
  346. // Allocate one from the array in the data segment. The check
  347. // at the top of the function ensures that one is available.
  348. //
  349. TailDescriptor =
  350. &HalpAllocationDescriptorArray[ HalpUsedAllocDescriptors];
  351. //
  352. // The extra descriptor is needed so intialize it and insert
  353. // it in the list.
  354. //
  355. TailDescriptor->PageCount =
  356. Descriptor->PageCount - NoPages - AlignmentOffset;
  357. TailDescriptor->BasePage =
  358. Descriptor->BasePage + NoPages + AlignmentOffset;
  359. TailDescriptor->MemoryType = MemoryFree;
  360. HalpUsedAllocDescriptors++;
  361. InsertHeadList(
  362. &Descriptor->ListEntry,
  363. &TailDescriptor->ListEntry
  364. );
  365. }
  366. //
  367. // Use the current entry as the descriptor for the first block.
  368. //
  369. Descriptor->PageCount = AlignmentOffset;
  370. //
  371. // Insert the allocation descriptor after the original
  372. // descriptor but before the tail descriptor if one was necessary.
  373. //
  374. InsertHeadList(
  375. &Descriptor->ListEntry,
  376. &NewDescriptor->ListEntry
  377. );
  378. }
  379. return PhysicalAddress;
  380. }