GNU Linux-libre 4.19.264-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         set_bit(EFI_MEM_ATTR, &efi.flags);
47
48 unmap:
49         early_memunmap(tbl, sizeof(*tbl));
50         return 0;
51 }
52
53 /*
54  * Returns a copy @out of the UEFI memory descriptor @in if it is covered
55  * entirely by a UEFI memory map entry with matching attributes. The virtual
56  * address of @out is set according to the matching entry that was found.
57  */
58 static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
59 {
60         u64 in_paddr = in->phys_addr;
61         u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
62         efi_memory_desc_t *md;
63
64         *out = *in;
65
66         if (in->type != EFI_RUNTIME_SERVICES_CODE &&
67             in->type != EFI_RUNTIME_SERVICES_DATA) {
68                 pr_warn("Entry type should be RuntimeServiceCode/Data\n");
69                 return false;
70         }
71
72         if (PAGE_SIZE > EFI_PAGE_SIZE &&
73             (!PAGE_ALIGNED(in->phys_addr) ||
74              !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
75                 /*
76                  * Since arm64 may execute with page sizes of up to 64 KB, the
77                  * UEFI spec mandates that RuntimeServices memory regions must
78                  * be 64 KB aligned. We need to validate this here since we will
79                  * not be able to tighten permissions on such regions without
80                  * affecting adjacent regions.
81                  */
82                 pr_warn("Entry address region misaligned\n");
83                 return false;
84         }
85
86         for_each_efi_memory_desc(md) {
87                 u64 md_paddr = md->phys_addr;
88                 u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
89
90                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
91                         continue;
92                 if (md->virt_addr == 0 && md->phys_addr != 0) {
93                         /* no virtual mapping has been installed by the stub */
94                         break;
95                 }
96
97                 if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
98                         continue;
99
100                 /*
101                  * This entry covers the start of @in, check whether
102                  * it covers the end as well.
103                  */
104                 if (md_paddr + md_size < in_paddr + in_size) {
105                         pr_warn("Entry covers multiple EFI memory map regions\n");
106                         return false;
107                 }
108
109                 if (md->type != in->type) {
110                         pr_warn("Entry type deviates from EFI memory map region type\n");
111                         return false;
112                 }
113
114                 out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
115
116                 return true;
117         }
118
119         pr_warn("No matching entry found in the EFI memory map\n");
120         return false;
121 }
122
123 /*
124  * To be called after the EFI page tables have been populated. If a memory
125  * attributes table is available, its contents will be used to update the
126  * mappings with tightened permissions as described by the table.
127  * This requires the UEFI memory map to have already been populated with
128  * virtual addresses.
129  */
130 int __init efi_memattr_apply_permissions(struct mm_struct *mm,
131                                          efi_memattr_perm_setter fn)
132 {
133         efi_memory_attributes_table_t *tbl;
134         int i, ret;
135
136         if (tbl_size <= sizeof(*tbl))
137                 return 0;
138
139         /*
140          * We need the EFI memory map to be setup so we can use it to
141          * lookup the virtual addresses of all entries in the  of EFI
142          * Memory Attributes table. If it isn't available, this
143          * function should not be called.
144          */
145         if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
146                 return 0;
147
148         tbl = memremap(efi.mem_attr_table, tbl_size, MEMREMAP_WB);
149         if (!tbl) {
150                 pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
151                        efi.mem_attr_table);
152                 return -ENOMEM;
153         }
154
155         if (efi_enabled(EFI_DBG))
156                 pr_info("Processing EFI Memory Attributes table:\n");
157
158         for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
159                 efi_memory_desc_t md;
160                 unsigned long size;
161                 bool valid;
162                 char buf[64];
163
164                 valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
165                                        &md);
166                 size = md.num_pages << EFI_PAGE_SHIFT;
167                 if (efi_enabled(EFI_DBG) || !valid)
168                         pr_info("%s 0x%012llx-0x%012llx %s\n",
169                                 valid ? "" : "!", md.phys_addr,
170                                 md.phys_addr + size - 1,
171                                 efi_md_typeattr_format(buf, sizeof(buf), &md));
172
173                 if (valid) {
174                         ret = fn(mm, &md);
175                         if (ret)
176                                 pr_err("Error updating mappings, skipping subsequent md's\n");
177                 }
178         }
179         memunmap(tbl);
180         return ret;
181 }