3

I am new to this and just learning about the kernel, and I am trying to add a custom call to kernel 4.20.4. This is the steps that I did.

  1. First I create the file (kernel/printmsg.c) that contains the code.
#include <linux/kernel.h>
#include <linux/syscalls.h>

SYSCALL_DEFINE1(printmsg, int, i)
{
    printk(KERN_DEBUG, "TESTING %d", i);
    return 1;
}
  1. Next, I add this file to the kernel/Makefile
obj-y = fork.o exec_domain.o panic.o \
        // A few more lines
obj-y += printmsg.o // I added this line
  1. Finally, I add the system call to the syscall table on arch/x86/entry/syscalls/syscall_64.tbl(I'm building this on a 64-bit Ubuntu) by appending this line:

548 64 printmsg sys_printmsg

Now, I proceed to run make. However, it has this error:

arch/x86/entry/syscall_64.o:(.rodata+0x1120): undefined reference to `sys_printmsg'
Makefile:1034: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1

I've been scratching my head for a long time for this but I can't seem to realised what went wrong.

Hope that anyone that managed to find a problem can help out a poor soul. Thanks in advance!

CSLser
  • 193
  • 1
  • 11

1 Answers1

3

Okay, after hours of trial and error, I have finally found the problem. From linux kernel v4.17 onwards, x86_64 system calls may begin with "__x64_sys".

So, instead of using 548 64 printmsg sys_printmsg, I changed it to 548 64 printmsg __x64_sys_printmsg. Then everything works.

Hoped this helped everyone that might have this problem.

CSLser
  • 193
  • 1
  • 11
  • The code change can be found in this commit [link](https://github.com/torvalds/linux/commit/d5a00528b58cdb2c71206e18bd021e34c4eab878) – CSLser Feb 25 '19 at 06:35