efi: Reduce the amount of memblock reservations for persistent allocations
[linux-block.git] / arch / x86 / platform / efi / quirks.c
CommitLineData
26d7f65f
MF
1#define pr_fmt(fmt) "efi: " fmt
2
eeb9db09
ST
3#include <linux/init.h>
4#include <linux/kernel.h>
5#include <linux/string.h>
6#include <linux/time.h>
7#include <linux/types.h>
8#include <linux/efi.h>
9#include <linux/slab.h>
10#include <linux/memblock.h>
44be28e9 11#include <linux/acpi.h>
d394f2d9 12#include <linux/dmi.h>
5520b7e7
IM
13
14#include <asm/e820/api.h>
eeb9db09
ST
15#include <asm/efi.h>
16#include <asm/uv/uv.h>
2959c95d 17#include <asm/cpu_device_id.h>
3425d934 18#include <asm/reboot.h>
eeb9db09
ST
19
20#define EFI_MIN_RESERVE 5120
21
22#define EFI_DUMMY_GUID \
23 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
24
2959c95d
JK
25#define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */
26#define QUARK_SECURITY_HEADER_SIZE 0x400
27
28/*
29 * Header prepended to the standard EFI capsule on Quark systems the are based
30 * on Intel firmware BSP.
31 * @csh_signature: Unique identifier to sanity check signed module
32 * presence ("_CSH").
33 * @version: Current version of CSH used. Should be one for Quark A0.
34 * @modulesize: Size of the entire module including the module header
35 * and payload.
36 * @security_version_number_index: Index of SVN to use for validation of signed
37 * module.
38 * @security_version_number: Used to prevent against roll back of modules.
39 * @rsvd_module_id: Currently unused for Clanton (Quark).
40 * @rsvd_module_vendor: Vendor Identifier. For Intel products value is
41 * 0x00008086.
42 * @rsvd_date: BCD representation of build date as yyyymmdd, where
43 * yyyy=4 digit year, mm=1-12, dd=1-31.
44 * @headersize: Total length of the header including including any
45 * padding optionally added by the signing tool.
46 * @hash_algo: What Hash is used in the module signing.
47 * @cryp_algo: What Crypto is used in the module signing.
48 * @keysize: Total length of the key data including including any
49 * padding optionally added by the signing tool.
50 * @signaturesize: Total length of the signature including including any
51 * padding optionally added by the signing tool.
52 * @rsvd_next_header: 32-bit pointer to the next Secure Boot Module in the
53 * chain, if there is a next header.
54 * @rsvd: Reserved, padding structure to required size.
55 *
56 * See also QuartSecurityHeader_t in
57 * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h
58 * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP
59 */
60struct quark_security_header {
61 u32 csh_signature;
62 u32 version;
63 u32 modulesize;
64 u32 security_version_number_index;
65 u32 security_version_number;
66 u32 rsvd_module_id;
67 u32 rsvd_module_vendor;
68 u32 rsvd_date;
69 u32 headersize;
70 u32 hash_algo;
71 u32 cryp_algo;
72 u32 keysize;
73 u32 signaturesize;
74 u32 rsvd_next_header;
75 u32 rsvd[2];
76};
77
36b64976 78static const efi_char16_t efi_dummy_name[] = L"DUMMY";
eeb9db09
ST
79
80static bool efi_no_storage_paranoia;
81
82/*
83 * Some firmware implementations refuse to boot if there's insufficient
84 * space in the variable store. The implementation of garbage collection
85 * in some FW versions causes stale (deleted) variables to take up space
86 * longer than intended and space is only freed once the store becomes
87 * almost completely full.
88 *
89 * Enabling this option disables the space checks in
90 * efi_query_variable_store() and forces garbage collection.
91 *
92 * Only enable this option if deleting EFI variables does not free up
93 * space in your variable store, e.g. if despite deleting variables
94 * you're unable to create new ones.
95 */
96static int __init setup_storage_paranoia(char *arg)
97{
98 efi_no_storage_paranoia = true;
99 return 0;
100}
101early_param("efi_no_storage_paranoia", setup_storage_paranoia);
102
103/*
104 * Deleting the dummy variable which kicks off garbage collection
105*/
106void efi_delete_dummy_variable(void)
107{
5a58bc1b
SP
108 efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name,
109 &EFI_DUMMY_GUID,
110 EFI_VARIABLE_NON_VOLATILE |
111 EFI_VARIABLE_BOOTSERVICE_ACCESS |
112 EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL);
eeb9db09
ST
113}
114
ca0e30dc
AB
115/*
116 * In the nonblocking case we do not attempt to perform garbage
117 * collection if we do not have enough free space. Rather, we do the
118 * bare minimum check and give up immediately if the available space
119 * is below EFI_MIN_RESERVE.
120 *
121 * This function is intended to be small and simple because it is
122 * invoked from crash handler paths.
123 */
124static efi_status_t
125query_variable_store_nonblocking(u32 attributes, unsigned long size)
126{
127 efi_status_t status;
128 u64 storage_size, remaining_size, max_size;
129
130 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
131 &remaining_size,
132 &max_size);
133 if (status != EFI_SUCCESS)
134 return status;
135
136 if (remaining_size - size < EFI_MIN_RESERVE)
137 return EFI_OUT_OF_RESOURCES;
138
139 return EFI_SUCCESS;
140}
141
eeb9db09
ST
142/*
143 * Some firmware implementations refuse to boot if there's insufficient space
144 * in the variable store. Ensure that we never use more than a safe limit.
145 *
146 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
147 * store.
148 */
ca0e30dc
AB
149efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
150 bool nonblocking)
eeb9db09
ST
151{
152 efi_status_t status;
153 u64 storage_size, remaining_size, max_size;
154
155 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
156 return 0;
157
ca0e30dc
AB
158 if (nonblocking)
159 return query_variable_store_nonblocking(attributes, size);
160
eeb9db09
ST
161 status = efi.query_variable_info(attributes, &storage_size,
162 &remaining_size, &max_size);
163 if (status != EFI_SUCCESS)
164 return status;
165
166 /*
167 * We account for that by refusing the write if permitting it would
168 * reduce the available space to under 5KB. This figure was provided by
169 * Samsung, so should be safe.
170 */
171 if ((remaining_size - size < EFI_MIN_RESERVE) &&
172 !efi_no_storage_paranoia) {
173
174 /*
175 * Triggering garbage collection may require that the firmware
176 * generate a real EFI_OUT_OF_RESOURCES error. We can force
177 * that by attempting to use more space than is available.
178 */
179 unsigned long dummy_size = remaining_size + 1024;
9f66d8d7 180 void *dummy = kzalloc(dummy_size, GFP_KERNEL);
eeb9db09
ST
181
182 if (!dummy)
183 return EFI_OUT_OF_RESOURCES;
184
36b64976
AB
185 status = efi.set_variable((efi_char16_t *)efi_dummy_name,
186 &EFI_DUMMY_GUID,
eeb9db09
ST
187 EFI_VARIABLE_NON_VOLATILE |
188 EFI_VARIABLE_BOOTSERVICE_ACCESS |
189 EFI_VARIABLE_RUNTIME_ACCESS,
190 dummy_size, dummy);
191
192 if (status == EFI_SUCCESS) {
193 /*
194 * This should have failed, so if it didn't make sure
195 * that we delete it...
196 */
197 efi_delete_dummy_variable();
198 }
199
200 kfree(dummy);
201
202 /*
203 * The runtime code may now have triggered a garbage collection
204 * run, so check the variable info again
205 */
206 status = efi.query_variable_info(attributes, &storage_size,
207 &remaining_size, &max_size);
208
209 if (status != EFI_SUCCESS)
210 return status;
211
212 /*
213 * There still isn't enough room, so return an error
214 */
215 if (remaining_size - size < EFI_MIN_RESERVE)
216 return EFI_OUT_OF_RESOURCES;
217 }
218
219 return EFI_SUCCESS;
220}
221EXPORT_SYMBOL_GPL(efi_query_variable_store);
222
816e7612
MF
223/*
224 * The UEFI specification makes it clear that the operating system is
225 * free to do whatever it wants with boot services code after
226 * ExitBootServices() has been called. Ignoring this recommendation a
227 * significant bunch of EFI implementations continue calling into boot
228 * services code (SetVirtualAddressMap). In order to work around such
229 * buggy implementations we reserve boot services region during EFI
230 * init and make sure it stays executable. Then, after
231 * SetVirtualAddressMap(), it is discarded.
232 *
233 * However, some boot services regions contain data that is required
234 * by drivers, so we need to track which memory ranges can never be
235 * freed. This is done by tagging those regions with the
236 * EFI_MEMORY_RUNTIME attribute.
237 *
238 * Any driver that wants to mark a region as reserved must use
239 * efi_mem_reserve() which will insert a new EFI memory descriptor
240 * into efi.memmap (splitting existing regions if necessary) and tag
241 * it with EFI_MEMORY_RUNTIME.
242 */
243void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
244{
245 phys_addr_t new_phys, new_size;
246 struct efi_mem_range mr;
247 efi_memory_desc_t md;
248 int num_entries;
249 void *new;
250
7e1550b8
AB
251 if (efi_mem_desc_lookup(addr, &md) ||
252 md.type != EFI_BOOT_SERVICES_DATA) {
816e7612
MF
253 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
254 return;
255 }
256
257 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
258 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
259 return;
260 }
261
6f6266a5
OS
262 /* No need to reserve regions that will never be freed. */
263 if (md.attribute & EFI_MEMORY_RUNTIME)
264 return;
265
92dc3350
MF
266 size += addr % EFI_PAGE_SIZE;
267 size = round_up(size, EFI_PAGE_SIZE);
268 addr = round_down(addr, EFI_PAGE_SIZE);
269
816e7612 270 mr.range.start = addr;
92dc3350 271 mr.range.end = addr + size - 1;
816e7612
MF
272 mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
273
274 num_entries = efi_memmap_split_count(&md, &mr.range);
275 num_entries += efi.memmap.nr_map;
276
277 new_size = efi.memmap.desc_size * num_entries;
278
20b1e22d 279 new_phys = efi_memmap_alloc(num_entries);
816e7612
MF
280 if (!new_phys) {
281 pr_err("Could not allocate boot services memmap\n");
282 return;
283 }
284
285 new = early_memremap(new_phys, new_size);
286 if (!new) {
287 pr_err("Failed to map new boot services memmap\n");
288 return;
289 }
290
291 efi_memmap_insert(&efi.memmap, new, &mr);
292 early_memunmap(new, new_size);
293
294 efi_memmap_install(new_phys, num_entries);
295}
296
452308de
MF
297/*
298 * Helper function for efi_reserve_boot_services() to figure out if we
299 * can free regions in efi_free_boot_services().
300 *
301 * Use this function to ensure we do not free regions owned by somebody
302 * else. We must only reserve (and then free) regions:
303 *
304 * - Not within any part of the kernel
09821ff1 305 * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)
452308de
MF
306 */
307static bool can_free_region(u64 start, u64 size)
308{
309 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
310 return false;
311
09821ff1 312 if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
452308de
MF
313 return false;
314
315 return true;
316}
317
eeb9db09
ST
318void __init efi_reserve_boot_services(void)
319{
78ce248f 320 efi_memory_desc_t *md;
eeb9db09 321
78ce248f 322 for_each_efi_memory_desc(md) {
eeb9db09
ST
323 u64 start = md->phys_addr;
324 u64 size = md->num_pages << EFI_PAGE_SHIFT;
452308de 325 bool already_reserved;
eeb9db09
ST
326
327 if (md->type != EFI_BOOT_SERVICES_CODE &&
328 md->type != EFI_BOOT_SERVICES_DATA)
329 continue;
452308de
MF
330
331 already_reserved = memblock_is_region_reserved(start, size);
332
333 /*
334 * Because the following memblock_reserve() is paired
53ab85eb 335 * with memblock_free_late() for this region in
452308de
MF
336 * efi_free_boot_services(), we must be extremely
337 * careful not to reserve, and subsequently free,
338 * critical regions of memory (like the kernel image) or
339 * those regions that somebody else has already
340 * reserved.
341 *
342 * A good example of a critical region that must not be
343 * freed is page zero (first 4Kb of memory), which may
344 * contain boot services code/data but is marked
09821ff1 345 * E820_TYPE_RESERVED by trim_bios_range().
452308de
MF
346 */
347 if (!already_reserved) {
eeb9db09 348 memblock_reserve(start, size);
452308de
MF
349
350 /*
351 * If we are the first to reserve the region, no
352 * one else cares about it. We own it and can
353 * free it later.
354 */
355 if (can_free_region(start, size))
356 continue;
357 }
358
359 /*
360 * We don't own the region. We must not free it.
361 *
362 * Setting this bit for a boot services region really
363 * doesn't make sense as far as the firmware is
364 * concerned, but it does provide us with a way to tag
365 * those regions that must not be paired with
53ab85eb 366 * memblock_free_late().
452308de
MF
367 */
368 md->attribute |= EFI_MEMORY_RUNTIME;
eeb9db09
ST
369 }
370}
371
08cfb38f
SPP
372/*
373 * Apart from having VA mappings for EFI boot services code/data regions,
374 * (duplicate) 1:1 mappings were also created as a quirk for buggy firmware. So,
375 * unmap both 1:1 and VA mappings.
376 */
377static void __init efi_unmap_pages(efi_memory_desc_t *md)
378{
379 pgd_t *pgd = efi_mm.pgd;
380 u64 pa = md->phys_addr;
381 u64 va = md->virt_addr;
382
383 if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages))
384 pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa);
385
386 if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages))
387 pr_err("Failed to unmap VA mapping for 0x%llx\n", va);
388}
389
eeb9db09
ST
390void __init efi_free_boot_services(void)
391{
816e7612 392 phys_addr_t new_phys, new_size;
78ce248f 393 efi_memory_desc_t *md;
816e7612
MF
394 int num_entries = 0;
395 void *new, *new_md;
eeb9db09 396
78ce248f 397 for_each_efi_memory_desc(md) {
eeb9db09
ST
398 unsigned long long start = md->phys_addr;
399 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
5bc653b7 400 size_t rm_size;
eeb9db09
ST
401
402 if (md->type != EFI_BOOT_SERVICES_CODE &&
816e7612
MF
403 md->type != EFI_BOOT_SERVICES_DATA) {
404 num_entries++;
eeb9db09 405 continue;
816e7612 406 }
eeb9db09 407
452308de 408 /* Do not free, someone else owns it: */
816e7612
MF
409 if (md->attribute & EFI_MEMORY_RUNTIME) {
410 num_entries++;
eeb9db09 411 continue;
816e7612 412 }
eeb9db09 413
08cfb38f
SPP
414 /*
415 * Before calling set_virtual_address_map(), EFI boot services
416 * code/data regions were mapped as a quirk for buggy firmware.
417 * Unmap them from efi_pgd before freeing them up.
418 */
419 efi_unmap_pages(md);
420
5bc653b7
AL
421 /*
422 * Nasty quirk: if all sub-1MB memory is used for boot
423 * services, we can get here without having allocated the
424 * real mode trampoline. It's too late to hand boot services
425 * memory back to the memblock allocator, so instead
426 * try to manually allocate the trampoline if needed.
427 *
428 * I've seen this on a Dell XPS 13 9350 with firmware
429 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
430 * grub2-efi on a hard disk. (And no, I don't know why
431 * this happened, but Linux should still try to boot rather
432 * panicing early.)
433 */
434 rm_size = real_mode_size_needed();
435 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
436 set_real_mode_mem(start, rm_size);
437 start += rm_size;
438 size -= rm_size;
439 }
440
53ab85eb 441 memblock_free_late(start, size);
eeb9db09 442 }
816e7612 443
1ea34adb
JG
444 if (!num_entries)
445 return;
446
816e7612 447 new_size = efi.memmap.desc_size * num_entries;
20b1e22d 448 new_phys = efi_memmap_alloc(num_entries);
816e7612
MF
449 if (!new_phys) {
450 pr_err("Failed to allocate new EFI memmap\n");
451 return;
452 }
453
454 new = memremap(new_phys, new_size, MEMREMAP_WB);
455 if (!new) {
456 pr_err("Failed to map new EFI memmap\n");
457 return;
458 }
459
460 /*
461 * Build a new EFI memmap that excludes any boot services
462 * regions that are not tagged EFI_MEMORY_RUNTIME, since those
463 * regions have now been freed.
464 */
465 new_md = new;
466 for_each_efi_memory_desc(md) {
467 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
468 (md->type == EFI_BOOT_SERVICES_CODE ||
469 md->type == EFI_BOOT_SERVICES_DATA))
470 continue;
471
472 memcpy(new_md, md, efi.memmap.desc_size);
473 new_md += efi.memmap.desc_size;
474 }
475
476 memunmap(new);
477
478 if (efi_memmap_install(new_phys, num_entries)) {
479 pr_err("Could not install new EFI memmap\n");
480 return;
481 }
eeb9db09
ST
482}
483
484/*
485 * A number of config table entries get remapped to virtual addresses
486 * after entering EFI virtual mode. However, the kexec kernel requires
487 * their physical addresses therefore we pass them via setup_data and
488 * correct those entries to their respective physical addresses here.
489 *
490 * Currently only handles smbios which is necessary for some firmware
491 * implementation.
492 */
493int __init efi_reuse_config(u64 tables, int nr_tables)
494{
495 int i, sz, ret = 0;
496 void *p, *tablep;
497 struct efi_setup_data *data;
498
499 if (!efi_setup)
500 return 0;
501
502 if (!efi_enabled(EFI_64BIT))
503 return 0;
504
505 data = early_memremap(efi_setup, sizeof(*data));
506 if (!data) {
507 ret = -ENOMEM;
508 goto out;
509 }
510
511 if (!data->smbios)
512 goto out_memremap;
513
514 sz = sizeof(efi_config_table_64_t);
515
516 p = tablep = early_memremap(tables, nr_tables * sz);
517 if (!p) {
518 pr_err("Could not map Configuration table!\n");
519 ret = -ENOMEM;
520 goto out_memremap;
521 }
522
523 for (i = 0; i < efi.systab->nr_tables; i++) {
524 efi_guid_t guid;
525
526 guid = ((efi_config_table_64_t *)p)->guid;
527
528 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
529 ((efi_config_table_64_t *)p)->table = data->smbios;
530 p += sz;
531 }
98a716b6 532 early_memunmap(tablep, nr_tables * sz);
eeb9db09
ST
533
534out_memremap:
98a716b6 535 early_memunmap(data, sizeof(*data));
eeb9db09
ST
536out:
537 return ret;
538}
539
d394f2d9
AT
540static const struct dmi_system_id sgi_uv1_dmi[] = {
541 { NULL, "SGI UV1",
542 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
543 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
544 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
545 }
546 },
547 { } /* NULL entry stops DMI scanning */
548};
549
eeb9db09
ST
550void __init efi_apply_memmap_quirks(void)
551{
552 /*
553 * Once setup is done earlier, unmap the EFI memory map on mismatched
554 * firmware/kernel architectures since there is no support for runtime
555 * services.
556 */
557 if (!efi_runtime_supported()) {
26d7f65f 558 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
9479c7ce 559 efi_memmap_unmap();
eeb9db09
ST
560 }
561
d394f2d9
AT
562 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
563 if (dmi_check_system(sgi_uv1_dmi))
eeb9db09
ST
564 set_bit(EFI_OLD_MEMMAP, &efi.flags);
565}
44be28e9
MF
566
567/*
568 * For most modern platforms the preferred method of powering off is via
569 * ACPI. However, there are some that are known to require the use of
570 * EFI runtime services and for which ACPI does not work at all.
571 *
572 * Using EFI is a last resort, to be used only if no other option
573 * exists.
574 */
575bool efi_reboot_required(void)
576{
577 if (!acpi_gbl_reduced_hardware)
578 return false;
579
580 efi_reboot_quirk_mode = EFI_RESET_WARM;
581 return true;
582}
583
584bool efi_poweroff_required(void)
585{
13737181 586 return acpi_gbl_reduced_hardware || acpi_no_s5;
44be28e9 587}
2959c95d
JK
588
589#ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
590
591static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
592 size_t hdr_bytes)
593{
594 struct quark_security_header *csh = *pkbuff;
595
596 /* Only process data block that is larger than the security header */
597 if (hdr_bytes < sizeof(struct quark_security_header))
598 return 0;
599
600 if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
601 csh->headersize != QUARK_SECURITY_HEADER_SIZE)
602 return 1;
603
604 /* Only process data block if EFI header is included */
605 if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
606 sizeof(efi_capsule_header_t))
607 return 0;
608
609 pr_debug("Quark security header detected\n");
610
611 if (csh->rsvd_next_header != 0) {
612 pr_err("multiple Quark security headers not supported\n");
613 return -EINVAL;
614 }
615
616 *pkbuff += csh->headersize;
617 cap_info->total_size = csh->headersize;
618
619 /*
620 * Update the first page pointer to skip over the CSH header.
621 */
f24c4d47
AB
622 cap_info->phys[0] += csh->headersize;
623
624 /*
625 * cap_info->capsule should point at a virtual mapping of the entire
626 * capsule, starting at the capsule header. Our image has the Quark
627 * security header prepended, so we cannot rely on the default vmap()
628 * mapping created by the generic capsule code.
629 * Given that the Quark firmware does not appear to care about the
630 * virtual mapping, let's just point cap_info->capsule at our copy
631 * of the capsule header.
632 */
633 cap_info->capsule = &cap_info->header;
2959c95d
JK
634
635 return 1;
636}
637
638#define ICPU(family, model, quirk_handler) \
639 { X86_VENDOR_INTEL, family, model, X86_FEATURE_ANY, \
640 (unsigned long)&quirk_handler }
641
642static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
643 ICPU(5, 9, qrk_capsule_setup_info), /* Intel Quark X1000 */
644 { }
645};
646
647int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
648 size_t hdr_bytes)
649{
650 int (*quirk_handler)(struct capsule_info *, void **, size_t);
651 const struct x86_cpu_id *id;
652 int ret;
653
654 if (hdr_bytes < sizeof(efi_capsule_header_t))
655 return 0;
656
657 cap_info->total_size = 0;
658
659 id = x86_match_cpu(efi_capsule_quirk_ids);
660 if (id) {
661 /*
662 * The quirk handler is supposed to return
663 * - a value > 0 if the setup should continue, after advancing
664 * kbuff as needed
665 * - 0 if not enough hdr_bytes are available yet
666 * - a negative error code otherwise
667 */
668 quirk_handler = (typeof(quirk_handler))id->driver_data;
669 ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
670 if (ret <= 0)
671 return ret;
672 }
673
674 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
675
676 cap_info->total_size += cap_info->header.imagesize;
677
678 return __efi_capsule_setup_info(cap_info);
679}
680
681#endif
3425d934
SP
682
683/*
684 * If any access by any efi runtime service causes a page fault, then,
685 * 1. If it's efi_reset_system(), reboot through BIOS.
686 * 2. If any other efi runtime service, then
687 * a. Return error status to the efi caller process.
688 * b. Disable EFI Runtime Services forever and
689 * c. Freeze efi_rts_wq and schedule new process.
690 *
691 * @return: Returns, if the page fault is not handled. This function
692 * will never return if the page fault is handled successfully.
693 */
694void efi_recover_from_page_fault(unsigned long phys_addr)
695{
696 if (!IS_ENABLED(CONFIG_X86_64))
697 return;
698
699 /*
700 * Make sure that an efi runtime service caused the page fault.
701 * "efi_mm" cannot be used to check if the page fault had occurred
702 * in the firmware context because efi=old_map doesn't use efi_pgd.
703 */
704 if (efi_rts_work.efi_rts_id == NONE)
705 return;
706
707 /*
708 * Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so
709 * page faulting on these addresses isn't expected.
710 */
711 if (phys_addr >= 0x0000 && phys_addr <= 0x0fff)
712 return;
713
714 /*
715 * Print stack trace as it might be useful to know which EFI Runtime
716 * Service is buggy.
717 */
718 WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
719 phys_addr);
720
721 /*
722 * Buggy efi_reset_system() is handled differently from other EFI
723 * Runtime Services as it doesn't use efi_rts_wq. Although,
724 * native_machine_emergency_restart() says that machine_real_restart()
725 * could fail, it's better not to compilcate this fault handler
726 * because this case occurs *very* rarely and hence could be improved
727 * on a need by basis.
728 */
729 if (efi_rts_work.efi_rts_id == RESET_SYSTEM) {
730 pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
731 machine_real_restart(MRR_BIOS);
732 return;
733 }
734
735 /*
736 * Before calling EFI Runtime Service, the kernel has switched the
737 * calling process to efi_mm. Hence, switch back to task_mm.
738 */
739 arch_efi_call_virt_teardown();
740
741 /* Signal error status to the efi caller process */
742 efi_rts_work.status = EFI_ABORTED;
743 complete(&efi_rts_work.efi_rts_comp);
744
745 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
746 pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
747
748 /*
749 * Call schedule() in an infinite loop, so that any spurious wake ups
750 * will never run efi_rts_wq again.
751 */
752 for (;;) {
753 set_current_state(TASK_IDLE);
754 schedule();
755 }
756
757 return;
758}