2019-2020-1 20199303 "Linux kernel principle and Analysis" in the fifth week of work

Three-tier system call mechanism

API: first layer means Libc defined API, which encapsulates the system call, using a system call int 0x80 interrupt trigger; Of course, not all of the API system calls are used, such as the completion of the addition and subtraction mathematics the API do not use a system call; it is also possible to use more than one API system calls; the presence of this layer is to provide value and easy to use API for application programmers to invoke system calls;

system_call: I Run in kernel mode. system_call all system calls the kernel entry point, wherein at the beginning of program execution to protect user mode context, at the end of the program execution to restore the user mode context, according to the intermediate system call number corresponding to the incoming interrupt service routine;

Package sys_xyz system call routines: perform a specific operation system call, the system call to complete the user's request; each corresponds to a packaged system call routine;

User mode, kernel mode interrupt processing, and

Modern general CPU has several different levels of instruction, execute at a high level code can execute a privileged instruction, access to any physical address, which corresponds to the level of the CPU executing kernel mode. Intel x86 cpu has four different execution level 0-3, Linux uses only one of the 0 and 3, respectively, to represent kernel mode (0) and user mode (level 3).
Interrupt handling is the main way to enter the kernel mode from user mode.
System call is just a special kind of interruption.

experiment procedure

Use c c code, you can return the current process ID

 #include <stdio.h>
     #include <unistd.h>
 int main(){
 pid_t pid;
 pid=getppid();
 printf("The process number is %d\n",pid);
return 0;
}

Switch assembly code as a result of

#include<stdio.h>
#include<unistd.h>

int main(){
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 process number is %d\n",pid);
return 0;
}

Execution results are as follows:

Guess you like

Origin www.cnblogs.com/besti-20199303/p/11708558.html