From c57f87ee623f1a1b59d140cd3abcf23966b5d4bb Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:49:28 +0000 Subject: [PATCH] The assembler knows a kernel's instructions cli, sti, iretq, swapgs, rdmsr, wrmsr and wbinvd in Plain; lgdt, lidt, invlpg, xsave and xrstor of one memory operand (MemOps); ltr of a 16-bit register; mov to and from cr0, cr2, cr3, cr4 and cr8, an operand of its own kind (oCr); in and out of al, ax or eax and a port, a byte or dx. Each checked against GNU as; test/run/AsmKernel reads their bytes back. The first of PROPOSALS.md's three changes for a kernel. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 21f1bbf..7293c87 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -1095,17 +1095,10 @@ today; `-bare` only stops the ones made before the program can. About twenty lines in `Raz.raz` and `Gen.entryPoint`. -**3. The privileged instructions in the assembler.** `X64.raz` -assembles what the code generator uses, and `db` gives the rest. A -kernel's code procedures are mostly the rest: `cli`, `sti`, `iretq`, -`swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`, `mov` to and from `cr0`, -`cr2`, `cr3`, `cr4`, `rdmsr`, `wrmsr`, `in` and `out`, `wbinvd`, -`xsave` and `xrstor`. Written as bytes they can't be read, and `raz -S` -can't name them. The proposal: these instructions in the assembler, -most a row of `Plain` (`cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, -`wrmsr`, `wbinvd`), the rest a few lines each, each checked against -`objdump` as the others were. 40 to 60 lines of the budget. It breaks -nothing: a word the assembler refused before now assembles. +**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 diff --git a/doc/compiler.md b/doc/compiler.md index 6150509..01ab221 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -20,7 +20,7 @@ | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | | `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,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | +| `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/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 | @@ -1044,7 +1044,14 @@ `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside them, and `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`, -`popcnt`). Labels are chains of jumps, as below. `db` gives bytes it +`popcnt`). A kernel's instructions are there too, though the code +generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, +`wrmsr` and `wbinvd` in `Plain`; `lgdt`, `lidt`, `invlpg`, `xsave` and +`xrstor`, of one memory operand, in `MemOps`; `ltr` of a 16-bit +register; `mov` to and from `cr0`, `cr2`, `cr3`, `cr4` and `cr8` (an +operand of its own kind, `oCr`); and `in` and `out`, of `al`, `ax` or +`eax` and a port, a byte or `dx`. Each was checked against GNU `as`, and +`test/run/AsmKernel.raz` reads their bytes back. Labels are chains of jumps, as below. `db` gives bytes it doesn't know. An error in an instruction is returned as an `ERROR`, which `assemble` passes up. An offset that doesn't fit in 32 bits (`mem`) can't be returned, since an operand has nowhere to give it: it diff --git a/src/X64.raz b/src/X64.raz index 8f3727e..1b047af 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -906,10 +906,10 @@ NoBase = -3; (* a memory operand's base, not yet read *) type - OpKind = (oReg, oXmm, oMem, oImm); + OpKind = (oReg, oXmm, oCr, oMem, oImm); Operand = record kind: OpKind; - r, size: INT; (* oReg, oXmm: the register and its size; oMem: its size, or 0 *) + r, size: INT; (* oReg, oXmm, oCr: the register and its size; oMem: its size, or 0 *) m: Mem; v: I64; (* oImm *) end; @@ -927,6 +927,7 @@ | sDiv: return "div"; | sShl: return "shl"; | sShr: return "shr"; + | sIn: return "in"; | else: return ""; end; end; @@ -1047,6 +1048,10 @@ or not ((o^.m.scale = 1) or (o^.m.scale = 2) or (o^.m.scale = 4) or (o^.m.scale = 8)) then return Scan.mark("expected a memory operand, [base + index * scale + disp]"); end; + Scan.next; + elsif (w = "cr0") or (w = "cr2") or (w = "cr3") or (w = "cr4") or (w = "cr8") then + (* a control register, for mov to and from a general one *) + o^.kind := oCr; o^.r := INT(w[2]) - 48; o^.size := 8; Scan.next; elsif isReg(w, ADR(o^.r), ADR(o^.size)) then if o^.size = 16 then o^.kind := oXmm; else o^.kind := oReg; end; @@ -1089,10 +1094,18 @@ BitOps = " bsf bsr tzcnt lzcnt popcnt "; BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; BitCodes: [5]INT = [0BC0FH, 0BD0FH, 0BC0FH, 0BD0FH, 0B80FH]; - Plain: [15][]U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", - "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence"]; - PlainOp: [15]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, - 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH]; + (* the instructions of no operands; the last seven are a kernel's *) + Plain: [22][]U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", + "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence", + "cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd"]; + PlainOp: [22]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, + 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH, + 0FAH, 0FBH, 0CF48H, 0F8010FH, 320FH, 300FH, 090FH]; + (* the privileged instructions of one memory operand, 0F 01 /digit + but for xsave and xrstor, 0F AE /digit *) + MemOps = " lgdt lidt invlpg xsave xrstor "; + MemCodes: [5]INT = [010FH, 010FH, 010FH, 0AE0FH, 0AE0FH]; + MemDigits: [5]INT = [2, 3, 7, 4, 5]; var (* the instruction being assembled *) @@ -1183,6 +1196,17 @@ k := wordIndex(aw, Alus); return two(k * 8 + 1, k * 8 + 3, 81H, k); elsif aw = "mov" then + (* to a control register, 0F 22, and from one, 0F 20: the control + register in ModRM's reg, always 64 bits wide with no REX.W *) + if (an = 2) and (ao[0].kind = oCr) and (ao[1].kind = oReg) and awide then + opR(0, false, 220FH, ao[0].r, ao[1].r, false); + return nil; + elsif (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oCr) and awide then + opR(0, false, 200FH, ao[1].r, ao[0].r, false); + return nil; + elsif (an = 2) and ((ao[0].kind = oCr) or (ao[1].kind = oCr)) then + return bad(); + end; if (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oImm) and (asize >= 4) then if awide then movRI(ao[0].r, ao[1].v); @@ -1290,6 +1314,32 @@ if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) or (asize < 4) then return bad(); end; k := wordIndex(aw, BitOps); rm(BitPfx[k], awide, BitCodes[k], ao[0].r, ao[1], false); + elsif wordIndex(aw, MemOps) >= 0 then + if (an # 1) or (ao[0].kind # oMem) then return bad(); end; + k := wordIndex(aw, MemOps); + rm(0, false, MemCodes[k], MemDigits[k], ao[0], false); + elsif aw = "ltr" then + (* 0F 00 /3 with a 16-bit register: written out, since opcode + ends an opcode at its first zero byte *) + if (an # 1) or (ao[0].kind # oReg) or (asize # 2) then return bad(); end; + prefix(0, false, 0, NoIndex, ao[0].r, false); + Chunk.put1(0FH); Chunk.put1(0); Chunk.put1(0C0H | (3 shl 3) | (ao[0].r & 7)); + elsif (aw = "in") or (aw = "out") then + (* in al, ax or eax from a port, a byte or dx: E4 or EC; out the + other way, E6 or EE; one more for a word or more. The size is + the register's, not the first operand's, which is dx for out *) + if aw = "in" then k := 1; else k := 0; end; (* the port's operand *) + if (an # 2) or (ao[1 - k].kind # oReg) or (ao[1 - k].r # RAX) or (ao[1 - k].size = 8) then return bad(); end; + if ao[1 - k].size = 2 then Chunk.put1(66H); end; + if ao[k].kind = oImm then + if (ao[k].v < 0) or (ao[k].v > 255) then return bad(); end; + Chunk.put1(0E4H + 2 * (1 - k) + bit(ao[1 - k].size # 1)); + Chunk.put1(INT(ao[k].v)); + elsif (ao[k].kind = oReg) and (ao[k].r = RDX) and (ao[k].size = 2) then + Chunk.put1(0ECH + 2 * (1 - k) + bit(ao[1 - k].size # 1)); + else + return bad(); + end; elsif (aw = "movsd") or (aw = "movss") then if aw = "movsd" then k := 0F2H; else k := 0F3H; end; if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then @@ -1325,8 +1375,8 @@ rm(realPfx(aw = "cvtss2sd"), false, 5A0FH, ao[0].r, ao[1], false); else k := 0; - while (k <= 14) and (Plain[k] # aw) do INC(k); end; - if (k > 14) or (an # 0) then + while (k < LEN(Plain)) and (Plain[k] # aw) do INC(k); end; + if (k = LEN(Plain)) or (an # 0) then return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)])); end; opcode(PlainOp[k]); diff --git a/test/err/AsmControl.err b/test/err/AsmControl.err new file mode 100644 index 0000000..42e52db --- /dev/null +++ b/test/err/AsmControl.err @@ -0,0 +1,4 @@ +test/err/AsmControl.raz:4:5: error: can't assemble mov with these operands + 4 | mov eax, cr0; + | ^^^ +raz: 1 error diff --git a/test/err/AsmControl.raz b/test/err/AsmControl.raz new file mode 100644 index 0000000..0109b97 --- /dev/null +++ b/test/err/AsmControl.raz @@ -0,0 +1,7 @@ +module AsmControl; +proc f; +code + mov eax, cr0; + ret; +end f; +end AsmControl. diff --git a/test/run/AsmKernel.expect b/test/run/AsmKernel.expect new file mode 100644 index 0000000..0a37d3d --- /dev/null +++ b/test/run/AsmKernel.expect @@ -0,0 +1,36 @@ +cli: FA +sti: FB +iretq: 48 CF +swapgs: 0F 01 F8 +rdmsr: 0F 32 +wrmsr: 0F 30 +wbinvd: 0F 09 +lgdt [rdi]: 0F 01 17 +lidt [rsi + 8]: 0F 01 5E 08 +invlpg [rax]: 0F 01 38 +invlpg [r12 + rcx * 8 + 400]: 41 0F 01 BC CC 90 01 00 00 +xsave [rdi]: 0F AE 27 +xrstor [r9]: 41 0F AE 29 +ltr ax: 0F 00 D8 +ltr r10w: 41 0F 00 DA +mov cr0, rax: 0F 22 C0 +mov cr2, rdx: 0F 22 D2 +mov cr3, r15: 41 0F 22 DF +mov cr4, rcx: 0F 22 E1 +mov cr8, rax: 44 0F 22 C0 +mov cr8, r11: 45 0F 22 C3 +mov rax, cr0: 0F 20 C0 +mov r9, cr3: 41 0F 20 D9 +mov rbx, cr8: 44 0F 20 C3 +in al, 60H: E4 60 +in ax, 61H: 66 E5 61 +in eax, 0FFH: E5 FF +in al, dx: EC +in ax, dx: 66 ED +in eax, dx: ED +out 80H, al: E6 80 +out 80H, ax: 66 E7 80 +out 0, eax: E7 00 +out dx, al: EE +out dx, ax: 66 EF +out dx, eax: EF diff --git a/test/run/AsmKernel.raz b/test/run/AsmKernel.raz new file mode 100644 index 0000000..f9f420f --- /dev/null +++ b/test/run/AsmKernel.raz @@ -0,0 +1,79 @@ +module AsmKernel; +(* A kernel's instructions in the assembler: each one's bytes, read + back from the procedure they are made in, since they can't run in + user mode. Each was checked against GNU as. *) +import Out; + +const + Count = 36; + Names: [Count][]U8 = ["cli", "sti", "iretq", "swapgs", + "rdmsr", "wrmsr", "wbinvd", "lgdt [rdi]", + "lidt [rsi + 8]", "invlpg [rax]", "invlpg [r12 + rcx * 8 + 400]", "xsave [rdi]", + "xrstor [r9]", "ltr ax", "ltr r10w", "mov cr0, rax", + "mov cr2, rdx", "mov cr3, r15", "mov cr4, rcx", "mov cr8, rax", + "mov cr8, r11", "mov rax, cr0", "mov r9, cr3", "mov rbx, cr8", + "in al, 60H", "in ax, 61H", "in eax, 0FFH", "in al, dx", + "in ax, dx", "in eax, dx", "out 80H, al", "out 80H, ax", + "out 0, eax", "out dx, al", "out dx, ax", "out dx, eax"]; + Sizes: [Count]INT = [1, 1, 2, 3, 2, 2, 2, 3, 4, 3, 9, 3, + 4, 3, 4, 3, 3, 4, 3, 4, 4, 3, 4, 4, + 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1]; + +proc kernel; +code + cli; + sti; + iretq; + swapgs; + rdmsr; + wrmsr; + wbinvd; + lgdt [rdi]; + lidt [rsi + 8]; + invlpg [rax]; + invlpg [r12 + rcx * 8 + 400]; + xsave [rdi]; + xrstor [r9]; + ltr ax; + ltr r10w; + mov cr0, rax; + mov cr2, rdx; + mov cr3, r15; + mov cr4, rcx; + mov cr8, rax; + mov cr8, r11; + mov rax, cr0; + mov r9, cr3; + mov rbx, cr8; + in al, 60H; + in ax, 61H; + in eax, 0FFH; + in al, dx; + in ax, dx; + in eax, dx; + out 80H, al; + out 80H, ax; + out 0, eax; + out dx, al; + out dx, ax; + out dx, eax; + ret; +end kernel; + +proc main*; +var p: proc; b: ^U8; i, j, at: INT; +begin + p := kernel; + b := ^U8(p); + at := 0; + for i := 0 to Count - 1 do + Out.str(Names[i]); Out.str(":"); + for j := 0 to Sizes[i] - 1 do + Out.str(" "); Out.hex(U64(b[at + j]), 2); + end; + Out.ln; + at := at + Sizes[i]; + end; +end; + +end AsmKernel.