环境配置
由于Ubuntu系统采用x86架构,我们需要在Ubuntu虚拟机下安装交叉编译器和GDB。
sudo apt-get install gcc-aarch64-linux-gnu
sudo apt-get install libncurses5-dev build-essential git bison flex libssl-dev
sudo apt install gdb-multiarch
下载并解压busybox以建立简易的根文件系统:
wget https://busybox.net/downloads/busybox-1.33.1.tar.bz
bzip2 -d FileName.bz2
tar -xvf busybox-1.33.1.tar
cd busybox-1.33.1
设置静态编译选项:
make menuconfig ARCH=arm64
# Settings --->
# [*] Build static binary (no shared libs)
然后编译busybox:
make
make install
在编译完成的_install文件夹下进行一些配置。
cd _install
mkdir etc dev lib
cd etc
新建文件profile:设置环境变量和shell参数
新建文件inittab:设置busybox执行的sysinit
新建文件fstsb:设置文件系统挂载信息
新建文件init.d/rcS:查找并创建字符设备与块设
回到_install目录,在dev文件夹下新建console使得用户态的输出打印到串口上
cd ../dev
sudo mknod console c 5 1
将支持动态编译的相关程序拷贝到lib文件夹下:
将_install文件夹整体放入linux编译后的文件夹中;在对linux源码编译前需要设置arm64编译选项
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
Kernel Features ---->
[] Randomize the address of the kernel image
然后开始编译,将花费较久的时间。
安装qemu,这里需要较高的版本才能启动arm64架构的内核,以下采用4.2.1版本
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 libpython-dev python-pip python-capstone virtualenv
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
可以用以下命令查看是否安装成功:
/usr/local/bin/qemu-system-aarch64 --version
接下来可以用以下命令启动内核
/usr/local/bin/qemu-system-aarch64 -m 512M -smp 4 -cpu cortex-a57 -machine virt -kernel arch/arm64/boot/Image -append "rdinit=/linuxrc nokaslr console=ttyAMA0 loglevel=8" -nographic -s
系统调用分析
在以下的标准库中定义了gettimeofday的原型,它将返回当前时间的很多信息,其中timeval中包含秒与微秒,timezone包含时区等,它相比time系统调用是更精确的。
#include <sys/time.h>
int gettimeofday(struct timeval* tv, struct timezone* tz);
对该原型函数简化的示范代码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;
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;
}
ARM64架构下,time/gettimeofday系统调用由同步异常svc指令触发,用户态的EL0权限的应用程序通过调用gettimeofday触发系统调用,相关参数分别进入X0-X5寄存器(示范代码中只有X0-X1),X8寄存器则用于存放系统调用号,然后通过svc指令进入EL1级内核态,svc后的立即数参数没有被Linux内核使用,而是把系统调用号放到了X8寄存器里传递。
以下由CPU自动完成:把异常原因(svc系统调用)放在ESR_EL1寄存器里;把当前的处理器状态(PSTATE)放入SPSR_EL1寄存器里;把当前程序指针寄存器(PC)放入ELR_EL1寄存器里,然后CPU通过异常向量表(vectors)基地址和异常的类型计算出异常处理程序的入口地址,即VBAR_EL1寄存器加上偏移量取得异常处理的入口地址,即可执行异常处理程序的第一行代码。
以下是对保存现场的指令的分析:
.macro kernel_entry, el, regsize = 64
...
; 保存通用寄存器值
stp x0, x1, [sp, #16 * 0]
stp x2, x3, [sp, #16 * 1]
...
stp x26, x27, [sp, #16 * 13]
stp x28, x29, [sp, #16 * 14]
...
; 异常发生时CPU的状态sp、pc和pstate分别保存在SP_EL0、ELR_EL1和SPSR_EL1寄存器中,mrs是状态寄存器到通用寄存器的传送指令。
mrs x21, sp_el0
mrs x22, elr_el1
mrs x23, spsr_el1
stp lr, x21, [sp, #S_LR] ; lr = x30
stp x22, x23, [sp, #S_PC] ; 返回地址和以上三个参数也会被压栈
...
.endm
确定异常处理程序入口:在ARM64 Linux中最常见的原因是svc指令触发了系统调用,所以排在最前面的就是条件判断跳转到el0_svc,el0_svc中主要负责调用C代码的el0_svc_handler处理系统调用和ret_to_user系统调用返回。
el0_svc:
gic_prio_kentry_setup tmp=x1
mov x0, sp
bl el0_svc_handler
b ret_to_user
ENDPROC(el0_svc)
系统调用返回前的工作:处理信号、判断是否需要进程调度等。
work_pending:
mov x0, sp // 'regs'
bl do_notify_resume
#ifdef CONFIG_TRACE_IRQFLAGS
bl trace_hardirqs_on // enabled while in userspace
#endif
ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step
b finish_ret_to_user
系统调用返回:目的是恢复现场,与保存现场kernel_entry 0相对应,kernel_exit 0的最后会执行eret指令系统调用返回。eret指令所做的工作与svc指令相对应,eret指令会将ELR_EL1寄存器里值恢复到程序指针寄存器PC中,把SPSR_EL1寄存器里的值恢复到PSTATE处理器状态中,同时会从内核态转换到用户态,在用户态堆栈栈顶指针sp代表的是sp_el0寄存器。
ret_to_user:
disable_daif
gic_prio_kentry_setup tmp=x3
ldr x1, [tsk, #TSK_TI_FLAGS]
and x2, x1, #_TIF_WORK_MASK
cbnz x2, work_pending
finish_ret_to_user:
enable_step_tsk x1, x2
#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
bl stackleak_erase
#endif
kernel_exit 0
ENDPROC(ret_to_user)
恢复现场:kernel_exit 0负责恢复现场的代码和kernel_entry 0负责保存现场的代码相对应
.macro kernel_exit, el
...
msr sp_el0, x23
msr elr_el1, x21 // set up the return data
msr spsr_el1, x22
ldp x0, x1, [sp, #16 * 0]
ldp x2, x3, [sp, #16 * 1]
ldp x4, x5, [sp, #16 * 2]
ldp x6, x7, [sp, #16 * 3]
...
ldp x24, x25, [sp, #16 * 12]
ldp x26, x27, [sp, #16 * 13]
ldp x28, x29, [sp, #16 * 14]
ldr lr, [sp, #S_LR]
...
eret
我们试着调试test.c这一示范代码,将test.c建立在linux源码的root文件夹下,进行交叉编译并执行:
sudo vim test.c
aarch64-linux-gnu-gcc -o test test.c -static
./test
我们可以在VSCode上进行相关调试。需要配置launch.json和tasks.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-arm.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"
}
}
]
}
点击debug按钮,即可正常进行调试: