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