Skip to main content

Implementing a new system call in Kernel version 2.6.32

A system call is used by application or user programs to request service from the operating systems. Since the user programs does not have direct access to the kernel whereas the OS has the direct access. OS can access the hardware through system calls only.

The following files has to be modified for implementing a system call

  1. /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S
  2. /usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h
  3. /usr/src/linux-2.6.32.5/include/linux/syscalls.h
  4. /usr/src/linux-2.6.32.5/Makefile

New set of files to be created

  1. Create a new directory newcall/ inside the path “/usr/src/linux-2.6.32.5/
  2. Create new files Makefile, newcall.c and put them in the /usr/src/linux-2.6.32.5/newcall/ folder
  3. Create new user files (in any folder of Linux) to test the system call
    testnewcall.c, testnewcall.h (created in /home/pradeepkumar)

syscall_table_32.S

Find the file /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S and add the following line at the end
".long sys_newcall"  (add without double quotes, but the preceding . should)

unistd_32.h
open the file /usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h
(all the system calls will be defined in this file using #define macro)

This file contains the system call number that is passed to the kernel through the register (EAX) when a system call is invoked.

Add "#define __NR_mycall <Last_System_Call_Num + 1>" at the end of the list.
If the last system call defined here is:
"#define __NR_vmsplice   336", then add:
"#define __NR_newcall   337" at the end of the list. (337 is the new system call number)

two

Increment the "NR_syscalls" by 1. So, if NR_syscalls is defined as:
"#define NR_syscalls 337", then change it to:
"#define NR_syscalls 338"  (Since we added a new kernel, so the total number of system calls should be incremented)

one

syscalls.h
open the file /usr/src/linux-2.6.32.5/include/linux/syscalls.h
Add the following line at the end of the file:
"asmlinkage long sys_newcall(int i);"  (without double quotes)

Makefile
Full path of the file - /usr/src/linux-2.6.32.5/Makefile
Create a new directory newcall/ under the folder /usr/src/linux-2.6.32.5
and include that path to /usr/src/linux-2.6.32.5/Makefile

open the /usr/src/linux-2.6.32.5/Makefile
and find the "core-y += " and append newcall/ to the path (please see the image below)

three

newcall.c
Create a new file called newcall.c with full path: /usr/src/linux-2.6.32.5/newcall/newcall.c
/*---Start of newcall.c----*/
#include <linux/linkage.h>
asmlinkage long sys_newcall(int i)
{
return i*10; //the value passed from the user program will be multiplied by 10
}
/*---End of newcall.c------*/

Makefile
Create a new file called Makefile with full path: /usr/src/linux-2.6.32.5/newcall/Makefile
and paste the following line
obj-y := newcall.o

Create userspace files to test the system call
create two files testnewcall.c and testnewcall.h and the full path of the files are
/home/pradeepkumar/testnewcall.c
/home/pradeepkumar/testnewcall.h

testnewcall.c

#include <stdio.h>
#include "testnewcall.h"
int main(void)
{
printf("%d\n", newcall(15)); // since 15 is passed, the output should be 15*10=150
return 0;
}

testnewcall.h

#include<linux/unistd.h>
#define __NR_newcall 337

long newcall(int i)
{
return syscall(__NR_newcall,i);
}

Note: "_syscall1(long, mycall, int, i)" this can be added instead of

long newcall(int i)
{
return syscall(__NR_newcall,i);
}

Macro _syscall1()

_syscall1(long, newcall, int, i)
the importance of the above syscall is

  • The name of the system call is newcall.
  • It takes one argument.
  • The argument is an int named number.
  • It returns an long.

Testing the new system call
Step 1: Recompile and install the new kernel so that our system call becomes available to the operating system. go to the kernel folder and give command make

Step 2: Reboot the system

Step 3: Compile and execute the user space C file (testnewcall.c) that we created above. (gcc testnewcall.c and then execute ./a.out)

RESULT: You should see the output as 150. This has been tested on kernel 2.6.32.5.

Source: http://tldp.org/HOWTO/html_single/Implement-Sys-Call-Linux-2.6-i386/ (The above link uses kernel version 2.6.17 and it uses different set of files)

My post uses a recent kernel 2.6.32.5 and it modifies different set of files.

Any doubts, please query through the comments….

Comments

  1. In the "testnewcall.h"...why do u use header file "unistd.h".....as u already defined _NR_newcall 337..

    ReplyDelete
  2. The unistd header defines the syscall system call.

    ReplyDelete
  3. very nice tutorial!!its very friendly to novice programmers of linux

    more power!!

    ReplyDelete
  4. ""Makefile
    Full path of the file – /usr/src/linux-2.6.32.5/Makefile
    Create a new directory newcall/ under the folder /usr/src/linux-2.6.32.5
    and include that path to /usr/src/linux-2.6.32.5/Makefile"""
    can you please explain this part in a more simple way?i didn't understand where should i add the path..please thanks in advance

    ReplyDelete
  5. Hi there!
    after i compiled then reboot
    then compiled the testnewcall.c
    and then ./a.out
    the answer is not 150 but -1 :(
    can u please help me thanks

    ReplyDelete
  6. Hi!I finally got it right, thanks a lot to your Great Tutorial
    http://sphotos.ak.fbcdn.net/hphotos-ak-snc4/hs286.snc4/40652_1555321852978_1533158400_1460756_1013043_n.jpg

    More power to you!

    ReplyDelete
  7. Hi,

    Is _syscallN still valid for use in linux 2.35.4. I seem to be running into errors while using this.and when i do look for the code in unistd.h the unistd.h in arch/x86 doesn't have this .Please comment.

    Thanks
    Ganesh

    ReplyDelete
  8. It returns -1 for kernel 2.6.35.5? Why?

    ReplyDelete
  9. -1 means there may be some error, keep trying to resolve..

    ReplyDelete
  10. It could be because the kernel is 2.6.35 instead of 2-6-32?

    ReplyDelete
  11. gcc testnewcall.c gives me a error:
    testnewcall.h error: invalid preprocessing directive #define __NR_newcall 337... Any idea Why?... i have recompiled everything but still it gives me this error

    ReplyDelete
  12. thanks. this helps my project

    ReplyDelete
  13. hi! can we implementing the same system call through module??how can we do?

    ReplyDelete
  14. [...] http://www.pradeepkumar.org/2010/01/implementing-a-new-system-call-in-kernel-version-2-6-32.html [...]

    ReplyDelete
  15. what is the meaning of Create a new directory newcall/ under the folder /usr/src/linux-2.6.32.5 and include that path to /usr/src/linux-2.6.32.5/Makefile.Can you please explain in detail

    ReplyDelete
  16. Right on the button for 32-bit, but what if I'm amd64 and need to use unistd_64.h? syscall_table_32.S does not apply, right? Does something else need to substitute for it?

    ReplyDelete
  17. It returns -1 instead of the expected 150.can any1 help,pliz....??
    I'm using linux 2.6.32-21

    ReplyDelete
  18. it doesn't work it reply -1 just
    I do every things in detail

    ReplyDelete

Post a Comment

Popular posts from this blog

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of vanet-routing-compare.cc (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./build.py --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from nsnam.org website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q

Installing NS-3.32 in Ubuntu 20.04

This is about installing ns version 3.32 in Ubuntu 20.04 LTS. #ns3 #ns3 .32 #networksimulation The commands used in the video are given here. $] sudo apt update $] sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-qmake qt5-default gnuplot-x11 wireshark Download the ns-allinone-3.32.tar.bz2 package from nsnam.org and copy it to /home/ folder See the full video for detailed instructions Extract it either in GUI or using command $] tar jxvf ns-allinone-3.32.tar.bz2 $] cd ns-allinone-3.32/ $] ./build.py --enable-examples --enable-tests The above command will take some time to install all the packages  You can see the output as shown below ns3 To check whether ns3 installed successfully, use the following commands. $] cd ns-3.32/ $] ./waf --run hello-simulator You should get the output as Hello Simulator $] ./waf --run first This is the example from the ns-3.32/exa