mm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* mm.c - generic EFI memory management */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/efi/api.h>
  22. #include <grub/efi/efi.h>
  23. #include <grub/cpu/efi/memory.h>
  24. #if defined (__i386__) || defined (__x86_64__)
  25. #include <grub/pci.h>
  26. #endif
  27. #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
  28. ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
  29. #define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> 12)
  30. #define BYTES_TO_PAGES_DOWN(bytes) ((bytes) >> 12)
  31. #define PAGES_TO_BYTES(pages) ((pages) << 12)
  32. /* The size of a memory map obtained from the firmware. This must be
  33. a multiplier of 4KB. */
  34. #define MEMORY_MAP_SIZE 0x3000
  35. /* The default heap size for GRUB itself in bytes. */
  36. #define DEFAULT_HEAP_SIZE 0x2000000
  37. static void *finish_mmap_buf = 0;
  38. static grub_efi_uintn_t finish_mmap_size = 0;
  39. static grub_efi_uintn_t finish_key = 0;
  40. static grub_efi_uintn_t finish_desc_size;
  41. static grub_efi_uint32_t finish_desc_version;
  42. int grub_efi_is_finished = 0;
  43. /*
  44. * We need to roll back EFI allocations on exit. Remember allocations that
  45. * we'll free on exit.
  46. */
  47. struct efi_allocation;
  48. struct efi_allocation {
  49. grub_efi_physical_address_t address;
  50. grub_efi_uint64_t pages;
  51. struct efi_allocation *next;
  52. };
  53. static struct efi_allocation *efi_allocated_memory;
  54. static void
  55. grub_efi_store_alloc (grub_efi_physical_address_t address,
  56. grub_efi_uintn_t pages)
  57. {
  58. grub_efi_boot_services_t *b;
  59. struct efi_allocation *alloc;
  60. grub_efi_status_t status;
  61. b = grub_efi_system_table->boot_services;
  62. status = b->allocate_pool (GRUB_EFI_LOADER_DATA,
  63. sizeof(*alloc), (void**)&alloc);
  64. if (status == GRUB_EFI_SUCCESS)
  65. {
  66. alloc->next = efi_allocated_memory;
  67. alloc->address = address;
  68. alloc->pages = pages;
  69. efi_allocated_memory = alloc;
  70. }
  71. else
  72. grub_printf ("Could not malloc memory to remember EFI allocation. "
  73. "Exiting GRUB won't free all memory.\n");
  74. }
  75. static void
  76. grub_efi_drop_alloc (grub_efi_physical_address_t address,
  77. grub_efi_uintn_t pages)
  78. {
  79. struct efi_allocation *ea, *eap;
  80. grub_efi_boot_services_t *b;
  81. b = grub_efi_system_table->boot_services;
  82. for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
  83. {
  84. if (ea->address != address)
  85. continue;
  86. if (ea->pages != pages)
  87. grub_fatal ("grub_efi_drop_alloc() called with wrong page count");
  88. /* Remove the current entry from the list. */
  89. if (eap)
  90. eap->next = ea->next;
  91. else
  92. efi_allocated_memory = ea->next;
  93. /* Then free the memory backing it. */
  94. b->free_pool (ea);
  95. /* And leave, we're done. */
  96. break;
  97. }
  98. }
  99. /* Allocate pages. Return the pointer to the first of allocated pages. */
  100. void *
  101. grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
  102. grub_efi_uintn_t pages,
  103. grub_efi_allocate_type_t alloctype,
  104. grub_efi_memory_type_t memtype)
  105. {
  106. grub_efi_status_t status;
  107. grub_efi_boot_services_t *b;
  108. /* Limit the memory access to less than 4GB for 32-bit platforms. */
  109. if (address > GRUB_EFI_MAX_USABLE_ADDRESS)
  110. {
  111. char inv_addr[17], max_addr[17]; /* log16(2^64) = 16, plus NUL. */
  112. grub_snprintf (inv_addr, sizeof (inv_addr) - 1, "%" PRIxGRUB_UINT64_T,
  113. address);
  114. grub_snprintf (max_addr, sizeof (max_addr) - 1, "%" PRIxGRUB_UINT64_T,
  115. (grub_efi_uint64_t) GRUB_EFI_MAX_USABLE_ADDRESS);
  116. grub_error (GRUB_ERR_BAD_ARGUMENT,
  117. N_("invalid memory address (0x%s > 0x%s)"), inv_addr, max_addr);
  118. return NULL;
  119. }
  120. b = grub_efi_system_table->boot_services;
  121. status = b->allocate_pages (alloctype, memtype, pages, &address);
  122. if (status != GRUB_EFI_SUCCESS)
  123. {
  124. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  125. return NULL;
  126. }
  127. if (address == 0)
  128. {
  129. /* Uggh, the address 0 was allocated... This is too annoying,
  130. so reallocate another one. */
  131. address = GRUB_EFI_MAX_USABLE_ADDRESS;
  132. status = b->allocate_pages (alloctype, memtype, pages, &address);
  133. b->free_pages (0, pages);
  134. if (status != GRUB_EFI_SUCCESS)
  135. {
  136. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  137. return NULL;
  138. }
  139. }
  140. grub_efi_store_alloc (address, pages);
  141. return (void *) ((grub_addr_t) address);
  142. }
  143. void *
  144. grub_efi_allocate_any_pages (grub_efi_uintn_t pages)
  145. {
  146. return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS,
  147. pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
  148. GRUB_EFI_LOADER_DATA);
  149. }
  150. void *
  151. grub_efi_allocate_fixed (grub_efi_physical_address_t address,
  152. grub_efi_uintn_t pages)
  153. {
  154. return grub_efi_allocate_pages_real (address, pages,
  155. GRUB_EFI_ALLOCATE_ADDRESS,
  156. GRUB_EFI_LOADER_DATA);
  157. }
  158. /* Free pages starting from ADDRESS. */
  159. void
  160. grub_efi_free_pages (grub_efi_physical_address_t address,
  161. grub_efi_uintn_t pages)
  162. {
  163. grub_efi_boot_services_t *b;
  164. b = grub_efi_system_table->boot_services;
  165. b->free_pages (address, pages);
  166. grub_efi_drop_alloc (address, pages);
  167. }
  168. #if defined (__i386__) || defined (__x86_64__)
  169. /* Helper for stop_broadcom. */
  170. static int
  171. find_card (grub_pci_device_t dev, grub_pci_id_t pciid,
  172. void *data __attribute__ ((unused)))
  173. {
  174. grub_pci_address_t addr;
  175. grub_uint8_t cap;
  176. grub_uint16_t pm_state;
  177. if ((pciid & 0xffff) != GRUB_PCI_VENDOR_BROADCOM)
  178. return 0;
  179. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  180. if (grub_pci_read (addr) >> 24 != GRUB_PCI_CLASS_NETWORK)
  181. return 0;
  182. cap = grub_pci_find_capability (dev, GRUB_PCI_CAP_POWER_MANAGEMENT);
  183. if (!cap)
  184. return 0;
  185. addr = grub_pci_make_address (dev, cap + 4);
  186. pm_state = grub_pci_read_word (addr);
  187. pm_state = pm_state | 0x03;
  188. grub_pci_write_word (addr, pm_state);
  189. grub_pci_read_word (addr);
  190. return 0;
  191. }
  192. static void
  193. stop_broadcom (void)
  194. {
  195. grub_pci_iterate (find_card, NULL);
  196. }
  197. #endif
  198. grub_err_t
  199. grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
  200. grub_efi_uintn_t *map_key,
  201. grub_efi_uintn_t *efi_desc_size,
  202. grub_efi_uint32_t *efi_desc_version)
  203. {
  204. grub_efi_boot_services_t *b;
  205. grub_efi_status_t status;
  206. #if defined (__i386__) || defined (__x86_64__)
  207. const grub_uint16_t apple[] = { 'A', 'p', 'p', 'l', 'e' };
  208. int is_apple;
  209. is_apple = (grub_memcmp (grub_efi_system_table->firmware_vendor,
  210. apple, sizeof (apple)) == 0);
  211. #endif
  212. while (1)
  213. {
  214. if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
  215. &finish_desc_size, &finish_desc_version) < 0)
  216. return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
  217. if (outbuf && *outbuf_size < finish_mmap_size)
  218. return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
  219. finish_mmap_buf = grub_malloc (finish_mmap_size);
  220. if (!finish_mmap_buf)
  221. return grub_errno;
  222. if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
  223. &finish_desc_size, &finish_desc_version) <= 0)
  224. {
  225. grub_free (finish_mmap_buf);
  226. finish_mmap_buf = NULL;
  227. return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
  228. }
  229. b = grub_efi_system_table->boot_services;
  230. status = b->exit_boot_services (grub_efi_image_handle, finish_key);
  231. if (status == GRUB_EFI_SUCCESS)
  232. break;
  233. if (status != GRUB_EFI_INVALID_PARAMETER)
  234. {
  235. grub_free (finish_mmap_buf);
  236. finish_mmap_buf = NULL;
  237. return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
  238. }
  239. grub_free (finish_mmap_buf);
  240. finish_mmap_buf = NULL;
  241. grub_printf ("Trying to terminate EFI services again\n");
  242. }
  243. grub_efi_is_finished = 1;
  244. if (outbuf_size)
  245. *outbuf_size = finish_mmap_size;
  246. if (outbuf)
  247. grub_memcpy (outbuf, finish_mmap_buf, finish_mmap_size);
  248. if (map_key)
  249. *map_key = finish_key;
  250. if (efi_desc_size)
  251. *efi_desc_size = finish_desc_size;
  252. if (efi_desc_version)
  253. *efi_desc_version = finish_desc_version;
  254. #if defined (__i386__) || defined (__x86_64__)
  255. if (is_apple)
  256. stop_broadcom ();
  257. #endif
  258. return GRUB_ERR_NONE;
  259. }
  260. /*
  261. * To obtain the UEFI memory map, we must pass a buffer of sufficient size
  262. * to hold the entire map. This function returns a sane start value for
  263. * buffer size.
  264. */
  265. grub_efi_uintn_t
  266. grub_efi_find_mmap_size (void)
  267. {
  268. grub_efi_uintn_t mmap_size = 0;
  269. grub_efi_uintn_t desc_size;
  270. if (grub_efi_get_memory_map (&mmap_size, NULL, NULL, &desc_size, 0) < 0)
  271. {
  272. grub_error (GRUB_ERR_IO, "cannot get EFI memory map size");
  273. return 0;
  274. }
  275. /*
  276. * Add an extra page, since UEFI can alter the memory map itself on
  277. * callbacks or explicit calls, including console output.
  278. */
  279. return ALIGN_UP (mmap_size + GRUB_EFI_PAGE_SIZE, GRUB_EFI_PAGE_SIZE);
  280. }
  281. /* Get the memory map as defined in the EFI spec. Return 1 if successful,
  282. return 0 if partial, or return -1 if an error occurs. */
  283. int
  284. grub_efi_get_memory_map (grub_efi_uintn_t *memory_map_size,
  285. grub_efi_memory_descriptor_t *memory_map,
  286. grub_efi_uintn_t *map_key,
  287. grub_efi_uintn_t *descriptor_size,
  288. grub_efi_uint32_t *descriptor_version)
  289. {
  290. grub_efi_status_t status;
  291. grub_efi_boot_services_t *b;
  292. grub_efi_uintn_t key;
  293. grub_efi_uint32_t version;
  294. grub_efi_uintn_t size;
  295. if (grub_efi_is_finished)
  296. {
  297. int ret = 1;
  298. if (memory_map != NULL)
  299. {
  300. if (*memory_map_size < finish_mmap_size)
  301. {
  302. grub_memcpy (memory_map, finish_mmap_buf, *memory_map_size);
  303. ret = 0;
  304. }
  305. else
  306. grub_memcpy (memory_map, finish_mmap_buf, finish_mmap_size);
  307. }
  308. else
  309. {
  310. /*
  311. * Incomplete, no buffer to copy into, same as
  312. * GRUB_EFI_BUFFER_TOO_SMALL below.
  313. */
  314. ret = 0;
  315. }
  316. *memory_map_size = finish_mmap_size;
  317. if (map_key)
  318. *map_key = finish_key;
  319. if (descriptor_size)
  320. *descriptor_size = finish_desc_size;
  321. if (descriptor_version)
  322. *descriptor_version = finish_desc_version;
  323. return ret;
  324. }
  325. /* Allow some parameters to be missing. */
  326. if (! map_key)
  327. map_key = &key;
  328. if (! descriptor_version)
  329. descriptor_version = &version;
  330. if (! descriptor_size)
  331. descriptor_size = &size;
  332. b = grub_efi_system_table->boot_services;
  333. status = b->get_memory_map (memory_map_size, memory_map, map_key,
  334. descriptor_size, descriptor_version);
  335. if (*descriptor_size == 0)
  336. *descriptor_size = sizeof (grub_efi_memory_descriptor_t);
  337. if (status == GRUB_EFI_SUCCESS)
  338. return 1;
  339. else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
  340. return 0;
  341. else
  342. return -1;
  343. }
  344. /* Sort the memory map in place. */
  345. static void
  346. sort_memory_map (grub_efi_memory_descriptor_t *memory_map,
  347. grub_efi_uintn_t desc_size,
  348. grub_efi_memory_descriptor_t *memory_map_end)
  349. {
  350. grub_efi_memory_descriptor_t *d1;
  351. grub_efi_memory_descriptor_t *d2;
  352. for (d1 = memory_map;
  353. d1 < memory_map_end;
  354. d1 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size))
  355. {
  356. grub_efi_memory_descriptor_t *max_desc = d1;
  357. for (d2 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size);
  358. d2 < memory_map_end;
  359. d2 = NEXT_MEMORY_DESCRIPTOR (d2, desc_size))
  360. {
  361. if (max_desc->num_pages < d2->num_pages)
  362. max_desc = d2;
  363. }
  364. if (max_desc != d1)
  365. {
  366. grub_efi_memory_descriptor_t tmp;
  367. tmp = *d1;
  368. *d1 = *max_desc;
  369. *max_desc = tmp;
  370. }
  371. }
  372. }
  373. /* Filter the descriptors. GRUB needs only available memory. */
  374. static grub_efi_memory_descriptor_t *
  375. filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
  376. grub_efi_memory_descriptor_t *filtered_memory_map,
  377. grub_efi_uintn_t desc_size,
  378. grub_efi_memory_descriptor_t *memory_map_end)
  379. {
  380. grub_efi_memory_descriptor_t *desc;
  381. grub_efi_memory_descriptor_t *filtered_desc;
  382. for (desc = memory_map, filtered_desc = filtered_memory_map;
  383. desc < memory_map_end;
  384. desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
  385. {
  386. if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
  387. #if 1
  388. && desc->physical_start <= GRUB_EFI_MAX_USABLE_ADDRESS
  389. #endif
  390. && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000
  391. && desc->num_pages != 0)
  392. {
  393. grub_memcpy (filtered_desc, desc, desc_size);
  394. /* Avoid less than 1MB, because some loaders seem to be confused. */
  395. if (desc->physical_start < 0x100000)
  396. {
  397. desc->num_pages -= BYTES_TO_PAGES (0x100000
  398. - desc->physical_start);
  399. desc->physical_start = 0x100000;
  400. }
  401. #if 1
  402. if (BYTES_TO_PAGES (filtered_desc->physical_start)
  403. + filtered_desc->num_pages
  404. > BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_USABLE_ADDRESS))
  405. filtered_desc->num_pages
  406. = (BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_USABLE_ADDRESS)
  407. - BYTES_TO_PAGES (filtered_desc->physical_start));
  408. #endif
  409. if (filtered_desc->num_pages == 0)
  410. continue;
  411. filtered_desc = NEXT_MEMORY_DESCRIPTOR (filtered_desc, desc_size);
  412. }
  413. }
  414. return filtered_desc;
  415. }
  416. /* Add memory regions. */
  417. static grub_err_t
  418. add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
  419. grub_efi_uintn_t desc_size,
  420. grub_efi_memory_descriptor_t *memory_map_end,
  421. grub_efi_uint64_t required_pages,
  422. unsigned int flags)
  423. {
  424. grub_efi_memory_descriptor_t *desc;
  425. for (desc = memory_map;
  426. desc < memory_map_end;
  427. desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
  428. {
  429. grub_efi_uint64_t pages;
  430. grub_efi_physical_address_t start;
  431. void *addr;
  432. start = desc->physical_start;
  433. pages = desc->num_pages;
  434. if (pages < required_pages && (flags & GRUB_MM_ADD_REGION_CONSECUTIVE))
  435. continue;
  436. if (pages > required_pages)
  437. {
  438. start += PAGES_TO_BYTES (pages - required_pages);
  439. pages = required_pages;
  440. }
  441. addr = grub_efi_allocate_pages_real (start, pages,
  442. GRUB_EFI_ALLOCATE_ADDRESS,
  443. GRUB_EFI_LOADER_CODE);
  444. if (! addr)
  445. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  446. "Memory starting at %p (%u pages) marked as free, but EFI would not allocate",
  447. (void *) ((grub_addr_t) start), (unsigned) pages);
  448. grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
  449. required_pages -= pages;
  450. if (required_pages == 0)
  451. break;
  452. }
  453. if (required_pages > 0)
  454. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  455. "could not allocate all requested memory: %" PRIuGRUB_UINT64_T " pages still required after iterating EFI memory map",
  456. required_pages);
  457. return GRUB_ERR_NONE;
  458. }
  459. void
  460. grub_efi_memory_fini (void)
  461. {
  462. /*
  463. * Free all stale allocations. grub_efi_free_pages() will remove
  464. * the found entry from the list and it will always find the first
  465. * list entry (efi_allocated_memory is the list start). Hence we
  466. * remove all entries from the list until none is left altogether.
  467. */
  468. while (efi_allocated_memory)
  469. grub_efi_free_pages (efi_allocated_memory->address,
  470. efi_allocated_memory->pages);
  471. }
  472. #if 0
  473. /* Print the memory map. */
  474. static void
  475. print_memory_map (grub_efi_memory_descriptor_t *memory_map,
  476. grub_efi_uintn_t desc_size,
  477. grub_efi_memory_descriptor_t *memory_map_end)
  478. {
  479. grub_efi_memory_descriptor_t *desc;
  480. int i;
  481. for (desc = memory_map, i = 0;
  482. desc < memory_map_end;
  483. desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size), i++)
  484. {
  485. grub_printf ("MD: t=%x, p=%llx, v=%llx, n=%llx, a=%llx\n",
  486. desc->type, desc->physical_start, desc->virtual_start,
  487. desc->num_pages, desc->attribute);
  488. }
  489. }
  490. #endif
  491. static grub_err_t
  492. grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags)
  493. {
  494. grub_efi_memory_descriptor_t *memory_map;
  495. grub_efi_memory_descriptor_t *memory_map_end;
  496. grub_efi_memory_descriptor_t *filtered_memory_map;
  497. grub_efi_memory_descriptor_t *filtered_memory_map_end;
  498. grub_efi_uintn_t alloc_size;
  499. grub_efi_uintn_t map_size;
  500. grub_efi_uintn_t desc_size;
  501. grub_err_t err;
  502. int mm_status;
  503. /* Prepare a memory region to store two memory maps. */
  504. alloc_size = 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE);
  505. memory_map = grub_efi_allocate_any_pages (alloc_size);
  506. if (! memory_map)
  507. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for memory map");
  508. /* Obtain descriptors for available memory. */
  509. map_size = MEMORY_MAP_SIZE;
  510. mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0);
  511. if (mm_status == 0)
  512. {
  513. grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t) memory_map, alloc_size);
  514. /* Freeing/allocating operations may increase memory map size. */
  515. map_size += desc_size * 32;
  516. alloc_size = 2 * BYTES_TO_PAGES (map_size);
  517. memory_map = grub_efi_allocate_any_pages (alloc_size);
  518. if (! memory_map)
  519. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for new memory map");
  520. mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0,
  521. &desc_size, 0);
  522. }
  523. if (mm_status < 0)
  524. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "error fetching memory map from EFI");
  525. memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
  526. filtered_memory_map = memory_map_end;
  527. filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
  528. desc_size, memory_map_end);
  529. /* Sort the filtered descriptors, so that GRUB can allocate pages
  530. from smaller regions. */
  531. sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
  532. /* Allocate memory regions for GRUB's memory management. */
  533. err = add_memory_regions (filtered_memory_map, desc_size,
  534. filtered_memory_map_end,
  535. BYTES_TO_PAGES (required_bytes),
  536. flags);
  537. if (err != GRUB_ERR_NONE)
  538. return err;
  539. #if 0
  540. /* For debug. */
  541. map_size = MEMORY_MAP_SIZE;
  542. if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
  543. grub_fatal ("cannot get memory map");
  544. grub_printf ("printing memory map\n");
  545. print_memory_map (memory_map, desc_size,
  546. NEXT_MEMORY_DESCRIPTOR (memory_map, map_size));
  547. grub_fatal ("Debug. ");
  548. #endif
  549. /* Release the memory maps. */
  550. grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t) memory_map, alloc_size);
  551. return GRUB_ERR_NONE;
  552. }
  553. void
  554. grub_efi_mm_init (void)
  555. {
  556. if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE, GRUB_MM_ADD_REGION_NONE) != GRUB_ERR_NONE)
  557. grub_fatal ("%s", grub_errmsg);
  558. grub_mm_add_region_fn = grub_efi_mm_add_regions;
  559. }
  560. #if defined (__aarch64__) || defined (__arm__) || defined (__riscv) || \
  561. defined (__loongarch__)
  562. grub_err_t
  563. grub_efi_get_ram_base(grub_addr_t *base_addr)
  564. {
  565. grub_efi_memory_descriptor_t *memory_map, *desc;
  566. grub_efi_uintn_t memory_map_size, desc_size;
  567. int ret;
  568. memory_map_size = grub_efi_find_mmap_size();
  569. memory_map = grub_malloc (memory_map_size);
  570. if (! memory_map)
  571. return GRUB_ERR_OUT_OF_MEMORY;
  572. ret = grub_efi_get_memory_map (&memory_map_size, memory_map, NULL,
  573. &desc_size, NULL);
  574. if (ret < 1)
  575. return GRUB_ERR_BUG;
  576. for (desc = memory_map, *base_addr = GRUB_EFI_MAX_USABLE_ADDRESS;
  577. (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size);
  578. desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
  579. if (desc->attribute & GRUB_EFI_MEMORY_WB)
  580. *base_addr = grub_min (*base_addr, desc->physical_start);
  581. grub_free(memory_map);
  582. return GRUB_ERR_NONE;
  583. }
  584. #endif
  585. static grub_uint64_t
  586. grub_mem_attrs_to_uefi_mem_attrs (grub_mem_attr_t attrs)
  587. {
  588. grub_efi_uint64_t ret = GRUB_EFI_MEMORY_RP | GRUB_EFI_MEMORY_RO | GRUB_EFI_MEMORY_XP;
  589. if (attrs & GRUB_MEM_ATTR_R)
  590. ret &= ~GRUB_EFI_MEMORY_RP;
  591. if (attrs & GRUB_MEM_ATTR_W)
  592. ret &= ~GRUB_EFI_MEMORY_RO;
  593. if (attrs & GRUB_MEM_ATTR_X)
  594. ret &= ~GRUB_EFI_MEMORY_XP;
  595. return ret;
  596. }
  597. static grub_mem_attr_t
  598. uefi_mem_attrs_to_grub_mem_attrs (grub_efi_uint64_t attrs)
  599. {
  600. grub_mem_attr_t ret = GRUB_MEM_ATTR_R | GRUB_MEM_ATTR_W | GRUB_MEM_ATTR_X;
  601. if (attrs & GRUB_EFI_MEMORY_RP)
  602. ret &= ~GRUB_MEM_ATTR_R;
  603. if (attrs & GRUB_EFI_MEMORY_RO)
  604. ret &= ~GRUB_MEM_ATTR_W;
  605. if (attrs & GRUB_EFI_MEMORY_XP)
  606. ret &= ~GRUB_MEM_ATTR_X;
  607. return ret;
  608. }
  609. grub_err_t
  610. grub_get_mem_attrs (grub_addr_t addr, grub_size_t size, grub_mem_attr_t *attrs)
  611. {
  612. grub_efi_memory_attribute_protocol_t *proto;
  613. grub_efi_physical_address_t physaddr = addr;
  614. static grub_guid_t protocol_guid = GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
  615. grub_efi_status_t efi_status;
  616. grub_efi_uint64_t efi_attrs;
  617. if (physaddr & (GRUB_EFI_PAGE_SIZE - 1) || size & (GRUB_EFI_PAGE_SIZE - 1) || size == 0 || attrs == NULL)
  618. return grub_error (GRUB_ERR_BAD_ARGUMENT, "%s() called with invalid arguments", __FUNCTION__);
  619. proto = grub_efi_locate_protocol (&protocol_guid, 0);
  620. if (proto == NULL)
  621. {
  622. /* No protocol -> do nothing, all memory is RWX in boot services */
  623. *attrs = GRUB_MEM_ATTR_R | GRUB_MEM_ATTR_W | GRUB_MEM_ATTR_X;
  624. return GRUB_ERR_NONE;
  625. }
  626. efi_status = proto->get_memory_attributes (proto, physaddr, size, &efi_attrs);
  627. if (efi_status != GRUB_EFI_SUCCESS)
  628. return grub_error (GRUB_ERR_BAD_ARGUMENT, "%s() called with invalid arguments", __FUNCTION__);
  629. *attrs = uefi_mem_attrs_to_grub_mem_attrs (efi_attrs);
  630. grub_dprintf ("nx", "get 0x%" PRIxGRUB_ADDR "-0x%" PRIxGRUB_ADDR ":%c%c%c\n",
  631. addr, addr + size - 1,
  632. (*attrs & GRUB_MEM_ATTR_R) ? 'r' : '-',
  633. (*attrs & GRUB_MEM_ATTR_W) ? 'w' : '-',
  634. (*attrs & GRUB_MEM_ATTR_X) ? 'x' : '-');
  635. return GRUB_ERR_NONE;
  636. }
  637. grub_err_t
  638. grub_update_mem_attrs (grub_addr_t addr, grub_size_t size,
  639. grub_mem_attr_t set_attrs, grub_mem_attr_t clear_attrs)
  640. {
  641. grub_efi_memory_attribute_protocol_t *proto;
  642. grub_efi_physical_address_t physaddr = addr;
  643. static grub_guid_t protocol_guid = GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
  644. grub_efi_status_t efi_status = GRUB_EFI_SUCCESS;
  645. grub_efi_uint64_t uefi_set_attrs, uefi_clear_attrs;
  646. if (physaddr & (GRUB_EFI_PAGE_SIZE - 1) || size & (GRUB_EFI_PAGE_SIZE - 1) || size == 0)
  647. return grub_error (GRUB_ERR_BAD_ARGUMENT, "%s() called with invalid arguments", __FUNCTION__);
  648. proto = grub_efi_locate_protocol (&protocol_guid, 0);
  649. if (proto == NULL)
  650. /* No protocol -> do nothing, all memory is RWX in boot services */
  651. return GRUB_ERR_NONE;
  652. uefi_set_attrs = grub_mem_attrs_to_uefi_mem_attrs (set_attrs);
  653. uefi_clear_attrs = grub_mem_attrs_to_uefi_mem_attrs (clear_attrs);
  654. if (uefi_set_attrs)
  655. efi_status = proto->set_memory_attributes (proto, physaddr, size, uefi_set_attrs);
  656. if (efi_status == GRUB_EFI_SUCCESS && uefi_clear_attrs)
  657. efi_status = proto->clear_memory_attributes (proto, physaddr, size, uefi_clear_attrs);
  658. if (efi_status != GRUB_EFI_SUCCESS)
  659. return grub_error (GRUB_ERR_BAD_ARGUMENT, "%s() called with invalid arguments", __FUNCTION__);
  660. grub_dprintf ("nx", "set +%s%s%s -%s%s%s on 0x%" PRIxGRUB_ADDR "-0x%" PRIxGRUB_ADDR "\n",
  661. (set_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
  662. (set_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
  663. (set_attrs & GRUB_MEM_ATTR_X) ? "x" : "",
  664. (clear_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
  665. (clear_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
  666. (clear_attrs & GRUB_MEM_ATTR_X) ? "x" : "",
  667. addr, addr + size - 1);
  668. return GRUB_ERR_NONE;
  669. }