raz / site / public / advanced / syscalls.html

210 lines · 16.1 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>System calls - More Raz</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="System calls - More Raz">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'>
22<li><a href="fmt.html">Output and formatting</a></li>
23<li><a href="bytes.html">Strings and bytes</a></li>
24<li><a href="collections.html">Lists, arrays, maps and sorting</a></li>
25<li><a href="files.html">Files and the command line</a></li>
26<li><a href="processes.html">Time, randomness and other programs</a></li>
27<li><a href="allocators.html">Writing an allocator</a></li>
28</ol>
29<h3>Standing alone</h3><ol start='7'>
30<li><a href="static.html">A program with no run time</a></li>
31<li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li>
32<li><a href="lifecycle.html">Starting, ending, and signals</a></li>
33<li><a href="targets.html">One module, one file per target</a></li>
34</ol>
35<h3>The machine</h3><ol start='11'>
36<li><a href="asm.html">Procedures in assembly</a></li>
37<li><a href="syscalls.html" aria-current="page">System calls</a></li>
38<li><a href="listing.html">What the compiler makes</a></li>
39</ol>
40<h3>Shared memory</h3><ol start='14'>
41<li><a href="volatile.html">Volatile</a></li>
42<li><a href="atomics.html">Atomics</a></li>
43<li><a href="ordering.html">Ordering and publication</a></li>
44<li><a href="lockfree.html">A lock-free stack</a></li>
45<li><a href="ring.html">A ring buffer between threads</a></li>
46</ol></aside><article><p class="kicker">The machine &middot; lesson 12 of 18</p><h1>System calls</h1>
47<p>Under <code>Sys</code> there is one built-in, <code>SYSTEM.SYSCALL(n, x1, ..., x6)</code>, which a module reaches with <code>import SYSTEM;</code>. It makes Linux system call number <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>, and gives the raw result, an <code>INT</code>. A negative result is <code>-errno</code>. That is all the interface there is; <code>Sys.write</code>, <code>Sys.open</code> and the rest of <code>lib/Sys.linux-x86_64.raz</code> are written with it, in raz, in a few lines each.</p>
48<p><code>SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, which is why their <code>Sys</code> calls the C library instead.</p>
49<h2>The shape of a call</h2>
50<p><code>write(fd, buf, count)</code> is call number 1. The arguments are the descriptor, a pointer to the bytes, and how many.</p>
51<div class="file"><span>Raw.raz</span></div><pre class="code"><code><span class="kw">module</span> Raw;
52<span class="kw">import</span> SYSTEM;
53
54<span class="kw">const</span> Msg = <span class="string">&quot;straight to the kernel\n&quot;</span>;
55
56<span class="kw">proc</span> main*;
57<span class="kw">var</span> n: <span class="ty">INT</span>;
58<span class="kw">begin</span>
59 n := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
60 <span class="kw">if</span> n # <span class="ty">LEN</span>(Msg) <span class="kw">then</span> _ := SYSTEM.SYSCALL(<span class="number">60</span>, <span class="number">1</span>); <span class="kw">end</span>; <span class="comment">(* exit(1), if it came up short *)</span>
61<span class="kw">end</span>;
62
63<span class="kw">end</span> Raw.</code></pre>
64<pre class="output term"><code><span class="cmd">$ raz -Werror Raw.raz</span>
65Raw.raz:9:42: error: Msg[0] is read through a []const slice, and can&#x27;t be changed
66 9 | n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
67 | ^^^^^^
68help: a []const slice&#x27;s elements are only read: write them through a []T
69raz: 1 error
70<span class="cmd">$ ./Raw</span>
71/bin/sh: line 1: ./Raw: No such file or directory</code></pre>
72<p>That program has no <code>Out</code>, no <code>Sys</code>, and no output buffer. What it needs of the library is <code>Rt</code>'s start-up, which the compiler links in anyway.</p>
73<h2>Reading the result</h2>
74<p>A result that is negative is a failure, and its negation is the <code>errno</code>. <code>open</code> is call 2 and wants a C string: the path's bytes followed by a zero. A <code>[N]U8</code> made from a string literal is zero-filled past its length, so there is always a terminator.</p>
75<div class="file"><span>Open.raz</span></div><pre class="code"><code><span class="kw">module</span> Open;
76<span class="kw">import</span> Out;
77<span class="kw">import</span> SYSTEM;
78
79<span class="kw">proc</span> main*;
80<span class="kw">var</span> path: [<span class="number">32</span>]<span class="ty">U8</span>; fd: <span class="ty">INT</span>;
81<span class="kw">begin</span>
82 path := <span class="string">&quot;/no/such/file&quot;</span>;
83 fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); <span class="comment">(* open(path, O_RDONLY) *)</span>
84 Out.str(<span class="string">&quot;open gave &quot;</span>); Out.int(fd); Out.ln; <span class="comment">(* -2: ENOENT *)</span>
85 path := <span class="string">&quot;/dev/null&quot;</span>;
86 fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>);
87 <span class="kw">if</span> fd &gt;= <span class="number">0</span> <span class="kw">then</span>
88 Out.str(<span class="string">&quot;opened /dev/null\n&quot;</span>);
89 _ := SYSTEM.SYSCALL(<span class="number">3</span>, fd); <span class="comment">(* close *)</span>
90 <span class="kw">end</span>;
91<span class="kw">end</span>;
92
93<span class="kw">end</span> Open.</code></pre>
94<pre class="output"><code>open gave -2
95opened /dev/null
96</code></pre>
97<p>The library's <code>Sys.openRead</code> does exactly this, and <code>Sys.close</code> too. Reading <code>lib/Sys.linux-x86_64.raz</code> is the best reference there is for what each call wants.</p>
98<h2>A call that fills a structure</h2>
99<p><code>uname</code> (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 <code>SYSTEM.ADR</code> given to the kernel.</p>
100<div class="file"><span>Who.raz</span></div><pre class="code"><code><span class="kw">module</span> Who;
101<span class="kw">import</span> Out;
102<span class="kw">import</span> SYSTEM;
103
104<span class="comment">(* the length of the zero-ended string at the start of s *)</span>
105<span class="kw">proc</span> cstr(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>;
106<span class="kw">var</span> n: <span class="ty">INT</span>;
107<span class="kw">begin</span>
108 n := <span class="number">0</span>;
109 <span class="kw">while</span> (n &lt; <span class="ty">LEN</span>(s)) <span class="kw">and</span> (s[n] # <span class="number">0</span>) <span class="kw">do</span> <span class="ty">INC</span>(n); <span class="kw">end</span>;
110 <span class="kw">return</span> n;
111<span class="kw">end</span>;
112
113<span class="kw">proc</span> main*;
114<span class="kw">var</span> u: [<span class="number">390</span>]<span class="ty">U8</span>;
115<span class="kw">begin</span>
116 <span class="kw">if</span> SYSTEM.SYSCALL(<span class="number">63</span>, SYSTEM.<span class="ty">ADR</span>(u[<span class="number">0</span>])) = <span class="number">0</span> <span class="kw">then</span>
117 Out.str(<span class="string">&quot;system: &quot;</span>); Out.str(u[<span class="number">0</span>..cstr(u[<span class="number">0</span>..<span class="number">65</span>])]); Out.ln;
118 Out.str(<span class="string">&quot;machine: &quot;</span>); Out.str(u[<span class="number">260</span>..<span class="number">260</span> + cstr(u[<span class="number">260</span>..<span class="number">325</span>])]); Out.ln;
119 <span class="kw">end</span>;
120<span class="kw">end</span>;
121
122<span class="kw">end</span> Who.</code></pre>
123<pre class="output"><code>system: Linux
124machine: x86_64
125</code></pre>
126<h2>Memory straight from the kernel</h2>
127<p><code>mmap</code> 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 <code>munmap</code> (call 11). It is what <code>Mem.pages</code> does, and so what every allocator is ultimately made of.</p>
128<div class="file"><span>Pages.raz</span></div><pre class="code"><code><span class="kw">module</span> Pages;
129<span class="kw">import</span> Out;
130<span class="kw">import</span> SYSTEM;
131
132<span class="kw">proc</span> main*;
133<span class="kw">var</span> p: ^<span class="ty">U8</span>; i: <span class="ty">INT</span>; total: <span class="ty">INT</span>;
134<span class="kw">begin</span>
135 <span class="comment">(* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *)</span>
136 p := ^<span class="ty">U8</span>(SYSTEM.SYSCALL(<span class="number">9</span>, <span class="number">0</span>, <span class="number">4096</span>, <span class="number">3</span>, <span class="number">22H</span>, -<span class="number">1</span>, <span class="number">0</span>));
137 <span class="kw">if</span> <span class="ty">INT</span>(p) &lt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;no memory\n&quot;</span>); <span class="kw">return</span>; <span class="kw">end</span>;
138 Out.int(<span class="ty">INT</span>(p[<span class="number">0</span>]) + p[<span class="number">4095</span>]); Out.str(<span class="string">&quot; (zero, as promised)\n&quot;</span>);
139 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> p[i] := <span class="ty">U8</span>(i); <span class="kw">end</span>;
140 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> <span class="ty">INC</span>(total, p[i]); <span class="kw">end</span>;
141 Out.int(total); Out.ln;
142 _ := SYSTEM.SYSCALL(<span class="number">11</span>, p, <span class="number">4096</span>);
143<span class="kw">end</span>;
144
145<span class="kw">end</span> Pages.</code></pre>
146<pre class="output"><code>0 (zero, as promised)
147522240
148</code></pre>
149<h2>A cat with no library</h2>
150<p>Reading and writing in a loop is all <code>cat</code> does. With system calls, it is one procedure. Call 0 is <code>read</code> and call 1 is <code>write</code>; 0 and 1 are standard input and output.</p>
151<div class="file"><span>Cat.raz</span></div><pre class="code"><code><span class="kw">module</span> Cat;
152<span class="kw">import</span> SYSTEM;
153
154<span class="kw">proc</span> main*: <span class="ty">INT</span>;
155<span class="kw">var</span> buf: [<span class="number">4096</span>]<span class="ty">U8</span>; n, wrote, at: <span class="ty">INT</span>;
156<span class="kw">begin</span>
157 n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
158 <span class="kw">while</span> n &gt; <span class="number">0</span> <span class="kw">do</span>
159 at := <span class="number">0</span>;
160 <span class="kw">while</span> at &lt; n <span class="kw">do</span> <span class="comment">(* a write may be partial *)</span>
161 wrote := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(buf[at]), n - at);
162 <span class="kw">if</span> wrote &lt; <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
163 <span class="ty">INC</span>(at, wrote);
164 <span class="kw">end</span>;
165 n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
166 <span class="kw">end</span>;
167 <span class="kw">if</span> n &lt; <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
168 <span class="kw">return</span> <span class="number">0</span>;
169<span class="kw">end</span>;
170
171<span class="kw">end</span> Cat.</code></pre>
172<pre class="output term"><code><span class="cmd">$ raz Cat.raz</span>
173<span class="cmd">$ echo &quot;through the pipe&quot; | ./Cat</span>
174through the pipe
175<span class="cmd">$ ./Cat &lt; /dev/null; echo &quot;status $?&quot;</span>
176status 0
177<span class="cmd">$ echo &quot;see the calls&quot; | strace -e trace=read,write ./Cat 2&gt;&amp;1 &gt;/dev/null | grep -v &#x27;^+++&#x27;</span>
178read(0, &quot;see the calls\n&quot;, 4096) = 14
179write(1, &quot;see the calls\n&quot;, 14) = 14
180read(0, &quot;&quot;, 4096) = 0</code></pre>
181<p>The partial-write loop is the kind of detail <code>Sys.write</code> leaves to the caller on purpose: it returns the bytes written, and the program decides what to do about the rest.</p>
182<h2>In a code procedure</h2>
183<p>A system call is also an instruction. <code>syscall</code> takes its number in <code>rax</code> and its arguments in <code>rdi</code>, <code>rsi</code>, <code>rdx</code>, <code>r10</code>, <code>r8</code> and <code>r9</code>, which is not quite the order a procedure's arguments arrive in, since <code>r10</code> takes the place of <code>rcx</code>, and <code>syscall</code> itself clobbers <code>rcx</code> and <code>r11</code>.</p>
184<div class="file"><span>Ppid.raz</span></div><pre class="code"><code><span class="kw">module</span> Ppid;
185<span class="kw">import</span> Out;
186<span class="kw">import</span> SYSTEM;
187
188<span class="comment">(* getppid, call 110: no arguments *)</span>
189<span class="kw">proc</span> getppid: <span class="ty">INT</span>;
190<span class="kw">code</span>
191 mov eax, <span class="number">110</span>;
192 syscall;
193 ret;
194<span class="kw">end</span> getppid;
195
196<span class="kw">proc</span> main*;
197<span class="kw">begin</span>
198 <span class="kw">if</span> (getppid() &gt; <span class="number">0</span>) <span class="kw">and</span> (SYSTEM.SYSCALL(<span class="number">110</span>) = getppid()) <span class="kw">then</span>
199 Out.str(<span class="string">&quot;two ways to ask, one answer\n&quot;</span>);
200 <span class="kw">end</span>;
201<span class="kw">end</span>;
202
203<span class="kw">end</span> Ppid.</code></pre>
204<pre class="output"><code>two ways to ask, one answer
205</code></pre>
206<p><code>SYSCALL</code> 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.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="asm.html">&larr; Procedures in assembly</a><a href="index.html">Contents</a><a class="next" href="listing.html">What the compiler makes &rarr;</a></div></article></div>
207</main>
208<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
209</body>
210</html>