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.
| @@ -1073,28 +1073,20 @@ | ||
| 1073 | 1073 | `raz -base ADDR`, a multiple of the page, the program wholly in the low |
| 1074 | 1074 | or the top 2 GB (`doc/compiler.md`, the linker). |
| 1075 | 1075 | |
| 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). | |
| 1090 | 1081 | |
| 1091 | 1082 | **3. The privileged instructions in the assembler.** Done (2026-10-17): |
| 1092 | 1083 | `cli`, `sti`, `iretq`, `swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`, |
| 1093 | 1084 | `mov` to and from the control registers, `rdmsr`, `wrmsr`, `in`, `out`, |
| 1094 | 1085 | `wbinvd`, `xsave` and `xrstor` (`doc/compiler.md`, the assembler). |
| 1095 | 1086 | |
| 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 | |
| 1098 | 1090 | Firecracker (GRUB's Multiboot2 and QEMU's PVH enter in 32-bit mode, |
| 1099 | 1091 | and the assembler makes only 64-bit code); interrupt stubs that save |
| 1100 | 1092 | the XMM registers, since the compiler's code uses them; and, if the |
| @@ -89,7 +89,7 @@ | ||
| 89 | 89 | |
| 90 | 90 | ## Using it |
| 91 | 91 | |
| 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 | |
| 93 | 93 | raz run [option]... Main.raz [arg]... |
| 94 | 94 | raz -h |
| 95 | 95 | raz --version |
| @@ -116,7 +116,9 @@ | ||
| 116 | 116 | the symbols and line numbers, the places a failure is told by, and the |
| 117 | 117 | profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves |
| 118 | 118 | 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. | |
| 120 | 122 | |
| 121 | 123 | At run time, a failed check says where it is and what failed, as in |
| 122 | 124 | `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends |
| @@ -21,12 +21,12 @@ | ||
| 21 | 21 | | `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout | |
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `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 | | |
| 25 | 25 | | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls | |
| 26 | 26 | | `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `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 | | |
| 30 | 30 | |
| 31 | 31 | Every file opens with a comment that says what it is, what it depends |
| 32 | 32 | on and what it leaves out. Read those first; they are the map. |
| @@ -1751,6 +1751,16 @@ | ||
| 1751 | 1751 | one starts at a `_start` that calls `__libc_start_main` the way C's own |
| 1752 | 1752 | start-up does, which sets C up and calls back a `main` that runs the |
| 1753 | 1753 | 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. | |
| 1754 | 1764 | |
| 1755 | 1765 | `raz -S` lists the code (`X64.list`) instead of writing |
| 1756 | 1766 | it. `raz run` writes the executable to a memfd, a file only in memory, and |
| @@ -107,6 +107,18 @@ | ||
| 107 | 107 | .Cm 0FFFFFFFF80000000H . |
| 108 | 108 | The program must lie wholly in the low or the top 2 GB of the address |
| 109 | 109 | 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. | |
| 110 | 122 | .It Fl noprofile |
| 111 | 123 | Leave out the profiler, which |
| 112 | 124 | .Ev RAZ_PROFILE |
| @@ -17,7 +17,9 @@ | ||
| 17 | 17 | placer: a failure is told by its address, and calls and place, |
| 18 | 18 | which would need the places, are left out the same way. |
| 19 | 19 | 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. *) | |
| 21 | 23 | module Rt; |
| 22 | 24 | import Sys; |
| 23 | 25 |
| @@ -2419,8 +2419,14 @@ | ||
| 2419 | 2419 | library's, in a program that uses C. exitProc: the chunk of Rt's exit. The |
| 2420 | 2420 | first numLinks of links: the addresses Rt is given, of what the program |
| 2421 | 2421 | 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; | |
| 2424 | 2430 | var entry, start, i: INT; m: ^Sym.Module; |
| 2425 | 2431 | begin |
| 2426 | 2432 | entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); |
| @@ -2450,6 +2456,9 @@ | ||
| 2450 | 2456 | X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); |
| 2451 | 2457 | X.store(X.memRip(cLinks[i].into, 0), RAX, 8); |
| 2452 | 2458 | end; |
| 2459 | + elsif bare then | |
| 2460 | + start := entry; | |
| 2461 | + Chunk.beginChunk(entry); | |
| 2453 | 2462 | else |
| 2454 | 2463 | start := entry; |
| 2455 | 2464 | Chunk.beginChunk(entry); |
| @@ -2464,7 +2473,7 @@ | ||
| 2464 | 2473 | end; |
| 2465 | 2474 | m := first; |
| 2466 | 2475 | 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; | |
| 2468 | 2477 | m := m^.next; |
| 2469 | 2478 | end; |
| 2470 | 2479 | if mainObj # nil then X.callChunk(mainObj^.chunk); end; |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | (* Raz: the command line. |
| 2 | 2 | |
| 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 | |
| 4 | 4 | raz run [-I dir]... Main.raz [arg]... |
| 5 | 5 | raz --version |
| 6 | 6 | |
| @@ -20,7 +20,8 @@ | ||
| 20 | 20 | C up and calls this as C's main; first it gives Rt C's exit, which |
| 21 | 21 | Rt.exit then ends the program with. The C library is glibc, or musl |
| 22 | 22 | 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). | |
| 24 | 25 | |
| 25 | 26 | Depends on: everything else, and Version, a module the build makes |
| 26 | 27 | (build/src/Version.raz) from build/version.inc: what git describe |
| @@ -134,6 +135,8 @@ | ||
| 134 | 135 | Out.str(" -base addr loads the program at addr, not 400000H: a multiple of 4096\n"); |
| 135 | 136 | Out.str(" in the low or the top 2 GB, as 0FFFFFFFF80000000H\n"); |
| 136 | 137 | 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"); | |
| 137 | 140 | Out.str(" -libc name links a program that uses C against glibc or musl; by\n"); |
| 138 | 141 | Out.str(" default glibc, or musl where only its loader is there\n"); |
| 139 | 142 | Out.str(" -h, --help says this\n"); |
| @@ -200,11 +203,12 @@ | ||
| 200 | 203 | path, outPath, s: []U8; dir: []U8; |
| 201 | 204 | i, first, entry, fd, last, errno, base: INT; |
| 202 | 205 | 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; | |
| 204 | 207 | begin |
| 205 | 208 | path := ""; |
| 206 | 209 | outPath := ""; |
| 207 | 210 | noProfile := false; |
| 211 | + bare := false; | |
| 208 | 212 | listing := false; |
| 209 | 213 | libcSaid := false; |
| 210 | 214 | run := (ARGC() >= 2) and (Files.arg(1) = "run"); |
| @@ -234,6 +238,8 @@ | ||
| 234 | 238 | end; |
| 235 | 239 | elsif s = "-noprofile" then |
| 236 | 240 | noProfile := true; |
| 241 | + elsif s = "-bare" then | |
| 242 | + bare := true; | |
| 237 | 243 | elsif s = "-libc" then |
| 238 | 244 | INC(i); |
| 239 | 245 | s := Files.arg(i); |
| @@ -304,7 +310,8 @@ | ||
| 304 | 310 | (* what Rt is given: the places, which the linker makes, for Rt to say |
| 305 | 311 | where a failed check or a signal was (place), and the calls under |
| 306 | 312 | 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. *) | |
| 308 | 315 | numLinks := 0; |
| 309 | 316 | if not Elf.stripped then |
| 310 | 317 | Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); |
| @@ -312,12 +319,13 @@ | ||
| 312 | 319 | addLink(Link.placesChunk, objIn(rt, "places")); |
| 313 | 320 | addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); |
| 314 | 321 | 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; | |
| 316 | 323 | end; |
| 317 | 324 | (* what a program that uses C is given of C: Rt its exit, and |
| 318 | 325 | Thread, if the program has it, the calls that make and join C's |
| 319 | 326 | threads, so that the C library knows of them *) |
| 320 | 327 | dynamic := usesC(); |
| 328 | + if dynamic and bare then Diag.fatal("-bare: the program uses C, which needs Linux and its loader"); end; | |
| 321 | 329 | numCLinks := 0; |
| 322 | 330 | if dynamic then |
| 323 | 331 | addCLink("exit", objIn(rt, "cExit")); |
| @@ -327,7 +335,7 @@ | ||
| 327 | 335 | addCLink("pthread_join", objIn(thread, "cJoin")); |
| 328 | 336 | end; |
| 329 | 337 | 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, | |
| 331 | 339 | objIn(rt, "exit")^.chunk, links[0..numLinks], numLinks); |
| 332 | 340 | |
| 333 | 341 | Link.markLive(entry); |
| @@ -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. |
| @@ -137,6 +137,16 @@ | ||
| 137 | 137 | if [ $? = 0 ] && cmp -s $out/Index.out $out/IndexB.out && grep -q 'Index.main' $out/IndexB.out \ |
| 138 | 138 | && entry $out/IndexB | grep -q '^000000007fe' && entry $out/IndexK | grep -q '^ffffffff800'; then pass=$((pass + 1)) |
| 139 | 139 | 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 | |
| 140 | 150 | |
| 141 | 151 | # raz -S: test/list/X.raz lists as X.expect |
| 142 | 152 | for f in test/list/*.raz; do |
| @@ -0,0 +1 @@ | ||
| 1 | +-bare |
| @@ -0,0 +1 @@ | ||
| 1 | +raz: -bare: the program uses C, which needs Linux and its loader |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module BareC; | |
| 2 | +extern proc getpid(): I32; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + _ := getpid(); | |
| 6 | +end; | |
| 7 | +end BareC. |