实验环境:ubuntu20.04
#安装编译内核所需的依赖---一些基础库
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
make install
配置ARM64环境:
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
配置内核编译:
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,否则会导致调试的时候打断点失败
Processor type and features ---->
[] Randomize the address of the kernel image (KASLR)
编译内核:
#export交叉编译
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make Image -j$(nproc) # 这里指定target为Image,只编译kernel不会编译modules,加快编译速度
借助BusyBox 构建极简内存根文件系统,提供基本的用户态可执行程序:
#官网https://www.busybox.net下载busybox1.36.0版本
tar -jxvf busybox-1.36.0.tar.bz2 #解压
cd busybox-1.36.0 #进入目录
#export交叉编译
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
#设置编译选项
make menuconfig
按以下方式修改配置:
Settings --->
[*] Build static binary (no shared libs)
编译安装,默认会安装到源码目录下的 _install 目录中
make -j$(nproc) && make install
然后制作内存根文件系统镜像,大致过程如下:
cd ~
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/
结果如下:
接下来准备init脚本文件放在根文件系统跟目录下(rootfs/init),添加如下内容到init文件:
touch init #新建文件
vim init #编辑
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Welcome Hello OS!"
echo "--------------------"
cd home
/bin/sh
chmod +x init #对脚本添加可执行权限
#打包成内存根文件系统镜像
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ~/rootfs.cpio.gz
#测试挂载根文件系统,看内核启动完成后是否执行init脚本
cd ~
qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel linux-5.4.34/arch/arm64/boot/Image -initrd rootfs.cpio.gz -append "rdinit=/init console=ttyAMA0 loglevel=8" -nographic -s
结果如下:
在rootfs目录下创建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
#打包
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz
#运行脚本
python3 ./scripts/gen_compile_commands.py
完成后,打开vscode,在vscode中新建.vscode文件夹,并创建以下几个文件:
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
}
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
}
]
}
]
}
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"
}
tasks.json:
{
// 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"
}
}
]
}
启动程序
qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel linux-5.4.34/arch/arm64/boot/Image -initrd rootfs.cpio.gz -append "rdinit=/init console=ttyAMA0 loglevel=8" -nographic -s
添加断点,分析:
系统调用过程:
在ARM64架构下svc指令触发系统调用。系统调用的参数传递是采用X0-X5这6个寄存器,系统调用号放在X8寄存器里传递。Linux系统调用最多有6个参数,ARM64函数调用参数可以使用X0-X7这8个寄存器。
CPU首先把异常的原因(比如执行svc指令触发系统调用)放在ESR_EL1寄存器里;把当前的处理器状态(PSTATE)放入SPSR_EL1寄存器里;把当前程序指针寄存器(PC)放入ELR_EL1寄存器里,然后CPU通过异常向量表(vectors)基地址和异常的类型计算出异常处理程序的入口地址,即VBAR_EL1寄存器加上偏移量取得异常处理的入口地址,将 X8 寄存器(regs->regs[8])中存放的系统调用号传递给 invoke_syscall 函数,接着开始执行异常处理入口的第一行代码。
接着执行 invoke_syscall 函数,将通用寄存器中的内容传入 syscall_fn(),引出系统调用内核处理函数 __arm64_sys_gettimeofday
从系统调用返回前会处理一些工作(work_pending),比如处理信号、判断是否需要进程调度等,ret_to_user的最后是kernel_exit 0负责恢复现场,与保存现场kernel_entry 0相对应,kernel_exit 0的最后会执行eret指令系统调用返回。eret指令所做的工作与svc指令相对应,eret指令会将ELR_EL1寄存器里值恢复到程序指针寄存器PC中,把SPSR_EL1寄存器里的值恢复到PSTATE处理器状态中,同时会从内核态转换到用户态,在用户态堆栈栈顶指针sp代表的是sp_el0寄存器。
参考:https://blog.csdn.net/qq_43495782/article/details/129852702