-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSoftInts.s
381 lines (325 loc) · 12 KB
/
SoftInts.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
; "Program" and related interrupts
kSoftIntAlign equ 5
########################################################################
_align 6
PerfMonitorInt
mfsprg r1, 2 ; Both r1 and LR are clobbered on entry,
mtlr r1 ; so might as well use r1 for scratch as
mfsprg r1, 1 ; we restore from SPRG1/2.
rfi
########################################################################
_align kSoftIntAlign
KCallRunAlternateContext ; CB r3, flags r4
; For the native CB pointers, maintain a 4-entry log-phys cache in KDP
and. r8, r4, r13
lwz r9, KDP.NCBCacheLA0(r1)
rlwinm r8, r3, 0, 0, 25
cmpw cr1, r8, r9
bne ReturnFromInt
lwz r9, KDP.NCBCachePA0(r1)
bne cr1, @search_cache
@searched_cache ; r9 now contains a physical ContextBlock ptr
; Use Exceptions code to get into the alternate context
addi r8, r1, KDP.VecTblAlternate ; different external interrupt handling
mtsprg 3, r8
lwz r8, KDP.EmuTrapTableLogical(r1)
mtcrf crMaskFlags, r7 ; why restore kernel condition regs?
rlwinm r7, r7, 0, ~maskExceptionNum ; no exception when we get back to sys
stw r8, CB.IntraState.HandlerReturn+4(r9)
stw r9, KDP.ContextPtr(r1) ; now use Exceptions code
b SwitchContext ; old_cb r6, new_cb r9
@search_cache
lwz r9, KDP.NCBCacheLA1(r1)
cmpw cr1, r8, r9
beq cr1, @slot1
lwz r9, KDP.NCBCacheLA2(r1)
cmpw cr1, r8, r9
beq cr1, @slot2
lwz r9, KDP.NCBCacheLA3(r1)
cmpw cr1, r8, r9
beq cr1, @slot3
; cache miss
stmw r14, KDP.r14(r1)
cmpw cr1, r8, r6
beq cr1, @fail
mr r27, r8
addi r29, r1, KDP.BatRanges + 0xa0
bl GetPhysical
clrlwi r23, r8, 20
beq @fail
cmplwi r23, 0x0d00
mr r9, r8
mr r8, r31
ble @contiguous
addi r27, r27, 0x1000
addi r29, r1, KDP.BatRanges + 0xa0
bl GetPhysical
beq @fail
subi r31, r31, 0x1000
xor r23, r8, r31
rlwinm. r23, r23, 0, 25, 22
bne @fail ; because physical pages are discontiguous
@contiguous
clrlwi r23, r31, 30
cmpwi r23, 3
rlwimi r8, r9, 0, 20, 31
beq @fail
; physical address lookup succeeded
lwz r23, KDP.NKInfo.NCBPtrCacheMissCount(r1)
addi r23, r23, 1
stw r23, KDP.NKInfo.NCBPtrCacheMissCount(r1)
; populate cache slot 3, then waterfall to @searched_cache
lmw r14, KDP.r14(r1)
stw r8, KDP.NCBCachePA3(r1)
@slot3 ; so promote to slot 2
lwz r8, KDP.NCBCacheLA2(r1)
stw r9, KDP.NCBCacheLA2(r1)
stw r8, KDP.NCBCacheLA3(r1)
lwz r9, KDP.NCBCachePA3(r1)
lwz r8, KDP.NCBCachePA2(r1)
stw r9, KDP.NCBCachePA2(r1)
stw r8, KDP.NCBCachePA3(r1)
lwz r9, KDP.NCBCacheLA2(r1)
@slot2 ; so promote to slot 1
lwz r8, KDP.NCBCacheLA1(r1)
stw r9, KDP.NCBCacheLA1(r1)
stw r8, KDP.NCBCacheLA2(r1)
lwz r9, KDP.NCBCachePA2(r1)
lwz r8, KDP.NCBCachePA1(r1)
stw r9, KDP.NCBCachePA1(r1)
stw r8, KDP.NCBCachePA2(r1)
lwz r9, KDP.NCBCacheLA1(r1)
@slot1 ; so promote to slot 0, save elsewhere, and push on
lwz r8, KDP.NCBCacheLA0(r1)
stw r9, KDP.NCBCacheLA0(r1)
stw r9, KDP.NatContextPtrLogical(r1)
stw r8, KDP.NCBCacheLA1(r1)
lwz r9, KDP.NCBCachePA1(r1)
lwz r8, KDP.NCBCachePA0(r1)
stw r9, KDP.NCBCachePA0(r1)
stw r8, KDP.NCBCachePA1(r1)
b @searched_cache
@fail
lmw r14, KDP.r14(r1)
li r8, ecTrapInstr
b Exception
########################################################################
_align kSoftIntAlign
KCallResetSystem ; PPC trap 1 (68k RESET instruction)
; If Gary Davidian's skeleton key is inserted then return
; to userspace with a different MSR instead of resetting
stmw r14, KDP.r14(r1)
xoris r8, r3, 'Ga' ; a0/r3 must be 'Gary'
cmplwi r8, 'ry'
bne @notskeleton
xoris r8, r4, 0x0505 ; a1/r4 must be 0x05051956
cmplwi r8, 0x1956
bne @notskeleton
andc r11, r11, r5 ; d0/r5 = msr_clear
lwz r8, CB.r7+4(r6)
or r11, r11, r8 ; d3/r7 = msr_set
b ReturnFromInt
@notskeleton
; This file starts with a Reset label, and is part of the init process
include 'Reset.s'
########################################################################
_align kSoftIntAlign
KCallPrioritizeInterrupts
; Left side: roll back the interrupt preparation before the int handler repeats is
; Right side: jump to the external interrupt handler (PIH or ProgramInt)
mtsprg 2, r12
mtsrr0 r10
mtsrr1 r11
mtcr r13
lwz r10, CB.r10+4(r6)
lwz r11, CB.r11+4(r6)
lwz r12, CB.r12+4(r6)
lwz r13, CB.r13+4(r6)
lwz r7, CB.r7+4(r6)
lwz r8, KDP.r1(r1)
mfsprg r9, 3
lwz r9, VecTbl.External(r9)
mtsprg 1, r8
mtlr r9
lwz r8, CB.r8+4(r6)
lwz r9, CB.r9+4(r6)
lwz r6, KDP.r6(r1)
blrl ; (could this ever fall though to KCallSystemCrash?)
########################################################################
KCallSystemCrash
stw r0, KDP.r0(r1)
stw r2, KDP.r2(r1)
stw r3, KDP.r3(r1)
stw r4, KDP.r4(r1)
stw r5, KDP.r5(r1)
lwz r8, CB.r7+4(r6)
lwz r9, CB.r8+4(r6)
stw r8, KDP.r7(r1)
stw r9, KDP.r8(r1)
lwz r8, CB.r9+4(r6)
lwz r9, CB.r10+4(r6)
stw r8, KDP.r9(r1)
stw r9, KDP.r10(r1)
lwz r8, CB.r11+4(r6)
lwz r9, CB.r12+4(r6)
stw r8, KDP.r11(r1)
stw r9, KDP.r12(r1)
lwz r8, CB.r13+4(r6)
stw r8, KDP.r13(r1)
stmw r14, KDP.r14(r1)
bl CrashSoftInts
########################################################################
_align 6
ProgramInt
; (also called when the Alternate Context gets an External Int => Exception)
; Standard interrupt palaver
mfsprg r1, 0
stw r6, KDP.r6(r1)
mfsprg r6, 1
stw r6, KDP.r1(r1)
lwz r6, KDP.ContextPtr(r1)
stw r7, CB.r7+4(r6)
stw r8, CB.r8+4(r6)
stw r9, CB.r9+4(r6)
stw r10, CB.r10+4(r6)
stw r11, CB.r11+4(r6)
stw r12, CB.r12+4(r6)
stw r13, CB.r13+4(r6)
; Compare SRR0 with address of Emulator's KCall trap table
lwz r8, KDP.EmuTrapTableLogical(r1)
mfsrr0 r10
mfcr r13
xor. r8, r10, r8
lwz r7, KDP.Flags(r1)
mfsprg r12, 2
beq KCallReturnFromExceptionFastPath ; KCall in Emulator table => fast path
rlwimi. r7, r7, bGlobalFlagSystem, 0, 0
cmplwi cr7, r8, 16 * 4
bge cr0, @fromAltContext ; Alt Context cannot make KCalls; this might be an External Int
bge cr7, @notFromEmulatorTrapTable ; from Emulator but not from its KCall table => do more checks
; SUCCESSFUL TRAP from emulator KCall table
; => Service call then return to link register
add r8, r8, r1
lwz r11, KDP.NKInfo.NanoKernelCallCounts(r8)
lwz r10, KDP.KCallTbl(r8)
addi r11, r11, 1
stw r11, KDP.NKInfo.NanoKernelCallCounts(r8)
mtlr r10
mr r10, r12 ; ret addr: LR was saved to SPRG2, SPRG2 to r12 above, r12 to r10 now, r10 to SRR0 to program ctr later
mfsrr1 r11
rlwimi r7, r7, 32-5, 26, 26 ; something about MSR[SE]
blr
@notFromEmulatorTrapTable ; so check if it is even a trap...
mfsrr1 r11
mtcrf 0x70, r11
bc BO_IF_NOT, 14, @notTrap
mfmsr r9 ; fetch the instruction to get the "trap number"
_ori r8, r9, MsrDR
mtmsr r8
isync
lwz r8, 0(r10)
mtmsr r9
isync
xoris r8, r8, 0xfff
cmplwi cr7, r8, 16 ; only traps 0-15 are allowed
slwi r8, r8, 2 ; (for "success" case below)
bge cr7, @illegalTrap
; SUCCESSFUL TRAP from outside emulator KCall table
; => Service call then return to following instruction
add r8, r8, r1
lwz r9, KDP.NKInfo.NanoKernelCallCounts(r8)
addi r9, r9, 1
stw r9, KDP.NKInfo.NanoKernelCallCounts(r8)
lwz r8, KDP.KCallTbl(r8)
mtlr r8
addi r10, r10, 4 ; continue executing the next instruction
rlwimi r7, r7, 32-5, 26, 26 ; something about MSR[SE]
blr
; Cannot service with a KCall => throw Exception
@fromAltContext ; external interrupt, or a (forbidden) KCall attempt
mfsrr1 r11
mtcrf 0x70, r11
@notTrap ; then it was some other software exception
bc BO_IF, 12, Emulate
bc BO_IF+1, 13, Emulate
bc BO_IF, 11, @floatingPointException
@illegalTrap ; because we only allow traps 0-15
rlwinm r8, r11, 17, 28, 29
addi r8, r8, 0x4b3
rlwnm r8, r8, r8, 28, 31
b Exception ; CLEVER BIT HACKING described below
; SRR1[13] SRR[14] Exception
; 0 0 ecNoException
; 0 1 ecTrapInstr
; 1 0 ecPrivilegedInstr
; 1 1 9 (floating-point?)
@floatingPointException
li r8, ecFloatException
bc BO_IF, 15, Exception ; SRR1[15] set => handler can retry
addi r10, r10, 4
rlwimi r7, r7, 32-5, 26, 26 ; something about MSR[SE]
b Exception ; SRR1[15] unset => can't retry
########################################################################
_align kSoftIntAlign
SysCallInt ; Now stolen from IntProgram
; Standard interrupt palaver
mfsprg r1, 0
stw r6, KDP.r6(r1)
mfsprg r6, 1
stw r6, KDP.r1(r1)
lwz r6, KDP.ContextPtr(r1)
stw r7, CB.r7+4(r6)
stw r8, CB.r8+4(r6)
stw r9, CB.r9+4(r6)
stw r10, CB.r10+4(r6)
stw r11, CB.r11+4(r6)
stw r12, CB.r12+4(r6)
stw r13, CB.r13+4(r6)
; Compare SRR0 with address of Emulator's KCall trap table
lwz r8, KDP.EmuTrapTableLogical(r1)
mfsrr0 r10
mfcr r13
subi r10, r10, 4
xor. r8, r10, r8
lwz r7, KDP.Flags(r1)
mfsprg r12, 2
beq KCallReturnFromExceptionFastPath ; KCall in Emulator table => fast path
rlwimi. r7, r7, bGlobalFlagSystem, 0, 0
cmplwi cr7, r8, 16 * 4
bge cr0, @fromAltContext ; Alt Context cannot make KCalls; this might be an External Int
bge cr7, @notFromEmulatorTrapTable ; from Emulator but not from its KCall table => do more checks
; SUCCESSFUL TRAP from emulator KCall table
; => Service call then return to link register
add r8, r8, r1
lwz r11, KDP.NKInfo.NanoKernelCallCounts(r8)
lwz r10, KDP.KCallTbl(r8)
addi r11, r11, 1
stw r11, KDP.NKInfo.NanoKernelCallCounts(r8)
mtlr r10
mr r10, r12 ; ret addr: LR was saved to SPRG2, SPRG2 to r12 above, r12 to r10 now, r10 to SRR0 to program ctr later
mfsrr1 r11
rlwimi r7, r7, 32-5, 26, 26 ; something about MSR[SE]
blr
@fromAltContext ; Raise Exception
@notFromEmulatorTrapTable
mfsrr1 r11
mfmsr r8
rlwimi r11, r8, 0, 0xFFFF0000
li r8, ecSystemCall
b Exception
########################################################################
_align kSoftIntAlign
TraceInt ; here because of MSR[SE/BE], possibly thanks to ContextFlagTraceWhenDone
bl LoadInterruptRegisters
li r8, ecInstTrace
b Exception
########################################################################
_align kSoftIntAlign
IgnoreSoftInt ; Used to probe CPU features at init time
mfsrr0 r1
addi r1, r1, 4
mtsrr0 r1
mfsprg r1, 2
mtlr r1
mfsprg r1, 1
rfi