QEMU can be very useful tool in developing wide range of software from simple application to even linux kernel itself for arm based embedded system. Here is to show how developing and debugging simple application for virtual arm based platform with QEMU is actually easy to people who are lazy so hate to move his/her finger to turn on real board and for poor people who do not have any real hardware.


Note: This post assume Host system is fairly new Ubuntu machine(For my case, its ubuntu 12.04 64bit)


Below is list for bullet items which this post focus.


1. How to build simple c application with a assembly library which can be callable in c function.

2. How to run the executable in Qemu

3. Debugging it with gdb



Phase 0. TOOL Setup


Before delving into programming, we need to check all necessary tools ready. Please refer to below command to install all of them. 


First install linaro image tools.


host$ sudo apt-get install linaro-image-tools


To get qemu to work with, there are two ways to building from source code and using apt-get but here I used apt-get as like below.


host$ sudo add-apt-repository ppa:linaro-maintainers/tools
host$ sudo apt-get update
host$ sudo apt-get install qemu-user-static qemu-system

To be able to compile c and assembly, we need to install crosstool chain for arm.


host$ sudo apt-get install gcc-arm-linux-gnueabi


Phase 1. Creating a simple application made up of c and assembly combination 


I wrote some articles in this site how to create CMake project so here I would like to show only source code without detailed explanation about it and why it is needed.


There are 4 files required in the project. 

1. main.c                                          # main c source code including main()

2. square.s                                       # assembly file containing functions callable in main() 

3. CMakeLists.txt                               # main cmake project file

4. toolchain-for-qemuarm.cmake          # cmake module used for cross compilation for arm target system


So just create below all 4 files in a $(EXAMPLE) directory.


main.c


square.s


CMakeLists.txt



toolchain-for-qemuarm.cmake


Note: Here I used gcc toolchain from Freescale site but you may use other popular toolchain also.


One you have those file, it is easy to build binary which will be run on qemu.


host$ mkdir build; cd build

host$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-for-qemuarm.cmake ..


Now you can check arm executable created correctly

host$ arm-linux-gnueabi-readelf ./program -a | more

ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: ARM Version: 0x1 Entry point address: 0x8538 Start of program headers: 52 (bytes into file) Start of section headers: 3592 (bytes into file) Flags: 0x5000002, has entry point, Version5 EABI

For source codes, download this. 

cassemtest.tar.gz



Phase 2. Test with QEmu

The built program depends on external libary called ld-linux.so.3 and this is arm side shared libary. So qemu need to know where to find correct shared library for this arm friendly executable. In below, I informed /usr/arm-linux-gnueabi which was created when I installed cross-toolchain mentioned in phase 0. ( FYI, when I used one included in freescale toolchain, system got corrupted for uncertain reason)

host$ qemu-arm-static -L /usr/arm-linux-gnueabi ./program

sum(1, 100) = 101 Hello, ARM World!

The printed message above shows that program ran as intended.


Phase 3. Debugging with Qemu + gdb

With gdb, user can debug application in source level. I already gave option "SET(CMAKE_BUILD_TYPE debug)" to include debug symbol in the executable by including one line in CMakeList.txt so the executable should contain symbol table in itself.

host$ qemu-arm-static -g 1234 -L /usr/arm-linux-gnueabi ./program


With -g 1234 option in the above command, qemu is waiting for gdb connection via port 1234 of localhost.

host$ sudo apt-get install gdb-multiarch

host$ gdb-multiarch

...

(gdb) file program

Reading symbols from /home/<user>/work/qemu/linaro/examples/cassemtest/build/program...done.

(gdb) target remote localhost:1234

Remote debugging using localhost:1234 [New Remote target] warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. [Switching to Remote target] 0xf67dfc40 in ?? ()

(gdb) b main

Breakpoint 1 at 0x85f0: file /home/<user>/work/qemu/linaro/examples/cassemtest/main.c, line 8.

(gdb) c

Continuing. warning: Could not load shared library symbols for 2 libraries, e.g. /lib/libc.so.6. Use the "info sharedlibrary" command to see the complete listing. Do you need "set solib-search-path" or "set sysroot"? Breakpoint 1, main () at /home/<user>/work/qemu/linaro/examples/cassemtest/main.c:8 8 int a=1, b =100;


Conclusion


So far showed how simple application debugging with qemu can be done and hope this post will help app development for embedded system under qemu sandbox which will throw away any worry to break system due to SW bug.

   



Posted by kevino
,

In the previous article, how to run OKL4 based application with android emulator was introduced. Here this time I will introduce another virtualization series, EmbeddedXen running on top of qemu. 

Before diving deep into this virtualization world, I knew there are only two XEN implementations on ARM architecture , one is XEN-ARM, don't know what exact name for this project, maintained by samsung and the other is EmbeddedXen. When I investigated two candidates to test, I could not find the source code for XEN-ARM but for EmbeddedXen, there is an official web site and source code is also available so I picked up Embedded Xen to test the feasibility and performance of virtualized embedded system.

Testing environment

Host OS: Windows XP or above
Cross compiler to compile EmbeddedXen for ARM: android NDK toolchain version arm-eabi-gcc-4.2.1
Compiler to compile qemu: latest MinGW gcc.

Building EmbeddedXen

There is already a good wiki page explaining to build and test but in Windows machine, some parts should be fixed and I will explain these below. It should be noted that this image, at default, can be built to be runnable on a virtual mainstone board which can be setup with qemu.

  • Download source codes: Open cygwin shell and install git if you don't have git installed using cygwin setup application. 
$ git clone git://embeddedxen.git.sourceforge.net/gitroot/embeddedxen/embeddedxen

$ cd embeddedxen
$ patch -p1 < ../embeddedxen-cygwin-patch.diff

  • Compile the sources:

$ build-embeddedxen -u -0 -x

This script will generate an u-boot compatible image which can be loaded in the virtual machine known as mainstone. This generated image is flashable and has no debugging information. If you want to debug Xen, then there is a symbolic image named as vmlinux.xen containing full debugging information. Later I will show how to debug xen code with help of gdb.


Building custom qemu executable

qemu-0.12.5 version can be compiled under MinGW shell so open MinGW shell. configure may require to install zlib and optional SDL library. In this case, here is a useful link
Original qemu source seems to have a bug which cannot start boot loader code when this embeddedXen rom is loaded so a patch file for this should be applied. Now let's do it.

  • Download source and untar it.

$ cd {work_directory}
$ wget http://download.savannah.gnu.org/releases/qemu/qemu-0.12.5.tar.gz
$ tar xvfz qemu-0.12.5.tar.gz

  • Download a patch file enable to load and run the above xen image and apply it.

$ patch -p0 < qemu-xen-mingw.diff

  • Compile qemu

$ cd qemu-0.12.5
$ configure --target-list=arm-softmmu --enable-sdl
$ make



Running EmbeddedXen

In order to simulate mainstone board on qemu, two flash files should be provided and here is the command to create them

$ cd {work_directory}
$ mkdir images
dd if=/dev/zero of=./images/flash1 bs=32M count=1
dd if=/dev/zero of=./images/flash2 bs=32M count=1

Now it is time to launch qemu with embeddedXen image

$ qemu-0.12.5/arm-softmmu/qemu-system-arm -localtime -M mainstone -kernel embeddedxen/xen/arch/arm/boot/uImage.mainstone -pflash ./images/flash1 -pflash ./images/flash2 -m 256 -serial telnet::4444,server



Debugging EmbeddedXen with gdb

As you can see in the above picture, there seems to be some bug in the xen code around mapping I/O of mainstone. You want to debug it? No problem, here is the instruction.

1. Add -s and -S option in the above command line. "-s" option make gdb enable remote debugging through TCP::1234 and "-S" option make qemu wait until gdb connection is established.

$ qemu-0.12.5/arm-softmmu/qemu-system-arm -localtime -M mainstone -kernel embeddedxen/xen/arch/arm/boot/uImage.mainstone -pflash ./images/flash1 -pflash ./images/flash2 -m 256 -serial telnet::4444,server -s -S

2. Open telnet session with address 127.0.0.1:4444 and connect.

3. After telnet connection for serial console is made, open another shell windows and run gdb and set break point.

$ cd {work_directory}
$ arm-eabi-gdb embeddedxen/vmlinux.xen
(gdb) br __start_xen_arm
(gdb) target remote :1234
(gdb) c


Then after few second, gdb will be waiting for user input at the break point.

(gdb) br __start_xen_arm
Breakpoint 1 at 0xff0089bc: file xen/arch/arm/hypervisor/setup.c, line 550.
(gdb) c
The program is not being run.
(gdb) target remote :1234
Remote debugging using :1234
[New Thread 1]
0xa0000000 in ?? ()
(gdb) c
Continuing.

Breakpoint 1, __start_xen_arm () at xen/arch/arm/hypervisor/setup.c:550
550             mbi.mods_count = 1;
(gdb)



free counters
Posted by kevino
,
If you are very poor developer who cannot afford to buy any cheep ARM evaluation kit, then qemu arm version including Google android emulator is a very good tool to experiment any embedded sw stuffs.

Here some instruction to run a basic C application to print "hello" message on Android emulator are shown.
All information listed here are all taken from the following very useful link and I modified source a little bit to support Android serial console out. 


So I will explain my modifications only which comes from the different serial port configuation used to stdio between versatilepb and android goldfish.

#define GOLDFISH_TTY_PUT_CHAR (*(volatile unsigned int *)0xff002000)

/*
 * This does not append a newline
 */
static void putc(int c)
{
GOLDFISH_TTY_PUT_CHAR = c;

void print_uart0(const char *s) {
 while(*s != '\0') { /* Loop until end of string */
 putc(*s); /* Transmit char */
 s++; /* Next char */
 }
}

Full source code can be downloaded here and compiling it will generate test.bin. It can be compiled with google arm tool chain version 4.2.1 included in Android NDK and include that into current PATH list.
mkdir baremetal
cd baremetal
unzip ../baremetal.zip
./bd.sh

To test this bare metal app, 

export ANDROID_SDK_ROOT={path to android sdk}

emulator @fry -kernel ./test.bin










Posted by kevino
,
I do most jobs on Windows for convenience and it makes me to find a way to port some useful linux applications. Here I introduce how to compile okl4_android_3.0 and emulator application runnable on Windows. If you want linux version, then see original link. http://ertos.nicta.com.au/software/okl4htcdream/


Compile android_emulator-ok using MinGW ( Edit: Do not use this. This windows version seems to have problem in dynamic translation from ARM code to x86 ).

1. Download emulator source. android-emulator-ok.tar.gz
2. Unrar it to {emulator_path}.
3. Apply the patch. 
4. Before running build-emulator.sh to build emulator, an sdl package should be compiled but the sdl source included in the above tar file has a compile error with mingw so I recommend to find different way.
5. Download qemu package included android froyo version. http://android.git.kernel.org/?p=platform/external/qemu.git;a=summary
6. Untar qemu package to {qemu_path}
7. cd {qemu_path}/distrib/sdl-1.2.12
8. ./android-rebuild.sh --sdl-config=$LOCAL/bin/sdl-config
9. Now ready to compile emulator
10. cd {emulator_path}/qemu
11. ./android-rebuild.sh --sdl-config=$LOCAL/bin/sdl-config --install=$CURDIR/emulator


Compile qemu included in latest android version using MinGW.

2. build SDL first
cd /d/work/qemu/distrib/sdl-1.2.12
./android-configure --prefix=/d/work/local
make
make install

3. Download below patch and apply it.
4. Build qemu
cd /d/work/qemu
./android-configure.sh --sdl-config=/d/work/local/bin/sdl-config
make



Compile okl4-android_3.0 using cygwin

Build okl4_android-3.0 binary using cygwin. Because python lower version 2.6 is required to compile okl4 source tree without any compile error, we need to install cygwin with python version 2.5 and it could not be done in MinGW. Please correct me if I am wrong.

1. For successful compile, setup build environment.
2. Download android-ndk package
3. Install it somewhere
4. export PATH={ndk_root}/build\prebuilt\windows\arm-eabi-4.2.1\bin:$PATH
5. Download soruce : okl4-android-3.0.tar.bz2
6. cd {okl4_path}
7. tar xfvj okl4-android_3.0.tar.bz2
8. Download and apply the patch. 
9. cd okl4-android_3.0
10. tools/build.py machine=androidsim project=examples example=hello PYFREEZE=False TOOLCHAIN=gnu_arm_eabi_toolchain
11. The above command will generate image.elf file successfully but failed to generate image.boot file. I don't know about python and someone will help me what is wrong?



Running emulator with okl4 test app under MinGW

1. Create an avd file(here froyo) required to run android emulator
2. Run emulator with okl4 example
cd /d/work/
./qemu/objs/emulator.exe @cupcake -os-type okl4 -show-kernel -verbose -kernel ../images/image.boot.bin




Posted by kevino
,