x86/microcode: Rework microcode loading

Yeah, I know, I know, this is a huuge patch and reviewing it is hard.

Sorry but this is the only way I could think of in which I can rewrite
the microcode patches loading procedure without breaking (knowingly) the
driver.

So maybe this patch is easier to review if one looks at the files after
the patch has been applied instead at the diff. Because then it becomes
pretty obvious:

* The BSP-loading path - load_ucode_bsp() is working independently from
  the AP path now and it doesn't save any pointers or patches anymore -
  it solely parses the builtin or initrd microcode and applies the patch.
  That's it.

This fixes the CONFIG_RANDOMIZE_MEMORY offset fun more solidly.

* The AP-loading path - load_ucode_ap() then goes and scans
  builtin/initrd *again* for the microcode patches but it caches them this
  time so that we don't have to do that scan on each AP but only once.

This simplifies the code considerably.

Then, when we save the microcode from the initrd/builtin, we go and
add the relevant patches to our own cache. The AMD side did do that
and now the Intel side does it too. So no more pointer copying and
blabla, we save the microcode patches ourselves and are independent from
initrd/builtin.

This whole conversion gives us other benefits like unifying the
initrd parsing into a single function: find_microcode_in_initrd() is
used by both.

The diffstat speaks for itself: 456 insertions(+), 695 deletions(-)

Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/20161025095522.11964-12-bp@alien8.de
Signed-off-by: Ingo Molnar <mingo@kernel.org>
This commit is contained in:
Borislav Petkov 2016-10-25 11:55:21 +02:00 committed by Ingo Molnar
parent 8027923ab4
commit 06b8534cb7
4 changed files with 491 additions and 742 deletions

View File

@ -64,6 +64,7 @@ struct ucode_cpu_info {
void *mc; void *mc;
}; };
extern struct ucode_cpu_info ucode_cpu_info[]; extern struct ucode_cpu_info ucode_cpu_info[];
struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa);
#ifdef CONFIG_MICROCODE_INTEL #ifdef CONFIG_MICROCODE_INTEL
extern struct microcode_ops * __init init_intel_microcode(void); extern struct microcode_ops * __init init_intel_microcode(void);

View File

@ -41,53 +41,23 @@ static struct equiv_cpu_entry *equiv_cpu_table;
/* /*
* This points to the current valid container of microcode patches which we will * This points to the current valid container of microcode patches which we will
* save from the initrd before jettisoning its contents. * save from the initrd/builtin before jettisoning its contents.
*/ */
static u8 *container; struct container {
static size_t container_size; u8 *data;
static bool ucode_builtin; size_t size;
} cont;
static u32 ucode_new_rev; static u32 ucode_new_rev;
static u8 amd_ucode_patch[PATCH_MAX_SIZE]; static u8 amd_ucode_patch[PATCH_MAX_SIZE];
static u16 this_equiv_id; static u16 this_equiv_id;
static struct cpio_data ucode_cpio; /*
* Microcode patch container file is prepended to the initrd in cpio
static struct cpio_data __init find_ucode_in_initrd(void) * format. See Documentation/x86/early-microcode.txt
{ */
#ifdef CONFIG_BLK_DEV_INITRD static const char
char *path; ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
void *start;
size_t size;
/*
* Microcode patch container file is prepended to the initrd in cpio
* format. See Documentation/x86/early-microcode.txt
*/
static __initdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
#ifdef CONFIG_X86_32
struct boot_params *p;
/*
* On 32-bit, early load occurs before paging is turned on so we need
* to use physical addresses.
*/
p = (struct boot_params *)__pa_nodebug(&boot_params);
path = (char *)__pa_nodebug(ucode_path);
start = (void *)p->hdr.ramdisk_image;
size = p->hdr.ramdisk_size;
#else
path = ucode_path;
start = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
size = boot_params.hdr.ramdisk_size;
#endif /* !CONFIG_X86_32 */
return find_cpio_data(path, start, size, NULL);
#else
return (struct cpio_data){ NULL, 0, "" };
#endif
}
static size_t compute_container_size(u8 *data, u32 total_size) static size_t compute_container_size(u8 *data, u32 total_size)
{ {
@ -143,6 +113,84 @@ static inline u16 find_equiv_id(struct equiv_cpu_entry *equiv_cpu_table,
return 0; return 0;
} }
/*
* This scans the ucode blob for the proper container as we can have multiple
* containers glued together.
*/
static struct container
find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
{
struct container ret = { NULL, 0 };
u32 eax, ebx, ecx, edx;
struct equiv_cpu_entry *eq;
int offset, left;
u16 eq_id = 0;
u32 *header;
u8 *data;
data = ucode;
left = size;
header = (u32 *)data;
/* find equiv cpu table */
if (header[0] != UCODE_MAGIC ||
header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
header[2] == 0) /* size */
return ret;
eax = 0x00000001;
ecx = 0;
native_cpuid(&eax, &ebx, &ecx, &edx);
while (left > 0) {
eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
ret.data = data;
/* Advance past the container header */
offset = header[2] + CONTAINER_HDR_SZ;
data += offset;
left -= offset;
eq_id = find_equiv_id(eq, eax);
if (eq_id) {
ret.size = compute_container_size(ret.data, left + offset);
/*
* truncate how much we need to iterate over in the
* ucode update loop below
*/
left = ret.size - offset;
*ret_id = eq_id;
return ret;
}
/*
* support multiple container files appended together. if this
* one does not have a matching equivalent cpu entry, we fast
* forward to the next container file.
*/
while (left > 0) {
header = (u32 *)data;
if (header[0] == UCODE_MAGIC &&
header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
break;
offset = header[1] + SECTION_HDR_SIZE;
data += offset;
left -= offset;
}
/* mark where the next microcode container file starts */
offset = data - (u8 *)ucode;
ucode = data;
}
return ret;
}
static int __apply_microcode_amd(struct microcode_amd *mc_amd) static int __apply_microcode_amd(struct microcode_amd *mc_amd)
{ {
u32 rev, dummy; u32 rev, dummy;
@ -166,96 +214,38 @@ static int __apply_microcode_amd(struct microcode_amd *mc_amd)
* load_microcode_amd() to save equivalent cpu table and microcode patches in * load_microcode_amd() to save equivalent cpu table and microcode patches in
* kernel heap memory. * kernel heap memory.
*/ */
static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch) static struct container
apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
{ {
struct equiv_cpu_entry *eq; struct container ret = { NULL, 0 };
size_t *cont_sz;
u32 *header;
u8 *data, **cont;
u8 (*patch)[PATCH_MAX_SIZE]; u8 (*patch)[PATCH_MAX_SIZE];
u16 eq_id = 0;
int offset, left; int offset, left;
u32 rev, eax, ebx, ecx, edx; u32 rev, *header;
u8 *data;
u16 eq_id = 0;
u32 *new_rev; u32 *new_rev;
#ifdef CONFIG_X86_32 #ifdef CONFIG_X86_32
new_rev = (u32 *)__pa_nodebug(&ucode_new_rev); new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
cont_sz = (size_t *)__pa_nodebug(&container_size);
cont = (u8 **)__pa_nodebug(&container);
patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch); patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
#else #else
new_rev = &ucode_new_rev; new_rev = &ucode_new_rev;
cont_sz = &container_size;
cont = &container;
patch = &amd_ucode_patch; patch = &amd_ucode_patch;
#endif #endif
data = ucode;
left = size;
header = (u32 *)data;
/* find equiv cpu table */
if (header[0] != UCODE_MAGIC ||
header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
header[2] == 0) /* size */
return;
eax = 0x00000001;
ecx = 0;
native_cpuid(&eax, &ebx, &ecx, &edx);
while (left > 0) {
eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
*cont = data;
/* Advance past the container header */
offset = header[2] + CONTAINER_HDR_SZ;
data += offset;
left -= offset;
eq_id = find_equiv_id(eq, eax);
if (eq_id) {
this_equiv_id = eq_id;
*cont_sz = compute_container_size(*cont, left + offset);
/*
* truncate how much we need to iterate over in the
* ucode update loop below
*/
left = *cont_sz - offset;
break;
}
/*
* support multiple container files appended together. if this
* one does not have a matching equivalent cpu entry, we fast
* forward to the next container file.
*/
while (left > 0) {
header = (u32 *)data;
if (header[0] == UCODE_MAGIC &&
header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
break;
offset = header[1] + SECTION_HDR_SIZE;
data += offset;
left -= offset;
}
/* mark where the next microcode container file starts */
offset = data - (u8 *)ucode;
ucode = data;
}
if (!eq_id) {
*cont = NULL;
*cont_sz = 0;
return;
}
if (check_current_patch_level(&rev, true)) if (check_current_patch_level(&rev, true))
return; return (struct container){ NULL, 0 };
ret = find_proper_container(ucode, size, &eq_id);
if (!eq_id)
return (struct container){ NULL, 0 };
this_equiv_id = eq_id;
header = (u32 *)ret.data;
/* We're pointing to an equiv table, skip over it. */
data = ret.data + header[2] + CONTAINER_HDR_SZ;
left = ret.size - (header[2] + CONTAINER_HDR_SZ);
while (left > 0) { while (left > 0) {
struct microcode_amd *mc; struct microcode_amd *mc;
@ -274,8 +264,7 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
*new_rev = rev; *new_rev = rev;
if (save_patch) if (save_patch)
memcpy(patch, mc, memcpy(patch, mc, min_t(u32, header[1], PATCH_MAX_SIZE));
min_t(u32, header[1], PATCH_MAX_SIZE));
} }
} }
@ -283,10 +272,10 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
data += offset; data += offset;
left -= offset; left -= offset;
} }
return ret;
} }
static bool __init load_builtin_amd_microcode(struct cpio_data *cp, static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
unsigned int family)
{ {
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
char fw_name[36] = "amd-ucode/microcode_amd.bin"; char fw_name[36] = "amd-ucode/microcode_amd.bin";
@ -303,32 +292,31 @@ static bool __init load_builtin_amd_microcode(struct cpio_data *cp,
void __init load_ucode_amd_bsp(unsigned int family) void __init load_ucode_amd_bsp(unsigned int family)
{ {
struct ucode_cpu_info *uci;
struct cpio_data cp; struct cpio_data cp;
bool *builtin; const char *path;
void **data; bool use_pa;
size_t *size;
#ifdef CONFIG_X86_32 if (IS_ENABLED(CONFIG_X86_32)) {
data = (void **)__pa_nodebug(&ucode_cpio.data); uci = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
size = (size_t *)__pa_nodebug(&ucode_cpio.size); path = (const char *)__pa_nodebug(ucode_path);
builtin = (bool *)__pa_nodebug(&ucode_builtin); use_pa = true;
#else } else {
data = &ucode_cpio.data; uci = ucode_cpu_info;
size = &ucode_cpio.size; path = ucode_path;
builtin = &ucode_builtin; use_pa = false;
#endif }
*builtin = load_builtin_amd_microcode(&cp, family); if (!get_builtin_microcode(&cp, family))
if (!*builtin) cp = find_microcode_in_initrd(path, use_pa);
cp = find_ucode_in_initrd();
if (!(cp.data && cp.size)) if (!(cp.data && cp.size))
return; return;
*data = cp.data; /* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
*size = cp.size; uci->cpu_sig.sig = cpuid_eax(1);
apply_ucode_in_initrd(cp.data, cp.size, true); apply_microcode_early_amd(cp.data, cp.size, true);
} }
#ifdef CONFIG_X86_32 #ifdef CONFIG_X86_32
@ -342,8 +330,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
void load_ucode_amd_ap(unsigned int family) void load_ucode_amd_ap(unsigned int family)
{ {
struct microcode_amd *mc; struct microcode_amd *mc;
size_t *usize; struct cpio_data cp;
void **ucode;
mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch); mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
if (mc->hdr.patch_id && mc->hdr.processor_rev_id) { if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
@ -351,55 +338,63 @@ void load_ucode_amd_ap(unsigned int family)
return; return;
} }
ucode = (void *)__pa_nodebug(&container); if (!get_builtin_microcode(&cp, family))
usize = (size_t *)__pa_nodebug(&container_size); cp = find_microcode_in_initrd((const char *)__pa_nodebug(ucode_path), true);
if (!*ucode || !*usize) if (!(cp.data && cp.size))
return; return;
apply_ucode_in_initrd(*ucode, *usize, false); /*
} * This would set amd_ucode_patch above so that the following APs can
* use it directly instead of going down this path again.
static void __init collect_cpu_sig_on_bsp(void *arg) */
{ apply_microcode_early_amd(cp.data, cp.size, true);
unsigned int cpu = smp_processor_id();
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
uci->cpu_sig.sig = cpuid_eax(0x00000001);
}
static void __init get_bsp_sig(void)
{
unsigned int bsp = boot_cpu_data.cpu_index;
struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
if (!uci->cpu_sig.sig)
smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
} }
#else #else
void load_ucode_amd_ap(unsigned int family) void load_ucode_amd_ap(unsigned int family)
{ {
struct equiv_cpu_entry *eq; struct equiv_cpu_entry *eq;
struct microcode_amd *mc; struct microcode_amd *mc;
u8 *cont = container;
u32 rev, eax; u32 rev, eax;
u16 eq_id; u16 eq_id;
if (!container) /* 64-bit runs with paging enabled, thus early==false. */
return;
/*
* 64-bit runs with paging enabled, thus early==false.
*/
if (check_current_patch_level(&rev, false)) if (check_current_patch_level(&rev, false))
return; return;
/* Add CONFIG_RANDOMIZE_MEMORY offset. */ /* First AP hasn't cached it yet, go through the blob. */
if (!ucode_builtin) if (!cont.data) {
cont += PAGE_OFFSET - __PAGE_OFFSET_BASE; struct cpio_data cp = { NULL, 0, "" };
if (cont.size == -1)
return;
reget:
if (!get_builtin_microcode(&cp, family)) {
#ifdef CONFIG_BLK_DEV_INITRD
cp = find_cpio_data(ucode_path, (void *)initrd_start,
initrd_end - initrd_start, NULL);
#endif
if (!(cp.data && cp.size)) {
/*
* Mark it so that other APs do not scan again
* for no real reason and slow down boot
* needlessly.
*/
cont.size = -1;
return;
}
}
cont = apply_microcode_early_amd(cp.data, cp.size, false);
if (!(cont.data && cont.size)) {
cont.size = -1;
return;
}
}
eax = cpuid_eax(0x00000001); eax = cpuid_eax(0x00000001);
eq = (struct equiv_cpu_entry *)(cont + CONTAINER_HDR_SZ); eq = (struct equiv_cpu_entry *)(cont.data + CONTAINER_HDR_SZ);
eq_id = find_equiv_id(eq, eax); eq_id = find_equiv_id(eq, eax);
if (!eq_id) if (!eq_id)
@ -414,64 +409,50 @@ void load_ucode_amd_ap(unsigned int family)
} }
} else { } else {
if (!ucode_cpio.data)
return;
/* /*
* AP has a different equivalence ID than BSP, looks like * AP has a different equivalence ID than BSP, looks like
* mixed-steppings silicon so go through the ucode blob anew. * mixed-steppings silicon so go through the ucode blob anew.
*/ */
apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false); goto reget;
} }
} }
#endif #endif /* CONFIG_X86_32 */
static enum ucode_state static enum ucode_state
load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size); load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size);
int __init save_microcode_in_initrd_amd(unsigned int family) int __init save_microcode_in_initrd_amd(unsigned int fam)
{ {
unsigned long cont;
int retval = 0;
enum ucode_state ret; enum ucode_state ret;
u8 *cont_va; int retval = 0;
u32 eax; u16 eq_id;
if (!container) if (!cont.data) {
return -EINVAL; if (IS_ENABLED(CONFIG_X86_32) && (cont.size != -1)) {
struct cpio_data cp = { NULL, 0, "" };
#ifdef CONFIG_X86_32 #ifdef CONFIG_BLK_DEV_INITRD
get_bsp_sig(); cp = find_cpio_data(ucode_path, (void *)initrd_start,
cont = (unsigned long)container; initrd_end - initrd_start, NULL);
cont_va = __va(container);
#else
/*
* We need the physical address of the container for both bitness since
* boot_params.hdr.ramdisk_image is a physical address.
*/
cont = __pa(container);
cont_va = container;
#endif #endif
/* if (!(cp.data && cp.size)) {
* Take into account the fact that the ramdisk might get relocated and cont.size = -1;
* therefore we need to recompute the container's position in virtual return -EINVAL;
* memory space. }
*/
if (relocated_ramdisk)
container = (u8 *)(__va(relocated_ramdisk) +
(cont - boot_params.hdr.ramdisk_image));
else
container = cont_va;
/* Add CONFIG_RANDOMIZE_MEMORY offset. */ cont = find_proper_container(cp.data, cp.size, &eq_id);
if (!ucode_builtin) if (!eq_id) {
container += PAGE_OFFSET - __PAGE_OFFSET_BASE; cont.size = -1;
return -EINVAL;
}
eax = cpuid_eax(0x00000001); } else
eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff); return -EINVAL;
}
ret = load_microcode_amd(smp_processor_id(), eax, container, container_size); ret = load_microcode_amd(smp_processor_id(), fam, cont.data, cont.size);
if (ret != UCODE_OK) if (ret != UCODE_OK)
retval = -EINVAL; retval = -EINVAL;
@ -479,8 +460,8 @@ int __init save_microcode_in_initrd_amd(unsigned int family)
* This will be freed any msec now, stash patches for the current * This will be freed any msec now, stash patches for the current
* family and switch to patch cache for cpu hotplug, etc later. * family and switch to patch cache for cpu hotplug, etc later.
*/ */
container = NULL; cont.data = NULL;
container_size = 0; cont.size = 0;
return retval; return retval;
} }
@ -498,8 +479,10 @@ void reload_ucode_amd(void)
return; return;
mc = (struct microcode_amd *)amd_ucode_patch; mc = (struct microcode_amd *)amd_ucode_patch;
if (!mc)
return;
if (mc && rev < mc->hdr.patch_id) { if (rev < mc->hdr.patch_id) {
if (!__apply_microcode_amd(mc)) { if (!__apply_microcode_amd(mc)) {
ucode_new_rev = mc->hdr.patch_id; ucode_new_rev = mc->hdr.patch_id;
pr_info("reload patch_level=0x%08x\n", ucode_new_rev); pr_info("reload patch_level=0x%08x\n", ucode_new_rev);

View File

@ -39,6 +39,7 @@
#include <asm/microcode.h> #include <asm/microcode.h>
#include <asm/processor.h> #include <asm/processor.h>
#include <asm/cmdline.h> #include <asm/cmdline.h>
#include <asm/setup.h>
#define MICROCODE_VERSION "2.01" #define MICROCODE_VERSION "2.01"
@ -196,6 +197,58 @@ static int __init save_microcode_in_initrd(void)
return -EINVAL; return -EINVAL;
} }
struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
{
#ifdef CONFIG_BLK_DEV_INITRD
unsigned long start = 0;
size_t size;
#ifdef CONFIG_X86_32
struct boot_params *params;
if (use_pa)
params = (struct boot_params *)__pa_nodebug(&boot_params);
else
params = &boot_params;
size = params->hdr.ramdisk_size;
/*
* Set start only if we have an initrd image. We cannot use initrd_start
* because it is not set that early yet.
*/
if (size)
start = params->hdr.ramdisk_image;
# else /* CONFIG_X86_64 */
size = (unsigned long)boot_params.ext_ramdisk_size << 32;
size |= boot_params.hdr.ramdisk_size;
if (size) {
start = (unsigned long)boot_params.ext_ramdisk_image << 32;
start |= boot_params.hdr.ramdisk_image;
start += PAGE_OFFSET;
}
# endif
/*
* Did we relocate the ramdisk?
*
* So we possibly relocate the ramdisk *after* applying microcode on the
* BSP so we rely on use_pa (use physical addresses) - even if it is not
* absolutely correct - to determine whether we've done the ramdisk
* relocation already.
*/
if (!use_pa && relocated_ramdisk)
start = initrd_start;
return find_cpio_data(path, (void *)start, size, NULL);
#else /* !CONFIG_BLK_DEV_INITRD */
return (struct cpio_data){ NULL, 0, "" };
#endif
}
void reload_early_microcode(void) void reload_early_microcode(void)
{ {
int vendor, family; int vendor, family;
@ -455,7 +508,8 @@ static struct attribute_group mc_attr_group = {
static void microcode_fini_cpu(int cpu) static void microcode_fini_cpu(int cpu)
{ {
microcode_ops->microcode_fini_cpu(cpu); if (microcode_ops->microcode_fini_cpu)
microcode_ops->microcode_fini_cpu(cpu);
} }
static enum ucode_state microcode_resume_cpu(int cpu) static enum ucode_state microcode_resume_cpu(int cpu)
@ -584,12 +638,7 @@ static int mc_cpu_down_prep(unsigned int cpu)
/* Suspend is in progress, only remove the interface */ /* Suspend is in progress, only remove the interface */
sysfs_remove_group(&dev->kobj, &mc_attr_group); sysfs_remove_group(&dev->kobj, &mc_attr_group);
pr_debug("CPU%d removed\n", cpu); pr_debug("CPU%d removed\n", cpu);
/*
* When a CPU goes offline, don't free up or invalidate the copy of
* the microcode in kernel memory, so that we can reuse it when the
* CPU comes back online without unnecessarily requesting the userspace
* for it again.
*/
return 0; return 0;
} }

View File

@ -39,27 +39,10 @@
#include <asm/setup.h> #include <asm/setup.h>
#include <asm/msr.h> #include <asm/msr.h>
/* static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
* Temporary microcode blobs pointers storage. We note here during early load
* the pointers to microcode blobs we've got from whatever storage (detached
* initrd, builtin). Later on, we put those into final storage
* mc_saved_data.mc_saved.
*
* Important: those are offsets from the beginning of initrd or absolute
* addresses within the kernel image when built-in.
*/
static unsigned long mc_tmp_ptrs[MAX_UCODE_COUNT];
static struct mc_saved_data { /* Current microcode patch used in early patching */
unsigned int num_saved; struct microcode_intel *intel_ucode_patch;
struct microcode_intel **mc_saved;
} mc_saved_data;
/* Microcode blobs within the initrd. 0 if builtin. */
static struct ucode_blobs {
unsigned long start;
bool valid;
} blobs;
static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1, static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
unsigned int s2, unsigned int p2) unsigned int s2, unsigned int p2)
@ -116,103 +99,23 @@ static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev
return find_matching_signature(mc, csig, cpf); return find_matching_signature(mc, csig, cpf);
} }
/* Go through saved patches and find the one suitable for the current CPU. */
static enum ucode_state
find_microcode_patch(struct microcode_intel **saved,
unsigned int num_saved, struct ucode_cpu_info *uci)
{
struct microcode_intel *ucode_ptr, *new_mc = NULL;
struct microcode_header_intel *mc_hdr;
int new_rev, ret, i;
new_rev = uci->cpu_sig.rev;
for (i = 0; i < num_saved; i++) {
ucode_ptr = saved[i];
mc_hdr = (struct microcode_header_intel *)ucode_ptr;
ret = has_newer_microcode(ucode_ptr,
uci->cpu_sig.sig,
uci->cpu_sig.pf,
new_rev);
if (!ret)
continue;
new_rev = mc_hdr->rev;
new_mc = ucode_ptr;
}
if (!new_mc)
return UCODE_NFOUND;
uci->mc = (struct microcode_intel *)new_mc;
return UCODE_OK;
}
static inline void
copy_ptrs(struct microcode_intel **mc_saved, unsigned long *mc_ptrs,
unsigned long off, int num_saved)
{
int i;
for (i = 0; i < num_saved; i++)
mc_saved[i] = (struct microcode_intel *)(mc_ptrs[i] + off);
}
#ifdef CONFIG_X86_32
static void
microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
{
int i;
struct microcode_intel ***mc_saved;
mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
for (i = 0; i < mcs->num_saved; i++) {
struct microcode_intel *p;
p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
}
}
#endif
static enum ucode_state
load_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
unsigned long offset, struct ucode_cpu_info *uci)
{
struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
unsigned int count = mcs->num_saved;
if (!mcs->mc_saved) {
copy_ptrs(mc_saved_tmp, mc_ptrs, offset, count);
return find_microcode_patch(mc_saved_tmp, count, uci);
} else {
#ifdef CONFIG_X86_32
microcode_phys(mc_saved_tmp, mcs);
return find_microcode_patch(mc_saved_tmp, count, uci);
#else
return find_microcode_patch(mcs->mc_saved, count, uci);
#endif
}
}
/* /*
* Given CPU signature and a microcode patch, this function finds if the * Given CPU signature and a microcode patch, this function finds if the
* microcode patch has matching family and model with the CPU. * microcode patch has matching family and model with the CPU.
*
* %true - if there's a match
* %false - otherwise
*/ */
static enum ucode_state static bool microcode_matches(struct microcode_header_intel *mc_header,
matching_model_microcode(struct microcode_header_intel *mc_header, unsigned long sig)
unsigned long sig)
{ {
unsigned int fam, model;
unsigned int fam_ucode, model_ucode;
struct extended_sigtable *ext_header;
unsigned long total_size = get_totalsize(mc_header); unsigned long total_size = get_totalsize(mc_header);
unsigned long data_size = get_datasize(mc_header); unsigned long data_size = get_datasize(mc_header);
int ext_sigcount, i; struct extended_sigtable *ext_header;
unsigned int fam_ucode, model_ucode;
struct extended_signature *ext_sig; struct extended_signature *ext_sig;
unsigned int fam, model;
int ext_sigcount, i;
fam = x86_family(sig); fam = x86_family(sig);
model = x86_model(sig); model = x86_model(sig);
@ -221,11 +124,11 @@ matching_model_microcode(struct microcode_header_intel *mc_header,
model_ucode = x86_model(mc_header->sig); model_ucode = x86_model(mc_header->sig);
if (fam == fam_ucode && model == model_ucode) if (fam == fam_ucode && model == model_ucode)
return UCODE_OK; return true;
/* Look for ext. headers: */ /* Look for ext. headers: */
if (total_size <= data_size + MC_HEADER_SIZE) if (total_size <= data_size + MC_HEADER_SIZE)
return UCODE_NFOUND; return false;
ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE; ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
ext_sig = (void *)ext_header + EXT_HEADER_SIZE; ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
@ -236,114 +139,69 @@ matching_model_microcode(struct microcode_header_intel *mc_header,
model_ucode = x86_model(ext_sig->sig); model_ucode = x86_model(ext_sig->sig);
if (fam == fam_ucode && model == model_ucode) if (fam == fam_ucode && model == model_ucode)
return UCODE_OK; return true;
ext_sig++; ext_sig++;
} }
return UCODE_NFOUND; return false;
} }
static int static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
save_microcode(struct mc_saved_data *mcs,
struct microcode_intel **mc_saved_src,
unsigned int num_saved)
{ {
int i, j; struct ucode_patch *p;
struct microcode_intel **saved_ptr;
int ret;
if (!num_saved) p = kzalloc(size, GFP_KERNEL);
return -EINVAL; if (!p)
return ERR_PTR(-ENOMEM);
/* p->data = kmemdup(data, size, GFP_KERNEL);
* Copy new microcode data. if (!p->data) {
*/ kfree(p);
saved_ptr = kcalloc(num_saved, sizeof(struct microcode_intel *), GFP_KERNEL); return ERR_PTR(-ENOMEM);
if (!saved_ptr)
return -ENOMEM;
for (i = 0; i < num_saved; i++) {
struct microcode_header_intel *mc_hdr;
struct microcode_intel *mc;
unsigned long size;
if (!mc_saved_src[i]) {
ret = -EINVAL;
goto err;
}
mc = mc_saved_src[i];
mc_hdr = &mc->hdr;
size = get_totalsize(mc_hdr);
saved_ptr[i] = kmemdup(mc, size, GFP_KERNEL);
if (!saved_ptr[i]) {
ret = -ENOMEM;
goto err;
}
} }
/* return p;
* Point to newly saved microcode.
*/
mcs->mc_saved = saved_ptr;
mcs->num_saved = num_saved;
return 0;
err:
for (j = 0; j <= i; j++)
kfree(saved_ptr[j]);
kfree(saved_ptr);
return ret;
} }
/* static void save_microcode_patch(void *data, unsigned int size)
* A microcode patch in ucode_ptr is saved into mc_saved
* - if it has matching signature and newer revision compared to an existing
* patch mc_saved.
* - or if it is a newly discovered microcode patch.
*
* The microcode patch should have matching model with CPU.
*
* Returns: The updated number @num_saved of saved microcode patches.
*/
static unsigned int _save_mc(struct microcode_intel **mc_saved,
u8 *ucode_ptr, unsigned int num_saved)
{ {
struct microcode_header_intel *mc_hdr, *mc_saved_hdr; struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
struct ucode_patch *iter, *tmp, *p;
bool prev_found = false;
unsigned int sig, pf; unsigned int sig, pf;
int found = 0, i;
mc_hdr = (struct microcode_header_intel *)ucode_ptr; mc_hdr = (struct microcode_header_intel *)data;
for (i = 0; i < num_saved; i++) { list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i]; mc_saved_hdr = (struct microcode_header_intel *)iter->data;
sig = mc_saved_hdr->sig; sig = mc_saved_hdr->sig;
pf = mc_saved_hdr->pf; pf = mc_saved_hdr->pf;
if (!find_matching_signature(ucode_ptr, sig, pf)) if (find_matching_signature(data, sig, pf)) {
continue; prev_found = true;
found = 1; if (mc_hdr->rev <= mc_saved_hdr->rev)
continue;
if (mc_hdr->rev <= mc_saved_hdr->rev) p = __alloc_microcode_buf(data, size);
continue; if (IS_ERR(p))
pr_err("Error allocating buffer %p\n", data);
/* else
* Found an older ucode saved earlier. Replace it with list_replace(&iter->plist, &p->plist);
* this newer one. }
*/
mc_saved[i] = (struct microcode_intel *)ucode_ptr;
break;
} }
/* Newly detected microcode, save it to memory. */ /*
if (i >= num_saved && !found) * There weren't any previous patches found in the list cache; save the
mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr; * newly found.
*/
return num_saved; if (!prev_found) {
p = __alloc_microcode_buf(data, size);
if (IS_ERR(p))
pr_err("Error allocating buffer for %p\n", data);
else
list_add_tail(&p->plist, &microcode_cache);
}
} }
static int microcode_sanity_check(void *mc, int print_err) static int microcode_sanity_check(void *mc, int print_err)
@ -448,78 +306,75 @@ static int microcode_sanity_check(void *mc, int print_err)
* Get microcode matching with BSP's model. Only CPUs with the same model as * Get microcode matching with BSP's model. Only CPUs with the same model as
* BSP can stay in the platform. * BSP can stay in the platform.
*/ */
static enum ucode_state __init static struct microcode_intel *
get_matching_model_microcode(unsigned long start, void *data, size_t size, scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
struct mc_saved_data *mcs, unsigned long *mc_ptrs,
struct ucode_cpu_info *uci)
{ {
struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
struct microcode_header_intel *mc_header; struct microcode_header_intel *mc_header;
unsigned int num_saved = mcs->num_saved; struct microcode_intel *patch = NULL;
enum ucode_state state = UCODE_OK;
unsigned int leftover = size;
u8 *ucode_ptr = data;
unsigned int mc_size; unsigned int mc_size;
int i;
while (leftover && num_saved < ARRAY_SIZE(mc_saved_tmp)) { while (size) {
if (size < sizeof(struct microcode_header_intel))
if (leftover < sizeof(mc_header))
break; break;
mc_header = (struct microcode_header_intel *)ucode_ptr; mc_header = (struct microcode_header_intel *)data;
mc_size = get_totalsize(mc_header); mc_size = get_totalsize(mc_header);
if (!mc_size || mc_size > leftover || if (!mc_size ||
microcode_sanity_check(ucode_ptr, 0) < 0) mc_size > size ||
microcode_sanity_check(data, 0) < 0)
break; break;
leftover -= mc_size; size -= mc_size;
/* if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
* Since APs with same family and model as the BSP may boot in data += mc_size;
* the platform, we need to find and save microcode patches
* with the same family and model as the BSP.
*/
if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != UCODE_OK) {
ucode_ptr += mc_size;
continue; continue;
} }
num_saved = _save_mc(mc_saved_tmp, ucode_ptr, num_saved); if (save) {
save_microcode_patch(data, mc_size);
goto next;
}
ucode_ptr += mc_size;
if (!patch) {
if (!has_newer_microcode(data,
uci->cpu_sig.sig,
uci->cpu_sig.pf,
uci->cpu_sig.rev))
goto next;
} else {
struct microcode_header_intel *phdr = &patch->hdr;
if (!has_newer_microcode(data,
phdr->sig,
phdr->pf,
phdr->rev))
goto next;
}
/* We have a newer patch, save it. */
patch = data;
next:
data += mc_size;
} }
if (leftover) { if (size)
state = UCODE_ERROR; return NULL;
return state;
}
if (!num_saved) { return patch;
state = UCODE_NFOUND;
return state;
}
for (i = 0; i < num_saved; i++)
mc_ptrs[i] = (unsigned long)mc_saved_tmp[i] - start;
mcs->num_saved = num_saved;
return state;
} }
static int collect_cpu_info_early(struct ucode_cpu_info *uci) static int collect_cpu_info_early(struct ucode_cpu_info *uci)
{ {
unsigned int val[2]; unsigned int val[2];
unsigned int family, model; unsigned int family, model;
struct cpu_signature csig; struct cpu_signature csig = { 0 };
unsigned int eax, ebx, ecx, edx; unsigned int eax, ebx, ecx, edx;
csig.sig = 0;
csig.pf = 0;
csig.rev = 0;
memset(uci, 0, sizeof(*uci)); memset(uci, 0, sizeof(*uci));
eax = 0x00000001; eax = 0x00000001;
@ -527,8 +382,8 @@ static int collect_cpu_info_early(struct ucode_cpu_info *uci)
native_cpuid(&eax, &ebx, &ecx, &edx); native_cpuid(&eax, &ebx, &ecx, &edx);
csig.sig = eax; csig.sig = eax;
family = x86_family(csig.sig); family = x86_family(eax);
model = x86_model(csig.sig); model = x86_model(eax);
if ((model >= 5) || (family > 6)) { if ((model >= 5) || (family > 6)) {
/* get processor flags from MSR 0x17 */ /* get processor flags from MSR 0x17 */
@ -554,40 +409,41 @@ static int collect_cpu_info_early(struct ucode_cpu_info *uci)
static void show_saved_mc(void) static void show_saved_mc(void)
{ {
#ifdef DEBUG #ifdef DEBUG
int i, j; int i = 0, j;
unsigned int sig, pf, rev, total_size, data_size, date; unsigned int sig, pf, rev, total_size, data_size, date;
struct ucode_cpu_info uci; struct ucode_cpu_info uci;
struct ucode_patch *p;
if (!mc_saved_data.num_saved) { if (list_empty(&microcode_cache)) {
pr_debug("no microcode data saved.\n"); pr_debug("no microcode data saved.\n");
return; return;
} }
pr_debug("Total microcode saved: %d\n", mc_saved_data.num_saved);
collect_cpu_info_early(&uci); collect_cpu_info_early(&uci);
sig = uci.cpu_sig.sig; sig = uci.cpu_sig.sig;
pf = uci.cpu_sig.pf; pf = uci.cpu_sig.pf;
rev = uci.cpu_sig.rev; rev = uci.cpu_sig.rev;
pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev); pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
for (i = 0; i < mc_saved_data.num_saved; i++) { list_for_each_entry(p, &microcode_cache, plist) {
struct microcode_header_intel *mc_saved_header; struct microcode_header_intel *mc_saved_header;
struct extended_sigtable *ext_header; struct extended_sigtable *ext_header;
int ext_sigcount;
struct extended_signature *ext_sig; struct extended_signature *ext_sig;
int ext_sigcount;
mc_saved_header = (struct microcode_header_intel *) mc_saved_header = (struct microcode_header_intel *)p->data;
mc_saved_data.mc_saved[i];
sig = mc_saved_header->sig; sig = mc_saved_header->sig;
pf = mc_saved_header->pf; pf = mc_saved_header->pf;
rev = mc_saved_header->rev; rev = mc_saved_header->rev;
total_size = get_totalsize(mc_saved_header); date = mc_saved_header->date;
data_size = get_datasize(mc_saved_header);
date = mc_saved_header->date; total_size = get_totalsize(mc_saved_header);
data_size = get_datasize(mc_saved_header);
pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n", pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
i, sig, pf, rev, total_size, i++, sig, pf, rev, total_size,
date & 0xffff, date & 0xffff,
date >> 24, date >> 24,
(date >> 16) & 0xff); (date >> 16) & 0xff);
@ -596,7 +452,7 @@ static void show_saved_mc(void)
if (total_size <= data_size + MC_HEADER_SIZE) if (total_size <= data_size + MC_HEADER_SIZE)
continue; continue;
ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE; ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
ext_sigcount = ext_header->count; ext_sigcount = ext_header->count;
ext_sig = (void *)ext_header + EXT_HEADER_SIZE; ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
@ -609,85 +465,43 @@ static void show_saved_mc(void)
ext_sig++; ext_sig++;
} }
} }
#endif #endif
} }
/* /*
* Save this mc into mc_saved_data. So it will be loaded early when a CPU is * Save this microcode patch. It will be loaded early when a CPU is
* hot added or resumes. * hot-added or resumes.
*
* Please make sure this mc should be a valid microcode patch before calling
* this function.
*/ */
static void save_mc_for_early(u8 *mc) static void save_mc_for_early(u8 *mc, unsigned int size)
{ {
#ifdef CONFIG_HOTPLUG_CPU #ifdef CONFIG_HOTPLUG_CPU
/* Synchronization during CPU hotplug. */ /* Synchronization during CPU hotplug. */
static DEFINE_MUTEX(x86_cpu_microcode_mutex); static DEFINE_MUTEX(x86_cpu_microcode_mutex);
struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
unsigned int mc_saved_count_init;
unsigned int num_saved;
struct microcode_intel **mc_saved;
int ret, i;
mutex_lock(&x86_cpu_microcode_mutex); mutex_lock(&x86_cpu_microcode_mutex);
mc_saved_count_init = mc_saved_data.num_saved; save_microcode_patch(mc, size);
num_saved = mc_saved_data.num_saved;
mc_saved = mc_saved_data.mc_saved;
if (mc_saved && num_saved)
memcpy(mc_saved_tmp, mc_saved,
num_saved * sizeof(struct microcode_intel *));
/*
* Save the microcode patch mc in mc_save_tmp structure if it's a newer
* version.
*/
num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
/*
* Save the mc_save_tmp in global mc_saved_data.
*/
ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
if (ret) {
pr_err("Cannot save microcode patch.\n");
goto out;
}
show_saved_mc(); show_saved_mc();
/*
* Free old saved microcode data.
*/
if (mc_saved) {
for (i = 0; i < mc_saved_count_init; i++)
kfree(mc_saved[i]);
kfree(mc_saved);
}
out:
mutex_unlock(&x86_cpu_microcode_mutex); mutex_unlock(&x86_cpu_microcode_mutex);
#endif #endif
} }
static bool __init load_builtin_intel_microcode(struct cpio_data *cp) static bool load_builtin_intel_microcode(struct cpio_data *cp)
{ {
#ifdef CONFIG_X86_64 unsigned int eax = 1, ebx, ecx = 0, edx;
unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
char name[30]; char name[30];
if (IS_ENABLED(CONFIG_X86_32))
return false;
native_cpuid(&eax, &ebx, &ecx, &edx); native_cpuid(&eax, &ebx, &ecx, &edx);
sprintf(name, "intel-ucode/%02x-%02x-%02x", sprintf(name, "intel-ucode/%02x-%02x-%02x",
x86_family(eax), x86_model(eax), x86_stepping(eax)); x86_family(eax), x86_model(eax), x86_stepping(eax));
return get_builtin_firmware(cp, name); return get_builtin_firmware(cp, name);
#else
return false;
#endif
} }
/* /*
@ -723,8 +537,7 @@ void show_ucode_info_early(void)
} }
/* /*
* At this point, we can not call printk() yet. Keep microcode patch number in * At this point, we can not call printk() yet. Delay printing microcode info in
* mc_saved_data.mc_saved and delay printing microcode info in
* show_ucode_info_early() until printk() works. * show_ucode_info_early() until printk() works.
*/ */
static void print_ucode(struct ucode_cpu_info *uci) static void print_ucode(struct ucode_cpu_info *uci)
@ -801,206 +614,140 @@ static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
return 0; return 0;
} }
/*
* This function converts microcode patch offsets previously stored in
* mc_tmp_ptrs to pointers and stores the pointers in mc_saved_data.
*/
int __init save_microcode_in_initrd_intel(void) int __init save_microcode_in_initrd_intel(void)
{ {
struct microcode_intel *mc_saved[MAX_UCODE_COUNT]; struct ucode_cpu_info uci;
unsigned int count = mc_saved_data.num_saved; struct cpio_data cp;
unsigned long offset = 0;
int ret;
if (!count) /*
* AP loading didn't find any microcode patch, no need to save anything.
*/
if (!intel_ucode_patch || IS_ERR(intel_ucode_patch))
return 0; return 0;
/* if (!load_builtin_intel_microcode(&cp))
* We have found a valid initrd but it might've been relocated in the cp = find_microcode_in_initrd(ucode_path, false);
* meantime so get its updated address.
*/
if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid)
offset = initrd_start;
copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count); if (!(cp.data && cp.size))
return 0;
ret = save_microcode(&mc_saved_data, mc_saved, count);
if (ret)
pr_err("Cannot save microcode patches from initrd.\n");
else
show_saved_mc();
return ret;
}
static __init enum ucode_state
__scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
{
#ifdef CONFIG_BLK_DEV_INITRD
static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
: ucode_name;
# ifdef CONFIG_X86_32
unsigned long start = 0, size;
struct boot_params *params;
params = (struct boot_params *)__pa_nodebug(&boot_params);
size = params->hdr.ramdisk_size;
/*
* Set start only if we have an initrd image. We cannot use initrd_start
* because it is not set that early yet.
*/
start = (size ? params->hdr.ramdisk_image : 0);
# else /* CONFIG_X86_64 */
unsigned long start = 0, size;
size = (u64)boot_params.ext_ramdisk_size << 32;
size |= boot_params.hdr.ramdisk_size;
if (size) {
start = (u64)boot_params.ext_ramdisk_image << 32;
start |= boot_params.hdr.ramdisk_image;
start += PAGE_OFFSET;
}
# endif
*cd = find_cpio_data(p, (void *)start, size, NULL);
if (cd->data) {
blbp->start = start;
blbp->valid = true;
return UCODE_OK;
} else
#endif /* CONFIG_BLK_DEV_INITRD */
return UCODE_ERROR;
}
static __init enum ucode_state
scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
{
struct cpio_data cd = { NULL, 0, "" };
enum ucode_state ret;
/* try built-in microcode first */
if (load_builtin_intel_microcode(&cd))
/*
* Invalidate blobs as we might've gotten an initrd too,
* supplied by the boot loader, by mistake or simply forgotten
* there. That's fine, we ignore it since we've found builtin
* microcode already.
*/
blbp->valid = false;
else {
ret = __scan_microcode_initrd(&cd, blbp);
if (ret != UCODE_OK)
return ret;
}
return get_matching_model_microcode(blbp->start, cd.data, cd.size,
mcs, mc_ptrs, uci);
}
static void __init
_load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
struct ucode_blobs *blbp)
{
struct ucode_cpu_info uci;
enum ucode_state ret;
collect_cpu_info_early(&uci); collect_cpu_info_early(&uci);
ret = scan_microcode(mcs, mc_ptrs, &uci, blbp); scan_microcode(cp.data, cp.size, &uci, true);
if (ret != UCODE_OK)
return;
ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci); show_saved_mc();
if (ret != UCODE_OK)
return;
apply_microcode_early(&uci, true); return 0;
}
/*
* @res_patch, output: a pointer to the patch we found.
*/
static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
{
static const char *path;
struct cpio_data cp;
bool use_pa;
if (IS_ENABLED(CONFIG_X86_32)) {
path = (const char *)__pa_nodebug(ucode_path);
use_pa = true;
} else {
path = ucode_path;
use_pa = false;
}
/* try built-in microcode first */
if (!load_builtin_intel_microcode(&cp))
cp = find_microcode_in_initrd(path, use_pa);
if (!(cp.data && cp.size))
return NULL;
collect_cpu_info_early(uci);
return scan_microcode(cp.data, cp.size, uci, false);
} }
void __init load_ucode_intel_bsp(void) void __init load_ucode_intel_bsp(void)
{ {
struct ucode_blobs *blobs_p; struct microcode_intel *patch;
struct mc_saved_data *mcs;
unsigned long *ptrs;
#ifdef CONFIG_X86_32
mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
ptrs = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs);
blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
#else
mcs = &mc_saved_data;
ptrs = mc_tmp_ptrs;
blobs_p = &blobs;
#endif
_load_ucode_intel_bsp(mcs, ptrs, blobs_p);
}
void load_ucode_intel_ap(void)
{
struct ucode_blobs *blobs_p;
unsigned long *ptrs, start = 0;
struct mc_saved_data *mcs;
struct ucode_cpu_info uci; struct ucode_cpu_info uci;
enum ucode_state ret;
#ifdef CONFIG_X86_32 patch = __load_ucode_intel(&uci);
mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data); if (!patch)
ptrs = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
#else
mcs = &mc_saved_data;
ptrs = mc_tmp_ptrs;
blobs_p = &blobs;
#endif
/*
* If there is no valid ucode previously saved in memory, no need to
* update ucode on this AP.
*/
if (!mcs->num_saved)
return; return;
if (blobs_p->valid) { uci.mc = patch;
start = blobs_p->start;
/*
* Pay attention to CONFIG_RANDOMIZE_MEMORY=y as it shuffles
* physmem mapping too and there we have the initrd.
*/
start += PAGE_OFFSET - __PAGE_OFFSET_BASE;
}
collect_cpu_info_early(&uci);
ret = load_microcode(mcs, ptrs, start, &uci);
if (ret != UCODE_OK)
return;
apply_microcode_early(&uci, true); apply_microcode_early(&uci, true);
} }
void load_ucode_intel_ap(void)
{
struct microcode_intel *patch, **iup;
struct ucode_cpu_info uci;
if (IS_ENABLED(CONFIG_X86_32))
iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
else
iup = &intel_ucode_patch;
reget:
if (!*iup) {
patch = __load_ucode_intel(&uci);
if (!patch)
return;
*iup = patch;
}
uci.mc = *iup;
if (apply_microcode_early(&uci, true)) {
/* Mixed-silicon system? Try to refetch the proper patch: */
*iup = NULL;
goto reget;
}
}
static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
{
struct microcode_header_intel *phdr;
struct ucode_patch *iter, *tmp;
list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
phdr = (struct microcode_header_intel *)iter->data;
if (phdr->rev <= uci->cpu_sig.rev)
continue;
if (!find_matching_signature(phdr,
uci->cpu_sig.sig,
uci->cpu_sig.pf))
continue;
return iter->data;
}
return NULL;
}
void reload_ucode_intel(void) void reload_ucode_intel(void)
{ {
struct microcode_intel *p;
struct ucode_cpu_info uci; struct ucode_cpu_info uci;
enum ucode_state ret;
if (!mc_saved_data.num_saved)
return;
collect_cpu_info_early(&uci); collect_cpu_info_early(&uci);
ret = find_microcode_patch(mc_saved_data.mc_saved, p = find_patch(&uci);
mc_saved_data.num_saved, &uci); if (!p)
if (ret != UCODE_OK)
return; return;
uci.mc = p;
apply_microcode_early(&uci, false); apply_microcode_early(&uci, false);
} }
@ -1032,24 +779,6 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
return 0; return 0;
} }
/*
* return 0 - no update found
* return 1 - found update
*/
static int get_matching_mc(struct microcode_intel *mc, int cpu)
{
struct cpu_signature cpu_sig;
unsigned int csig, cpf, crev;
collect_cpu_info(cpu, &cpu_sig);
csig = cpu_sig.sig;
cpf = cpu_sig.pf;
crev = cpu_sig.rev;
return has_newer_microcode(mc, csig, cpf, crev);
}
static int apply_microcode_intel(int cpu) static int apply_microcode_intel(int cpu)
{ {
struct microcode_intel *mc; struct microcode_intel *mc;
@ -1064,16 +793,12 @@ static int apply_microcode_intel(int cpu)
uci = ucode_cpu_info + cpu; uci = ucode_cpu_info + cpu;
mc = uci->mc; mc = uci->mc;
if (!mc) if (!mc) {
return 0; /* Look for a newer patch in our cache: */
mc = find_patch(uci);
/* if (!mc)
* Microcode on this CPU could be updated earlier. Only apply the return 0;
* microcode patch in mc when it is newer than the one on this }
* CPU.
*/
if (!get_matching_mc(mc, cpu))
return 0;
/* write microcode via MSR 0x79 */ /* write microcode via MSR 0x79 */
wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
@ -1181,7 +906,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
* permanent memory. So it will be loaded early when a CPU is hot added * permanent memory. So it will be loaded early when a CPU is hot added
* or resumes. * or resumes.
*/ */
save_mc_for_early(new_mc); save_mc_for_early(new_mc, curr_mc_size);
pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
cpu, new_rev, uci->cpu_sig.rev); cpu, new_rev, uci->cpu_sig.rev);
@ -1230,20 +955,11 @@ request_microcode_user(int cpu, const void __user *buf, size_t size)
return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user); return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
} }
static void microcode_fini_cpu(int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
vfree(uci->mc);
uci->mc = NULL;
}
static struct microcode_ops microcode_intel_ops = { static struct microcode_ops microcode_intel_ops = {
.request_microcode_user = request_microcode_user, .request_microcode_user = request_microcode_user,
.request_microcode_fw = request_microcode_fw, .request_microcode_fw = request_microcode_fw,
.collect_cpu_info = collect_cpu_info, .collect_cpu_info = collect_cpu_info,
.apply_microcode = apply_microcode_intel, .apply_microcode = apply_microcode_intel,
.microcode_fini_cpu = microcode_fini_cpu,
}; };
struct microcode_ops * __init init_intel_microcode(void) struct microcode_ops * __init init_intel_microcode(void)