hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/arch/arm64/kernel/efi.c
....@@ -1,20 +1,24 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Extensible Firmware Interface
34 *
45 * Based on Extensible Firmware Interface Specification version 2.4
56 *
67 * Copyright (C) 2013, 2014 Linaro Ltd.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
11
- *
128 */
139
1410 #include <linux/efi.h>
1511 #include <linux/init.h>
1612
1713 #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
+}
1822
1923 /*
2024 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
....@@ -29,14 +33,22 @@
2933 if (type == EFI_MEMORY_MAPPED_IO)
3034 return PROT_DEVICE_nGnRE;
3135
32
- if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
33
- "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
36
+ if (region_is_misaligned(md)) {
37
+ static bool __initdata code_is_misaligned;
38
+
3439 /*
35
- * If the region is not aligned to the page size of the OS, we
36
- * can not use strict permissions, since that would also affect
37
- * the mapping attributes of the adjacent regions.
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.
3846 */
39
- return pgprot_val(PAGE_KERNEL_EXEC);
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
+ }
4052
4153 /* R-- */
4254 if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
....@@ -58,7 +70,7 @@
5870 }
5971
6072 /* we will fill this structure from the stub, so don't put it in .bss */
61
-struct screen_info screen_info __section(.data);
73
+struct screen_info screen_info __section(".data");
6274
6375 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
6476 {
....@@ -66,19 +78,16 @@
6678 bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
6779 md->type == EFI_RUNTIME_SERVICES_DATA);
6880
69
- if (!PAGE_ALIGNED(md->phys_addr) ||
70
- !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
71
- /*
72
- * If the end address of this region is not aligned to page
73
- * size, the mapping is rounded up, and may end up sharing a
74
- * page frame with the next UEFI memory region. If we create
75
- * a block entry now, we may need to split it again when mapping
76
- * the next region, and support for that is going to be removed
77
- * from the MMU routines. So avoid block mappings altogether in
78
- * that case.
79
- */
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))
8090 page_mappings_only = true;
81
- }
8291
8392 create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
8493 md->num_pages << EFI_PAGE_SHIFT,
....@@ -86,8 +95,7 @@
8695 return 0;
8796 }
8897
89
-static int __init set_permissions(pte_t *ptep, pgtable_t token,
90
- unsigned long addr, void *data)
98
+static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
9199 {
92100 efi_memory_desc_t *md = data;
93101 pte_t pte = READ_ONCE(*ptep);
....@@ -105,6 +113,9 @@
105113 {
106114 BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
107115 md->type != EFI_RUNTIME_SERVICES_DATA);
116
+
117
+ if (region_is_misaligned(md))
118
+ return 0;
108119
109120 /*
110121 * Calling apply_to_page_range() is only safe on regions that are
....@@ -132,3 +143,30 @@
132143 pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
133144 return s;
134145 }
146
+
147
+DEFINE_RAW_SPINLOCK(efi_rt_lock);
148
+
149
+asmlinkage u64 *efi_rt_stack_top __ro_after_init;
150
+
151
+/* EFI requires 8 KiB of stack space for runtime services */
152
+static_assert(THREAD_SIZE >= SZ_8K);
153
+
154
+static int __init arm64_efi_rt_init(void)
155
+{
156
+ void *p;
157
+
158
+ if (!efi_enabled(EFI_RUNTIME_SERVICES))
159
+ return 0;
160
+
161
+ p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL,
162
+ NUMA_NO_NODE, &&l);
163
+l: if (!p) {
164
+ pr_warn("Failed to allocate EFI runtime stack\n");
165
+ clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
166
+ return -ENOMEM;
167
+ }
168
+
169
+ efi_rt_stack_top = p + THREAD_SIZE;
170
+ return 0;
171
+}
172
+core_initcall(arm64_efi_rt_init);