efi_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application memory management
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <inttypes.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <watchdog.h>
  13. #include <linux/list_sort.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. efi_uintn_t efi_memory_map_key;
  16. struct efi_mem_list {
  17. struct list_head link;
  18. struct efi_mem_desc desc;
  19. };
  20. #define EFI_CARVE_NO_OVERLAP -1
  21. #define EFI_CARVE_LOOP_AGAIN -2
  22. #define EFI_CARVE_OVERLAPS_NONRAM -3
  23. /* This list contains all memory map items */
  24. LIST_HEAD(efi_mem);
  25. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  26. void *efi_bounce_buffer;
  27. #endif
  28. /*
  29. * U-Boot services each EFI AllocatePool request as a separate
  30. * (multiple) page allocation. We have to track the number of pages
  31. * to be able to free the correct amount later.
  32. * EFI requires 8 byte alignment for pool allocations, so we can
  33. * prepend each allocation with an 64 bit header tracking the
  34. * allocation size, and hand out the remainder to the caller.
  35. */
  36. struct efi_pool_allocation {
  37. u64 num_pages;
  38. char data[] __aligned(ARCH_DMA_MINALIGN);
  39. };
  40. /*
  41. * Sorts the memory list from highest address to lowest address
  42. *
  43. * When allocating memory we should always start from the highest
  44. * address chunk, so sort the memory list such that the first list
  45. * iterator gets the highest address and goes lower from there.
  46. */
  47. static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
  48. {
  49. struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
  50. struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
  51. if (mema->desc.physical_start == memb->desc.physical_start)
  52. return 0;
  53. else if (mema->desc.physical_start < memb->desc.physical_start)
  54. return 1;
  55. else
  56. return -1;
  57. }
  58. static void efi_mem_sort(void)
  59. {
  60. list_sort(NULL, &efi_mem, efi_mem_cmp);
  61. }
  62. /** efi_mem_carve_out - unmap memory region
  63. *
  64. * @map: memory map
  65. * @carve_desc: memory region to unmap
  66. * @overlap_only_ram: the carved out region may only overlap RAM
  67. * Return Value: the number of overlapping pages which have been
  68. * removed from the map,
  69. * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
  70. * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
  71. * and the map contains anything but free ram
  72. * (only when overlap_only_ram is true),
  73. * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
  74. * traversed again, as it has been altered.
  75. *
  76. * Unmaps all memory occupied by the carve_desc region from the list entry
  77. * pointed to by map.
  78. *
  79. * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
  80. * to re-add the already carved out pages to the mapping.
  81. */
  82. static s64 efi_mem_carve_out(struct efi_mem_list *map,
  83. struct efi_mem_desc *carve_desc,
  84. bool overlap_only_ram)
  85. {
  86. struct efi_mem_list *newmap;
  87. struct efi_mem_desc *map_desc = &map->desc;
  88. uint64_t map_start = map_desc->physical_start;
  89. uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
  90. uint64_t carve_start = carve_desc->physical_start;
  91. uint64_t carve_end = carve_start +
  92. (carve_desc->num_pages << EFI_PAGE_SHIFT);
  93. /* check whether we're overlapping */
  94. if ((carve_end <= map_start) || (carve_start >= map_end))
  95. return EFI_CARVE_NO_OVERLAP;
  96. /* We're overlapping with non-RAM, warn the caller if desired */
  97. if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
  98. return EFI_CARVE_OVERLAPS_NONRAM;
  99. /* Sanitize carve_start and carve_end to lie within our bounds */
  100. carve_start = max(carve_start, map_start);
  101. carve_end = min(carve_end, map_end);
  102. /* Carving at the beginning of our map? Just move it! */
  103. if (carve_start == map_start) {
  104. if (map_end == carve_end) {
  105. /* Full overlap, just remove map */
  106. list_del(&map->link);
  107. free(map);
  108. } else {
  109. map->desc.physical_start = carve_end;
  110. map->desc.num_pages = (map_end - carve_end)
  111. >> EFI_PAGE_SHIFT;
  112. }
  113. return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
  114. }
  115. /*
  116. * Overlapping maps, just split the list map at carve_start,
  117. * it will get moved or removed in the next iteration.
  118. *
  119. * [ map_desc |__carve_start__| newmap ]
  120. */
  121. /* Create a new map from [ carve_start ... map_end ] */
  122. newmap = calloc(1, sizeof(*newmap));
  123. newmap->desc = map->desc;
  124. newmap->desc.physical_start = carve_start;
  125. newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
  126. /* Insert before current entry (descending address order) */
  127. list_add_tail(&newmap->link, &map->link);
  128. /* Shrink the map to [ map_start ... carve_start ] */
  129. map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
  130. return EFI_CARVE_LOOP_AGAIN;
  131. }
  132. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  133. bool overlap_only_ram)
  134. {
  135. struct list_head *lhandle;
  136. struct efi_mem_list *newlist;
  137. bool carve_again;
  138. uint64_t carved_pages = 0;
  139. debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
  140. start, pages, memory_type, overlap_only_ram ? "yes" : "no");
  141. if (memory_type >= EFI_MAX_MEMORY_TYPE)
  142. return EFI_INVALID_PARAMETER;
  143. if (!pages)
  144. return start;
  145. ++efi_memory_map_key;
  146. newlist = calloc(1, sizeof(*newlist));
  147. newlist->desc.type = memory_type;
  148. newlist->desc.physical_start = start;
  149. newlist->desc.virtual_start = start;
  150. newlist->desc.num_pages = pages;
  151. switch (memory_type) {
  152. case EFI_RUNTIME_SERVICES_CODE:
  153. case EFI_RUNTIME_SERVICES_DATA:
  154. newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
  155. (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
  156. break;
  157. case EFI_MMAP_IO:
  158. newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
  159. break;
  160. default:
  161. newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
  162. break;
  163. }
  164. /* Add our new map */
  165. do {
  166. carve_again = false;
  167. list_for_each(lhandle, &efi_mem) {
  168. struct efi_mem_list *lmem;
  169. s64 r;
  170. lmem = list_entry(lhandle, struct efi_mem_list, link);
  171. r = efi_mem_carve_out(lmem, &newlist->desc,
  172. overlap_only_ram);
  173. switch (r) {
  174. case EFI_CARVE_OVERLAPS_NONRAM:
  175. /*
  176. * The user requested to only have RAM overlaps,
  177. * but we hit a non-RAM region. Error out.
  178. */
  179. return 0;
  180. case EFI_CARVE_NO_OVERLAP:
  181. /* Just ignore this list entry */
  182. break;
  183. case EFI_CARVE_LOOP_AGAIN:
  184. /*
  185. * We split an entry, but need to loop through
  186. * the list again to actually carve it.
  187. */
  188. carve_again = true;
  189. break;
  190. default:
  191. /* We carved a number of pages */
  192. carved_pages += r;
  193. carve_again = true;
  194. break;
  195. }
  196. if (carve_again) {
  197. /* The list changed, we need to start over */
  198. break;
  199. }
  200. }
  201. } while (carve_again);
  202. if (overlap_only_ram && (carved_pages != pages)) {
  203. /*
  204. * The payload wanted to have RAM overlaps, but we overlapped
  205. * with an unallocated region. Error out.
  206. */
  207. return 0;
  208. }
  209. /* Add our new map */
  210. list_add_tail(&newlist->link, &efi_mem);
  211. /* And make sure memory is listed in descending order */
  212. efi_mem_sort();
  213. return start;
  214. }
  215. static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
  216. {
  217. struct list_head *lhandle;
  218. list_for_each(lhandle, &efi_mem) {
  219. struct efi_mem_list *lmem = list_entry(lhandle,
  220. struct efi_mem_list, link);
  221. struct efi_mem_desc *desc = &lmem->desc;
  222. uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
  223. uint64_t desc_end = desc->physical_start + desc_len;
  224. uint64_t curmax = min(max_addr, desc_end);
  225. uint64_t ret = curmax - len;
  226. /* We only take memory from free RAM */
  227. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  228. continue;
  229. /* Out of bounds for max_addr */
  230. if ((ret + len) > max_addr)
  231. continue;
  232. /* Out of bounds for upper map limit */
  233. if ((ret + len) > desc_end)
  234. continue;
  235. /* Out of bounds for lower map limit */
  236. if (ret < desc->physical_start)
  237. continue;
  238. /* Return the highest address in this map within bounds */
  239. return ret;
  240. }
  241. return 0;
  242. }
  243. /*
  244. * Allocate memory pages.
  245. *
  246. * @type type of allocation to be performed
  247. * @memory_type usage type of the allocated memory
  248. * @pages number of pages to be allocated
  249. * @memory allocated memory
  250. * @return status code
  251. */
  252. efi_status_t efi_allocate_pages(int type, int memory_type,
  253. efi_uintn_t pages, uint64_t *memory)
  254. {
  255. u64 len = pages << EFI_PAGE_SHIFT;
  256. efi_status_t r = EFI_SUCCESS;
  257. uint64_t addr;
  258. if (!memory)
  259. return EFI_INVALID_PARAMETER;
  260. switch (type) {
  261. case EFI_ALLOCATE_ANY_PAGES:
  262. /* Any page */
  263. addr = efi_find_free_memory(len, -1ULL);
  264. if (!addr) {
  265. r = EFI_NOT_FOUND;
  266. break;
  267. }
  268. break;
  269. case EFI_ALLOCATE_MAX_ADDRESS:
  270. /* Max address */
  271. addr = efi_find_free_memory(len, *memory);
  272. if (!addr) {
  273. r = EFI_NOT_FOUND;
  274. break;
  275. }
  276. break;
  277. case EFI_ALLOCATE_ADDRESS:
  278. /* Exact address, reserve it. The addr is already in *memory. */
  279. addr = *memory;
  280. break;
  281. default:
  282. /* UEFI doesn't specify other allocation types */
  283. r = EFI_INVALID_PARAMETER;
  284. break;
  285. }
  286. if (r == EFI_SUCCESS) {
  287. uint64_t ret;
  288. /* Reserve that map in our memory maps */
  289. ret = efi_add_memory_map(addr, pages, memory_type, true);
  290. if (ret == addr) {
  291. *memory = (uintptr_t)map_sysmem(addr, len);
  292. } else {
  293. /* Map would overlap, bail out */
  294. r = EFI_OUT_OF_RESOURCES;
  295. }
  296. }
  297. return r;
  298. }
  299. void *efi_alloc(uint64_t len, int memory_type)
  300. {
  301. uint64_t ret = 0;
  302. uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  303. efi_status_t r;
  304. r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
  305. &ret);
  306. if (r == EFI_SUCCESS)
  307. return (void*)(uintptr_t)ret;
  308. return NULL;
  309. }
  310. /*
  311. * Free memory pages.
  312. *
  313. * @memory start of the memory area to be freed
  314. * @pages number of pages to be freed
  315. * @return status code
  316. */
  317. efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
  318. {
  319. uint64_t r = 0;
  320. uint64_t addr = map_to_sysmem((void *)(uintptr_t)memory);
  321. r = efi_add_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY, false);
  322. /* Merging of adjacent free regions is missing */
  323. if (r == addr)
  324. return EFI_SUCCESS;
  325. return EFI_NOT_FOUND;
  326. }
  327. /*
  328. * Allocate memory from pool.
  329. *
  330. * @pool_type type of the pool from which memory is to be allocated
  331. * @size number of bytes to be allocated
  332. * @buffer allocated memory
  333. * @return status code
  334. */
  335. efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
  336. {
  337. efi_status_t r;
  338. struct efi_pool_allocation *alloc;
  339. u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
  340. EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  341. if (!buffer)
  342. return EFI_INVALID_PARAMETER;
  343. if (size == 0) {
  344. *buffer = NULL;
  345. return EFI_SUCCESS;
  346. }
  347. r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
  348. (uint64_t *)&alloc);
  349. if (r == EFI_SUCCESS) {
  350. alloc->num_pages = num_pages;
  351. *buffer = alloc->data;
  352. }
  353. return r;
  354. }
  355. /*
  356. * Free memory from pool.
  357. *
  358. * @buffer start of memory to be freed
  359. * @return status code
  360. */
  361. efi_status_t efi_free_pool(void *buffer)
  362. {
  363. efi_status_t r;
  364. struct efi_pool_allocation *alloc;
  365. if (buffer == NULL)
  366. return EFI_INVALID_PARAMETER;
  367. alloc = container_of(buffer, struct efi_pool_allocation, data);
  368. /* Sanity check, was the supplied address returned by allocate_pool */
  369. assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
  370. r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
  371. return r;
  372. }
  373. /*
  374. * Get map describing memory usage.
  375. *
  376. * @memory_map_size on entry the size, in bytes, of the memory map buffer,
  377. * on exit the size of the copied memory map
  378. * @memory_map buffer to which the memory map is written
  379. * @map_key key for the memory map
  380. * @descriptor_size size of an individual memory descriptor
  381. * @descriptor_version version number of the memory descriptor structure
  382. * @return status code
  383. */
  384. efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
  385. struct efi_mem_desc *memory_map,
  386. efi_uintn_t *map_key,
  387. efi_uintn_t *descriptor_size,
  388. uint32_t *descriptor_version)
  389. {
  390. efi_uintn_t map_size = 0;
  391. int map_entries = 0;
  392. struct list_head *lhandle;
  393. efi_uintn_t provided_map_size = *memory_map_size;
  394. if (!memory_map_size)
  395. return EFI_INVALID_PARAMETER;
  396. list_for_each(lhandle, &efi_mem)
  397. map_entries++;
  398. map_size = map_entries * sizeof(struct efi_mem_desc);
  399. *memory_map_size = map_size;
  400. if (provided_map_size < map_size)
  401. return EFI_BUFFER_TOO_SMALL;
  402. if (!memory_map)
  403. return EFI_INVALID_PARAMETER;
  404. if (descriptor_size)
  405. *descriptor_size = sizeof(struct efi_mem_desc);
  406. if (descriptor_version)
  407. *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
  408. /* Copy list into array */
  409. /* Return the list in ascending order */
  410. memory_map = &memory_map[map_entries - 1];
  411. list_for_each(lhandle, &efi_mem) {
  412. struct efi_mem_list *lmem;
  413. lmem = list_entry(lhandle, struct efi_mem_list, link);
  414. *memory_map = lmem->desc;
  415. memory_map--;
  416. }
  417. if (map_key)
  418. *map_key = efi_memory_map_key;
  419. return EFI_SUCCESS;
  420. }
  421. __weak void efi_add_known_memory(void)
  422. {
  423. int i;
  424. /* Add RAM */
  425. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  426. u64 ram_start = gd->bd->bi_dram[i].start;
  427. u64 ram_size = gd->bd->bi_dram[i].size;
  428. u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
  429. u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  430. efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
  431. false);
  432. }
  433. }
  434. /* Add memory regions for U-Boot's memory and for the runtime services code */
  435. static void add_u_boot_and_runtime(void)
  436. {
  437. unsigned long runtime_start, runtime_end, runtime_pages;
  438. unsigned long uboot_start, uboot_pages;
  439. unsigned long uboot_stack_size = 16 * 1024 * 1024;
  440. /* Add U-Boot */
  441. uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
  442. uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
  443. efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
  444. /* Add Runtime Services */
  445. runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
  446. runtime_end = (ulong)&__efi_runtime_stop;
  447. runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
  448. runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
  449. efi_add_memory_map(runtime_start, runtime_pages,
  450. EFI_RUNTIME_SERVICES_CODE, false);
  451. }
  452. int efi_memory_init(void)
  453. {
  454. efi_add_known_memory();
  455. if (!IS_ENABLED(CONFIG_SANDBOX))
  456. add_u_boot_and_runtime();
  457. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  458. /* Request a 32bit 64MB bounce buffer region */
  459. uint64_t efi_bounce_buffer_addr = 0xffffffff;
  460. if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
  461. (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
  462. &efi_bounce_buffer_addr) != EFI_SUCCESS)
  463. return -1;
  464. efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
  465. #endif
  466. return 0;
  467. }