Skip to content

Commit d239198

Browse files
suryasaimadhugregkh
authored andcommitted
x86, microcode: Reload microcode on resume
commit fbae4ba upstream. Normally, we do reapply microcode on resume. However, in the cases where that microcode comes from the early loader and the late loader hasn't been utilized yet, there's no easy way for us to go and apply the patch applied during boot by the early loader. Thus, reuse the patch stashed by the early loader for the BSP. Signed-off-by: Borislav Petkov <bp@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 44c4db3 commit d239198

7 files changed

Lines changed: 84 additions & 18 deletions

File tree

‎arch/x86/include/asm/microcode.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ static inline void __exit exit_amd_microcode(void) {}
7878
extern void __init load_ucode_bsp(void);
7979
extern void load_ucode_ap(void);
8080
extern int __init save_microcode_in_initrd(void);
81+
void reload_early_microcode(void);
8182
#else
8283
static inline void __init load_ucode_bsp(void) {}
8384
static inline void load_ucode_ap(void) {}
8485
static inline int __init save_microcode_in_initrd(void)
8586
{
8687
return 0;
8788
}
89+
static inline void reload_early_microcode(void) {}
8890
#endif
8991

9092
#endif /* _ASM_X86_MICROCODE_H */

‎arch/x86/include/asm/microcode_amd.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ extern u8 amd_ucode_patch[PATCH_MAX_SIZE];
6868
extern void __init load_ucode_amd_bsp(void);
6969
extern void load_ucode_amd_ap(void);
7070
extern int __init save_microcode_in_initrd_amd(void);
71+
void reload_ucode_amd(void);
7172
#else
7273
static inline void __init load_ucode_amd_bsp(void) {}
7374
static inline void load_ucode_amd_ap(void) {}
7475
static inline int __init save_microcode_in_initrd_amd(void) { return -EINVAL; }
76+
void reload_ucode_amd(void) {}
7577
#endif
7678

7779
#endif /* _ASM_X86_MICROCODE_AMD_H */

‎arch/x86/include/asm/microcode_intel.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ extern void __init load_ucode_intel_bsp(void);
6868
extern void load_ucode_intel_ap(void);
6969
extern void show_ucode_info_early(void);
7070
extern int __init save_microcode_in_initrd_intel(void);
71+
void reload_ucode_intel(void);
7172
#else
7273
static inline __init void load_ucode_intel_bsp(void) {}
7374
static inline void load_ucode_intel_ap(void) {}
7475
static inline void show_ucode_info_early(void) {}
7576
static inline int __init save_microcode_in_initrd_intel(void) { return -EINVAL; }
77+
static inline void reload_ucode_intel(void) {}
7678
#endif
7779

7880
#if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)

‎arch/x86/kernel/cpu/microcode/amd_early.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,21 @@ int __init save_microcode_in_initrd_amd(void)
402402

403403
return retval;
404404
}
405+
406+
void reload_ucode_amd(void)
407+
{
408+
struct microcode_amd *mc;
409+
u32 rev, eax;
410+
411+
rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
412+
413+
mc = (struct microcode_amd *)amd_ucode_patch;
414+
415+
if (mc && rev < mc->hdr.patch_id) {
416+
if (!__apply_microcode_amd(mc)) {
417+
ucode_new_rev = mc->hdr.patch_id;
418+
pr_info("microcode: reload patch_level=0x%08x\n",
419+
ucode_new_rev);
420+
}
421+
}
422+
}

‎arch/x86/kernel/cpu/microcode/core.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,8 @@ static void mc_bp_resume(void)
465465

466466
if (uci->valid && uci->mc)
467467
microcode_ops->apply_microcode(cpu);
468-
#ifdef CONFIG_X86_64
469468
else if (!uci->mc)
470-
/*
471-
* We might resume and not have applied late microcode but still
472-
* have a newer patch stashed from the early loader. We don't
473-
* have it in uci->mc so we have to load it the same way we're
474-
* applying patches early on the APs.
475-
*/
476-
load_ucode_ap();
477-
#endif
469+
reload_early_microcode();
478470
}
479471

480472
static struct syscore_ops mc_syscore_ops = {

‎arch/x86/kernel/cpu/microcode/core_early.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,24 @@ int __init save_microcode_in_initrd(void)
176176

177177
return 0;
178178
}
179+
180+
void reload_early_microcode(void)
181+
{
182+
int vendor, x86;
183+
184+
vendor = x86_vendor();
185+
x86 = x86_family();
186+
187+
switch (vendor) {
188+
case X86_VENDOR_INTEL:
189+
if (x86 >= 6)
190+
reload_ucode_intel();
191+
break;
192+
case X86_VENDOR_AMD:
193+
if (x86 >= 0x10)
194+
reload_ucode_amd();
195+
break;
196+
default:
197+
break;
198+
}
199+
}

‎arch/x86/kernel/cpu/microcode/intel_early.c‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static struct mc_saved_data {
3434
struct microcode_intel **mc_saved;
3535
} mc_saved_data;
3636

37+
static struct microcode_intel bsp_patch;
38+
3739
static enum ucode_state
3840
generic_load_microcode_early(struct microcode_intel **mc_saved_p,
3941
unsigned int mc_saved_count,
@@ -650,7 +652,7 @@ static inline void print_ucode(struct ucode_cpu_info *uci)
650652
}
651653
#endif
652654

653-
static int apply_microcode_early(struct ucode_cpu_info *uci)
655+
static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
654656
{
655657
struct microcode_intel *mc_intel;
656658
unsigned int val[2];
@@ -679,7 +681,10 @@ static int apply_microcode_early(struct ucode_cpu_info *uci)
679681
#endif
680682
uci->cpu_sig.rev = val[1];
681683

682-
print_ucode(uci);
684+
if (early)
685+
print_ucode(uci);
686+
else
687+
print_ucode_info(uci, mc_intel->hdr.date);
683688

684689
return 0;
685690
}
@@ -712,14 +717,22 @@ _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
712717
unsigned long *mc_saved_in_initrd,
713718
unsigned long initrd_start_early,
714719
unsigned long initrd_end_early,
715-
struct ucode_cpu_info *uci)
720+
struct ucode_cpu_info *uci,
721+
struct microcode_intel *bsp)
716722
{
723+
enum ucode_state ret;
724+
717725
collect_cpu_info_early(uci);
718726
scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data,
719727
mc_saved_in_initrd, uci);
720-
load_microcode(mc_saved_data, mc_saved_in_initrd,
721-
initrd_start_early, uci);
722-
apply_microcode_early(uci);
728+
729+
ret = load_microcode(mc_saved_data, mc_saved_in_initrd,
730+
initrd_start_early, uci);
731+
732+
if (ret == UCODE_OK) {
733+
apply_microcode_early(uci, true);
734+
memcpy(bsp, uci->mc, sizeof(*bsp));
735+
}
723736
}
724737

725738
void __init
@@ -728,10 +741,12 @@ load_ucode_intel_bsp(void)
728741
u64 ramdisk_image, ramdisk_size;
729742
unsigned long initrd_start_early, initrd_end_early;
730743
struct ucode_cpu_info uci;
744+
struct microcode_intel *bsp_p;
731745
#ifdef CONFIG_X86_32
732746
struct boot_params *boot_params_p;
733747

734748
boot_params_p = (struct boot_params *)__pa_nodebug(&boot_params);
749+
bsp_p = (struct microcode_intel *)__pa_nodebug(&bsp_patch);
735750
ramdisk_image = boot_params_p->hdr.ramdisk_image;
736751
ramdisk_size = boot_params_p->hdr.ramdisk_size;
737752
initrd_start_early = ramdisk_image;
@@ -740,15 +755,17 @@ load_ucode_intel_bsp(void)
740755
_load_ucode_intel_bsp(
741756
(struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
742757
(unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
743-
initrd_start_early, initrd_end_early, &uci);
758+
initrd_start_early, initrd_end_early, &uci, bsp_p);
744759
#else
760+
bsp_p = &bsp_patch;
745761
ramdisk_image = boot_params.hdr.ramdisk_image;
746762
ramdisk_size = boot_params.hdr.ramdisk_size;
747763
initrd_start_early = ramdisk_image + PAGE_OFFSET;
748764
initrd_end_early = initrd_start_early + ramdisk_size;
749765

750766
_load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
751-
initrd_start_early, initrd_end_early, &uci);
767+
initrd_start_early, initrd_end_early,
768+
&uci, bsp_p);
752769
#endif
753770
}
754771

@@ -782,5 +799,17 @@ void load_ucode_intel_ap(void)
782799
collect_cpu_info_early(&uci);
783800
load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
784801
initrd_start_addr, &uci);
785-
apply_microcode_early(&uci);
802+
apply_microcode_early(&uci, true);
803+
}
804+
805+
void reload_ucode_intel(void)
806+
{
807+
struct ucode_cpu_info uci;
808+
809+
if (!bsp_patch.hdr.rev)
810+
return;
811+
812+
uci.mc = &bsp_patch;
813+
814+
apply_microcode_early(&uci, false);
786815
}

0 commit comments

Comments
 (0)