Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * fake_mem.c |
| 3 | * |
| 4 | * Copyright (C) 2015 FUJITSU LIMITED |
| 5 | * Author: Taku Izumi <izumi.taku@jp.fujitsu.com> |
| 6 | * |
| 7 | * This code introduces new boot option named "efi_fake_mem" |
| 8 | * By specifying this parameter, you can add arbitrary attribute to |
| 9 | * specific memory range by updating original (firmware provided) EFI |
| 10 | * memmap. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms and conditions of the GNU General Public License, |
| 14 | * version 2, as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 19 | * more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * this program; if not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | * The full GNU General Public License is included in this distribution in |
| 25 | * the file called "COPYING". |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/efi.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/memblock.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/sort.h> |
| 34 | #include <asm/efi.h> |
| 35 | |
| 36 | #define EFI_MAX_FAKEMEM CONFIG_EFI_MAX_FAKE_MEM |
| 37 | |
| 38 | struct fake_mem { |
| 39 | struct range range; |
| 40 | u64 attribute; |
| 41 | }; |
| 42 | static struct fake_mem fake_mems[EFI_MAX_FAKEMEM]; |
| 43 | static int nr_fake_mem; |
| 44 | |
| 45 | static int __init cmp_fake_mem(const void *x1, const void *x2) |
| 46 | { |
| 47 | const struct fake_mem *m1 = x1; |
| 48 | const struct fake_mem *m2 = x2; |
| 49 | |
| 50 | if (m1->range.start < m2->range.start) |
| 51 | return -1; |
| 52 | if (m1->range.start > m2->range.start) |
| 53 | return 1; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void __init efi_fake_memmap(void) |
| 58 | { |
| 59 | u64 start, end, m_start, m_end, m_attr; |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 60 | int new_nr_map = efi.memmap.nr_map; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 61 | efi_memory_desc_t *md; |
Taku Izumi | 78b9bc9 | 2015-10-23 11:48:17 +0200 | [diff] [blame] | 62 | phys_addr_t new_memmap_phy; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 63 | void *new_memmap; |
| 64 | void *old, *new; |
| 65 | int i; |
| 66 | |
| 67 | if (!nr_fake_mem || !efi_enabled(EFI_MEMMAP)) |
| 68 | return; |
| 69 | |
| 70 | /* count up the number of EFI memory descriptor */ |
Matt Fleming | 78ce248 | 2016-04-25 21:06:38 +0100 | [diff] [blame] | 71 | for_each_efi_memory_desc(md) { |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 72 | start = md->phys_addr; |
| 73 | end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1; |
| 74 | |
| 75 | for (i = 0; i < nr_fake_mem; i++) { |
| 76 | /* modifying range */ |
| 77 | m_start = fake_mems[i].range.start; |
| 78 | m_end = fake_mems[i].range.end; |
| 79 | |
| 80 | if (m_start <= start) { |
| 81 | /* split into 2 parts */ |
| 82 | if (start < m_end && m_end < end) |
| 83 | new_nr_map++; |
| 84 | } |
| 85 | if (start < m_start && m_start < end) { |
| 86 | /* split into 3 parts */ |
| 87 | if (m_end < end) |
| 88 | new_nr_map += 2; |
| 89 | /* split into 2 parts */ |
| 90 | if (end <= m_end) |
| 91 | new_nr_map++; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* allocate memory for new EFI memmap */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 97 | new_memmap_phy = memblock_alloc(efi.memmap.desc_size * new_nr_map, |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 98 | PAGE_SIZE); |
| 99 | if (!new_memmap_phy) |
| 100 | return; |
| 101 | |
| 102 | /* create new EFI memmap */ |
| 103 | new_memmap = early_memremap(new_memmap_phy, |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 104 | efi.memmap.desc_size * new_nr_map); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 105 | if (!new_memmap) { |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 106 | memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 110 | for (old = efi.memmap.map, new = new_memmap; |
| 111 | old < efi.memmap.map_end; |
| 112 | old += efi.memmap.desc_size, new += efi.memmap.desc_size) { |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 113 | |
| 114 | /* copy original EFI memory descriptor */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 115 | memcpy(new, old, efi.memmap.desc_size); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 116 | md = new; |
| 117 | start = md->phys_addr; |
| 118 | end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1; |
| 119 | |
| 120 | for (i = 0; i < nr_fake_mem; i++) { |
| 121 | /* modifying range */ |
| 122 | m_start = fake_mems[i].range.start; |
| 123 | m_end = fake_mems[i].range.end; |
| 124 | m_attr = fake_mems[i].attribute; |
| 125 | |
| 126 | if (m_start <= start && end <= m_end) |
| 127 | md->attribute |= m_attr; |
| 128 | |
| 129 | if (m_start <= start && |
| 130 | (start < m_end && m_end < end)) { |
| 131 | /* first part */ |
| 132 | md->attribute |= m_attr; |
| 133 | md->num_pages = (m_end - md->phys_addr + 1) >> |
| 134 | EFI_PAGE_SHIFT; |
| 135 | /* latter part */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 136 | new += efi.memmap.desc_size; |
| 137 | memcpy(new, old, efi.memmap.desc_size); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 138 | md = new; |
| 139 | md->phys_addr = m_end + 1; |
| 140 | md->num_pages = (end - md->phys_addr + 1) >> |
| 141 | EFI_PAGE_SHIFT; |
| 142 | } |
| 143 | |
| 144 | if ((start < m_start && m_start < end) && m_end < end) { |
| 145 | /* first part */ |
| 146 | md->num_pages = (m_start - md->phys_addr) >> |
| 147 | EFI_PAGE_SHIFT; |
| 148 | /* middle part */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 149 | new += efi.memmap.desc_size; |
| 150 | memcpy(new, old, efi.memmap.desc_size); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 151 | md = new; |
| 152 | md->attribute |= m_attr; |
| 153 | md->phys_addr = m_start; |
| 154 | md->num_pages = (m_end - m_start + 1) >> |
| 155 | EFI_PAGE_SHIFT; |
| 156 | /* last part */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 157 | new += efi.memmap.desc_size; |
| 158 | memcpy(new, old, efi.memmap.desc_size); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 159 | md = new; |
| 160 | md->phys_addr = m_end + 1; |
| 161 | md->num_pages = (end - m_end) >> |
| 162 | EFI_PAGE_SHIFT; |
| 163 | } |
| 164 | |
| 165 | if ((start < m_start && m_start < end) && |
| 166 | (end <= m_end)) { |
| 167 | /* first part */ |
| 168 | md->num_pages = (m_start - md->phys_addr) >> |
| 169 | EFI_PAGE_SHIFT; |
| 170 | /* latter part */ |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 171 | new += efi.memmap.desc_size; |
| 172 | memcpy(new, old, efi.memmap.desc_size); |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 173 | md = new; |
| 174 | md->phys_addr = m_start; |
| 175 | md->num_pages = (end - md->phys_addr + 1) >> |
| 176 | EFI_PAGE_SHIFT; |
| 177 | md->attribute |= m_attr; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* swap into new EFI memmap */ |
| 183 | efi_unmap_memmap(); |
Matt Fleming | 884f4f6 | 2016-04-25 21:06:39 +0100 | [diff] [blame] | 184 | efi.memmap.map = new_memmap; |
| 185 | efi.memmap.phys_map = new_memmap_phy; |
| 186 | efi.memmap.nr_map = new_nr_map; |
| 187 | efi.memmap.map_end = efi.memmap.map + efi.memmap.nr_map * efi.memmap.desc_size; |
Taku Izumi | 0f96a99 | 2015-09-30 23:01:56 +0900 | [diff] [blame] | 188 | set_bit(EFI_MEMMAP, &efi.flags); |
| 189 | |
| 190 | /* print new EFI memmap */ |
| 191 | efi_print_memmap(); |
| 192 | } |
| 193 | |
| 194 | static int __init setup_fake_mem(char *p) |
| 195 | { |
| 196 | u64 start = 0, mem_size = 0, attribute = 0; |
| 197 | int i; |
| 198 | |
| 199 | if (!p) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | while (*p != '\0') { |
| 203 | mem_size = memparse(p, &p); |
| 204 | if (*p == '@') |
| 205 | start = memparse(p+1, &p); |
| 206 | else |
| 207 | break; |
| 208 | |
| 209 | if (*p == ':') |
| 210 | attribute = simple_strtoull(p+1, &p, 0); |
| 211 | else |
| 212 | break; |
| 213 | |
| 214 | if (nr_fake_mem >= EFI_MAX_FAKEMEM) |
| 215 | break; |
| 216 | |
| 217 | fake_mems[nr_fake_mem].range.start = start; |
| 218 | fake_mems[nr_fake_mem].range.end = start + mem_size - 1; |
| 219 | fake_mems[nr_fake_mem].attribute = attribute; |
| 220 | nr_fake_mem++; |
| 221 | |
| 222 | if (*p == ',') |
| 223 | p++; |
| 224 | } |
| 225 | |
| 226 | sort(fake_mems, nr_fake_mem, sizeof(struct fake_mem), |
| 227 | cmp_fake_mem, NULL); |
| 228 | |
| 229 | for (i = 0; i < nr_fake_mem; i++) |
| 230 | pr_info("efi_fake_mem: add attr=0x%016llx to [mem 0x%016llx-0x%016llx]", |
| 231 | fake_mems[i].attribute, fake_mems[i].range.start, |
| 232 | fake_mems[i].range.end); |
| 233 | |
| 234 | return *p == '\0' ? 0 : -EINVAL; |
| 235 | } |
| 236 | |
| 237 | early_param("efi_fake_mem", setup_fake_mem); |