Skip to content
systemcoding.dev
Kernels

Define a two-argument syscall with SYSCALL_DEFINE2

SYSCALL_DEFINE2() declares a system call with two arguments, builds its wrapper, and needs one line in the syscall table (Linux 6.12, x86-64).

1 min read
Summarize in AI

A new system call is a C function, a table entry and a number. SYSCALL_DEFINE2() is the macro for a call with two arguments: it names the call, takes each argument as a type and a name, and generates the entry point the architecture needs, including the argument handling. This is for Linux 6.12 on x86-64.

c
/* kernel/demo.c */
#include <linux/syscalls.h>

SYSCALL_DEFINE2(demo_add, int, a, int, b)
{
	return a + b;
}
Text
463	common	demo_add	sys_demo_add

The text line goes into arch/x86/entry/syscalls/syscall_64.tbl, using the next free number. On a 6.12 tree the last common call is mseal at 462, so the new entry is 463. The name in the table is sys_demo_add, which the macro creates from the first argument. Write the types and names in pairs separated by commas, and do not put a comma between a type and its name.

The macro does more than a plain function would: on x86-64 it generates a __x64_sys_demo_add wrapper that reads the arguments from the saved registers, and it makes the arguments sign-extend correctly. It also adds the call to the syscall metadata used by tracing. You must also add the prototype to include/linux/syscalls.h when the call is used elsewhere. See adding a new system call.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.