Making a “Hello World” System Call
Our goal is to make a system call (syscall) that takes a user-space string buffer as its input, prepends “Hello “ to it, and then copies it to another user-space string buffer.
The prototype of the syscall is
int k22_hello(const char *input, size_t in_size, char *output, size_t out_size);
-
input: The input buffer to read the input string from -
in_size: The size of the input buffer, excluding the null-terminator -
output: The the output buffer to place the new string to -
out_size: The size of the output buffer
Start simple
Since the syscall we are creating seems to provide generic functionality, not necessarily tied to any specific core kernel subsystem, we will put its implementation just inside the kernel sub-directory, and it will look something like this
// SPDX-License-Identifier: GPL-2.0
/*
* hello system call implementation
*/
#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#define HELLO_MSG "Hello "
/**
*
* Documentation will go here.
*
*/
static int do_k22_hello(const char *input, size_t in_size, char *output,
size_t out_size)
{
pr_info("I miss Alexis Delis! But I managed to invoke a dummy syscall...\n");
return 0;
}
SYSCALL_DEFINE4(k22_hello, const char __user *, input, size_t, in_size,
char __user *, output, size_t, out_size)
{
return do_k22_hello(input, in_size, output, out_size);
}
The above is a generic template, and it is a good idea to follow it when writing syscalls. Assuming the name of the file containing the implementation of your syscall is named “hello_syscall.c” and it is properly placed inside a fresh 7.2 Linux kernel source code tree, with no additional commits, create a new branch and name it tutorial2.
$ git clone --depth=1 --branch v7.2 --single-branch --no-tags https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git linux-7.2
$ cd linux-7.2/kernel
$ git checkout -b tutorial2
Switched to a new branch 'tutorial2'
$ git log
commit 8d3ae59288f1e7d58d76558a6ee96d533bc5019f (grafted, HEAD -> tutorial2, tag: v7.2, lab1)
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Sun Aug 16 14:32:26 2026 -0700
Linux 7.2
(END)
$ git status
git status
On branch tutorial2
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello_syscall.c
nothing added to commit but untracked files present (use "git add" to track)
After adding and committing the draft implementation to your branch
commit 11e34f9c2972e17d3715c4a13c38c214796786a4 (HEAD -> tutorial2)
Author: Vaggelis Atlidakis <vaggelis@crashoverride.com>
Date: Sun Oct 11 14:14:37 2026 +0300
adding draft
commit 8d3ae59288f1e7d58d76558a6ee96d533bc5019f (grafted, tag: v7.2, lab1)
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Sun Aug 16 14:32:26 2026 -0700
Linux 7.2
Your local commit(s) should differ from v7.2 as follows
$ git diff v7.2
diff --git a/kernel/hello_syscall.c b/kernel/hello_syscall.c
new file mode 100644
index 000000000..59d585ff6
--- /dev/null
+++ b/kernel/hello_syscall.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * hello system call implementation
+ */
+
+#include <linux/kernel.h>
+#include <linux/syscalls.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+
+#define HELLO_MSG "Hello "
+
+/**
+ *
+ * Documentation will go here.
+ *
+ */
+static int do_k22_hello(const char *input, size_t in_size, char *output,
+ size_t out_size)
+{
+ pr_info("I miss Alexis Delis! But I managed to invoke a dummy syscall...\n");
+ return 0;
+}
+
+SYSCALL_DEFINE4(k22_hello, const char __user *, input, size_t, in_size,
+ char __user *, output, size_t, out_size)
+{
+ return do_k22_hello(input, in_size, output, out_size);
+}
Expose the syscall
To make our syscall system-wide usable, we need to define it with a corresponding number in the syscall table, so that the OS know to invoke it when a user programs request to trap to it. Since the way the OS traps to execute syscalls is architecture-specific functionality (see slides from Lecture 03), the required changes must be somewhere inside the arch sub-directories.
For AArch64 the changes should go under arch/arm64/tools/syscall_64.tbl, while for x86_64 CPU the respective changes should go under arch/x86/entry/syscalls/syscall_64.tbl.
$ git diff v7.2
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index 524155d65..74fdd2434 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -396,6 +396,7 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello <---
#
# Due to a historical design error, certain syscalls are numbered differently
diff --git a/kernel/hello_syscall.c b/kernel/hello_syscall.c
new file mode 100644
index 000000000..59d585ff6
--- /dev/null
+++ b/kernel/hello_syscall.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * hello system call implementation
+ */
+
+#include <linux/kernel.h>
+#include <linux/syscalls.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+
+#define HELLO_MSG "Hello "
+
+/**
+ *
+ * Documentation will go here.
+ *
+ */
+static int do_k22_hello(const char *input, size_t in_size, char *output,
+ size_t out_size)
+{
+ pr_info("I miss Alexis Delis! But I managed to invoke a dummy syscall...\n");
+ return 0;
+}
+
+SYSCALL_DEFINE4(k22_hello, const char __user *, input, size_t, in_size,
+ char __user *, output, size_t, out_size)
+{
+ return do_k22_hello(input, in_size, output, out_size);
+}
diff --git a/scripts/syscall.tbl b/scripts/syscall.tbl
index 7a42b32b6..a7795ab8b 100644
--- a/scripts/syscall.tbl
+++ b/scripts/syscall.tbl
@@ -412,3 +412,4 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello <---
Build the kernel with it
Moreover, we should let the kernel’s build system know about the source code file with the implementation of the new syscall, in order for it to be compiled and linked. Therefore, we need to update the Makefile inside the kernel sub-directory.
$ git diff Makefile
diff --git a/kernel/Makefile b/kernel/Makefile
index 1e1a31673..88fd8b344 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -10,7 +10,7 @@ obj-y = fork.o exec_domain.o exec_state.o panic.o \
extable.o params.o \
kthread.o sys_ni.o nsproxy.o nstree.o nscommon.o \
notifier.o ksysfs.o cred.o reboot.o \
- async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
+ async.o range.o smpboot.o ucount.o regset.o ksyms_common.o hello_syscall.o
obj-$(CONFIG_MULTIUSER) += groups.o
obj-$(CONFIG_VHOST_TASK) += vhost_task.o
Although so far the changes we have made are “dummy” (and there is no actual syscall implementation, just a placeholder), what we have should at least compile and link successfully to a patched kernel. The complete patch (i.e., the changes on top of the original state of the source code of the repository) should, so far, look like as follows
$ git diff refs/tags/v7.2
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index 524155d65..74fdd2434 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -396,6 +396,7 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello
#
# Due to a historical design error, certain syscalls are numbered differently
diff --git a/kernel/Makefile b/kernel/Makefile
index 1e1a31673..88fd8b344 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -10,7 +10,7 @@ obj-y = fork.o exec_domain.o exec_state.o panic.o \
extable.o params.o \
kthread.o sys_ni.o nsproxy.o nstree.o nscommon.o \
notifier.o ksysfs.o cred.o reboot.o \
- async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
+ async.o range.o smpboot.o ucount.o regset.o ksyms_common.o hello_syscall.o
obj-$(CONFIG_MULTIUSER) += groups.o
obj-$(CONFIG_VHOST_TASK) += vhost_task.o
diff --git a/kernel/hello_syscall.c b/kernel/hello_syscall.c
new file mode 100644
index 000000000..59d585ff6
--- /dev/null
+++ b/kernel/hello_syscall.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * hello system call implementation
+ */
+
+#include <linux/kernel.h>
+#include <linux/syscalls.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+
+#define HELLO_MSG "Hello "
+
+/**
+ *
+ * Documentation will go here.
+ *
+ */
+static int do_k22_hello(const char *input, size_t in_size, char *output,
+ size_t out_size)
+{
+ pr_info("I miss Alexis Delis! But I managed to invoke a dummy syscall...\n");
+ return 0;
+}
+
+SYSCALL_DEFINE4(k22_hello, const char __user *, input, size_t, in_size,
+ char __user *, output, size_t, out_size)
+{
+ return do_k22_hello(input, in_size, output, out_size);
+}
diff --git a/scripts/syscall.tbl b/scripts/syscall.tbl
index 7a42b32b6..a7795ab8b 100644
--- a/scripts/syscall.tbl
+++ b/scripts/syscall.tbl
@@ -412,3 +412,4 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello
(END)
Boot with the patched kernel
After compiling and booting to the patched kernel (see instructions from tutorial1), you must be able to successfully invoke the dummy syscall from a user-space program.
$ cat ./test1.c
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(void) {
int ret = syscall(472, (char *)0, 0, (char *)0, 0);
printf("ret: %d\n", ret);
if (ret < 0)
perror("syscall error:");
return 0;
}
$ gcc -o test1 test1.c
$ ./test1
ret: 0
$ sudo dmesg | grep -i "delis"
[ 23.308410] I miss Alexis Delis! But I managed to invoke a dummy syscall...
Implement a correct and robust syscall
If all goes well, we are ready to add the intended syscall implementation. The complete patch will look as follows
$ pwd
/home/k22/linux-7.2
$ git diff v7.2 | cat
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index 524155d65..74fdd2434 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -396,6 +396,7 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello
#
# Due to a historical design error, certain syscalls are numbered differently
diff --git a/kernel/Makefile b/kernel/Makefile
index 1e1a31673..88fd8b344 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -10,7 +10,7 @@ obj-y = fork.o exec_domain.o exec_state.o panic.o \
extable.o params.o \
kthread.o sys_ni.o nsproxy.o nstree.o nscommon.o \
notifier.o ksysfs.o cred.o reboot.o \
- async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
+ async.o range.o smpboot.o ucount.o regset.o ksyms_common.o hello_syscall.o
obj-$(CONFIG_MULTIUSER) += groups.o
obj-$(CONFIG_VHOST_TASK) += vhost_task.o
diff --git a/kernel/hello_syscall.c b/kernel/hello_syscall.c
new file mode 100644
index 000000000..516d63ca7
--- /dev/null
+++ b/kernel/hello_syscall.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * hello system call implementation
+ */
+
+#include <linux/kernel.h>
+#include <linux/syscalls.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+
+#define HELLO_MSG "Hello "
+
+/**
+ *
+ * hello: Reads a string <s> and prepends "Hello " to it
+ * -
+ * @input: The input buffer to read the input string from
+ * @in_size: The size of the input buffer, excluding the null-terminator
+ *
+ * @output: The output buffer to place the new string at
+ * @out_size: The size of the output buffer
+ *
+ * This syscall returns an error if the generated string does not fit in the
+ * output buffer, or if (in_size + 1) including the null terminator does not
+ * fit within 2 pages, or if the output buffer does not fit within 2 pages;
+ * otherwise it returns the length of the new string on success.
+ *
+ * NOTE: This function calls kmalloc with the GFP_KERNEL flag on, which means it
+ * could sleep. Therefore, the caller of this function should probably not invoke
+ * it while holding a spinlock...
+ *
+ */
+static int do_k22_hello(const char *input, size_t in_size, char *output,
+ size_t out_size)
+{
+ int return_code;
+ int buf_in_size;
+ int actual_in_size;
+ int expected_out_size;
+ char *input_kbuf;
+ char *output_kbuf;
+
+ pr_info("I miss Alexis Delis! But I managed to invoke a syscall...\n");
+
+ if (!input || !output || in_size == 0 || in_size >= 2 * PAGE_SIZE ||
+ out_size == 0) {
+ return_code = -EINVAL;
+ goto out;
+ }
+
+ /* Allocate buffer to store user-provided string */
+ buf_in_size = in_size + 1;
+ input_kbuf = kmalloc(buf_in_size, GFP_KERNEL);
+ if (!input_kbuf) {
+ return_code = -ENOMEM;
+ goto out;
+ }
+
+ input_kbuf[in_size] = '\0';
+
+ /* Safely copy user-provided string from user space to kernel space */
+ if (copy_from_user(input_kbuf, input, in_size)) {
+ return_code = -EFAULT;
+ goto free_input;
+ }
+
+ actual_in_size = strlen(input_kbuf);
+ expected_out_size = strlen(HELLO_MSG) + actual_in_size + 1;
+
+ /* Make sure output fits into the output buffer */
+ if (expected_out_size > out_size || expected_out_size > 2 * PAGE_SIZE) {
+ return_code = -EINVAL;
+ goto free_input;
+ }
+
+ /* Allocate the output buffer */
+ output_kbuf = kmalloc(expected_out_size, GFP_KERNEL);
+ if (!output_kbuf) {
+ return_code = -ENOMEM;
+ goto free_input;
+ }
+
+ /* Format desired string into the temporary output buffer */
+ snprintf(output_kbuf, expected_out_size, HELLO_MSG "%s", input_kbuf);
+
+ /* Write resulting string back to user */
+ if (copy_to_user(output, output_kbuf, expected_out_size)) {
+ return_code = -EFAULT;
+ goto free_output;
+ }
+
+ return_code = expected_out_size;
+
+free_output:
+ kfree(output_kbuf);
+
+free_input:
+ kfree(input_kbuf);
+out:
+ return return_code;
+}
+
+SYSCALL_DEFINE4(k22_hello, const char __user *, input, size_t, in_size,
+ char __user *, output, size_t, out_size)
+{
+ return do_k22_hello(input, in_size, output, out_size);
+}
diff --git a/scripts/syscall.tbl b/scripts/syscall.tbl
index 7a42b32b6..a7795ab8b 100644
--- a/scripts/syscall.tbl
+++ b/scripts/syscall.tbl
@@ -412,3 +412,4 @@
469 common file_setattr sys_file_setattr
470 common listns sys_listns
471 common rseq_slice_yield sys_rseq_slice_yield
+472 common sys_k22_hello sys_k22_hello
After booting to the patched kernel with the proper syscall implementation, executing the user-space test program should now look like this:
$ cat test1.c
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(void) {
int ret = syscall(472, (char *)0, 0, (char *)0, 0);
printf("ret: %d\n", ret);
if (ret < 0)
perror("syscall error:");
return 0;
}
$ gcc -o test1 test1.c
$ ./test1
ret: -1
syscall error:: Invalid argument
$ sudo dmesg | grep -i "delis"
[ 15.336089] I miss Alexis Delis! But I managed to invoke a syscall...
$ ./test1
ret: -1
syscall error:: Invalid argument
$ sudo dmesg | grep -i "delis"
[ 15.336089] I miss Alexis Delis! But I managed to invoke a syscall...
[ 70.618119] I miss Alexis Delis! But I managed to invoke a syscall...
$ cat test2.c
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
char foo[] = "Foo";
char bar[100];
int main(void)
{
int ret = syscall(472, foo, 3, bar, 100);
printf("ret: %d\n%s\n", ret, bar);
if (ret < 0)
perror("syscall error:");
return 0;
}
$ ./test2
ret: 10
Hello Foo
Checking your patch for linting conformance errors
Finally, we will use the “checkpatch” script to confirm that our patch is conformant to the Linux kernel coding style.
$ pwd
/home/k22/linux-7.2
$ git diff v7.2 | ./scripts/checkpatch.pl
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#27:
new file mode 100644
total: 0 errors, 1 warnings, 126 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
Your patch has style problems, please review.
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
Authors
K22 Fall ‘26 Crew!