<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Alex's Adventures on the Infobahn - aarch64</title><link href="https://www.bennee.com/~alex/" rel="alternate"></link><link href="https://www.bennee.com/~alex/blog/tag/aarch64/feed" rel="self"></link><id>https://www.bennee.com/~alex/</id><updated>2014-05-09T13:14:00+01:00</updated><subtitle>the wanderings of a supposed digital native</subtitle><entry><title>Running Linux in QEMU's aarch64 system emulation mode</title><link href="https://www.bennee.com/~alex/blog/2014/05/09/running-linux-in-qemus-aarch64-system-emulation-mode/" rel="alternate"></link><published>2014-05-09T13:14:00+01:00</published><updated>2014-05-09T13:14:00+01:00</updated><author><name>alex</name></author><id>tag:www.bennee.com,2014-05-09:/~alex/blog/2014/05/09/running-linux-in-qemus-aarch64-system-emulation-mode/</id><summary type="html">&lt;p&gt;Since I started working on &lt;a class="reference external" href="http://translatedcode.wordpress.com/2014/04/24/64-bit-arm-usermode-emulation-in-qemu-2-0-0/"&gt;aarch64 support for QEMU&lt;/a&gt; the most frequently asked question I got was &amp;quot;when can I run aarch64 system emulation on QEMU?&amp;quot;. Well wait no more as support for a VIRT-IO based aarch64 board was recently merged into the master branch of QEMU. In this post …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Since I started working on &lt;a class="reference external" href="http://translatedcode.wordpress.com/2014/04/24/64-bit-arm-usermode-emulation-in-qemu-2-0-0/"&gt;aarch64 support for QEMU&lt;/a&gt; the most frequently asked question I got was &amp;quot;when can I run aarch64 system emulation on QEMU?&amp;quot;. Well wait no more as support for a VIRT-IO based aarch64 board was recently merged into the master branch of QEMU. In this post I'll talk about building QEMU, a rootfs and a kernel that will allow you to start experimenting with the architecture.&lt;/p&gt;
&lt;div class="section" id="quick-start"&gt;
&lt;h2&gt;Quick start&lt;/h2&gt;
&lt;p&gt;Let's first start with building and running QEMU with some pre-built images.&lt;/p&gt;
&lt;div class="section" id="build-dependancies"&gt;
&lt;h3&gt;Build Dependancies&lt;/h3&gt;
&lt;p&gt;As has been noted in the comments the &lt;em&gt;configure&lt;/em&gt; script will automatically enable features as long as the pre-requisite developer libraries are installed on your sytem. With a Debian/Ubuntu system this is easily achieved by running:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sudo apt-get build-dep qemu
&lt;/pre&gt;
&lt;p&gt;Of course if you want to enable a feature (either a bleeding edge or non-standard) that requires additional libraries then you will need to install the appropriate development packages manually. The &lt;em&gt;config.log&lt;/em&gt; file is usually a useful first step in working out what headers are being looked for.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="building-qemu"&gt;
&lt;h3&gt;Building QEMU&lt;/h3&gt;
&lt;pre class="literal-block"&gt;
git clone git://git.qemu.org/qemu.git qemu.git
cd qemu.git
./configure --target-list=aarch64-softmmu
make
&lt;/pre&gt;
&lt;p&gt;Assuming the build ran without any problems you should now have an executable &lt;em&gt;./aarch64-softmmu/qemu-system-aarch64&lt;/em&gt; in your build directory. Grab a pre-built image from &lt;a class="reference external" href="http://people.linaro.org/~alex.bennee/images/aarch64-linux-3.15rc2-buildroot.img"&gt;here&lt;/a&gt; and we'll check it works. The image is a kernel that has been combined with an initial RAM disk (initrd) with a basic root file-system. I go into more details on how to create this later on.&lt;/p&gt;
&lt;p&gt;Be aware the command line is quite long so make sure you copy it all ;-)&lt;/p&gt;
&lt;pre class="literal-block"&gt;
wget http://people.linaro.org/~alex.bennee/images/aarch64-linux-3.15rc2-buildroot.img
./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-linux-3.15rc2-buildroot.img  --append &amp;quot;console=ttyAMA0&amp;quot;
&lt;/pre&gt;
&lt;p&gt;If all went well you should see the familiar Linux boot sequence and eventually get a login prompt. Login as root (no password) and play in the new sandbox.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
... usual kernel boot output ...
Welcome to Buildroot
buildroot login: root
# uname -a
Linux buildroot 3.15.0-rc2ajb-00069-g1aae31c #39 SMP Thu Apr 24 11:48:57 BST 2014 aarch64 GNU/Linux
&lt;/pre&gt;
&lt;p&gt;Once you are done type &lt;em&gt;C-a c&lt;/em&gt; to enter QEMU's monitor mode and then quit to exit.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
QEMU 2.0.50 monitor - type 'help' for more information
(qemu) quit
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="accessing-your-local-file-system"&gt;
&lt;h2&gt;Accessing your local file-system&lt;/h2&gt;
&lt;p&gt;This is all very well but the test image only has a fairly limited root file-system attached to it. It will be a lot more useful if you could access your host file-system to test other binaries. Thanks to VIRT FS we can achieve this without too much hassle. Use the following extended QEMU command line:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-linux-3.15rc2-buildroot.img --append &amp;quot;console=ttyAMA0&amp;quot; -fsdev local,id=r,path=/home/alex/lsrc/qemu/rootfs/trusty-core,security_model=none -device virtio-9p-device,fsdev=r,mount_tag=r
&lt;/pre&gt;
&lt;p&gt;This sets up the selected path to be mountable by the guest. In this case I'm using an Ubuntu rootfs which can be downloaded from &lt;a class="reference external" href="http://cdimages.ubuntu.com/ubuntu-core/releases/14.04/release/"&gt;here&lt;/a&gt;. Once the system has booted the following commands on the guest will mount the local file-system:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Welcome to Buildroot
buildroot login: root
# mount -t 9p -o trans=virtio r /mnt
# ls -l /mnt/
total 84
drwxr-xr-x    2 default  default       4096 Apr  2  2014 bin
drwxr-xr-x    2 default  default       4096 Feb 27  2014 boot
drwxr-xr-x    3 default  default       4096 Apr  2  2014 dev
drwxr-xr-x   64 default  default       4096 Apr  3  2014 etc
drwxr-xr-x    2 default  default       4096 Feb 27  2014 home
..
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section" id="building-your-own-rootfs"&gt;
&lt;h2&gt;Building your own rootfs&lt;/h2&gt;
&lt;p&gt;There are many solutions to this (including downloading &lt;a class="reference external" href="http://www.linaro.org/downloads/"&gt;Linaro engineering builds&lt;/a&gt;) but the simplest one I've found for rolling your own from scratch is the &lt;a class="reference external" href="http://buildroot.uclibc.org/"&gt;Buildroot project&lt;/a&gt;. It present the familiar kernel menuconfig interface and deals with all the hassle of setting up cross compilers for you.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
git clone git://git.buildroot.net/buildroot buildroot.git
cd buildroot.git
make menuconfig
&lt;/pre&gt;
&lt;p&gt;There are lots of configuration options to choose from but the following are what I use:&lt;/p&gt;
&lt;div class="line-block"&gt;
&lt;div class="line"&gt;* Target Options -&amp;gt; Target Architecture(AArch64)&lt;/div&gt;
&lt;div class="line"&gt;* Toolchain -&amp;gt; Toolchain type (External toolchain)&lt;/div&gt;
&lt;div class="line"&gt;* Toolchain -&amp;gt; Toolchain (Linaro AArch64 14.02)&lt;/div&gt;
&lt;div class="line"&gt;* System configuration -&amp;gt; Run a getty (login prompt) after boot (BR2_TARGET_GENERIC_GETTY)&lt;/div&gt;
&lt;div class="line"&gt;* System configuration -&amp;gt; getty options -&amp;gt; TTY Port (ttyAMA0) (BR2_TARGET_GENERIC_GETTY_PORT)&lt;/div&gt;
&lt;div class="line"&gt;* Target Packages -&amp;gt; Show packages that are also provided by busybox (BR2_PACKAGE_BUSYBOX_SHOW_OTHERS)&lt;/div&gt;
&lt;div class="line"&gt;* Filesystem images -&amp;gt; cpio the root filesystem (for use as an initial RAM filesystem) (BR2_TARGET_ROOTFS_CPIO)&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The last one will be important for when we build the kernel next. Once you have configured buildroot to your liking it's time to type make and leave it for a while as you enjoy a nice lunch ;-)&lt;/p&gt;
&lt;pre class="literal-block"&gt;
make
.. lots of output ..
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section" id="building-a-kernel"&gt;
&lt;h2&gt;Building a kernel&lt;/h2&gt;
&lt;p&gt;For building the kernel I use my distro's aarch64 cross-compiler. On Debian/Ubuntu systems this is easily added with:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ sudo apt-get install gcc-aarch64-linux-gnu
&lt;/pre&gt;
&lt;p&gt;And the usual kernel building process, with a few tweaks for cross compiling:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux.git
cd linux.git
ARCH=arm64 make menuconfig
&lt;/pre&gt;
&lt;p&gt;I've put my full config up &lt;a class="reference external" href="http://people.linaro.org/~alex.bennee/images/aarch64-kernel.config"&gt;here&lt;/a&gt; but important options to note are:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
CONFIG_CROSS_COMPILE=&amp;quot;aarch64-linux-gnu-&amp;quot;                                               # needs to match your cross-compiler prefix
CONFIG_INITRAMFS_SOURCE=&amp;quot;/home/alex/lsrc/qemu/buildroot.git/output/images/rootfs.cpio&amp;quot;  # points at your buildroot image
CONFIG_NET_9P=y                                                                         # needed for virtfs mount
CONFIG_NET_9P_VIRTIO=y
&lt;/pre&gt;
&lt;p&gt;Finally you build it all with:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
ARCH=arm64 make -j 8
&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;-j 8&lt;/em&gt; just specifies how many parallel build threads to use. Generally set it to the number of cores you have on your machine.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="final-test"&gt;
&lt;h2&gt;Final test&lt;/h2&gt;
&lt;p&gt;All that remains is to test that the newly built kernel works:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel ../linux.git/arch/arm64/boot/Image  --append &amp;quot;console=ttyAMA0&amp;quot;
... lots more output ...
Welcome to Buildroot
ajbtest login: root
[root&amp;#64;ajbtest ~]# ls -l
total 0
[root&amp;#64;ajbtest ~]# uname -a
Linux ajbtest 3.15.0-rc4ajb-00320-gafcf0a2-dirty #41 SMP Fri May 9 13:05:31 BST 2014 aarch64 GNU/Linux
&lt;/pre&gt;
&lt;div class="line-block"&gt;
&lt;div class="line"&gt;&lt;strong&gt;UPDATED:&lt;/strong&gt; 27/05/2014&lt;/div&gt;
&lt;div class="line"&gt;* Added notes about library dependencies&lt;/div&gt;
&lt;div class="line"&gt;* Cleaned up formatting of shell sections, mention length of command line!&lt;/div&gt;
&lt;div class="line"&gt;* Fix some spelling errors&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><category term="geek"></category><category term="aarch64"></category><category term="arm64"></category><category term="linaro"></category><category term="qemu"></category></entry></feed>