raz / commit 7624e35d

commit
7624e35dc0da1f77400ce165b40bbdd4ef3b46c6
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e5d33f12
raz -bare: a program for the bare machine

Its entry point reads no argc and argv, which only Linux leaves on the
stack, and runs no body of Rt, whose rt_sigaction and sigaltstack would
fault with no operating system under it; Rt is given no profiler, which
its body would start. It is still given the places, by stores alone, so
a failed check says where it was. A program that uses C is refused.
The last of PROPOSALS.md's three changes for a kernel.

patch · log

12 files changed, +101 -30

PROPOSALS.md modified +8 -16

@@ -1073,28 +1073,20 @@
10731073 `raz -base ADDR`, a multiple of the page, the program wholly in the low
10741074 or the top 2 GB (`doc/compiler.md`, the linker).
10751075
1076-**2. A program with no `Rt` body.** `Rt` is always used, and its body
1077-runs before every other module's: it sets the handlers of SIGSEGV and
1078-SIGFPE with `rt_sigaction` and a stack for them with `sigaltstack`,
1079-and starts the profiler with `setitimer` when asked. On the bare
1080-machine those `syscall`s fault (`EFER.SCE` is clear), and with no IDT
1081-yet the machine resets. The entry point also reads `argc` and `argv`
1082-from the stack as Linux leaves them. The proposal: `raz -bare`, under
1083-which the entry point reads no arguments and calls no body of `Rt`, and
1084-gives `Rt` nothing (as `-s` does), so that `fail` and `exit` are all
1085-that is reached of it; a kernel's own module, the first used, sets up
1086-what `Rt`'s body would. `exit` and `fail` still make system calls,
1087-which a kernel would catch, or replace by a library of its own as
1088-today; `-bare` only stops the ones made before the program can. About
1089-twenty lines in `Raz.raz` and `Gen.entryPoint`.
1076+**2. A program with no `Rt` body.** Done (2026-10-17): `raz -bare`
1077+reads no `argc` and `argv`, runs no body of `Rt` and gives it no
1078+profiler. It still gives `Rt` the places, by stores alone, so that a
1079+failed check says where it was, which the proposal had left out as
1080+`-s` does (`doc/compiler.md`, the command line).
10901081
10911082 **3. The privileged instructions in the assembler.** Done (2026-10-17):
10921083 `cli`, `sti`, `iretq`, `swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`,
10931084 `mov` to and from the control registers, `rdmsr`, `wrmsr`, `in`, `out`,
10941085 `wbinvd`, `xsave` and `xrstor` (`doc/compiler.md`, the assembler).
10951086
1096-What is still left to a kernel's own code, with all three: the boot
1097-stub, since no common loader enters an ELF64 file in long mode but
1087+What is still left to a kernel's own code, with all three: zeroing
1088+the bss and a stack aligned to 16 bytes before `_start`, as Linux
1089+gives; the boot stub, since no common loader enters an ELF64 file in long mode but
10981090 Firecracker (GRUB's Multiboot2 and QEMU's PVH enter in 32-bit mode,
10991091 and the assembler makes only 64-bit code); interrupt stubs that save
11001092 the XMM registers, since the compiler's code uses them; and, if the

README.md (history) modified +4 -2

@@ -89,7 +89,7 @@
8989
9090 ## Using it
9191
92- raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] Main.raz
92+ raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] [-bare] Main.raz
9393 raz run [option]... Main.raz [arg]...
9494 raz -h
9595 raz --version
@@ -116,7 +116,9 @@
116116 the symbols and line numbers, the places a failure is told by, and the
117117 profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves
118118 out only the profiler. `-base addr` loads the program at `addr`, not
119-400000H, in the low or the top 2 GB, as a kernel may want.
119+400000H, in the low or the top 2 GB, as a kernel may want, and
120+`-bare` makes one for the bare machine: no arguments are read and no
121+signal handlers set.
120122
121123 At run time, a failed check says where it is and what failed, as in
122124 `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends

doc/compiler.md (history) modified +12 -2

@@ -21,12 +21,12 @@
2121 | `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
24+| `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls |
2626 | `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
29-| `src/Raz.raz` | 361 | 283 | The command line |
29+| `src/Raz.raz` | 369 | 289 | The command line |
3030
3131 Every file opens with a comment that says what it is, what it depends
3232 on and what it leaves out. Read those first; they are the map.
@@ -1751,6 +1751,16 @@
17511751 one starts at a `_start` that calls `__libc_start_main` the way C's own
17521752 start-up does, which sets C up and calls back a `main` that runs the
17531753 module bodies and the program's `main`.
1754+
1755+`raz -bare` makes a program for the bare machine, a kernel, with no
1756+Linux under it: its `_start` reads no `argc` and `argv`, which only
1757+Linux leaves on the stack, and calls no body of `Rt`, whose
1758+`rt_sigaction` and `sigaltstack` would fault there (`EFER.SCE` is
1759+clear) and, with no IDT yet, reset the machine. Nor is `Rt` given the
1760+profiler, which its body would start. It is still given the places, by
1761+stores alone, so a failed check says where it was; but `Rt.fail` and
1762+`Rt.exit` end in system calls, which the kernel catches or replaces. A
1763+program that uses C is refused.
17541764
17551765 `raz -S` lists the code (`X64.list`) instead of writing
17561766 it. `raz run` writes the executable to a memfd, a file only in memory, and

doc/raz.1 (history) modified +12 -0

@@ -107,6 +107,18 @@
107107 .Cm 0FFFFFFFF80000000H .
108108 The program must lie wholly in the low or the top 2 GB of the address
109109 space, which a 32-bit address reaches.
110+.It Fl bare
111+Make a program for the bare machine, a kernel, with no Linux under it.
112+Its entry point reads no arguments, so
113+.Fn ARGC
114+is 0, and runs no body of
115+.Cm Rt ,
116+whose system calls set the handlers of signals: a nil pointer or a
117+division fault is not told.
118+A failed check is still told, by a system call the kernel must catch.
119+Its stack must be 16-byte aligned, as Linux leaves it, and its bss
120+zeroed.
121+A program that uses C is refused.
110122 .It Fl noprofile
111123 Leave out the profiler, which
112124 .Ev RAZ_PROFILE

lib/Rt.raz (history) modified +3 -1

@@ -17,7 +17,9 @@
1717 placer: a failure is told by its address, and calls and place,
1818 which would need the places, are left out the same way.
1919 The handlers are Linux's on x86-64, set by system calls: what the
20- kernel hands them is laid out as it is there. *)
20+ kernel hands them is laid out as it is there. A program built with
21+ raz -bare, for the bare machine, runs no body of Rt, so has no
22+ handlers and no profiler. *)
2123 module Rt;
2224 import Sys;
2325

src/Gen.raz modified +12 -3

@@ -2419,8 +2419,14 @@
24192419 library's, in a program that uses C. exitProc: the chunk of Rt's exit. The
24202420 first numLinks of links: the addresses Rt is given, of what the program
24212421 has: the places, and the profiler and calls; what it isn't given,
2422- nothing reaches, and the linker leaves out. *)
2423-proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT;
2422+ nothing reaches, and the linker leaves out.
2423+
2424+ bare: raz -bare, a program for the bare machine, a kernel's, with
2425+ no Linux under it. Its entry point reads no argc and argv, which
2426+ only Linux leaves on the stack, so that ARGC() is 0; and runs no body
2427+ of Rt, whose system calls, to set the signals' handlers, would fault
2428+ there. A kernel's own module sets up what Rt's body would. *)
2429+proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT;
24242430 var entry, start, i: INT; m: ^Sym.Module;
24252431 begin
24262432 entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start"));
@@ -2450,6 +2456,9 @@
24502456 X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false);
24512457 X.store(X.memRip(cLinks[i].into, 0), RAX, 8);
24522458 end;
2459+ elsif bare then
2460+ start := entry;
2461+ Chunk.beginChunk(entry);
24532462 else
24542463 start := entry;
24552464 Chunk.beginChunk(entry);
@@ -2464,7 +2473,7 @@
24642473 end;
24652474 m := first;
24662475 while m # nil do
2467- if m^.used and (m^.body >= 0) then X.callChunk(m^.body); end;
2476+ if m^.used and (m^.body >= 0) and not (bare and (m^.name = internStr("Rt"))) then X.callChunk(m^.body); end;
24682477 m := m^.next;
24692478 end;
24702479 if mainObj # nil then X.callChunk(mainObj^.chunk); end;

src/Raz.raz (history) modified +14 -6

@@ -1,6 +1,6 @@
11 (* Raz: the command line.
22
3- raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] Main.raz
3+ raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] [-bare] Main.raz
44 raz run [-I dir]... Main.raz [arg]...
55 raz --version
66
@@ -20,7 +20,8 @@
2020 C up and calls this as C's main; first it gives Rt C's exit, which
2121 Rt.exit then ends the program with. The C library is glibc, or musl
2222 on a machine that has musl's loader and not glibc's, unless -libc
23- names one.
23+ names one. Under -bare, for the bare machine, it saves no argc and
24+ argv and runs no body of Rt (Gen.entryPoint).
2425
2526 Depends on: everything else, and Version, a module the build makes
2627 (build/src/Version.raz) from build/version.inc: what git describe
@@ -134,6 +135,8 @@
134135 Out.str(" -base addr loads the program at addr, not 400000H: a multiple of 4096\n");
135136 Out.str(" in the low or the top 2 GB, as 0FFFFFFFF80000000H\n");
136137 Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n");
138+ Out.str(" -bare for the bare machine, a kernel: reads no argc and argv, and\n");
139+ Out.str(" runs no body of Rt, which sets the signals' handlers\n");
137140 Out.str(" -libc name links a program that uses C against glibc or musl; by\n");
138141 Out.str(" default glibc, or musl where only its loader is there\n");
139142 Out.str(" -h, --help says this\n");
@@ -200,11 +203,12 @@
200203 path, outPath, s: []U8; dir: []U8;
201204 i, first, entry, fd, last, errno, base: INT;
202205 m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj;
203- run, noProfile, listing, dynamic, libcSaid, ok: BOOL; err: ERROR;
206+ run, noProfile, listing, dynamic, libcSaid, ok, bare: BOOL; err: ERROR;
204207 begin
205208 path := "";
206209 outPath := "";
207210 noProfile := false;
211+ bare := false;
208212 listing := false;
209213 libcSaid := false;
210214 run := (ARGC() >= 2) and (Files.arg(1) = "run");
@@ -234,6 +238,8 @@
234238 end;
235239 elsif s = "-noprofile" then
236240 noProfile := true;
241+ elsif s = "-bare" then
242+ bare := true;
237243 elsif s = "-libc" then
238244 INC(i);
239245 s := Files.arg(i);
@@ -304,7 +310,8 @@
304310 (* what Rt is given: the places, which the linker makes, for Rt to say
305311 where a failed check or a signal was (place), and the calls under
306312 way then (calls); and the profiler, which needs them too. raz -s
307- gives none, -noprofile no profiler. *)
313+ gives none, -noprofile no profiler, and -bare no profiler, since
314+ Rt's body, which starts it, doesn't run. *)
308315 numLinks := 0;
309316 if not Elf.stripped then
310317 Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1));
@@ -312,12 +319,13 @@
312319 addLink(Link.placesChunk, objIn(rt, "places"));
313320 addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller"));
314321 addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer"));
315- if not noProfile then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end;
322+ if not noProfile and not bare then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end;
316323 end;
317324 (* what a program that uses C is given of C: Rt its exit, and
318325 Thread, if the program has it, the calls that make and join C's
319326 threads, so that the C library knows of them *)
320327 dynamic := usesC();
328+ if dynamic and bare then Diag.fatal("-bare: the program uses C, which needs Linux and its loader"); end;
321329 numCLinks := 0;
322330 if dynamic then
323331 addCLink("exit", objIn(rt, "cExit"));
@@ -327,7 +335,7 @@
327335 addCLink("pthread_join", objIn(thread, "cJoin"));
328336 end;
329337 end;
330- entry := Gen.entryPoint(dynamic, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks,
338+ entry := Gen.entryPoint(dynamic, bare, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks,
331339 objIn(rt, "exit")^.chunk, links[0..numLinks], numLinks);
332340
333341 Link.markLive(entry);

test/bare/Bare.raz (history) added +17 -0

@@ -0,0 +1,17 @@
1+module Bare;
2+
3+(* For raz -bare: no argc and argv are read, so ARGC() is 0; and Rt's
4+ body, which sets the signals' handlers, doesn't run, so a nil pointer
5+ ends the program by SIGSEGV with nothing said. *)
6+import Out;
7+
8+proc main*;
9+var p: ^INT;
10+begin
11+ Out.int(ARGC()); Out.ln;
12+ Out.flush();
13+ p := nil;
14+ p^ := 1;
15+end;
16+
17+end Bare.

test/check.sh (history) modified +10 -0

@@ -137,6 +137,16 @@
137137 if [ $? = 0 ] && cmp -s $out/Index.out $out/IndexB.out && grep -q 'Index.main' $out/IndexB.out \
138138 && entry $out/IndexB | grep -q '^000000007fe' && entry $out/IndexK | grep -q '^ffffffff800'; then pass=$((pass + 1))
139139 else echo "FAIL raz -base:"; cat $out/IndexB.out; fail=$((fail + 1)); fi
140+# raz -bare: no argc read, so ARGC() is 0, and no handlers set, so a nil
141+# pointer stops the program by SIGSEGV with nothing said; a failed
142+# check is still told by its place, and nothing is profiled
143+$raz -bare -o $out/Bare test/bare/Bare.raz
144+status=$( { ( exec $out/Bare one two ) > $out/Bare.out 2>$out/Bare.err; echo $?; } 2>/dev/null )
145+$raz -bare -o $out/IndexBare test/strip/Index.raz
146+RAZ_PROFILE=1 $out/IndexBare > $out/IndexBare.out 2>&1
147+if [ $status = 139 ] && [ "$(cat $out/Bare.out)" = 0 ] && [ ! -s $out/Bare.err ] \
148+ && cmp -s $out/Index.out $out/IndexBare.out; then pass=$((pass + 1))
149+else echo "FAIL raz -bare: status $status"; cat $out/Bare.out $out/Bare.err $out/IndexBare.out; fail=$((fail + 1)); fi
140150
141151 # raz -S: test/list/X.raz lists as X.expect
142152 for f in test/list/*.raz; do

test/err/BareC.args (history) added +1 -0

@@ -0,0 +1 @@
1+-bare

test/err/BareC.err (history) added +1 -0

@@ -0,0 +1 @@
1+raz: -bare: the program uses C, which needs Linux and its loader

test/err/BareC.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module BareC;
2+extern proc getpid(): I32;
3+proc main*;
4+begin
5+ _ := getpid();
6+end;
7+end BareC.