lab4:以time/gettimeofday系统调用为例分析ARM64 Linux 5.4.34

ARM64 Linux系统调用大致过程

(1)svc指令触发系统调用。
(2)el0_sync处的内核汇编代码保存异常发生时程序的执行现场(保存现场),然后根据异常发生的原因(ESR_EL1寄存器)跳转到el0_svc,el0_svc处会根据系统调用号找到对应的系统调用内核处理函数。
(3)接着执行系统调用内核处理函数sys_syz()。
(4)系统调用内核处理函数执行完成后,系统调用返回前,需要恢复异常发生时程序的执行现场(恢复现场),其中就包括主动设置ELR_EL1和SPSR_EL1的值,原因是异常会发生嵌套,一旦发生异常嵌套ELR_EL1和SPSR_EL1的值就会随之发生改变,所以当系统调用返回时,需要恢复之前保存的ELR_EL1和SPSR_EL1的值。
(5)内核调用异常返回指令eret,CPU自动把ELR_EL1写回PC,把SPSR_EL1写回PSTATE,并返回到用户态程序里,可以继续运行了

qemu安装

在调试内核时,QEMU可以模拟一个虚拟机环境,使得开发者可以在虚拟机中运行内核,并且可以使用GDB等调试工具对内核进行调试。
使用如下命令下载并解压安装指定版本的qemu:

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 
sudo 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

配置内核编译选项

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
Processor type and features ---->
    [] Randomize the address of the kernel image (KASLR)
 
# 更改后进行编译
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make Image -j$(nproc)

制作根文件系统

本次实验制作根文件系统的步骤与上一个实验基本一致,最终生成的根文件系统压缩包命名为rootfs.cpio.gz,这里不展开赘述,可以参考lab3

启动内核

qemu-system-aarch64 -m 128M -smp 1 -cpu cortex-a57 -machine virt -kernel ~/linux_lab4/linux-5.4.34/arch/arm64/boot/Image -initrd ~/linux_lab4/rootfs.cpio.gz -append "rdinit=/init console=ttyAMA0 loglevel=8" -nographic -s

如果要在 VSCode 上调试,还需要更改 .vscode 目录下的 launch.json 和 tasks.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
        }
      ]
    }
  ]
}
{
    // 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"
        }
      }
    ]
}

调试

运行test代码后成功进入调试状态,程序在断点处停下并查看堆栈情况。
查看上述调用堆栈可知,gettimeofday系统调用在内核中的执行经历了如下步骤:

首先由el0_sync根据异常发生的原因跳转到 el0_svc,然后由el0_svc 调用 el0_svc_handler、el0_svc_common 函数,将 X8 寄存器中存放的系统调用号传递给 invoke_syscall 函数。

随后执行invoke_syscall函数,将通用寄存器中的内容传入 syscall_fn(),引出系统调用内核处理函数 __arm64_sys_gettimeofday。系统调用内核处理函数执行完成后,将系统调用的返回值存放在 X0 寄存器中。

在系统调用返回前,需要恢复异常发生时程序的执行现场,包括恢复ELR_EL1和SPSR_EL1的值。因为异常会发生嵌套,一旦发生异常嵌套,ELR_EL1和SPSR_EL1的值就会随之发生改变。最后内核调用异常返回指令eret,CPU硬件把ELR_EL1写回PC,把SPSR_EL1写回PSTATE,返回用户态继续执行用户态程序。这部分操作由ret_to_user函数中的kernel_exit 0完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值