andreas.schaertl

Compiling aarch64 Binaries for gem5 SE Mode

tech

Update April 2019: As of this commit, aarch64 Linux binaries should work as-is in SE mode, even if compiled with glibc. Thank you Ciro for letting me know.

The gem5 simulator has a useful system call emulation mode that lets you run programs without a Linux kernel. The system calls issued by your program are handled by gem5.

Unfortunately, using regular GCC with glibc creates programs that gem5 does not fully support.

~/gem5dir $ aarch64-unknown-linux-gnu-gcc -static hello.c -o hello
~/gem5dir $ build/ARM/gem5.opt configs/example/se.py -c hello
gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 compiled Sep  8 2018 15:18:08
gem5 started Sep  9 2018 10:45:17
gem5 executing on faui00h, pid 12059
command line: build/ARM/gem5.opt configs/example/se.py -c hello

/proj/ciptmp/ru64tiji/BA/Git/gem5/configs/common/CacheConfig.py:48: \
	SyntaxWarning: import * only allowed at module level
def config_cache(options, system):
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address \
	range assigned (512 Mbytes)
0: system.remote_gdb: listening for remote gdb on port 7000
**** REAL SIMULATION ****
info: Entering event queue @ 0.  Starting simulation...
panic: Attempted to execute unimplemented instruction 'mrs' \
	(inst 0x4d5380001)
Memory Usage: 639976 KBytes
Program aborted at tick 266500

The main culprit is the following error.

panic: Attempted to execute unimplemented instruction 'mrs'

To get a working program, you can use an embedded C library that does not use the advanced features glibc uses. I’ve had success with uClibc.

Using uClibc does require you to build your own tool chain including compiler, but it’s all automated and explained well at www.uclibc.org/about.html. Essentially, you’re going to do the following:

  1. Download buildroot.
  2. Run make menuconfig.
  3. Set (1) Target Architecture to AArch64 (little endian) and (2) Build options > libraries to static only. If you want C++ support or some other features, there are flags to configure that as well. It’s probably a good idea to go through all options, there aren’t that many.
  4. Save your config, exit the menu and run make.

After compilation, the directory /output/host/bin contains your just built compilers and binutils for aarch64. Let’s try them out!

~/gem5dir $ aarch64-buildroot-linux-uclibc-gcc -static hello.c -o hello
~/gem5dir $ build/ARM/gem5.opt configs/example/se.py -c hello
gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 compiled Sep  8 2018 15:18:08
gem5 started Sep  9 2018 10:58:26
gem5 executing on faui00h, pid 13513
command line: build/ARM/gem5.opt configs/example/se.py -c hello

/proj/ciptmp/ru64tiji/BA/Git/gem5/configs/common/CacheConfig.py:48: \
	SyntaxWarning: import * only allowed at module level
def config_cache(options, system):
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address \
	range assigned (512 Mbytes)
warn: Unknown operating system; assuming Linux.
0: system.remote_gdb: listening for remote gdb on port 7000
**** REAL SIMULATION ****
info: Entering event queue @ 0.  Starting simulation...
Hello, ARM!
Exiting @ tick 589000 because exiting with last active thread context

Success!