Saturday, 15 February 2014

Building custom (real-time) kernel for Atom (X86)

I found the sound quality really improved by using real time kernel after building real time kernel for Cubieboard2. Therefore I started working on real time kernel for my Atom PC.

I have a small form factor (about size of a 3.5" hard disk) Atom motherboard, the

Some highlights:
  • Atom MT2800 with fanless heatsink  
  • DDR3 SODIMM slot x 1
  • RTL8111E Gigabit Ethernet Adaptor x 2
  • Realtek ALC662 6ch Hi-def Audio on-board 
  • USB 2.0 x 6
When building custom kernel, I aim to build only those I needed for Voyage MPD, so I would like to build the following features:
  • CPU architecture select "Intel Atom" (which means some of the advance instruction sets not appeared in Atom will not be supported)
  • Real time support (Fully preemptive)
  • USB 2.0 controller and USB audio, no other USB device drivers will be built (such as USB network card, USB mouse, TV card, scanner, printer, etc)
  • No AGP, only PCI and PCIe controller
  • VGA only, no framebuffer, no 3D support.
  • SATA and SCSI (no legacy support like PATA)
  • IP v4 only without advance features like tunneling, multicast, etc
  • Driver (module) for realtek R8169 only (this is compatable with RTL8111E)
  • No virtualization support
  • No debugging / verbose support for every hardware!
I think that today's Linux kernel is very mature in the sense that building all the features in modules doesn't hurt the performance obviously. However, I believe that the system will still load those modules if it detects a hardware is supported, which will introduce some unnecessary latency inevitability. So the best way is not to provide those modules and not to build into the kernel core. Of course, the baseline is to have a kernel build that is bootable and be able to play music flawlessly. So I did compiling and testing the same kernel version with different combination of options on and off.

What I am trying is say is that, when you build your own kernel for a x86 PC, bare in mind that it may not success at once. You may need to turn the options on and off and try until you find one workable configuration with least number of modules loaded / built into the kernel.

Ok, let's start building the kernel!

First, get a disk drive (USB drive or SATA hard disk) with 4G (8G or more preferred) available. Load it with Voyage current version. You may refer to the "readme" of Voyage MPD, section 2 - "Installation" for the official setup guide. I used version 0.9.2:

The official installation guide assumes you run on a Linux environment. If you don't have another Voyage MPD ready, the easiest way is to download Ubuntu 12.04 32bit live CD and run in Ubuntu Desktop.

When you have the Voyage MPD disk ready, try to boot it in your target machine and make sure it can boot up flawlessly. After that, you can start the following steps on the same machine, or you may boot the Voyage MPD on another faster PC (say an Intel i7) so that you can build kernel much faster.

1. Connect a LAN cable (with internet connection), power up. After boot up, login (username=root, password=voyage). Remount as "read/write" mode:

2. Set correct date/time (optional, do it if necessary)
For example, set new data to 2 Oct 2006 18:00:00, type the following command:
# date -s "2 OCT 2006 18:00:00"
3. Update application package database:
%apt-get update
(this would take a minute or two, depends on your Internet connection)

%apt-get upgrade
(this would take another 2 to 10 minutes, depends on your Internet connection)

4. Install the required packages:
%apt-get install wget build-essential libncurses5-dev openssl bc
 (this would take about 10 minutes or more, be patient)

5. Download a kernel version with RT project. Check here for the available real-time kernel patch versions first.
%cd (go to root's home directory)

Then check the link for the corresponding kernel version in here:

In my case, I selected kernel version 3.10.27-rt25. Highlight and copy the URL for the kernel patch and paste the URL with wget command to download:
Extract it:
%unxz patch-3.10.27-rt25.patch.xz

And download the kernel source:
%wget --no-check-certificate
Extract it:
%tar xvzf linux-3.10.27.tar.gz
(a folder called "linux-3.10.27" under "/root" will be created)

6. Applies the real time kernel patch:
%cp patch-3.10.27-rt25.patch linux-3.10.27
%cd linux-3.10.27
%patch -p1 < patch-3.10.27-rt25.patch
(it would patch the kernel source in few seconds...)

7. Optional, but recommended, edit the "Makefile"
%nano Makefile
See the line starting with "EXTRAVERSION" (it should be the forth line), go to the end, add some text, say "-atom" (without quotes), the "extra version" just appended after the minor version.

Remember not to add a space "after" the text you added. You can have white space between the "=" and the first character of the text

Also optional, but recommended, copy the kernel configuration file that comes with Voyage MPD:
%cd /boot
%ls config*
You shall see a file named "config-3.x.x-voyage", just copy this file to the linux kernel source folder that you just made above and rename as ".config" (note that there is a dot before "config")
%cp config-3.10.11-voyage /root/linux-3.10.27/.config

8. Start kernel configuration menu:
%make menuconfig

You shall see a text mode menu with blue background. You (must) do the following changes in order to enjoy the real-time kernel:

Kernel Features -> Preemption Model
Change to "Fully Preemptible Kernel (RT)"

General setup -> RCU Subsystem, enables:
-Enable RCU priority boosting
-Offload RCU callback processing from boot-selected CPUs

Optional: As mentioned above, remove all the drivers and features that are not need. This could make the kernel and driver size smaller and minimize the change of CPU get bothered.
(Tips #1: Check the inline help page for explaination of the feature and the suggestion of keep or not

Tips #2: I found a web tutorial written by DevynCJohnson which is very comprehensive and useful. You may get more idea about what to include to kernel build after reading it:
Update (Aug-4-2014): Please select 32-bit kernel if you are building for Voyage Linux. 64-bit kernel doesn't work on Voyage Linux.

Exit and save the configuration. (The configuration would be saved as ".config", a hidden file, under the root folder of the kernel source, i.e. under "/root/linux-3.10.27" in my case)

9. Start building kernel

%make -j2 all

this step will compile the kernel and create a compressed binary image of the kernel and also the loadable kernel modules. After the step, the kernel image can be found at arch/i386/boot/bzImage (for a x86 based processor) and kernel modules (i.e. those files ended with .ko) will be built.

Remember to use "-j" option if you are building on a multiple core CPU. "-j2" means running the build in two threads. You may use "-j4" if you have 4 core/virtual threads in total.

(this would take at 10 minutes up to 2 hours, depends on what you have selected to build and the CPU power)

10. Install to Voyage MPD disk
%make -j2 modules_install
The above command will make those kernel modules being copied to "/lib/modules/3.10.27-rt25")
%make -j2 install
The above command will build the SystemMap, vmlinuz, initrd and config files that appear in "/boot". It will also update the GRUB (the boot loader) to include the newly built kernel as option in the boot menu.

11. You may want to touch up the boot menu to make it more readable and remove unuse menu items. Just:
%nano /boot/grub/menu.lst

Important: The default kernel menu links the kernel images in the root folder. (/vmlinuz and /initrd.img), this two links were being redirected to the newly built kernel images after executing the "make install" command. So you should update the menu.lst file to make the link points to the ACTUAL files instead of the symbolic links.

12. Now you can reboot and you shall see the newly built kernel version as one of the boot options. Select it and see what happen. It may not boot successfully, especially if you didn't select most of the items. If it just can't boot up or you want to further fine tune, do the following:

13. Reboot with the stock kernel version selected in boot menu, it shall boot up normally.

14. Login as root, remount as read/write:

15. Goto /root/linux-{version}, if your recent kernel built works fine, I suggest making a backup of the ".config" in case the new kernel built doesn't work.
%cp .config config.bak
Run make menuconfig again.

16. Based on the error message (you have to "guess" what blocks the machine bootup!), enable it this time.

Or, if it could boot up but some hardware is not working, just enable that particular hardware. You can choose to build that device driver into kernel core or build as module. I think it doesn't have noticible performance hurt for building the device driver as kernel module but it certainly would be more flexible. (For me, I built the USB keyboard, USB wi-fi driver as kernel modules because I may not always need them.)

17. Save and exit the kernel configuration menu, delete the files created previously.
%rm /boot/*3.10.27*
This will remove those files with the kernel version as part of the file name.
%rm /vmlinuz /vmlinuz.old /initrd.img /initrd.img.old

repeat steps 9-12. Good luck!

1 comment: