How to add System Call
Today We will implement a system call in 2.6x kernel . In my case I have chosen Kernel version 126.96.36.199 for reference. This method is valid for the kernels upto 3.5x versions. For higher versions the methodology slightly differs.
- Download the kernel version from the link www.kernel.org.
- Untar the compressed file by right clicking on it and extracting it with the archive manager.
- A folder name as linux-<kernel version> will be created in home or in the directory specified by the user.
- Now starts the real journey towards implementing your first kernel call. So follow the steps closely.
- The user has to make changes in three files as follows:
- syscall_table_.32.s : path-:linux-188.8.131.52/arch/x86/kernel/syscall_table_32.s
This file contains the list of interrupts add an entry of your system call here. In our case we are naming our system call as mycall . Note: It is important to write sys_mycall while entering your systemcall.
It is advisable to make the changes in both unistd_32.h as well as unistd_64.h, reader must notice that the id of the systemcall in both the cases is different its is so because depending on the system specifications these tables will be accessed accordingly as 32-bit systems will refer unistd_32.h and 64-bit systems will refer unistd_64.h.
In case of unistd_32.h increment the number of total syscalls by 1. In our case we have implemented it to 346. Also the unique id of your systemcall will be actually used by OS to recognize and identify your systemcall.
c. syscalls.h: path:- linux-184.108.40.206/include/linux/syscalls.h
This is where the prototype of your system call is given. If you want to pass any parameter in your systemcall then in place of void you can give the data type. In our case we are not passing any parameter so we have declared void.
d. After making the above changes next step is to create a new folder inside your kernel directory .In our case kernel is linux-220.127.116.11, we have created the folder systemcall(you can have any name of your choice).
Inside your folder make the following changes:
- Create a .c file and in this file write a c code to define the work of your systemcall. In our case we are making a simple hello world program that will print hello world on the console.
2.Create a Makefile (which is a gedit document with bane as Makefile) which will contain the object of the c file just created.
In our case the name of the c file is sys_mycall so the object is sys_mycall.o.
Now in the Make file of the folder linux-18.104.22.168.
Remember to use the name of your folder . In our case the folder name is systemcall.
e.) After the above changes open the terminal and write the following commands
- make– This command will create all the modules, links objects and will create the .config file of your kernel in the grub. This command in the end creates bzImage of your kernel.
- makemodules_install– This command will install all the modules required for the booting of your kernel.
- make install– This command creates initrd file.
So this completes the compilation of your kernel reboot your system and select your kernel in the grub menu.
If in case you cant see the grub menu then open the grub.config file from terminal and increase the timeout value in that file from 0 to 10. Again reboot and press shift, thus you will see your compiled kernel in boot menu. Now to see the output of your system call type the following code and execute it.
Compile the above .c-file like any other c- file and see the output
The last entry shows the execution of your system call.