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