1 // SPDX-License-Identifier: GPL-2.0-only
3 * Extensible Firmware Interface
5 * Based on Extensible Firmware Interface Specification version 2.4
7 * Copyright (C) 2013, 2014 Linaro Ltd.
10 #include <linux/efi.h>
11 #include <linux/init.h>
12 #include <linux/kmemleak.h>
13 #include <linux/screen_info.h>
14 #include <linux/vmalloc.h>
17 #include <asm/stacktrace.h>
18 #include <asm/vmap_stack.h>
20 static bool region_is_misaligned(const efi_memory_desc_t *md)
22 if (PAGE_SIZE == EFI_PAGE_SIZE)
24 return !PAGE_ALIGNED(md->phys_addr) ||
25 !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
29 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
30 * executable, everything else can be mapped with the XN bits
31 * set. Also take the new (optional) RO/XP bits into account.
33 static __init ptdesc_t create_mapping_protection(efi_memory_desc_t *md)
35 u64 attr = md->attribute;
38 if (type == EFI_MEMORY_MAPPED_IO) {
39 pgprot_t prot = __pgprot(PROT_DEVICE_nGnRE);
41 if (arm64_is_protected_mmio(md->phys_addr,
42 md->num_pages << EFI_PAGE_SHIFT))
43 prot = pgprot_encrypted(prot);
45 prot = pgprot_decrypted(prot);
46 return pgprot_val(prot);
49 if (region_is_misaligned(md)) {
50 static bool __initdata code_is_misaligned;
53 * Regions that are not aligned to the OS page size cannot be
54 * mapped with strict permissions, as those might interfere
55 * with the permissions that are needed by the adjacent
56 * region's mapping. However, if we haven't encountered any
57 * misaligned runtime code regions so far, we can safely use
58 * non-executable permissions for non-code regions.
60 code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
62 return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
63 : pgprot_val(PAGE_KERNEL);
67 if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
68 (EFI_MEMORY_XP | EFI_MEMORY_RO))
69 return pgprot_val(PAGE_KERNEL_RO);
72 if (attr & EFI_MEMORY_RO)
73 return pgprot_val(PAGE_KERNEL_ROX);
76 if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
78 type != EFI_RUNTIME_SERVICES_CODE)
79 return pgprot_val(PAGE_KERNEL);
82 return pgprot_val(PAGE_KERNEL_EXEC);
85 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
87 ptdesc_t prot_val = create_mapping_protection(md);
88 bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
89 md->type == EFI_RUNTIME_SERVICES_DATA);
92 * If this region is not aligned to the page size used by the OS, the
93 * mapping will be rounded outwards, and may end up sharing a page
94 * frame with an adjacent runtime memory region. Given that the page
95 * table descriptor covering the shared page will be rewritten when the
96 * adjacent region gets mapped, we must avoid block mappings here so we
97 * don't have to worry about splitting them when that happens.
99 if (region_is_misaligned(md))
100 page_mappings_only = true;
102 create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
103 md->num_pages << EFI_PAGE_SHIFT,
104 __pgprot(prot_val | PTE_NG), page_mappings_only);
108 struct set_perm_data {
109 const efi_memory_desc_t *md;
113 static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
115 struct set_perm_data *spd = data;
116 const efi_memory_desc_t *md = spd->md;
117 pte_t pte = __ptep_get(ptep);
119 if (md->attribute & EFI_MEMORY_RO)
120 pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
121 if (md->attribute & EFI_MEMORY_XP)
122 pte = set_pte_bit(pte, __pgprot(PTE_PXN));
123 else if (system_supports_bti_kernel() && spd->has_bti)
124 pte = set_pte_bit(pte, __pgprot(PTE_GP));
125 __set_pte(ptep, pte);
129 int __init efi_set_mapping_permissions(struct mm_struct *mm,
130 efi_memory_desc_t *md,
133 struct set_perm_data data = { md, has_bti };
135 BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
136 md->type != EFI_RUNTIME_SERVICES_DATA);
138 if (region_is_misaligned(md))
142 * Calling apply_to_page_range() is only safe on regions that are
143 * guaranteed to be mapped down to pages. Since we are only called
144 * for regions that have been mapped using efi_create_mapping() above
145 * (and this is checked by the generic Memory Attributes table parsing
146 * routines), there is no need to check that again here.
148 return apply_to_page_range(mm, md->virt_addr,
149 md->num_pages << EFI_PAGE_SHIFT,
150 set_permissions, &data);
154 * UpdateCapsule() depends on the system being shutdown via
157 bool efi_poweroff_required(void)
159 return efi_enabled(EFI_RUNTIME_SERVICES);
162 asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
164 pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
168 static DEFINE_RAW_SPINLOCK(efi_rt_lock);
170 void arch_efi_call_virt_setup(void)
173 raw_spin_lock(&efi_rt_lock);
174 __efi_fpsimd_begin();
177 void arch_efi_call_virt_teardown(void)
180 raw_spin_unlock(&efi_rt_lock);
181 efi_virtmap_unload();
184 asmlinkage u64 *efi_rt_stack_top __ro_after_init;
186 asmlinkage efi_status_t __efi_rt_asm_recover(void);
188 bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
190 /* Check whether the exception occurred while running the firmware */
191 if (!current_in_efi() || regs->pc >= TASK_SIZE_64)
194 pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
195 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
196 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
198 regs->regs[0] = EFI_ABORTED;
199 regs->regs[30] = efi_rt_stack_top[-1];
200 regs->pc = (u64)__efi_rt_asm_recover;
202 if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
203 regs->regs[18] = efi_rt_stack_top[-2];
208 /* EFI requires 8 KiB of stack space for runtime services */
209 static_assert(THREAD_SIZE >= SZ_8K);
211 static int __init arm64_efi_rt_init(void)
215 if (!efi_enabled(EFI_RUNTIME_SERVICES))
218 if (!IS_ENABLED(CONFIG_VMAP_STACK)) {
219 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
223 p = arch_alloc_vmap_stack(THREAD_SIZE, NUMA_NO_NODE);
225 pr_warn("Failed to allocate EFI runtime stack\n");
226 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
230 kmemleak_not_leak(p);
231 efi_rt_stack_top = p + THREAD_SIZE;
234 core_initcall(arm64_efi_rt_init);