.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * AMD CPU Microcode Update Driver for Linux |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * CPUs and later. |
---|
6 | 7 | * |
---|
7 | 8 | * Copyright (C) 2008-2011 Advanced Micro Devices Inc. |
---|
8 | | - * 2013-2016 Borislav Petkov <bp@alien8.de> |
---|
| 9 | + * 2013-2018 Borislav Petkov <bp@alien8.de> |
---|
9 | 10 | * |
---|
10 | 11 | * Author: Peter Oruba <peter.oruba@amd.com> |
---|
11 | 12 | * |
---|
.. | .. |
---|
17 | 18 | * |
---|
18 | 19 | * Author: Jacob Shin <jacob.shin@amd.com> |
---|
19 | 20 | * Fixes: Borislav Petkov <bp@suse.de> |
---|
20 | | - * |
---|
21 | | - * Licensed under the terms of the GNU General Public |
---|
22 | | - * License version 2. See file COPYING for details. |
---|
23 | 21 | */ |
---|
24 | 22 | #define pr_fmt(fmt) "microcode: " fmt |
---|
25 | 23 | |
---|
.. | .. |
---|
38 | 36 | #include <asm/cpu.h> |
---|
39 | 37 | #include <asm/msr.h> |
---|
40 | 38 | |
---|
41 | | -static struct equiv_cpu_entry *equiv_cpu_table; |
---|
| 39 | +static struct equiv_cpu_table { |
---|
| 40 | + unsigned int num_entries; |
---|
| 41 | + struct equiv_cpu_entry *entry; |
---|
| 42 | +} equiv_table; |
---|
42 | 43 | |
---|
43 | 44 | /* |
---|
44 | 45 | * This points to the current valid container of microcode patches which we will |
---|
.. | .. |
---|
58 | 59 | |
---|
59 | 60 | /* |
---|
60 | 61 | * Microcode patch container file is prepended to the initrd in cpio |
---|
61 | | - * format. See Documentation/x86/microcode.txt |
---|
| 62 | + * format. See Documentation/x86/microcode.rst |
---|
62 | 63 | */ |
---|
63 | 64 | static const char |
---|
64 | 65 | ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin"; |
---|
65 | 66 | |
---|
66 | | -static u16 find_equiv_id(struct equiv_cpu_entry *equiv_table, u32 sig) |
---|
| 67 | +static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig) |
---|
67 | 68 | { |
---|
68 | | - for (; equiv_table && equiv_table->installed_cpu; equiv_table++) { |
---|
69 | | - if (sig == equiv_table->installed_cpu) |
---|
70 | | - return equiv_table->equiv_cpu; |
---|
| 69 | + unsigned int i; |
---|
| 70 | + |
---|
| 71 | + if (!et || !et->num_entries) |
---|
| 72 | + return 0; |
---|
| 73 | + |
---|
| 74 | + for (i = 0; i < et->num_entries; i++) { |
---|
| 75 | + struct equiv_cpu_entry *e = &et->entry[i]; |
---|
| 76 | + |
---|
| 77 | + if (sig == e->installed_cpu) |
---|
| 78 | + return e->equiv_cpu; |
---|
| 79 | + |
---|
| 80 | + e++; |
---|
71 | 81 | } |
---|
| 82 | + return 0; |
---|
| 83 | +} |
---|
| 84 | + |
---|
| 85 | +/* |
---|
| 86 | + * Check whether there is a valid microcode container file at the beginning |
---|
| 87 | + * of @buf of size @buf_size. Set @early to use this function in the early path. |
---|
| 88 | + */ |
---|
| 89 | +static bool verify_container(const u8 *buf, size_t buf_size, bool early) |
---|
| 90 | +{ |
---|
| 91 | + u32 cont_magic; |
---|
| 92 | + |
---|
| 93 | + if (buf_size <= CONTAINER_HDR_SZ) { |
---|
| 94 | + if (!early) |
---|
| 95 | + pr_debug("Truncated microcode container header.\n"); |
---|
| 96 | + |
---|
| 97 | + return false; |
---|
| 98 | + } |
---|
| 99 | + |
---|
| 100 | + cont_magic = *(const u32 *)buf; |
---|
| 101 | + if (cont_magic != UCODE_MAGIC) { |
---|
| 102 | + if (!early) |
---|
| 103 | + pr_debug("Invalid magic value (0x%08x).\n", cont_magic); |
---|
| 104 | + |
---|
| 105 | + return false; |
---|
| 106 | + } |
---|
| 107 | + |
---|
| 108 | + return true; |
---|
| 109 | +} |
---|
| 110 | + |
---|
| 111 | +/* |
---|
| 112 | + * Check whether there is a valid, non-truncated CPU equivalence table at the |
---|
| 113 | + * beginning of @buf of size @buf_size. Set @early to use this function in the |
---|
| 114 | + * early path. |
---|
| 115 | + */ |
---|
| 116 | +static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early) |
---|
| 117 | +{ |
---|
| 118 | + const u32 *hdr = (const u32 *)buf; |
---|
| 119 | + u32 cont_type, equiv_tbl_len; |
---|
| 120 | + |
---|
| 121 | + if (!verify_container(buf, buf_size, early)) |
---|
| 122 | + return false; |
---|
| 123 | + |
---|
| 124 | + cont_type = hdr[1]; |
---|
| 125 | + if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) { |
---|
| 126 | + if (!early) |
---|
| 127 | + pr_debug("Wrong microcode container equivalence table type: %u.\n", |
---|
| 128 | + cont_type); |
---|
| 129 | + |
---|
| 130 | + return false; |
---|
| 131 | + } |
---|
| 132 | + |
---|
| 133 | + buf_size -= CONTAINER_HDR_SZ; |
---|
| 134 | + |
---|
| 135 | + equiv_tbl_len = hdr[2]; |
---|
| 136 | + if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) || |
---|
| 137 | + buf_size < equiv_tbl_len) { |
---|
| 138 | + if (!early) |
---|
| 139 | + pr_debug("Truncated equivalence table.\n"); |
---|
| 140 | + |
---|
| 141 | + return false; |
---|
| 142 | + } |
---|
| 143 | + |
---|
| 144 | + return true; |
---|
| 145 | +} |
---|
| 146 | + |
---|
| 147 | +/* |
---|
| 148 | + * Check whether there is a valid, non-truncated microcode patch section at the |
---|
| 149 | + * beginning of @buf of size @buf_size. Set @early to use this function in the |
---|
| 150 | + * early path. |
---|
| 151 | + * |
---|
| 152 | + * On success, @sh_psize returns the patch size according to the section header, |
---|
| 153 | + * to the caller. |
---|
| 154 | + */ |
---|
| 155 | +static bool |
---|
| 156 | +__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize, bool early) |
---|
| 157 | +{ |
---|
| 158 | + u32 p_type, p_size; |
---|
| 159 | + const u32 *hdr; |
---|
| 160 | + |
---|
| 161 | + if (buf_size < SECTION_HDR_SIZE) { |
---|
| 162 | + if (!early) |
---|
| 163 | + pr_debug("Truncated patch section.\n"); |
---|
| 164 | + |
---|
| 165 | + return false; |
---|
| 166 | + } |
---|
| 167 | + |
---|
| 168 | + hdr = (const u32 *)buf; |
---|
| 169 | + p_type = hdr[0]; |
---|
| 170 | + p_size = hdr[1]; |
---|
| 171 | + |
---|
| 172 | + if (p_type != UCODE_UCODE_TYPE) { |
---|
| 173 | + if (!early) |
---|
| 174 | + pr_debug("Invalid type field (0x%x) in container file section header.\n", |
---|
| 175 | + p_type); |
---|
| 176 | + |
---|
| 177 | + return false; |
---|
| 178 | + } |
---|
| 179 | + |
---|
| 180 | + if (p_size < sizeof(struct microcode_header_amd)) { |
---|
| 181 | + if (!early) |
---|
| 182 | + pr_debug("Patch of size %u too short.\n", p_size); |
---|
| 183 | + |
---|
| 184 | + return false; |
---|
| 185 | + } |
---|
| 186 | + |
---|
| 187 | + *sh_psize = p_size; |
---|
| 188 | + |
---|
| 189 | + return true; |
---|
| 190 | +} |
---|
| 191 | + |
---|
| 192 | +/* |
---|
| 193 | + * Check whether the passed remaining file @buf_size is large enough to contain |
---|
| 194 | + * a patch of the indicated @sh_psize (and also whether this size does not |
---|
| 195 | + * exceed the per-family maximum). @sh_psize is the size read from the section |
---|
| 196 | + * header. |
---|
| 197 | + */ |
---|
| 198 | +static unsigned int __verify_patch_size(u8 family, u32 sh_psize, size_t buf_size) |
---|
| 199 | +{ |
---|
| 200 | + u32 max_size; |
---|
| 201 | + |
---|
| 202 | + if (family >= 0x15) |
---|
| 203 | + return min_t(u32, sh_psize, buf_size); |
---|
| 204 | + |
---|
| 205 | +#define F1XH_MPB_MAX_SIZE 2048 |
---|
| 206 | +#define F14H_MPB_MAX_SIZE 1824 |
---|
| 207 | + |
---|
| 208 | + switch (family) { |
---|
| 209 | + case 0x10 ... 0x12: |
---|
| 210 | + max_size = F1XH_MPB_MAX_SIZE; |
---|
| 211 | + break; |
---|
| 212 | + case 0x14: |
---|
| 213 | + max_size = F14H_MPB_MAX_SIZE; |
---|
| 214 | + break; |
---|
| 215 | + default: |
---|
| 216 | + WARN(1, "%s: WTF family: 0x%x\n", __func__, family); |
---|
| 217 | + return 0; |
---|
| 218 | + break; |
---|
| 219 | + } |
---|
| 220 | + |
---|
| 221 | + if (sh_psize > min_t(u32, buf_size, max_size)) |
---|
| 222 | + return 0; |
---|
| 223 | + |
---|
| 224 | + return sh_psize; |
---|
| 225 | +} |
---|
| 226 | + |
---|
| 227 | +/* |
---|
| 228 | + * Verify the patch in @buf. |
---|
| 229 | + * |
---|
| 230 | + * Returns: |
---|
| 231 | + * negative: on error |
---|
| 232 | + * positive: patch is not for this family, skip it |
---|
| 233 | + * 0: success |
---|
| 234 | + */ |
---|
| 235 | +static int |
---|
| 236 | +verify_patch(u8 family, const u8 *buf, size_t buf_size, u32 *patch_size, bool early) |
---|
| 237 | +{ |
---|
| 238 | + struct microcode_header_amd *mc_hdr; |
---|
| 239 | + unsigned int ret; |
---|
| 240 | + u32 sh_psize; |
---|
| 241 | + u16 proc_id; |
---|
| 242 | + u8 patch_fam; |
---|
| 243 | + |
---|
| 244 | + if (!__verify_patch_section(buf, buf_size, &sh_psize, early)) |
---|
| 245 | + return -1; |
---|
| 246 | + |
---|
| 247 | + /* |
---|
| 248 | + * The section header length is not included in this indicated size |
---|
| 249 | + * but is present in the leftover file length so we need to subtract |
---|
| 250 | + * it before passing this value to the function below. |
---|
| 251 | + */ |
---|
| 252 | + buf_size -= SECTION_HDR_SIZE; |
---|
| 253 | + |
---|
| 254 | + /* |
---|
| 255 | + * Check if the remaining buffer is big enough to contain a patch of |
---|
| 256 | + * size sh_psize, as the section claims. |
---|
| 257 | + */ |
---|
| 258 | + if (buf_size < sh_psize) { |
---|
| 259 | + if (!early) |
---|
| 260 | + pr_debug("Patch of size %u truncated.\n", sh_psize); |
---|
| 261 | + |
---|
| 262 | + return -1; |
---|
| 263 | + } |
---|
| 264 | + |
---|
| 265 | + ret = __verify_patch_size(family, sh_psize, buf_size); |
---|
| 266 | + if (!ret) { |
---|
| 267 | + if (!early) |
---|
| 268 | + pr_debug("Per-family patch size mismatch.\n"); |
---|
| 269 | + return -1; |
---|
| 270 | + } |
---|
| 271 | + |
---|
| 272 | + *patch_size = sh_psize; |
---|
| 273 | + |
---|
| 274 | + mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE); |
---|
| 275 | + if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { |
---|
| 276 | + if (!early) |
---|
| 277 | + pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id); |
---|
| 278 | + return -1; |
---|
| 279 | + } |
---|
| 280 | + |
---|
| 281 | + proc_id = mc_hdr->processor_rev_id; |
---|
| 282 | + patch_fam = 0xf + (proc_id >> 12); |
---|
| 283 | + if (patch_fam != family) |
---|
| 284 | + return 1; |
---|
72 | 285 | |
---|
73 | 286 | return 0; |
---|
74 | 287 | } |
---|
.. | .. |
---|
80 | 293 | * Returns the amount of bytes consumed while scanning. @desc contains all the |
---|
81 | 294 | * data we're going to use in later stages of the application. |
---|
82 | 295 | */ |
---|
83 | | -static ssize_t parse_container(u8 *ucode, ssize_t size, struct cont_desc *desc) |
---|
| 296 | +static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc) |
---|
84 | 297 | { |
---|
85 | | - struct equiv_cpu_entry *eq; |
---|
86 | | - ssize_t orig_size = size; |
---|
| 298 | + struct equiv_cpu_table table; |
---|
| 299 | + size_t orig_size = size; |
---|
87 | 300 | u32 *hdr = (u32 *)ucode; |
---|
88 | 301 | u16 eq_id; |
---|
89 | 302 | u8 *buf; |
---|
90 | 303 | |
---|
91 | | - /* Am I looking at an equivalence table header? */ |
---|
92 | | - if (hdr[0] != UCODE_MAGIC || |
---|
93 | | - hdr[1] != UCODE_EQUIV_CPU_TABLE_TYPE || |
---|
94 | | - hdr[2] == 0) |
---|
95 | | - return CONTAINER_HDR_SZ; |
---|
| 304 | + if (!verify_equivalence_table(ucode, size, true)) |
---|
| 305 | + return 0; |
---|
96 | 306 | |
---|
97 | 307 | buf = ucode; |
---|
98 | 308 | |
---|
99 | | - eq = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ); |
---|
| 309 | + table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ); |
---|
| 310 | + table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry); |
---|
100 | 311 | |
---|
101 | | - /* Find the equivalence ID of our CPU in this table: */ |
---|
102 | | - eq_id = find_equiv_id(eq, desc->cpuid_1_eax); |
---|
| 312 | + /* |
---|
| 313 | + * Find the equivalence ID of our CPU in this table. Even if this table |
---|
| 314 | + * doesn't contain a patch for the CPU, scan through the whole container |
---|
| 315 | + * so that it can be skipped in case there are other containers appended. |
---|
| 316 | + */ |
---|
| 317 | + eq_id = find_equiv_id(&table, desc->cpuid_1_eax); |
---|
103 | 318 | |
---|
104 | 319 | buf += hdr[2] + CONTAINER_HDR_SZ; |
---|
105 | 320 | size -= hdr[2] + CONTAINER_HDR_SZ; |
---|
.. | .. |
---|
111 | 326 | while (size > 0) { |
---|
112 | 327 | struct microcode_amd *mc; |
---|
113 | 328 | u32 patch_size; |
---|
| 329 | + int ret; |
---|
114 | 330 | |
---|
115 | | - hdr = (u32 *)buf; |
---|
| 331 | + ret = verify_patch(x86_family(desc->cpuid_1_eax), buf, size, &patch_size, true); |
---|
| 332 | + if (ret < 0) { |
---|
| 333 | + /* |
---|
| 334 | + * Patch verification failed, skip to the next |
---|
| 335 | + * container, if there's one: |
---|
| 336 | + */ |
---|
| 337 | + goto out; |
---|
| 338 | + } else if (ret > 0) { |
---|
| 339 | + goto skip; |
---|
| 340 | + } |
---|
116 | 341 | |
---|
117 | | - if (hdr[0] != UCODE_UCODE_TYPE) |
---|
118 | | - break; |
---|
119 | | - |
---|
120 | | - /* Sanity-check patch size. */ |
---|
121 | | - patch_size = hdr[1]; |
---|
122 | | - if (patch_size > PATCH_MAX_SIZE) |
---|
123 | | - break; |
---|
124 | | - |
---|
125 | | - /* Skip patch section header: */ |
---|
126 | | - buf += SECTION_HDR_SIZE; |
---|
127 | | - size -= SECTION_HDR_SIZE; |
---|
128 | | - |
---|
129 | | - mc = (struct microcode_amd *)buf; |
---|
| 342 | + mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE); |
---|
130 | 343 | if (eq_id == mc->hdr.processor_rev_id) { |
---|
131 | 344 | desc->psize = patch_size; |
---|
132 | 345 | desc->mc = mc; |
---|
133 | 346 | } |
---|
134 | 347 | |
---|
135 | | - buf += patch_size; |
---|
136 | | - size -= patch_size; |
---|
| 348 | +skip: |
---|
| 349 | + /* Skip patch section header too: */ |
---|
| 350 | + buf += patch_size + SECTION_HDR_SIZE; |
---|
| 351 | + size -= patch_size + SECTION_HDR_SIZE; |
---|
137 | 352 | } |
---|
138 | 353 | |
---|
139 | 354 | /* |
---|
.. | .. |
---|
150 | 365 | return 0; |
---|
151 | 366 | } |
---|
152 | 367 | |
---|
| 368 | +out: |
---|
153 | 369 | return orig_size - size; |
---|
154 | 370 | } |
---|
155 | 371 | |
---|
.. | .. |
---|
159 | 375 | */ |
---|
160 | 376 | static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc) |
---|
161 | 377 | { |
---|
162 | | - ssize_t rem = size; |
---|
163 | | - |
---|
164 | | - while (rem >= 0) { |
---|
165 | | - ssize_t s = parse_container(ucode, rem, desc); |
---|
| 378 | + while (size) { |
---|
| 379 | + size_t s = parse_container(ucode, size, desc); |
---|
166 | 380 | if (!s) |
---|
167 | 381 | return; |
---|
168 | 382 | |
---|
169 | | - ucode += s; |
---|
170 | | - rem -= s; |
---|
| 383 | + /* catch wraparound */ |
---|
| 384 | + if (size >= s) { |
---|
| 385 | + ucode += s; |
---|
| 386 | + size -= s; |
---|
| 387 | + } else { |
---|
| 388 | + return; |
---|
| 389 | + } |
---|
171 | 390 | } |
---|
172 | 391 | } |
---|
173 | 392 | |
---|
.. | .. |
---|
222 | 441 | return ret; |
---|
223 | 442 | |
---|
224 | 443 | native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); |
---|
225 | | - if (rev >= mc->hdr.patch_id) |
---|
| 444 | + |
---|
| 445 | + /* |
---|
| 446 | + * Allow application of the same revision to pick up SMT-specific |
---|
| 447 | + * changes even if the revision of the other SMT thread is already |
---|
| 448 | + * up-to-date. |
---|
| 449 | + */ |
---|
| 450 | + if (rev > mc->hdr.patch_id) |
---|
226 | 451 | return ret; |
---|
227 | 452 | |
---|
228 | 453 | if (!__apply_microcode_amd(mc)) { |
---|
.. | .. |
---|
304 | 529 | |
---|
305 | 530 | native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); |
---|
306 | 531 | |
---|
307 | | - /* Check whether we have saved a new patch already: */ |
---|
308 | | - if (*new_rev && rev < mc->hdr.patch_id) { |
---|
| 532 | + /* |
---|
| 533 | + * Check whether a new patch has been saved already. Also, allow application of |
---|
| 534 | + * the same revision in order to pick up SMT-thread-specific configuration even |
---|
| 535 | + * if the sibling SMT thread already has an up-to-date revision. |
---|
| 536 | + */ |
---|
| 537 | + if (*new_rev && rev <= mc->hdr.patch_id) { |
---|
309 | 538 | if (!__apply_microcode_amd(mc)) { |
---|
310 | 539 | *new_rev = mc->hdr.patch_id; |
---|
311 | 540 | return; |
---|
.. | .. |
---|
348 | 577 | void reload_ucode_amd(void) |
---|
349 | 578 | { |
---|
350 | 579 | struct microcode_amd *mc; |
---|
351 | | - u32 rev, dummy; |
---|
| 580 | + u32 rev, dummy __always_unused; |
---|
352 | 581 | |
---|
353 | 582 | mc = (struct microcode_amd *)amd_ucode_patch; |
---|
354 | 583 | |
---|
.. | .. |
---|
364 | 593 | static u16 __find_equiv_id(unsigned int cpu) |
---|
365 | 594 | { |
---|
366 | 595 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
---|
367 | | - return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig); |
---|
368 | | -} |
---|
369 | | - |
---|
370 | | -static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu) |
---|
371 | | -{ |
---|
372 | | - int i = 0; |
---|
373 | | - |
---|
374 | | - BUG_ON(!equiv_cpu_table); |
---|
375 | | - |
---|
376 | | - while (equiv_cpu_table[i].equiv_cpu != 0) { |
---|
377 | | - if (equiv_cpu == equiv_cpu_table[i].equiv_cpu) |
---|
378 | | - return equiv_cpu_table[i].installed_cpu; |
---|
379 | | - i++; |
---|
380 | | - } |
---|
381 | | - return 0; |
---|
| 596 | + return find_equiv_id(&equiv_table, uci->cpu_sig.sig); |
---|
382 | 597 | } |
---|
383 | 598 | |
---|
384 | 599 | /* |
---|
.. | .. |
---|
461 | 676 | return 0; |
---|
462 | 677 | } |
---|
463 | 678 | |
---|
464 | | -static unsigned int verify_patch_size(u8 family, u32 patch_size, |
---|
465 | | - unsigned int size) |
---|
466 | | -{ |
---|
467 | | - u32 max_size; |
---|
468 | | - |
---|
469 | | -#define F1XH_MPB_MAX_SIZE 2048 |
---|
470 | | -#define F14H_MPB_MAX_SIZE 1824 |
---|
471 | | -#define F15H_MPB_MAX_SIZE 4096 |
---|
472 | | -#define F16H_MPB_MAX_SIZE 3458 |
---|
473 | | -#define F17H_MPB_MAX_SIZE 3200 |
---|
474 | | - |
---|
475 | | - switch (family) { |
---|
476 | | - case 0x14: |
---|
477 | | - max_size = F14H_MPB_MAX_SIZE; |
---|
478 | | - break; |
---|
479 | | - case 0x15: |
---|
480 | | - max_size = F15H_MPB_MAX_SIZE; |
---|
481 | | - break; |
---|
482 | | - case 0x16: |
---|
483 | | - max_size = F16H_MPB_MAX_SIZE; |
---|
484 | | - break; |
---|
485 | | - case 0x17: |
---|
486 | | - max_size = F17H_MPB_MAX_SIZE; |
---|
487 | | - break; |
---|
488 | | - default: |
---|
489 | | - max_size = F1XH_MPB_MAX_SIZE; |
---|
490 | | - break; |
---|
491 | | - } |
---|
492 | | - |
---|
493 | | - if (patch_size > min_t(u32, size, max_size)) { |
---|
494 | | - pr_err("patch size mismatch\n"); |
---|
495 | | - return 0; |
---|
496 | | - } |
---|
497 | | - |
---|
498 | | - return patch_size; |
---|
499 | | -} |
---|
500 | | - |
---|
501 | 679 | static enum ucode_state apply_microcode_amd(int cpu) |
---|
502 | 680 | { |
---|
503 | 681 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
---|
.. | .. |
---|
505 | 683 | struct ucode_cpu_info *uci; |
---|
506 | 684 | struct ucode_patch *p; |
---|
507 | 685 | enum ucode_state ret; |
---|
508 | | - u32 rev, dummy; |
---|
| 686 | + u32 rev, dummy __always_unused; |
---|
509 | 687 | |
---|
510 | 688 | BUG_ON(raw_smp_processor_id() != cpu); |
---|
511 | 689 | |
---|
.. | .. |
---|
548 | 726 | return ret; |
---|
549 | 727 | } |
---|
550 | 728 | |
---|
551 | | -static int install_equiv_cpu_table(const u8 *buf) |
---|
| 729 | +static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size) |
---|
552 | 730 | { |
---|
553 | | - unsigned int *ibuf = (unsigned int *)buf; |
---|
554 | | - unsigned int type = ibuf[1]; |
---|
555 | | - unsigned int size = ibuf[2]; |
---|
| 731 | + u32 equiv_tbl_len; |
---|
| 732 | + const u32 *hdr; |
---|
556 | 733 | |
---|
557 | | - if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) { |
---|
558 | | - pr_err("empty section/" |
---|
559 | | - "invalid type field in container file section header\n"); |
---|
560 | | - return -EINVAL; |
---|
561 | | - } |
---|
| 734 | + if (!verify_equivalence_table(buf, buf_size, false)) |
---|
| 735 | + return 0; |
---|
562 | 736 | |
---|
563 | | - equiv_cpu_table = vmalloc(size); |
---|
564 | | - if (!equiv_cpu_table) { |
---|
| 737 | + hdr = (const u32 *)buf; |
---|
| 738 | + equiv_tbl_len = hdr[2]; |
---|
| 739 | + |
---|
| 740 | + equiv_table.entry = vmalloc(equiv_tbl_len); |
---|
| 741 | + if (!equiv_table.entry) { |
---|
565 | 742 | pr_err("failed to allocate equivalent CPU table\n"); |
---|
566 | | - return -ENOMEM; |
---|
| 743 | + return 0; |
---|
567 | 744 | } |
---|
568 | 745 | |
---|
569 | | - memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size); |
---|
| 746 | + memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len); |
---|
| 747 | + equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry); |
---|
570 | 748 | |
---|
571 | 749 | /* add header length */ |
---|
572 | | - return size + CONTAINER_HDR_SZ; |
---|
| 750 | + return equiv_tbl_len + CONTAINER_HDR_SZ; |
---|
573 | 751 | } |
---|
574 | 752 | |
---|
575 | 753 | static void free_equiv_cpu_table(void) |
---|
576 | 754 | { |
---|
577 | | - vfree(equiv_cpu_table); |
---|
578 | | - equiv_cpu_table = NULL; |
---|
| 755 | + vfree(equiv_table.entry); |
---|
| 756 | + memset(&equiv_table, 0, sizeof(equiv_table)); |
---|
579 | 757 | } |
---|
580 | 758 | |
---|
581 | 759 | static void cleanup(void) |
---|
.. | .. |
---|
585 | 763 | } |
---|
586 | 764 | |
---|
587 | 765 | /* |
---|
588 | | - * We return the current size even if some of the checks failed so that |
---|
| 766 | + * Return a non-negative value even if some of the checks failed so that |
---|
589 | 767 | * we can skip over the next patch. If we return a negative value, we |
---|
590 | 768 | * signal a grave error like a memory allocation has failed and the |
---|
591 | 769 | * driver cannot continue functioning normally. In such cases, we tear |
---|
592 | 770 | * down everything we've used up so far and exit. |
---|
593 | 771 | */ |
---|
594 | | -static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover) |
---|
| 772 | +static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover, |
---|
| 773 | + unsigned int *patch_size) |
---|
595 | 774 | { |
---|
596 | 775 | struct microcode_header_amd *mc_hdr; |
---|
597 | 776 | struct ucode_patch *patch; |
---|
598 | | - unsigned int patch_size, crnt_size, ret; |
---|
599 | | - u32 proc_fam; |
---|
600 | 777 | u16 proc_id; |
---|
| 778 | + int ret; |
---|
601 | 779 | |
---|
602 | | - patch_size = *(u32 *)(fw + 4); |
---|
603 | | - crnt_size = patch_size + SECTION_HDR_SIZE; |
---|
604 | | - mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); |
---|
605 | | - proc_id = mc_hdr->processor_rev_id; |
---|
606 | | - |
---|
607 | | - proc_fam = find_cpu_family_by_equiv_cpu(proc_id); |
---|
608 | | - if (!proc_fam) { |
---|
609 | | - pr_err("No patch family for equiv ID: 0x%04x\n", proc_id); |
---|
610 | | - return crnt_size; |
---|
611 | | - } |
---|
612 | | - |
---|
613 | | - /* check if patch is for the current family */ |
---|
614 | | - proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff); |
---|
615 | | - if (proc_fam != family) |
---|
616 | | - return crnt_size; |
---|
617 | | - |
---|
618 | | - if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { |
---|
619 | | - pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", |
---|
620 | | - mc_hdr->patch_id); |
---|
621 | | - return crnt_size; |
---|
622 | | - } |
---|
623 | | - |
---|
624 | | - ret = verify_patch_size(family, patch_size, leftover); |
---|
625 | | - if (!ret) { |
---|
626 | | - pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id); |
---|
627 | | - return crnt_size; |
---|
628 | | - } |
---|
| 780 | + ret = verify_patch(family, fw, leftover, patch_size, false); |
---|
| 781 | + if (ret) |
---|
| 782 | + return ret; |
---|
629 | 783 | |
---|
630 | 784 | patch = kzalloc(sizeof(*patch), GFP_KERNEL); |
---|
631 | 785 | if (!patch) { |
---|
.. | .. |
---|
633 | 787 | return -EINVAL; |
---|
634 | 788 | } |
---|
635 | 789 | |
---|
636 | | - patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL); |
---|
| 790 | + patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL); |
---|
637 | 791 | if (!patch->data) { |
---|
638 | 792 | pr_err("Patch data allocation failure.\n"); |
---|
639 | 793 | kfree(patch); |
---|
640 | 794 | return -EINVAL; |
---|
641 | 795 | } |
---|
| 796 | + patch->size = *patch_size; |
---|
| 797 | + |
---|
| 798 | + mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); |
---|
| 799 | + proc_id = mc_hdr->processor_rev_id; |
---|
642 | 800 | |
---|
643 | 801 | INIT_LIST_HEAD(&patch->plist); |
---|
644 | 802 | patch->patch_id = mc_hdr->patch_id; |
---|
.. | .. |
---|
650 | 808 | /* ... and add to cache. */ |
---|
651 | 809 | update_cache(patch); |
---|
652 | 810 | |
---|
653 | | - return crnt_size; |
---|
| 811 | + return 0; |
---|
654 | 812 | } |
---|
655 | 813 | |
---|
656 | 814 | static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, |
---|
657 | 815 | size_t size) |
---|
658 | 816 | { |
---|
659 | | - enum ucode_state ret = UCODE_ERROR; |
---|
660 | | - unsigned int leftover; |
---|
661 | 817 | u8 *fw = (u8 *)data; |
---|
662 | | - int crnt_size = 0; |
---|
663 | | - int offset; |
---|
| 818 | + size_t offset; |
---|
664 | 819 | |
---|
665 | | - offset = install_equiv_cpu_table(data); |
---|
666 | | - if (offset < 0) { |
---|
667 | | - pr_err("failed to create equivalent cpu table\n"); |
---|
668 | | - return ret; |
---|
669 | | - } |
---|
670 | | - fw += offset; |
---|
671 | | - leftover = size - offset; |
---|
| 820 | + offset = install_equiv_cpu_table(data, size); |
---|
| 821 | + if (!offset) |
---|
| 822 | + return UCODE_ERROR; |
---|
| 823 | + |
---|
| 824 | + fw += offset; |
---|
| 825 | + size -= offset; |
---|
672 | 826 | |
---|
673 | 827 | if (*(u32 *)fw != UCODE_UCODE_TYPE) { |
---|
674 | 828 | pr_err("invalid type field in container file section header\n"); |
---|
675 | 829 | free_equiv_cpu_table(); |
---|
676 | | - return ret; |
---|
| 830 | + return UCODE_ERROR; |
---|
677 | 831 | } |
---|
678 | 832 | |
---|
679 | | - while (leftover) { |
---|
680 | | - crnt_size = verify_and_add_patch(family, fw, leftover); |
---|
681 | | - if (crnt_size < 0) |
---|
682 | | - return ret; |
---|
| 833 | + while (size > 0) { |
---|
| 834 | + unsigned int crnt_size = 0; |
---|
| 835 | + int ret; |
---|
683 | 836 | |
---|
684 | | - fw += crnt_size; |
---|
685 | | - leftover -= crnt_size; |
---|
| 837 | + ret = verify_and_add_patch(family, fw, size, &crnt_size); |
---|
| 838 | + if (ret < 0) |
---|
| 839 | + return UCODE_ERROR; |
---|
| 840 | + |
---|
| 841 | + fw += crnt_size + SECTION_HDR_SIZE; |
---|
| 842 | + size -= (crnt_size + SECTION_HDR_SIZE); |
---|
686 | 843 | } |
---|
687 | 844 | |
---|
688 | 845 | return UCODE_OK; |
---|
.. | .. |
---|
718 | 875 | return ret; |
---|
719 | 876 | |
---|
720 | 877 | memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); |
---|
721 | | - memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE)); |
---|
| 878 | + memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); |
---|
722 | 879 | |
---|
723 | 880 | return ret; |
---|
724 | 881 | } |
---|
.. | .. |
---|
761 | 918 | } |
---|
762 | 919 | |
---|
763 | 920 | ret = UCODE_ERROR; |
---|
764 | | - if (*(u32 *)fw->data != UCODE_MAGIC) { |
---|
765 | | - pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data); |
---|
| 921 | + if (!verify_container(fw->data, fw->size, false)) |
---|
766 | 922 | goto fw_release; |
---|
767 | | - } |
---|
768 | 923 | |
---|
769 | 924 | ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size); |
---|
770 | 925 | |
---|