<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Linux Blog &#187; Qemu</title>
	<atom:link href="http://linuxblog.ro/tag/qemu/feed/" rel="self" type="application/rss+xml" />
	<link>http://linuxblog.ro</link>
	<description>UNIX, LINUX, BSD, OSX</description>
	<lastBuildDate>Thu, 19 Aug 2010 14:32:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Building an embedded Linux system emulator</title>
		<link>http://linuxblog.ro/building-an-embedded-linux-system-emulator/</link>
		<comments>http://linuxblog.ro/building-an-embedded-linux-system-emulator/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 19:18:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shells & Scripts]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Qemu]]></category>
		<category><![CDATA[system]]></category>
		<category><![CDATA[virtual]]></category>

		<guid isPermaLink="false">http://linuxblog.ro/?p=185</guid>
		<description><![CDATA[One of the hallmarks of embedded system programming is working with specialized hardware. Unfortunately, embedded system developers do not always have the luxury to develop and test their code on the actual hardware they target.]]></description>
			<content:encoded><![CDATA[<p>One of the hallmarks of embedded system programming is working with specialized  hardware. Unfortunately, embedded system developers do not always have the  luxury to develop and test their code on the actual hardware they target. Often,  the hardware is developed in tandem with the system software and therefore it it  is not available for much of the embedded system software development cycle.</p>
<p>While one can develop and test much of our  code on a PC running Linux, such a PC is a very different environment from the  target board. More often then not, the target board is not even of the same  architecture as the PC. A solution to this problem can be obtained by using an  emulator &#8211; a software tool that executes software code of our target platform in  a virtual machine that is running on our development host, and running our  system software in it.</p>
<p>The following  article describes how to build an embedded Linux system running inside an  emulator which can be used to develop, test and debug target code even without  access to target hardware.</p>
<p><strong>The components</strong></p>
<p>To build our emulator we will need the following components:</p>
<ul><span></p>
<li>Hardware emulator (we&#8217;ll use Qemu)</li>
<li>Minimal Linux root file system containing a C library and <a href="http://www.busybox.net/" target="new"><span style="text-decoration: underline;">Busybox</span></a></li>
<li>The <a title="Linux" href="http://kernel.org/" target="new"><span style="text-decoration: underline;">Linux</span></a> kernel</li>
<p></span></ul>
<p><span><br />
<strong>Installing Qemu</strong></span></p>
<p>Created by Fabrice Ballard, <a href="http://qemu.org/" target="new">Qemu</a> is an open source machine emulator supporting seven target  architectures, including x86, MIPS, ARM, and PowerPC. As first step, we will  download and install the emulator. Depending on the Linux distribution you use  on your workstation, you might be able to use the native package management  system of the distribution to do so.</p>
<p>For  Debian, Ubuntu and derivatives:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ sudo apt-get install qemu</pre>
</td>
</tr>
</tbody>
</table>
<p>For Fedora and derivatives (as root):</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;"># yum install qemu</pre>
</td>
</tr>
</tbody>
</table>
<p>For other distributions lacking a Qemu package, or for those  wishing to obtain the very latest package (note that the &#8220;i386&#8243; label refers to  the host running the emulator, and not the target platform):</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ wget http://bellard.org/qemu/qemu-0.9.1-i386.tar.gz
$ cd /
$ sudo tar zxvf qemu-0.9.1-i386.tar.gz</pre>
</td>
</tr>
</tbody>
</table>
<p>Or, as root:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;"># tar zxvf qemu-0.9.1-i386.tar.gz</pre>
</td>
</tr>
</tbody>
</table>
<p>Alternatively, you can download the sources and build the  emulator from scratch. This has the added advantage that you can later adapt the  emulator to more accurately reflect your actual hardware:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ wget http://bellard.org/qemu/qemu-0.9.1.tar.gz
$ tar zxvf qemu-0.9.1.tar.gz
$ cd qemu-0.9.1/
$ ./configure
$ make
$ sudo make install</pre>
</td>
</tr>
</tbody>
</table>
<p>Or, as root:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;"># make install</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Kernel and file system  images</strong></p>
<p>The Qemu emulator we have just  installed provides a virtual machine mimicking our target hardware. To actually  get Linux running on this virtual machine, however, we will need to download an  image of the Linux kernel and a suitable root file system image for our target  architecture.</p>
<p>Luckily, the Qemu project  provides test images for several architectures that can be used to get a fast  start with Qemu as an embedded Linux system emulator. Go to the Qemu project <a title="Qemu download page" href="http://bellard.org/qemu/download.html" target="new"><span style="text-decoration: underline;">download page</span></a> and choose one of the Qemu test disk images  suitable for your embedded platform and download it to your Linux host (in this  example we use ARM):</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ wget http://bellard.org/qemu/arm-test-0.2.tar.gz</pre>
</td>
</tr>
</tbody>
</table>
<p>Now extract the image:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ tar zxvf arm-test-0.2.tar.gz
$ cd arm-test</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Booting Linux on the  emulator</strong></p>
<p>Start up Qemu with the  following command line, adjusting the architecture name, kernel file name, and  root file system image name according to your specific architecture (again, we  use ARM in this example):</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ qemu-system-arm -kernel zImage.integrator \
   -initrd arm_root.img -tftp / -redir tcp:9999::9999</pre>
</td>
</tr>
</tbody>
</table>
<p>The above command line starts Qemu in system emulation mode,  booting into the kernel image zImage.integrator while loading into the virtual  machine RAM the arm_root.img file system, and instructing Qemu to make your  entire host root file system available for access via TFTP from the emulated  machine (more on this ahead).</p>
<p>You should  now be seeing a window similar to the following in which the emulated LCD  display of the board is shown in main screenshot.</p>
<p>You can log-in with the user &#8220;root&#8221; &#8212; no password is required.</p>
<p><strong>Transferring files to and from the host</strong></p>
<p>The emulator and file system are set up to  automatically configure a virtual Ethernet interface in the virtual machine with  an internal IP. Through that virtual network interface, the emulator is set up  to enable transferring of files to and from the host machine file system using  the TFTP protocol.</p>
<p>For example, the  following command will copy the file &#8220;/home/gby/hello_emu&#8221; from the host file  system to the current directory inside the emulator:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ tftp -g -r /home/gby/hello_emu -l hello_emu 10.0.2.2</pre>
</td>
</tr>
</tbody>
</table>
<p>The following command will copy the file &#8220;/root/test.log&#8221; from  the emulator to the host file system directory &#8220;/home/gby/&#8221;:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ tftp -p -l/root/test.log -r /home/gby/test.log 10.0.2.2</pre>
</td>
</tr>
</tbody>
</table>
<p>In addition, you can use the &#8220;wget&#8221; comment to transfer files  using the FTP and HTTP protocol to the emulator from any compatible server  accessible in the network:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ wget http://codefidence.com/some/file</pre>
</td>
</tr>
</tbody>
</table>
<p>Qemu supports numerous other way to interact with the host and  it&#8217;s environment, including bridged virtual network interfaces (as opposed to  the default NAT used in the example above). Bridged virtual network interfaces  enable:</p>
<ul><span></p>
<li>Using NFS to communicate with the host</li>
<li>Remote debugging from the host</li>
<li>VLAN support</li>
<li>Exposing the host file system as a FAT file system</li>
<li>Mounting disk, flash, or CDROM images from the host file  system</li>
<li>Using USB devices connected to the host</li>
<p></span></ul>
<p><span>For more  information on these advanced options, please refer to the <a title="Qemu user manual" href="http://bellard.org/qemu/qemu-doc.html" target="new">Qemu user manual</a>.</span></p>
<p><strong>Debugging user applications</strong></p>
<p>Using the GNU debugger GDBserver agent, we  can debug applications running inside the emulator using the GDB debugger on the  host. To do this, first use one of the methods outlined above to copy the  &#8220;gdbserver&#8221; executable to the emulator. Note that you will need a gdbserver  executable that was built to run on the target architecture (such as ARM, in the  example above), and not on that of the host!</p>
<p>Also note that since the test images do not contain debugging  symbols for the system libraries, you will only be able to debug statically  compiled applications using them. This limitation can be removed by building  your own kernel and file system image (see below for more information on this  topic).</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ tftp -g -r /home/gby/src/gdb/gdb/gdbserver/gdberver \
   -l gdbserver 10.0.2.2</pre>
</td>
</tr>
</tbody>
</table>
<p>Next, assign the gdbserver binary execute permissions:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ chmod u+x gdbserver</pre>
</td>
</tr>
</tbody>
</table>
<p>Now, run the gdbserver agent, instructing it to use port 9999  (which we previously redirected to the emulator, when we launched  qemu-system-arm from the command-line) to listen for connections from the  debugger:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ gdbserver 0.0.0.0:9999 /bin/myprog</pre>
</td>
</tr>
</tbody>
</table>
<p>Or, if you wish to attach to an already running program, use:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ gdbserver 0.0.0.0:9999 --attach 1234</pre>
</td>
</tr>
</tbody>
</table>
<p>Finally, run the GDB debugger on your host and instruct it to  connect to the host local port 9999:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ arm-linux-gdb target/bin/myprog
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
...
(gdb) set solib-absulote-prefix /dev/null
(gdb) set solib-search-path target/lib/
(gdb) target remote 127.0.0.1:9999</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Debugging the  kernel</strong></p>
<p>Using the Qemu emulator to  debug kernel code is quite straight forward, as Qemu incorporates a minimal GDB  agent as part of the emulator itself. To debug the Linux kernel running inside  the emulator, add the &#8220;-s&#8221; parameter to the command line used to start Qemu:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ qemu-system-arm -kernel zImage.integrator \
    -initrd arm_root.img -tftp / -redir tcp:9999::9999 -s</pre>
</td>
</tr>
</tbody>
</table>
<p>Now when the emulator starts, it will wait for a debugger  connection on the default port &#8220;1234&#8243; (or a different port specific with the  &#8220;-p&#8221; option), before proceeding with the boot. Once the emulator has started,  you can debug the Linux kernel running inside it, using GDB on the host:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ arm-linux-gdb linux/vmlinux
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
...
(gdb) target remote 127.0.0.1:1234</pre>
</td>
</tr>
</tbody>
</table>
<p>You can use GDB as you normally would. For example, type &#8220;cont&#8221;  to launch the kernel:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">(gdb) cont</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Building your own kernel  and file system images</strong></p>
<p>So far we have  seen how to use the Qemu emulator with the test kernel and file system images  that are available on the Qemu site. To make full use of the emulator, we can  create our own custom kernel and file system images that will better reflect the  real target we are trying to develop for.</p>
<p>First, query Qemu regarding which boards it can emulate for your  chosen architecture. Replace &#8220;arm&#8221; in the example above with one of: mips,  x86_64, ppc, or sparc. For i386, simply use &#8220;qemu&#8221; as the command:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ qemu-system-arm -M \?</pre>
</td>
</tr>
</tbody>
</table>
<p>Choose the board that most closely resembles your real target  environment. Note that you can add support to Qemu of your specific true board.  This requires some programming though, and we shall not cover it in this  tutorial.</p>
<p>The creation of a kernel and  file system for our emulated target is no different then doing the same task for  real hardware. In fact, many tools are freely available to accomplish this task.  In this example, we shall use the Buildroot framework. Buildroot is a set of  make files and patches that simplify the generation of a cross-compilation tool  chain and root file system for your target Linux system, using the uClibc C  library.</p>
<p>First, we shall download the  latest Buildroot release from the project web site and extract it:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ wget http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2
$ tar jxvf buildroot-snapshot.tar.bz2
$ cd buildroot/</pre>
</td>
</tr>
</tbody>
</table>
<p>Next, let&#8217;s configure Buildroot for our chosen target board:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ make menuconfig</pre>
</td>
</tr>
</tbody>
</table>
<p>You will be presented with a menu enabling you to pick your  architecture, sub-architecture, specific board to build for. Other options  include GCC and uClibc versions, and related details. For each menu choice in  the configuration tool, you can find associated help information describing the  purpose of the entry.</p>
<p>At minimum, the  following configuration options needs to be set:</p>
<ul><span></p>
<li>Target Architecture option &#8212; choose your target  architecture (e.g., arm.)</li>
<li>Target Architecture Variant option &#8212; Chose a specific model  of the architecture (e.g., arm926t).</li>
<li>Target options menu &#8212; If the target board you wish to  emulate (that is supported by Qemu) is listed, turn on support for that board  (e.g., enable the &#8220;ARM Ltd. Device Support&#8221; menu, and inside it choose the  &#8220;Integrator arm926&#8243; option).</li>
<li>Toolchain menu &#8212; Turn on &#8220;Build gdb server for the Target&#8221;  option, and if you would like to test C++ programs on the emulator, also the  &#8220;C++ cross-compiler support&#8221; option.</li>
<li>Target filesystem options menu &#8212; Enable the &#8220;cpio the root  filesystem&#8221; option, and choose the &#8220;gzip&#8221; compression method. You may also  request the file system image to be copied to a specified directory once it is  generated.</li>
<li>Kernel menu &#8212; Choose the &#8220;linux (Advanced configuration)&#8221;  option, and pick one of the offered Linux kernel versions of the list offered.  Also, select the &#8220;zImage&#8221; binary format. Here, you can also specify a directory  to copy the generated kernel to.In  addition, you will need to supply a proper Linux kernel configuration file. Note  that you can extract the kernel configuration file used to generate the kernel  supplied as part of the test images, by issuing the following command from  inside the emulator:<br />
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ zcat /proc/config.gz &gt; linux.config</pre>
</td>
</tr>
</tbody>
</table>
<p>Alternatively, Linux provides specific kernel configuration for  optimal use with Qemu for some architectures. Run the following command to  inspect the default kernel configuration included in a specific Linux kernel  version:</p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ make help</pre>
</td>
</tr>
</tbody>
</table>
</li>
<p></span></ul>
<p><span>When  you&#8217;re done configuring Buildroot, exit the configuration utility (making sure  to OK saving the changes) and type: &#8220;make&#8221;. Buildroot will now download all  required sources, and build your new kernel and file system image for you. You  should now be able to run the emulator using the kernel and file system image  you have just created. Use the file name and path of the zImage binary as a  parameter to Qemu&#8217;s &#8220;-kernel&#8221; option, and the file name and path of the file  system image with Qemu&#8217;s &#8220;-initrd&#8221; parameter, like so:</span></p>
<table border="1" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td bgcolor="#e9e9e9">
<pre style="font-family: monospace;">$ qemu-system-arm -kernel zImage \
    -initrd rootfs.arm.cpio.gz -tftp / -redir tcp:9999::9999 -s</pre>
</td>
</tr>
</tbody>
</table>
<p>As we have shown, the Qemu emulator provides  a fairly simple way to develop, debug, and test Linux kernels, drivers, and  applications for a variety of embedded architectures, even when no actual  hardware is available. More information about the software used in this article  can be found on the <a href="http://bellard.org/qemu/" target="new"><span style="text-decoration: underline;">qemu</span></a>, <a href="http://www.gnu.org/software/gdb/" target="new"><span style="text-decoration: underline;">gdb</span></a>, and <a href="http://buildroot.uclibc.org/" target="new"><span style="text-decoration: underline;">Buildroot</span></a> websites.</p>
<p><strong>About the author</strong> &#8212; Gilad Ben-Yossef is the co-founder  and CTO of <a href="http://codefidence.com/" target="new"><span style="text-decoration: underline;">Codefidence  Ltd</span></a>, and has been helping OEMs make and use free and open source  software in commercial products and services since 1998. He is also the  co-author of the book &#8220;<a href="http://oreilly.com/catalog/9780596529680/index.html" target="new"><span style="text-decoration: underline;">Building Embedded Linux Systems</span></a>,&#8221; 2nd Edition. In  addition, he is co-founder of <a href="http://hamakor.org.il/" target="new"><span style="text-decoration: underline;">Hamakor</span></a>, an NPO devoted to the promotion of FOSS in  Israel, as well as a founding organizer of &#8220;<a href="http://august.penguin.org.il/" target="new"><span style="text-decoration: underline;">August Penguin</span></a>,&#8221; an  Israeli community FOSS conference.</p>
<p>Gilad  is a member of the Israeli chapter of Mensa, the Israeli Information Technology  Association and the Israeli chapter of the Internet Society. He holds a B.A. in  Computer Science from Tel-Aviv Jaffa Academic College. When not trying to make  FOSS software do something the authors never intended, Gilad likes to SCUBA  dive, read science fiction, and spend time with his wife Limor and his and two  adorable girls, Almog and Yael.</p>
]]></content:encoded>
			<wfw:commentRss>http://linuxblog.ro/building-an-embedded-linux-system-emulator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
