Thursday, May 23, 2013

Adding Own/New System-Calls in Linux-Kernel-3.5 and above....

System-calls are kernel functions that  serve as an interface (for user mode applications ) to invoke kernel services like drivers, file-systems, Network stacks  and others. system calls are also referred as “kernel entry points” since applications can enter kernel mode only through a valid system call interface. Applications can step into system calls using special processor specific soft interrupt instructions.


while customizing and deploying Linux on to various platforms  there may be need for adding new services and system calls to kernel sources.  Fresh programmers to Kernel programming concepts  may find it more interesting to explore system call , API concepts practically by adding new system call, and implementing applications to invoke those calls . This document provides details of how to add  new system calls to Linux kernel Sources  for x86 32 & 64 bit arch.



Download kernel source code.
Extract source from compressed tar file and change our working directory into kernel source tree.

#cd arch
#cd x86

there you find syscall directory.

#cd syscalls

#ls syscalls/
2Makefile  syscall_32.tbl  syscall_64.tbl  syscallhdr.sh  syscalltbl.sh


syscall_32.tbl  contains system calls for x86 32 bit and syscall_64.tbl   contains intel x86-64bit syscalls. add new entry into appropriate table as per x86 variant you are using

vim arch/x86/syscalls/syscall_32.tbl
2349     i386    kcmp           sys_kcmp
3350     i386    lsproc         sys_lsproc

New system call is implemented to show list of processes currently load and is given a name lsproc.  we have added a new entry into syscall tableentry  begins with offset no of the table , we added a new offset 350(already table had 249 offsets) , next we mention ABI tag as i386 for 32 bit x86,  next we mentioned name of new system call (lsproc) and at last address of the system call function sys_lsproc (fucntion name resolves to its address).  system call functions in Linux kernel  are assingned names as per  sys_syscallname naming convention, this makes it easy to identify a function as system call  while browsing kernel sources.
User Mode apps will use the offset no of the system call table while invoking system call, in our case the offset application will have to use  350 for invoking lsproc syscall

 X86 64bit:

vim arch/x86/syscalls/syscall_64.tbl
2313     64      lsproc                  sys_lsproc
313 is the offset, 64  indicates  x86 64bit  ABI and lsproc is the name of the system call and sys_lsproc is the address of function
Let us now implement our system call function with the name sys_lsproc. we will first declare fucntion prototype in the linux/syscalls.h kernel header


vim include/linux/syscalls.h
2asmlinkage int sys_lsproc(void);
asmlinkage  is kernel tag  #defined with a magic  no to instruct compiler   that all arguments to the function must be accessed from kernel stack.  let us implement function code in a new source file lsproc.c . we will host our source file lsproc.c in lsproc directory under kernel branch of source tree.



# mkdir kernel/lsproc
#vim kernel/lsproc/lsproc.c

1#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/sched.h>
4#include <linux/syscalls.h>
5 
6/* system call to print process information
7 * prints processname  pid  state
8 */
9asmlinkage int sys_lsproc(void)
10{
11        struct task_struct *p;
12        pr_info("tProcesstPidtstate");
13        for_each_process(p) {
14                pr_info("%15st%ut%ld", p->comm, task_pid_nr(p), p->state);
15        }
16        return 0;
17}
we need to add  Makefile and modify kernel build script Kconfig to intergrate our changes into source tree.





root@linux-3.5.4# vim kernel/lspoc/Makefile
2obj-$(CONFIG_LSPROC) += lsproc.o



root@linux-3.5.4# vim kernel/Kconfig.lsproc
2config LSPROC
3  bool "list current processes"
4  default y
5  help
6    This will list all current running process pid and their state
This completes the process of integrating new system call into kernel source tree.  Now Its time to compile kernel sources  and create a new kernel image that includes our system call. Following make commands will compile, build and install the new kernel image.


root@linux-3.5.4# vim Makefile
2EXTRAVERSION =.syscall
3root@linux-3.5.4#make menuconfig
4root@linux-3.5.4#make modules_install
5root@linux-3.5.4#make install

 Test Application
Application can invoke the system call using x86-32bit  trap exception  with system call offset as a parameter passed into eax accumulator register.
1# vim lsproc.c
2 
3#include <stdio.h>
4#include <stdlib.h>
5 
6int lsproc()
7{
8        int ret;
9        __asm__("movl $350, %eax");
10        __asm__("int $0x80");
11        __asm__("movl %eax, -4(%ebp)");
12        return ret;
13}
14 
15int main()
16{
17        int ret;
18        printf("invoking system calln");
19        ret = lsproc();
20        if (ret < 0)
21                exit(1);
22        return 0;
23}
1# gcc lsproc.c -o lsproc
2# ./lsproc
3invoking system call
4# dmesg
Dmesg would list process list extracted and printed by our system call on console


Source:: techveda blog



1 comment:

  1. Play'n GO Casino New Zealand - Mapyro
    All rooms offer 24/7 하남 출장샵 access. 동두천 출장안마 Take your pick of different rooms from your 하남 출장샵 room. The bet365 casino's non-smoking rooms are decorated in a crisp, neutral, 상주 출장샵

    ReplyDelete