Add system call

Kernel development, writing drivers, coding, and questions regarding FreeBSD internals.

Add system call

Postby mansoda » 24 Dec 2010, 06:39

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!

Junior Member
Posts: 9
Joined: 24 Dec 2010, 06:37

Postby DutchDaemon » 24 Dec 2010, 16:32

User avatar
Old Fart
Posts: 10467
Joined: 16 Nov 2008, 20:17
Location: The Netherlands

Postby Zare » 24 Dec 2010, 18:50

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 [FILE]/usr/src/sys/kern/syscalls.master[/FILE]), for instance

Code: Select all
static int my_syscall_slot = 210;

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

Code: Select all
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.

Code: Select all
static struct sysent my_syscall_sysent = { 2, syscall_code };

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

Code: Select all
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.
Posts: 385
Joined: 16 Nov 2008, 23:05
Location: Split, Dalmatia

Postby vdvluc » 29 Dec 2010, 02:22

You may want to look in [file]/usr/share/examples/kld/syscall/[/file] for example code.
Junior Member
Posts: 23
Joined: 28 May 2009, 20:44
Location: Netherlands

Return to FreeBSD Development

Who is online

Users browsing this forum: No registered users and 1 guest