Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
0004  */
0005 
0006 #include <linux/efi.h>
0007 #include <linux/log2.h>
0008 #include <asm/efi.h>
0009 
0010 #include "efistub.h"
0011 
0012 /*
0013  * Return the number of slots covered by this entry, i.e., the number of
0014  * addresses it covers that are suitably aligned and supply enough room
0015  * for the allocation.
0016  */
0017 static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
0018                      unsigned long size,
0019                      unsigned long align_shift)
0020 {
0021     unsigned long align = 1UL << align_shift;
0022     u64 first_slot, last_slot, region_end;
0023 
0024     if (md->type != EFI_CONVENTIONAL_MEMORY)
0025         return 0;
0026 
0027     if (efi_soft_reserve_enabled() &&
0028         (md->attribute & EFI_MEMORY_SP))
0029         return 0;
0030 
0031     region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
0032              (u64)ULONG_MAX);
0033     if (region_end < size)
0034         return 0;
0035 
0036     first_slot = round_up(md->phys_addr, align);
0037     last_slot = round_down(region_end - size + 1, align);
0038 
0039     if (first_slot > last_slot)
0040         return 0;
0041 
0042     return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
0043 }
0044 
0045 /*
0046  * The UEFI memory descriptors have a virtual address field that is only used
0047  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
0048  * is unused here, we can reuse it to keep track of each descriptor's slot
0049  * count.
0050  */
0051 #define MD_NUM_SLOTS(md)    ((md)->virt_addr)
0052 
0053 efi_status_t efi_random_alloc(unsigned long size,
0054                   unsigned long align,
0055                   unsigned long *addr,
0056                   unsigned long random_seed)
0057 {
0058     unsigned long map_size, desc_size, total_slots = 0, target_slot;
0059     unsigned long total_mirrored_slots = 0;
0060     unsigned long buff_size;
0061     efi_status_t status;
0062     efi_memory_desc_t *memory_map;
0063     int map_offset;
0064     struct efi_boot_memmap map;
0065 
0066     map.map =   &memory_map;
0067     map.map_size =  &map_size;
0068     map.desc_size = &desc_size;
0069     map.desc_ver =  NULL;
0070     map.key_ptr =   NULL;
0071     map.buff_size = &buff_size;
0072 
0073     status = efi_get_memory_map(&map);
0074     if (status != EFI_SUCCESS)
0075         return status;
0076 
0077     if (align < EFI_ALLOC_ALIGN)
0078         align = EFI_ALLOC_ALIGN;
0079 
0080     size = round_up(size, EFI_ALLOC_ALIGN);
0081 
0082     /* count the suitable slots in each memory map entry */
0083     for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
0084         efi_memory_desc_t *md = (void *)memory_map + map_offset;
0085         unsigned long slots;
0086 
0087         slots = get_entry_num_slots(md, size, ilog2(align));
0088         MD_NUM_SLOTS(md) = slots;
0089         total_slots += slots;
0090         if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
0091             total_mirrored_slots += slots;
0092     }
0093 
0094     /* consider only mirrored slots for randomization if any exist */
0095     if (total_mirrored_slots > 0)
0096         total_slots = total_mirrored_slots;
0097 
0098     /* find a random number between 0 and total_slots */
0099     target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
0100 
0101     /*
0102      * target_slot is now a value in the range [0, total_slots), and so
0103      * it corresponds with exactly one of the suitable slots we recorded
0104      * when iterating over the memory map the first time around.
0105      *
0106      * So iterate over the memory map again, subtracting the number of
0107      * slots of each entry at each iteration, until we have found the entry
0108      * that covers our chosen slot. Use the residual value of target_slot
0109      * to calculate the randomly chosen address, and allocate it directly
0110      * using EFI_ALLOCATE_ADDRESS.
0111      */
0112     for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
0113         efi_memory_desc_t *md = (void *)memory_map + map_offset;
0114         efi_physical_addr_t target;
0115         unsigned long pages;
0116 
0117         if (total_mirrored_slots > 0 &&
0118             !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
0119             continue;
0120 
0121         if (target_slot >= MD_NUM_SLOTS(md)) {
0122             target_slot -= MD_NUM_SLOTS(md);
0123             continue;
0124         }
0125 
0126         target = round_up(md->phys_addr, align) + target_slot * align;
0127         pages = size / EFI_PAGE_SIZE;
0128 
0129         status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
0130                      EFI_LOADER_DATA, pages, &target);
0131         if (status == EFI_SUCCESS)
0132             *addr = target;
0133         break;
0134     }
0135 
0136     efi_bs_call(free_pool, memory_map);
0137 
0138     return status;
0139 }