From 7624e35dc0da1f77400ce165b40bbdd4ef3b46c6 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 20:04:00 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 1baf5d0..6b85d8c 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -1073,28 +1073,20 @@ `raz -base ADDR`, a multiple of the page, the program wholly in the low or the top 2 GB (`doc/compiler.md`, the linker). -**2. A program with no `Rt` body.** `Rt` is always used, and its body -runs before every other module's: it sets the handlers of SIGSEGV and -SIGFPE with `rt_sigaction` and a stack for them with `sigaltstack`, -and starts the profiler with `setitimer` when asked. On the bare -machine those `syscall`s fault (`EFER.SCE` is clear), and with no IDT -yet the machine resets. The entry point also reads `argc` and `argv` -from the stack as Linux leaves them. The proposal: `raz -bare`, under -which the entry point reads no arguments and calls no body of `Rt`, and -gives `Rt` nothing (as `-s` does), so that `fail` and `exit` are all -that is reached of it; a kernel's own module, the first used, sets up -what `Rt`'s body would. `exit` and `fail` still make system calls, -which a kernel would catch, or replace by a library of its own as -today; `-bare` only stops the ones made before the program can. About -twenty lines in `Raz.raz` and `Gen.entryPoint`. +**2. A program with no `Rt` body.** Done (2026-10-17): `raz -bare` +reads no `argc` and `argv`, runs no body of `Rt` and gives it no +profiler. It still gives `Rt` the places, by stores alone, so that a +failed check says where it was, which the proposal had left out as +`-s` does (`doc/compiler.md`, the command line). **3. The privileged instructions in the assembler.** Done (2026-10-17): `cli`, `sti`, `iretq`, `swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`, `mov` to and from the control registers, `rdmsr`, `wrmsr`, `in`, `out`, `wbinvd`, `xsave` and `xrstor` (`doc/compiler.md`, the assembler). -What is still left to a kernel's own code, with all three: the boot -stub, since no common loader enters an ELF64 file in long mode but +What is still left to a kernel's own code, with all three: zeroing +the bss and a stack aligned to 16 bytes before `_start`, as Linux +gives; the boot stub, since no common loader enters an ELF64 file in long mode but Firecracker (GRUB's Multiboot2 and QEMU's PVH enter in 32-bit mode, and the assembler makes only 64-bit code); interrupt stubs that save the XMM registers, since the compiler's code uses them; and, if the diff --git a/README.md b/README.md index e9526a3..94bb70f 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ ## Using it - raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] [-libc glibc|musl] [-base addr] [-bare] Main.raz raz run [option]... Main.raz [arg]... raz -h raz --version @@ -116,7 +116,9 @@ the symbols and line numbers, the places a failure is told by, and the profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves out only the profiler. `-base addr` loads the program at `addr`, not -400000H, in the low or the top 2 GB, as a kernel may want. +400000H, in the low or the top 2 GB, as a kernel may want, and +`-bare` makes one for the bare machine: no arguments are read and no +signal handlers set. At run time, a failed check says where it is and what failed, as in `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends diff --git a/doc/compiler.md b/doc/compiler.md index a8d7f94..6f04d3c 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -21,12 +21,12 @@ | `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 361 | 283 | The command line | +| `src/Raz.raz` | 369 | 289 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -1751,6 +1751,16 @@ one starts at a `_start` that calls `__libc_start_main` the way C's own start-up does, which sets C up and calls back a `main` that runs the module bodies and the program's `main`. + +`raz -bare` makes a program for the bare machine, a kernel, with no +Linux under it: its `_start` reads no `argc` and `argv`, which only +Linux leaves on the stack, and calls no body of `Rt`, whose +`rt_sigaction` and `sigaltstack` would fault there (`EFER.SCE` is +clear) and, with no IDT yet, reset the machine. Nor is `Rt` given the +profiler, which its body would start. It is still given the places, by +stores alone, so a failed check says where it was; but `Rt.fail` and +`Rt.exit` end in system calls, which the kernel catches or replaces. A +program that uses C is refused. `raz -S` lists the code (`X64.list`) instead of writing it. `raz run` writes the executable to a memfd, a file only in memory, and diff --git a/doc/raz.1 b/doc/raz.1 index a4e1321..34c7d6b 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -107,6 +107,18 @@ .Cm 0FFFFFFFF80000000H . The program must lie wholly in the low or the top 2 GB of the address space, which a 32-bit address reaches. +.It Fl bare +Make a program for the bare machine, a kernel, with no Linux under it. +Its entry point reads no arguments, so +.Fn ARGC +is 0, and runs no body of +.Cm Rt , +whose system calls set the handlers of signals: a nil pointer or a +division fault is not told. +A failed check is still told, by a system call the kernel must catch. +Its stack must be 16-byte aligned, as Linux leaves it, and its bss +zeroed. +A program that uses C is refused. .It Fl noprofile Leave out the profiler, which .Ev RAZ_PROFILE diff --git a/lib/Rt.raz b/lib/Rt.raz index 5a8b2ad..2576481 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -17,7 +17,9 @@ placer: a failure is told by its address, and calls and place, which would need the places, are left out the same way. The handlers are Linux's on x86-64, set by system calls: what the - kernel hands them is laid out as it is there. *) + kernel hands them is laid out as it is there. A program built with + raz -bare, for the bare machine, runs no body of Rt, so has no + handlers and no profiler. *) module Rt; import Sys; diff --git a/src/Gen.raz b/src/Gen.raz index b619349..0bdcbfc 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -2419,8 +2419,14 @@ library's, in a program that uses C. exitProc: the chunk of Rt's exit. The first numLinks of links: the addresses Rt is given, of what the program has: the places, and the profiler and calls; what it isn't given, - nothing reaches, and the linker leaves out. *) -proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; + nothing reaches, and the linker leaves out. + + bare: raz -bare, a program for the bare machine, a kernel's, with + no Linux under it. Its entry point reads no argc and argv, which + only Linux leaves on the stack, so that ARGC() is 0; and runs no body + of Rt, whose system calls, to set the signals' handlers, would fault + there. A kernel's own module sets up what Rt's body would. *) +proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; var entry, start, i: INT; m: ^Sym.Module; begin entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); @@ -2450,6 +2456,9 @@ X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); X.store(X.memRip(cLinks[i].into, 0), RAX, 8); end; + elsif bare then + start := entry; + Chunk.beginChunk(entry); else start := entry; Chunk.beginChunk(entry); @@ -2464,7 +2473,7 @@ end; m := first; while m # nil do - if m^.used and (m^.body >= 0) then X.callChunk(m^.body); end; + if m^.used and (m^.body >= 0) and not (bare and (m^.name = internStr("Rt"))) then X.callChunk(m^.body); end; m := m^.next; end; if mainObj # nil then X.callChunk(mainObj^.chunk); end; diff --git a/src/Raz.raz b/src/Raz.raz index 25b55c8..6aab3dc 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -1,6 +1,6 @@ (* Raz: the command line. - raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] [-bare] Main.raz raz run [-I dir]... Main.raz [arg]... raz --version @@ -20,7 +20,8 @@ C up and calls this as C's main; first it gives Rt C's exit, which Rt.exit then ends the program with. The C library is glibc, or musl on a machine that has musl's loader and not glibc's, unless -libc - names one. + names one. Under -bare, for the bare machine, it saves no argc and + argv and runs no body of Rt (Gen.entryPoint). Depends on: everything else, and Version, a module the build makes (build/src/Version.raz) from build/version.inc: what git describe @@ -134,6 +135,8 @@ Out.str(" -base addr loads the program at addr, not 400000H: a multiple of 4096\n"); Out.str(" in the low or the top 2 GB, as 0FFFFFFFF80000000H\n"); Out.str(" -noprofile leaves out the profiler, which RAZ_PROFILE turns on\n"); + Out.str(" -bare for the bare machine, a kernel: reads no argc and argv, and\n"); + Out.str(" runs no body of Rt, which sets the signals' handlers\n"); Out.str(" -libc name links a program that uses C against glibc or musl; by\n"); Out.str(" default glibc, or musl where only its loader is there\n"); Out.str(" -h, --help says this\n"); @@ -200,11 +203,12 @@ path, outPath, s: []U8; dir: []U8; i, first, entry, fd, last, errno, base: INT; m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; - run, noProfile, listing, dynamic, libcSaid, ok: BOOL; err: ERROR; + run, noProfile, listing, dynamic, libcSaid, ok, bare: BOOL; err: ERROR; begin path := ""; outPath := ""; noProfile := false; + bare := false; listing := false; libcSaid := false; run := (ARGC() >= 2) and (Files.arg(1) = "run"); @@ -234,6 +238,8 @@ end; elsif s = "-noprofile" then noProfile := true; + elsif s = "-bare" then + bare := true; elsif s = "-libc" then INC(i); s := Files.arg(i); @@ -304,7 +310,8 @@ (* what Rt is given: the places, which the linker makes, for Rt to say where a failed check or a signal was (place), and the calls under way then (calls); and the profiler, which needs them too. raz -s - gives none, -noprofile no profiler. *) + gives none, -noprofile no profiler, and -bare no profiler, since + Rt's body, which starts it, doesn't run. *) numLinks := 0; if not Elf.stripped then Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); @@ -312,12 +319,13 @@ addLink(Link.placesChunk, objIn(rt, "places")); addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer")); - if not noProfile then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; + if not noProfile and not bare then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; end; (* what a program that uses C is given of C: Rt its exit, and Thread, if the program has it, the calls that make and join C's threads, so that the C library knows of them *) dynamic := usesC(); + if dynamic and bare then Diag.fatal("-bare: the program uses C, which needs Linux and its loader"); end; numCLinks := 0; if dynamic then addCLink("exit", objIn(rt, "cExit")); @@ -327,7 +335,7 @@ addCLink("pthread_join", objIn(thread, "cJoin")); end; end; - entry := Gen.entryPoint(dynamic, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks, + entry := Gen.entryPoint(dynamic, bare, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks, objIn(rt, "exit")^.chunk, links[0..numLinks], numLinks); Link.markLive(entry); diff --git a/test/bare/Bare.raz b/test/bare/Bare.raz new file mode 100644 index 0000000..99cfa60 --- /dev/null +++ b/test/bare/Bare.raz @@ -0,0 +1,17 @@ +module Bare; + +(* For raz -bare: no argc and argv are read, so ARGC() is 0; and Rt's + body, which sets the signals' handlers, doesn't run, so a nil pointer + ends the program by SIGSEGV with nothing said. *) +import Out; + +proc main*; +var p: ^INT; +begin + Out.int(ARGC()); Out.ln; + Out.flush(); + p := nil; + p^ := 1; +end; + +end Bare. diff --git a/test/check.sh b/test/check.sh index 5dd63b8..ec70a80 100755 --- a/test/check.sh +++ b/test/check.sh @@ -137,6 +137,16 @@ if [ $? = 0 ] && cmp -s $out/Index.out $out/IndexB.out && grep -q 'Index.main' $out/IndexB.out \ && entry $out/IndexB | grep -q '^000000007fe' && entry $out/IndexK | grep -q '^ffffffff800'; then pass=$((pass + 1)) else echo "FAIL raz -base:"; cat $out/IndexB.out; fail=$((fail + 1)); fi +# raz -bare: no argc read, so ARGC() is 0, and no handlers set, so a nil +# pointer stops the program by SIGSEGV with nothing said; a failed +# check is still told by its place, and nothing is profiled +$raz -bare -o $out/Bare test/bare/Bare.raz +status=$( { ( exec $out/Bare one two ) > $out/Bare.out 2>$out/Bare.err; echo $?; } 2>/dev/null ) +$raz -bare -o $out/IndexBare test/strip/Index.raz +RAZ_PROFILE=1 $out/IndexBare > $out/IndexBare.out 2>&1 +if [ $status = 139 ] && [ "$(cat $out/Bare.out)" = 0 ] && [ ! -s $out/Bare.err ] \ + && cmp -s $out/Index.out $out/IndexBare.out; then pass=$((pass + 1)) +else echo "FAIL raz -bare: status $status"; cat $out/Bare.out $out/Bare.err $out/IndexBare.out; fail=$((fail + 1)); fi # raz -S: test/list/X.raz lists as X.expect for f in test/list/*.raz; do diff --git a/test/err/BareC.args b/test/err/BareC.args new file mode 100644 index 0000000..58bcbee --- /dev/null +++ b/test/err/BareC.args @@ -0,0 +1 @@ +-bare diff --git a/test/err/BareC.err b/test/err/BareC.err new file mode 100644 index 0000000..064e0c0 --- /dev/null +++ b/test/err/BareC.err @@ -0,0 +1 @@ +raz: -bare: the program uses C, which needs Linux and its loader diff --git a/test/err/BareC.raz b/test/err/BareC.raz new file mode 100644 index 0000000..84acc3c --- /dev/null +++ b/test/err/BareC.raz @@ -0,0 +1,7 @@ +module BareC; +extern proc getpid(): I32; +proc main*; +begin + _ := getpid(); +end; +end BareC.