1 .. SPDX-License-Identifier: GPL-2.0
3 ==========================
4 The Linux Microcode Loader
5 ==========================
7 :Authors: - Fenghua Yu <fenghua.yu@intel.com>
8 - Borislav Petkov <bp@suse.de>
9 - Ashok Raj <ashok.raj@intel.com>
11 The kernel has a x86 microcode loading facility which is supposed to
12 provide microcode loading methods in the OS. Potential use cases are
13 updating the microcode on platforms beyond the OEM End-Of-Life support,
14 and updating the microcode on long-running systems without rebooting.
16 The loader supports three loading methods:
21 The kernel can update microcode very early during boot. Loading
22 microcode early can fix CPU issues before they are observed during
25 The microcode is stored in an initrd file. During boot, it is read from
26 it and loaded into the CPU cores.
28 The format of the combined initrd image is microcode in (uncompressed)
29 cpio format followed by the (possibly compressed) initrd image. The
30 loader parses the combined initrd image during boot.
32 The microcode files in cpio name space are:
35 kernel/x86/microcode/GenuineIntel.bin
37 kernel/x86/microcode/AuthenticAMD.bin
39 During BSP (BootStrapping Processor) boot (pre-SMP), the kernel
40 scans the microcode file in the initrd. If microcode matching the
41 CPU is found, it will be applied in the BSP and later on in all APs
42 (Application Processors).
44 The loader also saves the matching microcode for the CPU in memory.
45 Thus, the cached microcode patch is applied when CPUs resume from a
48 Here's a crude example how to prepare an initrd with microcode (this is
49 normally done automatically by the distribution, when recreating the
50 initrd, so you don't really have to do it yourself. It is documented
51 here for future reference only).
57 echo "You need to supply an initrd file"
63 DSTDIR=kernel/x86/microcode
72 if [ -d /lib/firmware/amd-ucode ]; then
73 cat /lib/firmware/amd-ucode/microcode_amd*.bin > $DSTDIR/AuthenticAMD.bin
76 if [ -d /lib/firmware/intel-ucode ]; then
77 cat /lib/firmware/intel-ucode/* > $DSTDIR/GenuineIntel.bin
80 find . | cpio -o -H newc >../ucode.cpio
82 mv $INITRD $INITRD.orig
83 cat ucode.cpio $INITRD.orig > $INITRD
88 The system needs to have the microcode packages installed into
89 /lib/firmware or you need to fixup the paths above if yours are
90 somewhere else and/or you've downloaded them directly from the processor
96 You simply install the microcode packages your distro supplies and
99 # echo 1 > /sys/devices/system/cpu/microcode/reload
103 The loading mechanism looks for microcode blobs in
104 /lib/firmware/{intel-ucode,amd-ucode}. The default distro installation
105 packages already put them there.
107 Since kernel 5.19, late loading is not enabled by default.
109 The /dev/cpu/microcode method has been removed in 5.19.
111 Why is late loading dangerous?
112 ==============================
114 Synchronizing all CPUs
115 ----------------------
117 The microcode engine which receives the microcode update is shared
118 between the two logical threads in a SMT system. Therefore, when
119 the update is executed on one SMT thread of the core, the sibling
120 "automatically" gets the update.
122 Since the microcode can "simulate" MSRs too, while the microcode update
123 is in progress, those simulated MSRs transiently cease to exist. This
124 can result in unpredictable results if the SMT sibling thread happens to
125 be in the middle of an access to such an MSR. The usual observation is
126 that such MSR accesses cause #GPs to be raised to signal that former are
129 The disappearing MSRs are just one common issue which is being observed.
130 Any other instruction that's being patched and gets concurrently
131 executed by the other SMT sibling, can also result in similar,
132 unpredictable behavior.
134 To eliminate this case, a stop_machine()-based CPU synchronization was
135 introduced as a way to guarantee that all logical CPUs will not execute
136 any code but just wait in a spin loop, polling an atomic variable.
138 While this took care of device or external interrupts, IPIs including
139 LVT ones, such as CMCI etc, it cannot address other special interrupts
140 that can't be shut off. Those are Machine Check (#MC), System Management
141 (#SMI) and Non-Maskable interrupts (#NMI).
146 Machine Checks (#MC) are non-maskable. There are two kinds of MCEs.
147 Fatal un-recoverable MCEs and recoverable MCEs. While un-recoverable
148 errors are fatal, recoverable errors can also happen in kernel context
149 are also treated as fatal by the kernel.
151 On certain Intel machines, MCEs are also broadcast to all threads in a
152 system. If one thread is in the middle of executing WRMSR, a MCE will be
153 taken at the end of the flow. Either way, they will wait for the thread
154 performing the wrmsr(0x79) to rendezvous in the MCE handler and shutdown
155 eventually if any of the threads in the system fail to check in to the
158 To be paranoid and get predictable behavior, the OS can choose to set
159 MCG_STATUS.MCIP. Since MCEs can be at most one in a system, if an
160 MCE was signaled, the above condition will promote to a system reset
161 automatically. OS can turn off MCIP at the end of the update for that
164 System Management Interrupt
165 ---------------------------
167 SMIs are also broadcast to all CPUs in the platform. Microcode update
168 requests exclusive access to the core before writing to MSR 0x79. So if
169 it does happen such that, one thread is in WRMSR flow, and the 2nd got
170 an SMI, that thread will be stopped in the first instruction in the SMI
173 Since the secondary thread is stopped in the first instruction in SMI,
174 there is very little chance that it would be in the middle of executing
175 an instruction being patched. Plus OS has no way to stop SMIs from
178 Non-Maskable Interrupts
179 -----------------------
181 When thread0 of a core is doing the microcode update, if thread1 is
182 pulled into NMI, that can cause unpredictable behavior due to the
185 OS can choose a variety of methods to avoid running into this situation.
188 Is the microcode suitable for late loading?
189 -------------------------------------------
191 Late loading is done when the system is fully operational and running
192 real workloads. Late loading behavior depends on what the base patch on
193 the CPU is before upgrading to the new patch.
195 This is true for Intel CPUs.
197 Consider, for example, a CPU has patch level 1 and the update is to
200 Between patch1 and patch3, patch2 might have deprecated a software-visible
203 This is unacceptable if software is even potentially using that feature.
204 For instance, say MSR_X is no longer available after an update,
205 accessing that MSR will cause a #GP fault.
207 Basically there is no way to declare a new microcode update suitable
208 for late-loading. This is another one of the problems that caused late
209 loading to be not enabled by default.
214 The loader supports also loading of a builtin microcode supplied through
215 the regular builtin firmware method CONFIG_EXTRA_FIRMWARE. Only 64-bit is
220 CONFIG_EXTRA_FIRMWARE="intel-ucode/06-3a-09 amd-ucode/microcode_amd_fam15h.bin"
221 CONFIG_EXTRA_FIRMWARE_DIR="/lib/firmware"
223 This basically means, you have the following tree structure locally::
228 | |-- microcode_amd_fam15h.bin
235 so that the build system can find those files and integrate them into
236 the final kernel image. The early loader finds them and applies them.
238 Needless to say, this method is not the most flexible one because it
239 requires rebuilding the kernel each time updated microcode from the CPU