raz / commit e009eff1

commit
e009eff13cf2c0a3d9d2361a52c4b0a482614556
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
384eba2a
compiler.md: the recipe for an interrupt's entry, and test/run/Interrupt, which runs it on Linux

patch · log

3 files changed, +525 -1

doc/compiler.md (history) modified +152 -1

@@ -2485,7 +2485,158 @@
24852485 as System V asks; an interrupt stub that uses `std` must clear it
24862486 before it calls a procedure.
24872487
2488-The recipe for an interrupt's entry, and its test, follow.
2488+**The recipe for an interrupt's entry.** From those facts, what a stub
2489+must keep is what the compiler's code may change and a raz procedure
2490+doesn't give back: `rax`, `rcx`, `rdx`, `rsi`, `rdi`, `r8` to `r11`,
2491+the SSE state, and the flags; `rbx`, `rbp` and `r12` to `r15` the
2492+handler keeps itself, as any procedure does. It must also give raz code
2493+what it counts on: `rsp` a multiple of 16 at the call, and the
2494+direction flag clear. So, a `code` procedure per vector pushes 0 where
2495+the CPU pushes no error code, then the vector, and jumps to one that
2496+does the rest and calls an ordinary procedure (`code` is a reserved
2497+word, so the error code is `errorCode`):
2498+
2499+```
2500+module Traps;
2501+(* An interrupt's entry: a stub per vector, the common part, and
2502+ handle, which is plain raz. *)
2503+import SYSTEM;
2504+
2505+type
2506+ (* what the CPU pushes, from the lowest address up *)
2507+ Frame = record
2508+ rip: UINT; (* where the interrupted code goes on *)
2509+ cs: UINT; (* its code segment *)
2510+ rflags: UINT; (* its flags *)
2511+ rsp: UINT; (* its stack *)
2512+ ss: UINT; (* its stack segment *)
2513+ end;
2514+
2515+var
2516+ counts*: [256]INT; (* how many times each vector came *)
2517+ lastCode*: INT; (* the last error code *)
2518+ lastRip*: UINT; (* and where the code was *)
2519+
2520+(* The handler: anything raz does, reals too *)
2521+proc handle(vector, errorCode: INT; frame: ^Frame);
2522+begin
2523+ INC(counts[vector]);
2524+ lastCode := errorCode;
2525+ lastRip := frame^.rip;
2526+end;
2527+
2528+(* What every stub goes on to. The stack holds, from rsp up: the
2529+ vector, the error code, then the CPU's frame *)
2530+proc common;
2531+code
2532+ push rax; (* what a call may change and handle doesn't keep *)
2533+ push rcx;
2534+ push rdx;
2535+ push rsi;
2536+ push rdi;
2537+ push r8;
2538+ push r9;
2539+ push r10;
2540+ push r11;
2541+ (* a 64-byte-aligned area: 576 bytes for xsave, then the old rsp *)
2542+ mov rax, rsp;
2543+ and rsp, -64;
2544+ sub rsp, 640;
2545+ mov [rsp + 576], rax;
2546+ (* xsave writes only the first word of its 64-byte header, at 512,
2547+ and xrstor refuses the rest unless it is zero *)
2548+ xor edx, edx;
2549+ mov ecx, 8;
2550+ zero:
2551+ mov [rsp + rcx * 8 + 504], rdx;
2552+ dec ecx;
2553+ jnz zero;
2554+ mov eax, 3; (* edx:eax, what to save: the x87 state and SSE's *)
2555+ xsave [rsp];
2556+ (* handle(vector, error code, frame), past the nine registers *)
2557+ mov rcx, [rsp + 576];
2558+ mov rdi, [rcx + 72];
2559+ mov rsi, [rcx + 80];
2560+ lea rdx, [rcx + 88];
2561+ cld;
2562+ call handle;
2563+ xor edx, edx;
2564+ mov eax, 3;
2565+ xrstor [rsp];
2566+ mov rsp, [rsp + 576];
2567+ pop r11;
2568+ pop r10;
2569+ pop r9;
2570+ pop r8;
2571+ pop rdi;
2572+ pop rsi;
2573+ pop rdx;
2574+ pop rcx;
2575+ pop rax;
2576+ add rsp, 16; (* the vector and the error code *)
2577+ iretq;
2578+end common;
2579+
2580+(* Vector 3, a breakpoint: the CPU pushes no error code *)
2581+proc vector3*;
2582+code
2583+ push 0;
2584+ push 3;
2585+ jmp common;
2586+end vector3;
2587+
2588+(* Vector 14, a page fault: the CPU pushes an error code *)
2589+proc vector14*;
2590+code
2591+ push 14;
2592+ jmp common;
2593+end vector14;
2594+
2595+end Traps.
2596+```
2597+
2598+What each part is for:
2599+
2600+- *The error code.* The CPU pushes one for vectors 8, 10 to 14, 17, 21,
2601+ 29 and 30, and for no other; a stub pushes 0 for the rest, so that
2602+ every vector's stack looks alike to `common`. `common` finds the two
2603+ words and the frame at 72, 80 and 88 bytes past its nine pushes;
2604+ `add rsp, 16` drops the two words before `iretq` takes the frame.
2605+- *Alignment.* The CPU aligns `rsp` to 16 before it pushes its frame,
2606+ but an odd number of words follow it, so `common` aligns again and
2607+ keeps the old `rsp` to go back to. `and rsp, -64` is for `xsave`,
2608+ whose area must be 64-byte aligned; 640 bytes keep `rsp` a multiple
2609+ of 16 at the call.
2610+- *The SSE state.* `handle` may change any of `xmm0` to `xmm15`.
2611+ `xsave` with `edx:eax` 3 saves the x87 and SSE state, `MXCSR` with
2612+ it, in 576 bytes, and `xrstor` with the same mask puts it back. The
2613+ assembler knows `xsave` and `xrstor`, not `fxsave` and `fxrstor`
2614+ (which would need no header and only 16-byte alignment), so the
2615+ recipe uses those. `xsave` needs `CR4.OSXSAVE` set and bits 0 and 1
2616+ of `XCR0`, which a kernel sets when it turns SSE on; Linux has. The
2617+ area is on the stack, so another interrupt may come inside `handle`.
2618+- *The direction flag.* `cld` before the call, since the interrupted
2619+ code may have set it; `iretq` gives it back with the other flags.
2620+- *No IST.* The compiler's code writes nothing below `rsp`, so a stub
2621+ pushes where it stands; the stack needs room for the CPU's frame,
2622+ `common`'s 784 bytes at most, and `handle`'s frames.
2623+- *What is a kernel's.* `common` touches no segment register and does
2624+ no `swapgs`: a kernel whose handlers come from ring 3 with a per-CPU
2625+ area in `gs` adds that, as it adds the IDT that names `vector3` and
2626+ `vector14`.
2627+
2628+**Its test.** `test/run/Interrupt.raz` runs the same `common` and stubs
2629+on Linux, since `iretq` from ring 3 to ring 3 is allowed. A code
2630+procedure, `interrupt`, pushes a frame as the CPU would, with Linux's
2631+user `cs` and `ss` (33H and 2BH), flags with the direction flag set,
2632+and for `rip` a code procedure `back`, and the error code for vector
2633+14; sets every general register and `xmm0` to `xmm15` to values of its
2634+own, and the direction flag; and reaches the stub by `ret`. `handle`
2635+reads the flags (`pushfq`, as `db 9CH`, which the assembler doesn't
2636+know), notes its arguments and the frame, and computes with reals.
2637+`back`, where `iretq` returns, notes every register. Each is checked,
2638+a line each: the vector, the error code, the frame, the direction flag
2639+clear inside `handle`, every register kept, and `rsp` the frame's.
24892640
24902641 ## What the compiler asks of Sys, Rt and Files
24912642

test/run/Interrupt.expect (history) added +78 -0

@@ -0,0 +1,78 @@
1+vector 3
2+handle was given the vector: yes
3+and the error code: yes
4+the frame's rip is back: yes
5+its cs, rflags, rsp and ss are as pushed: yes
6+the direction flag is clear in handle: yes
7+handle computed 631643 thousandths
8+rax kept: yes
9+rcx kept: yes
10+rdx kept: yes
11+rbx kept: yes
12+rsp is the frame's: yes
13+rbp kept: yes
14+rsi kept: yes
15+rdi kept: yes
16+r8 kept: yes
17+r9 kept: yes
18+r10 kept: yes
19+r11 kept: yes
20+r12 kept: yes
21+r13 kept: yes
22+r14 kept: yes
23+r15 kept: yes
24+xmm0 kept: yes
25+xmm1 kept: yes
26+xmm2 kept: yes
27+xmm3 kept: yes
28+xmm4 kept: yes
29+xmm5 kept: yes
30+xmm6 kept: yes
31+xmm7 kept: yes
32+xmm8 kept: yes
33+xmm9 kept: yes
34+xmm10 kept: yes
35+xmm11 kept: yes
36+xmm12 kept: yes
37+xmm13 kept: yes
38+xmm14 kept: yes
39+xmm15 kept: yes
40+vector 14
41+handle was given the vector: yes
42+and the error code: yes
43+the frame's rip is back: yes
44+its cs, rflags, rsp and ss are as pushed: yes
45+the direction flag is clear in handle: yes
46+handle computed 631643 thousandths
47+rax kept: yes
48+rcx kept: yes
49+rdx kept: yes
50+rbx kept: yes
51+rsp is the frame's: yes
52+rbp kept: yes
53+rsi kept: yes
54+rdi kept: yes
55+r8 kept: yes
56+r9 kept: yes
57+r10 kept: yes
58+r11 kept: yes
59+r12 kept: yes
60+r13 kept: yes
61+r14 kept: yes
62+r15 kept: yes
63+xmm0 kept: yes
64+xmm1 kept: yes
65+xmm2 kept: yes
66+xmm3 kept: yes
67+xmm4 kept: yes
68+xmm5 kept: yes
69+xmm6 kept: yes
70+xmm7 kept: yes
71+xmm8 kept: yes
72+xmm9 kept: yes
73+xmm10 kept: yes
74+xmm11 kept: yes
75+xmm12 kept: yes
76+xmm13 kept: yes
77+xmm14 kept: yes
78+xmm15 kept: yes

test/run/Interrupt.raz (history) added +295 -0

@@ -0,0 +1,295 @@
1+module Interrupt;
2+(* The recipe for an interrupt's entry (doc/compiler.md, "A program with
3+ its own Rt"), run on Linux: iretq from ring 3 to ring 3 is allowed.
4+ interrupt builds the frame the CPU would push, sets every register to
5+ a value of its own and the direction flag, and goes to a vector's
6+ stub; the stub calls handle, an ordinary procedure that computes with
7+ reals, and returns by iretq to back, which notes every register. Each
8+ is checked against what was set, for a vector with no error code (3)
9+ and one with (14). *)
10+import Out;
11+import SYSTEM;
12+
13+type
14+ (* what the CPU pushes, from the lowest address up *)
15+ Frame = record
16+ rip: UINT; (* where the interrupted code goes on *)
17+ cs: UINT; (* its code segment *)
18+ rflags: UINT; (* its flags *)
19+ rsp: UINT; (* its stack *)
20+ ss: UINT; (* its stack segment *)
21+ end;
22+
23+const
24+ UserCs = 33H; (* Linux's code segment for a 64-bit program *)
25+ UserSs = 2BH; (* and its stack segment *)
26+ Flags = 602H; (* the flags in the frame: IF, bit 1 (always set) and DF *)
27+ DirectionFlag = 400H;
28+ (* the general registers, by their numbers *)
29+ RegNames: [16][]const U8 = ["rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
30+ "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"];
31+ Rsp = 4;
32+
33+var
34+ before: [16]UINT; (* the general registers as set, by number; rsp's unused *)
35+ after: [16]UINT; (* and as back found them *)
36+ xbefore: [32]UINT; (* xmm0 to xmm15 as set, two words each *)
37+ xafter: [32]UINT; (* and as back found them *)
38+ sentRsp: UINT; (* the rsp put in the frame *)
39+ (* what handle saw *)
40+ gotVector, gotCode: INT;
41+ gotFrame: Frame;
42+ gotFlags: UINT; (* the flags inside handle *)
43+ gotSum: F64; (* what it computed *)
44+
45+(* The flags, by pushfq, which the assembler doesn't know *)
46+proc flags: UINT;
47+code
48+ db 9CH; (* pushfq *)
49+ pop rax;
50+ ret;
51+end flags;
52+
53+(* The handler: an ordinary procedure, whose reals use the xmm registers *)
54+proc handle(vector, errorCode: INT; frame: ^Frame);
55+var i: INT; x: F64;
56+begin
57+ gotFlags := flags();
58+ gotVector := vector;
59+ gotCode := errorCode;
60+ gotFrame := frame^;
61+ x := 1.0;
62+ for i := 1 to 20 do x := x * 1.25 + F64(i) / 3.0; end;
63+ gotSum := x;
64+end;
65+
66+(* What every stub goes on to. The stack holds, from rsp up: the vector,
67+ the error code, then the CPU's frame. It keeps what the compiler's code
68+ may change and handle doesn't keep: the general registers System V
69+ lets a call change, the SSE state (by xsave, as the assembler knows no
70+ fxsave) and the flags (by iretq); and it clears the direction flag *)
71+proc common;
72+code
73+ push rax; (* what a call may change and handle doesn't keep *)
74+ push rcx;
75+ push rdx;
76+ push rsi;
77+ push rdi;
78+ push r8;
79+ push r9;
80+ push r10;
81+ push r11;
82+ (* a 64-byte-aligned area: 576 bytes for xsave, then the old rsp *)
83+ mov rax, rsp;
84+ and rsp, -64;
85+ sub rsp, 640;
86+ mov [rsp + 576], rax;
87+ (* xsave writes only the first word of its 64-byte header, at 512,
88+ and xrstor refuses the rest unless it is zero *)
89+ xor edx, edx;
90+ mov ecx, 8;
91+ zero:
92+ mov [rsp + rcx * 8 + 504], rdx;
93+ dec ecx;
94+ jnz zero;
95+ mov eax, 3; (* edx:eax, what to save: the x87 state and SSE's *)
96+ xsave [rsp];
97+ (* handle(vector, error code, frame), past the nine registers *)
98+ mov rcx, [rsp + 576];
99+ mov rdi, [rcx + 72];
100+ mov rsi, [rcx + 80];
101+ lea rdx, [rcx + 88];
102+ cld;
103+ call handle;
104+ xor edx, edx;
105+ mov eax, 3;
106+ xrstor [rsp];
107+ mov rsp, [rsp + 576];
108+ pop r11;
109+ pop r10;
110+ pop r9;
111+ pop r8;
112+ pop rdi;
113+ pop rsi;
114+ pop rdx;
115+ pop rcx;
116+ pop rax;
117+ add rsp, 16; (* the vector and the error code *)
118+ iretq;
119+end common;
120+
121+(* The stub of vector 3, a breakpoint: the CPU pushes no error code *)
122+proc vector3;
123+code
124+ push 0;
125+ push 3;
126+ jmp common;
127+end vector3;
128+
129+(* The stub of vector 14, a page fault: the CPU pushes an error code *)
130+proc vector14;
131+code
132+ push 14;
133+ jmp common;
134+end vector14;
135+
136+(* Where iretq returns: notes every register in after and xafter, clears
137+ the direction flag the frame set again, and returns for interrupt *)
138+proc back;
139+code
140+ push rax;
141+ lea rax, [after];
142+ mov [rax + 8], rcx;
143+ mov [rax + 16], rdx;
144+ mov [rax + 24], rbx;
145+ lea rcx, [rsp + 8];
146+ mov [rax + 32], rcx;
147+ mov [rax + 40], rbp;
148+ mov [rax + 48], rsi;
149+ mov [rax + 56], rdi;
150+ mov [rax + 64], r8;
151+ mov [rax + 72], r9;
152+ mov [rax + 80], r10;
153+ mov [rax + 88], r11;
154+ mov [rax + 96], r12;
155+ mov [rax + 104], r13;
156+ mov [rax + 112], r14;
157+ mov [rax + 120], r15;
158+ pop rcx;
159+ mov [rax], rcx;
160+ lea rax, [xafter];
161+ movdqu [rax], xmm0;
162+ movdqu [rax + 16], xmm1;
163+ movdqu [rax + 32], xmm2;
164+ movdqu [rax + 48], xmm3;
165+ movdqu [rax + 64], xmm4;
166+ movdqu [rax + 80], xmm5;
167+ movdqu [rax + 96], xmm6;
168+ movdqu [rax + 112], xmm7;
169+ movdqu [rax + 128], xmm8;
170+ movdqu [rax + 144], xmm9;
171+ movdqu [rax + 160], xmm10;
172+ movdqu [rax + 176], xmm11;
173+ movdqu [rax + 192], xmm12;
174+ movdqu [rax + 208], xmm13;
175+ movdqu [rax + 224], xmm14;
176+ movdqu [rax + 240], xmm15;
177+ cld;
178+ pop r15;
179+ pop r14;
180+ pop r13;
181+ pop r12;
182+ pop rbp;
183+ pop rbx;
184+ ret;
185+end back;
186+
187+(* An interrupt of stub, with errorCode pushed if it isn't negative, as
188+ the CPU would push it: ss, rsp, rflags, cs and rip, which is back.
189+ Every register is set from before and xbefore, the direction flag
190+ too, and stub is reached by ret, which takes its address off the
191+ stack and leaves the frame *)
192+proc interrupt(stub: proc; errorCode: INT);
193+code
194+ push rbx; (* what our caller keeps, which back gives back *)
195+ push rbp;
196+ push r12;
197+ push r13;
198+ push r14;
199+ push r15;
200+ mov rax, rsp;
201+ lea rcx, [sentRsp];
202+ mov [rcx], rax;
203+ push UserSs;
204+ push rax;
205+ push Flags;
206+ push UserCs;
207+ lea rcx, [back];
208+ push rcx;
209+ test rsi, rsi;
210+ js noCode;
211+ push rsi;
212+ noCode:
213+ push rdi;
214+ lea rax, [xbefore];
215+ movdqu xmm0, [rax];
216+ movdqu xmm1, [rax + 16];
217+ movdqu xmm2, [rax + 32];
218+ movdqu xmm3, [rax + 48];
219+ movdqu xmm4, [rax + 64];
220+ movdqu xmm5, [rax + 80];
221+ movdqu xmm6, [rax + 96];
222+ movdqu xmm7, [rax + 112];
223+ movdqu xmm8, [rax + 128];
224+ movdqu xmm9, [rax + 144];
225+ movdqu xmm10, [rax + 160];
226+ movdqu xmm11, [rax + 176];
227+ movdqu xmm12, [rax + 192];
228+ movdqu xmm13, [rax + 208];
229+ movdqu xmm14, [rax + 224];
230+ movdqu xmm15, [rax + 240];
231+ lea rax, [before];
232+ mov rcx, [rax + 8];
233+ mov rdx, [rax + 16];
234+ mov rbx, [rax + 24];
235+ mov rbp, [rax + 40];
236+ mov rsi, [rax + 48];
237+ mov rdi, [rax + 56];
238+ mov r8, [rax + 64];
239+ mov r9, [rax + 72];
240+ mov r10, [rax + 80];
241+ mov r11, [rax + 88];
242+ mov r12, [rax + 96];
243+ mov r13, [rax + 104];
244+ mov r14, [rax + 112];
245+ mov r15, [rax + 120];
246+ mov rax, [rax];
247+ std;
248+ ret;
249+end interrupt;
250+
251+(* yes or no *)
252+proc say(what: []const U8; ok: BOOL);
253+begin
254+ Out.str(what);
255+ if ok then Out.str(": yes"); else Out.str(": no"); end;
256+ Out.ln;
257+end;
258+
259+(* An interrupt of stub, as vector, and every check of it *)
260+proc check(stub: proc; vector, errorCode: INT);
261+var i: INT; p: proc;
262+begin
263+ for i := 0 to 15 do before[i] := UINT(i + 1) * 0101010101010101H + UINT(vector); end;
264+ for i := 0 to 31 do xbefore[i] := UINT(i + 1) * 0001000100010001H + UINT(vector); end;
265+ interrupt(stub, errorCode);
266+ Out.str("vector "); Out.int(vector); Out.ln;
267+ say("handle was given the vector", gotVector = vector);
268+ say("and the error code", (gotCode = errorCode) or (errorCode < 0) and (gotCode = 0));
269+ p := back;
270+ say("the frame's rip is back", gotFrame.rip = UINT(^U8(p)));
271+ say("its cs, rflags, rsp and ss are as pushed", (gotFrame.cs = UserCs) and (gotFrame.rflags = Flags)
272+ and (gotFrame.rsp = sentRsp) and (gotFrame.ss = UserSs));
273+ say("the direction flag is clear in handle", gotFlags & DirectionFlag = 0);
274+ Out.str("handle computed "); Out.int(INT(gotSum * 1000.0)); Out.str(" thousandths"); Out.ln;
275+ for i := 0 to 15 do
276+ if i = Rsp then
277+ say("rsp is the frame's", after[i] = sentRsp);
278+ else
279+ Out.str(RegNames[i]);
280+ say(" kept", after[i] = before[i]);
281+ end;
282+ end;
283+ for i := 0 to 15 do
284+ Out.str("xmm"); Out.int(i);
285+ say(" kept", (xafter[2 * i] = xbefore[2 * i]) and (xafter[2 * i + 1] = xbefore[2 * i + 1]));
286+ end;
287+end;
288+
289+proc main*;
290+begin
291+ check(vector3, 3, -1);
292+ check(vector14, 14, 6);
293+end;
294+
295+end Interrupt.