Wednesday, September 26, 2012

Adding System Call in Android bionic library

Finally after long time gap I am in my blog again. This time I will explore adding system call in bionic libc.

For simplicity sake let me tell about getpid() system call which is already present

Kernel Changes :

1.  kernel/include/linux/syscalls.h
        This header file provides syscall function prototype
eg:   asmlinkage long sys_getpid(void);
return type: long
asmlinkage macro tells the compiler to pass all function arguments on the stack.

2. kernel/kernel/timer.c
        This source file contains actuall function definition

SYSCALL_DEFINE0(getpid)
{
     // Body of getpid() function.
}

3. kernel/arch/arm/include/asm/unistd.h

The system call need to be given a number in kernel that piece of code is done here
eg: #define __NR_getpid         (__NR_SYSCALL_BASE+ 20)

4. kernel/arch/arm/kernel/calls.S

The declared system call need to be exposed in syscall_table that is done here
eg: CALL(sys_getpid)

This completes Kernel part of exposing syscall getpid()


Bionic Changes:

5. bionic/libc/SYSCALLS.TXT

This is the only place getpid need to exposed in userpace, rest all is taken care automatically by gensyscalls.py python script.
The syntax goes likes this
return_type func_name[:syscall_name][:call_id]([parameter_list])  (#syscall_number|stub)

eg: pid_t getpid()  20

gensyscalls.py script automatically creates getpid.S Stub file and placed in 2 different locations as given below
a. bionic/libc/arch-arm/syscalls.mk
     eg: syscall_src += arch-arm/syscalls/getpid.S

b. bionic/libc/arch-arm/syscalls/getpid.S


With both the changes cross-compile kernel and platform binaries so that you can enjoy the syscall interface between userspace and kernel space.

Please do comment if something is missed...


Always brighter side of the world...
-chandu