Source code of Windows XP (NT5)
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.

2333 lines
58 KiB

  1. /*++
  2. Copyright (c) 1990, 1991 Microsoft Corporation
  3. Module Name:
  4. memory.c
  5. Abstract:
  6. This module sets up paging so that the first 1Mb of virtual memory is
  7. directly mapped to the first 1Mb of physical memory. This allows the
  8. BIOS callbacks to work, and the osloader to continue running below
  9. 1Mb. It also maps up to the first 16MB of physical memory to KSEG0_BASE
  10. and ALTERNATE_BASE, so the osloader can load kernel code into kernel
  11. space, and allocate kernel parameters in kernel space. This allows the
  12. dynamic configuration of the system for either a 2gb or 3gb user space
  13. address range.
  14. Note!!
  15. 3/16/00 (mikeg):
  16. All I/O (BlRead etc.) use a buffer below 1MB to do transfers so we
  17. don't need to worry about ISA cards DMA buffers. This change allows
  18. setupldr to run with all files compressed.
  19. If you need to change this, also change BASE_LOADER_IMAGE in bootx86.h
  20. or the PDE will not be completely unmapped. This must ALSO match
  21. ntos\mm\i386\mi386.h (BOOT_IMAGE_SIZE) so we know where
  22. to start loading images.
  23. Memory Map used by NTLDR:
  24. 000000 - 000fff RM IDT & Bios Data Area
  25. 007C00 - 007fff BPB loaded by Bootstrap
  26. 010000 - 01ffff Loadable miniport drivers, free memory
  27. 020000 - 02ffff SU + real-mode stack
  28. 030000 - 039000 BIOS disk cache
  29. 039000 - 039000 Permanent heap (GDT, IDT, TSS, Page Dir, Page Tables)
  30. (grows up)
  31. |
  32. v
  33. ^
  34. |
  35. (grows down)
  36. 039000 - 05ffff Temporary heap
  37. 060000 - 062000 osloader stack (grows down)
  38. 062000 - 09ffff osloader heap (grows down)
  39. 0b8000 - 0bbfff Video Buffer
  40. 0d0000 - 0fffff Bios and Adaptor ROM area
  41. Author:
  42. John Vert (jvert) 18-Jun-1991
  43. Environment:
  44. Kernel Mode
  45. Revision History:
  46. --*/
  47. #include "arccodes.h"
  48. #include "bootx86.h"
  49. //
  50. // 1-megabyte boundary line (in pages)
  51. //
  52. #define _1MB ((ULONG)0x100000 >> PAGE_SHIFT)
  53. //
  54. // 4-gigabyte boundary line (in pages)
  55. //
  56. #define _4G (1 << (32 - PAGE_SHIFT))
  57. //
  58. // Bogus memory line. (We don't ever want to use the memory that is in
  59. // the 0x40 pages just under the 16Mb line.)
  60. //
  61. #define _16MB_BOGUS (((ULONG)0x1000000-0x40*PAGE_SIZE) >> PAGE_SHIFT)
  62. #define ROM_START_PAGE (0x0A0000 >> PAGE_SHIFT)
  63. #define ROM_END_PAGE (0x100000 >> PAGE_SHIFT)
  64. //
  65. // Buffer for temporary storage of data read from the disk that needs
  66. // to end up in a location above the 1MB boundary.
  67. //
  68. // NOTE: it is very important that this buffer not cross a 64k boundary.
  69. //
  70. PUCHAR FwDiskCache = (PUCHAR)(BIOS_DISK_CACHE_START * PAGE_SIZE);
  71. //
  72. // Current heap start pointers (physical addresses)
  73. // Note that 0x50000 to 0x5ffff is reserved for detection configuration memory
  74. //
  75. ULONG FwPermanentHeap = PERMANENT_HEAP_START * PAGE_SIZE;
  76. ULONG FwTemporaryHeap = (TEMPORARY_HEAP_START - 0x10) * PAGE_SIZE;
  77. //
  78. // Current pool pointers. This is different than the temporary/permanent
  79. // heaps, because it is not required to be under 1MB. It is used by the
  80. // SCSI miniports for allocating their extensions and for the dbcs font image.
  81. //
  82. #define FW_POOL_SIZE 96
  83. ULONG FwPoolStart;
  84. ULONG FwPoolEnd;
  85. //
  86. // This gets set to FALSE right before we call into the osloader, so we
  87. // know that the fw memory descriptors can no longer be changed at will.
  88. //
  89. BOOLEAN FwDescriptorsValid = TRUE;
  90. ULONG HighestPde=((_16MB << PAGE_SHIFT) >> PDI_SHIFT);
  91. //
  92. // Private function prototypes
  93. //
  94. ARC_STATUS
  95. MempCopyGdt(
  96. VOID
  97. );
  98. ARC_STATUS
  99. MempSetupPaging(
  100. IN ULONG StartPage,
  101. IN ULONG NumberOfPages
  102. );
  103. VOID
  104. MempDisablePages(
  105. VOID
  106. );
  107. ARC_STATUS
  108. MempTurnOnPaging(
  109. VOID
  110. );
  111. ARC_STATUS
  112. MempSetDescriptorRegion (
  113. IN ULONG StartPage,
  114. IN ULONG EndPage,
  115. IN TYPE_OF_MEMORY MemoryType
  116. );
  117. ARC_STATUS
  118. MempSetPageMappingOverride(
  119. IN ULONG StartPage,
  120. IN ULONG NumberOfPages,
  121. IN BOOLEAN Enable
  122. );
  123. extern
  124. void
  125. BlpTrackUsage (
  126. MEMORY_TYPE MemoryType,
  127. ULONG ActualBase,
  128. ULONG NumberPages
  129. );
  130. //
  131. // Global - memory management variables.
  132. //
  133. PHARDWARE_PTE PDE;
  134. PHARDWARE_PTE HalPT;
  135. #define MAX_DESCRIPTORS 60
  136. MEMORY_DESCRIPTOR MDArray[MAX_DESCRIPTORS]; // Memory Descriptor List
  137. ULONG NumberDescriptors=0;
  138. ARC_STATUS
  139. InitializeMemorySubsystem(
  140. PBOOT_CONTEXT BootContext
  141. )
  142. /*++
  143. Routine Description:
  144. The initial heap is mapped and allocated. Pointers to the
  145. Page directory and page tables are initialized.
  146. Arguments:
  147. BootContext - Supplies basic information provided by SU module.
  148. Returns:
  149. ESUCCESS - Memory succesfully initialized.
  150. --*/
  151. {
  152. ARC_STATUS Status;
  153. PSU_MEMORY_DESCRIPTOR SuMemory;
  154. ULONG PageStart;
  155. ULONG PageEnd;
  156. ULONG RomStart = ROM_START_PAGE;
  157. ULONG LoaderStart;
  158. ULONG LoaderEnd;
  159. ULONG BAddr, EAddr, BRound, ERound;
  160. //
  161. // Start by creating memory descriptors to describe all of the memory
  162. // we know about. Then setup the page tables. Finally, allocate
  163. // descriptors that describe our memory layout.
  164. //
  165. //
  166. // We know that one of the SU descriptors is for < 1Mb,
  167. // and we don't care about that, since we know everything we'll run
  168. // on will have at least 1Mb of memory. The rest are for extended
  169. // memory, and those are the ones we are interested in.
  170. //
  171. SuMemory = BootContext->MemoryDescriptorList;
  172. while (SuMemory->BlockSize != 0) {
  173. BAddr = SuMemory->BlockBase;
  174. EAddr = BAddr + SuMemory->BlockSize - 1;
  175. //
  176. // Round the starting address to a page boundry.
  177. //
  178. BRound = BAddr & (ULONG) (PAGE_SIZE - 1);
  179. if (BRound) {
  180. BAddr = BAddr + PAGE_SIZE - BRound;
  181. }
  182. //
  183. // Round the ending address to a page boundry minus 1
  184. //
  185. ERound = (EAddr + 1) & (ULONG) (PAGE_SIZE - 1);
  186. if (ERound) {
  187. EAddr -= ERound;
  188. }
  189. //
  190. // Covert begining & ending address to page
  191. //
  192. PageStart = BAddr >> PAGE_SHIFT;
  193. PageEnd = (EAddr + 1) >> PAGE_SHIFT;
  194. //
  195. // If this memory descriptor describes conventional ( <640k )
  196. // memory, then assume the ROM starts immediately after it
  197. // ends.
  198. //
  199. if (PageStart == 0) {
  200. RomStart = PageEnd;
  201. }
  202. //
  203. // If PageStart was rounded up to a page boundry, then add
  204. // the fractional page as SpecialMemory
  205. //
  206. if (BRound) {
  207. Status = MempSetDescriptorRegion (
  208. PageStart - 1,
  209. PageStart,
  210. MemorySpecialMemory
  211. );
  212. if (Status != ESUCCESS) {
  213. break;
  214. }
  215. }
  216. //
  217. // If PageEnd was rounded down to a page boundry, then add
  218. // the fractional page as SpecialMemory
  219. //
  220. if (ERound) {
  221. Status = MempSetDescriptorRegion (
  222. PageEnd,
  223. PageEnd + 1,
  224. MemorySpecialMemory
  225. );
  226. if (Status != ESUCCESS) {
  227. break;
  228. }
  229. //
  230. // RomStart starts after the reserved page
  231. //
  232. if (RomStart == PageEnd) {
  233. RomStart += 1;
  234. }
  235. }
  236. //
  237. // Add memory range PageStart though PageEnd
  238. //
  239. if (PageEnd <= _16MB_BOGUS) {
  240. //
  241. // This memory descriptor is all below the 16MB_BOGUS mark
  242. //
  243. Status = MempSetDescriptorRegion( PageStart, PageEnd, MemoryFree );
  244. } else if (PageStart >= _16MB) {
  245. //
  246. // Memory above 16MB is only used when absolutely necessary so it
  247. // is flagged as LoaderReserve
  248. //
  249. // --- 3/14/00 Allow it to be used. The diamond code
  250. // and the bios disk code manage read buffers to
  251. // keep reads below the 1Mb or 16MB lines
  252. //
  253. Status = MempSetDescriptorRegion( PageStart, PageEnd, LoaderReserve);
  254. } else {
  255. //
  256. // This memory descriptor describes memory within the
  257. // last 40h pages of the 16MB mark - otherwise known as
  258. // 16MB_BOGUS.
  259. //
  260. //
  261. if (PageStart < _16MB_BOGUS) {
  262. //
  263. // Clip starting address to 16MB_BOGUS mark, and add
  264. // memory below 16MB_BOGUS as useable memory.
  265. //
  266. Status = MempSetDescriptorRegion( PageStart, _16MB_BOGUS,
  267. MemoryFree );
  268. if (Status != ESUCCESS) {
  269. break;
  270. }
  271. PageStart = _16MB_BOGUS;
  272. }
  273. //
  274. // Add remaining memory as LoaderReserve.
  275. //
  276. Status = MempSetDescriptorRegion( PageStart, PageEnd, LoaderReserve);
  277. }
  278. if (Status != ESUCCESS) {
  279. break;
  280. }
  281. //
  282. // Move to the next memory descriptor
  283. //
  284. ++SuMemory;
  285. }
  286. if (Status != ESUCCESS) {
  287. BlPrint("MempSetDescriptorRegion failed %lx\n",Status);
  288. return(Status);
  289. }
  290. //
  291. // Set the range 16MB_BOGUS - 16MB as unusable
  292. //
  293. Status = MempSetDescriptorRegion(_16MB_BOGUS, _16MB, MemorySpecialMemory);
  294. if (Status != ESUCCESS) {
  295. return(Status);
  296. }
  297. //
  298. // Hack for EISA machines that insist there is usable memory in the
  299. // ROM area, where we know darn well there isn't.
  300. //
  301. // Remove anything in this range..
  302. MempSetDescriptorRegion(ROM_START_PAGE, ROM_END_PAGE, LoaderMaximum);
  303. //
  304. // Describe the BIOS area
  305. //
  306. MempSetDescriptorRegion(RomStart, ROM_END_PAGE, MemoryFirmwarePermanent);
  307. //
  308. // If this is a remote boot, then everything between the "size of free
  309. // base memory" mark and the start of the ROM area needs to be marked
  310. // as firmware temporary. This is the boot ROM's data/stack area.
  311. //
  312. if ( BootContext->FSContextPointer->BootDrive == 0x40 ) {
  313. ULONG SizeOfFreeBaseMemory = (ULONG)*(USHORT *)0x413 * 1024;
  314. ULONG FirstRomDataPage = SizeOfFreeBaseMemory >> PAGE_SHIFT;
  315. if ( FirstRomDataPage < RomStart ) {
  316. MempSetDescriptorRegion(FirstRomDataPage, RomStart, MemoryFirmwareTemporary);
  317. }
  318. }
  319. //
  320. // Now we have descriptors that map all of physical memory. Carve
  321. // out descriptors from these that describe the parts that we are
  322. // currently using.
  323. //
  324. //
  325. // Create the descriptors which describe the low 1Mb of memory.
  326. //
  327. //
  328. // 00000 - 00fff real-mode interrupt vectors
  329. //
  330. Status = MempAllocDescriptor(0, 1, MemoryFirmwarePermanent);
  331. if (Status != ESUCCESS) {
  332. return(Status);
  333. }
  334. //
  335. // 01000 - 1ffff loadable miniport drivers, free memory.
  336. //
  337. Status = MempAllocDescriptor(1, 0x20, MemoryFree);
  338. if (Status != ESUCCESS) {
  339. return(Status);
  340. }
  341. //
  342. // 20000 - 2ffff SU module, SU stack
  343. //
  344. Status = MempAllocDescriptor(0x20, PERMANENT_HEAP_START, MemoryFirmwareTemporary);
  345. if (Status != ESUCCESS) {
  346. return(Status);
  347. }
  348. //
  349. // 30000 - 30000 Firmware Permanent
  350. // This starts out as zero-length. It grows into the firmware temporary
  351. // heap descriptor as we allocate permanent pages for the Page Directory
  352. // and Page Tables
  353. //
  354. Status = MempAllocDescriptor(PERMANENT_HEAP_START,
  355. PERMANENT_HEAP_START,
  356. LoaderMemoryData);
  357. if (Status != ESUCCESS) {
  358. return(Status);
  359. }
  360. //
  361. // 30000 - 5ffff Firmware temporary heap
  362. //
  363. Status = MempAllocDescriptor(PERMANENT_HEAP_START,
  364. TEMPORARY_HEAP_START,
  365. MemoryFirmwareTemporary);
  366. if (Status != ESUCCESS) {
  367. return(Status);
  368. }
  369. //
  370. // Stack we are currently running on.
  371. //
  372. Status = MempAllocDescriptor(TEMPORARY_HEAP_START,
  373. TEMPORARY_HEAP_START+2,
  374. MemoryFirmwareTemporary);
  375. if (Status != ESUCCESS) {
  376. return(Status);
  377. }
  378. //
  379. // Describe the osloader memory image
  380. //
  381. LoaderStart = BootContext->OsLoaderStart >> PAGE_SHIFT;
  382. LoaderEnd = (BootContext->OsLoaderEnd + PAGE_SIZE - 1) >> PAGE_SHIFT;
  383. Status = MempAllocDescriptor(LoaderStart,
  384. LoaderEnd,
  385. MemoryLoadedProgram);
  386. if (Status != ESUCCESS) {
  387. return(Status);
  388. }
  389. //
  390. // Describe the memory pool used to allocate memory for the SCSI
  391. // miniports.
  392. //
  393. Status = MempAllocDescriptor(LoaderEnd,
  394. LoaderEnd + FW_POOL_SIZE,
  395. MemoryFirmwareTemporary);
  396. if (Status != ESUCCESS) {
  397. return(Status);
  398. }
  399. FwPoolStart = LoaderEnd << PAGE_SHIFT;
  400. FwPoolEnd = FwPoolStart + (FW_POOL_SIZE << PAGE_SHIFT);
  401. //
  402. // HACKHACK - try to mark a page just below the osloader as firmwaretemp,
  403. // so it will not get used for heap/stack. This is to force
  404. // our heap/stack to be < 1Mb.
  405. //
  406. MempAllocDescriptor((BootContext->OsLoaderStart >> PAGE_SHIFT)-1,
  407. BootContext->OsLoaderStart >> PAGE_SHIFT,
  408. MemoryFirmwareTemporary);
  409. Status = MempTurnOnPaging();
  410. if (Status != ESUCCESS) {
  411. return(Status);
  412. }
  413. Status = MempCopyGdt();
  414. //
  415. // Find any reserved ranges described by the firmware and
  416. // record these
  417. //
  418. return(Status);
  419. }
  420. VOID
  421. InitializeMemoryDescriptors (
  422. VOID
  423. )
  424. /*++
  425. Routine Description:
  426. Pass 2 of InitializeMemorySubsystem. This function reads the
  427. firmware address space map and reserves ranges the firmware declares
  428. as "address space reserved".
  429. Note: free memory range descriptors has already been reported by su.
  430. Arguments:
  431. none
  432. Returns:
  433. none
  434. --*/
  435. {
  436. ULONGLONG BAddr, EAddr, Length;
  437. ULONG BPage, EPage;
  438. E820FRAME Frame;
  439. #ifdef LOADER_DEBUG
  440. BlPrint("Begin InitializeMemoryDescriptors\n") ;
  441. #endif
  442. Frame.Key = 0;
  443. do {
  444. Frame.Size = sizeof (Frame.Descriptor);
  445. GET_MEMORY_DESCRIPTOR (&Frame);
  446. if (Frame.ErrorFlag || Frame.Size < sizeof (Frame.Descriptor)) {
  447. break;
  448. }
  449. #ifdef LOADER_DEBUG
  450. BlPrint("*E820: %lx %lx:%lx %lx:%lx %lx %lx\n",
  451. Frame.Size,
  452. Frame.Descriptor.BaseAddrHigh, Frame.Descriptor.BaseAddrLow,
  453. Frame.Descriptor.SizeHigh, Frame.Descriptor.SizeLow,
  454. Frame.Descriptor.MemoryType,
  455. Frame.Key
  456. );
  457. #endif
  458. BAddr = Frame.Descriptor.BaseAddrHigh;
  459. BAddr = (BAddr << 32) + Frame.Descriptor.BaseAddrLow;
  460. Length = Frame.Descriptor.SizeHigh;
  461. Length = (Length << 32) + Frame.Descriptor.SizeLow;
  462. EAddr = BAddr + Length - 1;
  463. //
  464. // The memory range is described as the region from BAddr to EAddr
  465. // inclusive.
  466. //
  467. //
  468. // Some processors support physical addressing above 32 bits.
  469. //
  470. //
  471. // Based upon the address range descriptor type, find the
  472. // available memory and add it to the descriptor list
  473. //
  474. switch (Frame.Descriptor.MemoryType) {
  475. case 1:
  476. //
  477. // This is a memory descriptor - it's already been handled
  478. // by su (eisac.c)
  479. //
  480. // However, any memory within 16MB_BOGUS - 16MB was
  481. // considered unuseable. Reclaim memory within this
  482. // region which is described via this interface.
  483. //
  484. // Also, any memory above 4G was considered unusable.
  485. // Reclaim memory within this range as well.
  486. //
  487. BPage = (ULONG)((BAddr + PAGE_SIZE - 1) >> PAGE_SHIFT);
  488. EPage = (ULONG)((EAddr >> PAGE_SHIFT) + 1);
  489. //
  490. // Clip to bogus range
  491. //
  492. if (BPage < _16MB_BOGUS && EPage >= _16MB_BOGUS) {
  493. BPage = _16MB_BOGUS;
  494. }
  495. if (EPage > (_16MB-1) && BPage <= (_16MB-1)) {
  496. EPage = (_16MB-1);
  497. }
  498. if (BPage >= _16MB_BOGUS && EPage <= (_16MB-1)) {
  499. //
  500. // Reclaim memory within the bogus range
  501. // by setting it to FirmwareTemporary
  502. //
  503. MempSetDescriptorRegion (
  504. BPage,
  505. EPage,
  506. MemoryFirmwareTemporary
  507. );
  508. }
  509. //
  510. // Now reclaim any portion of this range that lies above
  511. // the 4G line.
  512. //
  513. BPage = (ULONG)((BAddr + PAGE_SIZE - 1) >> PAGE_SHIFT);
  514. EPage = (ULONG)((EAddr >> PAGE_SHIFT) + 1);
  515. if (EPage >= _4G) {
  516. //
  517. // At least part of this region is above 4G. Truncate
  518. // any portion that falls below 4G, and reclaim
  519. // the memory.
  520. //
  521. if (BPage < _4G) {
  522. BPage = _4G;
  523. }
  524. MempSetDescriptorRegion (
  525. BPage,
  526. EPage,
  527. MemoryFirmwareTemporary
  528. );
  529. }
  530. break;
  531. default: // unkown types are treated as Reserved
  532. case 2:
  533. //
  534. // This memory descriptor is a reserved address range
  535. //
  536. BPage = (ULONG)(BAddr >> PAGE_SHIFT);
  537. EPage = (ULONG)((EAddr + 1 + PAGE_SIZE - 1) >> PAGE_SHIFT);
  538. MempSetDescriptorRegion (
  539. BPage,
  540. EPage,
  541. MemorySpecialMemory
  542. );
  543. break;
  544. }
  545. } while (Frame.Key) ;
  546. //
  547. // Disable pages from KSEG0 which are disabled
  548. //
  549. MempDisablePages();
  550. #ifdef LOADER_DEBUG
  551. BlPrint("Complete InitializeMemoryDescriptors\n") ;
  552. #endif
  553. return;
  554. }
  555. ARC_STATUS
  556. MempCopyGdt(
  557. VOID
  558. )
  559. /*++
  560. Routine Description:
  561. Copies the GDT & IDT into pages allocated out of our permanent heap.
  562. Arguments:
  563. None
  564. Return Value:
  565. ESUCCESS - GDT & IDT copy successful
  566. --*/
  567. {
  568. #pragma pack(2)
  569. static struct {
  570. USHORT Limit;
  571. ULONG Base;
  572. } GdtDef, IdtDef;
  573. #pragma pack(4)
  574. ULONG BlockSize;
  575. PKGDTENTRY NewGdt;
  576. PKIDTENTRY NewIdt;
  577. ULONG NumPages;
  578. //
  579. // Get the current location of the GDT & IDT
  580. //
  581. _asm {
  582. sgdt GdtDef;
  583. sidt IdtDef;
  584. }
  585. if (GdtDef.Base + GdtDef.Limit + 1 != IdtDef.Base) {
  586. //
  587. // Just a sanity check to make sure that the IDT immediately
  588. // follows the GDT. (As set up in SUDATA.ASM)
  589. //
  590. BlPrint("ERROR - GDT and IDT are not contiguous!\n");
  591. BlPrint("GDT - %lx (%x) IDT - %lx (%x)\n",
  592. GdtDef.Base, GdtDef.Limit,
  593. IdtDef.Base, IdtDef.Limit);
  594. while (1);
  595. }
  596. BlockSize = GdtDef.Limit+1 + IdtDef.Limit+1;
  597. NumPages = (BlockSize + PAGE_SIZE-1) >> PAGE_SHIFT;
  598. NewGdt = (PKGDTENTRY)FwAllocateHeapPermanent(NumPages);
  599. if (NewGdt == NULL) {
  600. return(ENOMEM);
  601. }
  602. RtlMoveMemory((PVOID)NewGdt, (PVOID)GdtDef.Base, NumPages << PAGE_SHIFT);
  603. GdtDef.Base = (ULONG)NewGdt;
  604. IdtDef.Base = (ULONG)((PUCHAR)NewGdt + GdtDef.Limit + 1);
  605. //
  606. // Initialize the boot debugger IDT entries.
  607. //
  608. NewIdt = (PKIDTENTRY)IdtDef.Base;
  609. NewIdt[1].Offset = (USHORT)((ULONG)BdTrap01 & 0xffff);
  610. NewIdt[1].Selector = 8;
  611. NewIdt[1].Access = 0x8e00;
  612. NewIdt[1].ExtendedOffset = (USHORT)((ULONG)BdTrap01 >> 16);
  613. NewIdt[3].Offset = (USHORT)((ULONG)BdTrap03 & 0xffff);
  614. NewIdt[3].Selector = 8;
  615. NewIdt[3].Access = 0x8e00;
  616. NewIdt[3].ExtendedOffset = (USHORT)((ULONG)BdTrap03 >> 16);
  617. NewIdt[0xd].Offset = (USHORT)((ULONG)BdTrap0d & 0xffff);
  618. NewIdt[0xd].Selector = 8;
  619. NewIdt[0xd].Access = 0x8e00;
  620. NewIdt[0xd].ExtendedOffset = (USHORT)((ULONG)BdTrap0d >> 16);
  621. NewIdt[0xe].Offset = (USHORT)((ULONG)BdTrap0e & 0xffff);
  622. NewIdt[0xe].Selector = 8;
  623. NewIdt[0xe].Access = 0x8e00;
  624. NewIdt[0xe].ExtendedOffset = (USHORT)((ULONG)BdTrap0e >> 16);
  625. NewIdt[0x2d].Offset = (USHORT)((ULONG)BdTrap2d & 0xffff);
  626. NewIdt[0x2d].Selector = 8;
  627. NewIdt[0x2d].Access = 0x8e00;
  628. NewIdt[0x2d].ExtendedOffset = (USHORT)((ULONG)BdTrap2d >> 16);
  629. //
  630. // Load GDT and IDT registers.
  631. //
  632. _asm {
  633. lgdt GdtDef;
  634. lidt IdtDef;
  635. }
  636. //
  637. // Initialize the boot debugger.
  638. //
  639. #if defined(ENABLE_LOADER_DEBUG)
  640. BdInitDebugger(OsLoaderName, (PVOID)OsLoaderBase, ENABLE_LOADER_DEBUG);
  641. #else
  642. BdInitDebugger(OsLoaderName, (PVOID)OsLoaderBase, NULL);
  643. #endif
  644. return ESUCCESS;
  645. }
  646. ARC_STATUS
  647. MempSetDescriptorRegion (
  648. IN ULONG StartPage,
  649. IN ULONG EndPage,
  650. IN TYPE_OF_MEMORY MemoryType
  651. )
  652. /*++
  653. Routine Description:
  654. This function sets a range to the corrisponding memory type.
  655. Descriptors will be removed, modified, inserted as needed to
  656. set the specified range.
  657. Arguments:
  658. StartPage - Supplies the beginning page of the new memory descriptor
  659. EndPage - Supplies the ending page of the new memory descriptor
  660. MemoryType - Supplies the type of memory of the new memory descriptor
  661. Return Value:
  662. ESUCCESS - Memory descriptor succesfully added to MDL array
  663. ENOMEM - MDArray is full.
  664. --*/
  665. {
  666. ULONG i;
  667. ULONG sp, ep;
  668. TYPE_OF_MEMORY mt;
  669. BOOLEAN RegionAdded;
  670. if (EndPage <= StartPage) {
  671. //
  672. // This is a completely bogus memory descriptor. Ignore it.
  673. //
  674. #ifdef LOADER_DEBUG
  675. BlPrint("Attempt to create invalid memory descriptor %lx - %lx\n",
  676. StartPage,EndPage);
  677. #endif
  678. return(ESUCCESS);
  679. }
  680. RegionAdded = FALSE;
  681. //
  682. // Clip, remove, any descriptors in target area
  683. //
  684. for (i=0; i < NumberDescriptors; i++) {
  685. sp = MDArray[i].BasePage;
  686. ep = MDArray[i].BasePage + MDArray[i].PageCount;
  687. mt = MDArray[i].MemoryType;
  688. if (sp < StartPage) {
  689. if (ep > StartPage && ep <= EndPage) {
  690. // truncate this descriptor
  691. ep = StartPage;
  692. }
  693. if (ep > EndPage) {
  694. //
  695. // Target area is contained totally within this
  696. // descriptor. Split the descriptor into two ranges
  697. //
  698. if (NumberDescriptors == MAX_DESCRIPTORS) {
  699. return(ENOMEM);
  700. }
  701. //
  702. // Add descriptor for EndPage - ep
  703. //
  704. MDArray[NumberDescriptors].MemoryType = mt;
  705. MDArray[NumberDescriptors].BasePage = EndPage;
  706. MDArray[NumberDescriptors].PageCount = ep - EndPage;
  707. NumberDescriptors += 1;
  708. //
  709. // Adjust current descriptor for sp - StartPage
  710. //
  711. ep = StartPage;
  712. }
  713. } else {
  714. // sp >= StartPage
  715. if (sp < EndPage) {
  716. if (ep < EndPage) {
  717. //
  718. // This descriptor is totally within the target area -
  719. // remove it
  720. //
  721. ep = sp;
  722. } else {
  723. // bump begining page of this descriptor
  724. sp = EndPage;
  725. }
  726. }
  727. }
  728. //
  729. // Check if the new range can be appended or prepended to
  730. // this descriptor
  731. //
  732. if (mt == MemoryType && !RegionAdded) {
  733. if (sp == EndPage) {
  734. // prepend region being set
  735. sp = StartPage;
  736. RegionAdded = TRUE;
  737. } else if (ep == StartPage) {
  738. // append region being set
  739. ep = EndPage;
  740. RegionAdded = TRUE;
  741. }
  742. }
  743. if (MDArray[i].BasePage == sp && MDArray[i].PageCount == ep-sp) {
  744. //
  745. // Descriptor was not editted
  746. //
  747. continue;
  748. }
  749. //
  750. // Reset this descriptor
  751. //
  752. MDArray[i].BasePage = sp;
  753. MDArray[i].PageCount = ep - sp;
  754. if (ep == sp) {
  755. //
  756. // Descriptor vanished - remove it
  757. //
  758. NumberDescriptors -= 1;
  759. if (i < NumberDescriptors) {
  760. MDArray[i] = MDArray[NumberDescriptors];
  761. }
  762. i--; // backup & recheck current position
  763. }
  764. }
  765. //
  766. // If region wasn't already added to a neighboring region, then
  767. // create a new descriptor now
  768. //
  769. if (!RegionAdded && MemoryType < LoaderMaximum) {
  770. if (NumberDescriptors == MAX_DESCRIPTORS) {
  771. return(ENOMEM);
  772. }
  773. #ifdef LOADER_DEBUG
  774. BlPrint("Adding '%lx - %lx, type %x' to descriptor list\n",
  775. StartPage << PAGE_SHIFT,
  776. EndPage << PAGE_SHIFT,
  777. (USHORT) MemoryType
  778. );
  779. #endif
  780. MDArray[NumberDescriptors].MemoryType = MemoryType;
  781. MDArray[NumberDescriptors].BasePage = StartPage;
  782. MDArray[NumberDescriptors].PageCount = EndPage - StartPage;
  783. NumberDescriptors += 1;
  784. }
  785. return (ESUCCESS);
  786. }
  787. ARC_STATUS
  788. MempAllocDescriptor(
  789. IN ULONG StartPage,
  790. IN ULONG EndPage,
  791. IN TYPE_OF_MEMORY MemoryType
  792. )
  793. /*++
  794. Routine Description:
  795. This routine carves out a specific memory descriptor from the
  796. memory descriptors that have already been created. The MD array
  797. is updated to reflect the new state of memory.
  798. The new memory descriptor must be completely contained within an
  799. already existing memory descriptor. (i.e. memory that does not
  800. exist should never be marked as a certain type)
  801. Arguments:
  802. StartPage - Supplies the beginning page of the new memory descriptor
  803. EndPage - Supplies the ending page of the new memory descriptor
  804. MemoryType - Supplies the type of memory of the new memory descriptor
  805. Return Value:
  806. ESUCCESS - Memory descriptor succesfully added to MDL array
  807. ENOMEM - MDArray is full.
  808. --*/
  809. {
  810. ULONG i;
  811. //
  812. // Walk through the memory descriptors until we find one that
  813. // contains the start of the descriptor.
  814. //
  815. for (i=0; i<NumberDescriptors; i++) {
  816. if ((MDArray[i].MemoryType == MemoryFree) &&
  817. (MDArray[i].BasePage <= StartPage ) &&
  818. (MDArray[i].BasePage+MDArray[i].PageCount > StartPage) &&
  819. (MDArray[i].BasePage+MDArray[i].PageCount >= EndPage)) {
  820. break;
  821. }
  822. }
  823. if (i==NumberDescriptors) {
  824. return(ENOMEM);
  825. }
  826. if (MDArray[i].BasePage == StartPage) {
  827. if (MDArray[i].BasePage+MDArray[i].PageCount == EndPage) {
  828. //
  829. // The new descriptor is identical to the existing descriptor.
  830. // Simply change the memory type of the existing descriptor in
  831. // place.
  832. //
  833. MDArray[i].MemoryType = MemoryType;
  834. } else {
  835. //
  836. // The new descriptor starts on the same page, but is smaller
  837. // than the existing descriptor. Shrink the existing descriptor
  838. // by moving its start page up, and create a new descriptor.
  839. //
  840. if (NumberDescriptors == MAX_DESCRIPTORS) {
  841. return(ENOMEM);
  842. }
  843. MDArray[i].BasePage = EndPage;
  844. MDArray[i].PageCount -= (EndPage-StartPage);
  845. MDArray[NumberDescriptors].BasePage = StartPage;
  846. MDArray[NumberDescriptors].PageCount = EndPage-StartPage;
  847. MDArray[NumberDescriptors].MemoryType = MemoryType;
  848. ++NumberDescriptors;
  849. }
  850. } else if (MDArray[i].BasePage+MDArray[i].PageCount == EndPage) {
  851. //
  852. // The new descriptor ends on the same page. Shrink the existing
  853. // by decreasing its page count, and create a new descriptor.
  854. //
  855. if (NumberDescriptors == MAX_DESCRIPTORS) {
  856. return(ENOMEM);
  857. }
  858. MDArray[i].PageCount = StartPage - MDArray[i].BasePage;
  859. MDArray[NumberDescriptors].BasePage = StartPage;
  860. MDArray[NumberDescriptors].PageCount = EndPage-StartPage;
  861. MDArray[NumberDescriptors].MemoryType = MemoryType;
  862. ++NumberDescriptors;
  863. } else {
  864. //
  865. // The new descriptor is in the middle of the existing descriptor.
  866. // Shrink the existing descriptor by decreasing its page count, and
  867. // create two new descriptors.
  868. //
  869. if (NumberDescriptors+1 >= MAX_DESCRIPTORS) {
  870. return(ENOMEM);
  871. }
  872. MDArray[NumberDescriptors].BasePage = EndPage;
  873. MDArray[NumberDescriptors].PageCount = MDArray[i].PageCount -
  874. (EndPage-MDArray[i].BasePage);
  875. MDArray[NumberDescriptors].MemoryType = MemoryFree;
  876. ++NumberDescriptors;
  877. MDArray[i].PageCount = StartPage - MDArray[i].BasePage;
  878. MDArray[NumberDescriptors].BasePage = StartPage;
  879. MDArray[NumberDescriptors].PageCount = EndPage-StartPage;
  880. MDArray[NumberDescriptors].MemoryType = MemoryType;
  881. ++NumberDescriptors;
  882. }
  883. BlpTrackUsage (MemoryType,StartPage,EndPage-StartPage);
  884. return(ESUCCESS);
  885. }
  886. ARC_STATUS
  887. MempTurnOnPaging(
  888. VOID
  889. )
  890. /*++
  891. Routine Description:
  892. Sets up the page tables necessary to map the first 16mb of memory and
  893. enables paging.
  894. Arguments:
  895. None.
  896. Return Value:
  897. ESUCCESS - Paging successfully turned on
  898. --*/
  899. {
  900. ULONG i;
  901. ARC_STATUS Status;
  902. //
  903. // Allocate, initialize, and map the PDE page onto itself (i.e., virtual
  904. // address PDE_BASE).
  905. //
  906. PDE = FwAllocateHeapPermanent(1);
  907. if (PDE == NULL) {
  908. return ENOMEM;
  909. }
  910. RtlZeroMemory(PDE, PAGE_SIZE);
  911. PDE[PDE_BASE >> 22].Valid = 1;
  912. PDE[PDE_BASE >> 22].Write = 1;
  913. PDE[PDE_BASE >> 22].PageFrameNumber = (ULONG)PDE >> PAGE_SHIFT;
  914. //
  915. // Allocate, initialize, and map the HAL page into the last PDE (i.e.,
  916. // virtual address range 0xffc00000 - 0xffffffff).
  917. //
  918. HalPT = FwAllocateHeapPermanent(1);
  919. if (HalPT == NULL) {
  920. return ENOMEM;
  921. }
  922. RtlZeroMemory(HalPT, PAGE_SIZE);
  923. PDE[1023].Valid = 1;
  924. PDE[1023].Write = 1;
  925. PDE[1023].PageFrameNumber = (ULONG)HalPT >> PAGE_SHIFT;
  926. //
  927. // Scan the memory descriptor list and setup paging for each descriptor.
  928. //
  929. for (i = 0; i < NumberDescriptors; i++) {
  930. if (MDArray[i].BasePage < _16MB) {
  931. Status = MempSetupPaging(MDArray[i].BasePage,
  932. MDArray[i].PageCount);
  933. if (Status != ESUCCESS) {
  934. BlPrint("ERROR - MempSetupPaging(%lx, %lx) failed\n",
  935. MDArray[i].BasePage,
  936. MDArray[i].PageCount);
  937. return Status;
  938. }
  939. }
  940. }
  941. //
  942. // Turn on paging.
  943. //
  944. _asm {
  945. //
  946. // Load physical address of page directory
  947. //
  948. mov eax,PDE
  949. mov cr3,eax
  950. //
  951. // Enable paging mode
  952. //
  953. mov eax,cr0
  954. or eax,CR0_PG
  955. mov cr0,eax
  956. }
  957. return ESUCCESS;
  958. }
  959. ARC_STATUS
  960. MempSetupPaging(
  961. IN ULONG StartPage,
  962. IN ULONG NumberPages
  963. )
  964. /*++
  965. Routine Description:
  966. Allocates and initializes the page table pages required to identity map
  967. the specified region of memory at its physical address, at KSEG0_BASE, OLD_ALTERNATE
  968. and at ALTERNATE_BASE.
  969. Arguments:
  970. StartPage - Supplies the first page to start mapping.
  971. NumberPage - Supplies the number of pages to map.
  972. Return Value:
  973. ESUCCESS - Paging successfully set up
  974. --*/
  975. {
  976. ULONG EndPage;
  977. ULONG Entry;
  978. ULONG FrameNumber;
  979. ULONG Page;
  980. PHARDWARE_PTE PageTableP;
  981. PHARDWARE_PTE PageTableV;
  982. ULONG Offset;
  983. //
  984. // The page table pages that are used to map memory at physical equal
  985. // real addresses are allocated from firmware temporary memory which
  986. // gets released when memory management initializes.
  987. //
  988. // N.B. Physical memory is mapped at its physical address, KSEG0_BASE,
  989. // and at ALTERNATE_BASE. This allows the system to be configured
  990. // by the OS Loader to be either a 2gb or 3gb user space system
  991. // based on an OS Loader option.
  992. //
  993. EndPage = StartPage + NumberPages;
  994. for (Page = StartPage; Page < EndPage; Page += 1) {
  995. Entry = Page >> 10;
  996. //
  997. // If the PDE entry for this page address range is not allocated,
  998. // then allocate and initialize the PDE entry to map the page table
  999. // pages for the the memory range. Otherwise, compute the address
  1000. // of the page table pages.
  1001. //
  1002. if (PDE[Entry].Valid == 0) {
  1003. //
  1004. // Allocate and initialize a page table page to map the specified
  1005. // page into physical memory.
  1006. //
  1007. PageTableP = (PHARDWARE_PTE)FwAllocateHeapAligned(PAGE_SIZE);
  1008. if (PageTableP == NULL) {
  1009. return ENOMEM;
  1010. }
  1011. RtlZeroMemory(PageTableP, PAGE_SIZE);
  1012. FrameNumber = (ULONG)PageTableP >> PAGE_SHIFT;
  1013. PDE[Entry].Valid = 1;
  1014. PDE[Entry].Write = 1;
  1015. PDE[Entry].PageFrameNumber = FrameNumber;
  1016. //
  1017. // Allocate and initialize a page table page to map the specified
  1018. // page into KSEG0_BASE and ALTERNATE_BASE.
  1019. //
  1020. // N.B. Only one page table page is allocated since the contents
  1021. // for both mappings are the same.
  1022. //
  1023. PageTableV = (PHARDWARE_PTE)FwAllocateHeapPermanent(1);
  1024. if (PageTableV == NULL) {
  1025. return ENOMEM;
  1026. }
  1027. RtlZeroMemory(PageTableV, PAGE_SIZE);
  1028. FrameNumber = (ULONG)PageTableV >> PAGE_SHIFT;
  1029. Offset = Entry + (KSEG0_BASE >> 22);
  1030. PDE[Offset].Valid = 1;
  1031. PDE[Offset].Write = 1;
  1032. PDE[Offset].PageFrameNumber = FrameNumber;
  1033. Offset = Entry + (ALTERNATE_BASE >> 22);
  1034. PDE[Offset].Valid = 1;
  1035. PDE[Offset].Write = 1;
  1036. PDE[Offset].PageFrameNumber = FrameNumber;
  1037. if (Entry > HighestPde) {
  1038. HighestPde = Entry;
  1039. }
  1040. } else {
  1041. Offset = Entry + (KSEG0_BASE >> 22);
  1042. PageTableP = (PHARDWARE_PTE)(PDE[Entry].PageFrameNumber << PAGE_SHIFT);
  1043. PageTableV = (PHARDWARE_PTE)(PDE[Offset].PageFrameNumber << PAGE_SHIFT);
  1044. }
  1045. //
  1046. // If this is not the first page in memory, then mark it valid.
  1047. //
  1048. if (Page != 0) {
  1049. Offset = Page & 0x3ff;
  1050. PageTableP[Offset].Valid = 1;
  1051. PageTableP[Offset].Write = 1;
  1052. PageTableP[Offset].PageFrameNumber = Page;
  1053. PageTableV[Offset].Valid = 1;
  1054. PageTableV[Offset].Write = 1;
  1055. PageTableV[Offset].PageFrameNumber = Page;
  1056. }
  1057. }
  1058. return ESUCCESS;
  1059. }
  1060. VOID
  1061. MempDisablePages(
  1062. VOID
  1063. )
  1064. /*++
  1065. Routine Description:
  1066. Frees as many Page Tables as are required from the KSEG0_BASE, OLD_ALTERNATE
  1067. and ALTERNATE_BASE regions.
  1068. Arguments:
  1069. None.
  1070. Return Value:
  1071. none
  1072. --*/
  1073. {
  1074. ULONG EndPage;
  1075. ULONG Entry;
  1076. ULONG i;
  1077. ULONG Offset;
  1078. ULONG Page;
  1079. PHARDWARE_PTE PageTable;
  1080. //
  1081. // Cleanup the KSEG0_BASE and ALTERNATE_BASE regions. The MM PFN database
  1082. // is an array of entries which track each page of main memory. Large
  1083. // enough memory holes will cause this array to be sparse. MM requires
  1084. // enabled PTEs to have entries in the PFN database. So locate any memory
  1085. // hole and remove their PTEs.
  1086. //
  1087. for (i = 0; i < NumberDescriptors; i += 1) {
  1088. if (MDArray[i].MemoryType == MemorySpecialMemory ||
  1089. MDArray[i].MemoryType == MemoryFirmwarePermanent) {
  1090. //
  1091. // The KSEG0_BASE and ALTERNATE_BASE regions only map up to 16MB,
  1092. // so clip the high end at that address.
  1093. //
  1094. Page = MDArray[i].BasePage;
  1095. EndPage = Page + MDArray[i].PageCount;
  1096. if (EndPage > _16MB) {
  1097. EndPage = _16MB;
  1098. }
  1099. //
  1100. // Some PTEs below 1M may need to stay mapped since they may have
  1101. // been put into ABIOS selectors. Instead of determining which PTEs
  1102. // they may be, we will leave PTEs below 1M alone. This doesn't
  1103. // cause the PFN any problems since we know there is some memory
  1104. // below then 680K mark and some more memory at the 1M mark. Thus
  1105. // there is not a large enough "memory hole" to cause the PFN database
  1106. // to be sparse below 1M.
  1107. //
  1108. // Clip starting address to 1MB
  1109. //
  1110. if (Page < _1MB) {
  1111. Page = _1MB;
  1112. }
  1113. //
  1114. // For each page in this range make sure it is invalid in the
  1115. // KSEG0_BASE and ALTERNATE_BASE regions.
  1116. //
  1117. // N.B. Since there is only one page table page for both the
  1118. // KSEG0_BASE and ALTERNATE_BASE regions the page only
  1119. // needs to marked invalid once.
  1120. //
  1121. while (Page < EndPage) {
  1122. Entry = (Page >> 10) + (KSEG0_BASE >> 22);
  1123. if (PDE[Entry].Valid == 1) {
  1124. PageTable = (PHARDWARE_PTE)(PDE[Entry].PageFrameNumber << PAGE_SHIFT);
  1125. Offset = Page & 0x3ff;
  1126. PageTable[Offset].Valid = 0;
  1127. PageTable[Offset].Write = 0;
  1128. PageTable[Offset].PageFrameNumber = 0;
  1129. }
  1130. Page += 1;
  1131. }
  1132. }
  1133. }
  1134. }
  1135. PVOID
  1136. FwAllocateHeapPermanent(
  1137. IN ULONG NumberPages
  1138. )
  1139. /*++
  1140. Routine Description:
  1141. This allocates pages from the private heap. The memory descriptor for
  1142. the LoaderMemoryData area is grown to include the returned pages, while
  1143. the memory descriptor for the temporary heap is shrunk by the same amount.
  1144. N.B. DO NOT call this routine after we have passed control to
  1145. BlOsLoader! Once BlOsLoader calls BlMemoryInitialize, the
  1146. firmware memory descriptors are sucked into the OS Loader heap
  1147. and those are the descriptors passed to the kernel. So any
  1148. changes in the firmware private heap will be irrelevant.
  1149. If you need to allocate permanent memory after the OS Loader
  1150. has initialized, use BlAllocateDescriptor.
  1151. Arguments:
  1152. NumberPages - size of memory to allocate (in pages)
  1153. Return Value:
  1154. Pointer to block of memory, if successful.
  1155. NULL, if unsuccessful.
  1156. --*/
  1157. {
  1158. PVOID MemoryPointer;
  1159. PMEMORY_DESCRIPTOR Descriptor;
  1160. if (FwPermanentHeap + (NumberPages << PAGE_SHIFT) > FwTemporaryHeap) {
  1161. //
  1162. // Our heaps collide, so we are out of memory
  1163. //
  1164. BlPrint("Out of permanent heap!\n");
  1165. while (1) {
  1166. }
  1167. return(NULL);
  1168. }
  1169. //
  1170. // Find the memory descriptor which describes the LoaderMemoryData area,
  1171. // so we can grow it to include the just-allocated pages.
  1172. //
  1173. Descriptor = MDArray;
  1174. while (Descriptor->MemoryType != LoaderMemoryData) {
  1175. ++Descriptor;
  1176. if (Descriptor > MDArray+MAX_DESCRIPTORS) {
  1177. BlPrint("ERROR - FwAllocateHeapPermanent couldn't find the\n");
  1178. BlPrint(" LoaderMemoryData descriptor!\n");
  1179. while (1) {
  1180. }
  1181. return(NULL);
  1182. }
  1183. }
  1184. Descriptor->PageCount += NumberPages;
  1185. //
  1186. // We know that the memory descriptor after this one is the firmware
  1187. // temporary heap descriptor. Since it is physically contiguous with our
  1188. // LoaderMemoryData block, we remove the pages from its descriptor.
  1189. //
  1190. ++Descriptor;
  1191. Descriptor->PageCount -= NumberPages;
  1192. Descriptor->BasePage += NumberPages;
  1193. MemoryPointer = (PVOID)FwPermanentHeap;
  1194. FwPermanentHeap += NumberPages << PAGE_SHIFT;
  1195. return(MemoryPointer);
  1196. }
  1197. PVOID
  1198. FwAllocateHeap(
  1199. IN ULONG Size
  1200. )
  1201. /*++
  1202. Routine Description:
  1203. Allocates memory from the "firmware" temporary heap.
  1204. Arguments:
  1205. Size - Supplies size of block to allocate
  1206. Return Value:
  1207. PVOID - Pointer to the beginning of the block
  1208. NULL - Out of memory
  1209. --*/
  1210. {
  1211. ULONG i;
  1212. ULONG SizeInPages;
  1213. ULONG StartPage;
  1214. ARC_STATUS Status;
  1215. if (((FwTemporaryHeap - FwPermanentHeap) < Size) && (FwDescriptorsValid)) {
  1216. //
  1217. // Large allocations get their own descriptor so miniports that
  1218. // have huge device extensions don't suck up all of the heap.
  1219. //
  1220. // Note that we can only do this while running in "firmware" mode.
  1221. // Once we call into the osloader, it sucks all the memory descriptors
  1222. // out of the "firmware" and changes to this list will not show
  1223. // up there.
  1224. //
  1225. // We are looking for a descriptor that is MemoryFree and <16Mb.
  1226. //
  1227. // [ChuckL 13-Dec-2001]
  1228. // This routine has always been called after the loader's memory list
  1229. // was initialized, which meant that it was stomping on memory that
  1230. // might have been allocated by the loader. This was not a problem
  1231. // because the loader initialized its memory list twice(!), so it
  1232. // looked at the MDArray again to get an updated picture of the
  1233. // memory allocation situation. This didn't really work, and there
  1234. // used to be extra code here to handle the situation by calling
  1235. // BlFindDescriptor/BlGeneratorDescriptor to tell the loader about
  1236. // the low-level allocation. But even that didn't really work. And
  1237. // now, because of the elimination of the second call to
  1238. // BlMemoryInitialize(), the brokenness of the old code has been
  1239. // exposed. What happened is that this routine would use the MDArray
  1240. // to decide where to allocate memory, then it would tell the loader
  1241. // about it. But using MDArray to find free memory was bogus,
  1242. // because the loader had already used its own copy of the list to
  1243. // make its own allocations. So the same memory was allocated twice.
  1244. // The fix implemented here is to use BlAllocateAlignedDescriptor if
  1245. // the loader has been initialized, skipping the MDArray entirely.
  1246. //
  1247. SizeInPages = (Size+PAGE_SIZE-1) >> PAGE_SHIFT;
  1248. if (BlLoaderBlock != NULL) {
  1249. Status = BlAllocateAlignedDescriptor(
  1250. LoaderFirmwareTemporary,
  1251. 0,
  1252. SizeInPages,
  1253. 1,
  1254. &StartPage
  1255. );
  1256. if (Status == ESUCCESS) {
  1257. return((PVOID)(StartPage << PAGE_SHIFT));
  1258. }
  1259. } else {
  1260. for (i=0; i<NumberDescriptors; i++) {
  1261. if ((MDArray[i].MemoryType == MemoryFree) &&
  1262. (MDArray[i].BasePage <= _16MB_BOGUS) &&
  1263. (MDArray[i].PageCount >= SizeInPages)) {
  1264. break;
  1265. }
  1266. }
  1267. if (i < NumberDescriptors) {
  1268. StartPage = MDArray[i].BasePage+MDArray[i].PageCount-SizeInPages;
  1269. Status = MempAllocDescriptor(StartPage,
  1270. StartPage+SizeInPages,
  1271. MemoryFirmwareTemporary);
  1272. if (Status==ESUCCESS) {
  1273. return((PVOID)(StartPage << PAGE_SHIFT));
  1274. }
  1275. }
  1276. }
  1277. }
  1278. FwTemporaryHeap -= Size;
  1279. //
  1280. // Round down to 16-byte boundary
  1281. //
  1282. FwTemporaryHeap &= ~((ULONG)0xf);
  1283. if (FwTemporaryHeap < FwPermanentHeap) {
  1284. #if DBG
  1285. BlPrint("Out of temporary heap!\n");
  1286. #endif
  1287. return(NULL);
  1288. }
  1289. return((PVOID)FwTemporaryHeap);
  1290. }
  1291. PVOID
  1292. FwAllocatePool(
  1293. IN ULONG Size
  1294. )
  1295. /*++
  1296. Routine Description:
  1297. This routine allocates memory from the firmware pool. Note that
  1298. this memory is NOT under the 1MB line, so it cannot be used for
  1299. anything that must be accessed from real mode. It is currently used
  1300. only by the SCSI miniport drivers and dbcs font loader.
  1301. Arguments:
  1302. Size - Supplies size of block to allocate.
  1303. Return Value:
  1304. PVOID - pointer to the beginning of the block
  1305. NULL - out of memory
  1306. --*/
  1307. {
  1308. PVOID Buffer;
  1309. ULONG NewSize;
  1310. //
  1311. // round size up to 16 byte boundary
  1312. //
  1313. NewSize = (Size + 15) & ~0xf;
  1314. if ((FwPoolStart + NewSize) <= FwPoolEnd) {
  1315. Buffer = (PVOID)FwPoolStart;
  1316. FwPoolStart += NewSize;
  1317. return(Buffer);
  1318. } else {
  1319. //
  1320. // we've used up all our pool, try to allocate from the heap.
  1321. //
  1322. return(FwAllocateHeap(Size));
  1323. }
  1324. }
  1325. PVOID
  1326. FwAllocateHeapAligned(
  1327. IN ULONG Size
  1328. )
  1329. /*++
  1330. Routine Description:
  1331. Allocates memory from the "firmware" temporary heap. This memory is
  1332. always allocated on a page boundary, so it can readily be used for
  1333. temporary page tables
  1334. Arguments:
  1335. Size - Supplies size of block to allocate
  1336. Return Value:
  1337. PVOID - Pointer to the beginning of the block
  1338. NULL - Out of memory
  1339. --*/
  1340. {
  1341. FwTemporaryHeap -= Size;
  1342. //
  1343. // Round down to a page boundary
  1344. //
  1345. FwTemporaryHeap &= ~(PAGE_SIZE-1);
  1346. if (FwTemporaryHeap < FwPermanentHeap) {
  1347. BlPrint("Out of temporary heap!\n");
  1348. return(NULL);
  1349. }
  1350. RtlZeroMemory((PVOID)FwTemporaryHeap,Size);
  1351. return((PVOID)FwTemporaryHeap);
  1352. }
  1353. PVOID
  1354. MmMapIoSpace (
  1355. IN PHYSICAL_ADDRESS PhysicalAddress,
  1356. IN ULONG NumberOfBytes,
  1357. IN MEMORY_CACHING_TYPE CacheType
  1358. )
  1359. /*++
  1360. Routine Description:
  1361. This function returns the corresponding virtual address for a
  1362. known physical address.
  1363. Arguments:
  1364. PhysicalAddress - Supplies the physical address.
  1365. NumberOfBytes - Unused.
  1366. CacheType - Unused.
  1367. Return Value:
  1368. Returns the corresponding virtual address.
  1369. Environment:
  1370. Kernel mode. Any IRQL level.
  1371. --*/
  1372. {
  1373. ULONG i;
  1374. ULONG j;
  1375. ULONG NumberPages;
  1376. NumberPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(PhysicalAddress.LowPart, NumberOfBytes);
  1377. //
  1378. // We use the HAL's PDE for mapping memory buffers.
  1379. // Find enough free PTEs.
  1380. //
  1381. //
  1382. // Check the value of NumberPages
  1383. //
  1384. #define X86_MAX_NUMBER_OF_PAGES 1024
  1385. //
  1386. // since NumberPages is ULONG any arithmetic with NumberPages will
  1387. // result in a ULONG (unless casted)
  1388. // therefore if NumberPages is greated than X86_MAX_NUMBER_OF_PAGES
  1389. // the results of X86_MAX_NUMBER_OF_PAGES-NUmberPages
  1390. // will not be negative (its a ULONG!) therfore the following loop would
  1391. // have returned some bogus pointer...
  1392. //
  1393. // The following 3 line check was added to avoid this problem
  1394. //
  1395. if (NumberPages > X86_MAX_NUMBER_OF_PAGES) {
  1396. return (NULL);
  1397. }
  1398. for (i=0; i <= X86_MAX_NUMBER_OF_PAGES - NumberPages; i++) {
  1399. for (j=0; j < NumberPages; j++) {
  1400. if ((((PULONG)HalPT))[i+j]) {
  1401. break;
  1402. }
  1403. }
  1404. if (j == NumberPages) {
  1405. for (j=0; j<NumberPages; j++) {
  1406. HalPT[i+j].PageFrameNumber =
  1407. (PhysicalAddress.LowPart >> PAGE_SHIFT)+j;
  1408. HalPT[i+j].Valid = 1;
  1409. HalPT[i+j].Write = 1;
  1410. HalPT[i+j].WriteThrough = 1;
  1411. if (CacheType == MmNonCached) {
  1412. HalPT[i+j].CacheDisable = 1;
  1413. }
  1414. }
  1415. return((PVOID)(0xffc00000 | (i<<12) | (PhysicalAddress.LowPart & 0xfff)));
  1416. }
  1417. }
  1418. return(NULL);
  1419. }
  1420. VOID
  1421. MmUnmapIoSpace (
  1422. IN PVOID BaseAddress,
  1423. IN ULONG NumberOfBytes
  1424. )
  1425. /*++
  1426. Routine Description:
  1427. This function unmaps a range of physical address which were previously
  1428. mapped via an MmMapIoSpace function call.
  1429. Arguments:
  1430. BaseAddress - Supplies the base virtual address where the physical
  1431. address was previously mapped.
  1432. NumberOfBytes - Supplies the number of bytes which were mapped.
  1433. Return Value:
  1434. None.
  1435. Environment:
  1436. Kernel mode, IRQL of DISPATCH_LEVEL or below.
  1437. --*/
  1438. {
  1439. ULONG StartPage, PageCount;
  1440. PageCount = COMPUTE_PAGES_SPANNED(BaseAddress, NumberOfBytes);
  1441. StartPage = (((ULONG_PTR)BaseAddress & ~0xffc00000) >> PAGE_SHIFT);
  1442. if (BaseAddress > (PVOID)0xffc00000) {
  1443. RtlZeroMemory(&HalPT[StartPage], PageCount * sizeof(HARDWARE_PTE_X86));
  1444. }
  1445. _asm {
  1446. mov eax, cr3
  1447. mov cr3, eax
  1448. }
  1449. return;
  1450. }
  1451. VOID
  1452. BlpTruncateMemory (
  1453. IN ULONG MaxMemory
  1454. )
  1455. /*++
  1456. Routine Description:
  1457. Eliminates all the memory descriptors above a given boundary
  1458. Arguments:
  1459. MaxMemory - Supplies the maximum memory boundary in megabytes
  1460. Return Value:
  1461. None.
  1462. --*/
  1463. {
  1464. extern MEMORY_DESCRIPTOR MDArray[];
  1465. extern ULONG NumberDescriptors;
  1466. ULONG Current = 0;
  1467. ULONG MaxPage = MaxMemory * 256; // Convert Mb to pages
  1468. if (MaxMemory == 0) {
  1469. return;
  1470. }
  1471. while (Current < NumberDescriptors) {
  1472. if (MDArray[Current].BasePage >= MaxPage) {
  1473. //
  1474. // This memory descriptor lies entirely above the boundary,
  1475. // eliminate it.
  1476. //
  1477. RtlMoveMemory(MDArray+Current,
  1478. MDArray+Current+1,
  1479. sizeof(MEMORY_DESCRIPTOR)*
  1480. (NumberDescriptors-Current-1));
  1481. --NumberDescriptors;
  1482. } else if (MDArray[Current].BasePage + MDArray[Current].PageCount > MaxPage) {
  1483. //
  1484. // This memory descriptor crosses the boundary, truncate it.
  1485. //
  1486. MDArray[Current].PageCount = MaxPage - MDArray[Current].BasePage;
  1487. ++Current;
  1488. } else {
  1489. //
  1490. // This one's ok, keep it.
  1491. //
  1492. ++Current;
  1493. }
  1494. }
  1495. }
  1496. ARC_STATUS
  1497. MempCheckMapping(
  1498. ULONG StartPage,
  1499. ULONG NumberPages
  1500. )
  1501. /*++
  1502. Routine Description:
  1503. This routine makes sure all pages in the range are mapped and
  1504. tracks the highest page used.
  1505. X86 Only.
  1506. Arguments:
  1507. Page - Supplies the physical page number we are starting at.
  1508. Return Value:
  1509. None.
  1510. --*/
  1511. {
  1512. PUCHAR p;
  1513. ULONG EndPage;
  1514. ULONG Entry;
  1515. ULONG FrameNumber;
  1516. ULONG Page;
  1517. PHARDWARE_PTE PageTableP;
  1518. PHARDWARE_PTE PageTableV;
  1519. ULONG Offset;
  1520. //
  1521. // memory under 16MB is always mapped.
  1522. //
  1523. if (StartPage < _16MB) {
  1524. return(ESUCCESS);
  1525. }
  1526. //
  1527. // A PDE is 4MB (22 bits, so if we're in the same 4MB region, nothing to do)
  1528. //
  1529. EndPage = StartPage + NumberPages;
  1530. for (Page = StartPage; Page < EndPage; Page += 1) {
  1531. Entry = Page >> 10;
  1532. //
  1533. // If the PDE entry for this page address range is not allocated,
  1534. // then allocate and initialize the PDE entry to map the page table
  1535. // pages for the the memory range. Otherwise, compute the address
  1536. // of the page table pages.
  1537. //
  1538. if (PDE[Entry].Valid == 0) {
  1539. //
  1540. // Allocate and initialize two page table pages to map the specified
  1541. // page into physical memory.
  1542. //
  1543. p = BlAllocateHeapAligned(PAGE_SIZE*3);
  1544. if (p==NULL) {
  1545. return(ENOMEM);
  1546. }
  1547. PageTableP = (PHARDWARE_PTE)PAGE_ALIGN((ULONG)p+PAGE_SIZE-1);
  1548. RtlZeroMemory(PageTableP, PAGE_SIZE);
  1549. FrameNumber = ((ULONG)PageTableP & ~KSEG0_BASE) >> PAGE_SHIFT;
  1550. PDE[Entry].Valid = 1;
  1551. PDE[Entry].Write = 1;
  1552. PDE[Entry].PageFrameNumber = FrameNumber;
  1553. //
  1554. // initialize a page table page to map the specified
  1555. // page into KSEG0_BASE and ALTERNATE_BASE.
  1556. //
  1557. // N.B. Only one page table page is allocated since the contents
  1558. // for both mappings are the same.
  1559. //
  1560. PageTableV = (PHARDWARE_PTE)((PUCHAR)PageTableP + PAGE_SIZE);
  1561. RtlZeroMemory(PageTableV, PAGE_SIZE);
  1562. FrameNumber = ((ULONG)PageTableV & ~KSEG0_BASE) >> PAGE_SHIFT;
  1563. Offset = Entry + (KSEG0_BASE >> 22);
  1564. PDE[Offset].Valid = 1;
  1565. PDE[Offset].Write = 1;
  1566. PDE[Offset].PageFrameNumber = FrameNumber;
  1567. if (BlVirtualBias) {
  1568. Offset += (BlVirtualBias >> 22);
  1569. PDE[Offset].Valid = 1;
  1570. PDE[Offset].Write = 1;
  1571. PDE[Offset].PageFrameNumber = FrameNumber;
  1572. }
  1573. if (Entry > HighestPde) {
  1574. HighestPde = Entry;
  1575. }
  1576. } else {
  1577. Offset = Entry + (KSEG0_BASE >> 22);
  1578. PageTableP = (PHARDWARE_PTE)(PDE[Entry].PageFrameNumber << PAGE_SHIFT);
  1579. PageTableV = (PHARDWARE_PTE)(PDE[Offset].PageFrameNumber << PAGE_SHIFT);
  1580. }
  1581. //
  1582. // If this is not the first page in memory, then mark it valid.
  1583. //
  1584. if (Page != 0) {
  1585. Offset = Page & 0x3ff;
  1586. PageTableP[Offset].Valid = 1;
  1587. PageTableP[Offset].Write = 1;
  1588. PageTableP[Offset].PageFrameNumber = Page;
  1589. PageTableV[Offset].Valid = 1;
  1590. PageTableV[Offset].Write = 1;
  1591. PageTableV[Offset].PageFrameNumber = Page;
  1592. }
  1593. }
  1594. _asm {
  1595. //
  1596. // Reload cr3 to force a flush
  1597. //
  1598. mov eax,cr3
  1599. mov cr3,eax
  1600. }
  1601. return ESUCCESS;
  1602. }
  1603. ARC_STATUS
  1604. MempSetPageZeroOverride(
  1605. BOOLEAN Enable
  1606. )
  1607. /*++
  1608. Routine Description:
  1609. This routine maps or unmaps page 0.
  1610. X86 Only.
  1611. Arguments:
  1612. Enable - specifies whether to enable or disable the mapping for this page.
  1613. Return Value:
  1614. None.
  1615. --*/
  1616. {
  1617. ULONG Entry;
  1618. ULONG FrameNumber;
  1619. PHARDWARE_PTE PageTableP;
  1620. PHARDWARE_PTE PageTableV;
  1621. ULONG Offset;
  1622. const ULONG StartPage = 0;
  1623. Entry = StartPage >> 10;
  1624. //
  1625. // compute the address of the page table pages.
  1626. //
  1627. if (PDE[Entry].Valid == 0) {
  1628. //
  1629. // the pde for the pte should already be setup.
  1630. // if it's not then we're dead.
  1631. //
  1632. return(ENOMEM);
  1633. } else {
  1634. Offset = Entry + (KSEG0_BASE >> 22);
  1635. PageTableP = (PHARDWARE_PTE)(PDE[Entry].PageFrameNumber << PAGE_SHIFT);
  1636. PageTableV = (PHARDWARE_PTE)(PDE[Offset].PageFrameNumber << PAGE_SHIFT);
  1637. }
  1638. Offset = StartPage & 0x3ff;
  1639. if (PageTableP[Offset].PageFrameNumber != StartPage &&
  1640. PageTableV[Offset].PageFrameNumber != StartPage) {
  1641. //
  1642. // the PTE isn't setup correctly. Bail out.
  1643. //
  1644. return(ENOMEM);
  1645. }
  1646. PageTableP[Offset].Valid = Enable ? 1 : 0;
  1647. PageTableV[Offset].Valid = Enable ? 1 : 0;
  1648. _asm {
  1649. //
  1650. // Reload cr3 to force a flush
  1651. //
  1652. mov eax,cr3
  1653. mov cr3,eax
  1654. }
  1655. return ESUCCESS;
  1656. }
  1657. //
  1658. // Convert remaing LoaderReserve (>16MB mem) to
  1659. // MemoryFirmwareTemporary for the mmgr
  1660. //
  1661. //
  1662. void
  1663. BlpRemapReserve (void)
  1664. {
  1665. PMEMORY_ALLOCATION_DESCRIPTOR NextDescriptor;
  1666. PLIST_ENTRY NextEntry;
  1667. NextEntry = BlLoaderBlock->MemoryDescriptorListHead.Flink;
  1668. while (NextEntry != &BlLoaderBlock->MemoryDescriptorListHead) {
  1669. NextDescriptor = CONTAINING_RECORD(NextEntry,
  1670. MEMORY_ALLOCATION_DESCRIPTOR,
  1671. ListEntry);
  1672. if ((NextDescriptor->MemoryType == LoaderReserve)) {
  1673. NextDescriptor->MemoryType = MemoryFirmwareTemporary;
  1674. }
  1675. NextEntry = NextEntry->Flink;
  1676. }
  1677. }
  1678. ARC_STATUS
  1679. BlpMarkExtendedVideoRegionOffLimits(
  1680. VOID
  1681. )
  1682. /*++
  1683. Routine Description:
  1684. This routine marks the extended video memory region as permanant, so that
  1685. the OS doesn't try to map this memory.
  1686. The ntdetect.com module actually finds out the location of this region as
  1687. well as the region size. We read this from the memory location that
  1688. ntdetect put the data in.
  1689. Arguments:
  1690. None.
  1691. Return Value:
  1692. ARC_STATUS indicating outcome.
  1693. --*/
  1694. {
  1695. ULONG BaseOfExtendedVideoRegionInBytes;
  1696. ULONG SizeOfExtendedVideoRegionInBytes;
  1697. ARC_STATUS Status;
  1698. //
  1699. // ntdetect has placed the base page and size of video rom at physical
  1700. // address 0x740
  1701. //
  1702. //
  1703. // Before we go read this address, we have to explicitly map in page zero.
  1704. //
  1705. Status = MempSetPageZeroOverride(TRUE);
  1706. if (Status != ESUCCESS) {
  1707. return(Status);
  1708. }
  1709. //
  1710. // read the memory.
  1711. //
  1712. BaseOfExtendedVideoRegionInBytes = *(PULONG)0x740;
  1713. SizeOfExtendedVideoRegionInBytes = *(PULONG)0x744;
  1714. //
  1715. // Ok, we're done with this page. unmap it so no one can dereference null.
  1716. //
  1717. Status = MempSetPageZeroOverride(FALSE);
  1718. if (Status != ESUCCESS) {
  1719. return(Status);
  1720. }
  1721. if (BaseOfExtendedVideoRegionInBytes == 0 || SizeOfExtendedVideoRegionInBytes == 0) {
  1722. return(ESUCCESS);
  1723. }
  1724. if (BlLoaderBlock != NULL) {
  1725. PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
  1726. ULONG BasePage;
  1727. ULONG LastPage;
  1728. ULONG PageCount;
  1729. BasePage = BaseOfExtendedVideoRegionInBytes >> PAGE_SHIFT;
  1730. LastPage = (BaseOfExtendedVideoRegionInBytes + SizeOfExtendedVideoRegionInBytes - 1) >> PAGE_SHIFT;
  1731. PageCount = LastPage - BasePage + 1;
  1732. while ( PageCount != 0 ) {
  1733. ULONG thisCount;
  1734. MemoryDescriptor = BlFindMemoryDescriptor(BasePage);
  1735. if (MemoryDescriptor == NULL) {
  1736. break;
  1737. }
  1738. thisCount = PageCount;
  1739. //
  1740. // if we run off of this descriptor, truncate our region
  1741. // at the end of the descriptor.
  1742. //
  1743. if (BasePage + PageCount > MemoryDescriptor->BasePage + MemoryDescriptor->PageCount) {
  1744. thisCount = (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount) - BasePage;
  1745. }
  1746. BlGenerateDescriptor(MemoryDescriptor,
  1747. MemoryFirmwarePermanent,
  1748. BasePage,
  1749. thisCount);
  1750. BasePage += thisCount;
  1751. PageCount -= thisCount;
  1752. }
  1753. }
  1754. return(Status);
  1755. }