bootmem.c 4.8 KB

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