Category Archives: Computer Science

This category contains solutions to problems related to Computers .

How to add System Call

Today We will implement a system call in 2.6x kernel . In my case I have chosen Kernel version 2.6.39.4 for reference. This method is valid for the kernels upto 3.5x versions. For higher versions the methodology slightly differs.

  1. Download the kernel version from the link www.kernel.org.
  2. Untar the compressed file by right clicking on it and extracting it with the archive manager.
  3. A folder name as linux-<kernel version> will be created in home or in the directory specified by the user.
  4. Now starts the real journey towards implementing your first kernel call. So follow the steps closely.
  5. The user has to make changes in three files as follows:
    1. syscall_table_.32.s : path-:linux-2.6.39.4/arch/x86/kernel/syscall_table_32.s

1

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.

2                        1.  unistd_32.h or unistd_64.h:  path:- linux-2.6.39.4/arch/x86/asm/unistd_32.h | unistd_64.h

3

4

5

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-2.6.39.4/include/linux/syscalls.h

7

  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-2.6.39.4, we have created the folder systemcall(you can have any name of your choice).

9 8

Inside your folder make the following changes:

  1. 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.

9

2.Create a Makefile (which is a gedit document with bane as Makefile) which will contain the object of the c file just created.

10

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-2.6.39.4.

17

Remember to use the name of your folder . In our case the folder name is systemcall.

18

 

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.

11

13

   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.

14

Compile the above .c-file like any other c- file and see the output

15

16

The last entry shows the execution of your system call.

Enjoy!!:)

Advertisements
%d bloggers like this: