Analysis of the principle of adding system adjustment in Linux kernel

1. Linux architecture

  The address space of the Linux system is divided into user space and kernel space, and the transfer from user space to kernel space can be completed through system calls and hardware interrupts.

Technology to share pictures

 

2. System call interface

① In general, user processes cannot access kernel space. The Linux kernel provides a set of subroutines for implementing various system functions. Users can call them to access the data and functions of the Linux kernel. These subroutines are called system call interface (SCI).

② The difference between system calls and ordinary functions: system calls are implemented by the operating system kernel and run in kernel mode; ordinary function calls are provided by the function library or the user and run in user mode.

 

3. System call classification: mainly divided into 3 categories

① Process control class

  * fork

  * clone

  * execve

  ...

② File control

  * fcntl

  * open

  * read

  ...

③ System control

  * ioctl

  * reboot

  ...

 

4. How system calls work

  The working principle of the system call is (1) the process first fills the register (R7) with appropriate values, (2) and then calls a special instruction (swi), (3) this instruction will make the user program jump to a pre-defined A position (vector_swi) in the kernel, (4) The code at this position will look up the corresponding function from the table sys_call_table according to the value of the register (R7).

(1) Appropriate value: system call number, defined in the file arch \ arm \ include \ asm \ unistd.h.

#define __NR_restart_syscall        (__NR_SYSCALL_BASE+  0)
#define __NR_exit            (__NR_SYSCALL_BASE+  1)
#define __NR_fork            (__NR_SYSCALL_BASE+  2)
#define __NR_read            (__NR_SYSCALL_BASE+  3)
#define __NR_write            (__NR_SYSCALL_BASE+  4)
#define __NR_open            (__NR_SYSCALL_BASE+  5)
#define __NR_close            (__NR_SYSCALL_BASE+  6)

(2) Special instruction
  * In X86 CPU, this instruction is realized by interrupt 0x80

  * In ARM, this instruction is SWI (Software interrupt: software interrupt instruction), now renamed to SVC

(3) Fixed position: In the ARM system, this fixed position is ENTRY (vector_swi) (arch \ arm \ kernel \ entry-common.S)

(4) Corresponding function: The kernel finds the corresponding kernel function from the system call table sys_call_table (table entries in sys_call_table are in the file: arch \ arm \ kernel \ calls.S) according to the system call number passed by the application.

 

5. Add a new system call to the Linux kernel

① Add a function in a certain position of the kernel code, such as: add to kernel / printk.c

void sys_print()
{
    printk("Hello Kevin, this is a new system call\n");
}

② Add the function to sys_call_table, for example: add to line 373 in arch \ arm \ kernel \ calls.S

CALL(sys_print)

③ Add system call number, such as: add to arch \ arm \ include \ asm \ unistd.h

#define __NR_sys_print        (__NR_SYSCALL_BASE+361)


6. New system call test

①Application layer test code method 1:

#include <sys/syscall.h>

int main()
{
    syscall(361);
    
    return 0;
}


① Method 2: Test code of application layer:

void SystemCallTest()
{
    __asm__ (
        "ldr r7, =361 \n"
        "swi \n"
        :
        :
        :"memory"
    );
}


int main()
{
    SystemCallTest();
    
    return 0;
}
Published 10 original articles · Like 11 · Visits 20,000+

Guess you like

Origin blog.csdn.net/u013323018/article/details/91448750