How to add a new system call?

JohnReese

New Member
Joined
Sep 20, 2023
Messages
2
Reaction score
0
Credits
27
Hello,

I am working with 32bit version of RPiOS (kernel version 5.10.103) on raspberry pi 4. I am tying to add a new system call that will first simply print hello world. On the internet i have found many guids but most of them are for x86 architecture.


Nevertheless i tried to follow the steps from

https://www.linuxbnb.net/home/adding-a- ... hitecture/

and did the following:

1.
In include/uapi/asm-generic/unistd.h i added the following:

#define __NR_process_hello 441
__SYSCALL(__NR_process_hello, sys_hello)

and modified the counter by adding one to it:

#undef __NR_syscalls
#define __NR_syscalls 442

2.
In include/linux/syscalls.h. i added the following line:

asmlinkage long sys_hello(void);


3. In kernel/sys.c i added the implementation of the sys call:

SYSCALL_DEFINE0(hello)
{
printk("Hello world\n");
return 0;
}

Still the system call is not working and returns -1, what am I doing wrong?
 


  1. System Call Number Assignment: When adding a new system call, you need to assign a unique number to it. The number you chose (441) seems high and may conflict with existing or future system calls. It's a good practice to check the existing system call numbers in arch/arm/include/uapi/asm/unistd.h and choose an unused number. However, on ARM architecture, you can use the __ARM_NR_BASE macro as a base number for your custom system calls. So, you can define your syscall like this:

    #define __NR_process_hello (__ARM_NR_BASE + 441)

  2. sys_hello vs. hello Function Names: You've defined sys_hello as the system call function in include/uapi/asm-generic/unistd.h, but in your implementation in kernel/sys.c, you've named it hello. These names should match. So, you should change the function name in sys.c to sys_hello:

    SYSCALL_DEFINE0(sys_hello)
    {
    printk("Hello world\n");
    return 0;
    }

  3. Header File Inclusion: Make sure that you've included your modified header file in the right places. Ensure that include/uapi/asm-generic/unistd.h and include/linux/syscalls.h are properly included.
 
  1. System Call Number Assignment: When adding a new system call, you need to assign a unique number to it. The number you chose (441) seems high and may conflict with existing or future system calls. It's a good practice to check the existing system call numbers in arch/arm/include/uapi/asm/unistd.h and choose an unused number. However, on ARM architecture, you can use the __ARM_NR_BASE macro as a base number for your custom system calls. So, you can define your syscall like this:

    #define __NR_process_hello (__ARM_NR_BASE + 441)

  2. sys_hello vs. hello Function Names: You've defined sys_hello as the system call function in include/uapi/asm-generic/unistd.h, but in your implementation in kernel/sys.c, you've named it hello. These names should match. So, you should change the function name in sys.c to sys_hello:

    SYSCALL_DEFINE0(sys_hello)
    {
    printk("Hello world\n");
    return 0;
    }

  3. Header File Inclusion: Make sure that you've included your modified header file in the right places. Ensure that include/uapi/asm-generic/unistd.h and include/linux/syscalls.h are properly included.

1. 441 was the first free number, that was also the initial value of __NR_syscalls. If I use this:

#define __NR_process_hello (__ARM_NR_BASE + 441)

What value should __NR_syscalls get assigned?

2. I repaired it but it is still returning -1.

3. I didn't create any new files so I assume this is not the issue?


In tutorials for x86, they are always adding the new system call to a system call table, in the files I see that ARM has also simmilair table, why the author of the ARM tutorial is not adding anythin there? What is the function of the table?
 

Members online


Latest posts

Top