Read thin "Linux kernel design and Implementation" (3)-System call

Source: Internet
Author: User

This article is the "reading thin"linux core design and implementation "series of article III, this article mainly discusses the following questions: The concept of system calls, the implementation of system calls and procedures and how to add a system call in Linux.

0x00 system Call Concept

A system call is a set of interfaces provided by the kernel in order to interact with processes on the user space.

    • Applications access hardware and other operating system resources through this set of interfaces

    • Complete access control for hardware and resources

    • Abstraction of hardware devices (providing device independence)

0x01 System Call Introduction I common system calls
    • Fork (), exec (), open (), read (), write (), close (),......

    • More than 300 Linux systems are currently calling

II hierarchical relationship of application and system calls

Applications are programmed through APIs implemented in user space rather than directly through system tuning

Example: When invoking the printf () function, the relationship between the application, the C library, and the kernel:
The application calls the write () system call in the Write ()-C library in printf (), C library, printf ()

0X02 Linux System Call Implementation principle I related concepts
    • int 80H: Soft interrupt, notification kernel mechanism is implemented by soft interrupt, interrupt handler number 128th

    • IVT (Interrupt vector table): Interrupt vector tables, including all interrupt program entry addresses, which are fixed in memory (application in real mode)

    • IDT (Interrupt Descriptor Table): Interrupt descriptor table, non-fixed memory location, position it with IDTR Register (protected mode application, int 80H occupies one)

    • Syscall table: System call Tables

    • System call Number: In Linux, each system call is given a system call number, indicating its number in the table

II Loading of system calls

The loading of system calls made by the operating system at load time:

    • int 80H handler address loading: Trap_init () and Set_system_gate () in Start_kernel ()

    • Loading of each system call handler (ENTRY.S)

III system Call Process (take x86 as an example)

First, a soft interrupt is caught in an int 80h interrupt, prompting the system to switch to the kernel state to execute the exception handler (System call handler), after which the system obtains the system call number by reading the value of the EAX register, after which the system obtains the passed parameters by reading the registers (EBX, ECX, edx, ESI, EDI) stores the first five parameters sequentially, and if the parameter is 6 or more, points the value of one of the registers to the memory space, and finally executes the corresponding system call code to complete the system call

IV parameter Validation for system calls

System calls must be carefully checked to see if all of their parameters are valid, and if the user passes an illegal argument to the kernel, the security and stability of the system will be greatly challenged.

    • Permission Validation: callers of a system call can use the capable () function to check whether they are authorized to operate on the established resources

    • pointer legitimacy validation: before accepting a pointer to a user space, the kernel needs to verify:

      • The memory area pointed to by the pointer belongs to user space
      • The memory area pointed to by the pointer is in the address space of the process
      • If it is read, the memory should be marked as readable; if it is write, the memory should be marked writable; if it is executable, the process must not bypass the memory access limit
0x03 How to add a system call
    • Add system Call Function (/KERNEL/SYS.C)

    • Add the system call function entry to sys_call_table (ENTRY.S)

    • Add system call number

The meaning of the 0x04 system call
    • It provides users with an abstract interface to the hardware

    • Provide services to ensure system stability and security, and avoid application rampage

This article's copyright belongs to the author Luo voyage All, uses Attribution-noncommercial 3.0 License. Any person may reproduce, share, but not be used without permission for commercial purposes; reprint please specify the source. Thanks for cooperating!

Read thin "Linux kernel design and Implementation" (3)-System call

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.