Adding a system call to linux

Adding a system call to linux 3.1.5
Here are the steps to be followed to add a system call in linux ( Thest steps have been tested with linux 3.1.5 )

Note: The steps are valid only till kernel version 3.3

1. Download the source of the kernel version to which you want to add the system call  e.g. linux-3.1.5.tar.bz2 or linux-3.1.5.tar.gz 2. Untar the source using

This should create a folder linux-3.1.5 in the present working directory 3. cd linux-3.1.5/arch/x86/kernel (This is to add a system call for x86 architecture for other architectures replace x86 by the corresponding folder name)

4. Open the file syscall_table_32.S  
  (Using vi or gedit or any suitable eidtor )
  This acts file as a table of all the system calls present in the kernel . 
To identify the number of a system call easily after every 5 system call entries the number of the call is written as a comment next to it.

5. In the 3.1.5 version there are 347 system calls by default numbering from 0 to 346. To add your system call move to the end of the file and add an entry at the end with the syntax same as previous lines. i.e. to add a system call by the name hello add the line.

Note in this case the number for this system call will be 347.

6. The next step is to inform the kernel about the system call number. This is done in the file linux-3.1.5/arch/x86/include/asm/unistd_32.h
open this file in an editor, you will notice a list of #deines of the kind#define __NR_ move to end of these #defines and add the #define for the your system call i.e.

7. Now it is the time to implement the program for the system call. The program needs to be put into the respective file int source code of the kernel, for example if it is related to schedulers put it in sched.c. All generic system calls are present in sys.c,hence we will add our implementation to sys.c . Open the file linux-3.1.5/kernel/sys.c

8. Add the code for the system call at the end of the file

SYSCALL_DEFINE0 signifies it is a system call with zero arguments. The argument passed to SYSCALL_DEFINE0 is the system call name. In this system call there is only a print statement, you can implement any other relevant kernel code.

9. Compile the kernel using the steps given in the post "Compiling a linux kernel" 10. To check the system call we need to call it from the user space. This would require the gcc to be informed about the system call and recompiling gcc too. But there is shortcut to test the system call i.e. the function syscall()

11. To test our system call create a c file test_syscall.c with the following contents

12. Now compile the code using

13. If there are no errors then executable test_syscall would have got created in the current working directory. run the same to check the output

14. ./test_syscall

15. Our system call had only a printk statement hence it would appear in the kernel logs and not on the screen. To check this run the command

16. In the output of the dmesg you should see the message   "Hello system call" i.e. the message we printed in our system call being printed in the logs Thus confirming the successful execution of our system call.


  1. The "int" before SYSCALL_DEFINE0 won't compile.

    1. Do you mean to say you are getting an error because of the "int" ?

    thry the above link it is working perfectly


Follow by Email