| 1 | title: System calls |
| 2 | part: The machine |
| 3 | summary: SYSCALL directly: the numbers, the arguments, negative errno, and a cat without a library. |
| 4 | --- |
| 5 | Under `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 |
| 14 | module Raw; |
| 15 | import SYSTEM; |
| 16 | |
| 17 | const Msg = "straight to the kernel\n"; |
| 18 | |
| 19 | proc main*; |
| 20 | var n: INT; |
| 21 | begin |
| 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 *) |
| 24 | end; |
| 25 | |
| 26 | end Raw. |
| 27 | ``` |
| 28 | |
| 29 | ```sh |
| 30 | $ raz -Werror Raw.raz |
| 31 | $ ./Raw |
| 32 | ``` |
| 33 | |
| 34 | That 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 | |
| 38 | A 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 |
| 41 | module Open; |
| 42 | import Out; |
| 43 | import SYSTEM; |
| 44 | |
| 45 | proc main*; |
| 46 | var path: [32]U8; fd: INT; |
| 47 | begin |
| 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; |
| 57 | end; |
| 58 | |
| 59 | end Open. |
| 60 | ``` |
| 61 | |
| 62 | The 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 |
| 69 | module Who; |
| 70 | import Out; |
| 71 | import SYSTEM; |
| 72 | |
| 73 | (* the length of the zero-ended string at the start of s *) |
| 74 | proc cstr(s: []const U8): INT; |
| 75 | var n: INT; |
| 76 | begin |
| 77 | n := 0; |
| 78 | while (n < LEN(s)) and (s[n] # 0) do INC(n); end; |
| 79 | return n; |
| 80 | end; |
| 81 | |
| 82 | proc main*; |
| 83 | var u: [390]U8; |
| 84 | begin |
| 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; |
| 89 | end; |
| 90 | |
| 91 | end 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 |
| 99 | module Pages; |
| 100 | import Out; |
| 101 | import SYSTEM; |
| 102 | |
| 103 | proc main*; |
| 104 | var p: ^U8; i: INT; total: INT; |
| 105 | begin |
| 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); |
| 114 | end; |
| 115 | |
| 116 | end Pages. |
| 117 | ``` |
| 118 | |
| 119 | ## A cat with no library |
| 120 | |
| 121 | Reading 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 |
| 124 | module Cat; |
| 125 | import SYSTEM; |
| 126 | |
| 127 | proc main*: INT; |
| 128 | var buf: [4096]U8; n, wrote, at: INT; |
| 129 | begin |
| 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; |
| 142 | end; |
| 143 | |
| 144 | end 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 | |
| 154 | The 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 | |
| 158 | A 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 |
| 161 | module Ppid; |
| 162 | import Out; |
| 163 | import SYSTEM; |
| 164 | |
| 165 | (* getppid, call 110: no arguments *) |
| 166 | proc getppid: INT; |
| 167 | code |
| 168 | mov eax, 110; |
| 169 | syscall; |
| 170 | ret; |
| 171 | end getppid; |
| 172 | |
| 173 | proc main*; |
| 174 | begin |
| 175 | if (getppid() > 0) and (SYSTEM.SYSCALL(110) = getppid()) then |
| 176 | Out.str("two ways to ask, one answer\n"); |
| 177 | end; |
| 178 | end; |
| 179 | |
| 180 | end 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. |