Adding a system call to Linux 3.14

There are plenty of blog posts about adding a system call to Linux 2.6, but they are out of date now. Here is a article about adding a system call to Linux 3.14. I assume you can easily add the system call for further kernel. Shane wrote a great post about it.

System call

System call is how a user space application request a service from an operating system. Many libraries, even C standard library, are an wrapper with additional functions.

Environment

Gentoo Linux(amd64): aufs-sources:3.14.5

If you are not a Gentoo user, you can get the kernel source from your package manager and The Linux Kernel.

Adding a system call

A system call is defined in the system call entry table, a system call function prototype and actual implementation for the system call.

syscall table

The syscall table is located in the arch/x86/syscalls/syscall_64.tbl.

Format

<numver> <abi> <name> <entry point>

number: All syscalls are identified by a unique number. In order to call a syscall, we tell the kernel to call the syscall by its number rather than by its name.

abi: The ABI, or application binary interface, to use. Either 64, x32, or common for both.

name: This is simply the name of the syscall.

entry point: The entry point is the name of the function to call in order to handle the syscall. The naming convention for this function is the name of the syscall prefixed with sys_. For example, the read syscall’s entry point is sys_read.

How-to

Skipping the table, you’ll find a series of x86 system calls starting with 512. I’ll add the system call in the end after table right above that part.

For 3.14, the last system call is 315 common sched_getattr sys_sched_getattr. So I add the new one as 316. It look like this:
316 common set_root sys_set_root

System call function prototype

We need to define the function prototype in the include/linux/syscalls.h file.

Simply adding a line: asmlinkage long sys_set_root(void);

We’ll add a system call takes no argument.

asmlinkage is a modifier. This macro tells the compiler that the function should not expect to find any of its arguments in registers (a common optimization), but only on the CPU’s stack. You can find more in FAQ.

System call implementation

Now, just create a new file kernel/set_root.c. Kernel style guild requires 8 space tab for indentation. But I will insist 4 space here.