···2020 select GENERIC_ALLOCATOR2121 select GENERIC_ATOMIC64 if (CPU_V7M || CPU_V6 || !CPU_32v6K || !AEABI)2222 select GENERIC_CLOCKEVENTS_BROADCAST if SMP2323+ select GENERIC_EARLY_IOREMAP2324 select GENERIC_IDLE_POLL_SETUP2425 select GENERIC_IRQ_PROBE2526 select GENERIC_IRQ_SHOW···20602059 will be determined at run-time by masking the current IP with20612060 0xf8000000. This assumes the zImage being placed in the first 128MB20622061 from start of memory.20622062+20632063+config EFI_STUB20642064+ bool20652065+20662066+config EFI20672067+ bool "UEFI runtime support"20682068+ depends on OF && !CPU_BIG_ENDIAN && MMU && AUTO_ZRELADDR && !XIP_KERNEL20692069+ select UCS2_STRING20702070+ select EFI_PARAMS_FROM_FDT20712071+ select EFI_STUB20722072+ select EFI_ARMSTUB20732073+ select EFI_RUNTIME_WRAPPERS20742074+ ---help---20752075+ This option provides support for runtime services provided20762076+ by UEFI firmware (such as non-volatile variables, realtime20772077+ clock, and platform reset). A UEFI stub is also provided to20782078+ allow the kernel to be booted as an EFI application. This20792079+ is only useful for kernels that may run on systems that have20802080+ UEFI firmware.2063208120642082endmenu20652083
···11+/*22+ * Copyright (C) 2013-2015 Linaro Ltd33+ * Authors: Roy Franz <roy.franz@linaro.org>44+ * Ard Biesheuvel <ard.biesheuvel@linaro.org>55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ */1010+1111+ .macro __nop1212+#ifdef CONFIG_EFI_STUB1313+ @ This is almost but not quite a NOP, since it does clobber the1414+ @ condition flags. But it is the best we can do for EFI, since1515+ @ PE/COFF expects the magic string "MZ" at offset 0, while the1616+ @ ARM/Linux boot protocol expects an executable instruction1717+ @ there.1818+ .inst 'M' | ('Z' << 8) | (0x1310 << 16) @ tstne r0, #0x4d0001919+#else2020+ mov r0, r02121+#endif2222+ .endm2323+2424+ .macro __EFI_HEADER2525+#ifdef CONFIG_EFI_STUB2626+ b __efi_start2727+2828+ .set start_offset, __efi_start - start2929+ .org start + 0x3c3030+ @3131+ @ The PE header can be anywhere in the file, but for3232+ @ simplicity we keep it together with the MSDOS header3333+ @ The offset to the PE/COFF header needs to be at offset3434+ @ 0x3C in the MSDOS header.3535+ @ The only 2 fields of the MSDOS header that are used are this3636+ @ PE/COFF offset, and the "MZ" bytes at offset 0x0.3737+ @3838+ .long pe_header - start @ Offset to the PE header.3939+4040+pe_header:4141+ .ascii "PE\0\0"4242+4343+coff_header:4444+ .short 0x01c2 @ ARM or Thumb4545+ .short 2 @ nr_sections4646+ .long 0 @ TimeDateStamp4747+ .long 0 @ PointerToSymbolTable4848+ .long 1 @ NumberOfSymbols4949+ .short section_table - optional_header5050+ @ SizeOfOptionalHeader5151+ .short 0x306 @ Characteristics.5252+ @ IMAGE_FILE_32BIT_MACHINE |5353+ @ IMAGE_FILE_DEBUG_STRIPPED |5454+ @ IMAGE_FILE_EXECUTABLE_IMAGE |5555+ @ IMAGE_FILE_LINE_NUMS_STRIPPED5656+5757+optional_header:5858+ .short 0x10b @ PE32 format5959+ .byte 0x02 @ MajorLinkerVersion6060+ .byte 0x14 @ MinorLinkerVersion6161+ .long _end - __efi_start @ SizeOfCode6262+ .long 0 @ SizeOfInitializedData6363+ .long 0 @ SizeOfUninitializedData6464+ .long efi_stub_entry - start @ AddressOfEntryPoint6565+ .long start_offset @ BaseOfCode6666+ .long 0 @ data6767+6868+extra_header_fields:6969+ .long 0 @ ImageBase7070+ .long 0x200 @ SectionAlignment7171+ .long 0x200 @ FileAlignment7272+ .short 0 @ MajorOperatingSystemVersion7373+ .short 0 @ MinorOperatingSystemVersion7474+ .short 0 @ MajorImageVersion7575+ .short 0 @ MinorImageVersion7676+ .short 0 @ MajorSubsystemVersion7777+ .short 0 @ MinorSubsystemVersion7878+ .long 0 @ Win32VersionValue7979+8080+ .long _end - start @ SizeOfImage8181+ .long start_offset @ SizeOfHeaders8282+ .long 0 @ CheckSum8383+ .short 0xa @ Subsystem (EFI application)8484+ .short 0 @ DllCharacteristics8585+ .long 0 @ SizeOfStackReserve8686+ .long 0 @ SizeOfStackCommit8787+ .long 0 @ SizeOfHeapReserve8888+ .long 0 @ SizeOfHeapCommit8989+ .long 0 @ LoaderFlags9090+ .long 0x6 @ NumberOfRvaAndSizes9191+9292+ .quad 0 @ ExportTable9393+ .quad 0 @ ImportTable9494+ .quad 0 @ ResourceTable9595+ .quad 0 @ ExceptionTable9696+ .quad 0 @ CertificationTable9797+ .quad 0 @ BaseRelocationTable9898+9999+section_table:100100+ @101101+ @ The EFI application loader requires a relocation section102102+ @ because EFI applications must be relocatable. This is a103103+ @ dummy section as far as we are concerned.104104+ @105105+ .ascii ".reloc\0\0"106106+ .long 0 @ VirtualSize107107+ .long 0 @ VirtualAddress108108+ .long 0 @ SizeOfRawData109109+ .long 0 @ PointerToRawData110110+ .long 0 @ PointerToRelocations111111+ .long 0 @ PointerToLineNumbers112112+ .short 0 @ NumberOfRelocations113113+ .short 0 @ NumberOfLineNumbers114114+ .long 0x42100040 @ Characteristics115115+116116+ .ascii ".text\0\0\0"117117+ .long _end - __efi_start @ VirtualSize118118+ .long __efi_start @ VirtualAddress119119+ .long _edata - __efi_start @ SizeOfRawData120120+ .long __efi_start @ PointerToRawData121121+ .long 0 @ PointerToRelocations122122+ .long 0 @ PointerToLineNumbers123123+ .short 0 @ NumberOfRelocations124124+ .short 0 @ NumberOfLineNumbers125125+ .long 0xe0500020 @ Characteristics126126+127127+ .align 9128128+__efi_start:129129+#endif130130+ .endm
+52-2
arch/arm/boot/compressed/head.S
···1212#include <asm/assembler.h>1313#include <asm/v7m.h>14141515+#include "efi-header.S"1616+1517 AR_CLASS( .arch armv7-a )1618 M_CLASS( .arch armv7-m )1719···128126start:129127 .type start,#function130128 .rept 7131131- mov r0, r0129129+ __nop132130 .endr133131 ARM( mov r0, r0 )134132 ARM( b 1f )···141139 .word 0x04030201 @ endianness flag142140143141 THUMB( .thumb )144144-1:142142+1: __EFI_HEADER143143+145144 ARM_BE8( setend be ) @ go BE8 if compiled for BE8146145 AR_CLASS( mrs r9, cpsr )147146#ifdef CONFIG_ARM_VIRT_EXT···13551352 THUMB( bx r4 ) @ entry point is always ARM for A/R classes1356135313571354reloc_code_end:13551355+13561356+#ifdef CONFIG_EFI_STUB13571357+ .align 213581358+_start: .long start - .13591359+13601360+ENTRY(efi_stub_entry)13611361+ @ allocate space on stack for passing current zImage address13621362+ @ and for the EFI stub to return of new entry point of13631363+ @ zImage, as EFI stub may copy the kernel. Pointer address13641364+ @ is passed in r2. r0 and r1 are passed through from the13651365+ @ EFI firmware to efi_entry13661366+ adr ip, _start13671367+ ldr r3, [ip]13681368+ add r3, r3, ip13691369+ stmfd sp!, {r3, lr}13701370+ mov r2, sp @ pass zImage address in r213711371+ bl efi_entry13721372+13731373+ @ Check for error return from EFI stub. r0 has FDT address13741374+ @ or error code.13751375+ cmn r0, #113761376+ beq efi_load_fail13771377+13781378+ @ Preserve return value of efi_entry() in r413791379+ mov r4, r013801380+ bl cache_clean_flush13811381+ bl cache_off13821382+13831383+ @ Set parameters for booting zImage according to boot protocol13841384+ @ put FDT address in r2, it was returned by efi_entry()13851385+ @ r1 is the machine type, and r0 needs to be 013861386+ mov r0, #013871387+ mov r1, #0xFFFFFFFF13881388+ mov r2, r413891389+13901390+ @ Branch to (possibly) relocated zImage that is in [sp]13911391+ ldr lr, [sp]13921392+ ldr ip, =start_offset13931393+ add lr, lr, ip13941394+ mov pc, lr @ no mode switch13951395+13961396+efi_load_fail:13971397+ @ Return EFI_LOAD_ERROR to EFI firmware on error.13981398+ ldr r0, =0x8000000113991399+ ldmfd sp!, {ip, pc}14001400+ENDPROC(efi_stub_entry)14011401+#endif1358140213591403 .align13601404 .section ".stack", "aw", %nobits
+7
arch/arm/boot/compressed/vmlinux.lds.S
···4848 *(.rodata)4949 *(.rodata.*)5050 }5151+ .data : {5252+ /*5353+ * The EFI stub always executes from RAM, and runs strictly before the5454+ * decompressor, so we can make an exception for its r/w data, and keep it5555+ */5656+ *(.data.efistub)5757+ }5158 .piggydata : {5259 *(.piggydata)5360 }
···11+/*22+ * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ */88+99+#ifndef __ASM_ARM_EFI_H1010+#define __ASM_ARM_EFI_H1111+1212+#include <asm/cacheflush.h>1313+#include <asm/cachetype.h>1414+#include <asm/early_ioremap.h>1515+#include <asm/fixmap.h>1616+#include <asm/highmem.h>1717+#include <asm/mach/map.h>1818+#include <asm/mmu_context.h>1919+#include <asm/pgtable.h>2020+2121+#ifdef CONFIG_EFI2222+void efi_init(void);2323+2424+int efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md);2525+2626+#define efi_call_virt(f, ...) \2727+({ \2828+ efi_##f##_t *__f; \2929+ efi_status_t __s; \3030+ \3131+ efi_virtmap_load(); \3232+ __f = efi.systab->runtime->f; \3333+ __s = __f(__VA_ARGS__); \3434+ efi_virtmap_unload(); \3535+ __s; \3636+})3737+3838+#define __efi_call_virt(f, ...) \3939+({ \4040+ efi_##f##_t *__f; \4141+ \4242+ efi_virtmap_load(); \4343+ __f = efi.systab->runtime->f; \4444+ __f(__VA_ARGS__); \4545+ efi_virtmap_unload(); \4646+})4747+4848+static inline void efi_set_pgd(struct mm_struct *mm)4949+{5050+ check_and_switch_context(mm, NULL);5151+}5252+5353+void efi_virtmap_load(void);5454+void efi_virtmap_unload(void);5555+5656+#else5757+#define efi_init()5858+#endif /* CONFIG_EFI */5959+6060+/* arch specific definitions used by the stub code */6161+6262+#define efi_call_early(f, ...) sys_table_arg->boottime->f(__VA_ARGS__)6363+6464+/*6565+ * A reasonable upper bound for the uncompressed kernel size is 32 MBytes,6666+ * so we will reserve that amount of memory. We have no easy way to tell what6767+ * the actuall size of code + data the uncompressed kernel will use.6868+ * If this is insufficient, the decompressor will relocate itself out of the6969+ * way before performing the decompression.7070+ */7171+#define MAX_UNCOMP_KERNEL_SIZE SZ_32M7272+7373+/*7474+ * The kernel zImage should preferably be located between 32 MB and 128 MB7575+ * from the base of DRAM. The min address leaves space for a maximal size7676+ * uncompressed image, and the max address is due to how the zImage decompressor7777+ * picks a destination address.7878+ */7979+#define ZIMAGE_OFFSET_LIMIT SZ_128M8080+#define MIN_ZIMAGE_OFFSET MAX_UNCOMP_KERNEL_SIZE8181+#define MAX_FDT_OFFSET ZIMAGE_OFFSET_LIMIT8282+8383+#endif /* _ASM_ARM_EFI_H */
+28-1
arch/arm/include/asm/fixmap.h
···1919 FIX_TEXT_POKE0,2020 FIX_TEXT_POKE1,21212222- __end_of_fixed_addresses2222+ __end_of_fixmap_region,2323+2424+ /*2525+ * Share the kmap() region with early_ioremap(): this is guaranteed2626+ * not to clash since early_ioremap() is only available before2727+ * paging_init(), and kmap() only after.2828+ */2929+#define NR_FIX_BTMAPS 323030+#define FIX_BTMAPS_SLOTS 73131+#define TOTAL_FIX_BTMAPS (NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)3232+3333+ FIX_BTMAP_END = __end_of_permanent_fixed_addresses,3434+ FIX_BTMAP_BEGIN = FIX_BTMAP_END + TOTAL_FIX_BTMAPS - 1,3535+ __end_of_early_ioremap_region2336};3737+3838+static const enum fixed_addresses __end_of_fixed_addresses =3939+ __end_of_fixmap_region > __end_of_early_ioremap_region ?4040+ __end_of_fixmap_region : __end_of_early_ioremap_region;24412542#define FIXMAP_PAGE_COMMON (L_PTE_YOUNG | L_PTE_PRESENT | L_PTE_XN | L_PTE_DIRTY)26432744#define FIXMAP_PAGE_NORMAL (FIXMAP_PAGE_COMMON | L_PTE_MT_WRITEBACK)4545+#define FIXMAP_PAGE_RO (FIXMAP_PAGE_NORMAL | L_PTE_RDONLY)28462947/* Used by set_fixmap_(io|nocache), both meant for mapping a device */3048#define FIXMAP_PAGE_IO (FIXMAP_PAGE_COMMON | L_PTE_MT_DEV_SHARED | L_PTE_SHARED)3149#define FIXMAP_PAGE_NOCACHE FIXMAP_PAGE_IO5050+5151+#define __early_set_fixmap __set_fixmap5252+5353+#ifdef CONFIG_MMU32543355void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot);3456void __init early_fixmap_init(void);35573658#include <asm-generic/fixmap.h>37596060+#else6161+6262+static inline void early_fixmap_init(void) { }6363+6464+#endif3865#endif
+2
arch/arm/include/asm/mach/map.h
···4242extern void iotable_init(struct map_desc *, int);4343extern void vm_reserve_area_early(unsigned long addr, unsigned long size,4444 void *caller);4545+extern void create_mapping_late(struct mm_struct *mm, struct map_desc *md,4646+ bool ng);45474648#ifdef CONFIG_DEBUG_LL4749extern void debug_ll_addr(unsigned long *paddr, unsigned long *vaddr);
···11+/*22+ * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ */88+99+#include <linux/efi.h>1010+#include <asm/efi.h>1111+#include <asm/mach/map.h>1212+#include <asm/mmu_context.h>1313+1414+int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)1515+{1616+ struct map_desc desc = {1717+ .virtual = md->virt_addr,1818+ .pfn = __phys_to_pfn(md->phys_addr),1919+ .length = md->num_pages * EFI_PAGE_SIZE,2020+ };2121+2222+ /*2323+ * Order is important here: memory regions may have all of the2424+ * bits below set (and usually do), so we check them in order of2525+ * preference.2626+ */2727+ if (md->attribute & EFI_MEMORY_WB)2828+ desc.type = MT_MEMORY_RWX;2929+ else if (md->attribute & EFI_MEMORY_WT)3030+ desc.type = MT_MEMORY_RWX_NONCACHED;3131+ else if (md->attribute & EFI_MEMORY_WC)3232+ desc.type = MT_DEVICE_WC;3333+ else3434+ desc.type = MT_DEVICE;3535+3636+ create_mapping_late(mm, &desc, true);3737+ return 0;3838+}
+8-2
arch/arm/kernel/setup.c
···77 * it under the terms of the GNU General Public License version 2 as88 * published by the Free Software Foundation.99 */1010+#include <linux/efi.h>1011#include <linux/export.h>1112#include <linux/kernel.h>1213#include <linux/stddef.h>···3837#include <asm/cp15.h>3938#include <asm/cpu.h>4039#include <asm/cputype.h>4040+#include <asm/efi.h>4141#include <asm/elf.h>4242+#include <asm/early_ioremap.h>4243#include <asm/fixmap.h>4344#include <asm/procinfo.h>4445#include <asm/psci.h>···10261023 strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);10271024 *cmdline_p = cmd_line;1028102510291029- if (IS_ENABLED(CONFIG_FIX_EARLYCON_MEM))10301030- early_fixmap_init();10261026+ early_fixmap_init();10271027+ early_ioremap_init();1031102810321029 parse_early_param();10331030···10351032 early_paging_init(mdesc);10361033#endif10371034 setup_dma_zone(mdesc);10351035+ efi_init();10381036 sanity_check_meminfo();10391037 arm_memblock_init(mdesc);10381038+10391039+ early_ioremap_reset();1040104010411041 paging_init(mdesc);10421042 request_standard_resources(mdesc);
+4-1
arch/arm/mm/init.c
···192192#ifdef CONFIG_HAVE_ARCH_PFN_VALID193193int pfn_valid(unsigned long pfn)194194{195195- return memblock_is_memory(__pfn_to_phys(pfn));195195+ return memblock_is_map_memory(__pfn_to_phys(pfn));196196}197197EXPORT_SYMBOL(pfn_valid);198198#endif···431431432432 /* Ignore complete lowmem entries */433433 if (end <= max_low)434434+ continue;435435+436436+ if (memblock_is_nomap(mem))434437 continue;435438436439 /* Truncate partial highmem entries */
+9
arch/arm/mm/ioremap.c
···3030#include <asm/cp15.h>3131#include <asm/cputype.h>3232#include <asm/cacheflush.h>3333+#include <asm/early_ioremap.h>3334#include <asm/mmu_context.h>3435#include <asm/pgalloc.h>3536#include <asm/tlbflush.h>···470469}471470EXPORT_SYMBOL_GPL(pci_ioremap_io);472471#endif472472+473473+/*474474+ * Must be called after early_fixmap_init475475+ */476476+void __init early_ioremap_init(void)477477+{478478+ early_ioremap_setup();479479+}
+87-41
arch/arm/mm/mmu.c
···390390 * The early fixmap range spans multiple pmds, for which391391 * we are not prepared:392392 */393393- BUILD_BUG_ON((__fix_to_virt(__end_of_permanent_fixed_addresses) >> PMD_SHIFT)393393+ BUILD_BUG_ON((__fix_to_virt(__end_of_early_ioremap_region) >> PMD_SHIFT)394394 != FIXADDR_TOP >> PMD_SHIFT);395395396396 pmd = fixmap_pmd(FIXADDR_TOP);···724724 return early_alloc_aligned(sz, sz);725725}726726727727-static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr, unsigned long prot)727727+static void *__init late_alloc(unsigned long sz)728728+{729729+ void *ptr = (void *)__get_free_pages(PGALLOC_GFP, get_order(sz));730730+731731+ BUG_ON(!ptr);732732+ return ptr;733733+}734734+735735+static pte_t * __init pte_alloc(pmd_t *pmd, unsigned long addr,736736+ unsigned long prot,737737+ void *(*alloc)(unsigned long sz))728738{729739 if (pmd_none(*pmd)) {730730- pte_t *pte = early_alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);740740+ pte_t *pte = alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);731741 __pmd_populate(pmd, __pa(pte), prot);732742 }733743 BUG_ON(pmd_bad(*pmd));734744 return pte_offset_kernel(pmd, addr);735745}736746747747+static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,748748+ unsigned long prot)749749+{750750+ return pte_alloc(pmd, addr, prot, early_alloc);751751+}752752+737753static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,738754 unsigned long end, unsigned long pfn,739739- const struct mem_type *type)755755+ const struct mem_type *type,756756+ void *(*alloc)(unsigned long sz),757757+ bool ng)740758{741741- pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);759759+ pte_t *pte = pte_alloc(pmd, addr, type->prot_l1, alloc);742760 do {743743- set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);761761+ set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),762762+ ng ? PTE_EXT_NG : 0);744763 pfn++;745764 } while (pte++, addr += PAGE_SIZE, addr != end);746765}747766748767static void __init __map_init_section(pmd_t *pmd, unsigned long addr,749768 unsigned long end, phys_addr_t phys,750750- const struct mem_type *type)769769+ const struct mem_type *type, bool ng)751770{752771 pmd_t *p = pmd;753772···784765 pmd++;785766#endif786767 do {787787- *pmd = __pmd(phys | type->prot_sect);768768+ *pmd = __pmd(phys | type->prot_sect | (ng ? PMD_SECT_nG : 0));788769 phys += SECTION_SIZE;789770 } while (pmd++, addr += SECTION_SIZE, addr != end);790771···793774794775static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,795776 unsigned long end, phys_addr_t phys,796796- const struct mem_type *type)777777+ const struct mem_type *type,778778+ void *(*alloc)(unsigned long sz), bool ng)797779{798780 pmd_t *pmd = pmd_offset(pud, addr);799781 unsigned long next;···812792 */813793 if (type->prot_sect &&814794 ((addr | next | phys) & ~SECTION_MASK) == 0) {815815- __map_init_section(pmd, addr, next, phys, type);795795+ __map_init_section(pmd, addr, next, phys, type, ng);816796 } else {817797 alloc_init_pte(pmd, addr, next,818818- __phys_to_pfn(phys), type);798798+ __phys_to_pfn(phys), type, alloc, ng);819799 }820800821801 phys += next - addr;···825805826806static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,827807 unsigned long end, phys_addr_t phys,828828- const struct mem_type *type)808808+ const struct mem_type *type,809809+ void *(*alloc)(unsigned long sz), bool ng)829810{830811 pud_t *pud = pud_offset(pgd, addr);831812 unsigned long next;832813833814 do {834815 next = pud_addr_end(addr, end);835835- alloc_init_pmd(pud, addr, next, phys, type);816816+ alloc_init_pmd(pud, addr, next, phys, type, alloc, ng);836817 phys += next - addr;837818 } while (pud++, addr = next, addr != end);838819}839820840821#ifndef CONFIG_ARM_LPAE841841-static void __init create_36bit_mapping(struct map_desc *md,842842- const struct mem_type *type)822822+static void __init create_36bit_mapping(struct mm_struct *mm,823823+ struct map_desc *md,824824+ const struct mem_type *type,825825+ bool ng)843826{844827 unsigned long addr, length, end;845828 phys_addr_t phys;···882859 */883860 phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);884861885885- pgd = pgd_offset_k(addr);862862+ pgd = pgd_offset(mm, addr);886863 end = addr + length;887864 do {888865 pud_t *pud = pud_offset(pgd, addr);···890867 int i;891868892869 for (i = 0; i < 16; i++)893893- *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);870870+ *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER |871871+ (ng ? PMD_SECT_nG : 0));894872895873 addr += SUPERSECTION_SIZE;896874 phys += SUPERSECTION_SIZE;···900876}901877#endif /* !CONFIG_ARM_LPAE */902878903903-/*904904- * Create the page directory entries and any necessary905905- * page tables for the mapping specified by `md'. We906906- * are able to cope here with varying sizes and address907907- * offsets, and we take full advantage of sections and908908- * supersections.909909- */910910-static void __init create_mapping(struct map_desc *md)879879+static void __init __create_mapping(struct mm_struct *mm, struct map_desc *md,880880+ void *(*alloc)(unsigned long sz),881881+ bool ng)911882{912883 unsigned long addr, length, end;913884 phys_addr_t phys;914885 const struct mem_type *type;915886 pgd_t *pgd;916916-917917- if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {918918- pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",919919- (long long)__pfn_to_phys((u64)md->pfn), md->virtual);920920- return;921921- }922922-923923- if ((md->type == MT_DEVICE || md->type == MT_ROM) &&924924- md->virtual >= PAGE_OFFSET && md->virtual < FIXADDR_START &&925925- (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {926926- pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",927927- (long long)__pfn_to_phys((u64)md->pfn), md->virtual);928928- }929887930888 type = &mem_types[md->type];931889···916910 * Catch 36-bit addresses917911 */918912 if (md->pfn >= 0x100000) {919919- create_36bit_mapping(md, type);913913+ create_36bit_mapping(mm, md, type, ng);920914 return;921915 }922916#endif···931925 return;932926 }933927934934- pgd = pgd_offset_k(addr);928928+ pgd = pgd_offset(mm, addr);935929 end = addr + length;936930 do {937931 unsigned long next = pgd_addr_end(addr, end);938932939939- alloc_init_pud(pgd, addr, next, phys, type);933933+ alloc_init_pud(pgd, addr, next, phys, type, alloc, ng);940934941935 phys += next - addr;942936 addr = next;943937 } while (pgd++, addr != end);938938+}939939+940940+/*941941+ * Create the page directory entries and any necessary942942+ * page tables for the mapping specified by `md'. We943943+ * are able to cope here with varying sizes and address944944+ * offsets, and we take full advantage of sections and945945+ * supersections.946946+ */947947+static void __init create_mapping(struct map_desc *md)948948+{949949+ if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {950950+ pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",951951+ (long long)__pfn_to_phys((u64)md->pfn), md->virtual);952952+ return;953953+ }954954+955955+ if ((md->type == MT_DEVICE || md->type == MT_ROM) &&956956+ md->virtual >= PAGE_OFFSET && md->virtual < FIXADDR_START &&957957+ (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {958958+ pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",959959+ (long long)__pfn_to_phys((u64)md->pfn), md->virtual);960960+ }961961+962962+ __create_mapping(&init_mm, md, early_alloc, false);963963+}964964+965965+void __init create_mapping_late(struct mm_struct *mm, struct map_desc *md,966966+ bool ng)967967+{968968+#ifdef CONFIG_ARM_LPAE969969+ pud_t *pud = pud_alloc(mm, pgd_offset(mm, md->virtual), md->virtual);970970+ if (WARN_ON(!pud))971971+ return;972972+ pmd_alloc(mm, pud, 0);973973+#endif974974+ __create_mapping(mm, md, late_alloc, ng);944975}945976946977/*···14341391 phys_addr_t start = reg->base;14351392 phys_addr_t end = start + reg->size;14361393 struct map_desc map;13941394+13951395+ if (memblock_is_nomap(reg))13961396+ continue;1437139714381398 if (end > arm_lowmem_limit)14391399 end = arm_lowmem_limit;
+9
arch/arm64/include/asm/efi.h
···22#define _ASM_EFI_H3344#include <asm/io.h>55+#include <asm/mmu_context.h>56#include <asm/neon.h>77+#include <asm/tlbflush.h>6879#ifdef CONFIG_EFI810extern void efi_init(void);911#else1012#define efi_init()1113#endif1414+1515+int efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md);12161317#define efi_call_virt(f, ...) \1418({ \···6662 * into a private set of page tables. If this all succeeds, the Runtime6763 * Services are enabled and the EFI_RUNTIME_SERVICES bit set.6864 */6565+6666+static inline void efi_set_pgd(struct mm_struct *mm)6767+{6868+ switch_mm(NULL, mm, NULL);6969+}69707071void efi_virtmap_load(void);7172void efi_virtmap_unload(void);
+16-316
arch/arm64/kernel/efi.c
···1111 *1212 */13131414-#include <linux/atomic.h>1514#include <linux/dmi.h>1615#include <linux/efi.h>1717-#include <linux/export.h>1818-#include <linux/memblock.h>1919-#include <linux/mm_types.h>2020-#include <linux/bootmem.h>2121-#include <linux/of.h>2222-#include <linux/of_fdt.h>2323-#include <linux/preempt.h>2424-#include <linux/rbtree.h>2525-#include <linux/rwsem.h>2626-#include <linux/sched.h>2727-#include <linux/slab.h>2828-#include <linux/spinlock.h>1616+#include <linux/init.h>29173030-#include <asm/cacheflush.h>3118#include <asm/efi.h>3232-#include <asm/tlbflush.h>3333-#include <asm/mmu_context.h>3434-#include <asm/mmu.h>3535-#include <asm/pgtable.h>36193737-struct efi_memory_map memmap;3838-3939-static u64 efi_system_table;4040-4141-static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;4242-4343-static struct mm_struct efi_mm = {4444- .mm_rb = RB_ROOT,4545- .pgd = efi_pgd,4646- .mm_users = ATOMIC_INIT(2),4747- .mm_count = ATOMIC_INIT(1),4848- .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),4949- .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),5050- .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),5151-};5252-5353-static int __init is_normal_ram(efi_memory_desc_t *md)2020+int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)5421{5555- if (md->attribute & EFI_MEMORY_WB)5656- return 1;5757- return 0;5858-}5959-6060-/*6161- * Translate a EFI virtual address into a physical address: this is necessary,6262- * as some data members of the EFI system table are virtually remapped after6363- * SetVirtualAddressMap() has been called.6464- */6565-static phys_addr_t efi_to_phys(unsigned long addr)6666-{6767- efi_memory_desc_t *md;6868-6969- for_each_efi_memory_desc(&memmap, md) {7070- if (!(md->attribute & EFI_MEMORY_RUNTIME))7171- continue;7272- if (md->virt_addr == 0)7373- /* no virtual mapping has been installed by the stub */7474- break;7575- if (md->virt_addr <= addr &&7676- (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))7777- return md->phys_addr + addr - md->virt_addr;7878- }7979- return addr;8080-}8181-8282-static int __init uefi_init(void)8383-{8484- efi_char16_t *c16;8585- void *config_tables;8686- u64 table_size;8787- char vendor[100] = "unknown";8888- int i, retval;8989-9090- efi.systab = early_memremap(efi_system_table,9191- sizeof(efi_system_table_t));9292- if (efi.systab == NULL) {9393- pr_warn("Unable to map EFI system table.\n");9494- return -ENOMEM;9595- }9696-9797- set_bit(EFI_BOOT, &efi.flags);9898- set_bit(EFI_64BIT, &efi.flags);2222+ pteval_t prot_val;992310024 /*101101- * Verify the EFI Table2525+ * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be2626+ * executable, everything else can be mapped with the XN bits2727+ * set.10228 */103103- if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {104104- pr_err("System table signature incorrect\n");105105- retval = -EINVAL;106106- goto out;107107- }108108- if ((efi.systab->hdr.revision >> 16) < 2)109109- pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",110110- efi.systab->hdr.revision >> 16,111111- efi.systab->hdr.revision & 0xffff);2929+ if ((md->attribute & EFI_MEMORY_WB) == 0)3030+ prot_val = PROT_DEVICE_nGnRE;3131+ else if (md->type == EFI_RUNTIME_SERVICES_CODE ||3232+ !PAGE_ALIGNED(md->phys_addr))3333+ prot_val = pgprot_val(PAGE_KERNEL_EXEC);3434+ else3535+ prot_val = pgprot_val(PAGE_KERNEL);11236113113- /* Show what we know for posterity */114114- c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),115115- sizeof(vendor) * sizeof(efi_char16_t));116116- if (c16) {117117- for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)118118- vendor[i] = c16[i];119119- vendor[i] = '\0';120120- early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));121121- }122122-123123- pr_info("EFI v%u.%.02u by %s\n",124124- efi.systab->hdr.revision >> 16,125125- efi.systab->hdr.revision & 0xffff, vendor);126126-127127- table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;128128- config_tables = early_memremap(efi_to_phys(efi.systab->tables),129129- table_size);130130- if (config_tables == NULL) {131131- pr_warn("Unable to map EFI config table array.\n");132132- retval = -ENOMEM;133133- goto out;134134- }135135- retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,136136- sizeof(efi_config_table_64_t), NULL);137137-138138- early_memunmap(config_tables, table_size);139139-out:140140- early_memunmap(efi.systab, sizeof(efi_system_table_t));141141- return retval;142142-}143143-144144-/*145145- * Return true for RAM regions we want to permanently reserve.146146- */147147-static __init int is_reserve_region(efi_memory_desc_t *md)148148-{149149- switch (md->type) {150150- case EFI_LOADER_CODE:151151- case EFI_LOADER_DATA:152152- case EFI_BOOT_SERVICES_CODE:153153- case EFI_BOOT_SERVICES_DATA:154154- case EFI_CONVENTIONAL_MEMORY:155155- case EFI_PERSISTENT_MEMORY:156156- return 0;157157- default:158158- break;159159- }160160- return is_normal_ram(md);161161-}162162-163163-static __init void reserve_regions(void)164164-{165165- efi_memory_desc_t *md;166166- u64 paddr, npages, size;167167-168168- if (efi_enabled(EFI_DBG))169169- pr_info("Processing EFI memory map:\n");170170-171171- for_each_efi_memory_desc(&memmap, md) {172172- paddr = md->phys_addr;173173- npages = md->num_pages;174174-175175- if (efi_enabled(EFI_DBG)) {176176- char buf[64];177177-178178- pr_info(" 0x%012llx-0x%012llx %s",179179- paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,180180- efi_md_typeattr_format(buf, sizeof(buf), md));181181- }182182-183183- memrange_efi_to_native(&paddr, &npages);184184- size = npages << PAGE_SHIFT;185185-186186- if (is_normal_ram(md))187187- early_init_dt_add_memory_arch(paddr, size);188188-189189- if (is_reserve_region(md)) {190190- memblock_reserve(paddr, size);191191- if (efi_enabled(EFI_DBG))192192- pr_cont("*");193193- }194194-195195- if (efi_enabled(EFI_DBG))196196- pr_cont("\n");197197- }198198-199199- set_bit(EFI_MEMMAP, &efi.flags);200200-}201201-202202-void __init efi_init(void)203203-{204204- struct efi_fdt_params params;205205-206206- /* Grab UEFI information placed in FDT by stub */207207- if (!efi_get_fdt_params(¶ms))208208- return;209209-210210- efi_system_table = params.system_table;211211-212212- memblock_reserve(params.mmap & PAGE_MASK,213213- PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));214214- memmap.phys_map = params.mmap;215215- memmap.map = early_memremap(params.mmap, params.mmap_size);216216- if (memmap.map == NULL) {217217- /*218218- * If we are booting via UEFI, the UEFI memory map is the only219219- * description of memory we have, so there is little point in220220- * proceeding if we cannot access it.221221- */222222- panic("Unable to map EFI memory map.\n");223223- }224224- memmap.map_end = memmap.map + params.mmap_size;225225- memmap.desc_size = params.desc_size;226226- memmap.desc_version = params.desc_ver;227227-228228- if (uefi_init() < 0)229229- return;230230-231231- reserve_regions();232232- early_memunmap(memmap.map, params.mmap_size);233233-}234234-235235-static bool __init efi_virtmap_init(void)236236-{237237- efi_memory_desc_t *md;238238-239239- init_new_context(NULL, &efi_mm);240240-241241- for_each_efi_memory_desc(&memmap, md) {242242- pgprot_t prot;243243-244244- if (!(md->attribute & EFI_MEMORY_RUNTIME))245245- continue;246246- if (md->virt_addr == 0)247247- return false;248248-249249- pr_info(" EFI remap 0x%016llx => %p\n",250250- md->phys_addr, (void *)md->virt_addr);251251-252252- /*253253- * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be254254- * executable, everything else can be mapped with the XN bits255255- * set.256256- */257257- if (!is_normal_ram(md))258258- prot = __pgprot(PROT_DEVICE_nGnRE);259259- else if (md->type == EFI_RUNTIME_SERVICES_CODE ||260260- !PAGE_ALIGNED(md->phys_addr))261261- prot = PAGE_KERNEL_EXEC;262262- else263263- prot = PAGE_KERNEL;264264-265265- create_pgd_mapping(&efi_mm, md->phys_addr, md->virt_addr,266266- md->num_pages << EFI_PAGE_SHIFT, 267267- __pgprot(pgprot_val(prot) | PTE_NG));268268- }269269- return true;270270-}271271-272272-/*273273- * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,274274- * non-early mapping of the UEFI system table and virtual mappings for all275275- * EFI_MEMORY_RUNTIME regions.276276- */277277-static int __init arm64_enable_runtime_services(void)278278-{279279- u64 mapsize;280280-281281- if (!efi_enabled(EFI_BOOT)) {282282- pr_info("EFI services will not be available.\n");283283- return 0;284284- }285285-286286- if (efi_runtime_disabled()) {287287- pr_info("EFI runtime services will be disabled.\n");288288- return 0;289289- }290290-291291- pr_info("Remapping and enabling EFI services.\n");292292-293293- mapsize = memmap.map_end - memmap.map;294294- memmap.map = (__force void *)ioremap_cache(memmap.phys_map,295295- mapsize);296296- if (!memmap.map) {297297- pr_err("Failed to remap EFI memory map\n");298298- return -ENOMEM;299299- }300300- memmap.map_end = memmap.map + mapsize;301301- efi.memmap = &memmap;302302-303303- efi.systab = (__force void *)ioremap_cache(efi_system_table,304304- sizeof(efi_system_table_t));305305- if (!efi.systab) {306306- pr_err("Failed to remap EFI System Table\n");307307- return -ENOMEM;308308- }309309- set_bit(EFI_SYSTEM_TABLES, &efi.flags);310310-311311- if (!efi_virtmap_init()) {312312- pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");313313- return -ENOMEM;314314- }315315-316316- /* Set up runtime services function pointers */317317- efi_native_runtime_setup();318318- set_bit(EFI_RUNTIME_SERVICES, &efi.flags);319319-320320- efi.runtime_version = efi.systab->hdr.revision;321321-3737+ create_pgd_mapping(mm, md->phys_addr, md->virt_addr,3838+ md->num_pages << EFI_PAGE_SHIFT,3939+ __pgprot(prot_val | PTE_NG));32240 return 0;32341}324324-early_initcall(arm64_enable_runtime_services);3254232643static int __init arm64_dmi_init(void)32744{···53336 return 0;54337}55338core_initcall(arm64_dmi_init);5656-5757-static void efi_set_pgd(struct mm_struct *mm)5858-{5959- switch_mm(NULL, mm, NULL);6060-}6161-6262-void efi_virtmap_load(void)6363-{6464- preempt_disable();6565- efi_set_pgd(&efi_mm);6666-}6767-6868-void efi_virtmap_unload(void)6969-{7070- efi_set_pgd(current->active_mm);7171- preempt_enable();7272-}7333974340/*75341 * UpdateCapsule() depends on the system being shutdown via
···11+/*22+ * Extensible Firmware Interface33+ *44+ * Based on Extensible Firmware Interface Specification version 2.455+ *66+ * Copyright (C) 2013 - 2015 Linaro Ltd.77+ *88+ * This program is free software; you can redistribute it and/or modify99+ * it under the terms of the GNU General Public License version 2 as1010+ * published by the Free Software Foundation.1111+ *1212+ */1313+1414+#include <linux/efi.h>1515+#include <linux/init.h>1616+#include <linux/memblock.h>1717+#include <linux/mm_types.h>1818+#include <linux/of.h>1919+#include <linux/of_fdt.h>2020+2121+#include <asm/efi.h>2222+2323+struct efi_memory_map memmap;2424+2525+u64 efi_system_table;2626+2727+static int __init is_normal_ram(efi_memory_desc_t *md)2828+{2929+ if (md->attribute & EFI_MEMORY_WB)3030+ return 1;3131+ return 0;3232+}3333+3434+/*3535+ * Translate a EFI virtual address into a physical address: this is necessary,3636+ * as some data members of the EFI system table are virtually remapped after3737+ * SetVirtualAddressMap() has been called.3838+ */3939+static phys_addr_t efi_to_phys(unsigned long addr)4040+{4141+ efi_memory_desc_t *md;4242+4343+ for_each_efi_memory_desc(&memmap, md) {4444+ if (!(md->attribute & EFI_MEMORY_RUNTIME))4545+ continue;4646+ if (md->virt_addr == 0)4747+ /* no virtual mapping has been installed by the stub */4848+ break;4949+ if (md->virt_addr <= addr &&5050+ (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))5151+ return md->phys_addr + addr - md->virt_addr;5252+ }5353+ return addr;5454+}5555+5656+static int __init uefi_init(void)5757+{5858+ efi_char16_t *c16;5959+ void *config_tables;6060+ size_t table_size;6161+ char vendor[100] = "unknown";6262+ int i, retval;6363+6464+ efi.systab = early_memremap(efi_system_table,6565+ sizeof(efi_system_table_t));6666+ if (efi.systab == NULL) {6767+ pr_warn("Unable to map EFI system table.\n");6868+ return -ENOMEM;6969+ }7070+7171+ set_bit(EFI_BOOT, &efi.flags);7272+ if (IS_ENABLED(CONFIG_64BIT))7373+ set_bit(EFI_64BIT, &efi.flags);7474+7575+ /*7676+ * Verify the EFI Table7777+ */7878+ if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {7979+ pr_err("System table signature incorrect\n");8080+ retval = -EINVAL;8181+ goto out;8282+ }8383+ if ((efi.systab->hdr.revision >> 16) < 2)8484+ pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",8585+ efi.systab->hdr.revision >> 16,8686+ efi.systab->hdr.revision & 0xffff);8787+8888+ /* Show what we know for posterity */8989+ c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),9090+ sizeof(vendor) * sizeof(efi_char16_t));9191+ if (c16) {9292+ for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)9393+ vendor[i] = c16[i];9494+ vendor[i] = '\0';9595+ early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));9696+ }9797+9898+ pr_info("EFI v%u.%.02u by %s\n",9999+ efi.systab->hdr.revision >> 16,100100+ efi.systab->hdr.revision & 0xffff, vendor);101101+102102+ table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;103103+ config_tables = early_memremap(efi_to_phys(efi.systab->tables),104104+ table_size);105105+ if (config_tables == NULL) {106106+ pr_warn("Unable to map EFI config table array.\n");107107+ retval = -ENOMEM;108108+ goto out;109109+ }110110+ retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,111111+ sizeof(efi_config_table_t), NULL);112112+113113+ early_memunmap(config_tables, table_size);114114+out:115115+ early_memunmap(efi.systab, sizeof(efi_system_table_t));116116+ return retval;117117+}118118+119119+/*120120+ * Return true for RAM regions we want to permanently reserve.121121+ */122122+static __init int is_reserve_region(efi_memory_desc_t *md)123123+{124124+ switch (md->type) {125125+ case EFI_LOADER_CODE:126126+ case EFI_LOADER_DATA:127127+ case EFI_BOOT_SERVICES_CODE:128128+ case EFI_BOOT_SERVICES_DATA:129129+ case EFI_CONVENTIONAL_MEMORY:130130+ case EFI_PERSISTENT_MEMORY:131131+ return 0;132132+ default:133133+ break;134134+ }135135+ return is_normal_ram(md);136136+}137137+138138+static __init void reserve_regions(void)139139+{140140+ efi_memory_desc_t *md;141141+ u64 paddr, npages, size;142142+143143+ if (efi_enabled(EFI_DBG))144144+ pr_info("Processing EFI memory map:\n");145145+146146+ for_each_efi_memory_desc(&memmap, md) {147147+ paddr = md->phys_addr;148148+ npages = md->num_pages;149149+150150+ if (efi_enabled(EFI_DBG)) {151151+ char buf[64];152152+153153+ pr_info(" 0x%012llx-0x%012llx %s",154154+ paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,155155+ efi_md_typeattr_format(buf, sizeof(buf), md));156156+ }157157+158158+ memrange_efi_to_native(&paddr, &npages);159159+ size = npages << PAGE_SHIFT;160160+161161+ if (is_normal_ram(md))162162+ early_init_dt_add_memory_arch(paddr, size);163163+164164+ if (is_reserve_region(md)) {165165+ memblock_mark_nomap(paddr, size);166166+ if (efi_enabled(EFI_DBG))167167+ pr_cont("*");168168+ }169169+170170+ if (efi_enabled(EFI_DBG))171171+ pr_cont("\n");172172+ }173173+174174+ set_bit(EFI_MEMMAP, &efi.flags);175175+}176176+177177+void __init efi_init(void)178178+{179179+ struct efi_fdt_params params;180180+181181+ /* Grab UEFI information placed in FDT by stub */182182+ if (!efi_get_fdt_params(¶ms))183183+ return;184184+185185+ efi_system_table = params.system_table;186186+187187+ memmap.phys_map = params.mmap;188188+ memmap.map = early_memremap(params.mmap, params.mmap_size);189189+ if (memmap.map == NULL) {190190+ /*191191+ * If we are booting via UEFI, the UEFI memory map is the only192192+ * description of memory we have, so there is little point in193193+ * proceeding if we cannot access it.194194+ */195195+ panic("Unable to map EFI memory map.\n");196196+ }197197+ memmap.map_end = memmap.map + params.mmap_size;198198+ memmap.desc_size = params.desc_size;199199+ memmap.desc_version = params.desc_ver;200200+201201+ if (uefi_init() < 0)202202+ return;203203+204204+ reserve_regions();205205+ early_memunmap(memmap.map, params.mmap_size);206206+ memblock_mark_nomap(params.mmap & PAGE_MASK,207207+ PAGE_ALIGN(params.mmap_size +208208+ (params.mmap & ~PAGE_MASK)));209209+}
+135
drivers/firmware/efi/arm-runtime.c
···11+/*22+ * Extensible Firmware Interface33+ *44+ * Based on Extensible Firmware Interface Specification version 2.455+ *66+ * Copyright (C) 2013, 2014 Linaro Ltd.77+ *88+ * This program is free software; you can redistribute it and/or modify99+ * it under the terms of the GNU General Public License version 2 as1010+ * published by the Free Software Foundation.1111+ *1212+ */1313+1414+#include <linux/efi.h>1515+#include <linux/io.h>1616+#include <linux/memblock.h>1717+#include <linux/mm_types.h>1818+#include <linux/preempt.h>1919+#include <linux/rbtree.h>2020+#include <linux/rwsem.h>2121+#include <linux/sched.h>2222+#include <linux/slab.h>2323+#include <linux/spinlock.h>2424+2525+#include <asm/cacheflush.h>2626+#include <asm/efi.h>2727+#include <asm/mmu.h>2828+#include <asm/pgalloc.h>2929+#include <asm/pgtable.h>3030+3131+extern u64 efi_system_table;3232+3333+static struct mm_struct efi_mm = {3434+ .mm_rb = RB_ROOT,3535+ .mm_users = ATOMIC_INIT(2),3636+ .mm_count = ATOMIC_INIT(1),3737+ .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),3838+ .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),3939+ .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),4040+};4141+4242+static bool __init efi_virtmap_init(void)4343+{4444+ efi_memory_desc_t *md;4545+4646+ efi_mm.pgd = pgd_alloc(&efi_mm);4747+ init_new_context(NULL, &efi_mm);4848+4949+ for_each_efi_memory_desc(&memmap, md) {5050+ phys_addr_t phys = md->phys_addr;5151+ int ret;5252+5353+ if (!(md->attribute & EFI_MEMORY_RUNTIME))5454+ continue;5555+ if (md->virt_addr == 0)5656+ return false;5757+5858+ ret = efi_create_mapping(&efi_mm, md);5959+ if (!ret) {6060+ pr_info(" EFI remap %pa => %p\n",6161+ &phys, (void *)(unsigned long)md->virt_addr);6262+ } else {6363+ pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",6464+ &phys, ret);6565+ return false;6666+ }6767+ }6868+ return true;6969+}7070+7171+/*7272+ * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,7373+ * non-early mapping of the UEFI system table and virtual mappings for all7474+ * EFI_MEMORY_RUNTIME regions.7575+ */7676+static int __init arm_enable_runtime_services(void)7777+{7878+ u64 mapsize;7979+8080+ if (!efi_enabled(EFI_BOOT)) {8181+ pr_info("EFI services will not be available.\n");8282+ return 0;8383+ }8484+8585+ if (efi_runtime_disabled()) {8686+ pr_info("EFI runtime services will be disabled.\n");8787+ return 0;8888+ }8989+9090+ pr_info("Remapping and enabling EFI services.\n");9191+9292+ mapsize = memmap.map_end - memmap.map;9393+ memmap.map = (__force void *)ioremap_cache(memmap.phys_map,9494+ mapsize);9595+ if (!memmap.map) {9696+ pr_err("Failed to remap EFI memory map\n");9797+ return -ENOMEM;9898+ }9999+ memmap.map_end = memmap.map + mapsize;100100+ efi.memmap = &memmap;101101+102102+ efi.systab = (__force void *)ioremap_cache(efi_system_table,103103+ sizeof(efi_system_table_t));104104+ if (!efi.systab) {105105+ pr_err("Failed to remap EFI System Table\n");106106+ return -ENOMEM;107107+ }108108+ set_bit(EFI_SYSTEM_TABLES, &efi.flags);109109+110110+ if (!efi_virtmap_init()) {111111+ pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");112112+ return -ENOMEM;113113+ }114114+115115+ /* Set up runtime services function pointers */116116+ efi_native_runtime_setup();117117+ set_bit(EFI_RUNTIME_SERVICES, &efi.flags);118118+119119+ efi.runtime_version = efi.systab->hdr.revision;120120+121121+ return 0;122122+}123123+early_initcall(arm_enable_runtime_services);124124+125125+void efi_virtmap_load(void)126126+{127127+ preempt_disable();128128+ efi_set_pgd(&efi_mm);129129+}130130+131131+void efi_virtmap_unload(void)132132+{133133+ efi_set_pgd(current->active_mm);134134+ preempt_enable();135135+}
···3434lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o string.o \3535 $(patsubst %.c,lib-%.o,$(arm-deps))36363737+lib-$(CONFIG_ARM) += arm32-stub.o3738lib-$(CONFIG_ARM64) += arm64-stub.o3839CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET)3940···6867 $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y) \6968 && (echo >&2 "$@: absolute symbol references not allowed in the EFI stub"; \7069 rm -f $@; /bin/false); else /bin/false; fi7070+7171+#7272+# ARM discards the .data section because it disallows r/w data in the7373+# decompressor. So move our .data to .data.efistub, which is preserved7474+# explicitly by the decompressor linker script.7575+#7676+STUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub7777+STUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS
+3-1
drivers/firmware/efi/libstub/arm-stub.c
···303303 * The value chosen is the largest non-zero power of 2 suitable for this purpose304304 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can305305 * be mapped efficiently.306306+ * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,307307+ * map everything below 1 GB.306308 */307307-#define EFI_RT_VIRTUAL_BASE 0x40000000309309+#define EFI_RT_VIRTUAL_BASE SZ_512M308310309311static int cmp_mem_desc(const void *l, const void *r)310312{
+85
drivers/firmware/efi/libstub/arm32-stub.c
···11+/*22+ * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ *88+ */99+#include <linux/efi.h>1010+#include <asm/efi.h>1111+1212+efi_status_t handle_kernel_image(efi_system_table_t *sys_table,1313+ unsigned long *image_addr,1414+ unsigned long *image_size,1515+ unsigned long *reserve_addr,1616+ unsigned long *reserve_size,1717+ unsigned long dram_base,1818+ efi_loaded_image_t *image)1919+{2020+ unsigned long nr_pages;2121+ efi_status_t status;2222+ /* Use alloc_addr to tranlsate between types */2323+ efi_physical_addr_t alloc_addr;2424+2525+ /*2626+ * Verify that the DRAM base address is compatible with the ARM2727+ * boot protocol, which determines the base of DRAM by masking2828+ * off the low 27 bits of the address at which the zImage is2929+ * loaded. These assumptions are made by the decompressor,3030+ * before any memory map is available.3131+ */3232+ dram_base = round_up(dram_base, SZ_128M);3333+3434+ /*3535+ * Reserve memory for the uncompressed kernel image. This is3636+ * all that prevents any future allocations from conflicting3737+ * with the kernel. Since we can't tell from the compressed3838+ * image how much DRAM the kernel actually uses (due to BSS3939+ * size uncertainty) we allocate the maximum possible size.4040+ * Do this very early, as prints can cause memory allocations4141+ * that may conflict with this.4242+ */4343+ alloc_addr = dram_base;4444+ *reserve_size = MAX_UNCOMP_KERNEL_SIZE;4545+ nr_pages = round_up(*reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;4646+ status = sys_table->boottime->allocate_pages(EFI_ALLOCATE_ADDRESS,4747+ EFI_LOADER_DATA,4848+ nr_pages, &alloc_addr);4949+ if (status != EFI_SUCCESS) {5050+ *reserve_size = 0;5151+ pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");5252+ return status;5353+ }5454+ *reserve_addr = alloc_addr;5555+5656+ /*5757+ * Relocate the zImage, so that it appears in the lowest 128 MB5858+ * memory window.5959+ */6060+ *image_size = image->image_size;6161+ status = efi_relocate_kernel(sys_table, image_addr, *image_size,6262+ *image_size,6363+ dram_base + MAX_UNCOMP_KERNEL_SIZE, 0);6464+ if (status != EFI_SUCCESS) {6565+ pr_efi_err(sys_table, "Failed to relocate kernel.\n");6666+ efi_free(sys_table, *reserve_size, *reserve_addr);6767+ *reserve_size = 0;6868+ return status;6969+ }7070+7171+ /*7272+ * Check to see if we were able to allocate memory low enough7373+ * in memory. The kernel determines the base of DRAM from the7474+ * address at which the zImage is loaded.7575+ */7676+ if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {7777+ pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");7878+ efi_free(sys_table, *reserve_size, *reserve_addr);7979+ *reserve_size = 0;8080+ efi_free(sys_table, *image_size, *image_addr);8181+ *image_size = 0;8282+ return EFI_LOAD_ERROR;8383+ }8484+ return EFI_SUCCESS;8585+}
···822822 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);823823}824824825825+/**826826+ * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.827827+ * @base: the base phys addr of the region828828+ * @size: the size of the region829829+ *830830+ * Return 0 on success, -errno on failure.831831+ */832832+int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)833833+{834834+ return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);835835+}825836826837/**827838 * __next_reserved_mem_region - next function for for_each_reserved_region()···922911923912 /* if we want mirror memory skip non-mirror memory regions */924913 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))914914+ continue;915915+916916+ /* skip nomap memory unless we were asked for it explicitly */917917+ if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))925918 continue;926919927920 if (!type_b) {···1035102010361021 /* if we want mirror memory skip non-mirror memory regions */10371022 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))10231023+ continue;10241024+10251025+ /* skip nomap memory unless we were asked for it explicitly */10261026+ if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))10381027 continue;1039102810401029 if (!type_b) {···15361517int __init_memblock memblock_is_memory(phys_addr_t addr)15371518{15381519 return memblock_search(&memblock.memory, addr) != -1;15201520+}15211521+15221522+int __init_memblock memblock_is_map_memory(phys_addr_t addr)15231523+{15241524+ int i = memblock_search(&memblock.memory, addr);15251525+15261526+ if (i == -1)15271527+ return false;15281528+ return !memblock_is_nomap(&memblock.memory.regions[i]);15391529}1540153015411531#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP