11 September 2014

Add a Syscall

I am using Ubuntu Server 14.04 64 bit. Get the source and build dependencies:

apt-get source linux-image-$(uname -r)
sudo apt-get build-dep linux-image-$(uname -r)

Define the Syscall

hello/hello.c

#include <linux/kernel.h>
asmlinkage long sys_hello(void)
{
  printk("Hello world\n");
  return 0;
}

hello/Makefile

obj-y := hello.o

Makefile:

core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/

arch/x86/syscalls/syscall_64.tbl

314    common    hello    sys_hello

include/linux/syscalls.h

asmlinkage long sys_hello(void);

Install the Image

make
make install
reboot

Use the Syscall

$HOME/sysHello.c

#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>

int main()
{
  long int rtn = syscall(314);
  printf("System call sys_hello return %ld\n", rtn);
}
gcc sysHello.c
./a.out

tail /var/log/syslog should show:

...
Aug 23 05:53:26 userver kernel: [   79.473407] Hello world

References:

LWN: