Simple example of adding a system call interface to the Linux kernel

Source: Internet
Author: User

1. Linux Architecture

The address space of the Linux system is divided into user space and kernel space, which can be transferred from user space to kernel space through system call and hardware interrupt.

2. System Call Interface

① Generally, user processes cannot access kernel space. The Linux kernel provides a set of subroutines for implementing various system functions that users can call to access the data and functions of the Linux kernel, which are called system call Interfaces (SCI).

② the difference between a system call and a normal function: The system call is implemented by the operating system kernel, and the normal function call is provided by the function library or the user itself, running in the user state.

3. System Call Classification: 3 major categories

① Process Control class

* Fork

* Clone

* EXECVE

...

② file Control class

* FCNTL

* Open

* Read

...

③ System Control class

* IOCTL

* Reboot

...

4. How system calls Work

System invocation works by (1) The process first fills the register with the appropriate value (R7), (2) then calls a special instruction (Swi), (3) This instruction will allow the user program to jump to a predefined kernel in a position (VECTOR_SWI), (4) The code in this location looks for the corresponding function from table sys_call_table based on the value of the Register (R7).

(1) Appropriate value: The 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 instructions
* In X86 CPU, this instruction is implemented by interrupt 0x80

* In arm, this instruction is SWI (software interrupt: Soft interrupt instruction), now renamed to Svc

(3) Fixed position: In arm system, this fixed position is entry (VECTOR_SWI) (Arch\arm\kernel\entry-common. S

(4) The corresponding function: The kernel is passed the system call number according to the application, from the system call Table sys_call_table (sys_call_table table key is set to the file: Arch\arm\kernel\calls. S) to find the appropriate kernel function.

5. Add a new system call to the Linux kernel

① Add a function to a location in the kernel code, such as: Add to KERNEL/PRINTK.C

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

② add functions to sys_call_table, such as: to Arch\arm\kernel\calls. s in 373 rows added

Call (Sys_print)

③ Add the 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 ;}


① Application Layer Test code Method 2:

void systemcalltest () {    __asm__ (        "ldr R7, =361 \ n"        "SWI \ n  ":::"memory"    );}          int Main () {    systemcalltest ();         return 0 ;}


Simple example of adding a system call interface to the Linux kernel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.