一、环境搭建
基本环境:
操作系统:Ubuntu 18.04
调试内核:Linux 5.4.35
1.1 qemu安装
在调试内核时,QEMU可以模拟一个虚拟机环境,使得开发者可以在虚拟机中运行内核,并且可以使用GDB等调试工具对内核进行调试。
使用如下命令下载并解压安装指定版本的qemu:
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
1.2 配置ARM64环境
使用如下命令安装gcc-aarch64-linux-gnu、libncurses5-dev、build-essential、git、bison、flex和gdb-multiarch;
gcc-aarch64-linux-gnu是交叉编译工具链,可以用于编译ARM64架构的程序;
libncurses5-dev是ncurses库的开发文件,可以用于开发基于ncurses库的程序;
build-essential是一组编译工具,包括gcc、g++、make等;git是版本控制工具;
bison和flex是用于生成解析器和词法分析器的工具;
gdb-multiarch是支持多种架构的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
1.3 配置内核编译选项
按照如下命令配置内核编译的选项;
Compile the kernel with debug info选项会在编译内核时加入调试信息;
Provide GDB scripts for kernel debugging选项会提供一些GDB脚本以便于内核调试;
Kernel debugging选项会开启内核调试功能。
而关闭KASLR选项是为了避免内核地址空间随机化导致的调试问题。
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
Processor type and features ---->
[] Randomize the address of the kernel image (KASLR)
# 更改后进行编译
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make Image -j$(nproc)
1.4 制作根文件系统
先下载BusyBox并安装,需要通过其它工具与新编译的Linux内核交互。在内核调试中,BusyBox可以提供一些常用的命令和工具,如ls、cat、echo等,方便进行调试。
# 下载并解压指定版本的busybox
wget https://busybox.net/downloads/busybox-1.36.0.tar.bz2
tar -jxvf busybox-1.36.0.tar.bz2
cd busybox-1.36.0
# 配置编译选项
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make menuconfig
Settings --->
[*] Build static binary (no shared libs)
# 开始编译
make -j$(nproc) && make install
2. 制作根文件系统并编译
mkdir rootfs
cd rootfs
cp ../busybox-1.36.0/_install/* ./ -rf
mkdir dev proc sys home
sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
3. 在/rootfs下添加如下的init脚本
挂载proc和sysfs文件系统,并输出一些信息。其中,mount命令用于挂载文件系统,-t选项指定文件系统类型,none表示不需要设备;echo命令用于输出信息;/bin/sh命令用于启动一个新的shell进程。
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Wellcome TryOS!"
echo "--------------------"
cd home
/bin/sh
加上权限并打包成镜像文件存储于rootfs同级目录下
chmod +x init
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ~/linux_lab4/rootfs.cpio.gz
4. 启动内核
qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel ~/linux_lab4/linux-5.4.34/arch/arm64/boot/Image -initrd ~/linux_lab4/rootfs.cpio.gz -append "rdinit=/init console=ttyAMA0 loglevel=8" -nographic -s
二、系统调用
2.1 使用内嵌汇编触发系统调用
创建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;
}
该代码用于获取当前系统事件并输出;代码中使用了两种方式触发系统调用:
使用库函数gettimeofday(),
使用内嵌汇编方式触发系统调用。
最后使用printf()输出获取的时间,使用上述代码能够触发系统调用,打断点进入调试状态;
将上述代码编译后生成镜像文件并在linux内核的目录下运行python脚本
aarch64-linux-gnu-gcc -o test test.c -static
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz
python3 ./scripts/gen_compile_commands.py
2.2 创建vscode项目配置文件
1. 创建c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/arch/x86/include/**",
"${workspaceFolder}/include/**",
"${workspaceFolder}/include/linux/**",
"${workspaceFolder}/arch/x86/**",
"${workspaceFolder}/**"
],
"cStandard": "c11",
"intelliSenseMode": "gcc-x64",
"compileCommands": "${workspaceFolder}/compile_commands.json"
}
],
"version": 4
}
2. 创建launch.json
{
"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
}
]
}
]
}
3. 创建settings.json
{
"search.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.DS_Store": true,
"**/drivers": true,
"**/sound": true,
"**/tools": true,
"**/arch/alpha": true,
"**/arch/arc": true,
"**/arch/c6x": true,
"**/arch/h8300": true,
"**/arch/hexagon": true,
"**/arch/ia64": true,
"**/arch/m32r": true,
"**/arch/m68k": true,
"**/arch/microblaze": true,
"**/arch/mn10300": true,
"**/arch/nds32": true,
"**/arch/nios2": true,
"**/arch/parisc": true,
"**/arch/powerpc": true,
"**/arch/s390": true,
"**/arch/sparc": true,
"**/arch/score": true,
"**/arch/sh": true,
"**/arch/um": true,
"**/arch/unicore32": true,
"**/arch/xtensa": true
},
"files.exclude": {
"**/.*.*.cmd": true,
"**/.*.d": true,
"**/.*.o": true,
"**/.*.S": true,
"**/.git": true,
"**/.svn": true,
"**/.DS_Store": true,
"**/drivers": true,
"**/sound": true,
"**/tools": true,
"**/arch/alpha": true,
"**/arch/arc": true,
"**/arch/c6x": true,
"**/arch/h8300": true,
"**/arch/hexagon": true,
"**/arch/ia64": true,
"**/arch/m32r": true,
"**/arch/m68k": true,
"**/arch/microblaze": true,
"**/arch/mn10300": true,
"**/arch/nds32": true,
"**/arch/nios2": true,
"**/arch/parisc": true,
"**/arch/powerpc": true,
"**/arch/s390": true,
"**/arch/sparc": true,
"**/arch/score": true,
"**/arch/sh": true,
"**/arch/um": true,
"**/arch/unicore32": true,
"**/arch/xtensa": true
},
"[c]": {
"editor.detectIndentation": false,
"editor.tabSize": 8,
"editor.insertSpaces": false
},
"C_Cpp.errorSquiggles": "disabled"
}
4. 创建task.json
{
"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 ~/linux_lab4/rootfs.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"
}
}
]
}
三、调试分析代码
3.1 运行test代码
运行test代码后成功进入调试状态,程序在断点处停下
3.2 分析执行过程
查看上述调用堆栈可知,gettimeofday系统调用在内核中的执行经历了如下步骤:
首先由el0_sync根据异常发生的原因跳转到 el0_svc,然后由el0_svc 调用 el0_svc_handler、el0_svc_common 函数,将 X8 寄存器中存放的系统调用号传递给 invoke_syscall 函数。
随后执行invoke_syscall函数,将通用寄存器中的内容传入 syscall_fn(),引出系统调用内核处理函数 __arm64_sys_gettimeofday。系统调用内核处理函数执行完成后,将系统调用的返回值存放在 X0 寄存器中
在系统调用返回前,需要恢复异常发生时程序的执行现场,包括恢复ELR_EL1和SPSR_EL1的值。因为异常会发生嵌套,一旦发生异常嵌套,ELR_EL1和SPSR_EL1的值就会随之发生改变。最后内核调用异常返回指令eret,CPU硬件把ELR_EL1写回PC,把SPSR_EL1写回PSTATE,返回用户态继续执行用户态程序。这部分操作由ret_to_user函数中的kernel_exit 0完成。