20135327 Guo Hao--linux Core Analysis The fourth week of the three-layer flap of the system call (top)

Source: Internet
Author: User

Linux kernel analysis three-layer skin (top) of the fourth-week clawed system call

Guo Hao Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

One, the user state, the kernel state and interrupts

User state: When the process executes the user's own code, it is said to be in user state, that is, the processor is running in the least privileged (level 3) user code.

Kernel state: When a process executes a system call and is executed in kernel code, we call the process in the kernel state, at which point the processor is executed in the highest privileged (level 0) kernel code.

PS:CPU instruction Execution level

Interrupt:

Conversion of the user state and the kernel State:

Complete process of interrupt processing

Enter interrupt program, save register data save_all-.. // kernel code, complete interrupt service, process scheduling Restore_all// exit interrupt program, recover register data  from kernel stack)// corresponds to interrupt signal or int instruction, contrary to CPU action occurring 

II. Overview of System invocation

System calls:

This is a way for the user-state process to proactively switch to the kernel state, and the user-State process is requested to complete the work using the service provided by the operating system through system calls. The core of the system call mechanism is to use an interrupt that the operating system is particularly open to the user.

Meaning of the system call:

    • Freeing users from the underlying hardware programming
    • Greatly improves the security of the system
    • Make User Programs Portable

API and System calls:

The Application Programming Interface (application program INTERFACE,API) and the system call are different

    • API is just a function definition
    • A system call sends a clear request to the kernel via a soft interrupt

Some APIs defined by the LIBC library refer to the encapsulation routines (wrapper routine, the only purpose is to publish the system call)

    • Typically each system call corresponds to an encapsulation routine
    • The library then uses these encapsulation routines to define the API for the user

Not every API corresponds to a specific system call.

    • API may directly provide user-configured services
      • For example, some mathematical functions
    • A separate API may invoke several system calls
    • Different APIs may call the same system call

return value:

    • Most encapsulation routines return an integer whose meaning depends on the corresponding system call
    • 1 in most cases the kernel does not meet the request of the process
    • The errno variable defined in LIBC contains a specific error code

Relationships between applications, encapsulation routines, system call handlers, and system invoke service routines

XYZ (): API which encapsulates a system call to Intel 0x80 triggering an interrupt

Interrupt vector 0x80 corresponding kernel code system call start System_call

Interrupt Service Program (SYS_XYZ)

System calls three layers of skins:

    • API (XYZ)
    • Interrupt Vector (System_call)
    • Interrupt Service Program (SYS_XYZ)

When the user-state process invokes a system call, the CPU switches to the kernel state and starts executing a kernel function.

    • In Linux, which executes a system call by executing an int $0x80, this assembly instruction produces a programming exception with a vector of 128
    • The sysenter directive (fast system call) was introduced in Intel Pentium II and 2.6 has been supported (this course does not consider this)

To pass the parameter:
The kernel implements a number of different system calls, and the process must indicate which system call is required, which requires passing a parameter called the system call number

    • Using the EAX register

System calls also require input and output parameters, such as

    • The actual value
    • Address of the variable in the user-state process address space
    • Even the address of a data structure that contains pointers to user-state functions

System_call is the entry point for all system calls in Linux, with at least one parameter per system call, which is the system call number passed by eax

    • An application calls the fork () encapsulation routine, the value of the EAX register is set to 2 (that is, __nr_fork) before the int $0x80 is executed.
    • The setting of this register is done in the encapsulation routines in the LIBC library, so the user generally does not care about the system call number
    • Immediately after entering Sys_call, the value of EAX is pressed into the kernel stack

• The register pass parameters have the following limitations:
1) The length of each parameter cannot exceed the length of the register, i.e. 32 bits
·2) No more than 6 parameters (EBX,ECX, edx, ESI, EDI, EBP) in addition to the system call number (EAX)

Iii. experiments: Triggering the same system call using the Library function API and the embed assembly code in C code

1. Using the Library function API to get the system current process PID value

Here I'm using the library function to get the process PID Getpid ()

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5 intMainintargcConst Char*argv[])6 {7 pid_t tt;8tt =getpid ();9printf"%u\n", TT);Ten   return 0; One}

2. Embed assembly code in C code

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5 intMainintargcConst Char*argv[])6 {7 pid_t tt;8Asmvolatile(9       "mov $0x14,%%eax\n\t"//System call number 14 placed in EAXTen       "int $0x80\n\t"//System call interrupted One       "mov%%eax,%0\n\t"//Remove the value returned in the EAX A:"=m"(TT) -             ); -printf"%u\n", TT); the     return 0; -}

Summarize:

This week's learning is more familiar with the nature of system calls and the association of System invocations and interrupts. The system call is a bridge between the user state and the kernel state, and the specific measure is the interruption.

  

20135327 Guo Hao--linux Core Analysis The fourth week of the three-layer flap of the system call (top)

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.