# Add system call



## mansoda (Dec 24, 2010)

Hi,
I am a student studying FreeBSD Kernel. I want to ask how to add a system call to FreeBSD. Is there any detailed tutorial to follow?
Thanks indeed!

Juson


----------



## DutchDaemon (Dec 24, 2010)

Start here: http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook


----------



## Zare (Dec 24, 2010)

The easiest (and AFAIK, preferred) way is to code a loadable kernel module which contains syscall code.
Search the google and find examples of skeletal, "hello world" style kernel module code, and Makefile example you'll use to compile it, kld skeleton is about 15 lines of code and Makefile is like two-three. Simple to begin with.



After you have a skeleton, make a variable definition that will hold your syscall number (don't use one that's already occupied, check /usr/src/sys/kern/syscalls.master), for instance


```
static int my_syscall_slot = 210;
```

Then define function that'll hold syscall's usable code


```
static int syscall_code(int something, char something_else, void *syscall_args)
{
   ...
}
```

_syscall_args_ is mandatory, the first two are your optional arguments. Since this example uses two arguments for syscall, you need to define _sysent_ for your syscall with 2.


```
static struct sysent my_syscall_sysent = { 2, syscall_code };
```

Then use _SYSCALL_MODULE_ macro that'll declare the whole thing :


```
SYSCALL_MODULE(my_syscall, &my_syscall_slot, &my_syscall_sysent, put_the_name_of_the_module_entry_function_here, NULL);
```

So the args are : name (provisional), pointer to slot number, pointer to sysent, name of the kld entry function, optional callback argument for event handler.

Hope it helps, this is not tested and from the head. Last time i did a grounds up KLD was in days of FreeBSD 5.4, i might have missed something.


----------



## vdvluc (Dec 29, 2010)

You may want to look in /usr/share/examples/kld/syscall/ for example code.


----------

