25a353807a43c5233e4b1d8b042e48e5890bc198
explain/syscalls.md
... | ... | @@ -0,0 +1,38 @@ |
1 | +## Syscalls |
|
2 | + |
|
3 | +Main source: "Anatomy of a System Call", on lwn |
|
4 | +[1](https://lwn.net/Articles/604287/), [2](https://lwn.net/Articles/604515/). |
|
5 | +**READ THIS FIRST** |
|
6 | + |
|
7 | +Calling a syscall is done by firing a specific interrupt, and the parameters |
|
8 | +have to be placed in specific registers first. The kernel then handles the |
|
9 | +interrupt as explained in the above articles. (I'm not going to copy those |
|
10 | +texts here.) |
|
11 | + |
|
12 | +Each syscall is identified by its number, which should be placed in a specific |
|
13 | +regsiter before invoking the syscall. A table can be found in your system's |
|
14 | +`include/asm*/unistd*.h` files. Note that syscall numbers are |
|
15 | +architecture-dependent and some syscalls aren't implemented on certain hardware |
|
16 | +platforms, and some are only available in later versions of the kernel. |
|
17 | + |
|
18 | +### i386 |
|
19 | + |
|
20 | +On `i386`, syscalls are invoked using the `int 0x80` instruction. The syscall |
|
21 | +number is placed in `eax`, arguments are placed in `ebx`, `ecx`, `edx`, `esi`, |
|
22 | +`edi` registers. The return value is placed in the `eax` register. |
|
23 | + |
|
24 | +### x86_64 |
|
25 | + |
|
26 | +On `x86_64`, syscalls are invoked using the `syscall` instruction. The syscall |
|
27 | +number is placed in `rax`, arguments are placed in `rdi`, `rsi`, `rdx`, `r10`, |
|
28 | +`r8` and `r9`. *`r11` and `rcx` are destroyed when invoking a syscall.* The |
|
29 | +return value is placed in the `rax` register. |
|
30 | + |
|
31 | +### ARMv6 |
|
32 | + |
|
33 | +This is probably true for ARMv5 and ARMv7 as well. No guarantees for ARMv8 |
|
34 | +(aarch64). |
|
35 | + |
|
36 | +Syscalls are invoked using the `swi #0` instruction. The syscall number is |
|
37 | +placed in `r7`, arguments are placed in `r0` through `r6`. The return value is |
|
38 | +placed in the `r0` register. |