1.linux user-State and kernel-state
The Intel x86 architecture has 0~3 four levels of execution, 0 highest and 3 lowest, and Linux uses only 0 and 3, respectively, to represent the kernel State and user state. In Linux, only the kernel state can access space above the logical address of 0xc0000000. When executing a program, most of the time is run in the user state, and it switches to the kernel state when it needs the operating system to help complete some work that it does not have the power and ability to do. The way the program enters the kernel state from the user state is interrupted.
The core of the system call is to use an interrupt that is particularly open to the user by the operating system, such as an int 80h interrupt for Linux, and the system interrupt is actively switched from the user state to the kernel state.
Figure from Http://blog.chinaunix.net/uid-28458801-id-3468966.html
Shows the approximate process of the program switching between the user state and the kernel state.
(1) XYZ () is an API, which is a function definition.
(2) Each system call corresponds to an encapsulation routine, and the LIBC library uses these encapsulation routines to define the user's API. (The actual program in the LIBC library for the API is the individual steps that contain the soft interrupt?) The system call is an explicit request made to the kernel by a soft interrupt, interrupted by an int 80h in the libc library, into the system call.
(3) Enter the kernel state at this time, execute the system call handler, first save
2. The specific process of user-state-to-kernel switching
int 80h
SaveAll
assembly code corresponding to the 3.C code
Getuid.c
1#include <stdio.h>2#include <stdlib.h>3#include <sys/types.h>4#include <pwd.h>5#include <stdio.h>6#include <unistd.h>7 intMain ()8 {9 uid_t uid;TenUID =getuid (); Oneprintf"User ids:uid=%d\n", UID); AExit0); -}
uid_t is defined by a macro,is a dedicated type of user ID,used to represent the user ID. The data type is unsigned long or unsigned int.
Getuid-asm.c
7 intMain ()8 {9 uid_t uid;TenAsmvolatile( One "mov $0,%%ebx\n\t"//ebx save parameter, set to null A "mov $0x18,%%eax\n\t"The//getuid system call number is 24, passed to EAX - "int $0x80\n\t"//System call - "mov%%eax,%0\n\t"//return value saved with EAX the:"=m"(UID) - ); -printf"User ids:uid=%d\n", UID); - return 0; +}
GETUID-ASM.C differs from GETUID.C in that it turns uid = Getuid () into assembly language. Pass the system call number to EAX first, make the system call, then pass the function return value to EAX. The 11th line here is the argument pass, and this function has no arguments, and this sentence can be removed.
Compiling run Results
---restore content ends---
System calls under Linux