"Linux Kernel Analysis" (v)--linux system call execution Process __linux

Source: Internet
Author: User

Author: Sandy Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000 "
Experimental environment: C+linux64 bit (32-bit system may result in different)
In accordance with the academic integrity of the terms, I guarantee that the answer for my original, all the references to the external materials have been marked by provenance. One, the system calls the kernel source code

1, the kernel of the system call on the source code.
The course of the last week analyzed the whole process of system call, this article is mainly from the Linux kernel source level to analyze system call execution; The kernel source code used in this article can view the Linux kernel source code here; the System call dispatch table (Dispatch table) is located in the file \arch \X86\SYSCALLS\SYSCALL_32.TBL, which flags the system call number of the kernel's system call, and the kernel source of the system call is located in/linux-3.18.6/arch/x86/kernel/entry_32.s, The kernel source of the file is the main code of system call execution, which is also the focus of this paper.

2, the system calls the way in the kernel.
In the previous course of analysis of Linux kernel initiation, there was such a call in the \INIT\MAIN.C Start_kernel:

Trap_init ()

At that time, the module was analyzed primarily to initialize the system call, the position of the function:/ARCH/X86/KERNEL/TRAPS.C, which has the following code:

#ifdef config_x86_32
    set_system_trap_gate (Syscall_vector, &system_call);
    Set_bit (Syscall_vector, used_vectors);
#endif

Where Syscall_vector is the interrupt vector for the system call, while System_call is declared as a function (asmlinkage int system_call (void);), but it's actually an entry into the assembly code. The above code implements the function: once executes the command "int $0x80", will move to the System_call place, namely through the soft interrupt realizes to the kernel State transformation.
Think again about the location and important functions of System_call:

Program execution system calls can be summed up in the following steps:
1, the program calls the LIBC Library's encapsulation function.
2, call soft interrupt int 0x80 into the kernel.
3, in the kernel first executes the System_call function (the system call first number (EAX) and can
All CPU registers used are saved to the appropriate stack (completed by Save_all)),
The corresponding system call service routines are then found in the system call table according to the system call number.
4. Perform the service routine.
5, after the execution, into the Ret_from_sys_call routine, from the system call back

System_call is defined within the/LINUX-3.18.6/ARCH/X86/KERNEL/ENTRY_32.S, and then the implementation process of system calls is studied by analyzing System_call code. Two, the system calls the handler

The system call handlers defined within the ENTRY_32.S are too cumbersome, first simplifying the thousands of lines of code to the following short statement to make a whole grasp of the processing of system calls:

This is the simplification of the ENTRY_32.S code in the courseware by the Mengning teacher

The code between the beginning of the System_call and the end of the iret is the processing of the system call, and the next step is to understand the process of system invocation by analyzing the code.
First of all, the system call is also a kind of interruption, so there will be the context of the save, so the beginning of the System_all is Save_all save the scene of the command, save the need to use the register data, its definition is as follows:

The value in the register is pressed into the core stack so that the kernel can use the parameters passed by the user, and when the conversion is controlled between different privilege levels (0 and 3), the int instruction is different from the call instruction, and it does not automatically copy the parameters of the outer stack to the inner stack. So when calling a system call, you must specify the parameters to each register.

It corresponds to the command to restore the scene, restore the saved data when the system call returns, the 21st line in the above code

Restore_all:
    Restore_int_regs

It is defined as:

The code after the Save site is:

Syscall_call: Call
  *sys_call_table (,%eax,4)
  movl%eax,pt_eax (%ESP)

Where sys_call_table is the system call table,%eax is passing the system call number, which was analyzed in the course last week, so this code implements the function of calling the corresponding system call handler according to the system call number.
about how system call numbers are used:

After you have executed the code for the system call, it is the exit handling of the system call:

Syscall_exit:
    testl $_tif_allwork_mask,%cx #current->work
    jne

This is to detect whether the scheduling of processes occurs during the processing of system calls, and if process scheduling does not occur, then the system calls can be returned, and if there are process schedules, then the scheduling needs to be processed before they can be returned.
Code for line 33rd:

Work_resched: Call
  schedule
  JZ Restore_all

Schedule is a rerun of the schedule.

Line 23rd code:

Irq_return:
  Interrupu_return

Is the return of the system call, which is the end of the processing of the system call (that is, iret).

Through the above analysis, the following diagram can be used to express the process of system call processing:
third, the content of the experiment

First delete the system's menu directory, and then download a new one:

To recompile the system:

You can find more than one command: Time and Time-asm, whose function is to get the current system:

Then add the Getppid command from the previous week's course to the system call; The first step is to open the TEST.c file in the menu directory, where the main function is:

Add two lines of code to the main function:

Then add two more functions in this file:

int getppid (int argc,char *argv[])
{

    pid_t pid;
    Pid=getppid ();
    printf ("The number of parent process is:%d\n", PID);
    return 0;
}
int getppidasm (int argc,char *argv[])
{

    pid_t pid;
    ASM volatile (
        "mov $0,%%ebx\n\t"    
        "mov $0x40,%%eax\n\t"    
        "int $0x80\n\t" "    
        mov%%eax,%0\n\t"    
        : "=m" (PID)    
    ;
    printf ("The number of parent process is:%d\n", PID);
    return 0;
}

Recompile after completion, the results are as follows:

Reference Documentation:
Http://blog.chinaunix.net/uid-28458801-id-3468966.html
Https://git.oschina.net/exiahan/LinuxKernelStudy/blob/master/4/asmSCI.md

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.