~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/arm64/kernel/efi.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php