2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
29 /* Low level routines dealing with exception entry and exit.
30 * There are various types of exception:
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).
36 * The code for the FPU disabled handler (lazy fpu) is in cswtch.s
40 #include <mach_assert.h>
41 #include <mach/exception_types.h>
42 #include <mach/ppc/vm_param.h>
47 #include <ppc/proc_reg.h>
49 #include <ppc/exception.h>
50 #include <ppc/savearea.h>
61 * ENTRY: VM switched ON
63 * R3 contains exception code
64 * R4 points to the saved context (virtual address)
65 * Everything is saved in savearea
69 * If pcb.ksp == 0 then the kernel stack is already busy,
70 * we make a stack frame
71 * leaving enough space for the 'red zone' in case the
72 * trapped thread was in the middle of saving state below
75 * otherwise we make a stack frame and
76 * the kernel stack (setting pcb.ksp to 0)
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
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 :-)
89 #define TRAP_SPACE_NEEDED FM_REDZONE+(2*FM_SIZE)+256
95 LEXT(thandler) ; Trap handler
97 mfsprg r25,0 ; Get the per_proc
99 lwz r1,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
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
107 rlwinm. r26,r26,0,bbThreadbit,bbThreadbit ; Do we have Blue Box Assist active?
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
111 stw r8,SAVprev+4(r4) ; Queue the new save area in the front
114 bl versave ; (TEST/DEBUG)
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
120 lwz r26,saver1+4(r4) ; Get the stack at interrupt time
122 bne+ cr1,.L_kstackfree ; We are not on kernel stack yet...
124 subi r1,r26,FM_REDZONE ; Make a red zone on interrupt time kernel stack
127 lwz r7,savesrr1+4(r4) ; Pick up the entry MSR
128 sub r9,r1,r9 ; Get displacment into the kernel stack
129 li r0,0 ; Make this 0
130 rlwinm. r0,r9,0,28,31 ; Verify that we have a 16-byte aligned stack (and get a 0)
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
134 stw r0,ACT_MACT_KSP(r13) ; Show that we have taken the stack
137 lwz r9,savevrsave(r4) ; Get the VRSAVE register
138 bne-- kernelStackUnaligned ; Stack is unaligned...
139 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
140 subi r1,r1,FM_SIZE ; Push a header onto the current stack
141 bgt-- cr2,kernelStackBad ; Kernel stack is bogus...
143 kernelStackNotBad: ; Vector was off
144 beq++ tvecoff ; Vector off, do not save vrsave...
145 stw r9,liveVRS(r25) ; Set the live value
147 tvecoff: stw r26,FM_BACKPTR(r1) ; Link back to the previous frame
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
154 lwz r3,savesrr0+4(r4) ; Get the point of interruption
155 stw r3,FM_LR_SAVE(r1) ; save old instr ptr as LR value
156 stwu r1, -FM_SIZE(r1) ; and make new frame
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)
168 lwz r3,saveexception(r4) ; Get the exception code
169 lwz r0,ACT_MACT_SPF(r13) ; Get the special flags
171 addi r5,r3,-T_DATA_ACCESS ; Adjust to start of range
172 rlwinm. r0,r0,0,runningVMbit,runningVMbit ; Are we in VM state? (cr0_eq == 0 if yes)
173 cmplwi cr2,r5,T_TRACE-T_DATA_ACCESS ; Are we still in range? (cr_gt if not)
175 lwz r5,savedsisr(r4) ; Get the saved DSISR
177 crnor cr7_eq,cr0_eq,cr2_gt ; We should intercept if in VM and is a true trap (cr7_eq == 1 if yes)
178 rlwinm. r0,r7,0,MSR_PR_BIT,MSR_PR_BIT ; Are we trapping from supervisor state? (cr0_eq == 1 if yes)
180 cmpi cr2,r3,T_PREEMPT ; Is this a preemption?
182 crandc cr0_eq,cr7_eq,cr0_eq ; Do not intercept if we are in the kernel (cr0_eq == 1 if yes)
184 lwz r6,savedar(r4) ; Get the DAR (top)
185 lwz r7,savedar+4(r4) ; Get the DAR (bottom)
187 beq- cr2,.L_call_trap ; Do not turn on interrupts for T_PREEMPT
188 beq- exitFromVM ; Any true trap but T_MACHINE_CHECK exits us from the VM...
190 /* syscall exception might warp here if there's nothing left
191 * to do except generate a trap
198 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
199 mfmsr r7 ; Get the MSR
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
202 mtmsr r7 ; Disable for interrupts
203 mfsprg r10,0 ; Restore the per_proc info
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.
211 lwz r11,SAVflags(r3) ; Get the flags of the current savearea
212 lwz r0,savesrr1+4(r3) ; Get the MSR we are going to
213 lwz r4,SAVprev+4(r3) ; Pick up the previous savearea
214 mfsprg r8,1 ; Get the current activation
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?
218 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
220 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
221 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
222 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
224 beq-- chkfac ; We are not leaving the kernel yet...
226 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
227 b chkfac ; Go end it all...
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.
237 lwz r3,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
238 subi r3,r3,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
239 sub r3,r1,r3 ; Get displacement into debug stack
240 cmplwi cr2,r3,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
241 blt+ cr2,kernelStackNotBad ; Yeah, that is ok too...
243 lis r0,hi16(Choke) ; Choke code
244 ori r0,r0,lo16(Choke) ; and the rest
245 li r3,failStack ; Bad stack code
248 kernelStackUnaligned:
249 lis r0,hi16(Choke) ; Choke code
250 ori r0,r0,lo16(Choke) ; and the rest
251 li r3,failUnalignedStk ; Unaligned stack code
258 * ENTRY: VM switched ON
260 * R3 contains exception code
261 * R4 points to the saved context (virtual address)
262 * Everything is saved in savearea
266 * If pcb.ksp == 0 then the kernel stack is already busy,
267 * this is an error - jump to the debugger entry
269 * otherwise depending upon the type of
270 * syscall, look it up in the kernel table
271 * or pass it to the server.
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.
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
287 LEXT(shandler) ; System call handler
289 lwz r7,savesrr1+4(r4) ; Get the SRR1 value
290 mfsprg r25,0 ; Get the per proc area
291 lwz r0,saver0+4(r4) ; Get the original syscall number
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?
295 lwz r9,savevrsave(r4) ; Get the VRsave register
296 beq-- EXT(ihandler) ; On interrupt stack, not allowed...
297 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
298 lwz r16,PP_ACTIVE_THREAD(r25) ; Get the thread pointer
299 mfsprg r13,1 ; Pick up the active thread
301 beq++ svecoff ; Vector off, do not save vrsave...
302 stw r9,liveVRS(r25) ; Set the live value
304 ; Check if SCs are being redirected for the BlueBox or to VMM
307 svecoff: lwz r6,ACT_MACT_SPF(r13) ; Pick up activation special flags
308 mtcrf 0x40,r6 ; Check special flags
309 mtcrf 0x01,r6 ; Check special flags
310 crmove cr6_eq,runningVMbit ; Remember if we are in VMM
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:
317 lwz r26,saver3+4(r4) ; Get the original syscall number
318 cmpwi cr6,r26,kvmmExitToHost ; vmm_exit_to_host request
320 bf++ bbNoMachSCbit,noassist ; Take branch if SCs are not redirected
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...
324 noassist: cmplwi r15,0x7000 ; Do we have a fast path trap?
325 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
326 beql fastpath ; We think it is a fastpath...
328 lwz r1,ACT_MACT_KSP(r13) ; Get the kernel stack pointer
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?
335 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
336 li r0,0 ; Clear this out
337 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
338 stw r13,SAVact(r4) ; Point the savearea at its activation
341 bl versave ; (TEST/DEBUG)
344 lwz r15,saver1+4(r4) ; Grab interrupt time stack
345 mr r30,r4 ; Save pointer to the new context savearea
346 stw r0,ACT_MACT_KSP(r13) ; Mark stack as busy with 0 val
347 stw r15,FM_BACKPTR(r1) ; Link stack frame backwards
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
354 lwz r8,savesrr0+4(r30) ; Get the point of interruption
355 stw r8,FM_LR_SAVE(r1) ; Save old instr ptr as LR value
356 stwu r1, -FM_SIZE(r1) ; and make new frame
359 lwz r15,SAVflags(r30) ; Get the savearea flags
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
363 ori r11,r11,lo16(MASK(MSR_EE)) ; Turn on interruption enabled bit
364 rlwinm r10,r0,0,0,19 ; Keep only the top part
365 oris r15,r15,SAVsyscall >> 16 ; Mark that it this is a syscall
366 cmplwi r10,0x6000 ; Is it the special ppc-only guy?
367 stw r15,SAVflags(r30) ; Save syscall marker
368 beq-- cr6,exitFromVM ; It is time to exit from alternate context...
370 beq- ppcscall ; Call the ppc-only system call handler...
372 mr. r0,r0 ; What kind is it?
373 mtmsr r11 ; Enable interruptions
375 blt-- .L_kernel_syscall ; System call number if negative, this is a mach call...
377 cmpwi cr0,r0,0x7FFA ; Special blue box call?
378 beq-- .L_notify_interrupt_syscall ; Yeah, call it...
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
390 stw r9,0(r10) ; Save it back
391 bl EXT(unix_syscall) ; Check out unix...
393 .L_call_server_syscall_exception:
394 li r3,EXC_SYSCALL ; doexception(EXC_SYSCALL, num, 1)
396 .L_call_server_exception:
397 mr r4,r0 ; Set syscall selector
399 b EXT(doexception) ; Go away, never to return...
401 .L_notify_interrupt_syscall:
402 lwz r3,saver3+4(r30) ; Get the new PC address to pass in
403 bl EXT(syscall_notify_interrupt)
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.
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.
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
424 ; Note: the last option is intended for special diagnostics calls that
425 ; want the thread to return and execute before checking for preemption.
427 ; NOTE: Both R16 (thread) and R30 (savearea) need to be preserved over this call!!!!
432 ppcscall: 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
436 bgt- .L_call_server_syscall_exception ; Bogus call...
437 lwzx r11,r10,r11 ; Get function address
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.
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
448 mtctr r11 ; Set the function address
449 beq- .L_call_server_syscall_exception ; Disabled call...
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
465 mr. r3,r3 ; See what we should do
466 mr r31,r16 ; Restore the current thread pointer
467 bgt+ .L_thread_syscall_ret_check_ast ; Take normal AST checking return....
468 mfsprg r10,0 ; Get the per_proc
469 blt+ .L_thread_syscall_return ; Return, but no ASTs....
470 lwz r0,saver0+4(r30) ; Restore the system call number
471 b .L_call_server_syscall_exception ; Go to common exit...
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)
486 ; Call a function that can print out our syscall info
487 ; Note that we don t care about any volatiles yet
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
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
503 bl EXT(syscall_trace)
505 ksysnotrc: cmplw r31,r29 ; Is this syscall in the table?
506 add r31,r27,r28 ; Point right to the syscall table entry
508 bge- .L_call_server_syscall_exception ; The syscall number is invalid
510 lwz r0,MACH_TRAP_FUNCTION(r31) ; Pick up the function address
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.
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
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
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?
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
552 /* 'standard' syscall returns here - INTERRUPTS ARE STILL ON */
554 /* r3 contains value that we're going to return to the user
558 * Ok, return from C function, R3 = return value
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 .
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
572 /* When we are here, r16 should point to the current thread,
573 * r30 should point to the current pcb
576 /* save off return value, we must load it
577 * back anyway for thread_exception_return
581 mr r31,r16 ; Move the current thread pointer
582 stw r3,saver3+4(r30) ; Stash the return code
584 .L_thread_syscall_ret_check_ast:
585 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
586 mfmsr r12 ; Get the current MSR
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
589 mtmsr r12 ; Turn interruptions off
591 mfsprg r10,0 ; Get the per_processor block
593 /* Check to see if there's an outstanding AST */
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?
598 beq++ cr0,.L_syscall_no_ast ; Nope...
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
606 /* debug assert - make sure that we're not returning to kernel */
607 lwz r3,savesrr1+4(r30)
608 andi. r3,r3,MASK(MSR_PR)
609 bne++ scrnotkern ; returning to user level, check
611 lis r0,hi16(Choke) ; Choke code
612 ori r0,r0,lo16(Choke) ; and the rest
613 li r3,failContext ; Bad state code
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...
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).
628 * In particular for us,
629 * we still have r31 points to the current thread,
630 * r30 points to the current pcb
636 .L_thread_syscall_return:
638 mr r3,r30 ; Get savearea to the correct register for common exit
639 lwz r5,THREAD_KERNEL_STACK(r31) ; Get the base pointer to the stack
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
643 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
644 stw r4,ACT_MACT_PCB(r8) ; Save previous save area
645 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
646 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
647 stw r11,SAVflags(r30) ; Stick back the flags
648 b chkfac ; Go end it all...
651 * thread_exception_return()
653 * Return to user mode directly from within a system call.
657 .globl EXT(thread_bootstrap_return)
658 LEXT(thread_bootstrap_return) ; NOTE: THIS IS GOING AWAY IN A FEW DAYS....
660 .globl EXT(thread_exception_return)
661 LEXT(thread_exception_return) ; Directly return to user mode
663 .L_thread_exc_ret_check_ast:
664 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
665 mfmsr r3 ; Get the MSR
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
668 mtmsr r3 ; Disable interrupts
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.
676 mfsprg r10,0 ; Get the per_processor block
677 lwz r4,PP_NEED_AST(r10)
682 beq+ cr0,.L_exc_ret_no_ast
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
690 b .L_thread_exc_ret_check_ast ; check for a second AST (rare)
692 /* arriving here, interrupts should be disabled */
693 /* Get the active thread's PCB pointer to restore regs
697 mfsprg r30,1 ; Get the currrent activation
698 lwz r31,ACT_THREAD(r30) ; Get the current thread
700 lwz r30,ACT_MACT_PCB(r30)
701 mr. r30,r30 ; Is there any context yet?
702 beq- makeDummyCtx ; No, hack one up...
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
709 lwz r3,savesrr1+4(r30)
710 andi. r3,r3,MASK(MSR_PR)
711 bne+ ret_user2 ; We are ok...
713 lis r0,hi16(Choke) ; Choke code
714 ori r0,r0,lo16(Choke) ; and the rest
715 li r3,failContext ; Bad state code
721 /* If the system call flag isn't set, then we came from a trap,
722 * so warp into the return_from_trap (thread_return) routine,
723 * which takes PCB pointer in R3, not in r30!
725 lwz r0,SAVflags(r30) ; Grab the savearea flags
726 andis. r0,r0,SAVsyscall>>16 ; Are we returning from a syscall?
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...
729 b .L_thread_syscall_return ; Join up with the system call return...
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...
740 bl EXT(save_get) ; Get a save_area
741 li r4,SAVgeneral ; Get the general context type
743 stb r4,SAVflags+2(r3) ; Set type
744 addi r2,r3,savefpscr+4 ; Point past what we are clearing
745 mr r4,r3 ; Save the start
747 cleardummy: stw r0,0(r4) ; Clear stuff
748 addi r4,r4,4 ; Next word
749 cmplw r4,r2 ; Still some more?
750 blt+ cleardummy ; Yeah...
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
754 stw r2,savesrr1+4(r3) ; Set the default user MSR
756 b thread_return ; Go let em try to execute, hah!
761 * ENTRY: VM switched ON
763 * R3 contains exception code
764 * R4 points to the saved context (virtual address)
765 * Everything is saved in savearea
771 LEXT(ihandler) ; Interrupt handler */
774 * get the value of istackptr, if it's zero then we're already on the
778 lwz r10,savesrr1+4(r4) ; Get SRR1
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
782 rlwinm. r13,r10,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
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
787 beq+ ivecoff ; Vector off, do not save vrsave...
788 stw r7,liveVRS(r25) ; Set the live value
790 ivecoff: cmplwi cr1,r16,0 ; Are we still booting?
792 ifpoff: 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
797 ihboot1: 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
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
803 ihboot4: bne .L_istackfree ; Nope...
805 /* We're already on the interrupt stack, get back the old
806 * stack pointer and make room for a frame
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...
816 lwz r5,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
817 subi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
818 sub r5,r1,r5 ; Get displacement into debug stack
819 cmplwi cr2,r5,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
820 blt+ cr2,ihsetback ; Yeah, that is ok too...
822 lis r0,hi16(Choke) ; Choke code
823 ori r0,r0,lo16(Choke) ; and the rest
824 li r3,failStack ; Bad stack code
828 lis r0,hi16(Choke) ; Choke code
829 ori r0,r0,lo16(Choke) ; and the rest
830 li r3,failUnalignedStk ; Unaligned stack code
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...
839 stw r0,PP_ISTACKPTR(r25) ; Mark the stack in use
840 oris r10,r10,hi16(SAVrststk) ; Indicate we reset stack when we return from this one
841 stw r10,SAVflags(r4) ; Stick it back
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.
851 ihsetback: subi r1,r1,FM_SIZE ; Make a new frame
852 stw r9,FM_BACKPTR(r1) ; Point back to previous stackptr
855 beq- cr1,ihbootnover ; (TEST/DEBUG)
856 bl versave ; (TEST/DEBUG)
857 ihbootnover: ; (TEST/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
865 lwz r5,savesrr0+4(r4) ; Get interrupt address
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
870 lwz r5,savedsisr(r4) ; Get the DSISR
871 lwz r6,savedar+4(r4) ; Get the DAR
876 /* interrupt() returns a pointer to the saved state in r3
878 * Ok, back from C. Disable interrupts while we restore things
880 .globl EXT(ihandler_ret)
882 LEXT(ihandler_ret) ; Marks our return point from debugger entry
884 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
885 mfmsr r0 ; Get our MSR
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
888 mtmsr r0 ; Make sure interrupts are disabled
889 mfsprg r10,0 ; Get the per_proc block
891 lwz r7,SAVflags(r3) ; Pick up the flags
892 lwz r8,PP_ACTIVE_THREAD(r10) ; and the active thread
893 lwz r9,SAVprev+4(r3) ; Get previous save area
894 cmplwi cr1,r8,0 ; Are we still initializing?
895 lwz r12,savesrr1+4(r3) ; Get the MSR we will load on return
896 lwz r8,THREAD_TOP_ACT(r8) ; Pick up the active thread
897 andis. r11,r7,hi16(SAVrststk) ; Is this the first on the stack?
898 stw r9,ACT_MACT_PCB(r8) ; Point to previous context savearea
899 mr r4,r3 ; Move the savearea pointer
900 beq .L_no_int_ast2 ; Get going if not the top-o-stack...
903 /* We're the last frame on the stack. Restore istackptr to empty state.
905 * Check for ASTs if one of the below is true:
906 * returning to user mode
907 * returning to a kloaded server
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
912 lwz r3,ACT_PREEMPT_CNT(r8) ; Get preemption level
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...
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...
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.
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...
943 mr r3,r4 ; Get into the right register for common code
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
949 beq-- cr1,chkfac ; Jump away if we are in init...
951 lwz r4,ACT_MACT_PCB(r8) ; Get the new level marker
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
959 ; It also enables the facility if its context is live
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
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.
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.
975 chkfac: lwz r29,savesrr1+4(r3) ; Get the current MSR
976 mr. r28,r8 ; Are we still in boot?
977 mr r31,r10 ; Move per_proc address
978 mr r30,r4 ; Preserve new level
979 mr r27,r3 ; Save the old level
980 beq-- chkenax ; Yeah, skip it all...
982 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going into user state?
984 lwz r20,curctx(r28) ; Get our current context
985 lwz r26,deferctx(r28) ; Get any deferred context switch
986 li r0,1 ; Get set to hold off quickfret
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
992 lhz r19,PP_CPU_NUMBER(r31) ; Get our CPU number
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...
998 ; First clean up any live context we are returning from
1001 lwz r22,FPUcpu(r20) ; Get CPU this context was last dispatched on
1003 stw r19,FPUcpu(r20) ; Claim context for us
1005 eieio ; Make sure this gets out before owner clear
1008 #error per_proc_info is not 4k in size
1011 lis r23,hi16(EXT(per_proc_info)) ; Set base per_proc
1012 slwi r22,r22,12 ; FInd offset to the owner per_proc
1013 ori r23,r23,lo16(EXT(per_proc_info)) ; Set base per_proc
1014 li r24,FPUowner ; Displacement to FPU owner
1015 add r22,r23,r22 ; Point to the owner per_proc
1017 fpuinvothr: lwarx r23,r24,r22 ; Get the owner
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...
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.
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?
1037 beq++ fpusetlvl ; No, see if we need to enable...
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...
1043 lwz r24,SAVprev+4(r22) ; Pick up the previous area
1044 li r21,0 ; Assume we popped all the way out
1045 mr. r24,r24 ; Any more context stacked?
1046 beq-- fpuonlyone ; Nope...
1047 lwz r21,SAVlevel(r24) ; Get the level associated with save
1049 fpuonlyone: stw r24,FPUsave(r20) ; Dequeue this savearea
1051 rlwinm r3,r22,0,0,19 ; Find main savearea header
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)
1059 xor r3,r22,r3 ; Convert to physical
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)
1064 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1065 li r2,0x3301 ; (TEST/DEBUG)
1066 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1070 fpusetlvl: stw r21,FPUlevel(r20) ; Save the level
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.
1079 fpuchkena: bt-- cr2_eq,fpuhasdfrd ; Skip if deferred, R26 already set up...
1080 mr r26,r20 ; Use the non-deferred value
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)
1092 fpulvl0: 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)
1100 fpulvl0b: mr. r21,r21 ; (TEST/DEBUG) Is there a forward chain?
1101 beq++ fpunusrstt ; (TEST/DEBUG) Nope...
1102 BREAKPOINT_TRAP ; (TEST/DEBUG)
1104 fpunusrstt: ; (TEST/DEBUG)
1107 lwz r21,FPUowner(r31) ; Get the ID of the live context
1108 lwz r23,FPUlevel(r26) ; Get the level ID
1109 lwz r24,FPUcpu(r26) ; Get the CPU that the context was last dispatched on
1110 cmplw cr3,r26,r21 ; Do we have the live context?
1111 cmplw r30,r23 ; Are we about to launch the live level?
1112 bne-- cr3,chkvec ; No, can not possibly enable...
1113 cmplw cr1,r19,r24 ; Was facility used on this processor last?
1114 bne-- chkvec ; No, not live...
1115 bne-- cr1,chkvec ; No, wrong cpu, have to enable later....
1117 lwz r24,FPUsave(r26) ; Get the first savearea
1118 mr. r24,r24 ; Any savearea?
1119 beq++ fpuena ; Nope...
1120 lwz r25,SAVlevel(r24) ; Get the level of savearea
1121 lwz r0,SAVprev+4(r24) ; Get the previous
1122 cmplw r30,r25 ; Is savearea for the level we are launching?
1123 bne++ fpuena ; No, just go enable...
1125 stw r0,FPUsave(r26) ; Pop the chain
1127 rlwinm r3,r24,0,0,19 ; Find main savearea header
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)
1135 xor r3,r24,r3 ; Convert to physical
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)
1140 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1141 li r2,0x3302 ; (TEST/DEBUG)
1142 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1146 fpuena: ori r29,r29,lo16(MASK(MSR_FP)) ; Enable facility
1150 lwz r21,VMXlevel(r20) ; Get the facility level
1152 cmplw r27,r21 ; Are we returning from the active level?
1153 bne+ vmxchkena ; Nope...
1157 ; First clean up any live context we are returning from
1160 lwz r22,VMXcpu(r20) ; Get CPU this context was last dispatched on
1162 stw r19,VMXcpu(r20) ; Claim context for us
1164 eieio ; Make sure this gets out before owner clear
1166 lis r23,hi16(EXT(per_proc_info)) ; Set base per_proc
1167 slwi r22,r22,12 ; Find offset to the owner per_proc
1168 ori r23,r23,lo16(EXT(per_proc_info)) ; Set base per_proc
1169 li r24,VMXowner ; Displacement to VMX owner
1170 add r22,r23,r22 ; Point to the owner per_proc
1172 vmxinvothr: lwarx r23,r24,r22 ; Get the owner
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...
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.
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?
1192 beq++ vmxsetlvl ; No, see if we need to enable...
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...
1198 lwz r24,SAVprev+4(r22) ; Pick up the previous area
1199 li r21,0 ; Assume we popped all the way out
1200 mr. r24,r24 ; Any more context?
1201 beq-- vmxonlyone ; Nope...
1202 lwz r21,SAVlevel(r24) ; Get the level associated with save
1204 vmxonlyone: stw r24,VMXsave(r20) ; Dequeue this savearea
1206 rlwinm r3,r22,0,0,19 ; Find main savearea header
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)
1219 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1220 li r2,0x3401 ; (TEST/DEBUG)
1221 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1225 vmxsetlvl: stw r21,VMXlevel(r20) ; Save the level
1228 ; Here we check if we are at the right level
1231 vmxchkena: 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
1235 bne-- setena ; No, can not possibly enable...
1236 cmplw r30,r23 ; Are we about to launch the live level?
1237 cmplw cr1,r19,r24 ; Was facility used on this processor last?
1238 bne-- setena ; No, not live...
1239 bne-- cr1,setena ; No, wrong cpu, have to enable later....
1241 lwz r24,VMXsave(r26) ; Get the first savearea
1242 mr. r24,r24 ; Any savearea?
1243 beq++ vmxena ; Nope...
1244 lwz r25,SAVlevel(r24) ; Get the level of savearea
1245 lwz r0,SAVprev+4(r24) ; Get the previous
1246 cmplw r30,r25 ; Is savearea for the level we are launching?
1247 bne++ vmxena ; No, just go enable...
1249 stw r0,VMXsave(r26) ; Pop the chain
1251 rlwinm r3,r24,0,0,19 ; Find main savearea header
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)
1259 xor r3,r24,r3 ; Convert to physical
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)
1264 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1265 li r2,0x3402 ; (TEST/DEBUG)
1266 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1270 vmxena: oris r29,r29,hi16(MASK(MSR_VEC)) ; Enable facility
1272 setena: 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
1275 crmove cr7_eq,cr0_eq ; Remember if we are going to user state
1276 rlwimi. r20,r29,(((31-floatCngbit)+(MSR_FP_BIT+1))&31),floatCngbit,floatCngbit ; Set flag if we enabled floats
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
1283 beq setenaa ; Neither float nor vector turned on....
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
1292 setenaa: mfdec r24 ; Get decrementer
1293 bf+ cr2_eq,nodefer ; No deferred to switch to...
1295 li r20,0 ; Clear this
1296 stw r26,curctx(r28) ; Make the facility context current
1297 stw r20,deferctx(r28) ; Clear deferred context
1299 nodefer: 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
1302 ble- chkifuser ; We have popped or are just about to...
1304 segtb: 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?
1309 beq++ chkifuser ; No time set....
1310 bne-- cr1,segtb ; Timebase ticked, get them again...
1312 subfc r6,r21,r23 ; Subtract current from qact time
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
1318 bne chkifuser ; If high order is non-zero, this is too big for a decrementer
1319 cmplw r13,r24 ; Is this earlier than the decrementer? (logical compare takes care of high bit on)
1320 bge++ chkifuser ; No, do not reset decrementer...
1322 mtdec r13 ; Set our value
1324 chkifuser: beq-- cr7,chkenax ; Skip this if we are going to kernel...
1325 stw r18,cioSpace(r28) ; Half-invalidate to force MapUserAddressSpace to reload SRs
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)
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
1348 mr r3,r27 ; Pass savearea back
1349 b EXT(exception_exit) ; We are all done now...
1354 ; Null PPC call - performance testing, does absolutely nothing
1363 li r3,-1 ; Make sure we test no asts
1368 ; Instrumented null PPC call - performance testing, does absolutely nothing
1369 ; Forces various timestamps to be returned.
1374 .globl EXT(ppcNullinst)
1378 li r3,-1 ; Make sure we test no asts
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.
1388 * Registers when we get here:
1390 * r0 = syscall number
1393 * r14 = previous savearea (if any)
1400 fastpath: 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...
1406 * void cthread_set_self(cproc_t p)
1408 * set's thread state "user_value"
1410 * This op is invoked as follows:
1411 * li r0, CthreadSetSelfNumber // load the fast-trap number
1412 * sc // invoke fast-trap
1417 CthreadSetSelfNumber:
1419 lwz r5,saver3+4(r4) /* Retrieve the self number */
1420 stw r5,CTHREAD_SELF(r13) /* Remember it */
1421 stw r5,UAW(r25) /* Prime the per_proc_info with it */
1424 .globl EXT(fastexit)
1426 fastexutl: mr r3,r4 ; Pass back savearea
1427 b EXT(exception_exit) ; Go back to the caller...
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.
1440 lwz r0,saveexception(r4) ; Get the exception code
1441 lwz r23,savesrr1+4(r4) ; Get the interrupted MSR
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
1446 lwz r27,savesrr0+4(r4) ; Get trapped address
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...
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.
1465 exitFromVM: mr r30,r4 ; Get the savearea
1466 mr r3,r13 ; Get the activation
1468 b EXT(vmm_exit) ; Do it to it
1471 .globl EXT(retFromVM)
1474 mfsprg r10,0 ; Restore the per_proc info
1475 mr r8,r3 ; Get the activation
1476 lwz r4,SAVprev+4(r30) ; Pick up the previous savearea
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)
1483 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
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...
1492 ; chandler (note: not a candle maker or tallow merchant)
1494 ; Here is the system choke handler. This is where the system goes
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.
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.
1509 .globl EXT(chandler)
1510 LEXT(chandler) ; Choke handler
1513 mfsprg r25,0 ; Get the per_proc
1514 stw r31,traceMask(0) ; Force tracing off right now
1518 lwz r1,PP_DEBSTACKPTR(r25) ; Get debug stack pointer
1519 cmpwi r1,-1 ; Are we already choking?
1520 bne chokefirst ; Nope...
1522 chokespin: 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...
1530 chokefirst: li r0,-1 ; Set choke value
1531 mr. r1,r1 ; See if we are on debug stack yet
1532 lwz r10,saver1+4(r4) ;
1533 stw r0,PP_DEBSTACKPTR(r25) ; Show we are choking
1534 bne chokestart ; We are not on the debug stack yet...
1536 lwz r2,PP_DEBSTACK_TOP_SS(r25) ; Get debug stack top
1537 sub r11,r2,r10 ; Get stack depth
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...
1542 subi r1,r10,FM_REDZONE ; Make a red zone
1544 chokestart: li r0,0 ; Get a zero
1545 stw r0,FM_BACKPTR(r1) ; We now have terminated the back chain
1547 bl EXT(SysChoked) ; Call the "C" phase of this
1548 b chokespin ; Should not be here so just go spin...
1553 ; Savearea chain verification
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)
1568 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1569 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1574 ; Make sure that all savearea chains have the right type on them
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)
1584 mr. r26,r26 ; (TEST/DEBUG) Have we locked the test out?
1585 lwz r28,psthreads(r28) ; (TEST/DEBUG)
1586 mflr r31 ; (TEST/DEBUG) Save return
1587 bnelr- ; (TEST/DEBUG) Test already triggered, skip...
1588 b fckgo ; (TEST/DEBUG) Join up...
1590 fcknext: mr. r27,r27 ; (TEST/DEBUG) Any more threads?
1591 bne+ fckxxx ; (TEST/DEBUG) Yes...
1593 mtlr r31 ; (TEST/DEBUG) Restore return
1594 blr ; (TEST/DEBUG) Leave...
1596 fckxxx: lwz r28,THREAD_PSTHRN(r28) ; (TEST/DEBUG) Get next thread
1598 fckgo: 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
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
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
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...
1616 li r22,kVmmMaxContextsPerThread ; (TEST/DEBUG) Get the number of control blocks
1617 subi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get running start
1619 fcknvmm: 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...
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...
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
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...
1638 mr. r20,r20 ; (TEST/DEBUG) Chain done?
1639 beqlr- ; (TEST/DEBUG) Yes...
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...
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
1651 versvok: lwz r20,SAVprev+4(r20) ; (TEST/DEBUG) Get the previous one
1652 b versavetype ; (TEST/DEBUG) Go check its type...