memory.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. memory.h
  5. Abstract:
  6. This header contains definitions for UEFI core memory services.
  7. Author:
  8. Evan Green 27-Feb-2014
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. //
  14. // ---------------------------------------------------------------- Definitions
  15. //
  16. //
  17. // Define the expansion size of pool and memory descriptor allocations.
  18. //
  19. #define EFI_MEMORY_EXPANSION_SIZE EFI_PAGE_SIZE
  20. //
  21. // ------------------------------------------------------ Data Type Definitions
  22. //
  23. //
  24. // -------------------------------------------------------------------- Globals
  25. //
  26. extern EFI_LOCK EfiMemoryLock;
  27. //
  28. // -------------------------------------------------------- Function Prototypes
  29. //
  30. EFIAPI
  31. EFI_STATUS
  32. EfiCoreAllocatePages (
  33. EFI_ALLOCATE_TYPE Type,
  34. EFI_MEMORY_TYPE MemoryType,
  35. UINTN Pages,
  36. EFI_PHYSICAL_ADDRESS *Memory
  37. );
  38. /*++
  39. Routine Description:
  40. This routine allocates memory pages from the system.
  41. Arguments:
  42. Type - Supplies the allocation strategy to use.
  43. MemoryType - Supplies the memory type of the allocation.
  44. Pages - Supplies the number of contiguous EFI_PAGE_SIZE pages.
  45. Memory - Supplies a pointer that on input contains a physical address whose
  46. use depends on the allocation strategy. On output, the physical address
  47. of the allocation will be returned.
  48. Return Value:
  49. EFI_SUCCESS on success.
  50. EFI_INVALID_PARAMETER if the Type or MemoryType are invalid, or Memory is
  51. NULL.
  52. EFI_OUT_OF_RESOURCES if the pages could not be allocated.
  53. EFI_NOT_FOUND if the requested pages could not be found.
  54. --*/
  55. EFIAPI
  56. EFI_STATUS
  57. EfiCoreFreePages (
  58. EFI_PHYSICAL_ADDRESS Memory,
  59. UINTN Pages
  60. );
  61. /*++
  62. Routine Description:
  63. This routine frees memory pages back to the system.
  64. Arguments:
  65. Memory - Supplies the base physical address of the allocation to free.
  66. Pages - Supplies the number of pages to free.
  67. Return Value:
  68. EFI_SUCCESS on success.
  69. EFI_INVALID_PARAMETER if the memory is not page aligned or is invalid.
  70. EFI_NOT_FOUND if the requested pages were not allocated.
  71. --*/
  72. EFIAPI
  73. EFI_STATUS
  74. EfiCoreGetMemoryMap (
  75. UINTN *MemoryMapSize,
  76. EFI_MEMORY_DESCRIPTOR *MemoryMap,
  77. UINTN *MapKey,
  78. UINTN *DescriptorSize,
  79. UINT32 *DescriptorVersion
  80. );
  81. /*++
  82. Routine Description:
  83. This routine returns the current memory map.
  84. Arguments:
  85. MemoryMapSize - Supplies a pointer to the size, in bytes, of the memory
  86. map buffer. On input, this is the size of the buffer allocated by the
  87. caller. On output, this is the size of the buffer returned by the
  88. firmware if the buffer was large enough, or the size of the buffer
  89. needed if the buffer was too small.
  90. MemoryMap - Supplies a pointer to a caller-allocated buffer where the
  91. memory map will be written on success.
  92. MapKey - Supplies a pointer where the firmware returns the map key.
  93. DescriptorSize - Supplies a pointer where the firmware returns the size of
  94. the EFI_MEMORY_DESCRIPTOR structure.
  95. DescriptorVersion - Supplies a pointer where the firmware returns the
  96. version number associated with the EFI_MEMORY_DESCRIPTOR structure.
  97. Return Value:
  98. EFI_SUCCESS on success.
  99. EFI_BUFFER_TOO_SMALL if the supplied buffer was too small. The size needed
  100. is returned in the size parameter.
  101. EFI_INVALID_PARAMETER if the supplied size or memory map pointers are NULL.
  102. --*/
  103. VOID *
  104. EfiCoreAllocatePoolPages (
  105. EFI_MEMORY_TYPE PoolType,
  106. UINTN PageCount,
  107. UINTN Alignment
  108. );
  109. /*++
  110. Routine Description:
  111. This routine allocates pages to back pool allocations and memory map
  112. descriptors.
  113. Arguments:
  114. PoolType - Supplies the memory type of the allocation.
  115. PageCount - Supplies the number of pages to allocate.
  116. Alignment - Supplies the required alignment.
  117. Return Value:
  118. Returns a pointer to the allocated memory on success.
  119. NULL on allocation failure.
  120. --*/
  121. VOID
  122. EfiCoreFreePoolPages (
  123. EFI_PHYSICAL_ADDRESS Memory,
  124. UINTN PageCount
  125. );
  126. /*++
  127. Routine Description:
  128. This routine frees pages allocated for pool or descriptor.
  129. Arguments:
  130. Memory - Supplies the address of the allocation.
  131. PageCount - Supplies the number of pages to free.
  132. Return Value:
  133. None.
  134. --*/
  135. EFI_STATUS
  136. EfiCoreInitializeMemoryServices (
  137. VOID *FirmwareLowestAddress,
  138. UINTN FirmwareSize,
  139. VOID *StackBase,
  140. UINTN StackSize
  141. );
  142. /*++
  143. Routine Description:
  144. This routine initializes core UEFI memory services.
  145. Arguments:
  146. FirmwareLowestAddress - Supplies the lowest address where the firmware was
  147. loaded into memory.
  148. FirmwareSize - Supplies the size of the firmware image in memory, in bytes.
  149. StackBase - Supplies the base (lowest) address of the stack.
  150. StackSize - Supplies the size in bytes of the stack. This should be at
  151. least 0x4000 bytes (16kB).
  152. Return Value:
  153. EFI status code.
  154. --*/
  155. EFI_STATUS
  156. EfiCoreTerminateMemoryServices (
  157. UINTN MapKey
  158. );
  159. /*++
  160. Routine Description:
  161. This routine terminates memory services.
  162. Arguments:
  163. MapKey - Supplies the map key reported by the boot application. This is
  164. checked against the current map key to ensure the boot application has
  165. an up to date view of the world.
  166. Return Value:
  167. EFI_SUCCESS on success.
  168. EFI_INVALID_PARAMETER if the map key is not valid or the memory map is
  169. not consistent.
  170. --*/
  171. EFIAPI
  172. VOID
  173. EfiCoreEmptyCallbackFunction (
  174. EFI_EVENT Event,
  175. VOID *Context
  176. );
  177. /*++
  178. Routine Description:
  179. This routine does nothing but return. It conforms to the event notification
  180. function prototype.
  181. Arguments:
  182. Event - Supplies an unused event.
  183. Context - Supplies an unused context pointer.
  184. Return Value:
  185. None.
  186. --*/
  187. EFIAPI
  188. VOID
  189. EfiCoreCopyMemory (
  190. VOID *Destination,
  191. VOID *Source,
  192. UINTN Length
  193. );
  194. /*++
  195. Routine Description:
  196. This routine copies the contents of one buffer to another.
  197. Arguments:
  198. Destination - Supplies a pointer to the destination of the copy.
  199. Source - Supplies a pointer to the source of the copy.
  200. Length - Supplies the number of bytes to copy.
  201. Return Value:
  202. None.
  203. --*/
  204. EFIAPI
  205. VOID
  206. EfiCoreSetMemory (
  207. VOID *Buffer,
  208. UINTN Size,
  209. UINT8 Value
  210. );
  211. /*++
  212. Routine Description:
  213. This routine fills a buffer with a specified value.
  214. Arguments:
  215. Buffer - Supplies a pointer to the buffer to fill.
  216. Size - Supplies the size of the buffer in bytes.
  217. Value - Supplies the value to fill the buffer with.
  218. Return Value:
  219. None.
  220. --*/
  221. INTN
  222. EfiCoreCompareMemory (
  223. VOID *FirstBuffer,
  224. VOID *SecondBuffer,
  225. UINTN Length
  226. );
  227. /*++
  228. Routine Description:
  229. This routine compares the contents of two buffers for equality.
  230. Arguments:
  231. FirstBuffer - Supplies a pointer to the first buffer to compare.
  232. SecondBuffer - Supplies a pointer to the second buffer to compare.
  233. Length - Supplies the number of bytes to compare.
  234. Return Value:
  235. 0 if the buffers are identical.
  236. Returns the first mismatched byte as
  237. First[MismatchIndex] - Second[MismatchIndex].
  238. --*/
  239. BOOLEAN
  240. EfiCoreCompareGuids (
  241. EFI_GUID *FirstGuid,
  242. EFI_GUID *SecondGuid
  243. );
  244. /*++
  245. Routine Description:
  246. This routine compares two GUIDs.
  247. Arguments:
  248. FirstGuid - Supplies a pointer to the first GUID.
  249. SecondGuid - Supplies a pointer to the second GUID.
  250. Return Value:
  251. TRUE if the GUIDs are equal.
  252. FALSE if the GUIDs are different.
  253. --*/
  254. VOID *
  255. EfiCoreAllocateBootPool (
  256. UINTN Size
  257. );
  258. /*++
  259. Routine Description:
  260. This routine allocates pool from boot services data.
  261. Arguments:
  262. Size - Supplies the size of the allocation in bytes.
  263. Return Value:
  264. Returns a pointer to the allocation on success.
  265. NULL on allocation failure.
  266. --*/
  267. VOID *
  268. EfiCoreAllocateRuntimePool (
  269. UINTN Size
  270. );
  271. /*++
  272. Routine Description:
  273. This routine allocates pool from runtime services data.
  274. Arguments:
  275. Size - Supplies the size of the allocation in bytes.
  276. Return Value:
  277. Returns a pointer to the allocation on success.
  278. NULL on allocation failure.
  279. --*/
  280. INTN
  281. EfiCoreFindHighBitSet64 (
  282. UINT64 Value
  283. );
  284. /*++
  285. Routine Description:
  286. This routine returns the bit position of the highest bit set in a 64-bit
  287. value.
  288. Arguments:
  289. Value - Supplies the input value.
  290. Return Value:
  291. Returns the index of the highest bit set, between 0 and 63. If the value is
  292. zero, then -1 is returned.
  293. --*/
  294. INTN
  295. EfiCoreFindHighBitSet32 (
  296. UINT32 Value
  297. );
  298. /*++
  299. Routine Description:
  300. This routine returns the bit position of the highest bit set in a 32-bit
  301. value.
  302. Arguments:
  303. Value - Supplies the input value.
  304. Return Value:
  305. Returns the index of the highest bit set, between 0 and 31. If the value is
  306. zero, then -1 is returned.
  307. --*/
  308. VOID
  309. EfiCoreCalculateTableCrc32 (
  310. EFI_TABLE_HEADER *Header
  311. );
  312. /*++
  313. Routine Description:
  314. This routine recalculates the CRC32 of a given EFI table.
  315. Arguments:
  316. Header - Supplies a pointer to the header. The size member will be used to
  317. determine the size of the entire table.
  318. Return Value:
  319. None. The CRC is set in the header.
  320. --*/
  321. EFIAPI
  322. EFI_EVENT
  323. EfiCoreCreateProtocolNotifyEvent (
  324. EFI_GUID *ProtocolGuid,
  325. EFI_TPL NotifyTpl,
  326. EFI_EVENT_NOTIFY NotifyFunction,
  327. VOID *NotifyContext,
  328. VOID **Registration
  329. );
  330. /*++
  331. Routine Description:
  332. This routine creates an event, then registers that event to be notified
  333. whenever the given protocol appears. Finally, it signals the event so that
  334. any pre-existing protocols will be found.
  335. Arguments:
  336. ProtocolGuid - Supplies a pointer to the GUID of the protocol to watch.
  337. NotifyTpl - Supplies the Task Priority Level of the callback function.
  338. NotifyFunction - Supplies a pointer to the function to call when a new
  339. protocol with the given GUID crops up.
  340. NotifyContext - Supplies a pointer to pass into the notify function.
  341. Registration - Supplies a pointer where the registration token for the
  342. event will be returned.
  343. Return Value:
  344. Returns the notification event that was created.
  345. NULL on failure.
  346. --*/
  347. UINTN
  348. EfiCoreStringLength (
  349. CHAR16 *String
  350. );
  351. /*++
  352. Routine Description:
  353. This routine returns the length of the given string, in characters (not
  354. bytes).
  355. Arguments:
  356. String - Supplies a pointer to the string.
  357. Return Value:
  358. Returns the number of characters in the string.
  359. --*/
  360. VOID
  361. EfiCoreCopyString (
  362. CHAR16 *Destination,
  363. CHAR16 *Source
  364. );
  365. /*++
  366. Routine Description:
  367. This routine copies one string over to another buffer.
  368. Arguments:
  369. Destination - Supplies a pointer to the destination buffer where the
  370. string will be copied to.
  371. Source - Supplies a pointer to the string to copy.
  372. Return Value:
  373. None.
  374. --*/
  375. EFI_TPL
  376. EfiCoreGetCurrentTpl (
  377. VOID
  378. );
  379. /*++
  380. Routine Description:
  381. This routine returns the current TPL.
  382. Arguments:
  383. None.
  384. Return Value:
  385. Returns the current TPL.
  386. --*/
  387. EFI_STATUS
  388. EfiCoreInitializePool (
  389. VOID
  390. );
  391. /*++
  392. Routine Description:
  393. This routine initializes EFI core pool services.
  394. Arguments:
  395. None.
  396. Return Value:
  397. EFI status code.
  398. --*/
  399. EFIAPI
  400. EFI_STATUS
  401. EfiCoreAllocatePool (
  402. EFI_MEMORY_TYPE PoolType,
  403. UINTN Size,
  404. VOID **Buffer
  405. );
  406. /*++
  407. Routine Description:
  408. This routine allocates memory from the heap.
  409. Arguments:
  410. PoolType - Supplies the type of pool to allocate.
  411. Size - Supplies the number of bytes to allocate from the pool.
  412. Buffer - Supplies a pointer where a pointer to the allocated buffer will
  413. be returned on success.
  414. Return Value:
  415. EFI_SUCCESS on success.
  416. EFI_OUT_OF_RESOURCES if memory could not be allocated.
  417. EFI_INVALID_PARAMETER if the pool type was invalid or the buffer is NULL.
  418. --*/
  419. EFIAPI
  420. EFI_STATUS
  421. EfiCoreFreePool (
  422. VOID *Buffer
  423. );
  424. /*++
  425. Routine Description:
  426. This routine frees heap allocated memory.
  427. Arguments:
  428. Buffer - Supplies a pointer to the buffer to free.
  429. Return Value:
  430. EFI_SUCCESS on success.
  431. EFI_INVALID_PARAMETER if the buffer was invalid.
  432. --*/