raz / commit c57f87ee

commit
c57f87ee623f1a1b59d140cd3abcf23966b5d4bb
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
12154405
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.

patch · log

7 files changed, +197 -21

PROPOSALS.md modified +4 -11

@@ -1095,17 +1095,10 @@
10951095 today; `-bare` only stops the ones made before the program can. About
10961096 twenty lines in `Raz.raz` and `Gen.entryPoint`.
10971097
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.
1098+**3. The privileged instructions in the assembler.** Done (2026-10-17):
1099+`cli`, `sti`, `iretq`, `swapgs`, `lgdt`, `lidt`, `ltr`, `invlpg`,
1100+`mov` to and from the control registers, `rdmsr`, `wrmsr`, `in`, `out`,
1101+`wbinvd`, `xsave` and `xrstor` (`doc/compiler.md`, the assembler).
11091102
11101103 What is still left to a kernel's own code, with all three: the boot
11111104 stub, since no common loader enters an ELF64 file in long mode but

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

@@ -20,7 +20,7 @@
2020 | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions |
2121 | `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
23-| `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
23+| `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,552 | 1,821 | 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 |
@@ -1044,7 +1044,14 @@
10441044 `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes
10451045 at a time, with `movdqu`, `movdqa` and `pmovmskb` beside them, and
10461046 `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`,
1047-`popcnt`). Labels are chains of jumps, as below. `db` gives bytes it
1047+`popcnt`). A kernel's instructions are there too, though the code
1048+generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`,
1049+`wrmsr` and `wbinvd` in `Plain`; `lgdt`, `lidt`, `invlpg`, `xsave` and
1050+`xrstor`, of one memory operand, in `MemOps`; `ltr` of a 16-bit
1051+register; `mov` to and from `cr0`, `cr2`, `cr3`, `cr4` and `cr8` (an
1052+operand of its own kind, `oCr`); and `in` and `out`, of `al`, `ax` or
1053+`eax` and a port, a byte or `dx`. Each was checked against GNU `as`, and
1054+`test/run/AsmKernel.raz` reads their bytes back. Labels are chains of jumps, as below. `db` gives bytes it
10481055 doesn't know. An error in an instruction is returned as an `ERROR`,
10491056 which `assemble` passes up. An offset that doesn't fit in 32 bits
10501057 (`mem`) can't be returned, since an operand has nowhere to give it: it

src/X64.raz (history) modified +58 -8

@@ -906,10 +906,10 @@
906906 NoBase = -3; (* a memory operand's base, not yet read *)
907907
908908 type
909- OpKind = (oReg, oXmm, oMem, oImm);
909+ OpKind = (oReg, oXmm, oCr, oMem, oImm);
910910 Operand = record
911911 kind: OpKind;
912- r, size: INT; (* oReg, oXmm: the register and its size; oMem: its size, or 0 *)
912+ r, size: INT; (* oReg, oXmm, oCr: the register and its size; oMem: its size, or 0 *)
913913 m: Mem;
914914 v: I64; (* oImm *)
915915 end;
@@ -927,6 +927,7 @@
927927 | sDiv: return "div";
928928 | sShl: return "shl";
929929 | sShr: return "shr";
930+ | sIn: return "in";
930931 | else: return "";
931932 end;
932933 end;
@@ -1047,6 +1048,10 @@
10471048 or not ((o^.m.scale = 1) or (o^.m.scale = 2) or (o^.m.scale = 4) or (o^.m.scale = 8)) then
10481049 return Scan.mark("expected a memory operand, [base + index * scale + disp]");
10491050 end;
1051+ Scan.next;
1052+ elsif (w = "cr0") or (w = "cr2") or (w = "cr3") or (w = "cr4") or (w = "cr8") then
1053+ (* a control register, for mov to and from a general one *)
1054+ o^.kind := oCr; o^.r := INT(w[2]) - 48; o^.size := 8;
10501055 Scan.next;
10511056 elsif isReg(w, ADR(o^.r), ADR(o^.size)) then
10521057 if o^.size = 16 then o^.kind := oXmm; else o^.kind := oReg; end;
@@ -1089,10 +1094,18 @@
10891094 BitOps = " bsf bsr tzcnt lzcnt popcnt ";
10901095 BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H];
10911096 BitCodes: [5]INT = [0BC0FH, 0BD0FH, 0BC0FH, 0BD0FH, 0B80FH];
1092- Plain: [15][]U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc",
1093- "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence"];
1094- PlainOp: [15]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH,
1095- 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH];
1097+ (* the instructions of no operands; the last seven are a kernel's *)
1098+ Plain: [22][]U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc",
1099+ "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence",
1100+ "cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd"];
1101+ PlainOp: [22]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH,
1102+ 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH,
1103+ 0FAH, 0FBH, 0CF48H, 0F8010FH, 320FH, 300FH, 090FH];
1104+ (* the privileged instructions of one memory operand, 0F 01 /digit
1105+ but for xsave and xrstor, 0F AE /digit *)
1106+ MemOps = " lgdt lidt invlpg xsave xrstor ";
1107+ MemCodes: [5]INT = [010FH, 010FH, 010FH, 0AE0FH, 0AE0FH];
1108+ MemDigits: [5]INT = [2, 3, 7, 4, 5];
10961109
10971110 var
10981111 (* the instruction being assembled *)
@@ -1183,6 +1196,17 @@
11831196 k := wordIndex(aw, Alus);
11841197 return two(k * 8 + 1, k * 8 + 3, 81H, k);
11851198 elsif aw = "mov" then
1199+ (* to a control register, 0F 22, and from one, 0F 20: the control
1200+ register in ModRM's reg, always 64 bits wide with no REX.W *)
1201+ if (an = 2) and (ao[0].kind = oCr) and (ao[1].kind = oReg) and awide then
1202+ opR(0, false, 220FH, ao[0].r, ao[1].r, false);
1203+ return nil;
1204+ elsif (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oCr) and awide then
1205+ opR(0, false, 200FH, ao[1].r, ao[0].r, false);
1206+ return nil;
1207+ elsif (an = 2) and ((ao[0].kind = oCr) or (ao[1].kind = oCr)) then
1208+ return bad();
1209+ end;
11861210 if (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oImm) and (asize >= 4) then
11871211 if awide then
11881212 movRI(ao[0].r, ao[1].v);
@@ -1290,6 +1314,32 @@
12901314 if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) or (asize < 4) then return bad(); end;
12911315 k := wordIndex(aw, BitOps);
12921316 rm(BitPfx[k], awide, BitCodes[k], ao[0].r, ao[1], false);
1317+ elsif wordIndex(aw, MemOps) >= 0 then
1318+ if (an # 1) or (ao[0].kind # oMem) then return bad(); end;
1319+ k := wordIndex(aw, MemOps);
1320+ rm(0, false, MemCodes[k], MemDigits[k], ao[0], false);
1321+ elsif aw = "ltr" then
1322+ (* 0F 00 /3 with a 16-bit register: written out, since opcode
1323+ ends an opcode at its first zero byte *)
1324+ if (an # 1) or (ao[0].kind # oReg) or (asize # 2) then return bad(); end;
1325+ prefix(0, false, 0, NoIndex, ao[0].r, false);
1326+ Chunk.put1(0FH); Chunk.put1(0); Chunk.put1(0C0H | (3 shl 3) | (ao[0].r & 7));
1327+ elsif (aw = "in") or (aw = "out") then
1328+ (* in al, ax or eax from a port, a byte or dx: E4 or EC; out the
1329+ other way, E6 or EE; one more for a word or more. The size is
1330+ the register's, not the first operand's, which is dx for out *)
1331+ if aw = "in" then k := 1; else k := 0; end; (* the port's operand *)
1332+ 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;
1333+ if ao[1 - k].size = 2 then Chunk.put1(66H); end;
1334+ if ao[k].kind = oImm then
1335+ if (ao[k].v < 0) or (ao[k].v > 255) then return bad(); end;
1336+ Chunk.put1(0E4H + 2 * (1 - k) + bit(ao[1 - k].size # 1));
1337+ Chunk.put1(INT(ao[k].v));
1338+ elsif (ao[k].kind = oReg) and (ao[k].r = RDX) and (ao[k].size = 2) then
1339+ Chunk.put1(0ECH + 2 * (1 - k) + bit(ao[1 - k].size # 1));
1340+ else
1341+ return bad();
1342+ end;
12931343 elsif (aw = "movsd") or (aw = "movss") then
12941344 if aw = "movsd" then k := 0F2H; else k := 0F3H; end;
12951345 if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then
@@ -1325,8 +1375,8 @@
13251375 rm(realPfx(aw = "cvtss2sd"), false, 5A0FH, ao[0].r, ao[1], false);
13261376 else
13271377 k := 0;
1328- while (k <= 14) and (Plain[k] # aw) do INC(k); end;
1329- if (k > 14) or (an # 0) then
1378+ while (k < LEN(Plain)) and (Plain[k] # aw) do INC(k); end;
1379+ if (k = LEN(Plain)) or (an # 0) then
13301380 return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)]));
13311381 end;
13321382 opcode(PlainOp[k]);

test/err/AsmControl.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+test/err/AsmControl.raz:4:5: error: can't assemble mov with these operands
2+ 4 | mov eax, cr0;
3+ | ^^^
4+raz: 1 error

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

@@ -0,0 +1,7 @@
1+module AsmControl;
2+proc f;
3+code
4+ mov eax, cr0;
5+ ret;
6+end f;
7+end AsmControl.

test/run/AsmKernel.expect (history) added +36 -0

@@ -0,0 +1,36 @@
1+cli: FA
2+sti: FB
3+iretq: 48 CF
4+swapgs: 0F 01 F8
5+rdmsr: 0F 32
6+wrmsr: 0F 30
7+wbinvd: 0F 09
8+lgdt [rdi]: 0F 01 17
9+lidt [rsi + 8]: 0F 01 5E 08
10+invlpg [rax]: 0F 01 38
11+invlpg [r12 + rcx * 8 + 400]: 41 0F 01 BC CC 90 01 00 00
12+xsave [rdi]: 0F AE 27
13+xrstor [r9]: 41 0F AE 29
14+ltr ax: 0F 00 D8
15+ltr r10w: 41 0F 00 DA
16+mov cr0, rax: 0F 22 C0
17+mov cr2, rdx: 0F 22 D2
18+mov cr3, r15: 41 0F 22 DF
19+mov cr4, rcx: 0F 22 E1
20+mov cr8, rax: 44 0F 22 C0
21+mov cr8, r11: 45 0F 22 C3
22+mov rax, cr0: 0F 20 C0
23+mov r9, cr3: 41 0F 20 D9
24+mov rbx, cr8: 44 0F 20 C3
25+in al, 60H: E4 60
26+in ax, 61H: 66 E5 61
27+in eax, 0FFH: E5 FF
28+in al, dx: EC
29+in ax, dx: 66 ED
30+in eax, dx: ED
31+out 80H, al: E6 80
32+out 80H, ax: 66 E7 80
33+out 0, eax: E7 00
34+out dx, al: EE
35+out dx, ax: 66 EF
36+out dx, eax: EF

test/run/AsmKernel.raz (history) added +79 -0

@@ -0,0 +1,79 @@
1+module AsmKernel;
2+(* A kernel's instructions in the assembler: each one's bytes, read
3+ back from the procedure they are made in, since they can't run in
4+ user mode. Each was checked against GNU as. *)
5+import Out;
6+
7+const
8+ Count = 36;
9+ Names: [Count][]U8 = ["cli", "sti", "iretq", "swapgs",
10+ "rdmsr", "wrmsr", "wbinvd", "lgdt [rdi]",
11+ "lidt [rsi + 8]", "invlpg [rax]", "invlpg [r12 + rcx * 8 + 400]", "xsave [rdi]",
12+ "xrstor [r9]", "ltr ax", "ltr r10w", "mov cr0, rax",
13+ "mov cr2, rdx", "mov cr3, r15", "mov cr4, rcx", "mov cr8, rax",
14+ "mov cr8, r11", "mov rax, cr0", "mov r9, cr3", "mov rbx, cr8",
15+ "in al, 60H", "in ax, 61H", "in eax, 0FFH", "in al, dx",
16+ "in ax, dx", "in eax, dx", "out 80H, al", "out 80H, ax",
17+ "out 0, eax", "out dx, al", "out dx, ax", "out dx, eax"];
18+ Sizes: [Count]INT = [1, 1, 2, 3, 2, 2, 2, 3, 4, 3, 9, 3,
19+ 4, 3, 4, 3, 3, 4, 3, 4, 4, 3, 4, 4,
20+ 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1];
21+
22+proc kernel;
23+code
24+ cli;
25+ sti;
26+ iretq;
27+ swapgs;
28+ rdmsr;
29+ wrmsr;
30+ wbinvd;
31+ lgdt [rdi];
32+ lidt [rsi + 8];
33+ invlpg [rax];
34+ invlpg [r12 + rcx * 8 + 400];
35+ xsave [rdi];
36+ xrstor [r9];
37+ ltr ax;
38+ ltr r10w;
39+ mov cr0, rax;
40+ mov cr2, rdx;
41+ mov cr3, r15;
42+ mov cr4, rcx;
43+ mov cr8, rax;
44+ mov cr8, r11;
45+ mov rax, cr0;
46+ mov r9, cr3;
47+ mov rbx, cr8;
48+ in al, 60H;
49+ in ax, 61H;
50+ in eax, 0FFH;
51+ in al, dx;
52+ in ax, dx;
53+ in eax, dx;
54+ out 80H, al;
55+ out 80H, ax;
56+ out 0, eax;
57+ out dx, al;
58+ out dx, ax;
59+ out dx, eax;
60+ ret;
61+end kernel;
62+
63+proc main*;
64+var p: proc; b: ^U8; i, j, at: INT;
65+begin
66+ p := kernel;
67+ b := ^U8(p);
68+ at := 0;
69+ for i := 0 to Count - 1 do
70+ Out.str(Names[i]); Out.str(":");
71+ for j := 0 to Sizes[i] - 1 do
72+ Out.str(" "); Out.hex(U64(b[at + j]), 2);
73+ end;
74+ Out.ln;
75+ at := at + Sizes[i];
76+ end;
77+end;
78+
79+end AsmKernel.