一,学号末尾为63,63号系统调用为uname
二,配置环境:
安装qemu和其他包,下载linux源码
sudo apt install build-essential sudo apt install qemu # install QEMU sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev sudo apt install axel
axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz
xz -d linux-5.4.34.tar.xz tar -xvf linux-5.4.34.tar cd linux-5.4.34
配置内核的选项
make defconfig #Default configuration is based on 'x86_64_defconfig' make menuconfig #打开debug相关选项 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)
编译内核,qemu不能正常运行
make -j$(nproc)
# 测试一下内核能不能正常加载运行,因为没有文件系统最终会kernel panic
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
axel下载太慢了 ,我直接用浏览器下载busybox
制作根文件系统
制作内存根文件系统镜像
axel -n 20 https://busybox.net/downloads/busybox-1.31.1.tar.bz2
tar -jxvf busybox-1.31.1.tar.bz2
cd busybox-1.31.1
make menuconfig
记得要编译成静态链接,不用动态链接库。
Settings --->
[*] Build static binary (no shared libs)
然后编译安装,默认会安装到源码目录下的 _install 目录中。
make -j$(nproc) && make install
mkdir rootfs
cd rootfs cp ../busybox-1.31.1/_install/* ./ -rf mkdir dev proc sys home sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
打包内存根文件系统镜像,测试挂载根文件系统
#!/bin/sh mount -t proc none /proc mount -t sysfs none /sys echo "Wellcome MengningOS!" echo "--------------------" cd home /bin/sh chmod +x init find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz
三,gdb调试:
查看系统调用表 63为uname
改写为汇编,输出为乱码
在buf前加&
用gcc重新编译后,重新制作根文件系统
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz
启动qemu
另一个窗口运行gdb
运行包含汇编的testUname
四,分析
看到先进入syscall_define1,Linux的系统调用都改为SYSCALL_DEFINE定义的了
SYSCALL_DEFINEx里面的x代表的是系统调用参数个数
调用common.cdo_syscall_64
函数,在ax寄存器中获取到系统调用号,然后去执行系统调用内容:
再返回entry_64,为恢复现场做准备
最后的两个popq出栈指令恢复原 rdi 和 rsp的内容
系统调用主要分为以下五步,触发系统调用syscall,保存现场,中断处理,恢复现场,中断返回。