efi_memory.c 15 KB

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