Skip to content

Latest commit

 

History

History
 
 

labaddnewsyscall

Add a New System Call

Target

  1. Add a new system call into the linux kernel

  2. Test the new system call in user model

Tools

Install GCC Software Colletion

sudo apt-get install build-essential

How to use GCC

How to do

see the pdf document: newsyscall2020.pdf

Step0

new customized kernel config: config1, config2

Step1

include/linux/syscalls.h

在文件(No. 1381) #endif /* CONFIG_ARCH_HAS_SYSCALL_WRAPPER */之前,添加一行:

asmlinkage long sys_schello(void);

Step2

kernel/sys.c 在文件SYSCALL_DEFINE0(gettid)函数之后(No. 2734),添加如下行:

SYSCALL_DEFINE0(schello) { printk("Hello new system call schello!Your ID\n"); return 0; }

Step3

针对64位OS arch/x86/entry/syscalls/syscall_64.tbl 在文件447 common memfd_secret sys_memfd_secret 行之后,添加如下行:

448 common schello sys_schello

Step4

make clean make -j5 sudo make modules_install sudo make install

Step 5

重新启动:

reboot

确认新内核是否成功运行:

uname -a

new kernel

Step 6

编写用户态测试程序testschello.c

#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <stdio.h>
#define __NR_schello 335
int main(int argc, char *argv[])
{
 syscall(_NR_schello);
 printf("ok! run dmesg | grep hello in terminal!\n");
 return 0;
}

Step 7

编译用户态测试程序testschello.c,并执行

gcc -o testsc testschello.c

./testsc

$dmesg | grep schello

[ 1648.215250] Hello new system call schello!

testsc

End.