Professional Documents
Culture Documents
Adding New System-Calls Linux Kernel 3.5.x: Previous Next
Adding New System-Calls Linux Kernel 3.5.x: Previous Next
Adding New System-Calls Linux Kernel 3.5.x: Previous Next
Translate
Go back to main site techveda.org
search your keywords...
← Previous Next →
32
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 . (watch linux system calls video tutorial here.) This document provides
details of how to add new system calls to Linux kernel Sources (3.3 version onwords) for x86 32 & 64 bit arch.
Getting kernel sources
we will start by downloading latest stable kernel from kernel community website www.kernel.org. it is a common standard to host
source packages in /usr/src branch of rootfs (but you may copy compressed source tar file into any folder). This document assumes
/usr/src/ to be the location of source tar file .
root@techveda:/usr/src# pwd
/usr/src
root@techveda:/usr/src# ls
linux‐3.2.9 linux‐3.5.4.tar.bz2 linux‐headers‐2.6.38‐8‐generic linu
x‐source‐2.6.38
linux‐3.2.9.tar.bz2 linux‐headers‐2.6.38‐8 linux‐image‐3.2.9.debug_3.2.9.debug‐10.00.Custom_i386.deb linu
x‐source‐2.6.38.tar.bz2
root@techveda:/usr/src#
let’s Extract source from compressed tar file and change our working directory into kernel source tree.
root@techveda:/usr/src# tar xvf linux‐3.5.4.tar.bz2
root@techveda:/usr/src# cd linux‐3.5.4
root@techveda:/usr/src/linux‐3.5.4# pwd
/usr/src/linux‐3.5.4
root@techveda:/usr/src/linux‐3.5.4# ls
arch CREDITS drivers include Kbuild lib mm REPORTING‐BUGS security usr
block crypto firmware init Kconfig MAINTAINERS net samples sound virt
COPYING Documentation fs ipc kernel Makefile README scripts tools
root@techveda:/usr/src/linux‐3.5.4#
file:///C:/Users/c_bhush/Desktop/veda/How%20to%20add%20new%20system%20calls%20to%20Linux%20kernel%20%20TechvedaVeda%20Solutions.html 1/12
21/12/2015 How to add new system calls to Linux kernel TechvedaVeda Solutions
root@techveda:/usr/src/linux‐3.5.4#
root@techveda:/usr/src/linux‐3.5.4# ls arch/
alpha avr32 c6x frv hexagon Kconfig m68k mips openrisc powerpc score sparc u
m x86
arm blackfin cris h8300 ia64 m32r microblaze mn10300 parisc s390 sh tile unicor
e32 xtensa
root@techveda:/usr/src/linux‐3.5.4#
Above dump shows the contents of arch branch of kernel source. arch contains Linux hardware abstraction layer code for
various architectures that Linux kernel already includes support for, kernel maintains an architecture specific system call table which
holds system call addresses. To add an new system call we need to append a new entry into system call table and store address of
syscall function.
syscall table for x86 architecture can be found in arch/x86/syscalls branch of kernel source.
1 root@techveda:/usr/src/linux‐3.5.4# ls arch/x86/syscalls/
2 Makefile syscall_32.tbl syscall_64.tbl syscallhdr.sh syscalltbl.sh
3 root@techveda:/usr/src/linux‐3.5.4#
syscall_32.tbl contains system calls for x86 32 bit and syscall_64.tbl contains intel x8664bit syscalls. add new entry into appropriate
table as per x86 variant you are using .
1 root@techveda:/usr/src/linux‐3.5.4# vim arch/x86/syscalls/syscall_32.tbl
2 349 i386 kcmp sys_kcmp
3 350 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 table, entry 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
1 root@techveda:/usr/src/linux‐3.5.4# vim arch/x86/syscalls/syscall_64.tbl
2 313 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
1 root@techveda:/usr/src/linux‐3.5.4# vim include/linux/syscalls.h
2 asmlinkage 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.
1 root@techveda:/usr/src/linux‐3.5.4# mkdir kernel/lsproc
2 root@techveda:/usr/src/linux‐3.5.4# vim kernel/lsproc/lsproc.c
01 #include <linux/kernel.h>
02 #include <linux/init.h>
03 #include <linux/sched.h>
04 #include <linux/syscalls.h>
05
06 /* system call to print process information
07 * prints processname pid state
file:///C:/Users/c_bhush/Desktop/veda/How%20to%20add%20new%20system%20calls%20to%20Linux%20kernel%20%20TechvedaVeda%20Solutions.html 2/12
21/12/2015 How to add new system calls to Linux kernel TechvedaVeda Solutions
07 * prints processname pid state
08 */
09 asmlinkage 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.
1 root@techveda:/usr/src/linux‐3.5.4# vim kernel/lspoc/Makefile
2 obj‐$(CONFIG_LSPROC) += lsproc.o
1 root@techveda:/usr/src/linux‐3.5.4# vim kernel/Kconfig.lsproc
2 config 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.
1 root@techveda:/usr/src/linux‐3.5.4# vim Makefile
2 EXTRAVERSION =.syscall
3 root@techveda:/usr/src/linux‐3.5.4#make menuconfig
4 root@techveda:/usr/src/linux‐3.5.4#make modules_install
5 root@techveda:/usr/src/linux‐3.5.4#make install
look here for more details on kernel build
Test Application
Application can invoke the system call using x8632bit trap exception with system call offset as a parameter passed into eax
accumulator register.
01 root@techveda:~# vim lsproc.c
02
03 #include <stdio.h>
04 #include <stdlib.h>
05
06 int lsproc()
07 {
08 int ret;
09 __asm__("movl $350, %eax");
10 __asm__("int $0x80");
11 __asm__("movl %eax, ‐4(%ebp)");
12 return ret;
13 }
14
15 int 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 root@techveda:~# gcc lsproc.c ‐o lsproc
2 root@techveda:~# ./lsproc
3 invoking system call
4 root@techveda:~# dmesg
file:///C:/Users/c_bhush/Desktop/veda/How%20to%20add%20new%20system%20calls%20to%20Linux%20kernel%20%20TechvedaVeda%20Solutions.html 3/12
21/12/2015 How to add new system calls to Linux kernel TechvedaVeda Solutions
4 root@techveda:~# dmesg
Dmesg would list process list extracted and printed by our system call on console.
6 More
Like this:
Like
Be the first to like this.
Related
arungopal on June 14, 2013
how to create a root file system for arm board and how to build a kernel for for arm board and how to create a
zimage for arm board…Please post this procedure….
Current score: 0
Sandeep K Rai on April 23, 2013
The best way of build the kernel and Install in Ubuntu is: Take your kernel 3.5.x/ + version and make the changes
in their source tree accordingly this tutorial, and do folloing steps
1 make menuconfig > if any package is missed in your ubuntu then install, missed package will be mentioned
while you use this command , then install it as you do in ubuntu.
2 make bzImage >do it and open another terminal go in same path and do 3rd stage
3 make modules
when 2, 3 rae completed then do steps 4 in any terminal but in same path…
4 make modules_install
5 make install
Restart your PC and select new installed kernel to boot. then after write the user space program and run it as you
run simple c program in linux……
All the Best!!!!!!!!!
file:///C:/Users/c_bhush/Desktop/veda/How%20to%20add%20new%20system%20calls%20to%20Linux%20kernel%20%20TechvedaVeda%20Solutions.html 4/12