2014年10月11日 星期六

How to trace aarch64 Linux kernel with GDB and QEMU

架好了qemu的環境後,接下來當然就是要好好利用這個環境來認識aarch64 Linux kernel啦
這邊介紹如何使用gdb來做簡單的debug

之前是打入以下的指令來啟動qemu:
$./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-image/Image  --append "console=ttyAMA0"

接著,我們透過加入-s, -S來進行debug,-s的功能是加入 gdb debug的功能,-S是讓kernel停在第一行指令,不要往下跑。

$./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-image/Image  --append "console=ttyAMA0" -s -S

開啟另一個視窗,進到kernel tree中,設定好cross compiler toolchain(download link)的PATH,打入以下指令
$aarch64-linux-gnu-gdb vmlinux
(gdb) target remote localhost:1234
接著出現以下的字串,表示連線成功了。
Remote debugging using localhost:1234
0x0000000040000000 in ?? ()

接下來就可以開開心心(誤?)的trace aarch64 Linux kernel了。

以下是常用的gdb 指令:
列印系統registers
(gdb) info register
建立斷點
(gdb) break SYMBOL/ADDRESS
列印斷點資訊
(gdb) info break
刪除斷點1
(gdb) delete break 1
free run直接到中斷
(gdb) continue
單步執行
(gdb) stepin
列印目前pc附近的source code以及disassemly code
(gdb) disassemble /m
列印 address開始的16個value
(gdb) x/16 ADDRESS

gdb+qemu有太多可以玩的東西了,對於沒有版子但又想要學習aarch64平台的人來說,是一個很好的方式。
大家可以參考Jserv神之前的大作:快快樂樂學 GNU Debugger (gdb) 





2014年10月10日 星期五

Run an aarch64 linux on qemu

ARMv8的版子現在都超貴的,看看以下的報價就知道了。
剛剛發現最新版的qemu居然已經可以跑起aarch64了,立馬來玩玩
參考了以下兩個大大blog,終於在我的電腦上跑起來了。

http://www.bennee.com/~alex/blog/2014/05/09/running-linux-in-qemus-aarch64-system-emulation-mode/
http://suihkulokki.blogspot.tw/2014/08/testing-qemu-21-arm64-support.html

記錄一下過程。
$ sudo apt-get install qemu-system-aarch64
正在讀取套件清單... 完成
正在重建相依關係        
正在讀取狀態資料... 完成
E: 找不到套件 qemu-system-aarch64

....XD

ubuntu 12.04不支持,看來只好自已build了...

$git clone git://git.qemu.org/qemu.git qemu.git
$cd qemu.git
$./configure --target-list=aarch64-softmmuconfigure
ERROR: pixman >= 0.21.8 not present. Your options:
         (1) Preferred: Install the pixman devel package (any recent
             distro should have packages as Xorg needs pixman too).
         (2) Fetch the pixman submodule, using:
             git submodule update --init pixman
$git submodule update --init pixman
$./configure --target-list=aarch64-softmmu
$make 
$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 "console=ttyAMA0"

當然不能滿足於prebuilt 好的image
以下是自已build filesystem和kernel的方法
先從filesystem開始
$git clone git://git.buildroot.net/buildroot buildroot.git
$cd buildroot.git
$make menuconfig

* Target Options -> Target Architecture(AArch64)
* Toolchain -> Toolchain type (External toolchain)
* Toolchain -> Toolchain (Linaro AArch64 14.02)
* System configuration -> Run a getty (login prompt) after boot (BR2_TARGET_GENERIC_GETTY)
* System configuration -> getty options -> TTY Port (ttyAMA0) (BR2_TARGET_GENERIC_GETTY_PORT)
* Target Packages -> Show packages that are also provided by busybox (BR2_PACKAGE_BUSYBOX_SHOW_OTHERS)
* Filesystem images -> cpio the root filesystem (for use as an initial RAM filesystem) (BR2_TARGET_ROOTFS_CPIO)
$make
產生出來的image 在output/images/rootfs.cpio

最後是最重要的kernel
先下載tool chain:
$wget http://releases.linaro.org/14.09/components/toolchain/binaries/gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux.tar.xz
$unxz < gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux.tar.xz > gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux.tar
$tar -xvf gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux.tar
$cd gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux/bin
$export PATH=`pwd`:$PATH

再來是kernel source:
$git clone git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git 
$ARCH=arm64 make menuconfig
kernel config可以參考這裡:config
要記得改
CONFIG_INITRAMFS_SOURCE="/home/callmelei/buildroot/buildroot.git/output/images/rootfs.cpio"                                          
CONFIG_CROSS_COMPILE="aarch64-linux-gnu-"                                                                             $ARCH=arm64 make -j 8
再來就是組裝了
進到qemu的folder,打入指令
$./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-image/Image  --append "console=ttyAMA0"
當然也可以下載我prebuilt好的 image:image
一切都沒有意外的話,就開機囉

[    0.930569] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
[    0.950741] Freeing unused kernel memory: 2164K (ffffffc00060c000 - ffffffc000829000)
Starting logging: OK
Initializing random number generator... [    1.821649] random: dd urandom read with 1 bits of entropy available
done.
Starting network...

Welcome to Buildroot
buildroot login: root
# cat /proc/cpuinfo
Processor : AArch64 Processor rev 0 (aarch64)
processor : 0
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: AArch64
CPU variant : 0x1
CPU part : 0xd07
CPU revision : 0

Hardware : linux,dummy-virt

如果要退出,可以打ctrl-a c 進到qemu monitor mode來打quit,就可以離開了。