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