System calls Implementation

2019-01-23 09:26发布

If a user application makes a system call , a software interrupt/exception is triggered. How can I see the source code for generating a software interrupt ?

3条回答
小情绪 Triste *
2楼-- · 2019-01-23 09:59

It is explained in Linux Assembly Howto. And you should read wikipedia syscall page (and also about VDSO), and also intro(2) & syscalls(2) man pages. See also this answer and this one. Look also inside Gnu Libc & musl-libc source code. Learn also to use strace to find out which syscalls are made by a given command or process.

See also the calling conventions and Application Binary Interface specification relevant to your system. For x86-64 it is here.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-23 10:15

A software interrupt can be triggered with the Intel x86 assembly instruction int n, where n is the interrupt number. A syscall is a special case of software interrupt; in you can manually do a syscall by

mov eax, m
int 0x80

where m should be replaced with the interrupt number. Here are lists of 32-bit syscall numbers and 64-bit syscall numbers that linked to online manpages for the each function. You also need to pass parameters to the syscall via other registers (ebx, ecx, etc.), and you can read more about that here.

This is the most general way to do syscalls because it is independent of external libraries, like libc, and you can implement this in C/C++ if you need to by using inline assembly.

查看更多
做自己的国王
4楼-- · 2019-01-23 10:19

long long ago, there is an int 0x80 trap to enter into the kernel, but nowadays sysenter is preferred.

you can get the code by dumping vsyscall section which mapped into every process automatically by kernel.

$ cat /proc/self/maps 
blah blah blah
...
blah blah blah
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

for more information check this article

查看更多
登录 后发表回答