1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Add system call

Discussion in 'FreeBSD Development' started by mansoda, Dec 24, 2010.

  1. mansoda

    mansoda New Member

    Messages:
    9
    Likes Received:
    0
    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
     
  2. DutchDaemon

    DutchDaemon Administrator Staff Member Administrator Moderator

    Messages:
    10,661
    Likes Received:
    22
  3. Zare

    Zare New Member

    Messages:
    386
    Likes Received:
    0
    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

    Code:
    static int my_syscall_slot = 210;
    
    Then define function that'll hold syscall's usable code

    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.

    Code:
    static struct sysent my_syscall_sysent = { 2, syscall_code };
    
    Then use SYSCALL_MODULE macro that'll declare the whole thing :

    Code:
    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.
     
  4. vdvluc

    vdvluc New Member

    Messages:
    23
    Likes Received:
    0
    You may want to look in /usr/share/examples/kld/syscall/ for example code.