GNU Linux-libre 4.14.319-gnu1
[releases.git] / arch / arm64 / kernel / efi.c
1 /*
2  * Extensible Firmware Interface
3  *
4  * Based on Extensible Firmware Interface Specification version 2.4
5  *
6  * Copyright (C) 2013, 2014 Linaro Ltd.
7  *
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.
11  *
12  */
13
14 #include <linux/efi.h>
15 #include <linux/init.h>
16
17 #include <asm/efi.h>
18
19 static bool region_is_misaligned(const efi_memory_desc_t *md)
20 {
21         if (PAGE_SIZE == EFI_PAGE_SIZE)
22                 return false;
23         return !PAGE_ALIGNED(md->phys_addr) ||
24                !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
25 }
26
27 /*
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.
31  */
32 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
33 {
34         u64 attr = md->attribute;
35         u32 type = md->type;
36
37         if (type == EFI_MEMORY_MAPPED_IO)
38                 return PROT_DEVICE_nGnRE;
39
40         if (region_is_misaligned(md)) {
41                 static bool __initdata code_is_misaligned;
42
43                 /*
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.
50                  */
51                 code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
52
53                 return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
54                                           : pgprot_val(PAGE_KERNEL);
55         }
56
57         /* R-- */
58         if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
59             (EFI_MEMORY_XP | EFI_MEMORY_RO))
60                 return pgprot_val(PAGE_KERNEL_RO);
61
62         /* R-X */
63         if (attr & EFI_MEMORY_RO)
64                 return pgprot_val(PAGE_KERNEL_ROX);
65
66         /* RW- */
67         if (attr & EFI_MEMORY_XP || type != EFI_RUNTIME_SERVICES_CODE)
68                 return pgprot_val(PAGE_KERNEL);
69
70         /* RWX */
71         return pgprot_val(PAGE_KERNEL_EXEC);
72 }
73
74 /* we will fill this structure from the stub, so don't put it in .bss */
75 struct screen_info screen_info __section(.data);
76
77 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
78 {
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);
82
83         /*
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.
90          */
91         if (region_is_misaligned(md))
92                 page_mappings_only = true;
93
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);
97         return 0;
98 }
99
100 static int __init set_permissions(pte_t *ptep, pgtable_t token,
101                                   unsigned long addr, void *data)
102 {
103         efi_memory_desc_t *md = data;
104         pte_t pte = *ptep;
105
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));
110         set_pte(ptep, pte);
111         return 0;
112 }
113
114 int __init efi_set_mapping_permissions(struct mm_struct *mm,
115                                        efi_memory_desc_t *md)
116 {
117         BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
118                md->type != EFI_RUNTIME_SERVICES_DATA);
119
120         if (region_is_misaligned(md))
121                 return 0;
122
123         /*
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.
129          */
130         return apply_to_page_range(mm, md->virt_addr,
131                                    md->num_pages << EFI_PAGE_SHIFT,
132                                    set_permissions, md);
133 }
134
135 /*
136  * UpdateCapsule() depends on the system being shutdown via
137  * ResetSystem().
138  */
139 bool efi_poweroff_required(void)
140 {
141         return efi_enabled(EFI_RUNTIME_SERVICES);
142 }