Wednesday, August 19, 2015

Compiling a Linux Kernel for gem5

It took me a while to find a combination of versions of the Linux kernel and gcc that would boot on gem5 simulation x86, so I wanted to take some notes on the version I finally got to work.

First, you need the full system files for x86 for gem5, as explained in step 8 of this post. I'll go ahead and repost those instructions here.



  • To run in full system mode, you will need to download some more files from the gem5 website.
  • First, you'll need to download the full system files and config files for X86. They are available here: http://www.m5sim.org/Download. Extract both of these to a directory where they will live (for our example, we'll assume this directory is /home/user123/gem5-util/). They need to be in a place where gem5 can see them while they are running. When you extract these files, you will get two directories, "binaries" and "disks".
  • In the "disks" directory, there is a file called "linux-x86.img". This needs to be renamed to "x86root.img".
  • gem5 also needs a file called "linux2-bigswap.img" to be in the disks directory. The only place I've found this is in the full system files for the Alpha architecture posted on the gem5 website, here:http://www.m5sim.org/Download. Download the Alpha system files, extract them, and move the file "linux2-bigswap.img" from this package to the "disks" directory containing the file "x86root.img".
  • Now, set the environment variable M5_PATH to point to /home/user123/gem5-util (or wherever you put the "disks" and "config" directories).

Next, clone the Linux kernel repo using git.

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

The version of the kernel I've gotten to work with gem5 is 2.6.27.6, so checkout that one out.

$ git checkout v2.6.27.6.

Now, get the config files provided by gem5.

wget http://www.m5sim.org/dist/current/x86/config-x86.tar.bz2
$ tar -xjvf config-x86.tar.bz2

The only configuration I've gotten to work is "linux-2.6.22.9".

$ cp configs/linux-2.6.22.9 .config

I had to roll back to gcc-3.4.6 to get it to compile. You can get gcc-3.4.6 from here: link. I forced the kernel build system to use this version by doing:

$ ln -s <path to gcc-3.4.6> ./gcc
$ ln -s <path to gcc-3.4.6> ./g++
$ export PATH=:$PATH

Adding the colon to the beginning tells bash to look in the pwd first for programs. Now, build it.

$ make vmlinux -j

And you should be able to boot with this using gem5. It asks a bunch of questions regarding kernel configuration. I answer the default for all of them, and this works fine.

$ ./build/X86/gem5.opt ./configs/example/fs.py --cpu-type=timing \
--disk-image=<path to x86root.img> \
--kernel=<path to built vmlinux>

For a little more information on this topic, see this discussion on the gem5 mailing list: link.