2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 2.4
6 * Copyright (C) 2013, 2014 Linaro Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/efi.h>
15 #include <linux/init.h>
19 static bool region_is_misaligned(const efi_memory_desc_t *md)
21 if (PAGE_SIZE == EFI_PAGE_SIZE)
23 return !PAGE_ALIGNED(md->phys_addr) ||
24 !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
28 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
29 * executable, everything else can be mapped with the XN bits
30 * set. Also take the new (optional) RO/XP bits into account.
32 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
34 u64 attr = md->attribute;
37 if (type == EFI_MEMORY_MAPPED_IO)
38 return PROT_DEVICE_nGnRE;
40 if (region_is_misaligned(md)) {
41 static bool __initdata code_is_misaligned;
44 * Regions that are not aligned to the OS page size cannot be
45 * mapped with strict permissions, as those might interfere
46 * with the permissions that are needed by the adjacent
47 * region's mapping. However, if we haven't encountered any
48 * misaligned runtime code regions so far, we can safely use
49 * non-executable permissions for non-code regions.
51 code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
53 return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
54 : pgprot_val(PAGE_KERNEL);
58 if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
59 (EFI_MEMORY_XP | EFI_MEMORY_RO))
60 return pgprot_val(PAGE_KERNEL_RO);
63 if (attr & EFI_MEMORY_RO)
64 return pgprot_val(PAGE_KERNEL_ROX);
67 if (attr & EFI_MEMORY_XP || type != EFI_RUNTIME_SERVICES_CODE)
68 return pgprot_val(PAGE_KERNEL);
71 return pgprot_val(PAGE_KERNEL_EXEC);
74 /* we will fill this structure from the stub, so don't put it in .bss */
75 struct screen_info screen_info __section(.data);
77 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
79 pteval_t prot_val = create_mapping_protection(md);
80 bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
81 md->type == EFI_RUNTIME_SERVICES_DATA);
84 * If this region is not aligned to the page size used by the OS, the
85 * mapping will be rounded outwards, and may end up sharing a page
86 * frame with an adjacent runtime memory region. Given that the page
87 * table descriptor covering the shared page will be rewritten when the
88 * adjacent region gets mapped, we must avoid block mappings here so we
89 * don't have to worry about splitting them when that happens.
91 if (region_is_misaligned(md))
92 page_mappings_only = true;
94 create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
95 md->num_pages << EFI_PAGE_SHIFT,
96 __pgprot(prot_val | PTE_NG), page_mappings_only);
100 static int __init set_permissions(pte_t *ptep, pgtable_t token,
101 unsigned long addr, void *data)
103 efi_memory_desc_t *md = data;
106 if (md->attribute & EFI_MEMORY_RO)
107 pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
108 if (md->attribute & EFI_MEMORY_XP)
109 pte = set_pte_bit(pte, __pgprot(PTE_PXN));
114 int __init efi_set_mapping_permissions(struct mm_struct *mm,
115 efi_memory_desc_t *md)
117 BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
118 md->type != EFI_RUNTIME_SERVICES_DATA);
120 if (region_is_misaligned(md))
124 * Calling apply_to_page_range() is only safe on regions that are
125 * guaranteed to be mapped down to pages. Since we are only called
126 * for regions that have been mapped using efi_create_mapping() above
127 * (and this is checked by the generic Memory Attributes table parsing
128 * routines), there is no need to check that again here.
130 return apply_to_page_range(mm, md->virt_addr,
131 md->num_pages << EFI_PAGE_SHIFT,
132 set_permissions, md);
136 * UpdateCapsule() depends on the system being shutdown via
139 bool efi_poweroff_required(void)
141 return efi_enabled(EFI_RUNTIME_SERVICES);