深入理解系统调用

一、实验要求:

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

二、实验步骤:

安装qemu:
sudo apt install qemu
安装必须的库:
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
多线程下载:

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

 

生成内核编译:
make 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)

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脚本放到根文件系统目录下

#!/bin/sh

mount -t proc none /proc

mount -t sysfs none /sys

echo "Wellcome MyOS!"

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

 

/linux-5.4.34/arch/x86/entry/syscalls/syscall_64.tbl中查到我学号后两位34对应系统调用是sys_pause

 

测试代码:

 

跟踪端点部分如图:

 

原文地址:https://www.cnblogs.com/smyhrps/p/12974468.html