1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Extensible Firmware Interface 4 * 5 * Based on Extensible Firmware Interface Specification version 2.4 6 * 7 * Copyright (C) 2013, 2014 Linaro Ltd. 8 */ 9 10 #include <linux/efi.h> 11 #include <linux/init.h> 12 #include <linux/screen_info.h> 13 14 #include <asm/efi.h> 15 #include <asm/stacktrace.h> 16 17 static bool region_is_misaligned(const efi_memory_desc_t *md) 18 { 19 if (PAGE_SIZE == EFI_PAGE_SIZE) 20 return false; 21 return !PAGE_ALIGNED(md->phys_addr) || 22 !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT); 23 } 24 25 /* 26 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be 27 * executable, everything else can be mapped with the XN bits 28 * set. Also take the new (optional) RO/XP bits into account. 29 */ 30 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md) 31 { 32 u64 attr = md->attribute; 33 u32 type = md->type; 34 35 if (type == EFI_MEMORY_MAPPED_IO) 36 return PROT_DEVICE_nGnRE; 37 38 if (region_is_misaligned(md)) { 39 static bool __initdata code_is_misaligned; 40 41 /* 42 * Regions that are not aligned to the OS page size cannot be 43 * mapped with strict permissions, as those might interfere 44 * with the permissions that are needed by the adjacent 45 * region's mapping. However, if we haven't encountered any 46 * misaligned runtime code regions so far, we can safely use 47 * non-executable permissions for non-code regions. 48 */ 49 code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE); 50 51 return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC) 52 : pgprot_val(PAGE_KERNEL); 53 } 54 55 /* R-- */ 56 if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) == 57 (EFI_MEMORY_XP | EFI_MEMORY_RO)) 58 return pgprot_val(PAGE_KERNEL_RO); 59 60 /* R-X */ 61 if (attr & EFI_MEMORY_RO) 62 return pgprot_val(PAGE_KERNEL_ROX); 63 64 /* RW- */ 65 if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) == 66 EFI_MEMORY_XP) || 67 type != EFI_RUNTIME_SERVICES_CODE) 68 return pgprot_val(PAGE_KERNEL); 69 70 /* RWX */ 71 return pgprot_val(PAGE_KERNEL_EXEC); 72 } 73 74 /* we will fill this structure from the stub, so don't put it in .bss */ 75 struct screen_info screen_info __section(".data"); 76 EXPORT_SYMBOL(screen_info); 77 78 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) 79 { 80 pteval_t prot_val = create_mapping_protection(md); 81 bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE || 82 md->type == EFI_RUNTIME_SERVICES_DATA); 83 84 /* 85 * If this region is not aligned to the page size used by the OS, the 86 * mapping will be rounded outwards, and may end up sharing a page 87 * frame with an adjacent runtime memory region. Given that the page 88 * table descriptor covering the shared page will be rewritten when the 89 * adjacent region gets mapped, we must avoid block mappings here so we 90 * don't have to worry about splitting them when that happens. 91 */ 92 if (region_is_misaligned(md)) 93 page_mappings_only = true; 94 95 create_pgd_mapping(mm, md->phys_addr, md->virt_addr, 96 md->num_pages << EFI_PAGE_SHIFT, 97 __pgprot(prot_val | PTE_NG), page_mappings_only); 98 return 0; 99 } 100 101 struct set_perm_data { 102 const efi_memory_desc_t *md; 103 bool has_bti; 104 }; 105 106 static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data) 107 { 108 struct set_perm_data *spd = data; 109 const efi_memory_desc_t *md = spd->md; 110 pte_t pte = READ_ONCE(*ptep); 111 112 if (md->attribute & EFI_MEMORY_RO) 113 pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); 114 if (md->attribute & EFI_MEMORY_XP) 115 pte = set_pte_bit(pte, __pgprot(PTE_PXN)); 116 else if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && 117 system_supports_bti() && spd->has_bti) 118 pte = set_pte_bit(pte, __pgprot(PTE_GP)); 119 set_pte(ptep, pte); 120 return 0; 121 } 122 123 int __init efi_set_mapping_permissions(struct mm_struct *mm, 124 efi_memory_desc_t *md, 125 bool has_bti) 126 { 127 struct set_perm_data data = { md, has_bti }; 128 129 BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE && 130 md->type != EFI_RUNTIME_SERVICES_DATA); 131 132 if (region_is_misaligned(md)) 133 return 0; 134 135 /* 136 * Calling apply_to_page_range() is only safe on regions that are 137 * guaranteed to be mapped down to pages. Since we are only called 138 * for regions that have been mapped using efi_create_mapping() above 139 * (and this is checked by the generic Memory Attributes table parsing 140 * routines), there is no need to check that again here. 141 */ 142 return apply_to_page_range(mm, md->virt_addr, 143 md->num_pages << EFI_PAGE_SHIFT, 144 set_permissions, &data); 145 } 146 147 /* 148 * UpdateCapsule() depends on the system being shutdown via 149 * ResetSystem(). 150 */ 151 bool efi_poweroff_required(void) 152 { 153 return efi_enabled(EFI_RUNTIME_SERVICES); 154 } 155 156 asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f) 157 { 158 pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f); 159 return s; 160 } 161 162 static DEFINE_RAW_SPINLOCK(efi_rt_lock); 163 164 void arch_efi_call_virt_setup(void) 165 { 166 efi_virtmap_load(); 167 __efi_fpsimd_begin(); 168 raw_spin_lock(&efi_rt_lock); 169 } 170 171 void arch_efi_call_virt_teardown(void) 172 { 173 raw_spin_unlock(&efi_rt_lock); 174 __efi_fpsimd_end(); 175 efi_virtmap_unload(); 176 } 177 178 asmlinkage u64 *efi_rt_stack_top __ro_after_init; 179 180 asmlinkage efi_status_t __efi_rt_asm_recover(void); 181 182 bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg) 183 { 184 /* Check whether the exception occurred while running the firmware */ 185 if (!current_in_efi() || regs->pc >= TASK_SIZE_64) 186 return false; 187 188 pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg); 189 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); 190 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 191 192 regs->regs[0] = EFI_ABORTED; 193 regs->regs[30] = efi_rt_stack_top[-1]; 194 regs->pc = (u64)__efi_rt_asm_recover; 195 196 if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) 197 regs->regs[18] = efi_rt_stack_top[-2]; 198 199 return true; 200 } 201 202 /* EFI requires 8 KiB of stack space for runtime services */ 203 static_assert(THREAD_SIZE >= SZ_8K); 204 205 static int __init arm64_efi_rt_init(void) 206 { 207 void *p; 208 209 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 210 return 0; 211 212 p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL, 213 NUMA_NO_NODE, &&l); 214 l: if (!p) { 215 pr_warn("Failed to allocate EFI runtime stack\n"); 216 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 217 return -ENOMEM; 218 } 219 220 efi_rt_stack_top = p + THREAD_SIZE; 221 return 0; 222 } 223 core_initcall(arm64_efi_rt_init); 224