mm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* Memory management for efiemu */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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. /*
  20. To keep efiemu runtime contiguous this mm is special.
  21. It uses deferred allocation.
  22. In the first stage you may request memory with grub_efiemu_request_memalign
  23. It will give you a handle with which in the second phase you can access your
  24. memory with grub_efiemu_mm_obtain_request (handle). It's guaranteed that
  25. subsequent calls with the same handle return the same result. You can't request any additional memory once you're in the second phase
  26. */
  27. #include <grub/err.h>
  28. #include <grub/normal.h>
  29. #include <grub/mm.h>
  30. #include <grub/misc.h>
  31. #include <grub/machine/memory.h>
  32. #include <grub/efiemu/efiemu.h>
  33. GRUB_EXPORT(grub_efiemu_get_memory_map);
  34. struct grub_efiemu_memrequest
  35. {
  36. struct grub_efiemu_memrequest *next;
  37. grub_efi_memory_type_t type;
  38. grub_size_t size;
  39. grub_size_t align_overhead;
  40. int handle;
  41. void *val;
  42. };
  43. /* Linked list of requested memory. */
  44. static struct grub_efiemu_memrequest *memrequests = 0;
  45. /* Memory map. */
  46. static grub_efi_memory_descriptor_t *efiemu_mmap = 0;
  47. /* Pointer to allocated memory */
  48. static void *resident_memory = 0;
  49. /* Size of requested memory per type */
  50. static grub_size_t requested_memory[GRUB_EFI_MAX_MEMORY_TYPE];
  51. /* How many slots is allocated for memory_map and how many are already used */
  52. static int mmap_reserved_size = 0, mmap_num = 0;
  53. /* Add a memory region to map*/
  54. static grub_err_t
  55. grub_efiemu_add_to_mmap (grub_uint64_t start, grub_uint64_t size,
  56. grub_efi_memory_type_t type)
  57. {
  58. grub_uint64_t page_start, npages;
  59. /* Extend map if necessary*/
  60. if (mmap_num >= mmap_reserved_size)
  61. {
  62. efiemu_mmap = (grub_efi_memory_descriptor_t *)
  63. grub_realloc (efiemu_mmap, (++mmap_reserved_size)
  64. * sizeof (grub_efi_memory_descriptor_t));
  65. if (!efiemu_mmap)
  66. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  67. "not enough space for memory map");
  68. }
  69. /* Fill slot*/
  70. page_start = start - (start % GRUB_EFIEMU_PAGESIZE);
  71. npages = (size + (start % GRUB_EFIEMU_PAGESIZE) + GRUB_EFIEMU_PAGESIZE - 1)
  72. / GRUB_EFIEMU_PAGESIZE;
  73. efiemu_mmap[mmap_num].physical_start = page_start;
  74. efiemu_mmap[mmap_num].virtual_start = page_start;
  75. efiemu_mmap[mmap_num].num_pages = npages;
  76. efiemu_mmap[mmap_num].type = type;
  77. mmap_num++;
  78. return GRUB_ERR_NONE;
  79. }
  80. /* Request a resident memory of type TYPE of size SIZE aligned at ALIGN
  81. ALIGN must be a divisor of page size (if it's a divisor of 4096
  82. it should be ok on all platforms)
  83. */
  84. int
  85. grub_efiemu_request_memalign (grub_size_t align, grub_size_t size,
  86. grub_efi_memory_type_t type)
  87. {
  88. grub_size_t align_overhead;
  89. struct grub_efiemu_memrequest *ret, *cur, *prev;
  90. /* Check that the request is correct */
  91. if (type >= GRUB_EFI_MAX_MEMORY_TYPE || type <= GRUB_EFI_LOADER_CODE)
  92. return -2;
  93. /* Add new size to requested size */
  94. align_overhead = align - (requested_memory[type]%align);
  95. if (align_overhead == align)
  96. align_overhead = 0;
  97. requested_memory[type] += align_overhead + size;
  98. /* Remember the request */
  99. ret = grub_zalloc (sizeof (*ret));
  100. if (!ret)
  101. return -1;
  102. ret->type = type;
  103. ret->size = size;
  104. ret->align_overhead = align_overhead;
  105. prev = 0;
  106. /* Add request to the end of the chain.
  107. It should be at the end because otherwise alignment isn't guaranteed */
  108. for (cur = memrequests; cur; prev = cur, cur = cur->next);
  109. if (prev)
  110. {
  111. ret->handle = prev->handle + 1;
  112. prev->next = ret;
  113. }
  114. else
  115. {
  116. ret->handle = 1; /* Avoid 0 handle*/
  117. memrequests = ret;
  118. }
  119. return ret->handle;
  120. }
  121. /* Really allocate the memory */
  122. static grub_err_t
  123. efiemu_alloc_requests (void)
  124. {
  125. grub_size_t align_overhead = 0;
  126. grub_uint8_t *curptr, *typestart;
  127. struct grub_efiemu_memrequest *cur;
  128. grub_size_t total_alloc = 0;
  129. unsigned i;
  130. /* Order of memory regions */
  131. grub_efi_memory_type_t reqorder[] =
  132. {
  133. /* First come regions usable by OS*/
  134. GRUB_EFI_LOADER_CODE,
  135. GRUB_EFI_LOADER_DATA,
  136. GRUB_EFI_BOOT_SERVICES_CODE,
  137. GRUB_EFI_BOOT_SERVICES_DATA,
  138. GRUB_EFI_CONVENTIONAL_MEMORY,
  139. GRUB_EFI_ACPI_RECLAIM_MEMORY,
  140. /* Then memory used by runtime */
  141. /* This way all our regions are in a single block */
  142. GRUB_EFI_RUNTIME_SERVICES_CODE,
  143. GRUB_EFI_RUNTIME_SERVICES_DATA,
  144. GRUB_EFI_ACPI_MEMORY_NVS,
  145. /* And then unavailable memory types. This is more for a completeness.
  146. You should double think before allocating memory of any of these types
  147. */
  148. GRUB_EFI_UNUSABLE_MEMORY,
  149. GRUB_EFI_MEMORY_MAPPED_IO,
  150. GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE,
  151. GRUB_EFI_PAL_CODE
  152. };
  153. /* Compute total memory needed */
  154. for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
  155. {
  156. align_overhead = GRUB_EFIEMU_PAGESIZE
  157. - (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
  158. if (align_overhead == GRUB_EFIEMU_PAGESIZE)
  159. align_overhead = 0;
  160. total_alloc += requested_memory[reqorder[i]] + align_overhead;
  161. }
  162. /* Allocate the whole memory in one block */
  163. resident_memory = grub_memalign (GRUB_EFIEMU_PAGESIZE, total_alloc);
  164. if (!resident_memory)
  165. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  166. "couldn't allocate resident memory");
  167. /* Split the memory into blocks by type */
  168. curptr = resident_memory;
  169. for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
  170. {
  171. if (!requested_memory[reqorder[i]])
  172. continue;
  173. typestart = curptr;
  174. /* Write pointers to requests */
  175. for (cur = memrequests; cur; cur = cur->next)
  176. if (cur->type == reqorder[i])
  177. {
  178. curptr = ((grub_uint8_t *)curptr) + cur->align_overhead;
  179. cur->val = curptr;
  180. curptr = ((grub_uint8_t *)curptr) + cur->size;
  181. }
  182. /* Ensure that the regions are page-aligned */
  183. align_overhead = GRUB_EFIEMU_PAGESIZE
  184. - (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
  185. if (align_overhead == GRUB_EFIEMU_PAGESIZE)
  186. align_overhead = 0;
  187. curptr = ((grub_uint8_t *)curptr) + align_overhead;
  188. /* Add the region to memory map */
  189. grub_efiemu_add_to_mmap (PTR_TO_UINT64 (typestart),
  190. curptr - typestart, reqorder[i]);
  191. }
  192. return GRUB_ERR_NONE;
  193. }
  194. /* Get a pointer to requested memory from handle */
  195. void *
  196. grub_efiemu_mm_obtain_request (int handle)
  197. {
  198. struct grub_efiemu_memrequest *cur;
  199. for (cur = memrequests; cur; cur = cur->next)
  200. if (cur->handle == handle)
  201. return cur->val;
  202. return 0;
  203. }
  204. /* Get type of requested memory by handle */
  205. grub_efi_memory_type_t
  206. grub_efiemu_mm_get_type (int handle)
  207. {
  208. struct grub_efiemu_memrequest *cur;
  209. for (cur = memrequests; cur; cur = cur->next)
  210. if (cur->handle == handle)
  211. return cur->type;
  212. return 0;
  213. }
  214. /* Free a request */
  215. void
  216. grub_efiemu_mm_return_request (int handle)
  217. {
  218. struct grub_efiemu_memrequest *cur, *prev;
  219. /* Remove head if necessary */
  220. while (memrequests && memrequests->handle == handle)
  221. {
  222. cur = memrequests->next;
  223. grub_free (memrequests);
  224. memrequests = cur;
  225. }
  226. if (!memrequests)
  227. return;
  228. /* Remove request from a middle of chain*/
  229. for (prev = memrequests, cur = prev->next; cur;)
  230. if (cur->handle == handle)
  231. {
  232. prev->next = cur->next;
  233. grub_free (cur);
  234. cur = prev->next;
  235. }
  236. else
  237. {
  238. prev = cur;
  239. cur = prev->next;
  240. }
  241. }
  242. #ifndef GRUB_MACHINE_EMU
  243. static int
  244. bounds_hook (grub_uint64_t addr __attribute__ ((unused)),
  245. grub_uint64_t size __attribute__ ((unused)),
  246. grub_uint32_t type __attribute__ ((unused)),
  247. void *closure __attribute__ ((unused)))
  248. {
  249. mmap_reserved_size++;
  250. return 0;
  251. }
  252. #endif
  253. /* Reserve space for memory map */
  254. static grub_err_t
  255. grub_efiemu_mmap_init (void)
  256. {
  257. // the place for memory used by efiemu itself
  258. mmap_reserved_size = GRUB_EFI_MAX_MEMORY_TYPE + 1;
  259. #ifndef GRUB_MACHINE_EMU
  260. grub_machine_mmap_iterate (bounds_hook, 0);
  261. #endif
  262. return GRUB_ERR_NONE;
  263. }
  264. /* This is a drop-in replacement of grub_efi_get_memory_map */
  265. /* Get the memory map as defined in the EFI spec. Return 1 if successful,
  266. return 0 if partial, or return -1 if an error occurs. */
  267. int
  268. grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size,
  269. grub_efi_memory_descriptor_t *memory_map,
  270. grub_efi_uintn_t *map_key,
  271. grub_efi_uintn_t *descriptor_size,
  272. grub_efi_uint32_t *descriptor_version)
  273. {
  274. if (!efiemu_mmap)
  275. {
  276. grub_error (GRUB_ERR_INVALID_COMMAND,
  277. "you need to first launch efiemu_prepare");
  278. return -1;
  279. }
  280. if (*memory_map_size < mmap_num * sizeof (grub_efi_memory_descriptor_t))
  281. {
  282. *memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
  283. return 0;
  284. }
  285. *memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
  286. grub_memcpy (memory_map, efiemu_mmap, *memory_map_size);
  287. if (descriptor_size)
  288. *descriptor_size = sizeof (grub_efi_memory_descriptor_t);
  289. if (descriptor_version)
  290. *descriptor_version = 1;
  291. if (map_key)
  292. *map_key = 0;
  293. return 1;
  294. }
  295. /* Free everything */
  296. grub_err_t
  297. grub_efiemu_mm_unload (void)
  298. {
  299. struct grub_efiemu_memrequest *cur, *d;
  300. for (cur = memrequests; cur;)
  301. {
  302. d = cur->next;
  303. grub_free (cur);
  304. cur = d;
  305. }
  306. memrequests = 0;
  307. grub_memset (&requested_memory, 0, sizeof (requested_memory));
  308. grub_free (resident_memory);
  309. resident_memory = 0;
  310. grub_free (efiemu_mmap);
  311. efiemu_mmap = 0;
  312. mmap_reserved_size = mmap_num = 0;
  313. return GRUB_ERR_NONE;
  314. }
  315. /* This function should be called before doing any requests */
  316. grub_err_t
  317. grub_efiemu_mm_init (void)
  318. {
  319. grub_err_t err;
  320. err = grub_efiemu_mm_unload ();
  321. if (err)
  322. return err;
  323. grub_efiemu_mmap_init ();
  324. return GRUB_ERR_NONE;
  325. }
  326. #ifndef GRUB_UTIL
  327. static int
  328. fill_hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type,
  329. void *closure __attribute__ ((unused)))
  330. {
  331. switch (type)
  332. {
  333. case GRUB_MACHINE_MEMORY_AVAILABLE:
  334. return grub_efiemu_add_to_mmap (addr, size,
  335. GRUB_EFI_CONVENTIONAL_MEMORY);
  336. #ifdef GRUB_MACHINE_MEMORY_ACPI
  337. case GRUB_MACHINE_MEMORY_ACPI:
  338. return grub_efiemu_add_to_mmap (addr, size,
  339. GRUB_EFI_ACPI_RECLAIM_MEMORY);
  340. #endif
  341. #ifdef GRUB_MACHINE_MEMORY_NVS
  342. case GRUB_MACHINE_MEMORY_NVS:
  343. return grub_efiemu_add_to_mmap (addr, size,
  344. GRUB_EFI_ACPI_MEMORY_NVS);
  345. #endif
  346. default:
  347. grub_printf ("Unknown memory type %d. Marking as unusable\n", type);
  348. case GRUB_MACHINE_MEMORY_RESERVED:
  349. return grub_efiemu_add_to_mmap (addr, size,
  350. GRUB_EFI_UNUSABLE_MEMORY);
  351. }
  352. }
  353. #endif
  354. /* Copy host memory map */
  355. static grub_err_t
  356. grub_efiemu_mmap_fill (void)
  357. {
  358. #ifndef GRUB_UTIL
  359. grub_machine_mmap_iterate (fill_hook, 0);
  360. #endif
  361. return GRUB_ERR_NONE;
  362. }
  363. grub_err_t
  364. grub_efiemu_mmap_iterate (int (*hook) (grub_uint64_t, grub_uint64_t,
  365. grub_uint32_t, void *), void *closure)
  366. {
  367. unsigned i;
  368. for (i = 0; i < (unsigned) mmap_num; i++)
  369. switch (efiemu_mmap[i].type)
  370. {
  371. case GRUB_EFI_RUNTIME_SERVICES_CODE:
  372. hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
  373. GRUB_EFIEMU_MEMORY_CODE, closure);
  374. break;
  375. case GRUB_EFI_RESERVED_MEMORY_TYPE:
  376. case GRUB_EFI_RUNTIME_SERVICES_DATA:
  377. case GRUB_EFI_UNUSABLE_MEMORY:
  378. case GRUB_EFI_MEMORY_MAPPED_IO:
  379. case GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  380. case GRUB_EFI_PAL_CODE:
  381. case GRUB_EFI_MAX_MEMORY_TYPE:
  382. hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
  383. GRUB_EFIEMU_MEMORY_RESERVED, closure);
  384. break;
  385. case GRUB_EFI_LOADER_CODE:
  386. case GRUB_EFI_LOADER_DATA:
  387. case GRUB_EFI_BOOT_SERVICES_CODE:
  388. case GRUB_EFI_BOOT_SERVICES_DATA:
  389. case GRUB_EFI_CONVENTIONAL_MEMORY:
  390. hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
  391. GRUB_EFIEMU_MEMORY_AVAILABLE, closure);
  392. break;
  393. case GRUB_EFI_ACPI_RECLAIM_MEMORY:
  394. hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
  395. GRUB_EFIEMU_MEMORY_ACPI, closure);
  396. break;
  397. case GRUB_EFI_ACPI_MEMORY_NVS:
  398. hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
  399. GRUB_EFIEMU_MEMORY_NVS, closure);
  400. break;
  401. }
  402. return 0;
  403. }
  404. /* This function resolves overlapping regions and sorts the memory map
  405. It uses scanline (sweeping) algorithm
  406. */
  407. static grub_err_t
  408. grub_efiemu_mmap_sort_and_uniq (void)
  409. {
  410. /* If same page is used by multiple types it's resolved
  411. according to priority
  412. 0 - free memory
  413. 1 - memory immediately usable after ExitBootServices
  414. 2 - memory usable after loading ACPI tables
  415. 3 - efiemu memory
  416. 4 - unusable memory
  417. */
  418. int priority[GRUB_EFI_MAX_MEMORY_TYPE] =
  419. {
  420. [GRUB_EFI_RESERVED_MEMORY_TYPE] = 4,
  421. [GRUB_EFI_LOADER_CODE] = 1,
  422. [GRUB_EFI_LOADER_DATA] = 1,
  423. [GRUB_EFI_BOOT_SERVICES_CODE] = 1,
  424. [GRUB_EFI_BOOT_SERVICES_DATA] = 1,
  425. [GRUB_EFI_RUNTIME_SERVICES_CODE] = 3,
  426. [GRUB_EFI_RUNTIME_SERVICES_DATA] = 3,
  427. [GRUB_EFI_CONVENTIONAL_MEMORY] = 0,
  428. [GRUB_EFI_UNUSABLE_MEMORY] = 4,
  429. [GRUB_EFI_ACPI_RECLAIM_MEMORY] = 2,
  430. [GRUB_EFI_ACPI_MEMORY_NVS] = 3,
  431. [GRUB_EFI_MEMORY_MAPPED_IO] = 4,
  432. [GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE] = 4,
  433. [GRUB_EFI_PAL_CODE] = 4
  434. };
  435. int i, j, k, done;
  436. /* Scanline events */
  437. struct grub_efiemu_mmap_scan
  438. {
  439. /* At which memory address*/
  440. grub_uint64_t pos;
  441. /* 0 = region starts, 1 = region ends */
  442. int type;
  443. /* Which type of memory region */
  444. grub_efi_memory_type_t memtype;
  445. };
  446. struct grub_efiemu_mmap_scan *scanline_events;
  447. struct grub_efiemu_mmap_scan t;
  448. /* Previous scanline event */
  449. grub_uint64_t lastaddr;
  450. int lasttype;
  451. /* Current scanline event */
  452. int curtype;
  453. /* how many regions of given type overlap at current location */
  454. int present[GRUB_EFI_MAX_MEMORY_TYPE];
  455. /* Here is stored the resulting memory map*/
  456. grub_efi_memory_descriptor_t *result;
  457. /* Initialize variables*/
  458. grub_memset (present, 0, sizeof (int) * GRUB_EFI_MAX_MEMORY_TYPE);
  459. scanline_events = (struct grub_efiemu_mmap_scan *)
  460. grub_malloc (sizeof (struct grub_efiemu_mmap_scan) * 2 * mmap_num);
  461. /* Number of chunks can't increase more than by factor of 2 */
  462. result = (grub_efi_memory_descriptor_t *)
  463. grub_malloc (sizeof (grub_efi_memory_descriptor_t) * 2 * mmap_num);
  464. if (!result || !scanline_events)
  465. {
  466. grub_free (result);
  467. grub_free (scanline_events);
  468. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  469. "couldn't allocate space for new memory map");
  470. }
  471. /* Register scanline events */
  472. for (i = 0; i < mmap_num; i++)
  473. {
  474. scanline_events[2 * i].pos = efiemu_mmap[i].physical_start;
  475. scanline_events[2 * i].type = 0;
  476. scanline_events[2 * i].memtype = efiemu_mmap[i].type;
  477. scanline_events[2 * i + 1].pos = efiemu_mmap[i].physical_start
  478. + efiemu_mmap[i].num_pages * GRUB_EFIEMU_PAGESIZE;
  479. scanline_events[2 * i + 1].type = 1;
  480. scanline_events[2 * i + 1].memtype = efiemu_mmap[i].type;
  481. }
  482. /* Primitive bubble sort. It has complexity O(n^2) but since we're
  483. unlikely to have more than 100 chunks it's probably one of the
  484. fastest for one purpose */
  485. done = 1;
  486. while (done)
  487. {
  488. done = 0;
  489. for (i = 0; i < 2 * mmap_num - 1; i++)
  490. if (scanline_events[i + 1].pos < scanline_events[i].pos)
  491. {
  492. t = scanline_events[i + 1];
  493. scanline_events[i + 1] = scanline_events[i];
  494. scanline_events[i] = t;
  495. done = 1;
  496. }
  497. }
  498. /* Pointer in resulting memory map */
  499. j = 0;
  500. lastaddr = scanline_events[0].pos;
  501. lasttype = scanline_events[0].memtype;
  502. for (i = 0; i < 2 * mmap_num; i++)
  503. {
  504. /* Process event */
  505. if (scanline_events[i].type)
  506. present[scanline_events[i].memtype]--;
  507. else
  508. present[scanline_events[i].memtype]++;
  509. /* Determine current region type */
  510. curtype = -1;
  511. for (k = 0; k < GRUB_EFI_MAX_MEMORY_TYPE; k++)
  512. if (present[k] && (curtype == -1 || priority[k] > priority[curtype]))
  513. curtype = k;
  514. /* Add memory region to resulting map if necessary */
  515. if ((curtype == -1 || curtype != lasttype)
  516. && lastaddr != scanline_events[i].pos
  517. && lasttype != -1)
  518. {
  519. result[j].virtual_start = result[j].physical_start = lastaddr;
  520. result[j].num_pages = (scanline_events[i].pos - lastaddr)
  521. / GRUB_EFIEMU_PAGESIZE;
  522. result[j].type = lasttype;
  523. /* We set runtime attribute on pages we need to be mapped */
  524. result[j].attribute
  525. = (lasttype == GRUB_EFI_RUNTIME_SERVICES_CODE
  526. || lasttype == GRUB_EFI_RUNTIME_SERVICES_DATA)
  527. ? GRUB_EFI_MEMORY_RUNTIME : 0;
  528. grub_dprintf ("efiemu",
  529. "mmap entry: type %d start 0x%llx 0x%llx pages\n",
  530. result[j].type,
  531. result[j].physical_start, result[j].num_pages);
  532. j++;
  533. }
  534. /* Update last values if necessary */
  535. if (curtype == -1 || curtype != lasttype)
  536. {
  537. lasttype = curtype;
  538. lastaddr = scanline_events[i].pos;
  539. }
  540. }
  541. grub_free (scanline_events);
  542. /* Shrink resulting memory map to really used size and replace efiemu_mmap
  543. by new value */
  544. grub_free (efiemu_mmap);
  545. efiemu_mmap = grub_realloc (result, j * sizeof (*result));
  546. return GRUB_ERR_NONE;
  547. }
  548. /* This function is called to switch from first to second phase */
  549. grub_err_t
  550. grub_efiemu_mm_do_alloc (void)
  551. {
  552. grub_err_t err;
  553. /* Preallocate mmap */
  554. efiemu_mmap = (grub_efi_memory_descriptor_t *)
  555. grub_malloc (mmap_reserved_size * sizeof (grub_efi_memory_descriptor_t));
  556. if (!efiemu_mmap)
  557. {
  558. grub_efiemu_unload ();
  559. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't initialize mmap");
  560. }
  561. if ((err = efiemu_alloc_requests ()))
  562. return err;
  563. if ((err = grub_efiemu_mmap_fill ()))
  564. return err;
  565. return grub_efiemu_mmap_sort_and_uniq ();
  566. }