在 FreeBSD 10.1 中添加新的系统调用

Add new system call at FreeBSD 10.1

我想在 FreeBSD 上添加新的系统调用。我的系统调用代码是:

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/sysproto.h>

int Sum(int a, int b);

int
Sum(a,b)
{
   int c;
   c = a + b;
   return (0);
}

但是当我重建内核时,出现错误:

怎么了?你能帮帮我吗?

非常感谢。

请阅读this

我认为,您没有在内核 makefile 中包含带有 sys_Sum 函数的文件(请注意,在您提供的代码中,函数名称是 Sum 并且错误有对 sys_Sum 的调用。我希望这只是您代码中的错字,函数名称是 sys_Sum).

下面是我如何使用我的示例系统调用 setkey 完成的,它采用两个无符号整数。 我将我的系统调用添加到最后 /kern/syscalls.master

546 AUE_NULL    STD { int setkey(unsigned int k0, unsigned int k1);}

然后我做了

cd /usr/src
sudo make -C /sys/kern/ sysent

接下来,我将文件添加到 /sys/conf/files

kern/sys_setkey.c       standard

我的sys_setkey.c如下

#include <sys/sysproto.h>
#include <sys/proc.h>

//required for printf
#include <sys/types.h>
#include <sys/systm.h>

#ifndef _SYS_SYSPROTO_H_
struct setkey_args {
    unsigned int k0;
    unsigned int k1;
};
#endif
/* ARGSUSED */
int sys_setkey(struct thread *td, struct setkey_args *args)
{
    printf("Hello, Kernel!\n");
    return 0;
}

此外,我将系统调用添加到 /kern/capabilities.conf

##
## Allow associating SHA1 key with user
##
setkey

最后,在 /usr/src/ 我 运行 命令

sudo make -j8 kernel
sudo reboot

这是一个运行系统调用的程序

#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
int main(){
//syscall takes syscall.master offset,and the system call arguments
printf("out = %d\n",syscall(546,1,1));
return 0;
}