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