SYSTEM CALL IMPLEMENTATION TUTORIAL AND STEPS
How to Add a System Call
(for 2.6.x Kernels and x86 Type Architecture Machines)
Assume we’d like add a system call calles “mycall” that
takes two integers as input and return their sum.
Before using this Tutorial try this Tuoorial
Linux Kernel Compilation Steps & Tutorial , How to Compile a Kernel: The Ubuntu ?
1) Add “.long sys_mycall” at the end of the list in the file
syscall_table.S.
---Full path for the file syscall_table.S is
/usr/src/linux/arch/x86/kernel/syscall_table_32.S
NOTE: You can use the command sudo gedit syscall_table.S to
edit the file syscall_table.S. Beware that if you do not have the root
privileges, system will not allow you to editany of the kernel files.
2) In file unistd_32.h
- Add #define __NR_mycall <Last_System_Call_Num+1> at
the end of the list (e.g. If the number of the last system call
Last_System_Call_Num is 319 then the line you shall add should be #define
__NR_mycall 320.). - Increment NR_syscalls by one (e.g. If before adding your
system call total number of system calls is 320, then this will be #define
NR_syscalls 321.).
---Full path for the file unistd.h is
/usr/src/linux/arch/x86/include/asm/unistd_32.h
3) Add the following line at the end of the file syscalls.h:
asmlinkage long sys_mycall(int i, int j);
---Full path for the file syscalls.h is
/usr/src/linux/arch/x86/include/asm/syscalls.h
4) Add mycall/ to core-y += in Makefile.
The line in the end shall look like:
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
mycall/
---Full path for Makefile is
/usr/src/linux/Makefile
5) Create a new directory in /usr/src/linux and name it
mycall.
6) Create a new file called mycall.c in
/usr/src/linux/mycall. Contents of the file shall be
as follows:
/*----------Start of mycall.c----------*/
#include <linux/linkage.h>
asmlinkage long sys_mycall(int i, int j) {
return(i+j);
}
/*-----------End of mycall.c-----------*/
*asmlinkage is used to look for the arguments on the kernel
stack.
7) Create Makefile in /usr/src/linux/mycall. Makefile shall
be like:
########## Start of Makefile ##########
obj-y := mycall.o
########## End of Makefile ##########
8) Create the following userspace program to test your
system call and name it testmycall.c. The
contents of this file shall be:
/*---------- Start of testmycall.c File ----------*/
#include <unistd.h>
#include <stdio.h>
#define __NR_mycall <Last_System_Call_Num+1>
long mycall(int i, int j) {
return syscall(__NR_mycall, i, j);
};
int main() {
printf(“%d\n”, mycall(10, 20));
return 0;
}
/*---------- End of testmycall.c File ----------*/
6) Create a new file called mycall.c in
/usr/src/linux/mycall. Contents of the file shall be
as follows:
/*----------Start of mycall.c----------*/
#include <linux/linkage.h>
asmlinkage long sys_mycall(int i, int j) {
return(i+j);
}
/*-----------End of mycall.c-----------*/
*asmlinkage is used to look for the arguments on the kernel
stack.
7) Create Makefile in /usr/src/linux/mycall. Makefile shall
be like:
########## Start of Makefile ##########
obj-y := mycall.o
########## End of Makefile ##########
8) Create the following userspace program to test your
system call and name it testmycall.c. The
contents of this file shall be:
/*---------- Start of testmycall.c File ----------*/
#include <unistd.h>
#include <stdio.h>
#define __NR_mycall <Last_System_Call_Num+1>
long mycall(int i, int j) {
return syscall(__NR_mycall, i, j);
};
int main() {
printf(“%d\n”, mycall(10, 20));
return 0;
}
/*---------- End of testmycall.c File ----------*/
0 comments:
Post a Comment