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