memory.h 12 KB

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