hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/x86/platform/efi/efi.c
....@@ -36,9 +36,8 @@
3636 #include <linux/efi.h>
3737 #include <linux/efi-bgrt.h>
3838 #include <linux/export.h>
39
-#include <linux/bootmem.h>
40
-#include <linux/slab.h>
4139 #include <linux/memblock.h>
40
+#include <linux/slab.h>
4241 #include <linux/spinlock.h>
4342 #include <linux/uaccess.h>
4443 #include <linux/time.h>
....@@ -50,19 +49,50 @@
5049 #include <asm/efi.h>
5150 #include <asm/e820/api.h>
5251 #include <asm/time.h>
53
-#include <asm/set_memory.h>
5452 #include <asm/tlbflush.h>
5553 #include <asm/x86_init.h>
5654 #include <asm/uv/uv.h>
5755
58
-static struct efi efi_phys __initdata;
59
-static efi_system_table_t efi_systab __initdata;
56
+static unsigned long efi_systab_phys __initdata;
57
+static unsigned long prop_phys = EFI_INVALID_TABLE_ADDR;
58
+static unsigned long uga_phys = EFI_INVALID_TABLE_ADDR;
59
+static unsigned long efi_runtime, efi_nr_tables;
6060
61
-static efi_config_table_type_t arch_tables[] __initdata = {
61
+unsigned long efi_fw_vendor, efi_config_table;
62
+
63
+static const efi_config_table_type_t arch_tables[] __initconst = {
64
+ {EFI_PROPERTIES_TABLE_GUID, &prop_phys, "PROP" },
65
+ {UGA_IO_PROTOCOL_GUID, &uga_phys, "UGA" },
6266 #ifdef CONFIG_X86_UV
63
- {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
67
+ {UV_SYSTEM_TABLE_GUID, &uv_systab_phys, "UVsystab" },
6468 #endif
65
- {NULL_GUID, NULL, NULL},
69
+ {},
70
+};
71
+
72
+static const unsigned long * const efi_tables[] = {
73
+ &efi.acpi,
74
+ &efi.acpi20,
75
+ &efi.smbios,
76
+ &efi.smbios3,
77
+ &uga_phys,
78
+#ifdef CONFIG_X86_UV
79
+ &uv_systab_phys,
80
+#endif
81
+ &efi_fw_vendor,
82
+ &efi_runtime,
83
+ &efi_config_table,
84
+ &efi.esrt,
85
+ &prop_phys,
86
+ &efi_mem_attr_table,
87
+#ifdef CONFIG_EFI_RCI2_TABLE
88
+ &rci2_table_phys,
89
+#endif
90
+ &efi.tpm_log,
91
+ &efi.tpm_final_log,
92
+ &efi_rng_seed,
93
+#ifdef CONFIG_LOAD_UEFI_KEYS
94
+ &efi.mokvar_table,
95
+#endif
6696 };
6797
6898 u64 efi_setup; /* efi setup_data physical address */
....@@ -75,34 +105,13 @@
75105 }
76106 early_param("add_efi_memmap", setup_add_efi_memmap);
77107
78
-static efi_status_t __init phys_efi_set_virtual_address_map(
79
- unsigned long memory_map_size,
80
- unsigned long descriptor_size,
81
- u32 descriptor_version,
82
- efi_memory_desc_t *virtual_map)
83
-{
84
- efi_status_t status;
85
- unsigned long flags;
86
- pgd_t *save_pgd;
87
-
88
- save_pgd = efi_call_phys_prolog();
89
-
90
- /* Disable interrupts around EFI calls: */
91
- local_irq_save(flags);
92
- status = efi_call_phys(efi_phys.set_virtual_address_map,
93
- memory_map_size, descriptor_size,
94
- descriptor_version, virtual_map);
95
- local_irq_restore(flags);
96
-
97
- efi_call_phys_epilog(save_pgd);
98
-
99
- return status;
100
-}
101
-
102108 void __init efi_find_mirror(void)
103109 {
104110 efi_memory_desc_t *md;
105111 u64 mirror_size = 0, total_size = 0;
112
+
113
+ if (!efi_enabled(EFI_MEMMAP))
114
+ return;
106115
107116 for_each_efi_memory_desc(md) {
108117 unsigned long long start = md->phys_addr;
....@@ -121,13 +130,17 @@
121130
122131 /*
123132 * Tell the kernel about the EFI memory map. This might include
124
- * more than the max 128 entries that can fit in the e820 legacy
125
- * (zeropage) memory map.
133
+ * more than the max 128 entries that can fit in the passed in e820
134
+ * legacy (zeropage) memory map, but the kernel's e820 table can hold
135
+ * E820_MAX_ENTRIES.
126136 */
127137
128138 static void __init do_add_efi_memmap(void)
129139 {
130140 efi_memory_desc_t *md;
141
+
142
+ if (!efi_enabled(EFI_MEMMAP))
143
+ return;
131144
132145 for_each_efi_memory_desc(md) {
133146 unsigned long long start = md->phys_addr;
....@@ -140,7 +153,10 @@
140153 case EFI_BOOT_SERVICES_CODE:
141154 case EFI_BOOT_SERVICES_DATA:
142155 case EFI_CONVENTIONAL_MEMORY:
143
- if (md->attribute & EFI_MEMORY_WB)
156
+ if (efi_soft_reserve_enabled()
157
+ && (md->attribute & EFI_MEMORY_SP))
158
+ e820_type = E820_TYPE_SOFT_RESERVED;
159
+ else if (md->attribute & EFI_MEMORY_WB)
144160 e820_type = E820_TYPE_RAM;
145161 else
146162 e820_type = E820_TYPE_RESERVED;
....@@ -166,9 +182,34 @@
166182 e820_type = E820_TYPE_RESERVED;
167183 break;
168184 }
185
+
169186 e820__range_add(start, size, e820_type);
170187 }
171188 e820__update_table(e820_table);
189
+}
190
+
191
+/*
192
+ * Given add_efi_memmap defaults to 0 and there there is no alternative
193
+ * e820 mechanism for soft-reserved memory, import the full EFI memory
194
+ * map if soft reservations are present and enabled. Otherwise, the
195
+ * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
196
+ * the efi=nosoftreserve option.
197
+ */
198
+static bool do_efi_soft_reserve(void)
199
+{
200
+ efi_memory_desc_t *md;
201
+
202
+ if (!efi_enabled(EFI_MEMMAP))
203
+ return false;
204
+
205
+ if (!efi_soft_reserve_enabled())
206
+ return false;
207
+
208
+ for_each_efi_memory_desc(md)
209
+ if (md->type == EFI_CONVENTIONAL_MEMORY &&
210
+ (md->attribute & EFI_MEMORY_SP))
211
+ return true;
212
+ return false;
172213 }
173214
174215 int __init efi_memblock_x86_reserve_range(void)
....@@ -181,16 +222,13 @@
181222 if (efi_enabled(EFI_PARAVIRT))
182223 return 0;
183224
184
-#ifdef CONFIG_X86_32
185
- /* Can't handle data above 4GB at this time */
186
- if (e->efi_memmap_hi) {
225
+ /* Can't handle firmware tables above 4GB on i386 */
226
+ if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
187227 pr_err("Memory map is above 4GB, disabling EFI.\n");
188228 return -EINVAL;
189229 }
190
- pmap = e->efi_memmap;
191
-#else
192
- pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
193
-#endif
230
+ pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
231
+
194232 data.phys_map = pmap;
195233 data.size = e->efi_memmap_size;
196234 data.desc_size = e->efi_memdesc_size;
....@@ -200,14 +238,17 @@
200238 if (rv)
201239 return rv;
202240
203
- if (add_efi_memmap)
241
+ if (add_efi_memmap || do_efi_soft_reserve())
204242 do_add_efi_memmap();
243
+
244
+ efi_fake_memmap_early();
205245
206246 WARN(efi.memmap.desc_version != 1,
207247 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
208248 efi.memmap.desc_version);
209249
210250 memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
251
+ set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
211252
212253 return 0;
213254 }
....@@ -269,10 +310,16 @@
269310 }
270311
271312 if (n_removal > 0) {
272
- u64 size = efi.memmap.nr_map - n_removal;
313
+ struct efi_memory_map_data data = {
314
+ .phys_map = efi.memmap.phys_map,
315
+ .desc_version = efi.memmap.desc_version,
316
+ .desc_size = efi.memmap.desc_size,
317
+ .size = efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
318
+ .flags = 0,
319
+ };
273320
274321 pr_warn("Removing %d invalid memory map entries.\n", n_removal);
275
- efi_memmap_install(efi.memmap.phys_map, size);
322
+ efi_memmap_install(&data);
276323 }
277324 }
278325
....@@ -292,234 +339,124 @@
292339 }
293340 }
294341
295
-static int __init efi_systab_init(void *phys)
342
+static int __init efi_systab_init(unsigned long phys)
296343 {
344
+ int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
345
+ : sizeof(efi_system_table_32_t);
346
+ const efi_table_hdr_t *hdr;
347
+ bool over4g = false;
348
+ void *p;
349
+ int ret;
350
+
351
+ hdr = p = early_memremap_ro(phys, size);
352
+ if (p == NULL) {
353
+ pr_err("Couldn't map the system table!\n");
354
+ return -ENOMEM;
355
+ }
356
+
357
+ ret = efi_systab_check_header(hdr, 1);
358
+ if (ret) {
359
+ early_memunmap(p, size);
360
+ return ret;
361
+ }
362
+
297363 if (efi_enabled(EFI_64BIT)) {
298
- efi_system_table_64_t *systab64;
299
- struct efi_setup_data *data = NULL;
300
- u64 tmp = 0;
364
+ const efi_system_table_64_t *systab64 = p;
365
+
366
+ efi_runtime = systab64->runtime;
367
+ over4g = systab64->runtime > U32_MAX;
301368
302369 if (efi_setup) {
303
- data = early_memremap(efi_setup, sizeof(*data));
304
- if (!data)
370
+ struct efi_setup_data *data;
371
+
372
+ data = early_memremap_ro(efi_setup, sizeof(*data));
373
+ if (!data) {
374
+ early_memunmap(p, size);
305375 return -ENOMEM;
306
- }
307
- systab64 = early_memremap((unsigned long)phys,
308
- sizeof(*systab64));
309
- if (systab64 == NULL) {
310
- pr_err("Couldn't map the system table!\n");
311
- if (data)
312
- early_memunmap(data, sizeof(*data));
313
- return -ENOMEM;
314
- }
376
+ }
315377
316
- efi_systab.hdr = systab64->hdr;
317
- efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
318
- systab64->fw_vendor;
319
- tmp |= data ? data->fw_vendor : systab64->fw_vendor;
320
- efi_systab.fw_revision = systab64->fw_revision;
321
- efi_systab.con_in_handle = systab64->con_in_handle;
322
- tmp |= systab64->con_in_handle;
323
- efi_systab.con_in = systab64->con_in;
324
- tmp |= systab64->con_in;
325
- efi_systab.con_out_handle = systab64->con_out_handle;
326
- tmp |= systab64->con_out_handle;
327
- efi_systab.con_out = systab64->con_out;
328
- tmp |= systab64->con_out;
329
- efi_systab.stderr_handle = systab64->stderr_handle;
330
- tmp |= systab64->stderr_handle;
331
- efi_systab.stderr = systab64->stderr;
332
- tmp |= systab64->stderr;
333
- efi_systab.runtime = data ?
334
- (void *)(unsigned long)data->runtime :
335
- (void *)(unsigned long)systab64->runtime;
336
- tmp |= data ? data->runtime : systab64->runtime;
337
- efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
338
- tmp |= systab64->boottime;
339
- efi_systab.nr_tables = systab64->nr_tables;
340
- efi_systab.tables = data ? (unsigned long)data->tables :
341
- systab64->tables;
342
- tmp |= data ? data->tables : systab64->tables;
378
+ efi_fw_vendor = (unsigned long)data->fw_vendor;
379
+ efi_config_table = (unsigned long)data->tables;
343380
344
- early_memunmap(systab64, sizeof(*systab64));
345
- if (data)
381
+ over4g |= data->fw_vendor > U32_MAX ||
382
+ data->tables > U32_MAX;
383
+
346384 early_memunmap(data, sizeof(*data));
347
-#ifdef CONFIG_X86_32
348
- if (tmp >> 32) {
349
- pr_err("EFI data located above 4GB, disabling EFI.\n");
350
- return -EINVAL;
385
+ } else {
386
+ efi_fw_vendor = systab64->fw_vendor;
387
+ efi_config_table = systab64->tables;
388
+
389
+ over4g |= systab64->fw_vendor > U32_MAX ||
390
+ systab64->tables > U32_MAX;
351391 }
352
-#endif
392
+ efi_nr_tables = systab64->nr_tables;
353393 } else {
354
- efi_system_table_32_t *systab32;
394
+ const efi_system_table_32_t *systab32 = p;
355395
356
- systab32 = early_memremap((unsigned long)phys,
357
- sizeof(*systab32));
358
- if (systab32 == NULL) {
359
- pr_err("Couldn't map the system table!\n");
360
- return -ENOMEM;
361
- }
362
-
363
- efi_systab.hdr = systab32->hdr;
364
- efi_systab.fw_vendor = systab32->fw_vendor;
365
- efi_systab.fw_revision = systab32->fw_revision;
366
- efi_systab.con_in_handle = systab32->con_in_handle;
367
- efi_systab.con_in = systab32->con_in;
368
- efi_systab.con_out_handle = systab32->con_out_handle;
369
- efi_systab.con_out = systab32->con_out;
370
- efi_systab.stderr_handle = systab32->stderr_handle;
371
- efi_systab.stderr = systab32->stderr;
372
- efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
373
- efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
374
- efi_systab.nr_tables = systab32->nr_tables;
375
- efi_systab.tables = systab32->tables;
376
-
377
- early_memunmap(systab32, sizeof(*systab32));
396
+ efi_fw_vendor = systab32->fw_vendor;
397
+ efi_runtime = systab32->runtime;
398
+ efi_config_table = systab32->tables;
399
+ efi_nr_tables = systab32->nr_tables;
378400 }
379401
380
- efi.systab = &efi_systab;
402
+ efi.runtime_version = hdr->revision;
381403
382
- /*
383
- * Verify the EFI Table
384
- */
385
- if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
386
- pr_err("System table signature incorrect!\n");
404
+ efi_systab_report_header(hdr, efi_fw_vendor);
405
+ early_memunmap(p, size);
406
+
407
+ if (IS_ENABLED(CONFIG_X86_32) && over4g) {
408
+ pr_err("EFI data located above 4GB, disabling EFI.\n");
387409 return -EINVAL;
388410 }
389
- if ((efi.systab->hdr.revision >> 16) == 0)
390
- pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
391
- efi.systab->hdr.revision >> 16,
392
- efi.systab->hdr.revision & 0xffff);
393411
394412 return 0;
395413 }
396414
397
-static int __init efi_runtime_init32(void)
415
+static int __init efi_config_init(const efi_config_table_type_t *arch_tables)
398416 {
399
- efi_runtime_services_32_t *runtime;
417
+ void *config_tables;
418
+ int sz, ret;
400419
401
- runtime = early_memremap((unsigned long)efi.systab->runtime,
402
- sizeof(efi_runtime_services_32_t));
403
- if (!runtime) {
404
- pr_err("Could not map the runtime service table!\n");
420
+ if (efi_nr_tables == 0)
421
+ return 0;
422
+
423
+ if (efi_enabled(EFI_64BIT))
424
+ sz = sizeof(efi_config_table_64_t);
425
+ else
426
+ sz = sizeof(efi_config_table_32_t);
427
+
428
+ /*
429
+ * Let's see what config tables the firmware passed to us.
430
+ */
431
+ config_tables = early_memremap(efi_config_table, efi_nr_tables * sz);
432
+ if (config_tables == NULL) {
433
+ pr_err("Could not map Configuration table!\n");
405434 return -ENOMEM;
406435 }
407436
408
- /*
409
- * We will only need *early* access to the SetVirtualAddressMap
410
- * EFI runtime service. All other runtime services will be called
411
- * via the virtual mapping.
412
- */
413
- efi_phys.set_virtual_address_map =
414
- (efi_set_virtual_address_map_t *)
415
- (unsigned long)runtime->set_virtual_address_map;
416
- early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
437
+ ret = efi_config_parse_tables(config_tables, efi_nr_tables,
438
+ arch_tables);
417439
418
- return 0;
419
-}
420
-
421
-static int __init efi_runtime_init64(void)
422
-{
423
- efi_runtime_services_64_t *runtime;
424
-
425
- runtime = early_memremap((unsigned long)efi.systab->runtime,
426
- sizeof(efi_runtime_services_64_t));
427
- if (!runtime) {
428
- pr_err("Could not map the runtime service table!\n");
429
- return -ENOMEM;
430
- }
431
-
432
- /*
433
- * We will only need *early* access to the SetVirtualAddressMap
434
- * EFI runtime service. All other runtime services will be called
435
- * via the virtual mapping.
436
- */
437
- efi_phys.set_virtual_address_map =
438
- (efi_set_virtual_address_map_t *)
439
- (unsigned long)runtime->set_virtual_address_map;
440
- early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
441
-
442
- return 0;
443
-}
444
-
445
-static int __init efi_runtime_init(void)
446
-{
447
- int rv;
448
-
449
- /*
450
- * Check out the runtime services table. We need to map
451
- * the runtime services table so that we can grab the physical
452
- * address of several of the EFI runtime functions, needed to
453
- * set the firmware into virtual mode.
454
- *
455
- * When EFI_PARAVIRT is in force then we could not map runtime
456
- * service memory region because we do not have direct access to it.
457
- * However, runtime services are available through proxy functions
458
- * (e.g. in case of Xen dom0 EFI implementation they call special
459
- * hypercall which executes relevant EFI functions) and that is why
460
- * they are always enabled.
461
- */
462
-
463
- if (!efi_enabled(EFI_PARAVIRT)) {
464
- if (efi_enabled(EFI_64BIT))
465
- rv = efi_runtime_init64();
466
- else
467
- rv = efi_runtime_init32();
468
-
469
- if (rv)
470
- return rv;
471
- }
472
-
473
- set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
474
-
475
- return 0;
440
+ early_memunmap(config_tables, efi_nr_tables * sz);
441
+ return ret;
476442 }
477443
478444 void __init efi_init(void)
479445 {
480
- efi_char16_t *c16;
481
- char vendor[100] = "unknown";
482
- int i = 0;
483
-
484
-#ifdef CONFIG_X86_32
485
- if (boot_params.efi_info.efi_systab_hi ||
486
- boot_params.efi_info.efi_memmap_hi) {
446
+ if (IS_ENABLED(CONFIG_X86_32) &&
447
+ (boot_params.efi_info.efi_systab_hi ||
448
+ boot_params.efi_info.efi_memmap_hi)) {
487449 pr_info("Table located above 4GB, disabling EFI.\n");
488450 return;
489451 }
490
- efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
491
-#else
492
- efi_phys.systab = (efi_system_table_t *)
493
- (boot_params.efi_info.efi_systab |
494
- ((__u64)boot_params.efi_info.efi_systab_hi<<32));
495
-#endif
496452
497
- if (efi_systab_init(efi_phys.systab))
453
+ efi_systab_phys = boot_params.efi_info.efi_systab |
454
+ ((__u64)boot_params.efi_info.efi_systab_hi << 32);
455
+
456
+ if (efi_systab_init(efi_systab_phys))
498457 return;
499458
500
- efi.config_table = (unsigned long)efi.systab->tables;
501
- efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
502
- efi.runtime = (unsigned long)efi.systab->runtime;
503
-
504
- /*
505
- * Show what we know for posterity
506
- */
507
- c16 = early_memremap_ro(efi.systab->fw_vendor,
508
- sizeof(vendor) * sizeof(efi_char16_t));
509
- if (c16) {
510
- for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
511
- vendor[i] = c16[i];
512
- vendor[i] = '\0';
513
- early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
514
- } else {
515
- pr_err("Could not map the firmware vendor!\n");
516
- }
517
-
518
- pr_info("EFI v%u.%.02u by %s\n",
519
- efi.systab->hdr.revision >> 16,
520
- efi.systab->hdr.revision & 0xffff, vendor);
521
-
522
- if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
459
+ if (efi_reuse_config(efi_config_table, efi_nr_tables))
523460 return;
524461
525462 if (efi_config_init(arch_tables))
....@@ -532,81 +469,33 @@
532469
533470 if (!efi_runtime_supported())
534471 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
535
- else {
536
- if (efi_runtime_disabled() || efi_runtime_init()) {
537
- efi_memmap_unmap();
538
- return;
472
+
473
+ if (!efi_runtime_supported() || efi_runtime_disabled()) {
474
+ efi_memmap_unmap();
475
+ return;
476
+ }
477
+
478
+ /* Parse the EFI Properties table if it exists */
479
+ if (prop_phys != EFI_INVALID_TABLE_ADDR) {
480
+ efi_properties_table_t *tbl;
481
+
482
+ tbl = early_memremap_ro(prop_phys, sizeof(*tbl));
483
+ if (tbl == NULL) {
484
+ pr_err("Could not map Properties table!\n");
485
+ } else {
486
+ if (tbl->memory_protection_attribute &
487
+ EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
488
+ set_bit(EFI_NX_PE_DATA, &efi.flags);
489
+
490
+ early_memunmap(tbl, sizeof(*tbl));
539491 }
540492 }
541493
494
+ set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
542495 efi_clean_memmap();
543496
544497 if (efi_enabled(EFI_DBG))
545498 efi_print_memmap();
546
-}
547
-
548
-void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
549
-{
550
- u64 addr, npages;
551
-
552
- addr = md->virt_addr;
553
- npages = md->num_pages;
554
-
555
- memrange_efi_to_native(&addr, &npages);
556
-
557
- if (executable)
558
- set_memory_x(addr, npages);
559
- else
560
- set_memory_nx(addr, npages);
561
-}
562
-
563
-void __init runtime_code_page_mkexec(void)
564
-{
565
- efi_memory_desc_t *md;
566
-
567
- /* Make EFI runtime service code area executable */
568
- for_each_efi_memory_desc(md) {
569
- if (md->type != EFI_RUNTIME_SERVICES_CODE)
570
- continue;
571
-
572
- efi_set_executable(md, true);
573
- }
574
-}
575
-
576
-void __init efi_memory_uc(u64 addr, unsigned long size)
577
-{
578
- unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
579
- u64 npages;
580
-
581
- npages = round_up(size, page_shift) / page_shift;
582
- memrange_efi_to_native(&addr, &npages);
583
- set_memory_uc(addr, npages);
584
-}
585
-
586
-void __init old_map_region(efi_memory_desc_t *md)
587
-{
588
- u64 start_pfn, end_pfn, end;
589
- unsigned long size;
590
- void *va;
591
-
592
- start_pfn = PFN_DOWN(md->phys_addr);
593
- size = md->num_pages << PAGE_SHIFT;
594
- end = md->phys_addr + size;
595
- end_pfn = PFN_UP(end);
596
-
597
- if (pfn_range_is_mapped(start_pfn, end_pfn)) {
598
- va = __va(md->phys_addr);
599
-
600
- if (!(md->attribute & EFI_MEMORY_WB))
601
- efi_memory_uc((u64)(unsigned long)va, size);
602
- } else
603
- va = efi_ioremap(md->phys_addr, size,
604
- md->type, md->attribute);
605
-
606
- md->virt_addr = (u64) (unsigned long) va;
607
- if (!va)
608
- pr_err("ioremap of 0x%llX failed!\n",
609
- (unsigned long long)md->phys_addr);
610499 }
611500
612501 /* Merge contiguous regions of the same type and attribute */
....@@ -637,20 +526,6 @@
637526 continue;
638527 }
639528 prev_md = md;
640
- }
641
-}
642
-
643
-static void __init get_systab_virt_addr(efi_memory_desc_t *md)
644
-{
645
- unsigned long size;
646
- u64 end, systab;
647
-
648
- size = md->num_pages << EFI_PAGE_SHIFT;
649
- end = md->phys_addr + size;
650
- systab = (u64)(unsigned long)efi_phys.systab;
651
- if (md->phys_addr <= systab && systab < end) {
652
- systab += md->virt_addr - md->phys_addr;
653
- efi.systab = (efi_system_table_t *)(unsigned long)systab;
654529 }
655530 }
656531
....@@ -707,7 +582,7 @@
707582 */
708583 static void *efi_map_next_entry(void *entry)
709584 {
710
- if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
585
+ if (efi_enabled(EFI_64BIT)) {
711586 /*
712587 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
713588 * config table feature requires us to map all entries
....@@ -756,10 +631,19 @@
756631 return false;
757632
758633 /*
634
+ * EFI specific purpose memory may be reserved by default
635
+ * depending on kernel config and boot options.
636
+ */
637
+ if (md->type == EFI_CONVENTIONAL_MEMORY &&
638
+ efi_soft_reserve_enabled() &&
639
+ (md->attribute & EFI_MEMORY_SP))
640
+ return false;
641
+
642
+ /*
759643 * Map all of RAM so that we can access arguments in the 1:1
760644 * mapping when making EFI runtime calls.
761645 */
762
- if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
646
+ if (efi_is_mixed()) {
763647 if (md->type == EFI_CONVENTIONAL_MEMORY ||
764648 md->type == EFI_LOADER_DATA ||
765649 md->type == EFI_LOADER_CODE)
....@@ -800,7 +684,6 @@
800684 continue;
801685
802686 efi_map_region(md);
803
- get_systab_virt_addr(md);
804687
805688 if (left < desc_size) {
806689 new_memmap = realloc_pages(new_memmap, *pg_shift);
....@@ -826,15 +709,11 @@
826709 efi_memory_desc_t *md;
827710 unsigned int num_pages;
828711
829
- efi.systab = NULL;
830
-
831712 /*
832713 * We don't do virtual mode, since we don't do runtime services, on
833
- * non-native EFI. With efi=old_map, we don't do runtime services in
834
- * kexec kernel because in the initial boot something else might
835
- * have been mapped at these virtual addresses.
714
+ * non-native EFI.
836715 */
837
- if (!efi_is_native() || efi_enabled(EFI_OLD_MEMMAP)) {
716
+ if (efi_is_mixed()) {
838717 efi_memmap_unmap();
839718 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
840719 return;
....@@ -850,10 +729,8 @@
850729 * Map efi regions which were passed via setup_data. The virt_addr is a
851730 * fixed addr which was used in first kernel of a kexec boot.
852731 */
853
- for_each_efi_memory_desc(md) {
732
+ for_each_efi_memory_desc(md)
854733 efi_map_region_fixed(md); /* FIXME: add error handling */
855
- get_systab_virt_addr(md);
856
- }
857734
858735 /*
859736 * Unregister the early EFI memmap from efi_init() and install
....@@ -868,8 +745,6 @@
868745 return;
869746 }
870747
871
- BUG_ON(!efi.systab);
872
-
873748 num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
874749 num_pages >>= PAGE_SHIFT;
875750
....@@ -879,21 +754,7 @@
879754 }
880755
881756 efi_sync_low_kernel_mappings();
882
-
883
- /*
884
- * Now that EFI is in virtual mode, update the function
885
- * pointers in the runtime service table to the new virtual addresses.
886
- *
887
- * Call EFI services through wrapper functions.
888
- */
889
- efi.runtime_version = efi_systab.hdr.revision;
890
-
891757 efi_native_runtime_setup();
892
-
893
- efi.set_virtual_address_map = NULL;
894
-
895
- if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
896
- runtime_code_page_mkexec();
897758 #endif
898759 }
899760
....@@ -902,12 +763,6 @@
902763 * Essentially, we look through the EFI memmap and map every region that
903764 * has the runtime attribute bit set in its memory descriptor into the
904765 * efi_pgd page table.
905
- *
906
- * The old method which used to update that memory descriptor with the
907
- * virtual address obtained from ioremap() is still supported when the
908
- * kernel is booted with efi=old_map on its command line. Same old
909
- * method enabled the runtime services to be called without having to
910
- * thunk back into physical mode for every invocation.
911766 *
912767 * The new method does a pagetable switch in a preemption-safe manner
913768 * so that we're in a different address space when calling a runtime
....@@ -925,8 +780,6 @@
925780 void *new_memmap = NULL;
926781 efi_status_t status;
927782 unsigned long pa;
928
-
929
- efi.systab = NULL;
930783
931784 if (efi_alloc_page_tables()) {
932785 pr_err("Failed to allocate EFI page tables\n");
....@@ -959,49 +812,29 @@
959812 efi_print_memmap();
960813 }
961814
962
- if (WARN_ON(!efi.systab))
963
- goto err;
964
-
965815 if (efi_setup_page_tables(pa, 1 << pg_shift))
966816 goto err;
967817
968818 efi_sync_low_kernel_mappings();
969819
970
- if (efi_is_native()) {
971
- status = phys_efi_set_virtual_address_map(
972
- efi.memmap.desc_size * count,
973
- efi.memmap.desc_size,
974
- efi.memmap.desc_version,
975
- (efi_memory_desc_t *)pa);
976
- } else {
977
- status = efi_thunk_set_virtual_address_map(
978
- efi_phys.set_virtual_address_map,
979
- efi.memmap.desc_size * count,
980
- efi.memmap.desc_size,
981
- efi.memmap.desc_version,
982
- (efi_memory_desc_t *)pa);
983
- }
984
-
820
+ status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
821
+ efi.memmap.desc_size,
822
+ efi.memmap.desc_version,
823
+ (efi_memory_desc_t *)pa,
824
+ efi_systab_phys);
985825 if (status != EFI_SUCCESS) {
986826 pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
987827 status);
988828 goto err;
989829 }
990830
991
- /*
992
- * Now that EFI is in virtual mode, update the function
993
- * pointers in the runtime service table to the new virtual addresses.
994
- *
995
- * Call EFI services through wrapper functions.
996
- */
997
- efi.runtime_version = efi_systab.hdr.revision;
831
+ efi_check_for_embedded_firmwares();
832
+ efi_free_boot_services();
998833
999
- if (efi_is_native())
834
+ if (!efi_is_mixed())
1000835 efi_native_runtime_setup();
1001836 else
1002837 efi_thunk_runtime_setup();
1003
-
1004
- efi.set_virtual_address_map = NULL;
1005838
1006839 /*
1007840 * Apply more restrictive page table mapping attributes now that
....@@ -1023,6 +856,8 @@
1023856 if (efi_enabled(EFI_PARAVIRT))
1024857 return;
1025858
859
+ efi.runtime = (efi_runtime_services_t *)efi_runtime;
860
+
1026861 if (efi_setup)
1027862 kexec_enter_virtual_mode();
1028863 else
....@@ -1031,16 +866,56 @@
1031866 efi_dump_pagetable();
1032867 }
1033868
1034
-static int __init arch_parse_efi_cmdline(char *str)
869
+bool efi_is_table_address(unsigned long phys_addr)
1035870 {
1036
- if (!str) {
1037
- pr_warn("need at least one option\n");
1038
- return -EINVAL;
1039
- }
871
+ unsigned int i;
1040872
1041
- if (parse_option_str(str, "old_map"))
1042
- set_bit(EFI_OLD_MEMMAP, &efi.flags);
873
+ if (phys_addr == EFI_INVALID_TABLE_ADDR)
874
+ return false;
1043875
1044
- return 0;
876
+ for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
877
+ if (*(efi_tables[i]) == phys_addr)
878
+ return true;
879
+
880
+ return false;
1045881 }
1046
-early_param("efi", arch_parse_efi_cmdline);
882
+
883
+char *efi_systab_show_arch(char *str)
884
+{
885
+ if (uga_phys != EFI_INVALID_TABLE_ADDR)
886
+ str += sprintf(str, "UGA=0x%lx\n", uga_phys);
887
+ return str;
888
+}
889
+
890
+#define EFI_FIELD(var) efi_ ## var
891
+
892
+#define EFI_ATTR_SHOW(name) \
893
+static ssize_t name##_show(struct kobject *kobj, \
894
+ struct kobj_attribute *attr, char *buf) \
895
+{ \
896
+ return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
897
+}
898
+
899
+EFI_ATTR_SHOW(fw_vendor);
900
+EFI_ATTR_SHOW(runtime);
901
+EFI_ATTR_SHOW(config_table);
902
+
903
+struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
904
+struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
905
+struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
906
+
907
+umode_t efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
908
+{
909
+ if (attr == &efi_attr_fw_vendor.attr) {
910
+ if (efi_enabled(EFI_PARAVIRT) ||
911
+ efi_fw_vendor == EFI_INVALID_TABLE_ADDR)
912
+ return 0;
913
+ } else if (attr == &efi_attr_runtime.attr) {
914
+ if (efi_runtime == EFI_INVALID_TABLE_ADDR)
915
+ return 0;
916
+ } else if (attr == &efi_attr_config_table.attr) {
917
+ if (efi_config_table == EFI_INVALID_TABLE_ADDR)
918
+ return 0;
919
+ }
920
+ return attr->mode;
921
+}