]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/hw_exception.s
xnu-344.21.73.tar.gz
[apple/xnu.git] / osfmk / ppc / hw_exception.s
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
d7e50217 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
d7e50217
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
d7e50217
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * @OSF_COPYRIGHT@
27 */
28
29/* Low level routines dealing with exception entry and exit.
30 * There are various types of exception:
31 *
32 * Interrupt, trap, system call and debugger entry. Each has it's own
33 * handler since the state save routine is different for each. The
34 * code is very similar (a lot of cut and paste).
35 *
36 * The code for the FPU disabled handler (lazy fpu) is in cswtch.s
37 */
38
39#include <debug.h>
40#include <mach_assert.h>
41#include <mach/exception_types.h>
42#include <mach/ppc/vm_param.h>
43
44#include <assym.s>
45
46#include <ppc/asm.h>
47#include <ppc/proc_reg.h>
48#include <ppc/trap.h>
49#include <ppc/exception.h>
9bccf70c 50#include <ppc/savearea.h>
1c79356b
A
51#include <ppc/spl.h>
52
53
54#define VERIFYSAVE 0
55#define FPVECDBG 0
d7e50217
A
56#define INSTRUMENT 0
57
1c79356b
A
58/*
59 * thandler(type)
60 *
61 * ENTRY: VM switched ON
62 * Interrupts OFF
63 * R3 contains exception code
64 * R4 points to the saved context (virtual address)
65 * Everything is saved in savearea
66 */
67
68/*
69 * If pcb.ksp == 0 then the kernel stack is already busy,
9bccf70c 70 * we make a stack frame
1c79356b
A
71 * leaving enough space for the 'red zone' in case the
72 * trapped thread was in the middle of saving state below
73 * its stack pointer.
74 *
9bccf70c 75 * otherwise we make a stack frame and
1c79356b
A
76 * the kernel stack (setting pcb.ksp to 0)
77 *
78 * on return, we do the reverse, the last state is popped from the pcb
79 * and pcb.ksp is set to the top of stack
80 */
81
1c79356b
A
82/* TRAP_SPACE_NEEDED is the space assumed free on the kernel stack when
83 * another trap is taken. We need at least enough space for a saved state
84 * structure plus two small backpointer frames, and we add a few
85 * hundred bytes for the space needed by the C (which may be less but
86 * may be much more). We're trying to catch kernel stack overflows :-)
87 */
88
89#define TRAP_SPACE_NEEDED FM_REDZONE+(2*FM_SIZE)+256
90
1c79356b
A
91 .text
92
0b4e3aa0
A
93 .align 5
94 .globl EXT(thandler)
9bccf70c 95LEXT(thandler) ; Trap handler
0b4e3aa0 96
9bccf70c 97 mfsprg r25,0 ; Get the per_proc
1c79356b 98
9bccf70c 99 lwz r1,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
1c79356b 100
9bccf70c
A
101 cmpwi cr0,r1,0 ; Are we on interrupt stack?
102 lwz r6,PP_ACTIVE_THREAD(r25) ; Get the pointer to the currently active thread
103 beq- cr0,EXT(ihandler) ; If on interrupt stack, treat this as interrupt...
104 lwz r13,THREAD_TOP_ACT(r6) ; Point to the active activation
105 lwz r26,ACT_MACT_SPF(r13) ; Get special flags
106 lwz r8,ACT_MACT_PCB(r13) ; Get the last savearea used
0b4e3aa0 107 rlwinm. r26,r26,0,bbThreadbit,bbThreadbit ; Do we have Blue Box Assist active?
9bccf70c
A
108 lwz r1,ACT_MACT_KSP(r13) ; Get the top of kernel stack
109 bnel- checkassist ; See if we should assist this
110 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
d7e50217 111 stw r8,SAVprev+4(r4) ; Queue the new save area in the front
1c79356b
A
112
113#if VERIFYSAVE
9bccf70c 114 bl versave ; (TEST/DEBUG)
1c79356b
A
115#endif
116
9bccf70c
A
117 lwz r9,THREAD_KERNEL_STACK(r6) ; Get our kernel stack start
118 cmpwi cr1,r1,0 ; Are we already on kernel stack?
119 stw r13,SAVact(r4) ; Mark the savearea as belonging to this activation
d7e50217 120 lwz r26,saver1+4(r4) ; Get the stack at interrupt time
1c79356b 121
9bccf70c 122 bne+ cr1,.L_kstackfree ; We are not on kernel stack yet...
1c79356b 123
9bccf70c 124 subi r1,r26,FM_REDZONE ; Make a red zone on interrupt time kernel stack
1c79356b
A
125
126.L_kstackfree:
d7e50217 127 lwz r7,savesrr1+4(r4) ; Pick up the entry MSR
9bccf70c
A
128 sub r9,r1,r9 ; Get displacment into the kernel stack
129 li r0,0 ; Make this 0
d7e50217 130 rlwinm. r0,r9,0,28,31 ; Verify that we have a 16-byte aligned stack (and get a 0)
9bccf70c
A
131 cmplwi cr2,r9,KERNEL_STACK_SIZE ; Do we still have room on the stack?
132 beq cr1,.L_state_on_kstack ; using above test for pcb/stack
1c79356b 133
9bccf70c 134 stw r0,ACT_MACT_KSP(r13) ; Show that we have taken the stack
1c79356b
A
135
136.L_state_on_kstack:
9bccf70c 137 lwz r9,savevrsave(r4) ; Get the VRSAVE register
d7e50217 138 bne-- kernelStackUnaligned ; Stack is unaligned...
1c79356b 139 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
9bccf70c 140 subi r1,r1,FM_SIZE ; Push a header onto the current stack
d7e50217 141 bgt-- cr2,kernelStackBad ; Kernel stack is bogus...
9bccf70c
A
142
143kernelStackNotBad: ; Vector was off
d7e50217 144 beq++ tvecoff ; Vector off, do not save vrsave...
9bccf70c
A
145 stw r9,liveVRS(r25) ; Set the live value
146
147tvecoff: stw r26,FM_BACKPTR(r1) ; Link back to the previous frame
1c79356b
A
148
149#if DEBUG
150/* If debugging, we need two frames, the first being a dummy
151 * which links back to the trapped routine. The second is
152 * that which the C routine below will need
153 */
d7e50217 154 lwz r3,savesrr0+4(r4) ; Get the point of interruption
9bccf70c
A
155 stw r3,FM_LR_SAVE(r1) ; save old instr ptr as LR value
156 stwu r1, -FM_SIZE(r1) ; and make new frame
1c79356b
A
157#endif /* DEBUG */
158
159
160/* call trap handler proper, with
161 * ARG0 = type (not yet, holds pcb ptr)
162 * ARG1 = saved_state ptr (already there)
163 * ARG2 = dsisr (already there)
164 * ARG3 = dar (already there)
165 */
166
167
9bccf70c
A
168 lwz r3,saveexception(r4) ; Get the exception code
169 lwz r0,ACT_MACT_SPF(r13) ; Get the special flags
1c79356b 170
9bccf70c 171 addi r5,r3,-T_DATA_ACCESS ; Adjust to start of range
1c79356b 172 rlwinm. r0,r0,0,runningVMbit,runningVMbit ; Are we in VM state? (cr0_eq == 0 if yes)
9bccf70c 173 cmplwi cr2,r5,T_TRACE-T_DATA_ACCESS ; Are we still in range? (cr_gt if not)
1c79356b 174
9bccf70c 175 lwz r5,savedsisr(r4) ; Get the saved DSISR
1c79356b 176
9bccf70c 177 crnor cr7_eq,cr0_eq,cr2_gt ; We should intercept if in VM and is a true trap (cr7_eq == 1 if yes)
1c79356b
A
178 rlwinm. r0,r7,0,MSR_PR_BIT,MSR_PR_BIT ; Are we trapping from supervisor state? (cr0_eq == 1 if yes)
179
9bccf70c 180 cmpi cr2,r3,T_PREEMPT ; Is this a preemption?
1c79356b 181
9bccf70c 182 crandc cr0_eq,cr7_eq,cr0_eq ; Do not intercept if we are in the kernel (cr0_eq == 1 if yes)
1c79356b 183
d7e50217
A
184 lwz r6,savedar(r4) ; Get the DAR (top)
185 lwz r7,savedar+4(r4) ; Get the DAR (bottom)
1c79356b 186
d7e50217 187 beq- cr2,.L_call_trap ; Do not turn on interrupts for T_PREEMPT
9bccf70c 188 beq- exitFromVM ; Any true trap but T_MACHINE_CHECK exits us from the VM...
1c79356b
A
189
190/* syscall exception might warp here if there's nothing left
191 * to do except generate a trap
192 */
193
194.L_call_trap:
1c79356b
A
195
196 bl EXT(trap)
197
d7e50217 198 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 199 mfmsr r7 ; Get the MSR
d7e50217
A
200 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
201 andc r7,r7,r10 ; Turn off VEC, FP, and EE
9bccf70c
A
202 mtmsr r7 ; Disable for interrupts
203 mfsprg r10,0 ; Restore the per_proc info
1c79356b 204/*
1c79356b
A
205 * This is also the point where new threads come when they are created.
206 * The new thread is setup to look like a thread that took an
207 * interrupt and went immediatly into trap.
1c79356b
A
208 */
209
210thread_return:
9bccf70c 211 lwz r11,SAVflags(r3) ; Get the flags of the current savearea
d7e50217
A
212 lwz r0,savesrr1+4(r3) ; Get the MSR we are going to
213 lwz r4,SAVprev+4(r3) ; Pick up the previous savearea
9bccf70c 214 mfsprg r8,1 ; Get the current activation
d7e50217
A
215 lwz r1,PP_ACTIVE_THREAD(r10) ; Get the active thread
216 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
217 rlwinm. r0,r0,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going to the user?
9bccf70c 218 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
1c79356b 219
d7e50217 220 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
9bccf70c 221 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
d7e50217 222 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
1c79356b 223
d7e50217 224 beq-- chkfac ; We are not leaving the kernel yet...
1c79356b 225
9bccf70c
A
226 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
227 b chkfac ; Go end it all...
1c79356b
A
228
229
0b4e3aa0
A
230;
231; Here is where we go when we detect that the kernel stack is all messed up.
232; We just try to dump some info and get into the debugger.
233;
234
235kernelStackBad:
236
9bccf70c 237 lwz r3,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
0b4e3aa0 238 subi r3,r3,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
9bccf70c 239 sub r3,r1,r3 ; Get displacement into debug stack
0b4e3aa0 240 cmplwi cr2,r3,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
9bccf70c 241 blt+ cr2,kernelStackNotBad ; Yeah, that is ok too...
0b4e3aa0 242
9bccf70c
A
243 lis r0,hi16(Choke) ; Choke code
244 ori r0,r0,lo16(Choke) ; and the rest
245 li r3,failStack ; Bad stack code
246 sc ; System ABEND
0b4e3aa0 247
d7e50217
A
248kernelStackUnaligned:
249 lis r0,hi16(Choke) ; Choke code
250 ori r0,r0,lo16(Choke) ; and the rest
251 li r3,failUnalignedStk ; Unaligned stack code
252 sc ; System ABEND
253
1c79356b
A
254
255/*
256 * shandler(type)
257 *
258 * ENTRY: VM switched ON
259 * Interrupts OFF
260 * R3 contains exception code
261 * R4 points to the saved context (virtual address)
262 * Everything is saved in savearea
263 */
264
265/*
266 * If pcb.ksp == 0 then the kernel stack is already busy,
267 * this is an error - jump to the debugger entry
268 *
269 * otherwise depending upon the type of
270 * syscall, look it up in the kernel table
271 * or pass it to the server.
272 *
273 * on return, we do the reverse, the state is popped from the pcb
274 * and pcb.ksp is set to the top of stack.
275 */
276
277/*
278 * NOTE:
279 * mach system calls are negative
280 * BSD system calls are low positive
281 * PPC-only system calls are in the range 0x6xxx
282 * PPC-only "fast" traps are in the range 0x7xxx
283 */
284
0b4e3aa0
A
285 .align 5
286 .globl EXT(shandler)
9bccf70c
A
287LEXT(shandler) ; System call handler
288
d7e50217 289 lwz r7,savesrr1+4(r4) ; Get the SRR1 value
9bccf70c 290 mfsprg r25,0 ; Get the per proc area
d7e50217 291 lwz r0,saver0+4(r4) ; Get the original syscall number
9bccf70c
A
292 lwz r17,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
293 rlwinm r15,r0,0,0,19 ; Clear the bottom of call number for fast check
294 mr. r17,r17 ; Are we on interrupt stack?
9bccf70c 295 lwz r9,savevrsave(r4) ; Get the VRsave register
d7e50217 296 beq-- EXT(ihandler) ; On interrupt stack, not allowed...
1c79356b 297 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
9bccf70c
A
298 lwz r16,PP_ACTIVE_THREAD(r25) ; Get the thread pointer
299 mfsprg r13,1 ; Pick up the active thread
1c79356b 300
d7e50217 301 beq++ svecoff ; Vector off, do not save vrsave...
9bccf70c
A
302 stw r9,liveVRS(r25) ; Set the live value
303;
1c79356b 304; Check if SCs are being redirected for the BlueBox or to VMM
9bccf70c 305;
1c79356b 306
9bccf70c 307svecoff: lwz r6,ACT_MACT_SPF(r13) ; Pick up activation special flags
d7e50217
A
308 mtcrf 0x40,r6 ; Check special flags
309 mtcrf 0x01,r6 ; Check special flags
9bccf70c 310 crmove cr6_eq,runningVMbit ; Remember if we are in VMM
d7e50217
A
311 bne++ cr6,sVMchecked ; Not running VM
312 lwz r18,spcFlags(r25) ; Load per_proc special flags
313 rlwinm. r18,r18,0,FamVMmodebit,FamVMmodebit ; Is FamVMmodebit set?
314 beq sVMchecked ; Not in FAM
315 cmpwi r0,0x6004 ; Is it vmm_dispatch syscall:
316 bne sVMchecked
317 lwz r26,saver3+4(r4) ; Get the original syscall number
318 cmpwi cr6,r26,kvmmExitToHost ; vmm_exit_to_host request
319sVMchecked:
320 bf++ bbNoMachSCbit,noassist ; Take branch if SCs are not redirected
9bccf70c
A
321 lwz r26,ACT_MACT_BEDA(r13) ; Pick up the pointer to the blue box exception area
322 b EXT(atomic_switch_syscall) ; Go to the assist...
1c79356b 323
9bccf70c
A
324noassist: cmplwi r15,0x7000 ; Do we have a fast path trap?
325 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
d7e50217 326 beql fastpath ; We think it is a fastpath...
1c79356b 327
9bccf70c 328 lwz r1,ACT_MACT_KSP(r13) ; Get the kernel stack pointer
1c79356b 329#if DEBUG
9bccf70c
A
330 mr. r1,r1 ; Are we already on the kernel stack?
331 li r3,T_SYSTEM_CALL ; Yup, pretend we had an interrupt...
332 beq- EXT(ihandler) ; Bad boy, bad boy... What cha gonna do when they come for you?
1c79356b
A
333#endif /* DEBUG */
334
9bccf70c
A
335 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
336 li r0,0 ; Clear this out
d7e50217 337 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
9bccf70c 338 stw r13,SAVact(r4) ; Point the savearea at its activation
1c79356b
A
339
340#if VERIFYSAVE
9bccf70c 341 bl versave ; (TEST/DEBUG)
1c79356b
A
342#endif
343
d7e50217 344 lwz r15,saver1+4(r4) ; Grab interrupt time stack
9bccf70c 345 mr r30,r4 ; Save pointer to the new context savearea
9bccf70c
A
346 stw r0,ACT_MACT_KSP(r13) ; Mark stack as busy with 0 val
347 stw r15,FM_BACKPTR(r1) ; Link stack frame backwards
1c79356b
A
348
349#if DEBUG
9bccf70c
A
350/* If debugging, we need two frames, the first being a dummy
351 * which links back to the trapped routine. The second is
352 * that which the C routine below will need
353 */
d7e50217 354 lwz r8,savesrr0+4(r30) ; Get the point of interruption
9bccf70c
A
355 stw r8,FM_LR_SAVE(r1) ; Save old instr ptr as LR value
356 stwu r1, -FM_SIZE(r1) ; and make new frame
1c79356b
A
357#endif /* DEBUG */
358
9bccf70c 359 lwz r15,SAVflags(r30) ; Get the savearea flags
d7e50217
A
360 lwz r0,saver0+4(r30) ; Get R0 back
361 mfmsr r11 ; Get the MSR
362 stwu r1,-(FM_SIZE+ARG_SIZE)(r1) ; Make a stack frame
9bccf70c 363 ori r11,r11,lo16(MASK(MSR_EE)) ; Turn on interruption enabled bit
9bccf70c 364 rlwinm r10,r0,0,0,19 ; Keep only the top part
d7e50217 365 oris r15,r15,SAVsyscall >> 16 ; Mark that it this is a syscall
9bccf70c
A
366 cmplwi r10,0x6000 ; Is it the special ppc-only guy?
367 stw r15,SAVflags(r30) ; Save syscall marker
d7e50217 368 beq-- cr6,exitFromVM ; It is time to exit from alternate context...
9bccf70c
A
369
370 beq- ppcscall ; Call the ppc-only system call handler...
371
d7e50217 372 mr. r0,r0 ; What kind is it?
9bccf70c
A
373 mtmsr r11 ; Enable interruptions
374
d7e50217 375 blt-- .L_kernel_syscall ; System call number if negative, this is a mach call...
9bccf70c
A
376
377 cmpwi cr0,r0,0x7FFA ; Special blue box call?
d7e50217 378 beq-- .L_notify_interrupt_syscall ; Yeah, call it...
9bccf70c
A
379
380 lwz r8,ACT_TASK(r13) ; Get our task
381 lis r10,hi16(EXT(c_syscalls_unix)) ; Get top half of counter address
382 lwz r7,TASK_SYSCALLS_UNIX(r8) ; Get the current count
383 ori r10,r10,lo16(EXT(c_syscalls_unix)) ; Get low half of counter address
384 addi r7,r7,1 ; Bump it
385 lwz r9,0(r10) ; Get counter
386 stw r7,TASK_SYSCALLS_UNIX(r8) ; Save it
387 mr r3,r30 ; Get PCB/savearea
388 mr r4,r13 ; current activation
389 addi r9,r9,1 ; Add 1
390 stw r9,0(r10) ; Save it back
391 bl EXT(unix_syscall) ; Check out unix...
1c79356b
A
392
393.L_call_server_syscall_exception:
9bccf70c 394 li r3,EXC_SYSCALL ; doexception(EXC_SYSCALL, num, 1)
1c79356b
A
395
396.L_call_server_exception:
9bccf70c 397 mr r4,r0 ; Set syscall selector
1c79356b 398 li r5,1
9bccf70c 399 b EXT(doexception) ; Go away, never to return...
1c79356b
A
400
401.L_notify_interrupt_syscall:
d7e50217 402 lwz r3,saver3+4(r30) ; Get the new PC address to pass in
1c79356b
A
403 bl EXT(syscall_notify_interrupt)
404 b .L_syscall_return
405
406;
407; Handle PPC-only system call interface
408; These are called with interruptions disabled
409; and the savearea/pcb as the first parameter.
410; It is up to the callee to enable interruptions if
411; they should be. We are in a state here where
412; both interrupts and preemption is ok, but because we could
413; be calling diagnostic code we will not enable.
414;
415; Also, the callee is responsible for finding any parameters
416; in the savearea/pcb. It also must set saver3 with any return
417; code before returning.
418;
419; There are 3 possible return codes:
420; 0 the call is disabled or something, we treat this like it was bogus
421; + the call finished ok, check for AST
422; - the call finished ok, do not check for AST
423;
424; Note: the last option is intended for special diagnostics calls that
425; want the thread to return and execute before checking for preemption.
426;
9bccf70c
A
427; NOTE: Both R16 (thread) and R30 (savearea) need to be preserved over this call!!!!
428;
429
430 .align 5
1c79356b 431
9bccf70c
A
432ppcscall: rlwinm r11,r0,2,18,29 ; Make an index into the table
433 lis r10,hi16(EXT(PPCcalls)) ; Get PPC-only system call table
434 cmplwi r11,PPCcallmax ; See if we are too big
435 ori r10,r10,lo16(EXT(PPCcalls)) ; Merge in low half
1c79356b 436 bgt- .L_call_server_syscall_exception ; Bogus call...
9bccf70c 437 lwzx r11,r10,r11 ; Get function address
1c79356b
A
438
439;
440; Note: make sure we do not change the savearea in R30 to
441; a different register without checking. Some of the PPCcalls
442; depend upon it being there.
443;
444
9bccf70c
A
445 mr r3,r30 ; Pass the savearea
446 mr r4,r13 ; Pass the activation
447 mr. r11,r11 ; See if there is a function here
d7e50217 448 mtctr r11 ; Set the function address
1c79356b 449 beq- .L_call_server_syscall_exception ; Disabled call...
d7e50217
A
450#if INSTRUMENT
451 mfspr r4,pmc1 ; Get stamp
452 stw r4,0x6100+(9*16)+0x0(0) ; Save it
453 mfspr r4,pmc2 ; Get stamp
454 stw r4,0x6100+(9*16)+0x4(0) ; Save it
455 mfspr r4,pmc3 ; Get stamp
456 stw r4,0x6100+(9*16)+0x8(0) ; Save it
457 mfspr r4,pmc4 ; Get stamp
458 stw r4,0x6100+(9*16)+0xC(0) ; Save it
459#endif
460 bctrl ; Call it
9bccf70c 461
1c79356b 462 .globl EXT(ppcscret)
9bccf70c 463
1c79356b 464LEXT(ppcscret)
9bccf70c
A
465 mr. r3,r3 ; See what we should do
466 mr r31,r16 ; Restore the current thread pointer
1c79356b 467 bgt+ .L_thread_syscall_ret_check_ast ; Take normal AST checking return....
9bccf70c
A
468 mfsprg r10,0 ; Get the per_proc
469 blt+ .L_thread_syscall_return ; Return, but no ASTs....
d7e50217 470 lwz r0,saver0+4(r30) ; Restore the system call number
1c79356b
A
471 b .L_call_server_syscall_exception ; Go to common exit...
472
473
474/* Once here, we know that the syscall was -ve
475 * we should still have r1=ksp,
476 * r16 = pointer to current thread,
477 * r13 = pointer to top activation,
478 * r0 = syscall number
479 * r30 = pointer to saved state (in pcb)
480 */
1c79356b 481
9bccf70c 482 .align 5
1c79356b 483
9bccf70c
A
484.L_kernel_syscall:
485;
486; Call a function that can print out our syscall info
487; Note that we don t care about any volatiles yet
488;
d7e50217
A
489 lis r8,hi16(EXT(kdebug_enable)) ; Get top of kdebug_enable
490 ori r8,r8,lo16(EXT(kdebug_enable)) ; Get bottom of kdebug_enable
491 lwz r0,saver0+4(r30)
492 lwz r8,0(r8) ; Get kdebug_enable
493 lis r29,hi16(EXT(mach_trap_count)) ; Get address of count
494 neg r31,r0 ; Make this positive
495 ori r29,r29,lo16(EXT(mach_trap_count)) ; Get address of count
496 lis r28,hi16(EXT(mach_trap_table)) ; Get address of table
497 cmplwi r8,0 ; Is kdebug_enable false?
498 lwz r29,0(r29) ; Pick up the actual count of system calls
499 slwi r27,r31,MACH_TRAP_OFFSET_POW2 ; Convert index to offset
500 ori r28,r28,lo16(EXT(mach_trap_table)) ; Get address of table
501 beq++ ksysnotrc ; No tracing...
502 mr r4,r30 ; Pass in saved state
9bccf70c 503 bl EXT(syscall_trace)
d7e50217
A
504
505ksysnotrc: cmplw r31,r29 ; Is this syscall in the table?
506 add r31,r27,r28 ; Point right to the syscall table entry
1c79356b 507
d7e50217 508 bge- .L_call_server_syscall_exception ; The syscall number is invalid
1c79356b 509
d7e50217 510 lwz r0,MACH_TRAP_FUNCTION(r31) ; Pick up the function address
9bccf70c
A
511
512;
d7e50217
A
513; NOTE: We do not support more than 8 parameters for PPC. The only
514; system call to use more than 8 is mach_msg_overwrite_trap and it
515; uses 9. We pass a 0 in as number 9.
9bccf70c 516;
d7e50217
A
517 lwz r8,ACT_TASK(r13) ; Get our task
518 lis r29,hi16(EXT(kern_invalid)) ; Get high half of invalid syscall function
519 ori r29,r29,lo16(EXT(kern_invalid)) ; Get low half of invalid syscall function
520 lwz r7,TASK_SYSCALLS_MACH(r8) ; Get the current count
521 lis r10,hi16(EXT(c_syscalls_mach)) ; Get top half of counter address
522 lwz r3,saver3+4(r30) ; Restore r3
523 addi r7,r7,1 ; Bump it
524 cmp cr0,r0,r29 ; Check if this is an invalid system call
525 ori r10,r10,lo16(EXT(c_syscalls_mach)) ; Get low half of counter address
526 beq- .L_call_server_syscall_exception ; We have a bad one...
527 stw r7,TASK_SYSCALLS_MACH(r8) ; Save count
528 lwz r4,saver4+4(r30) ; Restore r4
529 lwz r9,0(r10) ; Get counter
530 mtctr r0 ; Set the function call address
531 lwz r5,saver5+4(r30) ; Restore r5
532 lwz r6,saver6+4(r30) ; Restore r6
533 addi r9,r9,1 ; Add 1
534 lwz r7,saver7+4(r30) ; Restore r7
535 li r0,0 ; Clear this out
536 lwz r8,saver8+4(r30) ; Restore r8
537 stw r9,0(r10) ; Save it back
538 lwz r9,saver9+4(r30) ; Restore r9
539 lwz r10,saver10+4(r30) ; Restore r10
540 stw r0,FM_ARG0(r1) ; Clear that 9th parameter just in case some fool uses it
541 bctrl ; perform the actual syscall
542
543 lis r10,hi16(EXT(kdebug_enable)) ; Get top of kdebug_enable
544 ori r10,r10,lo16(EXT(kdebug_enable)) ; Get bottom of kdebug_enable
545 lwz r10,0(r10) ; Get kdebug_enable
546 cmplwi r10,0 ; Is kdebug_enable false?
547
548 beq++ .L_syscall_return ; No tracing...
549 mr r4,r30 ; Pass in the savearea
550 bl EXT(syscall_trace_end) ; Trace the exit of the system call
1c79356b
A
551
552/* 'standard' syscall returns here - INTERRUPTS ARE STILL ON */
553
554/* r3 contains value that we're going to return to the user
555 */
556
557/*
9bccf70c 558 * Ok, return from C function, R3 = return value
1c79356b
A
559 *
560 * get the active thread's PCB pointer and thus pointer to user state
561 * saved state is still in R30 and the active thread is in R16 .
562 */
563
564/* Store return value into saved state structure, since
565 * we need to pick up the value from here later - the
566 * syscall may perform a thread_set_syscall_return
567 * followed by a thread_exception_return, ending up
568 * at thread_syscall_return below, with SS_R3 having
569 * been set up already
570 */
571
572/* When we are here, r16 should point to the current thread,
573 * r30 should point to the current pcb
574 */
575
576/* save off return value, we must load it
577 * back anyway for thread_exception_return
1c79356b 578 */
9bccf70c 579
1c79356b 580.L_syscall_return:
9bccf70c 581 mr r31,r16 ; Move the current thread pointer
d7e50217 582 stw r3,saver3+4(r30) ; Stash the return code
1c79356b 583
1c79356b 584.L_thread_syscall_ret_check_ast:
d7e50217 585 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 586 mfmsr r12 ; Get the current MSR
d7e50217
A
587 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
588 andc r12,r12,r10 ; Turn off VEC, FP, and EE
9bccf70c 589 mtmsr r12 ; Turn interruptions off
1c79356b 590
9bccf70c 591 mfsprg r10,0 ; Get the per_processor block
1c79356b
A
592
593/* Check to see if there's an outstanding AST */
594
9bccf70c
A
595 lwz r4,PP_NEED_AST(r10) ; Get the pointer to the ast requests
596 lwz r4,0(r4) ; Get the flags
597 cmpi cr0,r4, 0 ; Any pending asts?
d7e50217 598 beq++ cr0,.L_syscall_no_ast ; Nope...
1c79356b
A
599
600/* Yes there is, call ast_taken
601 * pretending that the user thread took an AST exception here,
602 * ast_taken will save all state and bring us back here
603 */
604
605#if DEBUG
606/* debug assert - make sure that we're not returning to kernel */
d7e50217 607 lwz r3,savesrr1+4(r30)
1c79356b 608 andi. r3,r3,MASK(MSR_PR)
d7e50217 609 bne++ scrnotkern ; returning to user level, check
1c79356b 610
9bccf70c
A
611 lis r0,hi16(Choke) ; Choke code
612 ori r0,r0,lo16(Choke) ; and the rest
613 li r3,failContext ; Bad state code
614 sc ; System ABEND
0b4e3aa0 615
9bccf70c 616scrnotkern:
1c79356b
A
617#endif /* DEBUG */
618
9bccf70c
A
619 li r3,AST_ALL ; Set ast flags
620 li r4,1 ; Set interrupt allowed
621 bl EXT(ast_taken) ; Process the pending ast
622 b .L_thread_syscall_ret_check_ast ; Go see if there was another...
1c79356b
A
623
624/* thread_exception_return returns to here, almost all
625 * registers intact. It expects a full context restore
626 * of what it hasn't restored itself (ie. what we use).
627 *
628 * In particular for us,
629 * we still have r31 points to the current thread,
630 * r30 points to the current pcb
631 */
632
9bccf70c
A
633 .align 5
634
1c79356b
A
635.L_syscall_no_ast:
636.L_thread_syscall_return:
637
9bccf70c 638 mr r3,r30 ; Get savearea to the correct register for common exit
9bccf70c 639 lwz r5,THREAD_KERNEL_STACK(r31) ; Get the base pointer to the stack
d7e50217
A
640 lwz r11,SAVflags(r30) ; Get the flags
641 lwz r4,SAVprev+4(r30) ; Get the previous save area
642 mfsprg r8,1 ; Now find the current activation
9bccf70c
A
643 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
644 stw r4,ACT_MACT_PCB(r8) ; Save previous save area
d7e50217 645 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
9bccf70c 646 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
d7e50217 647 stw r11,SAVflags(r30) ; Stick back the flags
9bccf70c 648 b chkfac ; Go end it all...
1c79356b 649
1c79356b
A
650/*
651 * thread_exception_return()
652 *
653 * Return to user mode directly from within a system call.
654 */
655
0b4e3aa0
A
656 .align 5
657 .globl EXT(thread_bootstrap_return)
658LEXT(thread_bootstrap_return) ; NOTE: THIS IS GOING AWAY IN A FEW DAYS....
659
660 .globl EXT(thread_exception_return)
661LEXT(thread_exception_return) ; Directly return to user mode
1c79356b
A
662
663.L_thread_exc_ret_check_ast:
d7e50217 664 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 665 mfmsr r3 ; Get the MSR
d7e50217
A
666 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
667 andc r3,r3,r10 ; Turn off VEC, FP, and EE
9bccf70c 668 mtmsr r3 ; Disable interrupts
1c79356b
A
669
670/* Check to see if there's an outstanding AST */
671/* We don't bother establishing a call frame even though CHECK_AST
672 can invoke ast_taken(), because it can just borrow our caller's
673 frame, given that we're not going to return.
674*/
675
9bccf70c 676 mfsprg r10,0 ; Get the per_processor block
1c79356b 677 lwz r4,PP_NEED_AST(r10)
d7e50217 678 li r3,AST_ALL
1c79356b
A
679 lwz r4,0(r4)
680 cmpi cr0,r4, 0
d7e50217
A
681 li r4,1
682 beq+ cr0,.L_exc_ret_no_ast
1c79356b 683
9bccf70c
A
684/* Yes there is, call ast_taken
685 * pretending that the user thread took an AST exception here,
686 * ast_taken will save all state and bring us back here
687 */
1c79356b
A
688
689 bl EXT(ast_taken)
9bccf70c 690 b .L_thread_exc_ret_check_ast ; check for a second AST (rare)
1c79356b
A
691
692/* arriving here, interrupts should be disabled */
693/* Get the active thread's PCB pointer to restore regs
694 */
695.L_exc_ret_no_ast:
696
9bccf70c
A
697 mfsprg r30,1 ; Get the currrent activation
698 lwz r31,ACT_THREAD(r30) ; Get the current thread
699
1c79356b 700 lwz r30,ACT_MACT_PCB(r30)
9bccf70c
A
701 mr. r30,r30 ; Is there any context yet?
702 beq- makeDummyCtx ; No, hack one up...
1c79356b
A
703#if DEBUG
704/*
705 * debug assert - make sure that we're not returning to kernel
706 * get the active thread's PCB pointer and thus pointer to user state
707 */
708
d7e50217 709 lwz r3,savesrr1+4(r30)
1c79356b 710 andi. r3,r3,MASK(MSR_PR)
9bccf70c 711 bne+ ret_user2 ; We are ok...
1c79356b 712
9bccf70c
A
713 lis r0,hi16(Choke) ; Choke code
714 ori r0,r0,lo16(Choke) ; and the rest
715 li r3,failContext ; Bad state code
716 sc ; System ABEND
0b4e3aa0 717
1c79356b
A
718ret_user2:
719#endif /* DEBUG */
720
9bccf70c 721/* If the system call flag isn't set, then we came from a trap,
1c79356b
A
722 * so warp into the return_from_trap (thread_return) routine,
723 * which takes PCB pointer in R3, not in r30!
724 */
9bccf70c 725 lwz r0,SAVflags(r30) ; Grab the savearea flags
9bccf70c 726 andis. r0,r0,SAVsyscall>>16 ; Are we returning from a syscall?
d7e50217
A
727 mr r3,r30 ; Copy pcb pointer into r3 in case we need it
728 beq-- cr0,thread_return ; Nope, must be a thread return...
9bccf70c 729 b .L_thread_syscall_return ; Join up with the system call return...
1c79356b
A
730
731;
732; This is where we handle someone trying who did a thread_create followed
733; by a thread_resume with no intervening thread_set_state. Just make an
734; empty context, initialize it to trash and let em execute at 0...
9bccf70c
A
735;
736
737 .align 5
1c79356b
A
738
739makeDummyCtx:
9bccf70c
A
740 bl EXT(save_get) ; Get a save_area
741 li r4,SAVgeneral ; Get the general context type
742 li r0,0 ; Get a 0
743 stb r4,SAVflags+2(r3) ; Set type
d7e50217 744 addi r2,r3,savefpscr+4 ; Point past what we are clearing
9bccf70c 745 mr r4,r3 ; Save the start
1c79356b 746
9bccf70c
A
747cleardummy: stw r0,0(r4) ; Clear stuff
748 addi r4,r4,4 ; Next word
749 cmplw r4,r2 ; Still some more?
750 blt+ cleardummy ; Yeah...
1c79356b
A
751
752 lis r2,hi16(MSR_EXPORT_MASK_SET) ; Set the high part of the user MSR
753 ori r2,r2,lo16(MSR_EXPORT_MASK_SET) ; And the low part
d7e50217 754 stw r2,savesrr1+4(r3) ; Set the default user MSR
1c79356b 755
9bccf70c 756 b thread_return ; Go let em try to execute, hah!
1c79356b
A
757
758/*
759 * ihandler(type)
760 *
761 * ENTRY: VM switched ON
762 * Interrupts OFF
763 * R3 contains exception code
764 * R4 points to the saved context (virtual address)
765 * Everything is saved in savearea
766 *
767 */
768
0b4e3aa0
A
769 .align 5
770 .globl EXT(ihandler)
9bccf70c 771LEXT(ihandler) ; Interrupt handler */
1c79356b
A
772
773/*
774 * get the value of istackptr, if it's zero then we're already on the
9bccf70c 775 * interrupt stack.
1c79356b
A
776 */
777
d7e50217 778 lwz r10,savesrr1+4(r4) ; Get SRR1
9bccf70c
A
779 lwz r7,savevrsave(r4) ; Get the VRSAVE register
780 mfsprg r25,0 ; Get the per_proc block
781 li r14,0 ; Zero this for now
1c79356b 782 rlwinm. r13,r10,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
9bccf70c
A
783 lwz r1,PP_ISTACKPTR(r25) ; Get the interrupt stack
784 li r13,0 ; Zero this for now
785 lwz r16,PP_ACTIVE_THREAD(r25) ; Get the thread pointer
786
787 beq+ ivecoff ; Vector off, do not save vrsave...
788 stw r7,liveVRS(r25) ; Set the live value
789
d7e50217 790ivecoff: cmplwi cr1,r16,0 ; Are we still booting?
9bccf70c
A
791
792ifpoff: mr. r1,r1 ; Is it active?
793 beq- cr1,ihboot1 ; We are still coming up...
794 lwz r13,THREAD_TOP_ACT(r16) ; Pick up the active thread
795 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
796
d7e50217
A
797ihboot1: lwz r9,saver1+4(r4) ; Pick up the rupt time stack
798 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
9bccf70c
A
799 stw r13,SAVact(r4) ; Point the savearea at its activation
800 beq- cr1,ihboot4 ; We are still coming up...
801 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
802
803ihboot4: bne .L_istackfree ; Nope...
1c79356b
A
804
805/* We're already on the interrupt stack, get back the old
806 * stack pointer and make room for a frame
807 */
808
9bccf70c
A
809 lwz r10,PP_INTSTACK_TOP_SS(r25) ; Get the top of the interrupt stack
810 addi r5,r9,INTSTACK_SIZE-FM_SIZE ; Shift stack for bounds check
811 subi r1,r9,FM_REDZONE ; Back up beyond the red zone
812 sub r5,r5,r10 ; Get displacement into stack
813 cmplwi r5,INTSTACK_SIZE-FM_SIZE ; Is the stack actually invalid?
814 blt+ ihsetback ; The stack is ok...
0b4e3aa0 815
9bccf70c 816 lwz r5,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
0b4e3aa0 817 subi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
9bccf70c 818 sub r5,r1,r5 ; Get displacement into debug stack
0b4e3aa0 819 cmplwi cr2,r5,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
d7e50217 820 blt+ cr2,ihsetback ; Yeah, that is ok too...
0b4e3aa0 821
9bccf70c
A
822 lis r0,hi16(Choke) ; Choke code
823 ori r0,r0,lo16(Choke) ; and the rest
824 li r3,failStack ; Bad stack code
825 sc ; System ABEND
0b4e3aa0 826
d7e50217
A
827intUnalignedStk:
828 lis r0,hi16(Choke) ; Choke code
829 ori r0,r0,lo16(Choke) ; and the rest
830 li r3,failUnalignedStk ; Unaligned stack code
831 sc ; System ABEND
832
0b4e3aa0
A
833 .align 5
834
1c79356b 835.L_istackfree:
d7e50217
A
836 rlwinm. r0,r1,0,28,31 ; Check if stack is aligned (and get 0)
837 lwz r10,SAVflags(r4) ; Get savearea flags
838 bne-- intUnalignedStk ; Stack is unaligned...
9bccf70c 839 stw r0,PP_ISTACKPTR(r25) ; Mark the stack in use
d7e50217 840 oris r10,r10,hi16(SAVrststk) ; Indicate we reset stack when we return from this one
9bccf70c 841 stw r10,SAVflags(r4) ; Stick it back
1c79356b 842
9bccf70c
A
843/*
844 * To summarize, when we reach here, the state has been saved and
845 * the stack is marked as busy. We now generate a small
846 * stack frame with backpointers to follow the calling
847 * conventions. We set up the backpointers to the trapped
848 * routine allowing us to backtrace.
849 */
1c79356b 850
9bccf70c
A
851ihsetback: subi r1,r1,FM_SIZE ; Make a new frame
852 stw r9,FM_BACKPTR(r1) ; Point back to previous stackptr
1c79356b
A
853
854#if VERIFYSAVE
9bccf70c
A
855 beq- cr1,ihbootnover ; (TEST/DEBUG)
856 bl versave ; (TEST/DEBUG)
857ihbootnover: ; (TEST/DEBUG)
1c79356b
A
858#endif
859
860#if DEBUG
861/* If debugging, we need two frames, the first being a dummy
862 * which links back to the trapped routine. The second is
863 * that which the C routine below will need
864 */
d7e50217 865 lwz r5,savesrr0+4(r4) ; Get interrupt address
9bccf70c
A
866 stw r5,FM_LR_SAVE(r1) ; save old instr ptr as LR value
867 stwu r1,-FM_SIZE(r1) ; Make another new frame for C routine
1c79356b
A
868#endif /* DEBUG */
869
9bccf70c 870 lwz r5,savedsisr(r4) ; Get the DSISR
d7e50217 871 lwz r6,savedar+4(r4) ; Get the DAR
1c79356b
A
872
873 bl EXT(interrupt)
874
875
876/* interrupt() returns a pointer to the saved state in r3
877 *
878 * Ok, back from C. Disable interrupts while we restore things
879 */
880 .globl EXT(ihandler_ret)
881
9bccf70c 882LEXT(ihandler_ret) ; Marks our return point from debugger entry
1c79356b 883
d7e50217 884 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 885 mfmsr r0 ; Get our MSR
d7e50217
A
886 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
887 andc r0,r0,r10 ; Turn off VEC, FP, and EE
9bccf70c
A
888 mtmsr r0 ; Make sure interrupts are disabled
889 mfsprg r10,0 ; Get the per_proc block
1c79356b 890
9bccf70c
A
891 lwz r7,SAVflags(r3) ; Pick up the flags
892 lwz r8,PP_ACTIVE_THREAD(r10) ; and the active thread
d7e50217 893 lwz r9,SAVprev+4(r3) ; Get previous save area
9bccf70c 894 cmplwi cr1,r8,0 ; Are we still initializing?
d7e50217 895 lwz r12,savesrr1+4(r3) ; Get the MSR we will load on return
9bccf70c 896 lwz r8,THREAD_TOP_ACT(r8) ; Pick up the active thread
d7e50217 897 andis. r11,r7,hi16(SAVrststk) ; Is this the first on the stack?
9bccf70c 898 stw r9,ACT_MACT_PCB(r8) ; Point to previous context savearea
d7e50217 899 mr r4,r3 ; Move the savearea pointer
9bccf70c 900 beq .L_no_int_ast2 ; Get going if not the top-o-stack...
1c79356b
A
901
902
903/* We're the last frame on the stack. Restore istackptr to empty state.
904 *
905 * Check for ASTs if one of the below is true:
906 * returning to user mode
907 * returning to a kloaded server
908 */
9bccf70c
A
909 lwz r9,PP_INTSTACK_TOP_SS(r10) ; Get the empty stack value
910 andc r7,r7,r11 ; Remove the stack reset bit in case we pass this one
911 stw r9,PP_ISTACKPTR(r10) ; Save that saved state ptr
d7e50217 912 lwz r3,ACT_PREEMPT_CNT(r8) ; Get preemption level
9bccf70c
A
913 stw r7,SAVflags(r4) ; Save the flags
914 cmplwi r3, 0 ; Check for preemption
915 bne .L_no_int_ast ; Do not preempt if level is not zero
916 andi. r6,r12,MASK(MSR_PR) ; privilege mode
917 lwz r11,PP_NEED_AST(r10) ; Get the AST request address
918 lwz r11,0(r11) ; Get the request
919 beq- .L_kernel_int_ast ; In kernel space, AST_URGENT check
920 li r3,T_AST ; Assume the worst
921 mr. r11,r11 ; Are there any pending?
922 beq .L_no_int_ast ; Nope...
1c79356b
A
923 b .L_call_thandler
924
925.L_kernel_int_ast:
9bccf70c
A
926 andi. r11,r11,AST_URGENT ; Do we have AST_URGENT?
927 li r3,T_PREEMPT ; Assume the worst
928 beq .L_no_int_ast ; Nope...
1c79356b
A
929
930/*
931 * There is a pending AST. Massage things to make it look like
932 * we took a trap and jump into the trap handler. To do this
933 * we essentially pretend to return from the interrupt but
934 * at the last minute jump into the trap handler with an AST
935 * trap instead of performing an rfi.
936 */
937
9bccf70c
A
938.L_call_thandler:
939 stw r3,saveexception(r4) ; Set the exception code to T_AST/T_PREEMPT
940 b EXT(thandler) ; We need to preempt so treat like a trap...
1c79356b
A
941
942.L_no_int_ast:
9bccf70c
A
943 mr r3,r4 ; Get into the right register for common code
944
1c79356b 945.L_no_int_ast2:
9bccf70c
A
946 rlwinm r7,r7,0,15,13 ; Clear the syscall flag
947 li r4,0 ; Assume for a moment that we are in init
948 stw r7,SAVflags(r3) ; Set the flags with cleared syscall flag
d7e50217 949 beq-- cr1,chkfac ; Jump away if we are in init...
9bccf70c
A
950
951 lwz r4,ACT_MACT_PCB(r8) ; Get the new level marker
1c79356b
A
952
953
954;
955; This section is common to all exception exits. It throws away vector
956; and floating point saveareas as the exception level of a thread is
957; exited.
958;
959; It also enables the facility if its context is live
960; Requires:
961; R3 = Savearea to be released (virtual)
962; R4 = New top of savearea stack (could be 0)
963; R8 = pointer to activation
964; R10 = per_proc block
965;
9bccf70c
A
966; Note that barring unforseen crashes, there is no escape from this point
967; on. We WILL call exception_exit and launch this context. No worries
968; about preemption or interruptions here.
969;
970; Note that we will set up R26 with whatever context we will be launching,
971; so it will indicate the current, or the deferred it it is set and we
972; are going to user state. CR2_eq will be set to indicate deferred.
973;
1c79356b 974
d7e50217 975chkfac: lwz r29,savesrr1+4(r3) ; Get the current MSR
9bccf70c 976 mr. r28,r8 ; Are we still in boot?
d7e50217
A
977 mr r31,r10 ; Move per_proc address
978 mr r30,r4 ; Preserve new level
9bccf70c 979 mr r27,r3 ; Save the old level
d7e50217 980 beq-- chkenax ; Yeah, skip it all...
1c79356b 981
9bccf70c
A
982 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going into user state?
983
9bccf70c
A
984 lwz r20,curctx(r28) ; Get our current context
985 lwz r26,deferctx(r28) ; Get any deferred context switch
d7e50217 986 li r0,1 ; Get set to hold off quickfret
9bccf70c
A
987 rlwinm r29,r29,0,MSR_FP_BIT+1,MSR_FP_BIT-1 ; Turn off floating point for now
988 lwz r21,FPUlevel(r20) ; Get the facility level
989 cmplwi cr2,r26,0 ; Are we going into a deferred context later?
990 rlwinm r29,r29,0,MSR_VEC_BIT+1,MSR_VEC_BIT-1 ; Turn off vector for now
991 crnor cr2_eq,cr0_eq,cr2_eq ; Set cr2_eq if going to user state and there is deferred
9bccf70c 992 lhz r19,PP_CPU_NUMBER(r31) ; Get our CPU number
d7e50217
A
993 cmplw r27,r21 ; Are we returning from the active level?
994 stw r0,holdQFret(r31) ; Make sure we hold off releasing quickfret
995 bne++ fpuchkena ; Nope...
1c79356b 996
9bccf70c
A
997;
998; First clean up any live context we are returning from
999;
1000
1001 lwz r22,FPUcpu(r20) ; Get CPU this context was last dispatched on
1002
1003 stw r19,FPUcpu(r20) ; Claim context for us
1004
1005 eieio ; Make sure this gets out before owner clear
d7e50217
A
1006
1007#if ppSize != 4096
1008#error per_proc_info is not 4k in size
1009#endif
9bccf70c 1010
0b4e3aa0 1011 lis r23,hi16(EXT(per_proc_info)) ; Set base per_proc
d7e50217 1012 slwi r22,r22,12 ; FInd offset to the owner per_proc
0b4e3aa0 1013 ori r23,r23,lo16(EXT(per_proc_info)) ; Set base per_proc
9bccf70c
A
1014 li r24,FPUowner ; Displacement to FPU owner
1015 add r22,r23,r22 ; Point to the owner per_proc
0b4e3aa0 1016
9bccf70c 1017fpuinvothr: lwarx r23,r24,r22 ; Get the owner
d7e50217
A
1018
1019 sub r0,r23,r20 ; Subtract one from the other
1020 sub r21,r20,r23 ; Subtract the other from the one
1021 or r21,r21,r0 ; Combine them
1022 srawi r21,r21,31 ; Get a 0 if equal or -1 of not
1023 and r23,r23,r21 ; Make 0 if same, unchanged if not
1024 stwcx. r23,r24,r22 ; Try to invalidate it
1025 bne-- fpuinvothr ; Try again if there was a collision...
1026
1027 isync
0b4e3aa0 1028
9bccf70c
A
1029;
1030; Now if there is a savearea associated with the popped context, release it.
1031; Either way, pop the level to the top stacked context.
1032;
0b4e3aa0 1033
9bccf70c
A
1034 lwz r22,FPUsave(r20) ; Get pointer to the first savearea
1035 li r21,0 ; Assume we popped all the way out
1036 mr. r22,r22 ; Is there anything there?
d7e50217 1037 beq++ fpusetlvl ; No, see if we need to enable...
9bccf70c
A
1038
1039 lwz r21,SAVlevel(r22) ; Get the level of that savearea
1040 cmplw r21,r27 ; Is this the saved copy of the live stuff?
1041 bne fpusetlvl ; No, leave as is...
1042
d7e50217 1043 lwz r24,SAVprev+4(r22) ; Pick up the previous area
9bccf70c
A
1044 li r21,0 ; Assume we popped all the way out
1045 mr. r24,r24 ; Any more context stacked?
d7e50217 1046 beq-- fpuonlyone ; Nope...
9bccf70c
A
1047 lwz r21,SAVlevel(r24) ; Get the level associated with save
1048
1049fpuonlyone: stw r24,FPUsave(r20) ; Dequeue this savearea
1050
1051 rlwinm r3,r22,0,0,19 ; Find main savearea header
d7e50217
A
1052
1053 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1054 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1055 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1056 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1057 stw r8,SAVprev(r22) ; Link the old in (top)
1058 stw r9,SAVprev+4(r22) ; Link the old in (bottom)
9bccf70c 1059 xor r3,r22,r3 ; Convert to physical
d7e50217
A
1060 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1061 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
9bccf70c
A
1062
1063#if FPVECDBG
1064 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1065 li r2,0x3301 ; (TEST/DEBUG)
1066 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1067 sc ; (TEST/DEBUG)
1068#endif
1069
9bccf70c
A
1070fpusetlvl: stw r21,FPUlevel(r20) ; Save the level
1071
1c79356b 1072;
9bccf70c
A
1073; Here we check if we are at the right level
1074; We need to check the level we are entering, not the one we are exiting.
1075; Therefore, we will use the defer level if it is non-zero and we are
1076; going into user state.
1c79356b 1077;
0b4e3aa0 1078
d7e50217 1079fpuchkena: bt-- cr2_eq,fpuhasdfrd ; Skip if deferred, R26 already set up...
9bccf70c
A
1080 mr r26,r20 ; Use the non-deferred value
1081
d7e50217
A
1082fpuhasdfrd:
1083#if 0
1084 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; (TEST/DEBUG) Going into user state?
1085 beq fpunusrstt ; (TEST/DEBUG) Nope...
1086 lwz r23,FPUlevel(r26) ; (TEST/DEBUG) Get the level ID
1087 lwz r24,FPUsave(r26) ; (TEST/DEBUG) Get the first savearea
1088 mr. r23,r23 ; (TEST/DEBUG) Should be level 0
1089 beq++ fpulvl0 ; (TEST/DEBUG) Yes...
1090 BREAKPOINT_TRAP ; (TEST/DEBUG)
1091
1092fpulvl0: mr. r24,r24 ; (TEST/DEBUG) Any context?
1093 beq fpunusrstt ; (TEST/DEBUG) No...
1094 lwz r23,SAVlevel(r24) ; (TEST/DEBUG) Get level of context
1095 lwz r21,SAVprev+4(r24) ; (TEST/DEBUG) Get previous pointer
1096 mr. r23,r23 ; (TEST/DEBUG) Is this our user context?
1097 beq++ fpulvl0b ; (TEST/DEBUG) Yes...
1098 BREAKPOINT_TRAP ; (TEST/DEBUG)
1099
1100fpulvl0b: mr. r21,r21 ; (TEST/DEBUG) Is there a forward chain?
1101 beq++ fpunusrstt ; (TEST/DEBUG) Nope...
1102 BREAKPOINT_TRAP ; (TEST/DEBUG)
1103
1104fpunusrstt: ; (TEST/DEBUG)
1105#endif
1106
1107 lwz r21,FPUowner(r31) ; Get the ID of the live context
9bccf70c 1108 lwz r23,FPUlevel(r26) ; Get the level ID
9bccf70c 1109 lwz r24,FPUcpu(r26) ; Get the CPU that the context was last dispatched on
d7e50217 1110 cmplw cr3,r26,r21 ; Do we have the live context?
9bccf70c 1111 cmplw r30,r23 ; Are we about to launch the live level?
d7e50217 1112 bne-- cr3,chkvec ; No, can not possibly enable...
9bccf70c 1113 cmplw cr1,r19,r24 ; Was facility used on this processor last?
d7e50217
A
1114 bne-- chkvec ; No, not live...
1115 bne-- cr1,chkvec ; No, wrong cpu, have to enable later....
9bccf70c
A
1116
1117 lwz r24,FPUsave(r26) ; Get the first savearea
1118 mr. r24,r24 ; Any savearea?
d7e50217 1119 beq++ fpuena ; Nope...
9bccf70c 1120 lwz r25,SAVlevel(r24) ; Get the level of savearea
d7e50217 1121 lwz r0,SAVprev+4(r24) ; Get the previous
9bccf70c 1122 cmplw r30,r25 ; Is savearea for the level we are launching?
d7e50217 1123 bne++ fpuena ; No, just go enable...
9bccf70c
A
1124
1125 stw r0,FPUsave(r26) ; Pop the chain
1126
1127 rlwinm r3,r24,0,0,19 ; Find main savearea header
d7e50217
A
1128
1129 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1130 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1131 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1132 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1133 stw r8,SAVprev(r24) ; Link the old in (top)
1134 stw r9,SAVprev+4(r24) ; Link the old in (bottom)
9bccf70c 1135 xor r3,r24,r3 ; Convert to physical
d7e50217
A
1136 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1137 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
1c79356b
A
1138
1139#if FPVECDBG
9bccf70c
A
1140 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1141 li r2,0x3302 ; (TEST/DEBUG)
1142 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1143 sc ; (TEST/DEBUG)
1144#endif
1c79356b 1145
9bccf70c 1146fpuena: ori r29,r29,lo16(MASK(MSR_FP)) ; Enable facility
1c79356b 1147
9bccf70c 1148chkvec:
1c79356b 1149
9bccf70c
A
1150 lwz r21,VMXlevel(r20) ; Get the facility level
1151
1152 cmplw r27,r21 ; Are we returning from the active level?
1153 bne+ vmxchkena ; Nope...
1154
1155
1156;
1157; First clean up any live context we are returning from
1158;
1c79356b 1159
9bccf70c
A
1160 lwz r22,VMXcpu(r20) ; Get CPU this context was last dispatched on
1161
1162 stw r19,VMXcpu(r20) ; Claim context for us
1163
1164 eieio ; Make sure this gets out before owner clear
1165
0b4e3aa0 1166 lis r23,hi16(EXT(per_proc_info)) ; Set base per_proc
d7e50217 1167 slwi r22,r22,12 ; Find offset to the owner per_proc
0b4e3aa0 1168 ori r23,r23,lo16(EXT(per_proc_info)) ; Set base per_proc
9bccf70c
A
1169 li r24,VMXowner ; Displacement to VMX owner
1170 add r22,r23,r22 ; Point to the owner per_proc
9bccf70c
A
1171
1172vmxinvothr: lwarx r23,r24,r22 ; Get the owner
d7e50217
A
1173
1174 sub r0,r23,r20 ; Subtract one from the other
1175 sub r21,r20,r23 ; Subtract the other from the one
1176 or r21,r21,r0 ; Combine them
1177 srawi r21,r21,31 ; Get a 0 if equal or -1 of not
1178 and r23,r23,r21 ; Make 0 if same, unchanged if not
1179 stwcx. r23,r24,r22 ; Try to invalidate it
1180 bne-- vmxinvothr ; Try again if there was a collision...
1181
1182 isync
0b4e3aa0 1183
9bccf70c
A
1184;
1185; Now if there is a savearea associated with the popped context, release it.
1186; Either way, pop the level to the top stacked context.
1187;
1188
1189 lwz r22,VMXsave(r20) ; Get pointer to the first savearea
1190 li r21,0 ; Assume we popped all the way out
1191 mr. r22,r22 ; Is there anything there?
d7e50217 1192 beq++ vmxsetlvl ; No, see if we need to enable...
9bccf70c
A
1193
1194 lwz r21,SAVlevel(r22) ; Get the level of that savearea
1195 cmplw r21,r27 ; Is this the saved copy of the live stuff?
1196 bne vmxsetlvl ; No, leave as is...
1197
d7e50217 1198 lwz r24,SAVprev+4(r22) ; Pick up the previous area
9bccf70c
A
1199 li r21,0 ; Assume we popped all the way out
1200 mr. r24,r24 ; Any more context?
d7e50217 1201 beq-- vmxonlyone ; Nope...
9bccf70c
A
1202 lwz r21,SAVlevel(r24) ; Get the level associated with save
1203
1204vmxonlyone: stw r24,VMXsave(r20) ; Dequeue this savearea
1205
1206 rlwinm r3,r22,0,0,19 ; Find main savearea header
d7e50217
A
1207
1208 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1209 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1210 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1211 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1212 stw r8,SAVprev(r22) ; Link the old in (top)
1213 stw r9,SAVprev+4(r22) ; Link the old in (bottom)
1214 xor r3,r24,r3 ; Convert to physical
1215 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1216 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
9bccf70c
A
1217
1218#if FPVECDBG
1219 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1220 li r2,0x3401 ; (TEST/DEBUG)
1221 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1222 sc ; (TEST/DEBUG)
1223#endif
9bccf70c
A
1224
1225vmxsetlvl: stw r21,VMXlevel(r20) ; Save the level
0b4e3aa0 1226
9bccf70c
A
1227;
1228; Here we check if we are at the right level
1229;
1230
1231vmxchkena: lwz r21,VMXowner(r31) ; Get the ID of the live context
1232 lwz r23,VMXlevel(r26) ; Get the level ID
1233 cmplw r26,r21 ; Do we have the live context?
1234 lwz r24,VMXcpu(r26) ; Get the CPU that the context was last dispatched on
d7e50217 1235 bne-- setena ; No, can not possibly enable...
9bccf70c
A
1236 cmplw r30,r23 ; Are we about to launch the live level?
1237 cmplw cr1,r19,r24 ; Was facility used on this processor last?
d7e50217
A
1238 bne-- setena ; No, not live...
1239 bne-- cr1,setena ; No, wrong cpu, have to enable later....
9bccf70c
A
1240
1241 lwz r24,VMXsave(r26) ; Get the first savearea
1242 mr. r24,r24 ; Any savearea?
d7e50217 1243 beq++ vmxena ; Nope...
9bccf70c 1244 lwz r25,SAVlevel(r24) ; Get the level of savearea
d7e50217 1245 lwz r0,SAVprev+4(r24) ; Get the previous
9bccf70c 1246 cmplw r30,r25 ; Is savearea for the level we are launching?
d7e50217 1247 bne++ vmxena ; No, just go enable...
9bccf70c
A
1248
1249 stw r0,VMXsave(r26) ; Pop the chain
1250
1251 rlwinm r3,r24,0,0,19 ; Find main savearea header
d7e50217
A
1252
1253 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1254 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1255 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1256 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1257 stw r8,SAVprev(r24) ; Link the old in (top)
1258 stw r9,SAVprev+4(r24) ; Link the old in (bottom)
9bccf70c 1259 xor r3,r24,r3 ; Convert to physical
d7e50217
A
1260 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1261 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
9bccf70c
A
1262
1263#if FPVECDBG
1264 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1265 li r2,0x3402 ; (TEST/DEBUG)
1266 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1267 sc ; (TEST/DEBUG)
1268#endif
1269
9bccf70c 1270vmxena: oris r29,r29,hi16(MASK(MSR_VEC)) ; Enable facility
0b4e3aa0 1271
d7e50217
A
1272setena: lwz r18,cioSpace(r28) ; Get the space ID in case we are launching user
1273 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we about to launch user state?
1274 li r0,0 ; Get set to release quickfret holdoff
9bccf70c 1275 crmove cr7_eq,cr0_eq ; Remember if we are going to user state
9bccf70c 1276 rlwimi. r20,r29,(((31-floatCngbit)+(MSR_FP_BIT+1))&31),floatCngbit,floatCngbit ; Set flag if we enabled floats
d7e50217
A
1277 lwz r19,deferctx(r28) ; Get any deferred facility context switch
1278 rlwinm r20,r29,(((31-vectorCngbit)+(MSR_VEC_BIT+1))&31),vectorCngbit,vectorCngbit ; Set flag if we enabled vector
1279 stw r29,savesrr1+4(r27) ; Turn facility on or off
1280 stw r0,holdQFret(r31) ; Release quickfret
1281 oris r18,r18,hi16(cioSwitchAway) ; Set the switch-away bit in case we go to user
1282
9bccf70c
A
1283 beq setenaa ; Neither float nor vector turned on....
1284
1285 lwz r5,ACT_MACT_SPF(r28) ; Get activation copy
1286 lwz r6,spcFlags(r31) ; Get per_proc copy
1287 or r5,r5,r20 ; Set vector/float changed bits in activation
1288 or r6,r6,r20 ; Set vector/float changed bits in per_proc
1289 stw r5,ACT_MACT_SPF(r28) ; Set activation copy
1290 stw r6,spcFlags(r31) ; Set per_proc copy
1291
1292setenaa: mfdec r24 ; Get decrementer
1293 bf+ cr2_eq,nodefer ; No deferred to switch to...
1294
1295 li r20,0 ; Clear this
1296 stw r26,curctx(r28) ; Make the facility context current
1297 stw r20,deferctx(r28) ; Clear deferred context
1298
1299nodefer: lwz r22,qactTimer(r28) ; Get high order quick activation timer
1300 mr. r24,r24 ; See if it has popped already...
1301 lwz r23,qactTimer+4(r28) ; Get low order qact timer
d7e50217 1302 ble- chkifuser ; We have popped or are just about to...
9bccf70c
A
1303
1304segtb: mftbu r20 ; Get the upper time base
1305 mftb r21 ; Get the low
1306 mftbu r19 ; Get upper again
1307 or. r0,r22,r23 ; Any time set?
1308 cmplw cr1,r20,r19 ; Did they change?
d7e50217
A
1309 beq++ chkifuser ; No time set....
1310 bne-- cr1,segtb ; Timebase ticked, get them again...
9bccf70c
A
1311
1312 subfc r6,r21,r23 ; Subtract current from qact time
1313 li r0,0 ; Make a 0
1314 subfe r5,r20,r22 ; Finish subtract
1315 subfze r0,r0 ; Get a 0 if qact was bigger than current, -1 otherwise
1316 andc. r12,r5,r0 ; Set 0 if qact has passed
1317 andc r13,r6,r0 ; Set 0 if qact has passed
d7e50217 1318 bne chkifuser ; If high order is non-zero, this is too big for a decrementer
9bccf70c 1319 cmplw r13,r24 ; Is this earlier than the decrementer? (logical compare takes care of high bit on)
d7e50217 1320 bge++ chkifuser ; No, do not reset decrementer...
1c79356b 1321
9bccf70c 1322 mtdec r13 ; Set our value
1c79356b 1323
d7e50217
A
1324chkifuser: beq-- cr7,chkenax ; Skip this if we are going to kernel...
1325 stw r18,cioSpace(r28) ; Half-invalidate to force MapUserAddressSpace to reload SRs
1326
9bccf70c 1327chkenax:
1c79356b
A
1328
1329
1330#if DEBUG
9bccf70c
A
1331 lwz r20,SAVact(r27) ; (TEST/DEBUG) Make sure our restore
1332 lwz r21,PP_ACTIVE_THREAD(r31) ; (TEST/DEBUG) with the current act.
1333 cmpwi r21,0 ; (TEST/DEBUG)
1334 beq- yeswereok ; (TEST/DEBUG)
1335 lwz r21,THREAD_TOP_ACT(r21) ; (TEST/DEBUG)
1336 cmplw r21,r20 ; (TEST/DEBUG)
1337 beq+ yeswereok ; (TEST/DEBUG)
1338
1339 lis r0,hi16(Choke) ; (TEST/DEBUG) Choke code
1340 ori r0,r0,lo16(Choke) ; (TEST/DEBUG) and the rest
1341 mr r21,r27 ; (TEST/DEBUG) Save the savearea address
1342 li r3,failContext ; (TEST/DEBUG) Bad state code
1343 sc ; (TEST/DEBUG) System ABEND
1c79356b
A
1344
1345yeswereok:
1346#endif
1347
d7e50217 1348 mr r3,r27 ; Pass savearea back
9bccf70c 1349 b EXT(exception_exit) ; We are all done now...
1c79356b
A
1350
1351
1352
d7e50217
A
1353;
1354; Null PPC call - performance testing, does absolutely nothing
1355;
1356
1357 .align 5
1358
1359 .globl EXT(ppcNull)
1360
1361LEXT(ppcNull)
1362
1363 li r3,-1 ; Make sure we test no asts
1364 blr
1365
1366
1367;
1368; Instrumented null PPC call - performance testing, does absolutely nothing
1369; Forces various timestamps to be returned.
1370;
1371
1372 .align 5
1373
1374 .globl EXT(ppcNullinst)
1375
1376LEXT(ppcNullinst)
1377
1378 li r3,-1 ; Make sure we test no asts
1379 blr
1380
1381
1c79356b
A
1382/*
1383 * Here's where we handle the fastpath stuff
1384 * We'll do what we can here because registers are already
1385 * loaded and it will be less confusing that moving them around.
1386 * If we need to though, we'll branch off somewhere's else.
1387 *
1388 * Registers when we get here:
1389 *
1390 * r0 = syscall number
1391 * r4 = savearea/pcb
1392 * r13 = activation
1393 * r14 = previous savearea (if any)
1394 * r16 = thread
1395 * r25 = per_proc
1396 */
1397
0b4e3aa0
A
1398 .align 5
1399
d7e50217
A
1400fastpath: cmplwi cr3,r0,0x7FF5 ; Is this a null fastpath?
1401 beq-- cr3,fastexutl ; Yes, bail fast...
1402 cmplwi cr3,r0,0x7FF1 ; Is it CthreadSetSelfNumber?
1403 bnelr-- cr3 ; Not a fast path...
1c79356b
A
1404
1405/*
1406 * void cthread_set_self(cproc_t p)
1407 *
1408 * set's thread state "user_value"
1409 *
1410 * This op is invoked as follows:
1411 * li r0, CthreadSetSelfNumber // load the fast-trap number
1412 * sc // invoke fast-trap
1413 * blr
1414 *
1415 */
1416
1417CthreadSetSelfNumber:
1418
d7e50217 1419 lwz r5,saver3+4(r4) /* Retrieve the self number */
1c79356b
A
1420 stw r5,CTHREAD_SELF(r13) /* Remember it */
1421 stw r5,UAW(r25) /* Prime the per_proc_info with it */
1422
1423
1424 .globl EXT(fastexit)
1425EXT(fastexit):
d7e50217
A
1426fastexutl: mr r3,r4 ; Pass back savearea
1427 b EXT(exception_exit) ; Go back to the caller...
1c79356b
A
1428
1429
1430/*
1431 * Here's where we check for a hit on the Blue Box Assist
1432 * Most registers are non-volatile, so be careful here. If we don't
1433 * recognize the trap instruction we go back for regular processing.
1434 * Otherwise we transfer to the assist code.
1435 */
1436
0b4e3aa0
A
1437 .align 5
1438
1c79356b 1439checkassist:
0b4e3aa0 1440 lwz r0,saveexception(r4) ; Get the exception code
d7e50217 1441 lwz r23,savesrr1+4(r4) ; Get the interrupted MSR
0b4e3aa0
A
1442 lwz r26,ACT_MACT_BEDA(r13) ; Get Blue Box Descriptor Area
1443 mtcrf 0x18,r23 ; Check what SRR1 says
1444 lwz r24,ACT_MACT_BTS(r13) ; Get the table start
1445 cmplwi r0,T_AST ; Check for T_AST trap
d7e50217 1446 lwz r27,savesrr0+4(r4) ; Get trapped address
0b4e3aa0
A
1447 crnand cr1_eq,SRR1_PRG_TRAP_BIT,MSR_PR_BIT ; We need both trap and user state
1448 sub r24,r27,r24 ; See how far into it we are
1449 cror cr0_eq,cr0_eq,cr1_eq ; Need to bail if AST or not trap or not user state
1450 cmplwi cr1,r24,BB_MAX_TRAP ; Do we fit in the list?
1451 cror cr0_eq,cr0_eq,cr1_gt ; Also leave it trap not in range
1452 btlr- cr0_eq ; No assist if AST or not trap or not user state or trap not in range
1453 b EXT(atomic_switch_trap) ; Go to the assist...
1c79356b
A
1454
1455;
1456; Virtual Machine Monitor
1457; Here is where we exit from the emulated context
1458; Note that most registers get trashed here
1459; R3 and R30 are preserved across the call and hold the activation
1460; and savearea respectivily.
1461;
1462
0b4e3aa0
A
1463 .align 5
1464
1c79356b
A
1465exitFromVM: mr r30,r4 ; Get the savearea
1466 mr r3,r13 ; Get the activation
1467
1468 b EXT(vmm_exit) ; Do it to it
1469
1470 .align 5
1471 .globl EXT(retFromVM)
1472
1473LEXT(retFromVM)
1474 mfsprg r10,0 ; Restore the per_proc info
1475 mr r8,r3 ; Get the activation
d7e50217 1476 lwz r4,SAVprev+4(r30) ; Pick up the previous savearea
1c79356b
A
1477 mr r3,r30 ; Put savearea in proper register for common code
1478 lwz r11,SAVflags(r30) ; Get the flags of the current savearea
1479 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
1480 lwz r1,ACT_THREAD(r8) ; and the active thread
1481 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
1482
1483 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
1484
1485 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
1486 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
1487 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
1488 b chkfac ; Go end it all...
1489
1490
0b4e3aa0
A
1491;
1492; chandler (note: not a candle maker or tallow merchant)
1493;
1494; Here is the system choke handler. This is where the system goes
1495; to die.
1496;
1497; We get here as a result of a T_CHOKE exception which is generated
1498; by the Choke firmware call or by lowmem_vectors when it detects a
1499; fatal error. Examples of where this may be used is when we detect
1500; problems in low-level mapping chains, trashed savearea free chains,
1501; or stack guardpage violations.
1502;
1503; Note that we can not set a back chain in the stack when we come
1504; here because we are probably here because the chain was corrupt.
1505;
1c79356b 1506
0b4e3aa0
A
1507
1508 .align 5
1509 .globl EXT(chandler)
d7e50217 1510LEXT(chandler) ; Choke handler
0b4e3aa0 1511
d7e50217
A
1512 li r31,0 ; Get a 0
1513 mfsprg r25,0 ; Get the per_proc
1514 stw r31,traceMask(0) ; Force tracing off right now
0b4e3aa0
A
1515
1516
0b4e3aa0
A
1517
1518 lwz r1,PP_DEBSTACKPTR(r25) ; Get debug stack pointer
1519 cmpwi r1,-1 ; Are we already choking?
1520 bne chokefirst ; Nope...
1c79356b 1521
0b4e3aa0
A
1522chokespin: addi r31,r31,1 ; Spin and hope for an analyzer connection...
1523 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1524 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1525 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1526 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1527 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1528 b chokespin ; Spin and hope for an analyzer connection...
1c79356b 1529
0b4e3aa0
A
1530chokefirst: li r0,-1 ; Set choke value
1531 mr. r1,r1 ; See if we are on debug stack yet
d7e50217 1532 lwz r10,saver1+4(r4) ;
0b4e3aa0
A
1533 stw r0,PP_DEBSTACKPTR(r25) ; Show we are choking
1534 bne chokestart ; We are not on the debug stack yet...
1c79356b 1535
0b4e3aa0
A
1536 lwz r2,PP_DEBSTACK_TOP_SS(r25) ; Get debug stack top
1537 sub r11,r2,r10 ; Get stack depth
1c79356b 1538
0b4e3aa0
A
1539 cmplwi r11,KERNEL_STACK_SIZE-FM_SIZE-TRAP_SPACE_NEEDED ; Check if stack pointer is ok
1540 bgt chokespin ; Bad stack pointer or too little left, just die...
1c79356b 1541
0b4e3aa0
A
1542 subi r1,r10,FM_REDZONE ; Make a red zone
1543
1544chokestart: li r0,0 ; Get a zero
1545 stw r0,FM_BACKPTR(r1) ; We now have terminated the back chain
1546
1547 bl EXT(SysChoked) ; Call the "C" phase of this
1548 b chokespin ; Should not be here so just go spin...
1549
1c79356b
A
1550
1551#if VERIFYSAVE
1552;
1553; Savearea chain verification
1554;
1555
1556versave:
d7e50217
A
1557#if 0
1558 lis r22,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1559 ori r22,r22,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1560 lwz r23,0(r22) ; (TEST/DEBUG)
1561 mr. r23,r23 ; (TEST/DEBUG)
1562 beqlr- ; (TEST/DEBUG)
1563 mfsprg r20,0 ; (TEST/DEBUG)
1564 lwz r21,pfAvailable(r20) ; (TEST/DEBUG)
1565 mr. r21,r21 ; (TEST/DEBUG)
1566 bnelr+ ; (TEST/DEBUG)
1567
1568 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1569 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1570#endif
1571
1572#if 0
1c79356b 1573;
9bccf70c 1574; Make sure that all savearea chains have the right type on them
1c79356b
A
1575;
1576
1577 lis r28,hi16(EXT(default_pset)) ; (TEST/DEBUG)
1578 lis r27,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1579 ori r28,r28,lo16(EXT(default_pset)) ; (TEST/DEBUG)
1580 ori r27,r27,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1581 li r20,0 ; (TEST/DEBUG)
1582 lwz r26,0(r27) ; (TEST/DEBUG)
1583 lwz r27,psthreadcnt(r28) ; (TEST/DEBUG)
9bccf70c 1584 mr. r26,r26 ; (TEST/DEBUG) Have we locked the test out?
1c79356b 1585 lwz r28,psthreads(r28) ; (TEST/DEBUG)
9bccf70c
A
1586 mflr r31 ; (TEST/DEBUG) Save return
1587 bnelr- ; (TEST/DEBUG) Test already triggered, skip...
1588 b fckgo ; (TEST/DEBUG) Join up...
1c79356b 1589
9bccf70c
A
1590fcknext: mr. r27,r27 ; (TEST/DEBUG) Any more threads?
1591 bne+ fckxxx ; (TEST/DEBUG) Yes...
1592
1593 mtlr r31 ; (TEST/DEBUG) Restore return
1594 blr ; (TEST/DEBUG) Leave...
1c79356b 1595
9bccf70c 1596fckxxx: lwz r28,THREAD_PSTHRN(r28) ; (TEST/DEBUG) Get next thread
1c79356b 1597
9bccf70c
A
1598fckgo: subi r27,r27,1 ; (TEST/DEBUG) Decrement thread count
1599 lwz r24,THREAD_TOP_ACT(r28) ; (TEST/DEBUG) Get activation for the thread
1600 lwz r20,ACT_MACT_PCB(r24) ; (TEST/DEBUG) Get the normal context
1601 li r21,SAVgeneral ; (TEST/DEBUG) Make sure this is all general context
1602 bl versavetype ; (TEST/DEBUG) Check the chain
1c79356b 1603
9bccf70c
A
1604 lwz r20,facctx+FPUsave(r24) ; (TEST/DEBUG) Get regular floating point
1605 li r21,SAVfloat ; (TEST/DEBUG) Make sure this is all floating point
1606 bl versavetype ; (TEST/DEBUG) Check the chain
1c79356b 1607
9bccf70c
A
1608 lwz r20,facctx+VMXsave(r24) ; (TEST/DEBUG) Get regular vector point
1609 li r21,SAVvector ; (TEST/DEBUG) Make sure this is all vector
1610 bl versavetype ; (TEST/DEBUG) Check the chain
1c79356b 1611
9bccf70c
A
1612 lwz r29,vmmControl(r24) ; (TEST/DEBUG) Get the virtual machine control blocks
1613 mr. r29,r29 ; (TEST/DEBUG) Are there any?
1614 beq+ fcknext ; (TEST/DEBUG) Nope, next thread...
1c79356b 1615
9bccf70c
A
1616 li r22,kVmmMaxContextsPerThread ; (TEST/DEBUG) Get the number of control blocks
1617 subi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get running start
1618
1619fcknvmm: subi r22,r22,1 ; (TEST/DEBUG) Do all of them
1620 mr. r22,r22 ; (TEST/DEBUG) Are we all done?
1621 addi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get the next entry
1622 blt- fcknext ; (TEST/DEBUG) Yes, check next thread...
1623
1624 lwz r23,vmmFlags(r29) ; (TEST/DEBUG) Get entry flags
1625 rlwinm. r23,r23,0,0,0 ; (TEST/DEBUG) Is this in use?
1626 beq+ fcknvmm ; (TEST/DEBUG) Not in use...
1627
1628 lwz r20,vmmFacCtx+FPUsave(r29) ; (TEST/DEBUG) Get regular floating point
1629 li r21,SAVfloat ; (TEST/DEBUG) Make sure this is all floating point
1630 bl versavetype ; (TEST/DEBUG) Check the chain
1631
1632 lwz r20,vmmFacCtx+VMXsave(r29) ; (TEST/DEBUG) Get regular vector point
1633 li r21,SAVvector ; (TEST/DEBUG) Make sure this is all vector
1634 bl versavetype ; (TEST/DEBUG) Check the chain
1635 b fcknvmm ; (TEST/DEBUG) Get then vmm block...
1636
1637versavetype:
1638 mr. r20,r20 ; (TEST/DEBUG) Chain done?
1639 beqlr- ; (TEST/DEBUG) Yes...
1640
1641 lwz r23,SAVflags(r20) ; (TEST/DEBUG) Get the flags
1642 rlwinm r23,r23,24,24,31 ; (TEST/DEBUG) Position it
1643 cmplw r23,r21 ; (TEST/DEBUG) Are we the correct type?
1644 beq+ versvok ; (TEST/DEBUG) This one is ok...
1645
1646 lis r22,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1647 ori r22,r22,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1648 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1649 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1650
d7e50217 1651versvok: lwz r20,SAVprev+4(r20) ; (TEST/DEBUG) Get the previous one
9bccf70c 1652 b versavetype ; (TEST/DEBUG) Go check its type...
1c79356b
A
1653#endif
1654
1c79356b 1655
1c79356b 1656#endif