Windows NT 4.0 source code leak
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.

179 lines
4.4 KiB

4 years ago
  1. //#pragma comment(exestr, "$Header: /usr4/winnt/SOURCES/ddk35/src/hal/halsni/mips/RCS/xxmemory.c,v 1.2 1994/11/09 07:54:26 holli Exp $")
  2. /*++
  3. Copyright (c) 1991 Microsoft Corporation
  4. Module Name:
  5. xxmemory.c
  6. Abstract:
  7. Provides routines to allow the HAL to map physical memory.
  8. Environment:
  9. Phase 0 initialization only.
  10. Changes:
  11. All stuff comes from the x86 HAL Sources (xxmemory.c)
  12. --*/
  13. #include "halp.h"
  14. //
  15. // Put all code for HAL initialization in the INIT section. It will be
  16. // deallocated by memory management when phase 1 initialization is
  17. // completed.
  18. //
  19. #if defined(ALLOC_PRAGMA)
  20. #pragma alloc_text(INIT, HalpAllocPhysicalMemory)
  21. #endif
  22. MEMORY_ALLOCATION_DESCRIPTOR HalpExtraAllocationDescriptor;
  23. ULONG
  24. HalpAllocPhysicalMemory(
  25. IN PLOADER_PARAMETER_BLOCK LoaderBlock,
  26. IN ULONG MaxPhysicalAddress,
  27. IN ULONG NoPages,
  28. IN BOOLEAN bAlignOn64k
  29. )
  30. /*++
  31. Routine Description:
  32. Carves out N pages of physical memory from the memory descriptor
  33. list in the desired location. This function is to be called only
  34. during phase zero initialization. (ie, before the kernel's memory
  35. management system is running)
  36. Arguments:
  37. MaxPhysicalAddress - The max address where the physical memory can be
  38. NoPages - Number of pages to allocate
  39. Return Value:
  40. The pyhsical address or NULL if the memory could not be obtained.
  41. --*/
  42. {
  43. PMEMORY_ALLOCATION_DESCRIPTOR Descriptor;
  44. PLIST_ENTRY NextMd;
  45. ULONG AlignmentOffset;
  46. ULONG MaxPageAddress;
  47. ULONG PhysicalAddress;
  48. MaxPageAddress = MaxPhysicalAddress >> PAGE_SHIFT;
  49. //
  50. // Scan the memory allocation descriptors and allocate map buffers
  51. //
  52. NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
  53. while (NextMd != &LoaderBlock->MemoryDescriptorListHead) {
  54. Descriptor = CONTAINING_RECORD(NextMd,
  55. MEMORY_ALLOCATION_DESCRIPTOR,
  56. ListEntry);
  57. AlignmentOffset = bAlignOn64k ?
  58. ((Descriptor->BasePage + 0x0f) & ~0x0f) - Descriptor->BasePage :
  59. 0;
  60. //
  61. // Search for a block of memory which contains a memory chunk
  62. // that is greater than size pages, and has a physical address less
  63. // than MAXIMUM_PHYSICAL_ADDRESS.
  64. //
  65. if ((Descriptor->MemoryType == LoaderFree ||
  66. Descriptor->MemoryType == MemoryFirmwareTemporary) &&
  67. (Descriptor->BasePage) &&
  68. (Descriptor->PageCount >= NoPages + AlignmentOffset) &&
  69. (Descriptor->BasePage + NoPages + AlignmentOffset < MaxPageAddress)) {
  70. PhysicalAddress = (AlignmentOffset + Descriptor->BasePage)
  71. << PAGE_SHIFT;
  72. break;
  73. }
  74. NextMd = NextMd->Flink;
  75. }
  76. //
  77. // Use the extra descriptor to define the memory at the end of the
  78. // original block.
  79. //
  80. ASSERT(NextMd != &LoaderBlock->MemoryDescriptorListHead);
  81. if (NextMd == &LoaderBlock->MemoryDescriptorListHead)
  82. return (ULONG)NULL;
  83. //
  84. // Adjust the memory descriptors.
  85. //
  86. if (AlignmentOffset == 0) {
  87. Descriptor->BasePage += NoPages;
  88. Descriptor->PageCount -= NoPages;
  89. if (Descriptor->PageCount == 0) {
  90. //
  91. // The whole block was allocated,
  92. // Remove the entry from the list completely.
  93. //
  94. RemoveEntryList(&Descriptor->ListEntry);
  95. }
  96. } else {
  97. if (Descriptor->PageCount - NoPages - AlignmentOffset) {
  98. //
  99. // Currently we only allow one Align64K allocation
  100. //
  101. ASSERT (HalpExtraAllocationDescriptor.PageCount == 0);
  102. //
  103. // The extra descriptor is needed so intialize it and insert
  104. // it in the list.
  105. //
  106. HalpExtraAllocationDescriptor.PageCount =
  107. Descriptor->PageCount - NoPages - AlignmentOffset;
  108. HalpExtraAllocationDescriptor.BasePage =
  109. Descriptor->BasePage + NoPages + AlignmentOffset;
  110. HalpExtraAllocationDescriptor.MemoryType = MemoryFree;
  111. InsertTailList(
  112. &Descriptor->ListEntry,
  113. &HalpExtraAllocationDescriptor.ListEntry
  114. );
  115. }
  116. //
  117. // Use the current entry as the descriptor for the first block.
  118. //
  119. Descriptor->PageCount = AlignmentOffset;
  120. }
  121. return PhysicalAddress;
  122. }