深入理解系统调用

1、实验要求:

  • 找一个系统调用,系统调用号为学号最后2位相同的系统调用
  • 通过汇编指令触发该系统调用
  • 通过gdb跟踪该系统调用的内核处理过程
  • 重点阅读分析系统调用入口的保存现场、恢复现场和系统调用返回,以及重点关注系统调用过程中内核堆栈状态的变化

2.利用嵌入式汇编激活系统调用

我的学号是19225500,在64位系统中系统调用号为0的是read,先利用c代码进行read调用

读取文本文件ss.txt,其中有提前输入的文字,运行结果是:

利用嵌入式汇编代码:

利用rdi,rsi,rdx依次传递参数,特别注意输入的限制符,如果不合适就会出错,运行结果如下:

3.内核GDB

下面是一系过程,我们需要下载完整内核利用模拟器运行配置好的内核,以及自己的文件系统。

第一步:安装开发⼯具

• sudo apt install build-essential 
• sudo apt install qemu # install QEMU 
• sudo apt install libncurses5-dev bison flex libssl-dev libelf-de

第二步:下载内核源代码

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
复制代码

第四步:编译和运⾏内核

• make -j$(nproc) # nproc gives the number of CPU cores/threads available 
• # 测试⼀下内核能不能正常加载运⾏,因为没有⽂件系统终会kernel panic 
• qemu-system-x86_64 -kernel arch/x86/boot/bzImage

第五步:制作内存根⽂件系统的过程

复制代码
• 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/
• 准备init脚本⽂件放在根⽂件系统跟⽬录下(rootfs/init),添加如下内容到init⽂件。
• #!/bin/sh
• mount -t proc none /proc
• mount -t sysfs none /sys
• echo "Wellcome MengningOS!"
• echo "--------------------"
• cd home • /bin/sh
• 给init脚本添加可执⾏权限
• chmod +x init
• 打包成内存根⽂件系统镜像
• find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../ rootfs.cpio.gz 
• 测试挂载根⽂件系统,看内核启动完成后是否执⾏init脚本
• qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz

从这开始就能在qemu上运行系统了但是我的系统出现bug,在编译linux时出错,多次重装未解决问题如下,

问题困扰了我好久尚未解决,如果有哪位大佬有方麻烦回复我下,给无知的小菜鸡一点动力。

内核的跟踪调试没有完成。

原文地址:https://www.cnblogs.com/zhaopenghui/p/12975466.html