18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/efi.h> 78c2ecf20Sopenharmony_ci#include <linux/log2.h> 88c2ecf20Sopenharmony_ci#include <asm/efi.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include "efistub.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/* 138c2ecf20Sopenharmony_ci * Return the number of slots covered by this entry, i.e., the number of 148c2ecf20Sopenharmony_ci * addresses it covers that are suitably aligned and supply enough room 158c2ecf20Sopenharmony_ci * for the allocation. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_cistatic unsigned long get_entry_num_slots(efi_memory_desc_t *md, 188c2ecf20Sopenharmony_ci unsigned long size, 198c2ecf20Sopenharmony_ci unsigned long align_shift) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci unsigned long align = 1UL << align_shift; 228c2ecf20Sopenharmony_ci u64 first_slot, last_slot, region_end; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci if (md->type != EFI_CONVENTIONAL_MEMORY) 258c2ecf20Sopenharmony_ci return 0; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci if (efi_soft_reserve_enabled() && 288c2ecf20Sopenharmony_ci (md->attribute & EFI_MEMORY_SP)) 298c2ecf20Sopenharmony_ci return 0; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, 328c2ecf20Sopenharmony_ci (u64)ULONG_MAX); 338c2ecf20Sopenharmony_ci if (region_end < size) 348c2ecf20Sopenharmony_ci return 0; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci first_slot = round_up(md->phys_addr, align); 378c2ecf20Sopenharmony_ci last_slot = round_down(region_end - size + 1, align); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci if (first_slot > last_slot) 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1; 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* 468c2ecf20Sopenharmony_ci * The UEFI memory descriptors have a virtual address field that is only used 478c2ecf20Sopenharmony_ci * when installing the virtual mapping using SetVirtualAddressMap(). Since it 488c2ecf20Sopenharmony_ci * is unused here, we can reuse it to keep track of each descriptor's slot 498c2ecf20Sopenharmony_ci * count. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_ci#define MD_NUM_SLOTS(md) ((md)->virt_addr) 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciefi_status_t efi_random_alloc(unsigned long size, 548c2ecf20Sopenharmony_ci unsigned long align, 558c2ecf20Sopenharmony_ci unsigned long *addr, 568c2ecf20Sopenharmony_ci unsigned long random_seed) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci unsigned long total_slots = 0, target_slot; 598c2ecf20Sopenharmony_ci struct efi_boot_memmap *map; 608c2ecf20Sopenharmony_ci efi_status_t status; 618c2ecf20Sopenharmony_ci int map_offset; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci status = efi_get_memory_map(&map, false); 648c2ecf20Sopenharmony_ci if (status != EFI_SUCCESS) 658c2ecf20Sopenharmony_ci return status; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (align < EFI_ALLOC_ALIGN) 688c2ecf20Sopenharmony_ci align = EFI_ALLOC_ALIGN; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci size = round_up(size, EFI_ALLOC_ALIGN); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* count the suitable slots in each memory map entry */ 738c2ecf20Sopenharmony_ci for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) { 748c2ecf20Sopenharmony_ci efi_memory_desc_t *md = (void *)map->map + map_offset; 758c2ecf20Sopenharmony_ci unsigned long slots; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci slots = get_entry_num_slots(md, size, ilog2(align)); 788c2ecf20Sopenharmony_ci MD_NUM_SLOTS(md) = slots; 798c2ecf20Sopenharmony_ci total_slots += slots; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci /* find a random number between 0 and total_slots */ 838c2ecf20Sopenharmony_ci target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci /* 868c2ecf20Sopenharmony_ci * target_slot is now a value in the range [0, total_slots), and so 878c2ecf20Sopenharmony_ci * it corresponds with exactly one of the suitable slots we recorded 888c2ecf20Sopenharmony_ci * when iterating over the memory map the first time around. 898c2ecf20Sopenharmony_ci * 908c2ecf20Sopenharmony_ci * So iterate over the memory map again, subtracting the number of 918c2ecf20Sopenharmony_ci * slots of each entry at each iteration, until we have found the entry 928c2ecf20Sopenharmony_ci * that covers our chosen slot. Use the residual value of target_slot 938c2ecf20Sopenharmony_ci * to calculate the randomly chosen address, and allocate it directly 948c2ecf20Sopenharmony_ci * using EFI_ALLOCATE_ADDRESS. 958c2ecf20Sopenharmony_ci */ 968c2ecf20Sopenharmony_ci for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) { 978c2ecf20Sopenharmony_ci efi_memory_desc_t *md = (void *)map->map + map_offset; 988c2ecf20Sopenharmony_ci efi_physical_addr_t target; 998c2ecf20Sopenharmony_ci unsigned long pages; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci if (target_slot >= MD_NUM_SLOTS(md)) { 1028c2ecf20Sopenharmony_ci target_slot -= MD_NUM_SLOTS(md); 1038c2ecf20Sopenharmony_ci continue; 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci target = round_up(md->phys_addr, align) + target_slot * align; 1078c2ecf20Sopenharmony_ci pages = size / EFI_PAGE_SIZE; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, 1108c2ecf20Sopenharmony_ci EFI_LOADER_DATA, pages, &target); 1118c2ecf20Sopenharmony_ci if (status == EFI_SUCCESS) 1128c2ecf20Sopenharmony_ci *addr = target; 1138c2ecf20Sopenharmony_ci break; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci efi_bs_call(free_pool, map); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci return status; 1198c2ecf20Sopenharmony_ci} 120