Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Common EFI (Extensible Firmware Interface) support functions
0004  * Based on Extensible Firmware Interface Specification version 1.0
0005  *
0006  * Copyright (C) 1999 VA Linux Systems
0007  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
0008  * Copyright (C) 1999-2002 Hewlett-Packard Co.
0009  *  David Mosberger-Tang <davidm@hpl.hp.com>
0010  *  Stephane Eranian <eranian@hpl.hp.com>
0011  * Copyright (C) 2005-2008 Intel Co.
0012  *  Fenghua Yu <fenghua.yu@intel.com>
0013  *  Bibo Mao <bibo.mao@intel.com>
0014  *  Chandramouli Narayanan <mouli@linux.intel.com>
0015  *  Huang Ying <ying.huang@intel.com>
0016  * Copyright (C) 2013 SuSE Labs
0017  *  Borislav Petkov <bp@suse.de> - runtime services VA mapping
0018  *
0019  * Copied from efi_32.c to eliminate the duplicated code between EFI
0020  * 32/64 support code. --ying 2007-10-26
0021  *
0022  * All EFI Runtime Services are not implemented yet as EFI only
0023  * supports physical mode addressing on SoftSDV. This is to be fixed
0024  * in a future version.  --drummond 1999-07-20
0025  *
0026  * Implemented EFI runtime services and virtual mode calls.  --davidm
0027  *
0028  * Goutham Rao: <goutham.rao@intel.com>
0029  *  Skip non-WB memory and ignore empty memory ranges.
0030  */
0031 
0032 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0033 
0034 #include <linux/kernel.h>
0035 #include <linux/init.h>
0036 #include <linux/efi.h>
0037 #include <linux/efi-bgrt.h>
0038 #include <linux/export.h>
0039 #include <linux/memblock.h>
0040 #include <linux/slab.h>
0041 #include <linux/spinlock.h>
0042 #include <linux/uaccess.h>
0043 #include <linux/time.h>
0044 #include <linux/io.h>
0045 #include <linux/reboot.h>
0046 #include <linux/bcd.h>
0047 
0048 #include <asm/setup.h>
0049 #include <asm/efi.h>
0050 #include <asm/e820/api.h>
0051 #include <asm/time.h>
0052 #include <asm/tlbflush.h>
0053 #include <asm/x86_init.h>
0054 #include <asm/uv/uv.h>
0055 
0056 static unsigned long efi_systab_phys __initdata;
0057 static unsigned long prop_phys = EFI_INVALID_TABLE_ADDR;
0058 static unsigned long uga_phys = EFI_INVALID_TABLE_ADDR;
0059 static unsigned long efi_runtime, efi_nr_tables;
0060 
0061 unsigned long efi_fw_vendor, efi_config_table;
0062 
0063 static const efi_config_table_type_t arch_tables[] __initconst = {
0064     {EFI_PROPERTIES_TABLE_GUID, &prop_phys,     "PROP"      },
0065     {UGA_IO_PROTOCOL_GUID,      &uga_phys,      "UGA"       },
0066 #ifdef CONFIG_X86_UV
0067     {UV_SYSTEM_TABLE_GUID,      &uv_systab_phys,    "UVsystab"  },
0068 #endif
0069     {},
0070 };
0071 
0072 static const unsigned long * const efi_tables[] = {
0073     &efi.acpi,
0074     &efi.acpi20,
0075     &efi.smbios,
0076     &efi.smbios3,
0077     &uga_phys,
0078 #ifdef CONFIG_X86_UV
0079     &uv_systab_phys,
0080 #endif
0081     &efi_fw_vendor,
0082     &efi_runtime,
0083     &efi_config_table,
0084     &efi.esrt,
0085     &prop_phys,
0086     &efi_mem_attr_table,
0087 #ifdef CONFIG_EFI_RCI2_TABLE
0088     &rci2_table_phys,
0089 #endif
0090     &efi.tpm_log,
0091     &efi.tpm_final_log,
0092     &efi_rng_seed,
0093 #ifdef CONFIG_LOAD_UEFI_KEYS
0094     &efi.mokvar_table,
0095 #endif
0096 #ifdef CONFIG_EFI_COCO_SECRET
0097     &efi.coco_secret,
0098 #endif
0099 };
0100 
0101 u64 efi_setup;      /* efi setup_data physical address */
0102 
0103 static int add_efi_memmap __initdata;
0104 static int __init setup_add_efi_memmap(char *arg)
0105 {
0106     add_efi_memmap = 1;
0107     return 0;
0108 }
0109 early_param("add_efi_memmap", setup_add_efi_memmap);
0110 
0111 /*
0112  * Tell the kernel about the EFI memory map.  This might include
0113  * more than the max 128 entries that can fit in the passed in e820
0114  * legacy (zeropage) memory map, but the kernel's e820 table can hold
0115  * E820_MAX_ENTRIES.
0116  */
0117 
0118 static void __init do_add_efi_memmap(void)
0119 {
0120     efi_memory_desc_t *md;
0121 
0122     if (!efi_enabled(EFI_MEMMAP))
0123         return;
0124 
0125     for_each_efi_memory_desc(md) {
0126         unsigned long long start = md->phys_addr;
0127         unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
0128         int e820_type;
0129 
0130         switch (md->type) {
0131         case EFI_LOADER_CODE:
0132         case EFI_LOADER_DATA:
0133         case EFI_BOOT_SERVICES_CODE:
0134         case EFI_BOOT_SERVICES_DATA:
0135         case EFI_CONVENTIONAL_MEMORY:
0136             if (efi_soft_reserve_enabled()
0137                 && (md->attribute & EFI_MEMORY_SP))
0138                 e820_type = E820_TYPE_SOFT_RESERVED;
0139             else if (md->attribute & EFI_MEMORY_WB)
0140                 e820_type = E820_TYPE_RAM;
0141             else
0142                 e820_type = E820_TYPE_RESERVED;
0143             break;
0144         case EFI_ACPI_RECLAIM_MEMORY:
0145             e820_type = E820_TYPE_ACPI;
0146             break;
0147         case EFI_ACPI_MEMORY_NVS:
0148             e820_type = E820_TYPE_NVS;
0149             break;
0150         case EFI_UNUSABLE_MEMORY:
0151             e820_type = E820_TYPE_UNUSABLE;
0152             break;
0153         case EFI_PERSISTENT_MEMORY:
0154             e820_type = E820_TYPE_PMEM;
0155             break;
0156         default:
0157             /*
0158              * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
0159              * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
0160              * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
0161              */
0162             e820_type = E820_TYPE_RESERVED;
0163             break;
0164         }
0165 
0166         e820__range_add(start, size, e820_type);
0167     }
0168     e820__update_table(e820_table);
0169 }
0170 
0171 /*
0172  * Given add_efi_memmap defaults to 0 and there there is no alternative
0173  * e820 mechanism for soft-reserved memory, import the full EFI memory
0174  * map if soft reservations are present and enabled. Otherwise, the
0175  * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
0176  * the efi=nosoftreserve option.
0177  */
0178 static bool do_efi_soft_reserve(void)
0179 {
0180     efi_memory_desc_t *md;
0181 
0182     if (!efi_enabled(EFI_MEMMAP))
0183         return false;
0184 
0185     if (!efi_soft_reserve_enabled())
0186         return false;
0187 
0188     for_each_efi_memory_desc(md)
0189         if (md->type == EFI_CONVENTIONAL_MEMORY &&
0190             (md->attribute & EFI_MEMORY_SP))
0191             return true;
0192     return false;
0193 }
0194 
0195 int __init efi_memblock_x86_reserve_range(void)
0196 {
0197     struct efi_info *e = &boot_params.efi_info;
0198     struct efi_memory_map_data data;
0199     phys_addr_t pmap;
0200     int rv;
0201 
0202     if (efi_enabled(EFI_PARAVIRT))
0203         return 0;
0204 
0205     /* Can't handle firmware tables above 4GB on i386 */
0206     if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
0207         pr_err("Memory map is above 4GB, disabling EFI.\n");
0208         return -EINVAL;
0209     }
0210     pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
0211 
0212     data.phys_map       = pmap;
0213     data.size       = e->efi_memmap_size;
0214     data.desc_size      = e->efi_memdesc_size;
0215     data.desc_version   = e->efi_memdesc_version;
0216 
0217     rv = efi_memmap_init_early(&data);
0218     if (rv)
0219         return rv;
0220 
0221     if (add_efi_memmap || do_efi_soft_reserve())
0222         do_add_efi_memmap();
0223 
0224     efi_fake_memmap_early();
0225 
0226     WARN(efi.memmap.desc_version != 1,
0227          "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
0228          efi.memmap.desc_version);
0229 
0230     memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
0231     set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
0232 
0233     return 0;
0234 }
0235 
0236 #define OVERFLOW_ADDR_SHIFT (64 - EFI_PAGE_SHIFT)
0237 #define OVERFLOW_ADDR_MASK  (U64_MAX << OVERFLOW_ADDR_SHIFT)
0238 #define U64_HIGH_BIT        (~(U64_MAX >> 1))
0239 
0240 static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
0241 {
0242     u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
0243     u64 end_hi = 0;
0244     char buf[64];
0245 
0246     if (md->num_pages == 0) {
0247         end = 0;
0248     } else if (md->num_pages > EFI_PAGES_MAX ||
0249            EFI_PAGES_MAX - md->num_pages <
0250            (md->phys_addr >> EFI_PAGE_SHIFT)) {
0251         end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
0252             >> OVERFLOW_ADDR_SHIFT;
0253 
0254         if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
0255             end_hi += 1;
0256     } else {
0257         return true;
0258     }
0259 
0260     pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
0261 
0262     if (end_hi) {
0263         pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
0264             i, efi_md_typeattr_format(buf, sizeof(buf), md),
0265             md->phys_addr, end_hi, end);
0266     } else {
0267         pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
0268             i, efi_md_typeattr_format(buf, sizeof(buf), md),
0269             md->phys_addr, end);
0270     }
0271     return false;
0272 }
0273 
0274 static void __init efi_clean_memmap(void)
0275 {
0276     efi_memory_desc_t *out = efi.memmap.map;
0277     const efi_memory_desc_t *in = out;
0278     const efi_memory_desc_t *end = efi.memmap.map_end;
0279     int i, n_removal;
0280 
0281     for (i = n_removal = 0; in < end; i++) {
0282         if (efi_memmap_entry_valid(in, i)) {
0283             if (out != in)
0284                 memcpy(out, in, efi.memmap.desc_size);
0285             out = (void *)out + efi.memmap.desc_size;
0286         } else {
0287             n_removal++;
0288         }
0289         in = (void *)in + efi.memmap.desc_size;
0290     }
0291 
0292     if (n_removal > 0) {
0293         struct efi_memory_map_data data = {
0294             .phys_map   = efi.memmap.phys_map,
0295             .desc_version   = efi.memmap.desc_version,
0296             .desc_size  = efi.memmap.desc_size,
0297             .size       = efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
0298             .flags      = 0,
0299         };
0300 
0301         pr_warn("Removing %d invalid memory map entries.\n", n_removal);
0302         efi_memmap_install(&data);
0303     }
0304 }
0305 
0306 void __init efi_print_memmap(void)
0307 {
0308     efi_memory_desc_t *md;
0309     int i = 0;
0310 
0311     for_each_efi_memory_desc(md) {
0312         char buf[64];
0313 
0314         pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
0315             i++, efi_md_typeattr_format(buf, sizeof(buf), md),
0316             md->phys_addr,
0317             md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
0318             (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
0319     }
0320 }
0321 
0322 static int __init efi_systab_init(unsigned long phys)
0323 {
0324     int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
0325                       : sizeof(efi_system_table_32_t);
0326     const efi_table_hdr_t *hdr;
0327     bool over4g = false;
0328     void *p;
0329     int ret;
0330 
0331     hdr = p = early_memremap_ro(phys, size);
0332     if (p == NULL) {
0333         pr_err("Couldn't map the system table!\n");
0334         return -ENOMEM;
0335     }
0336 
0337     ret = efi_systab_check_header(hdr, 1);
0338     if (ret) {
0339         early_memunmap(p, size);
0340         return ret;
0341     }
0342 
0343     if (efi_enabled(EFI_64BIT)) {
0344         const efi_system_table_64_t *systab64 = p;
0345 
0346         efi_runtime = systab64->runtime;
0347         over4g      = systab64->runtime > U32_MAX;
0348 
0349         if (efi_setup) {
0350             struct efi_setup_data *data;
0351 
0352             data = early_memremap_ro(efi_setup, sizeof(*data));
0353             if (!data) {
0354                 early_memunmap(p, size);
0355                 return -ENOMEM;
0356             }
0357 
0358             efi_fw_vendor       = (unsigned long)data->fw_vendor;
0359             efi_config_table    = (unsigned long)data->tables;
0360 
0361             over4g |= data->fw_vendor   > U32_MAX ||
0362                   data->tables      > U32_MAX;
0363 
0364             early_memunmap(data, sizeof(*data));
0365         } else {
0366             efi_fw_vendor       = systab64->fw_vendor;
0367             efi_config_table    = systab64->tables;
0368 
0369             over4g |= systab64->fw_vendor   > U32_MAX ||
0370                   systab64->tables  > U32_MAX;
0371         }
0372         efi_nr_tables = systab64->nr_tables;
0373     } else {
0374         const efi_system_table_32_t *systab32 = p;
0375 
0376         efi_fw_vendor       = systab32->fw_vendor;
0377         efi_runtime     = systab32->runtime;
0378         efi_config_table    = systab32->tables;
0379         efi_nr_tables       = systab32->nr_tables;
0380     }
0381 
0382     efi.runtime_version = hdr->revision;
0383 
0384     efi_systab_report_header(hdr, efi_fw_vendor);
0385     early_memunmap(p, size);
0386 
0387     if (IS_ENABLED(CONFIG_X86_32) && over4g) {
0388         pr_err("EFI data located above 4GB, disabling EFI.\n");
0389         return -EINVAL;
0390     }
0391 
0392     return 0;
0393 }
0394 
0395 static int __init efi_config_init(const efi_config_table_type_t *arch_tables)
0396 {
0397     void *config_tables;
0398     int sz, ret;
0399 
0400     if (efi_nr_tables == 0)
0401         return 0;
0402 
0403     if (efi_enabled(EFI_64BIT))
0404         sz = sizeof(efi_config_table_64_t);
0405     else
0406         sz = sizeof(efi_config_table_32_t);
0407 
0408     /*
0409      * Let's see what config tables the firmware passed to us.
0410      */
0411     config_tables = early_memremap(efi_config_table, efi_nr_tables * sz);
0412     if (config_tables == NULL) {
0413         pr_err("Could not map Configuration table!\n");
0414         return -ENOMEM;
0415     }
0416 
0417     ret = efi_config_parse_tables(config_tables, efi_nr_tables,
0418                       arch_tables);
0419 
0420     early_memunmap(config_tables, efi_nr_tables * sz);
0421     return ret;
0422 }
0423 
0424 void __init efi_init(void)
0425 {
0426     if (IS_ENABLED(CONFIG_X86_32) &&
0427         (boot_params.efi_info.efi_systab_hi ||
0428          boot_params.efi_info.efi_memmap_hi)) {
0429         pr_info("Table located above 4GB, disabling EFI.\n");
0430         return;
0431     }
0432 
0433     efi_systab_phys = boot_params.efi_info.efi_systab |
0434               ((__u64)boot_params.efi_info.efi_systab_hi << 32);
0435 
0436     if (efi_systab_init(efi_systab_phys))
0437         return;
0438 
0439     if (efi_reuse_config(efi_config_table, efi_nr_tables))
0440         return;
0441 
0442     if (efi_config_init(arch_tables))
0443         return;
0444 
0445     /*
0446      * Note: We currently don't support runtime services on an EFI
0447      * that doesn't match the kernel 32/64-bit mode.
0448      */
0449 
0450     if (!efi_runtime_supported())
0451         pr_err("No EFI runtime due to 32/64-bit mismatch with kernel\n");
0452 
0453     if (!efi_runtime_supported() || efi_runtime_disabled()) {
0454         efi_memmap_unmap();
0455         return;
0456     }
0457 
0458     /* Parse the EFI Properties table if it exists */
0459     if (prop_phys != EFI_INVALID_TABLE_ADDR) {
0460         efi_properties_table_t *tbl;
0461 
0462         tbl = early_memremap_ro(prop_phys, sizeof(*tbl));
0463         if (tbl == NULL) {
0464             pr_err("Could not map Properties table!\n");
0465         } else {
0466             if (tbl->memory_protection_attribute &
0467                 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
0468                 set_bit(EFI_NX_PE_DATA, &efi.flags);
0469 
0470             early_memunmap(tbl, sizeof(*tbl));
0471         }
0472     }
0473 
0474     set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0475     efi_clean_memmap();
0476 
0477     if (efi_enabled(EFI_DBG))
0478         efi_print_memmap();
0479 }
0480 
0481 /* Merge contiguous regions of the same type and attribute */
0482 static void __init efi_merge_regions(void)
0483 {
0484     efi_memory_desc_t *md, *prev_md = NULL;
0485 
0486     for_each_efi_memory_desc(md) {
0487         u64 prev_size;
0488 
0489         if (!prev_md) {
0490             prev_md = md;
0491             continue;
0492         }
0493 
0494         if (prev_md->type != md->type ||
0495             prev_md->attribute != md->attribute) {
0496             prev_md = md;
0497             continue;
0498         }
0499 
0500         prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
0501 
0502         if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
0503             prev_md->num_pages += md->num_pages;
0504             md->type = EFI_RESERVED_TYPE;
0505             md->attribute = 0;
0506             continue;
0507         }
0508         prev_md = md;
0509     }
0510 }
0511 
0512 static void *realloc_pages(void *old_memmap, int old_shift)
0513 {
0514     void *ret;
0515 
0516     ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
0517     if (!ret)
0518         goto out;
0519 
0520     /*
0521      * A first-time allocation doesn't have anything to copy.
0522      */
0523     if (!old_memmap)
0524         return ret;
0525 
0526     memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
0527 
0528 out:
0529     free_pages((unsigned long)old_memmap, old_shift);
0530     return ret;
0531 }
0532 
0533 /*
0534  * Iterate the EFI memory map in reverse order because the regions
0535  * will be mapped top-down. The end result is the same as if we had
0536  * mapped things forward, but doesn't require us to change the
0537  * existing implementation of efi_map_region().
0538  */
0539 static inline void *efi_map_next_entry_reverse(void *entry)
0540 {
0541     /* Initial call */
0542     if (!entry)
0543         return efi.memmap.map_end - efi.memmap.desc_size;
0544 
0545     entry -= efi.memmap.desc_size;
0546     if (entry < efi.memmap.map)
0547         return NULL;
0548 
0549     return entry;
0550 }
0551 
0552 /*
0553  * efi_map_next_entry - Return the next EFI memory map descriptor
0554  * @entry: Previous EFI memory map descriptor
0555  *
0556  * This is a helper function to iterate over the EFI memory map, which
0557  * we do in different orders depending on the current configuration.
0558  *
0559  * To begin traversing the memory map @entry must be %NULL.
0560  *
0561  * Returns %NULL when we reach the end of the memory map.
0562  */
0563 static void *efi_map_next_entry(void *entry)
0564 {
0565     if (efi_enabled(EFI_64BIT)) {
0566         /*
0567          * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
0568          * config table feature requires us to map all entries
0569          * in the same order as they appear in the EFI memory
0570          * map. That is to say, entry N must have a lower
0571          * virtual address than entry N+1. This is because the
0572          * firmware toolchain leaves relative references in
0573          * the code/data sections, which are split and become
0574          * separate EFI memory regions. Mapping things
0575          * out-of-order leads to the firmware accessing
0576          * unmapped addresses.
0577          *
0578          * Since we need to map things this way whether or not
0579          * the kernel actually makes use of
0580          * EFI_PROPERTIES_TABLE, let's just switch to this
0581          * scheme by default for 64-bit.
0582          */
0583         return efi_map_next_entry_reverse(entry);
0584     }
0585 
0586     /* Initial call */
0587     if (!entry)
0588         return efi.memmap.map;
0589 
0590     entry += efi.memmap.desc_size;
0591     if (entry >= efi.memmap.map_end)
0592         return NULL;
0593 
0594     return entry;
0595 }
0596 
0597 static bool should_map_region(efi_memory_desc_t *md)
0598 {
0599     /*
0600      * Runtime regions always require runtime mappings (obviously).
0601      */
0602     if (md->attribute & EFI_MEMORY_RUNTIME)
0603         return true;
0604 
0605     /*
0606      * 32-bit EFI doesn't suffer from the bug that requires us to
0607      * reserve boot services regions, and mixed mode support
0608      * doesn't exist for 32-bit kernels.
0609      */
0610     if (IS_ENABLED(CONFIG_X86_32))
0611         return false;
0612 
0613     /*
0614      * EFI specific purpose memory may be reserved by default
0615      * depending on kernel config and boot options.
0616      */
0617     if (md->type == EFI_CONVENTIONAL_MEMORY &&
0618         efi_soft_reserve_enabled() &&
0619         (md->attribute & EFI_MEMORY_SP))
0620         return false;
0621 
0622     /*
0623      * Map all of RAM so that we can access arguments in the 1:1
0624      * mapping when making EFI runtime calls.
0625      */
0626     if (efi_is_mixed()) {
0627         if (md->type == EFI_CONVENTIONAL_MEMORY ||
0628             md->type == EFI_LOADER_DATA ||
0629             md->type == EFI_LOADER_CODE)
0630             return true;
0631     }
0632 
0633     /*
0634      * Map boot services regions as a workaround for buggy
0635      * firmware that accesses them even when they shouldn't.
0636      *
0637      * See efi_{reserve,free}_boot_services().
0638      */
0639     if (md->type == EFI_BOOT_SERVICES_CODE ||
0640         md->type == EFI_BOOT_SERVICES_DATA)
0641         return true;
0642 
0643     return false;
0644 }
0645 
0646 /*
0647  * Map the efi memory ranges of the runtime services and update new_mmap with
0648  * virtual addresses.
0649  */
0650 static void * __init efi_map_regions(int *count, int *pg_shift)
0651 {
0652     void *p, *new_memmap = NULL;
0653     unsigned long left = 0;
0654     unsigned long desc_size;
0655     efi_memory_desc_t *md;
0656 
0657     desc_size = efi.memmap.desc_size;
0658 
0659     p = NULL;
0660     while ((p = efi_map_next_entry(p))) {
0661         md = p;
0662 
0663         if (!should_map_region(md))
0664             continue;
0665 
0666         efi_map_region(md);
0667 
0668         if (left < desc_size) {
0669             new_memmap = realloc_pages(new_memmap, *pg_shift);
0670             if (!new_memmap)
0671                 return NULL;
0672 
0673             left += PAGE_SIZE << *pg_shift;
0674             (*pg_shift)++;
0675         }
0676 
0677         memcpy(new_memmap + (*count * desc_size), md, desc_size);
0678 
0679         left -= desc_size;
0680         (*count)++;
0681     }
0682 
0683     return new_memmap;
0684 }
0685 
0686 static void __init kexec_enter_virtual_mode(void)
0687 {
0688 #ifdef CONFIG_KEXEC_CORE
0689     efi_memory_desc_t *md;
0690     unsigned int num_pages;
0691 
0692     /*
0693      * We don't do virtual mode, since we don't do runtime services, on
0694      * non-native EFI.
0695      */
0696     if (efi_is_mixed()) {
0697         efi_memmap_unmap();
0698         clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0699         return;
0700     }
0701 
0702     if (efi_alloc_page_tables()) {
0703         pr_err("Failed to allocate EFI page tables\n");
0704         clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0705         return;
0706     }
0707 
0708     /*
0709     * Map efi regions which were passed via setup_data. The virt_addr is a
0710     * fixed addr which was used in first kernel of a kexec boot.
0711     */
0712     for_each_efi_memory_desc(md)
0713         efi_map_region_fixed(md); /* FIXME: add error handling */
0714 
0715     /*
0716      * Unregister the early EFI memmap from efi_init() and install
0717      * the new EFI memory map.
0718      */
0719     efi_memmap_unmap();
0720 
0721     if (efi_memmap_init_late(efi.memmap.phys_map,
0722                  efi.memmap.desc_size * efi.memmap.nr_map)) {
0723         pr_err("Failed to remap late EFI memory map\n");
0724         clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0725         return;
0726     }
0727 
0728     num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
0729     num_pages >>= PAGE_SHIFT;
0730 
0731     if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
0732         clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0733         return;
0734     }
0735 
0736     efi_sync_low_kernel_mappings();
0737     efi_native_runtime_setup();
0738 #endif
0739 }
0740 
0741 /*
0742  * This function will switch the EFI runtime services to virtual mode.
0743  * Essentially, we look through the EFI memmap and map every region that
0744  * has the runtime attribute bit set in its memory descriptor into the
0745  * efi_pgd page table.
0746  *
0747  * The new method does a pagetable switch in a preemption-safe manner
0748  * so that we're in a different address space when calling a runtime
0749  * function. For function arguments passing we do copy the PUDs of the
0750  * kernel page table into efi_pgd prior to each call.
0751  *
0752  * Specially for kexec boot, efi runtime maps in previous kernel should
0753  * be passed in via setup_data. In that case runtime ranges will be mapped
0754  * to the same virtual addresses as the first kernel, see
0755  * kexec_enter_virtual_mode().
0756  */
0757 static void __init __efi_enter_virtual_mode(void)
0758 {
0759     int count = 0, pg_shift = 0;
0760     void *new_memmap = NULL;
0761     efi_status_t status;
0762     unsigned long pa;
0763 
0764     if (efi_alloc_page_tables()) {
0765         pr_err("Failed to allocate EFI page tables\n");
0766         goto err;
0767     }
0768 
0769     efi_merge_regions();
0770     new_memmap = efi_map_regions(&count, &pg_shift);
0771     if (!new_memmap) {
0772         pr_err("Error reallocating memory, EFI runtime non-functional!\n");
0773         goto err;
0774     }
0775 
0776     pa = __pa(new_memmap);
0777 
0778     /*
0779      * Unregister the early EFI memmap from efi_init() and install
0780      * the new EFI memory map that we are about to pass to the
0781      * firmware via SetVirtualAddressMap().
0782      */
0783     efi_memmap_unmap();
0784 
0785     if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
0786         pr_err("Failed to remap late EFI memory map\n");
0787         goto err;
0788     }
0789 
0790     if (efi_enabled(EFI_DBG)) {
0791         pr_info("EFI runtime memory map:\n");
0792         efi_print_memmap();
0793     }
0794 
0795     if (efi_setup_page_tables(pa, 1 << pg_shift))
0796         goto err;
0797 
0798     efi_sync_low_kernel_mappings();
0799 
0800     status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
0801                          efi.memmap.desc_size,
0802                          efi.memmap.desc_version,
0803                          (efi_memory_desc_t *)pa,
0804                          efi_systab_phys);
0805     if (status != EFI_SUCCESS) {
0806         pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
0807                status);
0808         goto err;
0809     }
0810 
0811     efi_check_for_embedded_firmwares();
0812     efi_free_boot_services();
0813 
0814     if (!efi_is_mixed())
0815         efi_native_runtime_setup();
0816     else
0817         efi_thunk_runtime_setup();
0818 
0819     /*
0820      * Apply more restrictive page table mapping attributes now that
0821      * SVAM() has been called and the firmware has performed all
0822      * necessary relocation fixups for the new virtual addresses.
0823      */
0824     efi_runtime_update_mappings();
0825 
0826     /* clean DUMMY object */
0827     efi_delete_dummy_variable();
0828     return;
0829 
0830 err:
0831     clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0832 }
0833 
0834 void __init efi_enter_virtual_mode(void)
0835 {
0836     if (efi_enabled(EFI_PARAVIRT))
0837         return;
0838 
0839     efi.runtime = (efi_runtime_services_t *)efi_runtime;
0840 
0841     if (efi_setup)
0842         kexec_enter_virtual_mode();
0843     else
0844         __efi_enter_virtual_mode();
0845 
0846     efi_dump_pagetable();
0847 }
0848 
0849 bool efi_is_table_address(unsigned long phys_addr)
0850 {
0851     unsigned int i;
0852 
0853     if (phys_addr == EFI_INVALID_TABLE_ADDR)
0854         return false;
0855 
0856     for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
0857         if (*(efi_tables[i]) == phys_addr)
0858             return true;
0859 
0860     return false;
0861 }
0862 
0863 char *efi_systab_show_arch(char *str)
0864 {
0865     if (uga_phys != EFI_INVALID_TABLE_ADDR)
0866         str += sprintf(str, "UGA=0x%lx\n", uga_phys);
0867     return str;
0868 }
0869 
0870 #define EFI_FIELD(var) efi_ ## var
0871 
0872 #define EFI_ATTR_SHOW(name) \
0873 static ssize_t name##_show(struct kobject *kobj, \
0874                 struct kobj_attribute *attr, char *buf) \
0875 { \
0876     return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
0877 }
0878 
0879 EFI_ATTR_SHOW(fw_vendor);
0880 EFI_ATTR_SHOW(runtime);
0881 EFI_ATTR_SHOW(config_table);
0882 
0883 struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
0884 struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
0885 struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
0886 
0887 umode_t efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
0888 {
0889     if (attr == &efi_attr_fw_vendor.attr) {
0890         if (efi_enabled(EFI_PARAVIRT) ||
0891                 efi_fw_vendor == EFI_INVALID_TABLE_ADDR)
0892             return 0;
0893     } else if (attr == &efi_attr_runtime.attr) {
0894         if (efi_runtime == EFI_INVALID_TABLE_ADDR)
0895             return 0;
0896     } else if (attr == &efi_attr_config_table.attr) {
0897         if (efi_config_table == EFI_INVALID_TABLE_ADDR)
0898             return 0;
0899     }
0900     return attr->mode;
0901 }