Add New System Call to Linux 3.6.1

Home / Add New System Call to Linux 3.6.1

Add New System Call to Linux 3.6.1

November 24, 2015 | Article | No Comments

As promised, this time I will show you how to add new syscall to linux 3.6.1.

Please note that the overall kernel structure in linux prior to 3.2 is different to the newer one so adding syscall in 3.6.1 would has different technique than technique discussed earlier. But by the way, the step for adding system call is similiar to previous article (step by step compile kernel 3.6.1)

If you haven’t download linux 3.6.1 you can download it from http://kernel.org/ website.

wget http://www.kernel.org/pub/linux/kernel/v3.x/linux-3.6.1.tar.xz

The reason I used linux-3.6.1.tar.xz instead of linux-3.6.1.tar.gz or linux-3.6.1.tar.bz2 is simply because of size.

Make sure you move the source to /usr/src/ directory. Do that using root access.

Now extract the source code.

tar -Jxf linux-3.6.1.tar.xz<br>cd linux-3.6.1

We can take the old configuration as our basis for creating configuration file.

cp /boot/config-2.6.35.22-generic .config

In this case I use kernel 2.6.35.22-generic. It can be different in your system so match the version number with your current kernel version.

It’s a wise way to see what we can do by invoking make. A little help could give us hint

make help

Now, to configure the config file, invoke

make menuconfig

After deciding what configuration you choose, It’s time for compilation. We will using generic way of compilation. It is the standard way provided by kernel and not platform oriented.

Now let’s jump into main subject. We will modify some files and do it sequentially and in the last we will test our new syscalls.

I’m going to create 3 syscalls, same as previous article: generate odd random-number, generate even random-number, and a dummy syscall which add input integer with 10.

Open and edit file /usr/src/linux-3.6.1/arch/x86/syscall/syscall_32.tbl. At end of file add 3 entries:

  • 350 i386 oddrandom sys_oddrandom
  • 351 i386 evenrandom sys_evenrandom
  • 352 i386 mycall sys_mycall

Did you notice number above? The entries are registered with unique numbers as seen above. Our 3 syscalls have id 350, 351, and 352. It’s easy to figure it out :). You can add more or less than this example, just remember what number your syscalls are.

Open and edit file /usr/src/linux-3.6.1/include/linux/syscalls.h. Add following entries before #endif:

  • asmlinkage unsigned int sys_oddrandom();
  • asmlinkage unsigned int sys_evenrandom();
  • asmlinkage int sys_mycall(int i);

Now create a new folder in /usr/src/linux-3.6.1/kernel, named randomsys/.

Create 4 files: sys_oddrandom.c; sys_evenrandom.c; sys_mycall.c; Makefile

Open and edit file “/usr/src/linux-3.6.1/kernel/randomsys/sys_oddrandom.c” with:

#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/random.h>

asmlinkage unsigned int sys_oddrandom()
{
   unsigned int v = 0;
   while( (v=get_random_int()) % 2 != 1 );
   return v;
}

Open and edit file “/usr/src/linux-3.6.1/kernel/randomsys/sys_evenrandom.c” with:

#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/random.h>

asmlinkage unsigned int sys_evenrandom()
{
   unsigned int v = 0;

   while( (v=get_random_int()) % 2 != 0 );

   return v;
}

Open and edit file “/usr/src/linux-3.6.1/kernel/randomsys/sys_mycall.c” with:

#include <linux/kernel.h>
#include <linux/syscalls.h>

asmlinkage int sys_mycall(int i) {
    return i+10;
}

Now open and edit “/usr/src/linux-3.6.1/kernel/randomsys/Makefile” with:

obj-y    := sys_oddrandom.o
obj-y    += sys_evenrandom.o
obj-y    += sys_mycall.o

Now open and edit “/usr/src/linux-3.6.1/kernel/Makefile“. Locate line with obj-y += power/. In my case it is on line 26th. Now add new entry below it.

obj-y += randomsys/

Next, compile our kernel and its modules. Invoke this command:

make

The process could be 1 to 4 hours depending on your system performance. For old machine like mine, it could be around 4 hours to do so. In this step the compiled kernel is stored in binary form as arc/x86/boot/bzImage file. Based on kernel headers it will compile the kernel modules (device driver, filesystem, network, etc) and generate .ko files which are kernel objects. These modules also called Loadable Kernel Modules (LKM).

Next, we will install kernel modules. to do so we can invoke

make modules_install

This step will copy all kernel modules (*.ko) to /lib/modules/<version>/kernel/ folder.

Next we will install the kernel itself. We did that by invoking

make install

This step copy the arch/x86/boot/bzImage to /boot folder and copy the .config to /boot/config-<latest version> and generate System.map file.

Next we need to create initramfs file. Next time I would write article about initramfs, but for this time just do so.

update-initramfs -c -k <version>

Here <version> can be 3.2.29 for this case.

Next we have to update the boot loader. I’m using GRUB so we have to invoke:

update-grub

This command automatically probe the kernels in /boot folder and add the entries in configuration file, grub.cfg

Restart the system and look at your bootloader entry. choose the new kernel in bootloader. Now open terminal and invoke

uname -r

Congratulations, you have build new kernel with our own syscalls now! Let’s test now.

Create 2 file: testmycall.h and testmycall.c. You can place it anywhere you want.

Now, open and edit testmycall.h

#include <linux/unistd.h>

unsigned int oddrandom() {
   return syscall(350);
}
unsigned int evenrandom() {
   return syscall(351);
}
unsigned int mycall(int i) {
   return syscall(352,i);
}

You must have known what those numbers are, aren’t you? 🙂

Now open and edit testmycall.c

#include <linux/unistd.h>
#include <sys/syscall.h>
#include "testmycall.h"

int main() {
   unsigned int v1=0,v2=0;
   int keluar=0;

   v1 = oddrandom();
   v2 = evenrandom();
   keluar = mycall(10);

   printf("v1         = %u\n",v1);
   printf("v2         = %u\n",v2);
   printf("keluar     = %d\n",keluar);

   return 0;
}

Compile and see the result by yourself 🙂

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial