randomalloc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd; <[email protected]>
  4. */
  5. #include <linux/efi.h>
  6. #include <linux/log2.h>
  7. #include <asm/efi.h>
  8. #include "efistub.h"
  9. /*
  10. * Return the number of slots covered by this entry, i.e., the number of
  11. * addresses it covers that are suitably aligned and supply enough room
  12. * for the allocation.
  13. */
  14. static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
  15. unsigned long size,
  16. unsigned long align_shift)
  17. {
  18. unsigned long align = 1UL << align_shift;
  19. u64 first_slot, last_slot, region_end;
  20. if (md->type != EFI_CONVENTIONAL_MEMORY)
  21. return 0;
  22. if (efi_soft_reserve_enabled() &&
  23. (md->attribute & EFI_MEMORY_SP))
  24. return 0;
  25. region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
  26. (u64)ULONG_MAX);
  27. if (region_end < size)
  28. return 0;
  29. first_slot = round_up(md->phys_addr, align);
  30. last_slot = round_down(region_end - size + 1, align);
  31. if (first_slot > last_slot)
  32. return 0;
  33. return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
  34. }
  35. /*
  36. * The UEFI memory descriptors have a virtual address field that is only used
  37. * when installing the virtual mapping using SetVirtualAddressMap(). Since it
  38. * is unused here, we can reuse it to keep track of each descriptor's slot
  39. * count.
  40. */
  41. #define MD_NUM_SLOTS(md) ((md)->virt_addr)
  42. efi_status_t efi_random_alloc(unsigned long size,
  43. unsigned long align,
  44. unsigned long *addr,
  45. unsigned long random_seed)
  46. {
  47. unsigned long total_slots = 0, target_slot;
  48. unsigned long total_mirrored_slots = 0;
  49. struct efi_boot_memmap *map;
  50. efi_status_t status;
  51. int map_offset;
  52. status = efi_get_memory_map(&map, false);
  53. if (status != EFI_SUCCESS)
  54. return status;
  55. if (align < EFI_ALLOC_ALIGN)
  56. align = EFI_ALLOC_ALIGN;
  57. size = round_up(size, EFI_ALLOC_ALIGN);
  58. /* count the suitable slots in each memory map entry */
  59. for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
  60. efi_memory_desc_t *md = (void *)map->map + map_offset;
  61. unsigned long slots;
  62. slots = get_entry_num_slots(md, size, ilog2(align));
  63. MD_NUM_SLOTS(md) = slots;
  64. total_slots += slots;
  65. if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
  66. total_mirrored_slots += slots;
  67. }
  68. /* consider only mirrored slots for randomization if any exist */
  69. if (total_mirrored_slots > 0)
  70. total_slots = total_mirrored_slots;
  71. /* find a random number between 0 and total_slots */
  72. target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
  73. /*
  74. * target_slot is now a value in the range [0, total_slots), and so
  75. * it corresponds with exactly one of the suitable slots we recorded
  76. * when iterating over the memory map the first time around.
  77. *
  78. * So iterate over the memory map again, subtracting the number of
  79. * slots of each entry at each iteration, until we have found the entry
  80. * that covers our chosen slot. Use the residual value of target_slot
  81. * to calculate the randomly chosen address, and allocate it directly
  82. * using EFI_ALLOCATE_ADDRESS.
  83. */
  84. for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
  85. efi_memory_desc_t *md = (void *)map->map + map_offset;
  86. efi_physical_addr_t target;
  87. unsigned long pages;
  88. if (total_mirrored_slots > 0 &&
  89. !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
  90. continue;
  91. if (target_slot >= MD_NUM_SLOTS(md)) {
  92. target_slot -= MD_NUM_SLOTS(md);
  93. continue;
  94. }
  95. target = round_up(md->phys_addr, align) + target_slot * align;
  96. pages = size / EFI_PAGE_SIZE;
  97. status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
  98. EFI_LOADER_DATA, pages, &target);
  99. if (status == EFI_SUCCESS)
  100. *addr = target;
  101. break;
  102. }
  103. efi_bs_call(free_pool, map);
  104. return status;
  105. }