raz / test / run / Interrupt.raz

295 lines · 8.4 KiB · raw · history · last change e009eff1

1module 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). *)
10import Out;
11import SYSTEM;
12
13type
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
23const
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
33var
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 *)
46proc flags: UINT;
47code
48 db 9CH; (* pushfq *)
49 pop rax;
50 ret;
51end flags;
52
53(* The handler: an ordinary procedure, whose reals use the xmm registers *)
54proc handle(vector, errorCode: INT; frame: ^Frame);
55var i: INT; x: F64;
56begin
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;
64end;
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 *)
71proc common;
72code
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;
119end common;
120
121(* The stub of vector 3, a breakpoint: the CPU pushes no error code *)
122proc vector3;
123code
124 push 0;
125 push 3;
126 jmp common;
127end vector3;
128
129(* The stub of vector 14, a page fault: the CPU pushes an error code *)
130proc vector14;
131code
132 push 14;
133 jmp common;
134end 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 *)
138proc back;
139code
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;
185end 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 *)
192proc interrupt(stub: proc; errorCode: INT);
193code
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;
249end interrupt;
250
251(* yes or no *)
252proc say(what: []const U8; ok: BOOL);
253begin
254 Out.str(what);
255 if ok then Out.str(": yes"); else Out.str(": no"); end;
256 Out.ln;
257end;
258
259(* An interrupt of stub, as vector, and every check of it *)
260proc check(stub: proc; vector, errorCode: INT);
261var i: INT; p: proc;
262begin
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;
287end;
288
289proc main*;
290begin
291 check(vector3, 3, -1);
292 check(vector14, 14, 6);
293end;
294
295end Interrupt.