mem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <dragonstub/dragonstub.h>
  2. #include <dragonstub/linux/math.h>
  3. #include <dragonstub/linux/align.h>
  4. #include <dragonstub/minmax.h>
  5. /**
  6. * efi_get_memory_map() - get memory map
  7. * @map: pointer to memory map pointer to which to assign the
  8. * newly allocated memory map
  9. * @install_cfg_tbl: whether or not to install the boot memory map as a
  10. * configuration table
  11. *
  12. * Retrieve the UEFI memory map. The allocated memory leaves room for
  13. * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
  14. *
  15. * Return: status code
  16. */
  17. efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
  18. bool install_cfg_tbl)
  19. {
  20. int memtype = install_cfg_tbl ? EfiACPIReclaimMemory : EfiLoaderData;
  21. efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
  22. struct efi_boot_memmap *m, tmp;
  23. efi_status_t status;
  24. unsigned long size;
  25. tmp.map_size = 0;
  26. status = efi_bs_call(GetMemoryMap, &tmp.map_size, NULL, &tmp.map_key,
  27. &tmp.desc_size, &tmp.desc_ver);
  28. if (status != EFI_BUFFER_TOO_SMALL)
  29. return EFI_LOAD_ERROR;
  30. size = tmp.map_size + tmp.desc_size * EFI_MMAP_NR_SLACK_SLOTS;
  31. status = efi_bs_call(AllocatePool, memtype, sizeof(*m) + size,
  32. (void **)&m);
  33. if (status != EFI_SUCCESS)
  34. return status;
  35. if (install_cfg_tbl) {
  36. /*
  37. * Installing a configuration table might allocate memory, and
  38. * this may modify the memory map. This means we should install
  39. * the configuration table first, and re-install or delete it
  40. * as needed.
  41. */
  42. status = efi_bs_call(InstallConfigurationTable, &tbl_guid, m);
  43. if (status != EFI_SUCCESS)
  44. goto free_map;
  45. }
  46. m->buff_size = m->map_size = size;
  47. status = efi_bs_call(GetMemoryMap, &m->map_size, m->map, &m->map_key,
  48. &m->desc_size, &m->desc_ver);
  49. if (status != EFI_SUCCESS)
  50. goto uninstall_table;
  51. *map = m;
  52. return EFI_SUCCESS;
  53. uninstall_table:
  54. if (install_cfg_tbl)
  55. efi_bs_call(InstallConfigurationTable, &tbl_guid, NULL);
  56. free_map:
  57. efi_bs_call(FreePool, m);
  58. return status;
  59. }
  60. /**
  61. * efi_allocate_pages() - Allocate memory pages
  62. * @size: minimum number of bytes to allocate
  63. * @addr: On return the address of the first allocated page. The first
  64. * allocated page has alignment EFI_ALLOC_ALIGN which is an
  65. * architecture dependent multiple of the page size.
  66. * @max: the address that the last allocated memory page shall not
  67. * exceed
  68. *
  69. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  70. * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
  71. * given by @max.
  72. *
  73. * Return: status code
  74. */
  75. efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
  76. unsigned long max)
  77. {
  78. efi_physical_addr_t alloc_addr;
  79. efi_status_t status;
  80. max = min(max, EFI_ALLOC_LIMIT);
  81. // if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
  82. // return efi_allocate_pages_aligned(
  83. // size, addr, max, EFI_ALLOC_ALIGN, EfiLoaderData);
  84. alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
  85. status = efi_bs_call(AllocatePages, EFI_ALLOCATE_MAX_ADDRESS,
  86. EfiLoaderData, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
  87. &alloc_addr);
  88. if (status != EFI_SUCCESS)
  89. return status;
  90. *addr = alloc_addr;
  91. return EFI_SUCCESS;
  92. }
  93. /**
  94. * efi_allocate_pages_exact() - Allocate memory pages at a specific address
  95. * @size: minimum number of bytes to allocate
  96. * @addr: The address of the first allocated page.
  97. *
  98. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  99. * to EFI_ALLOC_ALIGN.
  100. *
  101. * Return: status code
  102. */
  103. efi_status_t efi_allocate_pages_exact(unsigned long size, unsigned long addr)
  104. {
  105. efi_status_t status;
  106. u64 addr_rounded = addr & ~(EFI_ALLOC_ALIGN - 1);
  107. size += addr - addr_rounded;
  108. u32 pagecount = DIV_ROUND_UP(size, EFI_PAGE_SIZE);
  109. efi_debug(
  110. "efi_allocate_pages_exact: size=%d, addr=%p, addr_rounded=%p, pagecount=%d\n",
  111. size, addr, addr_rounded, pagecount);
  112. status = efi_bs_call(AllocatePages, AllocateAddress, EfiLoaderData,
  113. pagecount, (EFI_PHYSICAL_ADDRESS *)&addr);
  114. // status = efi_bs_call(AllocatePages, AllocateAddress, EfiLoaderData,
  115. // DIV_ROUND_UP(size, EFI_PAGE_SIZE),
  116. // (EFI_PHYSICAL_ADDRESS *)&addr);
  117. if (status != EFI_SUCCESS)
  118. return status;
  119. return EFI_SUCCESS;
  120. }
  121. /**
  122. * efi_free() - free memory pages
  123. * @size: size of the memory area to free in bytes
  124. * @addr: start of the memory area to free (must be EFI_PAGE_SIZE
  125. * aligned)
  126. *
  127. * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
  128. * architecture specific multiple of EFI_PAGE_SIZE. So this function should
  129. * only be used to return pages allocated with efi_allocate_pages() or
  130. * efi_low_alloc_above().
  131. */
  132. void efi_free(unsigned long size, unsigned long addr)
  133. {
  134. unsigned long nr_pages;
  135. if (!size)
  136. return;
  137. nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
  138. efi_bs_call(FreePages, addr, nr_pages);
  139. }