| | @@ -1056,3 +1056,64 @@ |
| 1056 | 1056 | Met in the discussion of `const` parameters (2026-10-17): "If a const |
| 1057 | 1057 | can still be written, what's the point?", and "Can records have |
| 1058 | 1058 | constant slices?" |
| 1059 | + |
| 1060 | +## For a kernel: three changes to the compiler |
| 1061 | + |
| 1062 | +Not the language's but this compiler's, kept together since they serve |
| 1063 | +one end: a raz program run on the bare machine, a unikernel, with no |
| 1064 | +operating system under it. None is needed for one: a kernel can be |
| 1065 | +made today, with a boot stub of its own that enters long mode, `db` |
| 1066 | +for the instructions the assembler lacks, and either a library of its |
| 1067 | +own beside a copy of `raz` (the library is read from beside the |
| 1068 | +compiler) or a handler for `syscall` that does what `Rt` and `Sys` ask |
| 1069 | +of Linux. Each piece below takes away one of those work-arounds, and |
| 1070 | +stands on its own. |
| 1071 | + |
| 1072 | +**1. A load address chosen at the command line.** The executable is |
| 1073 | +loaded at `Link.Base`, 400000H, fixed. A kernel is often wanted |
| 1074 | +elsewhere: Limine wants one in the top 2 GB of the address space, and |
| 1075 | +a kernel that maps itself may want room below it. The proposal: |
| 1076 | +`raz -base ADDR`, a multiple of the page, and `Link.Base` a variable |
| 1077 | +it sets. Code reaches its data relative to `rip` already; what holds |
| 1078 | +an address whole (`Chunk.addAbsFixup`) is patched by the linker, which |
| 1079 | +knows the base, so the cost is the option and the check that an |
| 1080 | +address above 2 GB, as a higher-half kernel's, still fits where the |
| 1081 | +code needs 32 bits of it. A few lines. |
| 1082 | + |
| 1083 | +**2. A program with no `Rt` body.** `Rt` is always used, and its body |
| 1084 | +runs before every other module's: it sets the handlers of SIGSEGV and |
| 1085 | +SIGFPE with `rt_sigaction` and a stack for them with `sigaltstack`, |
| 1086 | +and starts the profiler with `setitimer` when asked. On the bare |
| 1087 | +machine those `syscall`s fault (`EFER.SCE` is clear), and with no IDT |
| 1088 | +yet the machine resets. The entry point also reads `argc` and `argv` |
| 1089 | +from the stack as Linux leaves them. The proposal: `raz -bare`, under |
| 1090 | +which the entry point reads no arguments and calls no body of `Rt`, and |
| 1091 | +gives `Rt` nothing (as `-s` does), so that `fail` and `exit` are all |
| 1092 | +that is reached of it; a kernel's own module, the first used, sets up |
| 1093 | +what `Rt`'s body would. `exit` and `fail` still make system calls, |
| 1094 | +which a kernel would catch, or replace by a library of its own as |
| 1095 | +today; `-bare` only stops the ones made before the program can. About |
| 1096 | +twenty lines in `Raz.raz` and `Gen.entryPoint`. |
| 1097 | + |
| 1098 | +**3. The privileged instructions in the assembler.** `X64.raz` |
| 1099 | +assembles what the code generator uses, and `db` gives the rest. A |
| 1100 | +kernel's code procedures are mostly the rest: `cli`, `sti`, `iretq`, |
| 1101 | +`swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`, `mov` to and from `cr0`, |
| 1102 | +`cr2`, `cr3`, `cr4`, `rdmsr`, `wrmsr`, `in` and `out`, `wbinvd`, |
| 1103 | +`xsave` and `xrstor`. Written as bytes they can't be read, and `raz -S` |
| 1104 | +can't name them. The proposal: these instructions in the assembler, |
| 1105 | +most a row of `Plain` (`cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, |
| 1106 | +`wrmsr`, `wbinvd`), the rest a few lines each, each checked against |
| 1107 | +`objdump` as the others were. 40 to 60 lines of the budget. It breaks |
| 1108 | +nothing: a word the assembler refused before now assembles. |
| 1109 | + |
| 1110 | +What is still left to a kernel's own code, with all three: the boot |
| 1111 | +stub, since no common loader enters an ELF64 file in long mode but |
| 1112 | +Firecracker (GRUB's Multiboot2 and QEMU's PVH enter in 32-bit mode, |
| 1113 | +and the assembler makes only 64-bit code); interrupt stubs that save |
| 1114 | +the XMM registers, since the compiler's code uses them; and, if the |
| 1115 | +compiler's code ever writes below `rsp`, which isn't yet checked, an |
| 1116 | +IST stack for each interrupt. |
| 1117 | + |
| 1118 | +Met in the question "how difficult to create a simple amd64 unikernel |
| 1119 | +in raz?" (2026-10-17). |