最近在学习 《linux Kernel Development》,本书用的linux kernel 是v2.6 版本的。看完”系统调用“一节后,想尝试添加一个系统调用,然后重编一个kernel。经过几个小时的尝试,实现了这个小功能,其中也遇到了不少坑,本文主要是记录分享下如何在Linux Kernel (V5.17.7) 中添加一个系统调用(System call)。
编kernel之前需要注意:
本文主要包含以下几点内容:
我编kernel的机器是:Ubuntu 20.04.1 LTS,内存180GB, cores: 88
sudo apt update && sudo apt upgrade -y
sudo apt install build-essential libncurses-dev libssl-dev libelf-dev bison flex -y
我这里用的vim,没有的话也需要安装:
sudo apt install vim -y
sudo apt clean && sudo apt autoremove -y
wget -P ~/ https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.17.7.tar.xz
tar -xvf ~/linux-5.17.7.tar.xz -C ~/
uname -r
5.11.0-36-generic
重新安装kernel之后,这个版本号会被修改。
cd ~/linux-5.17.7/
mkdir hello
vim hello/hello.c
添加代码。
#include
#include
SYSCALL_DEFINE0(hello)
{
printk("hello_system_call.\n");
return 0;
}
vim hello/Makefile
添加下面内容:
obj-y := hello.o
vim Makefile
搜索 core-y, 完成如下添加:
vim include/linux/syscalls.h
添加:
asmlinkage long sys_hello(void);
vim arch/x86/entry/syscalls/syscall_64.tbl
前面的步骤都很简单,这一步可能会出现各种问题,而且很耗时。
这里一路默认设置就好。
make menuconfig
nproc
make -j32
echo $? // make 结束之后记得检查一下 状态
###if output is 0 then
sudo make modules_install -j32
echo $?
sudo make install -j32
sudo update-grub
sudo reboot
uname -r
由于系统调用不像普通函数那样,需要通过sys_call 以及系统调用号才能实现系统调用。创建一个test.c
#include
#include
#include
#include
#include
#include
#define __NR_hello 451
long hello_syscall(void)
{
return syscall(__NR_hello);
}
int main(int argc, char *argv[])
{
long activity;
activity = hello_syscall();
if(activity < 0)
{
perror("Sorry, xxx. Your system call appears to have failed.");
}
else
{
printf("Congratulations, xxx! Your system call is functional. Run the command dmesg in the terminal and find out!\n");
}
return 0;
}
gcc -o test test.c
./test
dmesg // 后面也能看到系统调用打印的信息
Congratulations, xxx! Your system call is functional. Run the command dmesg in the terminal and find out!