Linux实验 | Adding a new system call to Linux kernel

Different kernels are different

static compilation

Add the driver to the kernel source code and configure the driver through Kconfig

There are two documents Kconfig and Makefile in the directory of the kernel source tree

  • The Kconfigs distributed to each directory constitute a distributed kernel configuration database, and each Kconfig describes the kernel configuration menu related to the source document of the directory it belongs to.
  • When the kernel configures make menuconfig (or xconfig, etc.), the menu is read from Kconfig, and the user selects it and saves it to the kernel configuration file of .config.

Execute sudo make mrproper to clear the .config and .o files.
restore the original state

syscall_32.tbl contains system calls for x86 32 bit and
syscall_64.tbl contains intel x86-64bit syscalls. add new entry into appropriate table as per x86 variant you are using .

Step 1:

sudo vim arch/x86/entry/syscalls/syscall_64.tbl
333 64 newscall sys_newscal

write picture description here

Step 2:

sudo vim include/linux/syscalls.h
asmlinkage long sys_newscall(void); 

write picture description here
Step 3:
write picture description here
write picture description here

Step 4:

recompile the kernel

Step 5:

Create a file syscall.c in user space

compile and run

#include <stdio.h>  
#include <stdlib.h>  

int newscall() {          
    int ret;          
    __asm__("movl $***, %eax");          
    __asm__("int $0x80");          
    __asm__("movl %eax, -4(%ebp)");          
    return ret;  
} 

int main(void)  {          
    long ret;          
    ret = newscall();                    
    if (ret < 0)                  
    exit(1);                            
    printf("%ld\n", ret);          
    return 0;  
}  

(To be continued...)
You can also refer to https://blog.csdn.net/qvjunping/article/details/72771848

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325846841&siteId=291194637