Friday, October 9, 2015

Sabayon and microcodes

Hi everybody. This is the first of new kind of posts on my blog to help Sabayon users (and Linux users in general) to figure out how to fix some issue that I found on my way.
Today I want to share with you a little and simple guide to upgrade microcode in your Sabayon. 
But let's us start from the beginning.

What is microcode?

Intel and AMD microcodes are, citing Debian wiki:
Processor microcode is akin to processor firmware. The kernel is able to update the processor's firmware without the need to update it via a BIOS update.
so to guarantee the system stability and security, you need to keep your CPU microcodes updated.

How do I update my microcodes?

If you have an AMD CPU the microcode are shipped in the linux-firmware package, so you need to do nothing if you update frequently your system, but things get a bit more complex with an Intel CPU (like Haswell).
That is because Intel ucode need to be loaded before the kernel, so you have to do this using the following steps.
The first thing to do consist in generating the microcode cpio archive through iucode_tool (you can find it on sabayonlinux.org repo installing sys-apps/iucode_tool):

root# iucode_tool -S --write-earlyfw=/boot/early-ucode.cpio /lib/firmware/intel-ucode/*
iucode_tool: system has processor(s) with signature 0x000306c3
iucode_tool: Writing selected microcodes to: /boot/early-ucode.cpio

the file early-ucode.cpio should be placed in the initrd of your grub.cfg just before your kernel, but if you put it there directly every time you will regenerate your grub it will be removed.
So the best thing to do is edit the script used from grub-mkconfig.
The file you are looking for is /etc/grub.d/10_linux and you need to change the if clause here reported as follows.

140
141
142
143
144
145
146
147
 if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    message="$(gettext_printf "Loading initial ramdisk ...")"
    sed "s/^/$submenu_indentation/" << EOF
 echo '$(echo "$message" | grub_quote)'
 initrd ${rel_dirname}/early-ucode.cpio ${rel_dirname}/${initrd}
EOF
  fi

(WARNING: line's numbers reported could change between GRUB 2 versions)
After this we have almost done, so you can simply rebuild your GRUB configuration using:

root# grub-mkconfig -o /boot/grub/grub.cfg

To check if you upgraded correctly your microcode dmesg should be like this:

root#  dmesg | grep microcode
[    6.430637] microcode: CPU0 sig=0x306c3, pf=0x10, revision=0x8
[    6.432835] microcode: CPU0 sig=0x306c3, pf=0x10, revision=0x8
[    6.434503] microcode: CPU0 updated to revision 0x1c, date = 2014-07-03
[    6.434511] microcode: CPU1 sig=0x306c3, pf=0x10, revision=0x8
[    6.436075] microcode: CPU1 sig=0x306c3, pf=0x10, revision=0x8
[    6.436958] microcode: CPU1 updated to revision 0x1c, date = 2014-07-03
[    6.436987] microcode: CPU2 sig=0x306c3, pf=0x10, revision=0x8
[    6.437015] microcode: CPU2 sig=0x306c3, pf=0x10, revision=0x8
[    6.437903] microcode: CPU2 updated to revision 0x1c, date = 2014-07-03
[    6.437912] microcode: CPU3 sig=0x306c3, pf=0x10, revision=0x8
[    6.437934] microcode: CPU3 sig=0x306c3, pf=0x10, revision=0x8
[    6.438823] microcode: CPU3 updated to revision 0x1c, date = 2014-07-03
[    6.438830] microcode: CPU4 sig=0x306c3, pf=0x10, revision=0x8
[    6.438864] microcode: CPU4 sig=0x306c3, pf=0x10, revision=0x8
[    6.439753] microcode: CPU4 updated to revision 0x1c, date = 2014-07-03
[    6.439760] microcode: CPU5 sig=0x306c3, pf=0x10, revision=0x8
[    6.439785] microcode: CPU5 sig=0x306c3, pf=0x10, revision=0x8
[    6.440669] microcode: CPU5 updated to revision 0x1c, date = 2014-07-03
[    6.440682] microcode: CPU6 sig=0x306c3, pf=0x10, revision=0x8
[    6.440707] microcode: CPU6 sig=0x306c3, pf=0x10, revision=0x8
[    6.441594] microcode: CPU6 updated to revision 0x1c, date = 2014-07-03
[    6.441601] microcode: CPU7 sig=0x306c3, pf=0x10, revision=0x8
[    6.441621] microcode: CPU7 sig=0x306c3, pf=0x10, revision=0x8
[    6.442512] microcode: CPU7 updated to revision 0x1c, date = 2014-07-03
[    6.442557] microcode: Microcode Update Driver: v2.00 <tigran@aivazian.fsnet.co.uk>, Peter Oruba

Remember to check frequently if you have the last microcode updated and don't forget to regenerate your cpio file.
And that's all folks, I hope this could help you to have a more stable system.

Sources: