GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / firmware / efi / memattr.c
1 /*
2  * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #define pr_fmt(fmt)     "efi: memattr: " fmt
10
11 #include <linux/efi.h>
12 #include <linux/init.h>
13 #include <linux/io.h>
14 #include <linux/memblock.h>
15
16 #include <asm/early_ioremap.h>
17
18 static int __initdata tbl_size;
19
20 /*
21  * Reserve the memory associated with the Memory Attributes configuration
22  * table, if it exists.
23  */
24 int __init efi_memattr_init(void)
25 {
26         efi_memory_attributes_table_t *tbl;
27
28         if (efi.mem_attr_table == EFI_INVALID_TABLE_ADDR)
29                 return 0;
30
31         tbl = early_memremap(efi.mem_attr_table, sizeof(*tbl));
32         if (!tbl) {
33                 pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
34                        efi.mem_attr_table);
35                 return -ENOMEM;
36         }
37
38         if (tbl->version > 1) {
39                 pr_warn("Unexpected EFI Memory Attributes table version %d\n",
40                         tbl->version);
41                 goto unmap;
42         }
43
44         tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
45         memblock_reserve(efi.mem_attr_table, tbl_size);
46
47 unmap:
48         early_memunmap(tbl, sizeof(*tbl));
49         return 0;
50 }
51
52 /*
53  * Returns a copy @out of the UEFI memory descriptor @in if it is covered
54  * entirely by a UEFI memory map entry with matching attributes. The virtual
55  * address of @out is set according to the matching entry that was found.
56  */
57 static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
58 {
59         u64 in_paddr = in->phys_addr;
60         u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
61         efi_memory_desc_t *md;
62
63         *out = *in;
64
65         if (in->type != EFI_RUNTIME_SERVICES_CODE &&
66             in->type != EFI_RUNTIME_SERVICES_DATA) {
67                 pr_warn("Entry type should be RuntimeServiceCode/Data\n");
68                 return false;
69         }
70
71         if (PAGE_SIZE > EFI_PAGE_SIZE &&
72             (!PAGE_ALIGNED(in->phys_addr) ||
73              !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
74                 /*
75                  * Since arm64 may execute with page sizes of up to 64 KB, the
76                  * UEFI spec mandates that RuntimeServices memory regions must
77                  * be 64 KB aligned. We need to validate this here since we will
78                  * not be able to tighten permissions on such regions without
79                  * affecting adjacent regions.
80                  */
81                 pr_warn("Entry address region misaligned\n");
82                 return false;
83         }
84
85         for_each_efi_memory_desc(md) {
86                 u64 md_paddr = md->phys_addr;
87                 u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
88
89                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
90                         continue;
91                 if (md->virt_addr == 0 && md->phys_addr != 0) {
92                         /* no virtual mapping has been installed by the stub */
93                         break;
94                 }
95
96                 if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
97                         continue;
98
99                 /*
100                  * This entry covers the start of @in, check whether
101                  * it covers the end as well.
102                  */
103                 if (md_paddr + md_size < in_paddr + in_size) {
104                         pr_warn("Entry covers multiple EFI memory map regions\n");
105                         return false;
106                 }
107
108                 if (md->type != in->type) {
109                         pr_warn("Entry type deviates from EFI memory map region type\n");
110                         return false;
111                 }
112
113                 out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
114
115                 return true;
116         }
117
118         pr_warn("No matching entry found in the EFI memory map\n");
119         return false;
120 }
121
122 /*
123  * To be called after the EFI page tables have been populated. If a memory
124  * attributes table is available, its contents will be used to update the
125  * mappings with tightened permissions as described by the table.
126  * This requires the UEFI memory map to have already been populated with
127  * virtual addresses.
128  */
129 int __init efi_memattr_apply_permissions(struct mm_struct *mm,
130                                          efi_memattr_perm_setter fn)
131 {
132         efi_memory_attributes_table_t *tbl;
133         int i, ret;
134
135         if (tbl_size <= sizeof(*tbl))
136                 return 0;
137
138         /*
139          * We need the EFI memory map to be setup so we can use it to
140          * lookup the virtual addresses of all entries in the  of EFI
141          * Memory Attributes table. If it isn't available, this
142          * function should not be called.
143          */
144         if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
145                 return 0;
146
147         tbl = memremap(efi.mem_attr_table, tbl_size, MEMREMAP_WB);
148         if (!tbl) {
149                 pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
150                        efi.mem_attr_table);
151                 return -ENOMEM;
152         }
153
154         if (efi_enabled(EFI_DBG))
155                 pr_info("Processing EFI Memory Attributes table:\n");
156
157         for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
158                 efi_memory_desc_t md;
159                 unsigned long size;
160                 bool valid;
161                 char buf[64];
162
163                 valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
164                                        &md);
165                 size = md.num_pages << EFI_PAGE_SHIFT;
166                 if (efi_enabled(EFI_DBG) || !valid)
167                         pr_info("%s 0x%012llx-0x%012llx %s\n",
168                                 valid ? "" : "!", md.phys_addr,
169                                 md.phys_addr + size - 1,
170                                 efi_md_typeattr_format(buf, sizeof(buf), &md));
171
172                 if (valid)
173                         ret = fn(mm, &md);
174         }
175         memunmap(tbl);
176         return ret;
177 }