raz / site / content / advanced / 12-syscalls.txt

183 lines · 5.7 KiB · raw · history · last change a5145f64

1title: System calls
2part: The machine
3summary: SYSCALL directly: the numbers, the arguments, negative errno, and a cat without a library.
4---
5Under `Sys` there is one built-in, `SYSTEM.SYSCALL(n, x1, ..., x6)`, which a module reaches with `import SYSTEM;`. It makes Linux system call number `n` with up to six arguments, each a pointer or an integer no wider than `INT`, and gives the raw result, an `INT`. A negative result is `-errno`. That is all the interface there is; `Sys.write`, `Sys.open` and the rest of `lib/Sys.linux-x86_64.raz` are written with it, in raz, in a few lines each.
6
7`SYSCALL` exists only on Linux targets. Other systems have no stable system call interface, which is why their `Sys` calls the C library instead.
8
9## The shape of a call
10
11`write(fd, buf, count)` is call number 1. The arguments are the descriptor, a pointer to the bytes, and how many.
12
13```raz Raw
14module Raw;
15import SYSTEM;
16
17const Msg = "straight to the kernel\n";
18
19proc main*;
20var n: INT;
21begin
22 n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
23 if n # LEN(Msg) then _ := SYSTEM.SYSCALL(60, 1); end; (* exit(1), if it came up short *)
24end;
25
26end Raw.
27```
28
29```sh
30$ raz -Werror Raw.raz
31$ ./Raw
32```
33
34That program has no `Out`, no `Sys`, and no output buffer. What it needs of the library is `Rt`'s start-up, which the compiler links in anyway.
35
36## Reading the result
37
38A result that is negative is a failure, and its negation is the `errno`. `open` is call 2 and wants a C string: the path's bytes followed by a zero. A `[N]U8` made from a string literal is zero-filled past its length, so there is always a terminator.
39
40```raz Open run
41module Open;
42import Out;
43import SYSTEM;
44
45proc main*;
46var path: [32]U8; fd: INT;
47begin
48 path := "/no/such/file";
49 fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0); (* open(path, O_RDONLY) *)
50 Out.str("open gave "); Out.int(fd); Out.ln; (* -2: ENOENT *)
51 path := "/dev/null";
52 fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0);
53 if fd >= 0 then
54 Out.str("opened /dev/null\n");
55 _ := SYSTEM.SYSCALL(3, fd); (* close *)
56 end;
57end;
58
59end Open.
60```
61
62The library's `Sys.openRead` does exactly this, and `Sys.close` too. Reading `lib/Sys.linux-x86_64.raz` is the best reference there is for what each call wants.
63
64## A call that fills a structure
65
66`uname` (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with its `SYSTEM.ADR` given to the kernel.
67
68```raz Who run
69module Who;
70import Out;
71import SYSTEM;
72
73(* the length of the zero-ended string at the start of s *)
74proc cstr(s: []const U8): INT;
75var n: INT;
76begin
77 n := 0;
78 while (n < LEN(s)) and (s[n] # 0) do INC(n); end;
79 return n;
80end;
81
82proc main*;
83var u: [390]U8;
84begin
85 if SYSTEM.SYSCALL(63, SYSTEM.ADR(u[0])) = 0 then
86 Out.str("system: "); Out.str(u[0..cstr(u[0..65])]); Out.ln;
87 Out.str("machine: "); Out.str(u[260..260 + cstr(u[260..325])]); Out.ln;
88 end;
89end;
90
91end Who.
92```
93
94## Memory straight from the kernel
95
96`mmap` is call 9: six arguments, the most one call can take. This asks for a page of anonymous, zeroed memory, writes in it, and gives it back with `munmap` (call 11). It is what `Mem.pages` does, and so what every allocator is ultimately made of.
97
98```raz Pages run
99module Pages;
100import Out;
101import SYSTEM;
102
103proc main*;
104var p: ^U8; i: INT; total: INT;
105begin
106 (* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *)
107 p := ^U8(SYSTEM.SYSCALL(9, 0, 4096, 3, 22H, -1, 0));
108 if INT(p) < 0 then Out.str("no memory\n"); return; end;
109 Out.int(INT(p[0]) + p[4095]); Out.str(" (zero, as promised)\n");
110 for i := 0 to 4095 do p[i] := U8(i); end;
111 for i := 0 to 4095 do INC(total, p[i]); end;
112 Out.int(total); Out.ln;
113 _ := SYSTEM.SYSCALL(11, p, 4096);
114end;
115
116end Pages.
117```
118
119## A cat with no library
120
121Reading and writing in a loop is all `cat` does. With system calls, it is one procedure. Call 0 is `read` and call 1 is `write`; 0 and 1 are standard input and output.
122
123```raz Cat
124module Cat;
125import SYSTEM;
126
127proc main*: INT;
128var buf: [4096]U8; n, wrote, at: INT;
129begin
130 n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf));
131 while n > 0 do
132 at := 0;
133 while at < n do (* a write may be partial *)
134 wrote := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(buf[at]), n - at);
135 if wrote < 0 then return 1; end;
136 INC(at, wrote);
137 end;
138 n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf));
139 end;
140 if n < 0 then return 1; end;
141 return 0;
142end;
143
144end Cat.
145```
146
147```sh
148$ raz Cat.raz
149$ echo "through the pipe" | ./Cat
150$ ./Cat < /dev/null; echo "status $?"
151$ echo "see the calls" | strace -e trace=read,write ./Cat 2>&1 >/dev/null | grep -v '^+++'
152```
153
154The partial-write loop is the kind of detail `Sys.write` leaves to the caller on purpose: it returns the bytes written, and the program decides what to do about the rest.
155
156## In a code procedure
157
158A system call is also an instruction. `syscall` takes its number in `rax` and its arguments in `rdi`, `rsi`, `rdx`, `r10`, `r8` and `r9`, which is not quite the order a procedure's arguments arrive in, since `r10` takes the place of `rcx`, and `syscall` itself clobbers `rcx` and `r11`.
159
160```raz Ppid run
161module Ppid;
162import Out;
163import SYSTEM;
164
165(* getppid, call 110: no arguments *)
166proc getppid: INT;
167code
168 mov eax, 110;
169 syscall;
170 ret;
171end getppid;
172
173proc main*;
174begin
175 if (getppid() > 0) and (SYSTEM.SYSCALL(110) = getppid()) then
176 Out.str("two ways to ask, one answer\n");
177 end;
178end;
179
180end Ppid.
181```
182
183`SYSCALL` is the right tool nearly always: the compiler puts the arguments where they belong, and the result is checked in the open. A code procedure is for the rare call you want to make from inside another piece of assembly.