2008-01-30 20:31:19 +08:00
|
|
|
/*
|
|
|
|
* x86_64 specific EFI support functions
|
|
|
|
* Based on Extensible Firmware Interface Specification version 1.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Intel Co.
|
|
|
|
* Fenghua Yu <fenghua.yu@intel.com>
|
|
|
|
* Bibo Mao <bibo.mao@intel.com>
|
|
|
|
* Chandramouli Narayanan <mouli@linux.intel.com>
|
|
|
|
* Huang Ying <ying.huang@intel.com>
|
|
|
|
*
|
|
|
|
* Code to convert EFI to E820 map has been implemented in elilo bootloader
|
|
|
|
* based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
|
|
|
|
* is setup appropriately for EFI runtime code.
|
|
|
|
* - mouli 06/14/2007.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/bootmem.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/reboot.h>
|
2013-04-12 06:51:01 +08:00
|
|
|
#include <linux/slab.h>
|
2008-01-30 20:31:19 +08:00
|
|
|
|
|
|
|
#include <asm/setup.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/e820.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm/efi.h>
|
2008-02-13 17:22:41 +08:00
|
|
|
#include <asm/cacheflush.h>
|
2009-01-23 10:03:29 +08:00
|
|
|
#include <asm/fixmap.h>
|
2013-11-01 00:25:08 +08:00
|
|
|
#include <asm/realmode.h>
|
2008-01-30 20:31:19 +08:00
|
|
|
|
2013-01-08 23:02:43 +08:00
|
|
|
static pgd_t *save_pgd __initdata;
|
2008-01-30 20:31:19 +08:00
|
|
|
static unsigned long efi_flags __initdata;
|
|
|
|
|
2013-11-01 00:25:08 +08:00
|
|
|
/*
|
|
|
|
* We allocate runtime services regions bottom-up, starting from -4G, i.e.
|
|
|
|
* 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
|
|
|
|
*/
|
|
|
|
static u64 efi_va = -4 * (1UL << 30);
|
|
|
|
#define EFI_VA_END (-68 * (1UL << 30))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scratch space used for switching the pagetable in the EFI stub
|
|
|
|
*/
|
|
|
|
struct efi_scratch {
|
|
|
|
u64 r15;
|
|
|
|
u64 prev_cr3;
|
|
|
|
pgd_t *efi_pgt;
|
|
|
|
bool use_pgd;
|
|
|
|
};
|
|
|
|
|
2011-05-06 03:19:43 +08:00
|
|
|
static void __init early_code_mapping_set_exec(int executable)
|
2008-01-30 20:31:19 +08:00
|
|
|
{
|
|
|
|
efi_memory_desc_t *md;
|
|
|
|
void *p;
|
|
|
|
|
2008-01-30 20:33:55 +08:00
|
|
|
if (!(__supported_pte_mask & _PAGE_NX))
|
|
|
|
return;
|
|
|
|
|
x86, efi: Retain boot service code until after switching to virtual mode
UEFI stands for "Unified Extensible Firmware Interface", where "Firmware"
is an ancient African word meaning "Why do something right when you can
do it so wrong that children will weep and brave adults will cower before
you", and "UEI" is Celtic for "We missed DOS so we burned it into your
ROMs". The UEFI specification provides for runtime services (ie, another
way for the operating system to be forced to depend on the firmware) and
we rely on these for certain trivial tasks such as setting up the
bootloader. But some hardware fails to work if we attempt to use these
runtime services from physical mode, and so we have to switch into virtual
mode. So far so dreadful.
The specification makes it clear that the operating system is free to do
whatever it wants with boot services code after ExitBootServices() has been
called. SetVirtualAddressMap() can't be called until ExitBootServices() has
been. So, obviously, a whole bunch of EFI implementations call into boot
services code when we do that. Since we've been charmingly naive and
trusted that the specification may be somehow relevant to the real world,
we've already stuffed a picture of a penguin or something in that address
space. And just to make things more entertaining, we've also marked it
non-executable.
This patch allocates the boot services regions during EFI init and makes
sure that they're executable. Then, after SetVirtualAddressMap(), it
discards them and everyone lives happily ever after. Except for the ones
who have to work on EFI, who live sad lives haunted by the knowledge that
someone's eventually going to write yet another firmware specification.
[ hpa: adding this to urgent with a stable tag since it fixes currently-broken
hardware. However, I do not know what the dependencies are and so I do
not know which -stable versions this may be a candidate for. ]
Signed-off-by: Matthew Garrett <mjg@redhat.com>
Link: http://lkml.kernel.org/r/1306331593-28715-1-git-send-email-mjg@redhat.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: <stable@kernel.org>
2011-05-25 21:53:13 +08:00
|
|
|
/* Make EFI service code area executable */
|
2008-01-30 20:31:19 +08:00
|
|
|
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
|
|
|
|
md = p;
|
x86, efi: Retain boot service code until after switching to virtual mode
UEFI stands for "Unified Extensible Firmware Interface", where "Firmware"
is an ancient African word meaning "Why do something right when you can
do it so wrong that children will weep and brave adults will cower before
you", and "UEI" is Celtic for "We missed DOS so we burned it into your
ROMs". The UEFI specification provides for runtime services (ie, another
way for the operating system to be forced to depend on the firmware) and
we rely on these for certain trivial tasks such as setting up the
bootloader. But some hardware fails to work if we attempt to use these
runtime services from physical mode, and so we have to switch into virtual
mode. So far so dreadful.
The specification makes it clear that the operating system is free to do
whatever it wants with boot services code after ExitBootServices() has been
called. SetVirtualAddressMap() can't be called until ExitBootServices() has
been. So, obviously, a whole bunch of EFI implementations call into boot
services code when we do that. Since we've been charmingly naive and
trusted that the specification may be somehow relevant to the real world,
we've already stuffed a picture of a penguin or something in that address
space. And just to make things more entertaining, we've also marked it
non-executable.
This patch allocates the boot services regions during EFI init and makes
sure that they're executable. Then, after SetVirtualAddressMap(), it
discards them and everyone lives happily ever after. Except for the ones
who have to work on EFI, who live sad lives haunted by the knowledge that
someone's eventually going to write yet another firmware specification.
[ hpa: adding this to urgent with a stable tag since it fixes currently-broken
hardware. However, I do not know what the dependencies are and so I do
not know which -stable versions this may be a candidate for. ]
Signed-off-by: Matthew Garrett <mjg@redhat.com>
Link: http://lkml.kernel.org/r/1306331593-28715-1-git-send-email-mjg@redhat.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: <stable@kernel.org>
2011-05-25 21:53:13 +08:00
|
|
|
if (md->type == EFI_RUNTIME_SERVICES_CODE ||
|
|
|
|
md->type == EFI_BOOT_SERVICES_CODE)
|
2011-05-06 03:19:43 +08:00
|
|
|
efi_set_executable(md, executable);
|
2008-01-30 20:31:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init efi_call_phys_prelog(void)
|
|
|
|
{
|
|
|
|
unsigned long vaddress;
|
2013-01-08 23:02:43 +08:00
|
|
|
int pgd;
|
|
|
|
int n_pgds;
|
2008-01-30 20:31:19 +08:00
|
|
|
|
2013-11-01 00:25:08 +08:00
|
|
|
if (!efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
return;
|
|
|
|
|
2011-05-06 03:19:43 +08:00
|
|
|
early_code_mapping_set_exec(1);
|
2008-02-13 17:22:41 +08:00
|
|
|
local_irq_save(efi_flags);
|
2013-01-08 23:02:43 +08:00
|
|
|
|
|
|
|
n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
|
|
|
|
save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
|
|
|
|
|
|
|
|
for (pgd = 0; pgd < n_pgds; pgd++) {
|
|
|
|
save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
|
|
|
|
vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
|
|
|
|
set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
|
|
|
|
}
|
2008-01-30 20:31:19 +08:00
|
|
|
__flush_tlb_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init efi_call_phys_epilog(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* After the lock is released, the original page table is restored.
|
|
|
|
*/
|
2013-01-08 23:02:43 +08:00
|
|
|
int pgd;
|
|
|
|
int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
|
2013-11-01 00:25:08 +08:00
|
|
|
|
|
|
|
if (!efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
return;
|
|
|
|
|
2013-01-08 23:02:43 +08:00
|
|
|
for (pgd = 0; pgd < n_pgds; pgd++)
|
|
|
|
set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
|
|
|
|
kfree(save_pgd);
|
2008-01-30 20:31:19 +08:00
|
|
|
__flush_tlb_all();
|
|
|
|
local_irq_restore(efi_flags);
|
2011-05-06 03:19:43 +08:00
|
|
|
early_code_mapping_set_exec(0);
|
2008-01-30 20:31:19 +08:00
|
|
|
}
|
2011-12-12 08:12:42 +08:00
|
|
|
|
2013-11-01 00:25:08 +08:00
|
|
|
/*
|
|
|
|
* Add low kernel mappings for passing arguments to EFI functions.
|
|
|
|
*/
|
|
|
|
void efi_sync_low_kernel_mappings(void)
|
|
|
|
{
|
|
|
|
unsigned num_pgds;
|
|
|
|
pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
|
|
|
|
|
|
|
|
if (efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
return;
|
|
|
|
|
|
|
|
num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
|
|
|
|
|
|
|
|
memcpy(pgd + pgd_index(PAGE_OFFSET),
|
|
|
|
init_mm.pgd + pgd_index(PAGE_OFFSET),
|
|
|
|
sizeof(pgd_t) * num_pgds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void efi_setup_page_tables(void)
|
|
|
|
{
|
|
|
|
efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
|
|
|
|
|
|
|
|
if (!efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
efi_scratch.use_pgd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init __map_region(efi_memory_desc_t *md, u64 va)
|
|
|
|
{
|
|
|
|
pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
|
2013-12-20 18:02:13 +08:00
|
|
|
unsigned long pf = 0;
|
2013-11-01 00:25:08 +08:00
|
|
|
|
|
|
|
if (!(md->attribute & EFI_MEMORY_WB))
|
|
|
|
pf |= _PAGE_PCD;
|
|
|
|
|
|
|
|
if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
|
|
|
|
pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
|
|
|
|
md->phys_addr, va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init efi_map_region(efi_memory_desc_t *md)
|
|
|
|
{
|
|
|
|
unsigned long size = md->num_pages << PAGE_SHIFT;
|
|
|
|
u64 pa = md->phys_addr;
|
|
|
|
|
|
|
|
if (efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
return old_map_region(md);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the 1:1 mappings are present as a catch-all for b0rked
|
|
|
|
* firmware which doesn't update all internal pointers after switching
|
|
|
|
* to virtual mode and would otherwise crap on us.
|
|
|
|
*/
|
|
|
|
__map_region(md, md->phys_addr);
|
|
|
|
|
|
|
|
efi_va -= size;
|
|
|
|
|
|
|
|
/* Is PA 2M-aligned? */
|
|
|
|
if (!(pa & (PMD_SIZE - 1))) {
|
|
|
|
efi_va &= PMD_MASK;
|
|
|
|
} else {
|
|
|
|
u64 pa_offset = pa & (PMD_SIZE - 1);
|
|
|
|
u64 prev_va = efi_va;
|
|
|
|
|
|
|
|
/* get us the same offset within this 2M page */
|
|
|
|
efi_va = (efi_va & PMD_MASK) + pa_offset;
|
|
|
|
|
|
|
|
if (efi_va > prev_va)
|
|
|
|
efi_va -= PMD_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (efi_va < EFI_VA_END) {
|
|
|
|
pr_warn(FW_WARN "VA address range overflow!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the VA map */
|
|
|
|
__map_region(md, efi_va);
|
|
|
|
md->virt_addr = efi_va;
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:02:14 +08:00
|
|
|
/*
|
|
|
|
* kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
|
|
|
|
* md->virt_addr is the original virtual address which had been mapped in kexec
|
|
|
|
* 1st kernel.
|
|
|
|
*/
|
|
|
|
void __init efi_map_region_fixed(efi_memory_desc_t *md)
|
|
|
|
{
|
|
|
|
__map_region(md, md->virt_addr);
|
|
|
|
}
|
|
|
|
|
2011-12-12 08:12:42 +08:00
|
|
|
void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
|
2012-10-19 20:25:46 +08:00
|
|
|
u32 type, u64 attribute)
|
2011-12-12 08:12:42 +08:00
|
|
|
{
|
|
|
|
unsigned long last_map_pfn;
|
|
|
|
|
|
|
|
if (type == EFI_MEMORY_MAPPED_IO)
|
|
|
|
return ioremap(phys_addr, size);
|
|
|
|
|
|
|
|
last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
|
|
|
|
if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
|
|
|
|
unsigned long top = last_map_pfn << PAGE_SHIFT;
|
2012-10-19 20:25:46 +08:00
|
|
|
efi_ioremap(top, size - (top - phys_addr), type, attribute);
|
2011-12-12 08:12:42 +08:00
|
|
|
}
|
|
|
|
|
2012-10-19 20:25:46 +08:00
|
|
|
if (!(attribute & EFI_MEMORY_WB))
|
|
|
|
efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
|
|
|
|
|
2011-12-12 08:12:42 +08:00
|
|
|
return (void __iomem *)__va(phys_addr);
|
|
|
|
}
|
2013-12-20 18:02:19 +08:00
|
|
|
|
|
|
|
void __init parse_efi_setup(u64 phys_addr, u32 data_len)
|
|
|
|
{
|
|
|
|
efi_setup = phys_addr + sizeof(struct setup_data);
|
|
|
|
}
|
2014-02-14 15:24:24 +08:00
|
|
|
|
|
|
|
void __init efi_runtime_mkexec(void)
|
|
|
|
{
|
|
|
|
if (!efi_enabled(EFI_OLD_MEMMAP))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (__supported_pte_mask & _PAGE_NX)
|
|
|
|
runtime_code_page_mkexec();
|
|
|
|
}
|