Linux system calls (syscall) principle


LinuxArchitecture

Kernel space and user space are two different states of program execution, through system calls and hardware interrupts to complete the transfer from user space to kernel space. As shown below:

Architecture FIG linux


From that figure, Linux kernel space and user space

Under normal circumstances, the user process can not access the kernel. It can not access kernel memory space is located, the kernel function can not be called. Linux kernel is provided a set of subroutines for implementing various functions of the system, the user can call to access their data and linux kernel function, which system call interface (SCI) called the system call.


The difference between system calls and general functions:

Common system calls and function calls are very similar, only difference is that, by the system calloperating systemKernel implementation, running in kernel mode; and a normal function call provided by the library or the users themselves, running in user mode.


Number of system calls:

In the 2.6.32 version of the kernel, the system calls a total of 365, they can be found in arch / arm / include / asm / unistd.h in.

/* This file contains the system call numbers*/

#define __NR_restart_syscall (__NR_SYSCALL_BASE+ 0)
#define __NR_exit (__NR_SYSCALL_BASE+ 1)
#define __NR_fork (__NR_SYSCALL_BASE+ 2)
......

#define __NR_preadv(__NR_SYSCALL_BASE+361)
#define __NR_pwritev (__NR_SYSCALL_BASE+362)
#define __NR_rt_tgsigqueueinfo (__NR_SYSCALL_BASE+363)
#define __NR_perf_event_open (__NR_SYSCALL_BASE+364)


System call features:

Mainly divided into three categories:

(1) Process Control category

fork to create a child process

clone create a child process in accordance with the specified conditions

execve run the executable file

...

(2) File Control Operations

fcntl document control

Open the file open

read file read

...

(3) System Control

ioctl I / O control functions Total

reboot restart

-sysctl read and write system parameters

...


The use of system calls, for example:

By the time the following function system call from Greenwich time at 0:00 on January 1970 the 1st to now the number of seconds.

#include<time.h>
main()
{
time_t t_time;
t_time = time ((time_t *) 0); / * time to call the system call * /
printf("The time is %ld\n",t_time);
}


System call works:

Under normal circumstances, the user process can not access the kernel. It can not access kernel memory space is located, the kernel function can not be called. system
System call is an exception. The principle is that (1) is filled with the appropriate process to the value of the register, (2) then calls a special instruction, (3) the user program instruction will jump to a pre-defined position a good kernel. (4)
The process can jump to a fixed core position. This process checks the system call number, this number tells the kernel what kind of service request process. Then, it looks at the system call table (sys_call_table) to find the kernel function entry address called. Then, you call the function, such as after return, do some system checks, and finally returned to the process.


Works Overview:

(1) appropriate value

We all appropriate value can be found in include / asm / unistd.h in, in this file for each system call provides a unique number, called the system call number.

#define __NR_utimensat(__NR_SYSCALL_BASE+348)
#define __NR_signalfd (__NR_SYSCALL_BASE+349)
#define __NR_timerfd_create (__NR_SYSCALL_BASE+350)
#define __NR_eventfd (__NR_SYSCALL_BASE+351)
#define __NR_fallocate (__NR_SYSCALL_BASE+352)

Each macro is there a system call number

(2) special instruction

In the Intel CPU, this directive is implemented by an interrupt 0x80

In ARM, this instruction is SWI (softwhere interrupt: soft interrupt instruction), now renamed SVC

(3) a fixed position

Fixed location of each CPU is not the same, the ARM architecture in the fixed core position is ENTRY (vector_swi) (in arch \ sh \ kernel \ entry-common.S), that is, PC pointer will jump to this location

(4) the corresponding function

The kernel system call number is transmitted to the application, find the corresponding table from a system call kernel function sys_call_table

CALL(sys_restart_syscall)

CALL(sys_exit)

CALL(sys_fork_wrapper)


Example:

Principle (application): The following is a call from the user to find the open system call kernel specific function entry address process substantially

#define __syscall(name) "swi\t" __NR_##name "\n\t“
int open( const char * pathname, int flags)
{
。。。。。。
__syscall(open);
。。。。。。
}
Converted to
int open( const char * pathname, int flags)
{
。。。。。。
swi\t __NR_open  //#define __NR_open(__NR_SYSCALL_BASE+  5)
。。。。。。
}

// kernel entry

/* arch/arm/kernel/entry-common.S */
ENTRY(vector_swi)
…… …… …… ……
adr tbl, sys_call_table @ load syscall table pointer
…… …… …… ……
ldrcc pc, [tbl, scno, lsl #2] @ call sys_* routine
…… …… …… ……
ENTRY(sys_call_table)


#include "calls.S"

/* arch/arm/kernel/calls.S */
/* 0 */ CALL(sys_restart_syscall)
CALL(sys_exit)
CALL(sys_fork_wrapper)
CALL(sys_read)
CALL(sys_write)
/* 5 */ CALL(sys_open)
………………………………………………………………
CALL(sys_dup3)
CALL(sys_pipe2)
/* 360 */CALL(sys_inotify_init1)


Intelligent Recommendation

Detailed explanation of syscall system call function under Linux system

NAME        syscall-indirect system call SYNOPSIS               #define _GNU_SOURCE   ...

Golang syscall principle

Golang System call Entrance 2. System call management 3. SYSCALL in runtime 4. User code system call and scheduling interaction Entersyscall and exitsyscall pipeline entersyscall exitsyscall entersysc...

Linux (program design): 16 --- System call function syscall

First, format Second, function and characteristics Syscall () is a small library function that calls the system call, the assembly language interface of the system call has a specified number of speci...

Golang system call syscall

For people who write back-end languages, file operations are very common. Go support for file operations is very good. Today, the syscall related content is recorded by the file operation in go. First...

syscall-use system call

table of Contents Function description reference Function description syscall is an instance of directly calling system calls. Parameter Description: number is the system call number, you can find it ...

More Recommendation

【Operating System XV6】 Syscall

【Operating System XV6】 Syscall Exercise 1 Trace Exercise 2 sysinfo Exercise 1 Trace Problem Description: Add a tracking system calling function to help debug. Create a new Trace system call that calls...

Linux syscall Hook

Introduction The Linux program is basically implemented by the system call (Syscall), so you can control the program to monitor or modify the program; Pre-knowledge We know that the procedures in the ...

Linux Advanced 49 - Syscall

Concept Syscall () is a small library function that calls the system call, the assembly language interface of the system call has a specified number of specified parameters. Syscall () Save the CPU re...

C language 64 -bit internal joint assembly uses the syscall system calls printing string

Under 64 -bit systems, the system calls can be passed directlysyscallThe assembly instructions are implemented, and the parameters are put in sequentiallyrax、rsi、rdi、rdx、 r10、r8、r9Back value put inrax...

Principle niushop system calls

Niushop open call principle Mall 1. Advantages and Disadvantages of common small systems General small business systems, most of the design is such that Controller (Model +DAO)---> View Simult...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top