bootmem.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. bootmem.c
  5. Abstract:
  6. This module implements general memory management support for the Boot
  7. Library.
  8. Author:
  9. Evan Green 19-Feb-2014
  10. Environment:
  11. Boot
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/kernel/kernel.h>
  17. #include "firmware.h"
  18. #include "bootlibp.h"
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #define BOOT_HEAP_GRANULARITY 0x1000
  23. #define BOOT_HEAP_EXPANSION_SIZE (0x10 * 0x1000)
  24. #define BOOT_ALLOCATION_TAG 0x746F6F42 // 'tooB'
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. VOID
  32. BopHandleHeapCorruption (
  33. PMEMORY_HEAP Heap,
  34. HEAP_CORRUPTION_CODE Code,
  35. PVOID Parameter
  36. );
  37. //
  38. // -------------------------------------------------------------------- Globals
  39. //
  40. MEMORY_DESCRIPTOR_LIST BoMemoryMap;
  41. MEMORY_HEAP BoHeap;
  42. //
  43. // ------------------------------------------------------------------ Functions
  44. //
  45. PVOID
  46. BoAllocateMemory (
  47. UINTN Size
  48. )
  49. /*++
  50. Routine Description:
  51. This routine allocates memory in the loader. This memory is marked as
  52. loader temporary, meaning it will get unmapped and reclaimed during kernel
  53. initialization.
  54. Arguments:
  55. Size - Supplies the size of the desired allocation, in bytes.
  56. Return Value:
  57. Returns a physical pointer to the allocation on success, or NULL on failure.
  58. --*/
  59. {
  60. return RtlHeapAllocate(&BoHeap, Size, BOOT_ALLOCATION_TAG);
  61. }
  62. VOID
  63. BoFreeMemory (
  64. PVOID Allocation
  65. )
  66. /*++
  67. Routine Description:
  68. This routine frees memory allocated in the boot environment.
  69. Arguments:
  70. Allocation - Supplies a pointer to the memory allocation being freed.
  71. Return Value:
  72. None.
  73. --*/
  74. {
  75. RtlHeapFree(&BoHeap, Allocation);
  76. return;
  77. }
  78. KSTATUS
  79. BopInitializeMemory (
  80. PBOOT_INITIALIZATION_BLOCK Parameters
  81. )
  82. /*++
  83. Routine Description:
  84. This routine initializes memory services for the boot library.
  85. Arguments:
  86. Parameters - Supplies a pointer to the application initialization
  87. information.
  88. Return Value:
  89. Status code.
  90. --*/
  91. {
  92. MEMORY_DESCRIPTOR Descriptor;
  93. PBOOT_RESERVED_REGION Region;
  94. ULONG RegionIndex;
  95. KSTATUS Status;
  96. //
  97. // Loop through and mark all the reserved regions to prevent allocations
  98. // there. Some firmware (PC/AT) doesn't track allocations made by boot
  99. // applications, and this list is used to mark allocations from a previous
  100. // boot application (like the boot manager).
  101. //
  102. for (RegionIndex = 0;
  103. RegionIndex < Parameters->ReservedRegionCount;
  104. RegionIndex += 1) {
  105. Region = &(Parameters->ReservedRegions[RegionIndex]);
  106. //
  107. // Mark these regions as "firmware temporary" so that they can get
  108. // reclaimed in the kernel, but don't get freed if this boot
  109. // application fails and cleans up.
  110. //
  111. MmMdInitDescriptor(&Descriptor,
  112. Region->Address,
  113. Region->Address + Region->Size,
  114. MemoryTypeFirmwareTemporary);
  115. Status = MmMdAddDescriptorToList(&BoMemoryMap, &Descriptor);
  116. if (!KSUCCESS(Status)) {
  117. goto InitializeMemoryEnd;
  118. }
  119. }
  120. Status = STATUS_SUCCESS;
  121. RtlHeapInitialize(&BoHeap,
  122. BoExpandHeap,
  123. NULL,
  124. BopHandleHeapCorruption,
  125. BOOT_HEAP_EXPANSION_SIZE,
  126. BOOT_HEAP_GRANULARITY,
  127. 0,
  128. MEMORY_HEAP_FLAG_NO_PARTIAL_FREES);
  129. Status = STATUS_SUCCESS;
  130. InitializeMemoryEnd:
  131. return Status;
  132. }
  133. //
  134. // --------------------------------------------------------- Internal Functions
  135. //
  136. VOID
  137. BopHandleHeapCorruption (
  138. PMEMORY_HEAP Heap,
  139. HEAP_CORRUPTION_CODE Code,
  140. PVOID Parameter
  141. )
  142. /*++
  143. Routine Description:
  144. This routine is called when the heap detects internal corruption.
  145. Arguments:
  146. Heap - Supplies a pointer to the heap containing the corruption.
  147. Code - Supplies the code detailing the problem.
  148. Parameter - Supplies an optional parameter pointing at a problem area.
  149. Return Value:
  150. None. This routine probably shouldn't return.
  151. --*/
  152. {
  153. RtlDebugPrint(" *** Heap corruption: Heap 0x%x, Code %d, Parameter 0x%x "
  154. "***\n",
  155. Heap,
  156. Code,
  157. Parameter);
  158. ASSERT(FALSE);
  159. return;
  160. }