Add Linux System Call
In this post we are going to add a new Linux system call. First off all we need to modify our current kernel so we add our system call and then compile it.
First off all lets write the system call, which is a very simple c program.
First off all lets write the system call, which is a very simple c program.
#include
<linux/linkage.h>
#include
<linux/kernel.h>
asmlinkage long
sys_hello()
{
printk(“Hello World\n”);
return 0;
}
Then we need to add this system call to our kernel.
1. -Download Linux Kernel : Version 4.5.2 www.kernel.org
Lets extract the downloaded file using sudo mv linux-4.5.2 /usr/src command.
Then we will create a new folder and name it Hello. In this folder we are going to put our hello.c file and a Makefile which is going to be like this:
In the next step we are going to open linux-4.5.2/arch/x86/entry/syscalls/syscall_32.tbl and add our call, like in the photo below:
Now lets open linux-4.5.2/ include/linux/syscalls.h and before #endif we are going to add: asmlinkage long sys_hello(void);
1. After that we are going to declare our new system call in the Makefile. Lets open linux-4.5.2/ Makefile and add: hello in core-y, which shows the system in which directory it is going to search.
Compiling The Kernel
1.Lets change the directory in terminal using cd linux-4.5.2
2. Then we are going to use make oldconfig command to compile the kernel.
3. Use command make modules and make install
4. Reboot the system
After we have rebooted the system we can choose the kernel we want to run, and we will choose our new kernel. To prove that we have added our call we are going to use the following program.
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#define nr_hello 378
int main ()
{
syscall( nr_hello);
return 0;
}
Comments
Post a Comment