以time/gettimeofday系统调用为例分析ARM64 Linux 5.4.34
1.初始工作
1.1安装编译工具链
由于Ubuntu系统是基于x86架构的,要在上面编译arm64内核,需要安装交叉编译工具。
sudo apt-get install gcc-aarch64-linux-gnu
sudo apt-get install libncurses5-dev build-essential git bison flex libssl-dev
1.2编译busybox
wget https://busybox.net/downloads/busybox-1.36.0.tar.bz2
tar -xjf busybox-1.36.0.tar.bz2
cd busybox-1.36.0
# 引入环境变量, 基于arm64架构编译
gedit ~/.bashrc
# 将下面的环境变量加入
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
source ~/.bashrc
# 修改配置文件
make menuconfig
配置选项(选择成静态链接,不要选择动态链接)
Settings —>
[*] Build static binary (no shared libs)
# 编译安装
make -j$(nproc) && make install
1.3制作根文件系统
和lab3的过程基本一致,最终打包生成的根文件系统名为rootfs.cpio.gz。
1.4修改linux内核配置文件
cd linux-5.4.34/
make defconfig ARCH=arm64
make menuconfig ARCH=arm64
Kernel hacking --->
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
[*] Provide GDB scripts for kernel debugging
[*] Kernel debugging
# 关闭KASLR,否则会导致打断点失败
Kernel Features ---->
[] Randomize the address of the kernel image (KASLR)
1.5编译内核
# 内核编译
make -j$(nproc)
1.4启动qemu
启动qemu测试环境,通过gdb remote功能,链接QEMU并调试内核。需要注意的是,由于我们调试的是ARM64模拟环境,需要使用"gdb-multiarch"而不是ubuntu自带的gdb工具。如果没有可以通过下面命令安装:
sudo apt-get install gdb-multiarch
下载qemu4.21版本:
sudo apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev
wget https://download.qemu.org/qemu-4.2.1.tar.xz
tar xvJf qemu-4.2.1.tar.xz
cd qemu-4.2.1
./configure --target-list=x86_64-softmmu,x86_64-linux-user,arm-softmmu,arm-linux-user,aarch64-softmmu,aarch64-linux-user --enable-kvm
make
sudo make install
在上面步骤./confgure…,如果出现以下错误:
可以通过下面的命令解决:
sudo apt install -y libglib2.0-dev
出现这样的错误:
可以通过下面的命令解决:
sudo apt install -y libpixman-1-dev
1.4.2 启动内核
qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel ~/tomdogProject/linux/lab3/linux-5.4.34/arch/arm64/boot/Image -initrd ~/tomdogProject/linux/lab3/rootfs.cpio.gz -append "rdinit=/init console=ttyAMA0 loglevel=8" -nographic -s -S
2.触发系统调用
在linux-5.4.34中创建test.c文件,test.c的代码如下所示:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main()
{
time_t tt;
struct timeval tv;
struct tm *t;
#if 0
gettimeofday(&tv,NULL);
#else
asm volatile(
"add x0, x29, 16\n\t" //X0寄存器用于传递参数&tv
"mov x1, #0x0\n\t" //X1寄存器用于传递参数NULL
"mov x8, #0xa9\n\t" //使用X8传递系统调用号169
"svc #0x0\n\t" //触发系统调用
);
#endif
tt = tv.tv_sec; //tv是保存获取时间结果的结构体
t = localtime(&tt); //将世纪秒转换成对应的年月日时分秒
printf("time: %d/%d/%d %d:%d:%d\n",
t->tm_year + 1900,
t->tm_mon,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec);
return 0;
}
进行交叉编译:
aarch64-linux-gnu-gcc -o test test.c -static
将生成的test放入到rootfs根文件夹下。之后启动vscode,修改launch.json和tasks.json文件:
/*launch.json*/
{
// launch.json
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) linux",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "vm",
"program": "${workspaceRoot}/vmlinux",
"miDebuggerPath":"/usr/bin/gdb-multiarch",
"miDebuggerServerAddress": "localhost:1234",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerArgs": "-n",
"targetArchitecture": "x64",
"setupCommands": [
{
"text": "dir .",
"ignoreFailures": false
},
{
"text": "add-auto-load-safe-path ./",
"ignoreFailures": false
},
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
/*tasks.json*/
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "vm",
"type": "shell",
"command": "qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel arch/arm64/boot/Image -initrd ../rootfs_arm64.cpio.gz -append \"rdinit=/init console=ttyAMA0 loglevel=8\" -nographic -s",
"presentation": {
"echo": true,
"clear": true,
"group": "vm"
},
"isBackground": true,
"problemMatcher": [
{
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"beginsPattern": ".",
"endsPattern": ".",
}
}
]
},
{
"label": "build linux",
"type": "shell",
"command": "make",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": false,
"group": "build"
}
}
]
}
设置断点__arm64_sys_gettimeofday,启动调试,等内核启动之后在下面执行test,就可以实现gdb对gettimeofday的过程进行调试分析。
用户态程序执行svc指令,CPU会把当前程序指针寄存器PC放入ELR_EL1寄存器里,把当前状态PSTATE放入SPSR_EL1寄存器里,把异常产生的原因放在ESR_EL1寄存器里。此时CPU是知道异常类型和异常向量表的起始地址,所以可以自动计算得出该异常向量空间的入口地址,然后跳转到那里执行异常向量空间里面的指令。异常向量空间最后一条指令是b指令,对于用户态的系统调用来说会跳转到el0_sync,这样就从异常向量空间跳转同步异常处理程序的入口。
kernel_entry 0用于保存用户态寄存器信息(x0~x30/sp/pc/spsr等)到内核栈。然后根据异常类型调用其他函数。这里程序发生了系统调用(gettimeofday),因此会调用el0_svc。
参考资料:
大佬的博客