#创作灵感#
- 没有灵感,走个形式
- make 的时候可以开一局王者或金铲铲
- 互联网冲浪,得保护个人隐私,匿了
这次的实验跟着文档走就行,有几个地方需要更正一下的我会在文中指出
Target
- Add a new system call into the linux kernel
- Test the new system call in user model
Tools
Install GCC Software Colletion
sudo apt-get install build-essential
How to use GCC
- gcc and make
How to do
see the pdf document: newsyscall_step2.pdf
Step0
new customized kernel config: config1, config2
其实到这里都不用看,如果做了之前的实验是不需要做这些的,更重要的是这个文档我不知道在哪,懒得找了……
Step1 (Linux kernel 6.5.7)
这步应该之前实验做过了
include/linux/syscalls.h
在文件(No. 1176)
#endif /* CONFIG_ARCH_HAS_SYSCALL_WRAPPER */ 之前,添加一行:
asmlinkage long sys_schello(void);
Step2 (Linux kernel 6.5.7)
这步直接添加后面的要求,可以少make一次,但是后面的代码有坑,这里是我的代码(与文档结果不同)
记得改内核的姓名和学号啊!!!
kernel/sys.c
在文件SYSCALL_DEFINE0(gettid)函数之后(No. 958),添加如下行:
SYSCALL_DEFINE0(schello)
{
printk("Hello new system call schello! name\n");
printk("Hello new system call schello! hello id\n");
struct task_struct *p;
printk("%-20s %-6s %-6s %-6s\n","Name","p_Pid","Pid","Stat");
for_each_process(p) {
char state_str[32];
switch (p->__state) {
case TASK_RUNNING:
strcpy(state_str, "RUNNING");
break;
case TASK_INTERRUPTIBLE:
strcpy(state_str, "INTERRUPTIBLE");
break;
case TASK_UNINTERRUPTIBLE:
strcpy(state_str, "UNINTERRUPTIBLE");
break;
case TASK_STOPPED:
strcpy(state_str, "STOPPED");
break;
case TASK_TRACED:
strcpy(state_str, "TRACED");
break;
default:
strcpy(state_str, "UNKNOWN");
break;
}
printk("%-20s %-6d %-6d %-6s\n",p->comm,p->parent->pid,p->pid,state_str);
}
printk("Hello new system call schello! goodbye name\n");
return 0;
}
Step3 (Linux kernel 6.5.7)
这步应该之前实验也做过了
针对64位OS
arch/x86/entry/syscalls/syscall_64.tbl
在 334 common resq sys_rseq 行之后,添加如下行:
335 common schello sys_schello
Step4
make clean
make -j7(这个主要看你分配的核数,1.5 - 2 倍比较好,我给的 7 )
sudo make modules_install
sudo make install
Step 5
重新启动:
reboot
确认新内核是否成功运行:
uname -a
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);
print("ok! run dmesg | grep hello in terminal!\n");
return 0;
}
Step 7
编译用户态测试程序testschello.c,并执行
gcc -o testsc testschello.c
$ sudo dmesg -C
./testsc
$ dmesg ==这里不用 | 后面的,看来代码块里不能标记文本,你看,不起效果==
运行结果: