17. The Linux Microcode Loader

Authors:

The kernel has a x86 microcode loading facility which is supposed toprovide microcode loading methods in the OS. Potential use cases areupdating the microcode on platforms beyond the OEM End-Of-Life support,and updating the microcode on long-running systems without rebooting.

The loader supports three loading methods:

17.1. Early load microcode

The kernel can update microcode very early during boot. Loadingmicrocode early can fix CPU issues before they are observed duringkernel boot time.

The microcode is stored in an initrd file. During boot, it is read fromit and loaded into the CPU cores.

The format of the combined initrd image is microcode in (uncompressed)cpio format followed by the (possibly compressed) initrd image. Theloader parses the combined initrd image during boot.

The microcode files in cpio name space are:

on Intel:
kernel/x86/microcode/GenuineIntel.bin
on AMD :
kernel/x86/microcode/AuthenticAMD.bin

During BSP (BootStrapping Processor) boot (pre-SMP), the kernelscans the microcode file in the initrd. If microcode matching theCPU is found, it will be applied in the BSP and later on in all APs(Application Processors).

The loader also saves the matching microcode for the CPU in memory.Thus, the cached microcode patch is applied when CPUs resume from asleep state.

Here’s a crude example how to prepare an initrd with microcode (this isnormally done automatically by the distribution, when recreating theinitrd, so you don’t really have to do it yourself. It is documentedhere for future reference only).

#!/bin/bashif [ -z "$1" ]; then    echo "You need to supply an initrd file"    exit 1fiINITRD="$1"DSTDIR=kernel/x86/microcodeTMPDIR=/tmp/initrdrm -rf $TMPDIRmkdir $TMPDIRcd $TMPDIRmkdir -p $DSTDIRif [ -d /lib/firmware/amd-ucode ]; then        cat /lib/firmware/amd-ucode/microcode_amd*.bin > $DSTDIR/AuthenticAMD.binfiif [ -d /lib/firmware/intel-ucode ]; then        cat /lib/firmware/intel-ucode/* > $DSTDIR/GenuineIntel.binfifind . | cpio -o -H newc >../ucode.cpiocd ..mv $INITRD $INITRD.origcat ucode.cpio $INITRD.orig > $INITRDrm -rf $TMPDIR

The system needs to have the microcode packages installed into/lib/firmware or you need to fixup the paths above if yours aresomewhere else and/or you’ve downloaded them directly from the processorvendor’s site.

17.2. Late loading

There are two legacy user space interfaces to load microcode, either through/dev/cpu/microcode or through /sys/devices/system/cpu/microcode/reload filein sysfs.

The /dev/cpu/microcode method is deprecated because it needs a specialuserspace tool for that.

The easier method is simply installing the microcode packages your distrosupplies and running:

# echo 1 > /sys/devices/system/cpu/microcode/reload

as root.

The loading mechanism looks for microcode blobs in/lib/firmware/{intel-ucode,amd-ucode}. The default distro installationpackages already put them there.

17.3. Builtin microcode

The loader supports also loading of a builtin microcode supplied throughthe regular builtin firmware method CONFIG_EXTRA_FIRMWARE. Only 64-bit iscurrently supported.

Here’s an example:

CONFIG_EXTRA_FIRMWARE="intel-ucode/06-3a-09 amd-ucode/microcode_amd_fam15h.bin"CONFIG_EXTRA_FIRMWARE_DIR="/lib/firmware"

This basically means, you have the following tree structure locally:

/lib/firmware/|-- amd-ucode...|   |-- microcode_amd_fam15h.bin...|-- intel-ucode...|   |-- 06-3a-09...

so that the build system can find those files and integrate them intothe final kernel image. The early loader finds them and applies them.

Needless to say, this method is not the most flexible one because itrequires rebuilding the kernel each time updated microcode from the CPUvendor is available.