From e009eff13cf2c0a3d9d2361a52c4b0a482614556 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:35:42 +0000 Subject: [PATCH] compiler.md: the recipe for an interrupt's entry, and test/run/Interrupt, which runs it on Linux --- diff --git a/doc/compiler.md b/doc/compiler.md index 539f1a1..c753a49 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -2485,7 +2485,158 @@ as System V asks; an interrupt stub that uses `std` must clear it before it calls a procedure. -The recipe for an interrupt's entry, and its test, follow. +**The recipe for an interrupt's entry.** From those facts, what a stub +must keep is what the compiler's code may change and a raz procedure +doesn't give back: `rax`, `rcx`, `rdx`, `rsi`, `rdi`, `r8` to `r11`, +the SSE state, and the flags; `rbx`, `rbp` and `r12` to `r15` the +handler keeps itself, as any procedure does. It must also give raz code +what it counts on: `rsp` a multiple of 16 at the call, and the +direction flag clear. So, a `code` procedure per vector pushes 0 where +the CPU pushes no error code, then the vector, and jumps to one that +does the rest and calls an ordinary procedure (`code` is a reserved +word, so the error code is `errorCode`): + +``` +module Traps; +(* An interrupt's entry: a stub per vector, the common part, and + handle, which is plain raz. *) +import SYSTEM; + +type + (* what the CPU pushes, from the lowest address up *) + Frame = record + rip: UINT; (* where the interrupted code goes on *) + cs: UINT; (* its code segment *) + rflags: UINT; (* its flags *) + rsp: UINT; (* its stack *) + ss: UINT; (* its stack segment *) + end; + +var + counts*: [256]INT; (* how many times each vector came *) + lastCode*: INT; (* the last error code *) + lastRip*: UINT; (* and where the code was *) + +(* The handler: anything raz does, reals too *) +proc handle(vector, errorCode: INT; frame: ^Frame); +begin + INC(counts[vector]); + lastCode := errorCode; + lastRip := frame^.rip; +end; + +(* What every stub goes on to. The stack holds, from rsp up: the + vector, the error code, then the CPU's frame *) +proc common; +code + push rax; (* what a call may change and handle doesn't keep *) + push rcx; + push rdx; + push rsi; + push rdi; + push r8; + push r9; + push r10; + push r11; + (* a 64-byte-aligned area: 576 bytes for xsave, then the old rsp *) + mov rax, rsp; + and rsp, -64; + sub rsp, 640; + mov [rsp + 576], rax; + (* xsave writes only the first word of its 64-byte header, at 512, + and xrstor refuses the rest unless it is zero *) + xor edx, edx; + mov ecx, 8; + zero: + mov [rsp + rcx * 8 + 504], rdx; + dec ecx; + jnz zero; + mov eax, 3; (* edx:eax, what to save: the x87 state and SSE's *) + xsave [rsp]; + (* handle(vector, error code, frame), past the nine registers *) + mov rcx, [rsp + 576]; + mov rdi, [rcx + 72]; + mov rsi, [rcx + 80]; + lea rdx, [rcx + 88]; + cld; + call handle; + xor edx, edx; + mov eax, 3; + xrstor [rsp]; + mov rsp, [rsp + 576]; + pop r11; + pop r10; + pop r9; + pop r8; + pop rdi; + pop rsi; + pop rdx; + pop rcx; + pop rax; + add rsp, 16; (* the vector and the error code *) + iretq; +end common; + +(* Vector 3, a breakpoint: the CPU pushes no error code *) +proc vector3*; +code + push 0; + push 3; + jmp common; +end vector3; + +(* Vector 14, a page fault: the CPU pushes an error code *) +proc vector14*; +code + push 14; + jmp common; +end vector14; + +end Traps. +``` + +What each part is for: + +- *The error code.* The CPU pushes one for vectors 8, 10 to 14, 17, 21, + 29 and 30, and for no other; a stub pushes 0 for the rest, so that + every vector's stack looks alike to `common`. `common` finds the two + words and the frame at 72, 80 and 88 bytes past its nine pushes; + `add rsp, 16` drops the two words before `iretq` takes the frame. +- *Alignment.* The CPU aligns `rsp` to 16 before it pushes its frame, + but an odd number of words follow it, so `common` aligns again and + keeps the old `rsp` to go back to. `and rsp, -64` is for `xsave`, + whose area must be 64-byte aligned; 640 bytes keep `rsp` a multiple + of 16 at the call. +- *The SSE state.* `handle` may change any of `xmm0` to `xmm15`. + `xsave` with `edx:eax` 3 saves the x87 and SSE state, `MXCSR` with + it, in 576 bytes, and `xrstor` with the same mask puts it back. The + assembler knows `xsave` and `xrstor`, not `fxsave` and `fxrstor` + (which would need no header and only 16-byte alignment), so the + recipe uses those. `xsave` needs `CR4.OSXSAVE` set and bits 0 and 1 + of `XCR0`, which a kernel sets when it turns SSE on; Linux has. The + area is on the stack, so another interrupt may come inside `handle`. +- *The direction flag.* `cld` before the call, since the interrupted + code may have set it; `iretq` gives it back with the other flags. +- *No IST.* The compiler's code writes nothing below `rsp`, so a stub + pushes where it stands; the stack needs room for the CPU's frame, + `common`'s 784 bytes at most, and `handle`'s frames. +- *What is a kernel's.* `common` touches no segment register and does + no `swapgs`: a kernel whose handlers come from ring 3 with a per-CPU + area in `gs` adds that, as it adds the IDT that names `vector3` and + `vector14`. + +**Its test.** `test/run/Interrupt.raz` runs the same `common` and stubs +on Linux, since `iretq` from ring 3 to ring 3 is allowed. A code +procedure, `interrupt`, pushes a frame as the CPU would, with Linux's +user `cs` and `ss` (33H and 2BH), flags with the direction flag set, +and for `rip` a code procedure `back`, and the error code for vector +14; sets every general register and `xmm0` to `xmm15` to values of its +own, and the direction flag; and reaches the stub by `ret`. `handle` +reads the flags (`pushfq`, as `db 9CH`, which the assembler doesn't +know), notes its arguments and the frame, and computes with reals. +`back`, where `iretq` returns, notes every register. Each is checked, +a line each: the vector, the error code, the frame, the direction flag +clear inside `handle`, every register kept, and `rsp` the frame's. ## What the compiler asks of Sys, Rt and Files diff --git a/test/run/Interrupt.expect b/test/run/Interrupt.expect new file mode 100644 index 0000000..1d5e06e --- /dev/null +++ b/test/run/Interrupt.expect @@ -0,0 +1,78 @@ +vector 3 +handle was given the vector: yes +and the error code: yes +the frame's rip is back: yes +its cs, rflags, rsp and ss are as pushed: yes +the direction flag is clear in handle: yes +handle computed 631643 thousandths +rax kept: yes +rcx kept: yes +rdx kept: yes +rbx kept: yes +rsp is the frame's: yes +rbp kept: yes +rsi kept: yes +rdi kept: yes +r8 kept: yes +r9 kept: yes +r10 kept: yes +r11 kept: yes +r12 kept: yes +r13 kept: yes +r14 kept: yes +r15 kept: yes +xmm0 kept: yes +xmm1 kept: yes +xmm2 kept: yes +xmm3 kept: yes +xmm4 kept: yes +xmm5 kept: yes +xmm6 kept: yes +xmm7 kept: yes +xmm8 kept: yes +xmm9 kept: yes +xmm10 kept: yes +xmm11 kept: yes +xmm12 kept: yes +xmm13 kept: yes +xmm14 kept: yes +xmm15 kept: yes +vector 14 +handle was given the vector: yes +and the error code: yes +the frame's rip is back: yes +its cs, rflags, rsp and ss are as pushed: yes +the direction flag is clear in handle: yes +handle computed 631643 thousandths +rax kept: yes +rcx kept: yes +rdx kept: yes +rbx kept: yes +rsp is the frame's: yes +rbp kept: yes +rsi kept: yes +rdi kept: yes +r8 kept: yes +r9 kept: yes +r10 kept: yes +r11 kept: yes +r12 kept: yes +r13 kept: yes +r14 kept: yes +r15 kept: yes +xmm0 kept: yes +xmm1 kept: yes +xmm2 kept: yes +xmm3 kept: yes +xmm4 kept: yes +xmm5 kept: yes +xmm6 kept: yes +xmm7 kept: yes +xmm8 kept: yes +xmm9 kept: yes +xmm10 kept: yes +xmm11 kept: yes +xmm12 kept: yes +xmm13 kept: yes +xmm14 kept: yes +xmm15 kept: yes diff --git a/test/run/Interrupt.raz b/test/run/Interrupt.raz new file mode 100644 index 0000000..4aa78e0 --- /dev/null +++ b/test/run/Interrupt.raz @@ -0,0 +1,295 @@ +module Interrupt; +(* The recipe for an interrupt's entry (doc/compiler.md, "A program with + its own Rt"), run on Linux: iretq from ring 3 to ring 3 is allowed. + interrupt builds the frame the CPU would push, sets every register to + a value of its own and the direction flag, and goes to a vector's + stub; the stub calls handle, an ordinary procedure that computes with + reals, and returns by iretq to back, which notes every register. Each + is checked against what was set, for a vector with no error code (3) + and one with (14). *) +import Out; +import SYSTEM; + +type + (* what the CPU pushes, from the lowest address up *) + Frame = record + rip: UINT; (* where the interrupted code goes on *) + cs: UINT; (* its code segment *) + rflags: UINT; (* its flags *) + rsp: UINT; (* its stack *) + ss: UINT; (* its stack segment *) + end; + +const + UserCs = 33H; (* Linux's code segment for a 64-bit program *) + UserSs = 2BH; (* and its stack segment *) + Flags = 602H; (* the flags in the frame: IF, bit 1 (always set) and DF *) + DirectionFlag = 400H; + (* the general registers, by their numbers *) + RegNames: [16][]const U8 = ["rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"]; + Rsp = 4; + +var + before: [16]UINT; (* the general registers as set, by number; rsp's unused *) + after: [16]UINT; (* and as back found them *) + xbefore: [32]UINT; (* xmm0 to xmm15 as set, two words each *) + xafter: [32]UINT; (* and as back found them *) + sentRsp: UINT; (* the rsp put in the frame *) + (* what handle saw *) + gotVector, gotCode: INT; + gotFrame: Frame; + gotFlags: UINT; (* the flags inside handle *) + gotSum: F64; (* what it computed *) + +(* The flags, by pushfq, which the assembler doesn't know *) +proc flags: UINT; +code + db 9CH; (* pushfq *) + pop rax; + ret; +end flags; + +(* The handler: an ordinary procedure, whose reals use the xmm registers *) +proc handle(vector, errorCode: INT; frame: ^Frame); +var i: INT; x: F64; +begin + gotFlags := flags(); + gotVector := vector; + gotCode := errorCode; + gotFrame := frame^; + x := 1.0; + for i := 1 to 20 do x := x * 1.25 + F64(i) / 3.0; end; + gotSum := x; +end; + +(* What every stub goes on to. The stack holds, from rsp up: the vector, + the error code, then the CPU's frame. It keeps what the compiler's code + may change and handle doesn't keep: the general registers System V + lets a call change, the SSE state (by xsave, as the assembler knows no + fxsave) and the flags (by iretq); and it clears the direction flag *) +proc common; +code + push rax; (* what a call may change and handle doesn't keep *) + push rcx; + push rdx; + push rsi; + push rdi; + push r8; + push r9; + push r10; + push r11; + (* a 64-byte-aligned area: 576 bytes for xsave, then the old rsp *) + mov rax, rsp; + and rsp, -64; + sub rsp, 640; + mov [rsp + 576], rax; + (* xsave writes only the first word of its 64-byte header, at 512, + and xrstor refuses the rest unless it is zero *) + xor edx, edx; + mov ecx, 8; + zero: + mov [rsp + rcx * 8 + 504], rdx; + dec ecx; + jnz zero; + mov eax, 3; (* edx:eax, what to save: the x87 state and SSE's *) + xsave [rsp]; + (* handle(vector, error code, frame), past the nine registers *) + mov rcx, [rsp + 576]; + mov rdi, [rcx + 72]; + mov rsi, [rcx + 80]; + lea rdx, [rcx + 88]; + cld; + call handle; + xor edx, edx; + mov eax, 3; + xrstor [rsp]; + mov rsp, [rsp + 576]; + pop r11; + pop r10; + pop r9; + pop r8; + pop rdi; + pop rsi; + pop rdx; + pop rcx; + pop rax; + add rsp, 16; (* the vector and the error code *) + iretq; +end common; + +(* The stub of vector 3, a breakpoint: the CPU pushes no error code *) +proc vector3; +code + push 0; + push 3; + jmp common; +end vector3; + +(* The stub of vector 14, a page fault: the CPU pushes an error code *) +proc vector14; +code + push 14; + jmp common; +end vector14; + +(* Where iretq returns: notes every register in after and xafter, clears + the direction flag the frame set again, and returns for interrupt *) +proc back; +code + push rax; + lea rax, [after]; + mov [rax + 8], rcx; + mov [rax + 16], rdx; + mov [rax + 24], rbx; + lea rcx, [rsp + 8]; + mov [rax + 32], rcx; + mov [rax + 40], rbp; + mov [rax + 48], rsi; + mov [rax + 56], rdi; + mov [rax + 64], r8; + mov [rax + 72], r9; + mov [rax + 80], r10; + mov [rax + 88], r11; + mov [rax + 96], r12; + mov [rax + 104], r13; + mov [rax + 112], r14; + mov [rax + 120], r15; + pop rcx; + mov [rax], rcx; + lea rax, [xafter]; + movdqu [rax], xmm0; + movdqu [rax + 16], xmm1; + movdqu [rax + 32], xmm2; + movdqu [rax + 48], xmm3; + movdqu [rax + 64], xmm4; + movdqu [rax + 80], xmm5; + movdqu [rax + 96], xmm6; + movdqu [rax + 112], xmm7; + movdqu [rax + 128], xmm8; + movdqu [rax + 144], xmm9; + movdqu [rax + 160], xmm10; + movdqu [rax + 176], xmm11; + movdqu [rax + 192], xmm12; + movdqu [rax + 208], xmm13; + movdqu [rax + 224], xmm14; + movdqu [rax + 240], xmm15; + cld; + pop r15; + pop r14; + pop r13; + pop r12; + pop rbp; + pop rbx; + ret; +end back; + +(* An interrupt of stub, with errorCode pushed if it isn't negative, as + the CPU would push it: ss, rsp, rflags, cs and rip, which is back. + Every register is set from before and xbefore, the direction flag + too, and stub is reached by ret, which takes its address off the + stack and leaves the frame *) +proc interrupt(stub: proc; errorCode: INT); +code + push rbx; (* what our caller keeps, which back gives back *) + push rbp; + push r12; + push r13; + push r14; + push r15; + mov rax, rsp; + lea rcx, [sentRsp]; + mov [rcx], rax; + push UserSs; + push rax; + push Flags; + push UserCs; + lea rcx, [back]; + push rcx; + test rsi, rsi; + js noCode; + push rsi; + noCode: + push rdi; + lea rax, [xbefore]; + movdqu xmm0, [rax]; + movdqu xmm1, [rax + 16]; + movdqu xmm2, [rax + 32]; + movdqu xmm3, [rax + 48]; + movdqu xmm4, [rax + 64]; + movdqu xmm5, [rax + 80]; + movdqu xmm6, [rax + 96]; + movdqu xmm7, [rax + 112]; + movdqu xmm8, [rax + 128]; + movdqu xmm9, [rax + 144]; + movdqu xmm10, [rax + 160]; + movdqu xmm11, [rax + 176]; + movdqu xmm12, [rax + 192]; + movdqu xmm13, [rax + 208]; + movdqu xmm14, [rax + 224]; + movdqu xmm15, [rax + 240]; + lea rax, [before]; + mov rcx, [rax + 8]; + mov rdx, [rax + 16]; + mov rbx, [rax + 24]; + mov rbp, [rax + 40]; + mov rsi, [rax + 48]; + mov rdi, [rax + 56]; + mov r8, [rax + 64]; + mov r9, [rax + 72]; + mov r10, [rax + 80]; + mov r11, [rax + 88]; + mov r12, [rax + 96]; + mov r13, [rax + 104]; + mov r14, [rax + 112]; + mov r15, [rax + 120]; + mov rax, [rax]; + std; + ret; +end interrupt; + +(* yes or no *) +proc say(what: []const U8; ok: BOOL); +begin + Out.str(what); + if ok then Out.str(": yes"); else Out.str(": no"); end; + Out.ln; +end; + +(* An interrupt of stub, as vector, and every check of it *) +proc check(stub: proc; vector, errorCode: INT); +var i: INT; p: proc; +begin + for i := 0 to 15 do before[i] := UINT(i + 1) * 0101010101010101H + UINT(vector); end; + for i := 0 to 31 do xbefore[i] := UINT(i + 1) * 0001000100010001H + UINT(vector); end; + interrupt(stub, errorCode); + Out.str("vector "); Out.int(vector); Out.ln; + say("handle was given the vector", gotVector = vector); + say("and the error code", (gotCode = errorCode) or (errorCode < 0) and (gotCode = 0)); + p := back; + say("the frame's rip is back", gotFrame.rip = UINT(^U8(p))); + say("its cs, rflags, rsp and ss are as pushed", (gotFrame.cs = UserCs) and (gotFrame.rflags = Flags) + and (gotFrame.rsp = sentRsp) and (gotFrame.ss = UserSs)); + say("the direction flag is clear in handle", gotFlags & DirectionFlag = 0); + Out.str("handle computed "); Out.int(INT(gotSum * 1000.0)); Out.str(" thousandths"); Out.ln; + for i := 0 to 15 do + if i = Rsp then + say("rsp is the frame's", after[i] = sentRsp); + else + Out.str(RegNames[i]); + say(" kept", after[i] = before[i]); + end; + end; + for i := 0 to 15 do + Out.str("xmm"); Out.int(i); + say(" kept", (xafter[2 * i] = xbefore[2 * i]) and (xafter[2 * i + 1] = xbefore[2 * i + 1])); + end; +end; + +proc main*; +begin + check(vector3, 3, -1); + check(vector14, 14, 6); +end; + +end Interrupt.