深入理解系统调用

时间:2020-05-27
本文章向大家介绍深入理解系统调用,主要包括深入理解系统调用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实验内容:

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

实验环境:

VMWare虚拟机下的Ubuntu18.04.4,实验采用的内核版本为linux-5.4.34。


1 环境准备

1.1 内核编译

回退实验一的补丁操作:

cd linux-5.4.34
patch -R -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
make defconfig

修改内核编译配置重新编译:

#打开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 menuconfig
make -j$(nproc)

启动内核,此时内核无法正常运行,提示Kernel panic报错:

qemu-system-x86_64 -kernel arch/x86/boot/bzImage

根据报错提示,可以看出是缺少必要的根文件系统,导致内核无法挂载。

1.2 制作根文件系统

电脑加电启动首先由bootloader加载内核,内核紧接着需要挂载内存根文件系统,其中包含必要的设备驱动和工具。

为了简化实验环境,仅借助 BusyBox 制作极简内存根文件系统,提供基本的用户态可执行程序。

首先从https://www.busybox.net下载 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

编译安装,默认会安装到源码目录下的 _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

bootloader成功加载根文件系统到内存中后,内核会将其挂载到根目录下。
然后运行根文件系统中 init 脚本执行一些启动任务,最后才挂载真正的磁盘根文件系统。

2 系统调用

2.1 查找系统调用

在 linux-5.4.34/arch/x86/entry/syscalls/syscall_64.tbl 文件中找到相应的系统调用:

2.2 触发系统调用

getrlimit用于获得每个进程能够创建的各种系统资源的限制使用量。

在rootfs/home/目录下新建getrlimit_test.c进行测试:

#include <stdio.h>
#include <sys/resource.h>
int main()
{
        struct rlimit limit;
        int ret = getrlimit(RLIMIT_NOFILE, &limit);
        printf("ret = %d,\tcur = %ld,\tmax = %ld\n", 
        			ret, limit.rlim_cur, limit.rlim_max);
        return 0;
}

函数执行成功返回0,失败返回1。

其中,RLIMIT_NOFILE表示每个进程能打开的最多文件数。

limit.rlim_cur为当前软件限制,limit.rlim_max为最大硬件限制。

采用静态编译:

gcc -o getrlimit_test getrlimit_test.c -static

代码测试结果如下:

getrlimit测试成功后,通过编写汇编代码来触发系统调用:

#include <stdio.h>
#include <sys/resource.h>
int main()
{
        struct rlimit limit;
        int ret = -1;
		asm volatile(
			"movq %2, %%rsi\n\t"
			"movl %1, %%edi\n\t"
			"movl $0x61, %%eax\n\t"
			"syscall\n\t"
			"movq %%rax,%0\n\t"
			:"=m"(ret)
			:"a"(RLIMIT_NOFILE), "b"(&limit)
        );
        printf("ret = %d,\tcur = %ld,\tmax = %ld\n", 
        			ret, limit.rlim_cur, limit.rlim_max);
        return 0;
}

2.3 跟踪系统调用内核处理过程

重新制作根文件系统:

find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz

纯命令行启动qemu:

qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz -S -s -nographic -append "console=ttyS0"

开启新的terminal进行gdb调试:

cd linux-5.4.34
gdb vmlinux
target remote:1234
c

添加断点测试:

b __x64_sys_getrlimit

2.4 系统调用过程分析

重点阅读分析系统调用入口的保存现场、恢复现场和系统调用返回,重点关注系统调用过程中内核堆栈状态的变化

原文地址:https://www.cnblogs.com/ustca/p/12965967.html