Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <cbmem.h> |
| 18 | #include <console/console.h> |
| 19 | #include <imd.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* For more details on implementation and usage please see the imd.h header. */ |
| 24 | |
| 25 | static const uint32_t IMD_ROOT_PTR_MAGIC = 0xc0389481; |
| 26 | static const uint32_t IMD_ENTRY_MAGIC = ~0xc0389481; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 27 | static const uint32_t SMALL_REGION_ID = CBMEM_ID_IMD_SMALL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 28 | static const size_t LIMIT_ALIGN = 4096; |
| 29 | |
| 30 | /* In-memory data structures. */ |
| 31 | struct imd_root_pointer { |
| 32 | uint32_t magic; |
| 33 | /* Relative to upper limit/offset. */ |
| 34 | int32_t root_offset; |
| 35 | } __attribute__((packed)); |
| 36 | |
| 37 | struct imd_entry { |
| 38 | uint32_t magic; |
| 39 | /* start is located relative to imd_root */ |
| 40 | int32_t start_offset; |
| 41 | uint32_t size; |
| 42 | uint32_t id; |
| 43 | } __attribute__((packed)); |
| 44 | |
| 45 | struct imd_root { |
| 46 | uint32_t max_entries; |
| 47 | uint32_t num_entries; |
| 48 | uint32_t flags; |
| 49 | uint32_t entry_align; |
| 50 | /* Used for fixing the size of an imd. Relative to the root. */ |
| 51 | int32_t max_offset; |
| 52 | struct imd_entry entries[0]; |
| 53 | } __attribute__((packed)); |
| 54 | |
| 55 | #define IMD_FLAG_LOCKED 1 |
| 56 | |
| 57 | static void *relative_pointer(void *base, ssize_t offset) |
| 58 | { |
| 59 | intptr_t b = (intptr_t)base; |
| 60 | b += offset; |
| 61 | return (void *)b; |
| 62 | } |
| 63 | |
| 64 | static bool imd_root_pointer_valid(const struct imd_root_pointer *rp) |
| 65 | { |
| 66 | return !!(rp->magic == IMD_ROOT_PTR_MAGIC); |
| 67 | } |
| 68 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 69 | static struct imd_root *imdr_root(const struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 70 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 71 | return imdr->r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * The root pointer is relative to the upper limit of the imd. i.e. It sits |
| 76 | * just below the upper limit. |
| 77 | */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 78 | static struct imd_root_pointer *imdr_get_root_pointer(const struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 79 | { |
| 80 | struct imd_root_pointer *rp; |
| 81 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 82 | rp = relative_pointer((void *)imdr->limit, -sizeof(*rp)); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 83 | |
| 84 | return rp; |
| 85 | } |
| 86 | |
| 87 | static void imd_link_root(struct imd_root_pointer *rp, struct imd_root *r) |
| 88 | { |
| 89 | rp->magic = IMD_ROOT_PTR_MAGIC; |
| 90 | rp->root_offset = (int32_t)((intptr_t)r - (intptr_t)rp); |
| 91 | } |
| 92 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 93 | static struct imd_entry *root_last_entry(struct imd_root *r) |
| 94 | { |
| 95 | return &r->entries[r->num_entries - 1]; |
| 96 | } |
| 97 | |
| 98 | static size_t root_num_entries(size_t root_size) |
| 99 | { |
| 100 | size_t entries_size; |
| 101 | |
| 102 | entries_size = root_size; |
| 103 | entries_size -= sizeof(struct imd_root_pointer); |
| 104 | entries_size -= sizeof(struct imd_root); |
| 105 | |
| 106 | return entries_size / sizeof(struct imd_entry); |
| 107 | } |
| 108 | |
| 109 | static size_t imd_root_data_left(struct imd_root *r) |
| 110 | { |
| 111 | struct imd_entry *last_entry; |
| 112 | |
| 113 | last_entry = root_last_entry(r); |
| 114 | |
| 115 | if (r->max_offset != 0) |
| 116 | return last_entry->start_offset - r->max_offset; |
| 117 | |
| 118 | return ~(size_t)0; |
| 119 | } |
| 120 | |
| 121 | static bool root_is_locked(const struct imd_root *r) |
| 122 | { |
| 123 | return !!(r->flags & IMD_FLAG_LOCKED); |
| 124 | } |
| 125 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 126 | static void imd_entry_assign(struct imd_entry *e, uint32_t id, |
| 127 | ssize_t offset, size_t size) |
| 128 | { |
| 129 | e->magic = IMD_ENTRY_MAGIC; |
| 130 | e->start_offset = offset; |
| 131 | e->size = size; |
| 132 | e->id = id; |
| 133 | } |
| 134 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 135 | static void imdr_init(struct imdr *ir, void *upper_limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 136 | { |
| 137 | uintptr_t limit = (uintptr_t)upper_limit; |
| 138 | /* Upper limit is aligned down to 4KiB */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 139 | ir->limit = ALIGN_DOWN(limit, LIMIT_ALIGN); |
| 140 | ir->r = NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 141 | } |
| 142 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 143 | static int imdr_create_empty(struct imdr *imdr, size_t root_size, |
| 144 | size_t entry_align) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 145 | { |
| 146 | struct imd_root_pointer *rp; |
| 147 | struct imd_root *r; |
| 148 | struct imd_entry *e; |
| 149 | ssize_t root_offset; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 150 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 151 | if (!imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 152 | return -1; |
| 153 | |
| 154 | /* root_size and entry_align should be a power of 2. */ |
| 155 | assert(IS_POWER_OF_2(root_size)); |
| 156 | assert(IS_POWER_OF_2(entry_align)); |
| 157 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 158 | if (!imdr->limit) |
| 159 | return -1; |
| 160 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 161 | /* |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 162 | * root_size needs to be large enough to accommodate root pointer and |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 163 | * root book keeping structure. The caller needs to ensure there's |
| 164 | * enough room for tracking individual allocations. |
| 165 | */ |
| 166 | if (root_size < (sizeof(*rp) + sizeof(*r))) |
| 167 | return -1; |
| 168 | |
| 169 | /* For simplicity don't allow sizes or alignments to exceed LIMIT_ALIGN. */ |
| 170 | if (root_size > LIMIT_ALIGN || entry_align > LIMIT_ALIGN) |
| 171 | return -1; |
| 172 | |
| 173 | /* Additionally, don't handle an entry alignment > root_size. */ |
| 174 | if (entry_align > root_size) |
| 175 | return -1; |
| 176 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 177 | rp = imdr_get_root_pointer(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 178 | |
| 179 | root_offset = -(ssize_t)root_size; |
| 180 | /* Set root pointer. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 181 | imdr->r = relative_pointer((void *)imdr->limit, root_offset); |
| 182 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 183 | imd_link_root(rp, r); |
| 184 | |
| 185 | memset(r, 0, sizeof(*r)); |
| 186 | r->entry_align = entry_align; |
| 187 | |
| 188 | /* Calculate size left for entries. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 189 | r->max_entries = root_num_entries(root_size); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 190 | |
| 191 | /* Fill in first entry covering the root region. */ |
| 192 | r->num_entries = 1; |
| 193 | e = &r->entries[0]; |
| 194 | imd_entry_assign(e, CBMEM_ID_IMD_ROOT, 0, root_size); |
| 195 | |
| 196 | printk(BIOS_DEBUG, "IMD: root @ %p %u entries.\n", r, r->max_entries); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 201 | static int imdr_recover(struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 202 | { |
| 203 | struct imd_root_pointer *rp; |
| 204 | struct imd_root *r; |
| 205 | uintptr_t low_limit; |
| 206 | size_t i; |
| 207 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 208 | if (!imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 209 | return -1; |
| 210 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 211 | rp = imdr_get_root_pointer(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 212 | |
| 213 | if (!imd_root_pointer_valid(rp)) |
| 214 | return -1; |
| 215 | |
| 216 | r = relative_pointer(rp, rp->root_offset); |
| 217 | |
| 218 | /* Confirm the root and root pointer are just under the limit. */ |
| 219 | if (ALIGN_UP((uintptr_t)&r->entries[r->max_entries], LIMIT_ALIGN) != |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 220 | imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 221 | return -1; |
| 222 | |
| 223 | if (r->num_entries > r->max_entries) |
| 224 | return -1; |
| 225 | |
| 226 | /* Entry alignment should be power of 2. */ |
| 227 | if (!IS_POWER_OF_2(r->entry_align)) |
| 228 | return -1; |
| 229 | |
| 230 | low_limit = (uintptr_t)relative_pointer(r, r->max_offset); |
| 231 | |
| 232 | /* If no max_offset then lowest limit is 0. */ |
| 233 | if (low_limit == (uintptr_t)r) |
| 234 | low_limit = 0; |
| 235 | |
| 236 | for (i = 0; i < r->num_entries; i++) { |
| 237 | uintptr_t start_addr; |
| 238 | const struct imd_entry *e = &r->entries[i]; |
| 239 | |
| 240 | if (e->magic != IMD_ENTRY_MAGIC) |
| 241 | return -1; |
| 242 | |
| 243 | start_addr = (uintptr_t)relative_pointer(r, e->start_offset); |
| 244 | if (start_addr < low_limit) |
| 245 | return -1; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 246 | if (start_addr >= imdr->limit || |
| 247 | (start_addr + e->size) > imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | /* Set root pointer. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 252 | imdr->r = r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 257 | static const struct imd_entry *imdr_entry_find(const struct imdr *imdr, |
| 258 | uint32_t id) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 259 | { |
| 260 | struct imd_root *r; |
| 261 | struct imd_entry *e; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 262 | size_t i; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 263 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 264 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 265 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 266 | if (r == NULL) |
| 267 | return NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 268 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 269 | e = NULL; |
| 270 | /* Skip first entry covering the root. */ |
| 271 | for (i = 1; i < r->num_entries; i++) { |
| 272 | if (id != r->entries[i].id) |
| 273 | continue; |
| 274 | e = &r->entries[i]; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | return e; |
| 279 | } |
| 280 | |
| 281 | static int imdr_limit_size(struct imdr *imdr, size_t max_size) |
| 282 | { |
| 283 | struct imd_root *r; |
| 284 | ssize_t smax_size; |
| 285 | size_t root_size; |
| 286 | |
| 287 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 288 | if (r == NULL) |
| 289 | return -1; |
| 290 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 291 | root_size = imdr->limit - (uintptr_t)r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 292 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 293 | if (max_size < root_size) |
| 294 | return -1; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 295 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 296 | /* Take into account the root size. */ |
| 297 | smax_size = max_size - root_size; |
| 298 | smax_size = -smax_size; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 299 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 300 | r->max_offset = smax_size; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 305 | static size_t imdr_entry_size(const struct imdr *imdr, |
| 306 | const struct imd_entry *e) |
| 307 | { |
| 308 | return e->size; |
| 309 | } |
| 310 | |
| 311 | static void *imdr_entry_at(const struct imdr *imdr, const struct imd_entry *e) |
| 312 | { |
| 313 | return relative_pointer(imdr_root(imdr), e->start_offset); |
| 314 | } |
| 315 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 316 | static struct imd_entry *imd_entry_add_to_root(struct imd_root *r, uint32_t id, |
| 317 | size_t size) |
| 318 | { |
| 319 | struct imd_entry *entry; |
| 320 | struct imd_entry *last_entry; |
| 321 | ssize_t e_offset; |
| 322 | size_t used_size; |
| 323 | |
| 324 | if (r->num_entries == r->max_entries) |
| 325 | return NULL; |
| 326 | |
| 327 | /* Determine total size taken up by entry. */ |
| 328 | used_size = ALIGN_UP(size, r->entry_align); |
| 329 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 330 | /* See if size overflows imd total size. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 331 | if (used_size > imd_root_data_left(r)) |
| 332 | return NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * Determine if offset field overflows. All offsets should be lower |
| 336 | * than the previous one. |
| 337 | */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 338 | last_entry = root_last_entry(r); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 339 | e_offset = last_entry->start_offset; |
| 340 | e_offset -= (ssize_t)used_size; |
| 341 | if (e_offset > last_entry->start_offset) |
| 342 | return NULL; |
| 343 | |
| 344 | entry = root_last_entry(r) + 1; |
| 345 | r->num_entries++; |
| 346 | |
| 347 | imd_entry_assign(entry, id, e_offset, size); |
| 348 | |
| 349 | return entry; |
| 350 | } |
| 351 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 352 | static const struct imd_entry *imdr_entry_add(const struct imdr *imdr, |
| 353 | uint32_t id, size_t size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 354 | { |
| 355 | struct imd_root *r; |
| 356 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 357 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 358 | |
| 359 | if (r == NULL) |
| 360 | return NULL; |
| 361 | |
| 362 | if (root_is_locked(r)) |
| 363 | return NULL; |
| 364 | |
| 365 | return imd_entry_add_to_root(r, id, size); |
| 366 | } |
| 367 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 368 | static bool imdr_has_entry(const struct imdr *imdr, const struct imd_entry *e) |
| 369 | { |
| 370 | struct imd_root *r; |
| 371 | size_t idx; |
| 372 | |
| 373 | r = imdr_root(imdr); |
| 374 | if (r == NULL) |
| 375 | return false; |
| 376 | |
| 377 | /* Determine if the entry is within this root structure. */ |
| 378 | idx = e - &r->entries[0]; |
| 379 | if (idx >= r->num_entries) |
| 380 | return false; |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | static const struct imdr *imd_entry_to_imdr(const struct imd *imd, |
| 386 | const struct imd_entry *entry) |
| 387 | { |
| 388 | if (imdr_has_entry(&imd->lg, entry)) |
| 389 | return &imd->lg; |
| 390 | |
| 391 | if (imdr_has_entry(&imd->sm, entry)) |
| 392 | return &imd->sm; |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | /* Initialize imd handle. */ |
| 398 | void imd_handle_init(struct imd *imd, void *upper_limit) |
| 399 | { |
| 400 | imdr_init(&imd->lg, upper_limit); |
| 401 | imdr_init(&imd->sm, NULL); |
| 402 | } |
| 403 | |
| 404 | void imd_handle_init_partial_recovery(struct imd *imd) |
| 405 | { |
| 406 | const struct imd_entry *e; |
| 407 | struct imd_root_pointer *rp; |
| 408 | struct imdr *imdr; |
| 409 | |
Aaron Durbin | 01562b6 | 2015-05-11 14:19:37 -0500 | [diff] [blame] | 410 | if (imd->lg.limit == 0) |
| 411 | return; |
| 412 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 413 | imd_handle_init(imd, (void *)imd->lg.limit); |
| 414 | |
| 415 | /* Initialize root pointer for the large regions. */ |
| 416 | imdr = &imd->lg; |
| 417 | rp = imdr_get_root_pointer(imdr); |
| 418 | imdr->r = relative_pointer(rp, rp->root_offset); |
| 419 | |
| 420 | e = imdr_entry_find(imdr, SMALL_REGION_ID); |
| 421 | |
| 422 | if (e == NULL) |
| 423 | return; |
| 424 | |
| 425 | imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 426 | imd->sm.limit += imdr_entry_size(imdr, e); |
| 427 | imdr = &imd->sm; |
| 428 | rp = imdr_get_root_pointer(imdr); |
| 429 | imdr->r = relative_pointer(rp, rp->root_offset); |
| 430 | } |
| 431 | |
| 432 | int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align) |
| 433 | { |
| 434 | return imdr_create_empty(&imd->lg, root_size, entry_align); |
| 435 | } |
| 436 | |
| 437 | int imd_create_tiered_empty(struct imd *imd, |
| 438 | size_t lg_root_size, size_t lg_entry_align, |
| 439 | size_t sm_root_size, size_t sm_entry_align) |
| 440 | { |
| 441 | size_t sm_region_size;; |
| 442 | const struct imd_entry *e; |
| 443 | struct imdr *imdr; |
| 444 | |
| 445 | imdr = &imd->lg; |
| 446 | |
| 447 | if (imdr_create_empty(imdr, lg_root_size, lg_entry_align) != 0) |
| 448 | return -1; |
| 449 | |
| 450 | /* Calculate the size of the small region to request. */ |
| 451 | sm_region_size = root_num_entries(sm_root_size) * sm_entry_align; |
| 452 | sm_region_size += sm_root_size; |
| 453 | sm_region_size = ALIGN_UP(sm_region_size, lg_entry_align); |
| 454 | |
| 455 | /* Add a new entry to the large region to cover the root and entries. */ |
| 456 | e = imdr_entry_add(imdr, SMALL_REGION_ID, sm_region_size); |
| 457 | |
| 458 | if (e == NULL) |
| 459 | goto fail; |
| 460 | |
| 461 | imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 462 | imd->sm.limit += sm_region_size; |
| 463 | |
| 464 | if (imdr_create_empty(&imd->sm, sm_root_size, sm_entry_align) != 0 || |
| 465 | imdr_limit_size(&imd->sm, sm_region_size)) |
| 466 | goto fail; |
| 467 | |
| 468 | return 0; |
| 469 | fail: |
| 470 | imd_handle_init(imd, (void *)imdr->limit); |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | int imd_recover(struct imd *imd) |
| 475 | { |
| 476 | const struct imd_entry *e; |
| 477 | uintptr_t small_upper_limit; |
| 478 | struct imdr *imdr; |
| 479 | |
| 480 | imdr = &imd->lg; |
| 481 | if (imdr_recover(imdr) != 0) |
| 482 | return -1; |
| 483 | |
| 484 | /* Determine if small region is region is present. */ |
| 485 | e = imdr_entry_find(imdr, SMALL_REGION_ID); |
| 486 | |
| 487 | if (e == NULL) |
| 488 | return 0; |
| 489 | |
| 490 | small_upper_limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 491 | small_upper_limit += imdr_entry_size(imdr, e); |
| 492 | |
| 493 | imd->sm.limit = small_upper_limit; |
| 494 | |
| 495 | /* Tear down any changes on failure. */ |
| 496 | if (imdr_recover(&imd->sm) != 0) { |
| 497 | imd_handle_init(imd, (void *)imd->lg.limit); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | int imd_limit_size(struct imd *imd, size_t max_size) |
| 505 | { |
| 506 | return imdr_limit_size(&imd->lg, max_size); |
| 507 | } |
| 508 | |
| 509 | int imd_lockdown(struct imd *imd) |
| 510 | { |
| 511 | struct imd_root *r; |
| 512 | |
| 513 | r = imdr_root(&imd->lg); |
| 514 | if (r == NULL) |
| 515 | return -1; |
| 516 | |
| 517 | r->flags |= IMD_FLAG_LOCKED; |
| 518 | |
| 519 | r = imdr_root(&imd->sm); |
| 520 | if (r != NULL) |
| 521 | r->flags |= IMD_FLAG_LOCKED; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int imd_region_used(struct imd *imd, void **base, size_t *size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 527 | { |
| 528 | struct imd_root *r; |
| 529 | struct imd_entry *e; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 530 | void *low_addr; |
| 531 | size_t sz_used; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 532 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 533 | if (!imd->lg.limit) |
| 534 | return -1; |
| 535 | |
| 536 | r = imdr_root(&imd->lg); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 537 | |
| 538 | if (r == NULL) |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 539 | return -1; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 540 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 541 | /* Use last entry to obtain lowest address. */ |
| 542 | e = root_last_entry(r); |
| 543 | |
| 544 | low_addr = relative_pointer(r, e->start_offset); |
| 545 | |
| 546 | /* Total size used is the last entry's base up to the limit. */ |
| 547 | sz_used = imd->lg.limit - (uintptr_t)low_addr; |
| 548 | |
| 549 | *base = low_addr; |
| 550 | *size = sz_used; |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id, |
| 556 | size_t size) |
| 557 | { |
| 558 | struct imd_root *r; |
| 559 | const struct imdr *imdr; |
| 560 | const struct imd_entry *e = NULL; |
| 561 | |
| 562 | /* |
| 563 | * Determine if requested size is less than 1/4 of small data |
| 564 | * region is left. |
| 565 | */ |
| 566 | imdr = &imd->sm; |
| 567 | r = imdr_root(imdr); |
| 568 | |
| 569 | /* No small region. Use the large region. */ |
| 570 | if (r == NULL) |
| 571 | return imdr_entry_add(&imd->lg, id, size); |
| 572 | else if (size <= r->entry_align || size <= imd_root_data_left(r) / 4) |
| 573 | e = imdr_entry_add(imdr, id, size); |
| 574 | |
| 575 | /* Fall back on large region allocation. */ |
| 576 | if (e == NULL) |
| 577 | e = imdr_entry_add(&imd->lg, id, size); |
| 578 | |
| 579 | return e; |
| 580 | } |
| 581 | |
| 582 | const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id) |
| 583 | { |
| 584 | const struct imd_entry *e; |
| 585 | |
| 586 | /* Many of the smaller allocations are used a lot. Therefore, try |
| 587 | * the small region first. */ |
| 588 | e = imdr_entry_find(&imd->sm, id); |
| 589 | |
| 590 | if (e == NULL) |
| 591 | e = imdr_entry_find(&imd->lg, id); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 592 | |
| 593 | return e; |
| 594 | } |
| 595 | |
| 596 | const struct imd_entry *imd_entry_find_or_add(const struct imd *imd, |
| 597 | uint32_t id, size_t size) |
| 598 | { |
| 599 | const struct imd_entry *e; |
| 600 | |
| 601 | e = imd_entry_find(imd, id); |
| 602 | |
| 603 | if (e != NULL) |
| 604 | return e; |
| 605 | |
| 606 | return imd_entry_add(imd, id, size); |
| 607 | } |
| 608 | |
| 609 | size_t imd_entry_size(const struct imd *imd, const struct imd_entry *entry) |
| 610 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 611 | return imdr_entry_size(NULL, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry) |
| 615 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 616 | const struct imdr *imdr; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 617 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 618 | imdr = imd_entry_to_imdr(imd, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 619 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 620 | if (imdr == NULL) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 621 | return NULL; |
| 622 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 623 | return imdr_entry_at(imdr, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 624 | } |
| 625 | |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 626 | uint32_t imd_entry_id(const struct imd *imd, const struct imd_entry *entry) |
| 627 | { |
| 628 | return entry->id; |
| 629 | } |
| 630 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 631 | int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry) |
| 632 | { |
| 633 | struct imd_root *r; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 634 | const struct imdr *imdr; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 635 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 636 | imdr = imd_entry_to_imdr(imd, entry); |
| 637 | |
| 638 | if (imdr == NULL) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 639 | return -1; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 640 | |
| 641 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 642 | |
| 643 | if (r == NULL) |
| 644 | return -1; |
| 645 | |
| 646 | if (root_is_locked(r)) |
| 647 | return -1; |
| 648 | |
| 649 | if (entry != root_last_entry(r)) |
| 650 | return -1; |
| 651 | |
| 652 | r->num_entries--; |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 657 | static void imdr_print_entries(const struct imdr *imdr, const char *indent, |
| 658 | const struct imd_lookup *lookup, size_t size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 659 | { |
| 660 | struct imd_root *r; |
| 661 | size_t i; |
| 662 | size_t j; |
| 663 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 664 | if (imdr == NULL) |
| 665 | return; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 666 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 667 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 668 | |
| 669 | for (i = 0; i < r->num_entries; i++) { |
| 670 | const char *name = NULL; |
| 671 | const struct imd_entry *e = &r->entries[i]; |
| 672 | |
| 673 | for (j = 0; j < size; j++) { |
| 674 | if (lookup[j].id == e->id) { |
| 675 | name = lookup[j].name; |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 680 | printk(BIOS_DEBUG, "%s", indent); |
| 681 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 682 | if (name == NULL) |
| 683 | printk(BIOS_DEBUG, "%08x ", e->id); |
| 684 | else |
| 685 | printk(BIOS_DEBUG, "%s", name); |
| 686 | printk(BIOS_DEBUG, "%2zu. ", i); |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 687 | printk(BIOS_DEBUG, "%p ", imdr_entry_at(imdr, e)); |
| 688 | printk(BIOS_DEBUG, "%08zx\n", imdr_entry_size(imdr, e)); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup, |
| 693 | size_t size) |
| 694 | { |
| 695 | if (imdr_root(&imd->lg) == NULL) |
| 696 | return -1; |
| 697 | |
| 698 | imdr_print_entries(&imd->lg, "", lookup, size); |
| 699 | if (imdr_root(&imd->sm) != NULL) { |
| 700 | printk(BIOS_DEBUG, "IMD small region:\n"); |
| 701 | imdr_print_entries(&imd->sm, " ", lookup, size); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 706 | |
| 707 | int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor) |
| 708 | { |
| 709 | if (imd == NULL || cursor == NULL) |
| 710 | return -1; |
| 711 | |
| 712 | memset(cursor, 0, sizeof(*cursor)); |
| 713 | |
| 714 | cursor->imdr[0] = &imd->lg; |
| 715 | cursor->imdr[1] = &imd->sm; |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor) |
| 721 | { |
| 722 | struct imd_root *r; |
| 723 | const struct imd_entry *e; |
| 724 | |
| 725 | if (cursor->current_imdr >= ARRAY_SIZE(cursor->imdr)) |
| 726 | return NULL; |
| 727 | |
| 728 | r = imdr_root(cursor->imdr[cursor->current_imdr]); |
| 729 | |
| 730 | if (r == NULL) |
| 731 | return NULL; |
| 732 | |
| 733 | if (cursor->current_entry >= r->num_entries) { |
| 734 | /* Try next imdr. */ |
| 735 | cursor->current_imdr++; |
| 736 | cursor->current_entry = 0; |
| 737 | return imd_cursor_next(cursor); |
| 738 | } |
| 739 | |
| 740 | e = &r->entries[cursor->current_entry]; |
| 741 | cursor->current_entry++; |
| 742 | |
| 743 | return e; |
| 744 | } |