1. Description
A system call is a set of interfaces provided by the operating system for a process running in user mode to interact with hardware devices (such as CPU, disk, printer, etc.). When the user process needs to make a system call, the CPU switches to the kernel mode through a soft interrupt to start executing the kernel system call function.
2. Inline assembly
The following takes obtaining the current time of the system as an example to discuss the system call method.
1. glibc call
int tt = time(NULL);
2. syscall call
linux-2.6.32/arch/x86/kernel/syscall_table_32.S linux-2.6.32 system call table
linux-3.18.6/arch/x86/kernel/syscall_table_32.S linux-2.6.32 system call table
linux-3.5/arch/x86/syscalls/syscall_32.tbl linux-3.5 system call table
By viewing the syscall_table_32.S interrupt call table file, the sys_time interrupt call number is 13.
int tt = syscall(13);
3. Inline assembly
int tt = 0; asm("mov $0,%%ebx\n\t" "mov $13,%%eax\n\t" "int $0x80\n\t" "mov %%eax,%0\n\t" :"=m"(tt) ); printf( "time:%d\n", tt );
eax=13 incoming system call number
ebx=0 is passed to the system function, which is equivalent to time(NULL). If there are multiple incoming parameters, you can use ebx,
ecx, edx, esi, edi, ebp registers
eax receives the return value of the function and sends it out to tt
int $0x80 Interrupt trap, 0x2E is used as the system call entry under windows
3. Extended system calls
The time() calling process in glibc: the user mode calls time(), the interrupt falls into the kernel mode, and the kernel function sys_time() is called.
Custom system calls can be implemented in the following four steps:
linux-2.6.32/kernel/sys.c
sys_xxx(), implements system calls
linux-2.6.32/arch/x86/include/asm/unistd.h
_NR_xxx, add interrupt number macro definition
linux-2.6.32/arch/x86/kernel/syscall_table_32.S
.long sys_xx, add interrupt assembly definition
linux-2.6.32/include/linux/syscalls.h
asmlinkage, export the function
References:
"Programmer's Self-cultivation"