randomalloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
  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. u64 alloc_min, u64 alloc_max)
  18. {
  19. unsigned long align = 1UL << align_shift;
  20. u64 first_slot, last_slot, region_end;
  21. if (md->type != EFI_CONVENTIONAL_MEMORY)
  22. return 0;
  23. if (md->attribute & EFI_MEMORY_HOT_PLUGGABLE)
  24. return 0;
  25. if (efi_soft_reserve_enabled() &&
  26. (md->attribute & EFI_MEMORY_SP))
  27. return 0;
  28. region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
  29. alloc_max);
  30. if (region_end < size)
  31. return 0;
  32. first_slot = round_up(max(md->phys_addr, alloc_min), align);
  33. last_slot = round_down(region_end - size + 1, align);
  34. if (first_slot > last_slot)
  35. return 0;
  36. return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
  37. }
  38. /*
  39. * The UEFI memory descriptors have a virtual address field that is only used
  40. * when installing the virtual mapping using SetVirtualAddressMap(). Since it
  41. * is unused here, we can reuse it to keep track of each descriptor's slot
  42. * count.
  43. */
  44. #define MD_NUM_SLOTS(md) ((md)->virt_addr)
  45. efi_status_t efi_random_alloc(unsigned long size,
  46. unsigned long align,
  47. unsigned long *addr,
  48. unsigned long random_seed,
  49. int memory_type,
  50. unsigned long alloc_min,
  51. unsigned long alloc_max)
  52. {
  53. unsigned long total_slots = 0, target_slot;
  54. unsigned long total_mirrored_slots = 0;
  55. struct efi_boot_memmap *map;
  56. efi_status_t status;
  57. int map_offset;
  58. status = efi_get_memory_map(&map, false);
  59. if (status != EFI_SUCCESS)
  60. return status;
  61. if (align < EFI_ALLOC_ALIGN)
  62. align = EFI_ALLOC_ALIGN;
  63. /* Avoid address 0x0, as it can be mistaken for NULL */
  64. if (alloc_min == 0)
  65. alloc_min = align;
  66. size = round_up(size, EFI_ALLOC_ALIGN);
  67. /* count the suitable slots in each memory map entry */
  68. for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
  69. efi_memory_desc_t *md = (void *)map->map + map_offset;
  70. unsigned long slots;
  71. slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
  72. alloc_max);
  73. MD_NUM_SLOTS(md) = slots;
  74. total_slots += slots;
  75. if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
  76. total_mirrored_slots += slots;
  77. }
  78. /* consider only mirrored slots for randomization if any exist */
  79. if (total_mirrored_slots > 0)
  80. total_slots = total_mirrored_slots;
  81. /* find a random number between 0 and total_slots */
  82. target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
  83. /*
  84. * target_slot is now a value in the range [0, total_slots), and so
  85. * it corresponds with exactly one of the suitable slots we recorded
  86. * when iterating over the memory map the first time around.
  87. *
  88. * So iterate over the memory map again, subtracting the number of
  89. * slots of each entry at each iteration, until we have found the entry
  90. * that covers our chosen slot. Use the residual value of target_slot
  91. * to calculate the randomly chosen address, and allocate it directly
  92. * using EFI_ALLOCATE_ADDRESS.
  93. */
  94. status = EFI_OUT_OF_RESOURCES;
  95. for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
  96. efi_memory_desc_t *md = (void *)map->map + map_offset;
  97. efi_physical_addr_t target;
  98. unsigned long pages;
  99. if (total_mirrored_slots > 0 &&
  100. !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
  101. continue;
  102. if (target_slot >= MD_NUM_SLOTS(md)) {
  103. target_slot -= MD_NUM_SLOTS(md);
  104. continue;
  105. }
  106. target = round_up(max_t(u64, md->phys_addr, alloc_min), align) + target_slot * align;
  107. pages = size / EFI_PAGE_SIZE;
  108. status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
  109. memory_type, pages, &target);
  110. if (status == EFI_SUCCESS)
  111. *addr = target;
  112. break;
  113. }
  114. efi_bs_call(free_pool, map);
  115. return status;
  116. }