2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 /* Low level routines dealing with exception entry and exit.
27 * There are various types of exception:
29 * Interrupt, trap, system call and debugger entry. Each has it's own
30 * handler since the state save routine is different for each. The
31 * code is very similar (a lot of cut and paste).
33 * The code for the FPU disabled handler (lazy fpu) is in cswtch.s
37 #include <mach_assert.h>
38 #include <mach/exception_types.h>
39 #include <mach/kern_return.h>
40 #include <mach/ppc/vm_param.h>
45 #include <ppc/proc_reg.h>
47 #include <ppc/exception.h>
48 #include <ppc/savearea.h>
58 * ENTRY: VM switched ON
60 * R3 contains exception code
61 * R4 points to the saved context (virtual address)
62 * Everything is saved in savearea
66 * If pcb.ksp == 0 then the kernel stack is already busy,
67 * we make a stack frame
68 * leaving enough space for the 'red zone' in case the
69 * trapped thread was in the middle of saving state below
72 * otherwise we make a stack frame and
73 * the kernel stack (setting pcb.ksp to 0)
75 * on return, we do the reverse, the last state is popped from the pcb
76 * and pcb.ksp is set to the top of stack
79 /* TRAP_SPACE_NEEDED is the space assumed free on the kernel stack when
80 * another trap is taken. We need at least enough space for a saved state
81 * structure plus two small backpointer frames, and we add a few
82 * hundred bytes for the space needed by the C (which may be less but
83 * may be much more). We're trying to catch kernel stack overflows :-)
86 #define TRAP_SPACE_NEEDED FM_REDZONE+(2*FM_SIZE)+256
92 LEXT(thandler) ; Trap handler
94 mfsprg r13,1 ; Get the current activation
95 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
97 lwz r1,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
99 cmpwi cr0,r1,0 ; Are we on interrupt stack?
101 beq- cr0,EXT(ihandler) ; If on interrupt stack, treat this as interrupt...
102 lwz r26,ACT_MACT_SPF(r13) ; Get special flags
103 lwz r8,ACT_MACT_PCB(r13) ; Get the last savearea used
104 rlwinm. r26,r26,0,bbThreadbit,bbThreadbit ; Do we have Blue Box Assist active?
105 lwz r1,ACT_MACT_KSP(r13) ; Get the top of kernel stack
106 bnel- checkassist ; See if we should assist this
107 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
108 stw r8,SAVprev+4(r4) ; Queue the new save area in the front
111 bl versave ; (TEST/DEBUG)
114 lwz r9,THREAD_KERNEL_STACK(r6) ; Get our kernel stack start
115 cmpwi cr1,r1,0 ; Are we already on kernel stack?
116 stw r13,SAVact(r4) ; Mark the savearea as belonging to this activation
117 lwz r26,saver1+4(r4) ; Get the stack at interrupt time
119 bne+ cr1,.L_kstackfree ; We are not on kernel stack yet...
121 subi r1,r26,FM_REDZONE ; Make a red zone on interrupt time kernel stack
124 lwz r31,savesrr1+4(r4) ; Pick up the entry MSR
125 sub r9,r1,r9 ; Get displacment into the kernel stack
126 li r0,0 ; Make this 0
127 rlwinm. r0,r9,0,28,31 ; Verify that we have a 16-byte aligned stack (and get a 0)
128 cmplwi cr2,r9,KERNEL_STACK_SIZE ; Do we still have room on the stack?
129 beq cr1,.L_state_on_kstack ; using above test for pcb/stack
131 stw r0,ACT_MACT_KSP(r13) ; Show that we have taken the stack
134 lwz r9,savevrsave(r4) ; Get the VRSAVE register
135 bne-- kernelStackUnaligned ; Stack is unaligned...
136 rlwinm. r6,r31,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
137 subi r1,r1,FM_SIZE ; Push a header onto the current stack
138 bgt-- cr2,kernelStackBad ; Kernel stack is bogus...
140 kernelStackNotBad: ; Vector was off
141 beq++ tvecoff ; Vector off, do not save vrsave...
142 stw r9,liveVRS(r25) ; Set the live value
144 tvecoff: stw r26,FM_BACKPTR(r1) ; Link back to the previous frame
147 /* If debugging, we need two frames, the first being a dummy
148 * which links back to the trapped routine. The second is
149 * that which the C routine below will need
151 lwz r3,savesrr0+4(r4) ; Get the point of interruption
152 stw r3,FM_LR_SAVE(r1) ; save old instr ptr as LR value
153 stwu r1, -FM_SIZE(r1) ; and make new frame
158 addi r4,r13,SYSTEM_TIMER
161 /* call trap handler proper, with
163 * ARG1 = saved_state ptr
169 lwz r3,saveexception(r30) ; Get the exception code
170 lwz r0,ACT_MACT_SPF(r13) ; Get the special flags
172 addi r5,r3,-T_DATA_ACCESS ; Adjust to start of range
173 rlwinm. r0,r0,0,runningVMbit,runningVMbit ; Are we in VM state? (cr0_eq == 0 if yes)
174 cmplwi cr2,r5,T_TRACE-T_DATA_ACCESS ; Are we still in range? (cr_gt if not)
176 lwz r5,savedsisr(r4) ; Get the saved DSISR
178 crnor cr7_eq,cr0_eq,cr2_gt ; We should intercept if in VM and is a true trap (cr7_eq == 1 if yes)
179 rlwinm. r0,r31,0,MSR_PR_BIT,MSR_PR_BIT ; Are we trapping from supervisor state? (cr0_eq == 1 if yes)
181 cmpi cr2,r3,T_PREEMPT ; Is this a preemption?
184 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
187 crandc cr0_eq,cr7_eq,cr0_eq ; Do not intercept if we are in the kernel (cr0_eq == 1 if yes)
189 lwz r6,savedar(r4) ; Get the DAR (top)
190 lwz r7,savedar+4(r4) ; Get the DAR (bottom)
192 beq- cr2,.L_call_trap ; Do not turn on interrupts for T_PREEMPT
193 beq- exitFromVM ; Any true trap but T_MACHINE_CHECK exits us from the VM...
195 /* syscall exception might warp here if there's nothing left
196 * to do except generate a trap
203 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
204 mfmsr r7 ; Get the MSR
205 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
206 andc r7,r7,r10 ; Turn off VEC, FP, and EE
207 mtmsr r7 ; Disable for interrupts
208 mfsprg r8,1 ; Get the current activation
209 lwz r10,ACT_PER_PROC(r8) ; Get the per_proc block
211 * This is also the point where new threads come when they are created.
212 * The new thread is setup to look like a thread that took an
213 * interrupt and went immediatly into trap.
217 lwz r11,SAVflags(r3) ; Get the flags of the current savearea
218 lwz r0,savesrr1+4(r3) ; Get the MSR we are going to
219 lwz r4,SAVprev+4(r3) ; Pick up the previous savearea
220 mfsprg r8,1 ; Get the current thread
221 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
222 rlwinm. r0,r0,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going to the user?
224 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
226 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
227 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
228 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
230 beq-- chkfac ; We are not leaving the kernel yet...
232 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
233 b chkfac ; Go end it all...
237 ; Here is where we go when we detect that the kernel stack is all messed up.
238 ; We just try to dump some info and get into the debugger.
243 lwz r3,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
244 subi r3,r3,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
245 sub r3,r1,r3 ; Get displacement into debug stack
246 cmplwi cr2,r3,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
247 blt+ cr2,kernelStackNotBad ; Yeah, that is ok too...
249 lis r0,hi16(Choke) ; Choke code
250 ori r0,r0,lo16(Choke) ; and the rest
251 li r3,failStack ; Bad stack code
254 kernelStackUnaligned:
255 lis r0,hi16(Choke) ; Choke code
256 ori r0,r0,lo16(Choke) ; and the rest
257 li r3,failUnalignedStk ; Unaligned stack code
264 * ENTRY: VM switched ON
266 * R3 contains exception code
267 * R4 points to the saved context (virtual address)
268 * Everything is saved in savearea
272 * If pcb.ksp == 0 then the kernel stack is already busy,
273 * this is an error - jump to the debugger entry
275 * otherwise depending upon the type of
276 * syscall, look it up in the kernel table
277 * or pass it to the server.
279 * on return, we do the reverse, the state is popped from the pcb
280 * and pcb.ksp is set to the top of stack.
285 * mach system calls are negative
286 * BSD system calls are low positive
287 * PPC-only system calls are in the range 0x6xxx
288 * PPC-only "fast" traps are in the range 0x7xxx
293 LEXT(shandler) ; System call handler
295 lwz r7,savesrr1+4(r4) ; Get the SRR1 value
296 mfsprg r13,1 ; Get the current activation
297 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
298 lwz r0,saver0+4(r4) ; Get the original syscall number
299 lwz r17,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
300 rlwinm r15,r0,0,0,19 ; Clear the bottom of call number for fast check
301 mr. r17,r17 ; Are we on interrupt stack?
302 lwz r9,savevrsave(r4) ; Get the VRsave register
303 beq-- EXT(ihandler) ; On interrupt stack, not allowed...
304 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
307 beq++ svecoff ; Vector off, do not save vrsave...
308 stw r9,liveVRS(r25) ; Set the live value
310 ; Check if SCs are being redirected for the BlueBox or to VMM
313 svecoff: lwz r6,ACT_MACT_SPF(r13) ; Pick up activation special flags
314 mtcrf 0x40,r6 ; Check special flags
315 mtcrf 0x01,r6 ; Check special flags
316 crmove cr6_eq,runningVMbit ; Remember if we are in VMM
317 bne++ cr6,sVMchecked ; Not running VM
318 lwz r18,spcFlags(r25) ; Load per_proc special flags
319 rlwinm. r18,r18,0,FamVMmodebit,FamVMmodebit ; Is FamVMmodebit set?
320 beq sVMchecked ; Not in FAM
321 cmpwi r0,0x6004 ; Is it vmm_dispatch syscall:
323 lwz r26,saver3+4(r4) ; Get the original syscall number
324 cmpwi cr6,r26,kvmmExitToHost ; vmm_exit_to_host request
326 bf++ bbNoMachSCbit,noassist ; Take branch if SCs are not redirected
327 lwz r26,ACT_MACT_BEDA(r13) ; Pick up the pointer to the blue box exception area
328 b EXT(atomic_switch_syscall) ; Go to the assist...
330 noassist: cmplwi r15,0x7000 ; Do we have a fast path trap?
331 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
332 beql fastpath ; We think it is a fastpath...
334 lwz r1,ACT_MACT_KSP(r13) ; Get the kernel stack pointer
336 mr. r1,r1 ; Are we already on the kernel stack?
337 li r3,T_SYSTEM_CALL ; Yup, pretend we had an interrupt...
338 beq- EXT(ihandler) ; Bad boy, bad boy... What cha gonna do when they come for you?
341 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
342 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
343 li r0,0 ; Clear this out
344 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
345 stw r13,SAVact(r4) ; Point the savearea at its activation
348 bl versave ; (TEST/DEBUG)
351 lwz r15,saver1+4(r4) ; Grab interrupt time stack
352 mr r30,r4 ; Save pointer to the new context savearea
353 stw r0,ACT_MACT_KSP(r13) ; Mark stack as busy with 0 val
354 stw r15,FM_BACKPTR(r1) ; Link stack frame backwards
356 lwz r3,SAVtime+4(r30)
357 addi r4,r13,SYSTEM_TIMER
361 /* If debugging, we need two frames, the first being a dummy
362 * which links back to the trapped routine. The second is
363 * that which the C routine below will need
365 lwz r8,savesrr0+4(r30) ; Get the point of interruption
366 stw r8,FM_LR_SAVE(r1) ; Save old instr ptr as LR value
367 stwu r1, -FM_SIZE(r1) ; and make new frame
372 lwz r15,SAVflags(r30) ; Get the savearea flags
373 lwz r0,saver0+4(r30) ; Get R0 back
374 mfmsr r11 ; Get the MSR
375 stwu r1,-(FM_SIZE+ARG_SIZE+MUNGE_ARGS_SIZE)(r1) ; Make a stack frame
376 ori r11,r11,lo16(MASK(MSR_EE)) ; Turn on interruption enabled bit
377 rlwinm r10,r0,0,0,19 ; Keep only the top part
378 oris r15,r15,SAVsyscall >> 16 ; Mark that it this is a syscall
379 cmplwi r10,0x6000 ; Is it the special ppc-only guy?
380 stw r15,SAVflags(r30) ; Save syscall marker
381 beq-- cr6,exitFromVM ; It is time to exit from alternate context...
383 beq-- ppcscall ; Call the ppc-only system call handler...
385 mr. r0,r0 ; What kind is it?
386 mtmsr r11 ; Enable interruptions
388 blt-- .L_kernel_syscall ; System call number if negative, this is a mach call...
390 lwz r8,ACT_TASK(r13) ; Get our task
391 cmpwi cr0,r0,0x7FFA ; Special blue box call?
392 beq-- .L_notify_interrupt_syscall ; Yeah, call it...
394 lwz r7,TASK_SYSCALLS_UNIX(r8) ; Get the current count
395 mr r3,r30 ; Get PCB/savearea
396 mr r4,r13 ; current activation
397 addi r7,r7,1 ; Bump it
398 stw r7,TASK_SYSCALLS_UNIX(r8) ; Save it
399 bl EXT(unix_syscall) ; Check out unix...
401 .L_call_server_syscall_exception:
402 li r3,EXC_SYSCALL ; doexception(EXC_SYSCALL, num, 1)
404 .L_call_server_exception:
405 mr r4,r0 ; Set syscall selector
407 b EXT(doexception) ; Go away, never to return...
409 .L_notify_interrupt_syscall:
410 lwz r3,saver3+4(r30) ; Get the new PC address to pass in
411 bl EXT(syscall_notify_interrupt)
413 * Ok, return from C function, R3 = return value
415 * saved state is still in R30 and the active thread is in R16 .
417 mr r31,r16 ; Move the current thread pointer
418 stw r3,saver3+4(r30) ; Stash the return code
419 b .L_thread_syscall_ret_check_ast
422 ; Handle PPC-only system call interface
423 ; These are called with interruptions disabled
424 ; and the savearea/pcb as the first parameter.
425 ; It is up to the callee to enable interruptions if
426 ; they should be. We are in a state here where
427 ; both interrupts and preemption is ok, but because we could
428 ; be calling diagnostic code we will not enable.
430 ; Also, the callee is responsible for finding any parameters
431 ; in the savearea/pcb. It also must set saver3 with any return
432 ; code before returning.
434 ; There are 3 possible return codes:
435 ; 0 the call is disabled or something, we treat this like it was bogus
436 ; + the call finished ok, check for AST
437 ; - the call finished ok, do not check for AST
439 ; Note: the last option is intended for special diagnostics calls that
440 ; want the thread to return and execute before checking for preemption.
442 ; NOTE: Both R16 (thread) and R30 (savearea) need to be preserved over this call!!!!
447 ppcscall: rlwinm r11,r0,2,18,29 ; Make an index into the table
448 lis r10,hi16(EXT(PPCcalls)) ; Get PPC-only system call table
449 cmplwi r11,PPCcallmax ; See if we are too big
450 ori r10,r10,lo16(EXT(PPCcalls)) ; Merge in low half
451 bgt- .L_call_server_syscall_exception ; Bogus call...
452 lwzx r11,r10,r11 ; Get function address
455 ; Note: make sure we do not change the savearea in R30 to
456 ; a different register without checking. Some of the PPCcalls
457 ; depend upon it being there.
460 mr r3,r30 ; Pass the savearea
461 mr r4,r13 ; Pass the activation
462 mr. r11,r11 ; See if there is a function here
463 mtctr r11 ; Set the function address
464 beq- .L_call_server_syscall_exception ; Disabled call...
466 mfspr r4,pmc1 ; Get stamp
467 stw r4,0x6100+(9*16)+0x0(0) ; Save it
468 mfspr r4,pmc2 ; Get stamp
469 stw r4,0x6100+(9*16)+0x4(0) ; Save it
470 mfspr r4,pmc3 ; Get stamp
471 stw r4,0x6100+(9*16)+0x8(0) ; Save it
472 mfspr r4,pmc4 ; Get stamp
473 stw r4,0x6100+(9*16)+0xC(0) ; Save it
480 mr. r3,r3 ; See what we should do
481 mr r31,r16 ; Restore the current thread pointer
482 bgt+ .L_thread_syscall_ret_check_ast ; Take normal AST checking return....
483 mfsprg r10,1 ; Get the current activation
484 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
485 blt+ .L_thread_syscall_return ; Return, but no ASTs....
486 lwz r0,saver0+4(r30) ; Restore the system call number
487 b .L_call_server_syscall_exception ; Go to common exit...
492 * we get here for mach system calls
493 * when kdebug tracing is enabled
497 mr r4,r30 ; Pass in saved state
498 bl EXT(syscall_trace)
500 cmplw r31,r29 ; Is this syscall in the table?
501 add r31,r27,r28 ; Point right to the syscall table entry
503 bge- .L_call_server_syscall_exception ; The syscall number is invalid
505 lwz r0,savesrr1(r30) ; Get the saved srr1
506 rlwinm. r0,r0,0,MSR_SF_BIT,MSR_SF_BIT ; Test for 64 bit caller
507 lwz r0,MACH_TRAP_ARG_MUNGE32(r31) ; Pick up the 32 bit munge function address
508 beq-- .L_ksystrace_munge
509 lwz r0,MACH_TRAP_ARG_MUNGE64(r31) ; Pick up the 64 bit munge function address
512 cmplwi r0,0 ; do we have a munger to call?
513 mtctr r0 ; Set the function call address
514 addi r3,r30,saver3 ; Pointer to args from save area
515 addi r4,r1,FM_ARG0+ARG_SIZE ; Pointer for munged args
516 beq-- .L_ksystrace_trapcall ; just make the trap call
517 bctrl ; Call the munge function
519 .L_ksystrace_trapcall:
520 lwz r0,MACH_TRAP_FUNCTION(r31) ; Pick up the function address
521 mtctr r0 ; Set the function call address
522 addi r3,r1,FM_ARG0+ARG_SIZE ; Pointer to munged args
525 mr r4,r30 ; Pass in the savearea
526 bl EXT(syscall_trace_end) ; Trace the exit of the system call
531 /* Once here, we know that the syscall was -ve
532 * we should still have r1=ksp,
533 * r16 = pointer to current thread,
534 * r13 = pointer to top activation,
535 * r0 = syscall number
536 * r30 = pointer to saved state (in pcb)
543 ; Call a function that can print out our syscall info
544 ; Note that we don t care about any volatiles yet
546 lwz r10,ACT_TASK(r13) ; Get our task
548 lis r8,hi16(EXT(kdebug_enable)) ; Get top of kdebug_enable
549 lis r28,hi16(EXT(mach_trap_table)) ; Get address of table
550 ori r8,r8,lo16(EXT(kdebug_enable)) ; Get bottom of kdebug_enable
551 lwz r8,0(r8) ; Get kdebug_enable
553 lwz r7,TASK_SYSCALLS_MACH(r10) ; Get the current count
554 neg r31,r0 ; Make this positive
556 slwi r27,r3,4 ; multiply by 16
557 slwi r3,r3,2 ; and the original by 4
558 ori r28,r28,lo16(EXT(mach_trap_table)) ; Get address of table
559 add r27,r27,r3 ; for a total of 20x (5 words/entry)
560 addi r7,r7,1 ; Bump TASK_SYSCALLS_MACH count
561 cmplwi r8,0 ; Is kdebug_enable non-zero
562 stw r7,TASK_SYSCALLS_MACH(r10) ; Save count
563 bne-- ksystrace ; yes, tracing enabled
565 cmplwi r31,MACH_TRAP_TABLE_COUNT ; Is this syscall in the table?
566 add r31,r27,r28 ; Point right to the syscall table entry
568 bge-- .L_call_server_syscall_exception ; The syscall number is invalid
570 lwz r0,savesrr1(r30) ; Get the saved srr1
571 rlwinm. r0,r0,0,MSR_SF_BIT,MSR_SF_BIT ; Test for 64 bit caller
572 lwz r0,MACH_TRAP_ARG_MUNGE32(r31) ; Pick up the 32 bit munge function address
573 beq-- .L_kernel_syscall_munge
574 lwz r0,MACH_TRAP_ARG_MUNGE64(r31) ; Pick up the 64 bit munge function address
576 .L_kernel_syscall_munge:
577 cmplwi r0,0 ; test for null munger
578 mtctr r0 ; Set the function call address
579 addi r3,r30,saver3 ; Pointer to args from save area
580 addi r4,r1,FM_ARG0+ARG_SIZE ; Pointer for munged args
581 beq-- .L_kernel_syscall_trapcall ; null munger - skip to trap call
582 bctrl ; Call the munge function
584 .L_kernel_syscall_trapcall:
585 lwz r0,MACH_TRAP_FUNCTION(r31) ; Pick up the function address
586 mtctr r0 ; Set the function call address
587 addi r3,r1,FM_ARG0+ARG_SIZE ; Pointer to munged args
592 * Ok, return from C function, R3 = return value
594 * get the active thread's PCB pointer and thus pointer to user state
595 * saved state is still in R30 and the active thread is in R16
599 srawi r0,r3,31 ; properly extend the return code
600 cmpi cr0,r3,KERN_INVALID_ARGUMENT ; deal with invalid system calls
601 mr r31,r16 ; Move the current thread pointer
602 stw r0, saver3(r30) ; stash the high part of the return code
603 stw r3,saver3+4(r30) ; Stash the low part of the return code
604 beq- cr0,.L_mach_invalid_ret ; otherwise fall through into the normal return path
608 /* 'standard' syscall returns here - INTERRUPTS ARE STILL ON
609 * the syscall may perform a thread_set_syscall_return
610 * followed by a thread_exception_return, ending up
611 * at thread_syscall_return below, with SS_R3 having
612 * been set up already
614 * When we are here, r31 should point to the current thread,
615 * r30 should point to the current pcb
616 * r3 contains value that we're going to return to the user
617 * which has already been stored back into the save area
620 .L_thread_syscall_ret_check_ast:
621 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
622 mfmsr r12 ; Get the current MSR
623 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
624 andc r12,r12,r10 ; Turn off VEC, FP, and EE
625 mtmsr r12 ; Turn interruptions off
627 mfsprg r10,1 ; Get the current activation
628 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
630 /* Check to see if there's an outstanding AST */
632 lwz r4,PP_PENDING_AST(r10)
633 cmpi cr0,r4, 0 ; Any pending asts?
634 beq++ cr0,.L_syscall_no_ast ; Nope...
636 /* Yes there is, call ast_taken
637 * pretending that the user thread took an AST exception here,
638 * ast_taken will save all state and bring us back here
642 /* debug assert - make sure that we're not returning to kernel */
643 lwz r3,savesrr1+4(r30)
644 andi. r3,r3,MASK(MSR_PR)
645 bne++ scrnotkern ; returning to user level, check
647 lis r0,hi16(Choke) ; Choke code
648 ori r0,r0,lo16(Choke) ; and the rest
649 li r3,failContext ; Bad state code
655 lis r3,hi16(AST_ALL) ; Set ast flags
656 li r4,1 ; Set interrupt allowed
657 ori r3,r3,lo16(AST_ALL)
658 bl EXT(ast_taken) ; Process the pending ast
659 b .L_thread_syscall_ret_check_ast ; Go see if there was another...
663 * need to figure out why we got an KERN_INVALID_ARG
664 * if it was due to a non-existent system call
665 * then we want to throw an exception... otherwise
666 * we want to pass the error code back to the caller
668 lwz r0,saver0+4(r30) ; reload the original syscall number
669 neg r28,r0 ; Make this positive
670 mr r4,r28 ; save a copy
671 slwi r27,r4,4 ; multiply by 16
672 slwi r4,r4,2 ; and another 4
673 lis r28,hi16(EXT(mach_trap_table)) ; Get address of table
674 add r27,r27,r4 ; for a total of 20x (5 words/entry)
675 ori r28,r28,lo16(EXT(mach_trap_table)) ; Get address of table
676 add r28,r27,r28 ; Point right to the syscall table entry
677 lwz r27,MACH_TRAP_FUNCTION(r28) ; Pick up the function address
678 lis r28,hi16(EXT(kern_invalid)) ; Get high half of invalid syscall function
679 ori r28,r28,lo16(EXT(kern_invalid)) ; Get low half of invalid syscall function
680 cmpw cr0,r27,r28 ; Check if this is an invalid system call
681 beq-- .L_call_server_syscall_exception ; We have a bad system call
682 b .L_mach_invalid_arg ; a system call returned KERN_INVALID_ARG
685 /* thread_exception_return returns to here, almost all
686 * registers intact. It expects a full context restore
687 * of what it hasn't restored itself (ie. what we use).
689 * In particular for us,
690 * we still have r31 points to the current thread,
691 * r30 points to the current pcb
697 .L_thread_syscall_return:
699 mr r3,r30 ; Get savearea to the correct register for common exit
701 lwz r11,SAVflags(r30) ; Get the flags
702 lwz r5,THREAD_KERNEL_STACK(r31) ; Get the base pointer to the stack
703 lwz r4,SAVprev+4(r30) ; Get the previous save area
704 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
705 mfsprg r8,1 ; Now find the current activation
706 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
707 stw r11,SAVflags(r30) ; Stick back the flags
708 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
709 stw r4,ACT_MACT_PCB(r8) ; Save previous save area
710 b chkfac ; Go end it all...
713 * thread_exception_return()
715 * Return to user mode directly from within a system call.
719 .globl EXT(thread_bootstrap_return)
720 LEXT(thread_bootstrap_return) ; NOTE: THIS IS GOING AWAY IN A FEW DAYS....
722 .globl EXT(thread_exception_return)
723 LEXT(thread_exception_return) ; Directly return to user mode
725 .L_thread_exc_ret_check_ast:
726 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
727 mfmsr r3 ; Get the MSR
728 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
729 andc r3,r3,r10 ; Turn off VEC, FP, and EE
730 mtmsr r3 ; Disable interrupts
732 /* Check to see if there's an outstanding AST */
733 /* We don't bother establishing a call frame even though CHECK_AST
734 can invoke ast_taken(), because it can just borrow our caller's
735 frame, given that we're not going to return.
738 mfsprg r10,1 ; Get the current activation
739 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
740 lwz r4,PP_PENDING_AST(r10)
742 beq+ cr0,.L_exc_ret_no_ast
744 /* Yes there is, call ast_taken
745 * pretending that the user thread took an AST exception here,
746 * ast_taken will save all state and bring us back here
751 ori r3,r3,lo16(AST_ALL)
754 b .L_thread_exc_ret_check_ast ; check for a second AST (rare)
756 /* arriving here, interrupts should be disabled */
757 /* Get the active thread's PCB pointer to restore regs
761 mfsprg r30,1 ; Get the currrent activation
764 lwz r30,ACT_MACT_PCB(r30)
765 mr. r30,r30 ; Is there any context yet?
766 beq- makeDummyCtx ; No, hack one up...
769 * debug assert - make sure that we're not returning to kernel
770 * get the active thread's PCB pointer and thus pointer to user state
773 lwz r3,savesrr1+4(r30)
774 andi. r3,r3,MASK(MSR_PR)
775 bne+ ret_user2 ; We are ok...
777 lis r0,hi16(Choke) ; Choke code
778 ori r0,r0,lo16(Choke) ; and the rest
779 li r3,failContext ; Bad state code
785 /* If the system call flag isn't set, then we came from a trap,
786 * so warp into the return_from_trap (thread_return) routine,
787 * which takes PCB pointer in R3, not in r30!
789 lwz r0,SAVflags(r30) ; Grab the savearea flags
790 andis. r0,r0,SAVsyscall>>16 ; Are we returning from a syscall?
791 mr r3,r30 ; Copy pcb pointer into r3 in case we need it
792 beq-- cr0,thread_return ; Nope, must be a thread return...
793 b .L_thread_syscall_return ; Join up with the system call return...
796 ; This is where we handle someone trying who did a thread_create followed
797 ; by a thread_resume with no intervening thread_set_state. Just make an
798 ; empty context, initialize it to trash and let em execute at 0...
804 bl EXT(save_get) ; Get a save_area
805 li r4,SAVgeneral ; Get the general context type
807 stb r4,SAVflags+2(r3) ; Set type
808 addi r2,r3,savefpscr+4 ; Point past what we are clearing
809 mr r4,r3 ; Save the start
811 cleardummy: stw r0,0(r4) ; Clear stuff
812 addi r4,r4,4 ; Next word
813 cmplw r4,r2 ; Still some more?
814 blt+ cleardummy ; Yeah...
816 lis r2,hi16(MSR_EXPORT_MASK_SET) ; Set the high part of the user MSR
817 ori r2,r2,lo16(MSR_EXPORT_MASK_SET) ; And the low part
818 stw r2,savesrr1+4(r3) ; Set the default user MSR
820 b thread_return ; Go let em try to execute, hah!
825 * ENTRY: VM switched ON
827 * R3 contains exception code
828 * R4 points to the saved context (virtual address)
829 * Everything is saved in savearea
835 LEXT(ihandler) ; Interrupt handler */
838 * get the value of istackptr, if it's zero then we're already on the
842 lwz r10,savesrr1+4(r4) ; Get SRR1
843 lwz r7,savevrsave(r4) ; Get the VRSAVE register
844 mfsprg r13,1 ; Get the current activation
845 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
846 li r14,0 ; Zero this for now
847 rlwinm. r16,r10,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
848 lwz r1,PP_ISTACKPTR(r25) ; Get the interrupt stack
849 li r16,0 ; Zero this for now
851 beq+ ivecoff ; Vector off, do not save vrsave...
852 stw r7,liveVRS(r25) ; Set the live value
854 ivecoff: li r0,0 ; Get a constant 0
855 rlwinm r5,r10,0,MSR_PR_BIT,MSR_PR_BIT ; Are we trapping from supervisor state?
856 mr. r1,r1 ; Is it active?
857 cmplwi cr2,r5,0 ; cr2_eq == 1 if yes
859 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
860 lwz r9,saver1+4(r4) ; Pick up the rupt time stack
861 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
862 stw r13,SAVact(r4) ; Point the savearea at its activation
863 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
865 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
867 ifromk: bne .L_istackfree ; Nope...
869 /* We're already on the interrupt stack, get back the old
870 * stack pointer and make room for a frame
873 lwz r10,PP_INTSTACK_TOP_SS(r25) ; Get the top of the interrupt stack
874 addi r5,r9,INTSTACK_SIZE-FM_SIZE ; Shift stack for bounds check
875 subi r1,r9,FM_REDZONE ; Back up beyond the red zone
876 sub r5,r5,r10 ; Get displacement into stack
877 cmplwi r5,INTSTACK_SIZE-FM_SIZE ; Is the stack actually invalid?
878 blt+ ihsetback ; The stack is ok...
880 lwz r5,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
881 subi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
882 sub r5,r1,r5 ; Get displacement into debug stack
883 cmplwi cr2,r5,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
884 blt+ cr2,ihsetback ; Yeah, that is ok too...
886 lis r0,hi16(Choke) ; Choke code
887 ori r0,r0,lo16(Choke) ; and the rest
888 li r3,failStack ; Bad stack code
892 lis r0,hi16(Choke) ; Choke code
893 ori r0,r0,lo16(Choke) ; and the rest
894 li r3,failUnalignedStk ; Unaligned stack code
900 rlwinm. r0,r1,0,28,31 ; Check if stack is aligned (and get 0)
901 lwz r10,SAVflags(r4) ; Get savearea flags
902 bne-- intUnalignedStk ; Stack is unaligned...
903 stw r0,PP_ISTACKPTR(r25) ; Mark the stack in use
904 oris r10,r10,hi16(SAVrststk) ; Indicate we reset stack when we return from this one
905 stw r10,SAVflags(r4) ; Stick it back
908 * To summarize, when we reach here, the state has been saved and
909 * the stack is marked as busy. We now generate a small
910 * stack frame with backpointers to follow the calling
911 * conventions. We set up the backpointers to the trapped
912 * routine allowing us to backtrace.
915 ihsetback: subi r1,r1,FM_SIZE ; Make a new frame
916 stw r9,FM_BACKPTR(r1) ; Point back to previous stackptr
919 beq- cr1,ihbootnover ; (TEST/DEBUG)
920 bl versave ; (TEST/DEBUG)
921 ihbootnover: ; (TEST/DEBUG)
925 /* If debugging, we need two frames, the first being a dummy
926 * which links back to the trapped routine. The second is
927 * that which the C routine below will need
929 lwz r5,savesrr0+4(r4) ; Get interrupt address
930 stw r5,FM_LR_SAVE(r1) ; save old instr ptr as LR value
931 stwu r1,-FM_SIZE(r1) ; Make another new frame for C routine
938 addi r4,r13,SYSTEM_TIMER
943 lwz r5,savedsisr(r30) ; Get the DSISR
944 lwz r6,savedar+4(r30) ; Get the DAR
949 /* interrupt() returns a pointer to the saved state in r3
951 * Ok, back from C. Disable interrupts while we restore things
953 .globl EXT(ihandler_ret)
955 LEXT(ihandler_ret) ; Marks our return point from debugger entry
957 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
958 mfmsr r0 ; Get our MSR
959 ori r10,r10,lo16(MASK(MSR_FP)|MASK(MSR_EE)) ; Add in FP and EE
960 andc r0,r0,r10 ; Turn off VEC, FP, and EE
961 mtmsr r0 ; Make sure interrupts are disabled
962 mfsprg r8,1 ; Get the current activation
963 lwz r10,ACT_PER_PROC(r8) ; Get the per_proc block
965 lwz r7,SAVflags(r3) ; Pick up the flags
966 lwz r9,SAVprev+4(r3) ; Get previous save area
967 cmplwi cr1,r8,0 ; Are we still initializing?
968 lwz r12,savesrr1+4(r3) ; Get the MSR we will load on return
969 andis. r11,r7,hi16(SAVrststk) ; Is this the first on the stack?
970 stw r9,ACT_MACT_PCB(r8) ; Point to previous context savearea
971 mr r4,r3 ; Move the savearea pointer
972 beq .L_no_int_ast2 ; Get going if not the top-o-stack...
975 /* We're the last frame on the stack. Restore istackptr to empty state.
977 * Check for ASTs if one of the below is true:
978 * returning to user mode
979 * returning to a kloaded server
981 lwz r9,PP_INTSTACK_TOP_SS(r10) ; Get the empty stack value
982 andc r7,r7,r11 ; Remove the stack reset bit in case we pass this one
983 stw r9,PP_ISTACKPTR(r10) ; Save that saved state ptr
984 lwz r3,ACT_PREEMPT_CNT(r8) ; Get preemption level
985 stw r7,SAVflags(r4) ; Save the flags
986 cmplwi r3, 0 ; Check for preemption
987 bne .L_no_int_ast ; Do not preempt if level is not zero
988 andi. r6,r12,MASK(MSR_PR) ; privilege mode
989 lwz r11,PP_PENDING_AST(r10) ; Get the pending AST mask
990 beq- .L_kernel_int_ast ; In kernel space, AST_URGENT check
991 li r3,T_AST ; Assume the worst
992 mr. r11,r11 ; Are there any pending?
993 beq .L_no_int_ast ; Nope...
997 andi. r11,r11,AST_URGENT ; Do we have AST_URGENT?
998 li r3,T_PREEMPT ; Assume the worst
999 beq .L_no_int_ast ; Nope...
1002 * There is a pending AST. Massage things to make it look like
1003 * we took a trap and jump into the trap handler. To do this
1004 * we essentially pretend to return from the interrupt but
1005 * at the last minute jump into the trap handler with an AST
1006 * trap instead of performing an rfi.
1010 stw r3,saveexception(r4) ; Set the exception code to T_AST/T_PREEMPT
1011 b EXT(thandler) ; We need to preempt so treat like a trap...
1014 mr r3,r4 ; Get into the right register for common code
1017 rlwinm r7,r7,0,15,13 ; Clear the syscall flag
1018 li r4,0 ; Assume for a moment that we are in init
1019 stw r7,SAVflags(r3) ; Set the flags with cleared syscall flag
1020 beq-- cr1,chkfac ; Jump away if we are in init...
1022 lwz r4,ACT_MACT_PCB(r8) ; Get the new level marker
1026 ; This section is common to all exception exits. It throws away vector
1027 ; and floating point saveareas as the exception level of a thread is
1030 ; It also enables the facility if its context is live
1032 ; R3 = Savearea to be released (virtual)
1033 ; R4 = New top of savearea stack (could be 0)
1034 ; R8 = pointer to activation
1035 ; R10 = per_proc block
1037 ; Note that barring unforseen crashes, there is no escape from this point
1038 ; on. We WILL call exception_exit and launch this context. No worries
1039 ; about preemption or interruptions here.
1041 ; Note that we will set up R26 with whatever context we will be launching,
1042 ; so it will indicate the current, or the deferred it it is set and we
1043 ; are going to user state. CR2_eq will be set to indicate deferred.
1046 chkfac: lwz r29,savesrr1+4(r3) ; Get the current MSR
1047 mr. r28,r8 ; Are we still in boot?
1048 mr r31,r10 ; Move per_proc address
1049 mr r30,r4 ; Preserve new level
1050 mr r27,r3 ; Save the old level
1051 beq-- chkenax ; Yeah, skip it all...
1053 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going into user state?
1055 lwz r20,curctx(r28) ; Get our current context
1056 lwz r26,deferctx(r28) ; Get any deferred context switch
1057 li r0,1 ; Get set to hold off quickfret
1058 rlwinm r29,r29,0,MSR_FP_BIT+1,MSR_FP_BIT-1 ; Turn off floating point for now
1059 lwz r21,FPUlevel(r20) ; Get the facility level
1060 cmplwi cr2,r26,0 ; Are we going into a deferred context later?
1061 rlwinm r29,r29,0,MSR_VEC_BIT+1,MSR_VEC_BIT-1 ; Turn off vector for now
1062 crnor cr2_eq,cr0_eq,cr2_eq ; Set cr2_eq if going to user state and there is deferred
1063 lhz r19,PP_CPU_NUMBER(r31) ; Get our CPU number
1064 cmplw r27,r21 ; Are we returning from the active level?
1065 stw r0,holdQFret(r31) ; Make sure we hold off releasing quickfret
1066 bne++ fpuchkena ; Nope...
1069 ; First clean up any live context we are returning from
1072 lwz r22,FPUcpu(r20) ; Get CPU this context was last dispatched on
1074 stw r19,FPUcpu(r20) ; Claim context for us
1076 eieio ; Make sure this gets out before owner clear
1079 #error per_proc_entry is not 16bytes in size
1082 lis r23,hi16(EXT(PerProcTable)) ; Set base PerProcTable
1083 slwi r22,r22,4 ; Find offset to the owner per_proc_entry
1084 ori r23,r23,lo16(EXT(PerProcTable)) ; Set base PerProcTable
1085 li r24,FPUowner ; Displacement to float owner
1086 add r22,r23,r22 ; Point to the owner per_proc_entry
1087 lwz r22,ppe_vaddr(r22) ; Point to the owner per_proc
1089 fpuinvothr: lwarx r23,r24,r22 ; Get the owner
1091 sub r0,r23,r20 ; Subtract one from the other
1092 sub r21,r20,r23 ; Subtract the other from the one
1093 or r21,r21,r0 ; Combine them
1094 srawi r21,r21,31 ; Get a 0 if equal or -1 of not
1095 and r23,r23,r21 ; Make 0 if same, unchanged if not
1096 stwcx. r23,r24,r22 ; Try to invalidate it
1097 bne-- fpuinvothr ; Try again if there was a collision...
1102 ; Now if there is a savearea associated with the popped context, release it.
1103 ; Either way, pop the level to the top stacked context.
1106 lwz r22,FPUsave(r20) ; Get pointer to the first savearea
1107 li r21,0 ; Assume we popped all the way out
1108 mr. r22,r22 ; Is there anything there?
1109 beq++ fpusetlvl ; No, see if we need to enable...
1111 lwz r21,SAVlevel(r22) ; Get the level of that savearea
1112 cmplw r21,r27 ; Is this the saved copy of the live stuff?
1113 bne fpusetlvl ; No, leave as is...
1115 lwz r24,SAVprev+4(r22) ; Pick up the previous area
1116 li r21,0 ; Assume we popped all the way out
1117 mr. r24,r24 ; Any more context stacked?
1118 beq-- fpuonlyone ; Nope...
1119 lwz r21,SAVlevel(r24) ; Get the level associated with save
1121 fpuonlyone: stw r24,FPUsave(r20) ; Dequeue this savearea
1123 rlwinm r3,r22,0,0,19 ; Find main savearea header
1125 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1126 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1127 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1128 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1129 stw r8,SAVprev(r22) ; Link the old in (top)
1130 stw r9,SAVprev+4(r22) ; Link the old in (bottom)
1131 xor r3,r22,r3 ; Convert to physical
1132 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1133 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
1136 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1137 li r2,0x3301 ; (TEST/DEBUG)
1138 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1142 fpusetlvl: stw r21,FPUlevel(r20) ; Save the level
1145 ; Here we check if we are at the right level
1146 ; We need to check the level we are entering, not the one we are exiting.
1147 ; Therefore, we will use the defer level if it is non-zero and we are
1148 ; going into user state.
1151 fpuchkena: bt-- cr2_eq,fpuhasdfrd ; Skip if deferred, R26 already set up...
1152 mr r26,r20 ; Use the non-deferred value
1156 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; (TEST/DEBUG) Going into user state?
1157 beq fpunusrstt ; (TEST/DEBUG) Nope...
1158 lwz r23,FPUlevel(r26) ; (TEST/DEBUG) Get the level ID
1159 lwz r24,FPUsave(r26) ; (TEST/DEBUG) Get the first savearea
1160 mr. r23,r23 ; (TEST/DEBUG) Should be level 0
1161 beq++ fpulvl0 ; (TEST/DEBUG) Yes...
1162 BREAKPOINT_TRAP ; (TEST/DEBUG)
1164 fpulvl0: mr. r24,r24 ; (TEST/DEBUG) Any context?
1165 beq fpunusrstt ; (TEST/DEBUG) No...
1166 lwz r23,SAVlevel(r24) ; (TEST/DEBUG) Get level of context
1167 lwz r21,SAVprev+4(r24) ; (TEST/DEBUG) Get previous pointer
1168 mr. r23,r23 ; (TEST/DEBUG) Is this our user context?
1169 beq++ fpulvl0b ; (TEST/DEBUG) Yes...
1170 BREAKPOINT_TRAP ; (TEST/DEBUG)
1172 fpulvl0b: mr. r21,r21 ; (TEST/DEBUG) Is there a forward chain?
1173 beq++ fpunusrstt ; (TEST/DEBUG) Nope...
1174 BREAKPOINT_TRAP ; (TEST/DEBUG)
1176 fpunusrstt: ; (TEST/DEBUG)
1179 lwz r21,FPUowner(r31) ; Get the ID of the live context
1180 lwz r23,FPUlevel(r26) ; Get the level ID
1181 lwz r24,FPUcpu(r26) ; Get the CPU that the context was last dispatched on
1182 cmplw cr3,r26,r21 ; Do we have the live context?
1183 cmplw r30,r23 ; Are we about to launch the live level?
1184 bne-- cr3,chkvec ; No, can not possibly enable...
1185 cmplw cr1,r19,r24 ; Was facility used on this processor last?
1186 bne-- chkvec ; No, not live...
1187 bne-- cr1,chkvec ; No, wrong cpu, have to enable later....
1189 lwz r24,FPUsave(r26) ; Get the first savearea
1190 mr. r24,r24 ; Any savearea?
1191 beq++ fpuena ; Nope...
1192 lwz r25,SAVlevel(r24) ; Get the level of savearea
1193 lwz r0,SAVprev+4(r24) ; Get the previous
1194 cmplw r30,r25 ; Is savearea for the level we are launching?
1195 bne++ fpuena ; No, just go enable...
1197 stw r0,FPUsave(r26) ; Pop the chain
1199 rlwinm r3,r24,0,0,19 ; Find main savearea header
1201 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1202 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1203 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1204 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1205 stw r8,SAVprev(r24) ; Link the old in (top)
1206 stw r9,SAVprev+4(r24) ; Link the old in (bottom)
1207 xor r3,r24,r3 ; Convert to physical
1208 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1209 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
1212 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1213 li r2,0x3302 ; (TEST/DEBUG)
1214 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1218 fpuena: ori r29,r29,lo16(MASK(MSR_FP)) ; Enable facility
1222 lwz r21,VMXlevel(r20) ; Get the facility level
1224 cmplw r27,r21 ; Are we returning from the active level?
1225 bne+ vmxchkena ; Nope...
1229 ; First clean up any live context we are returning from
1232 lwz r22,VMXcpu(r20) ; Get CPU this context was last dispatched on
1234 stw r19,VMXcpu(r20) ; Claim context for us
1236 eieio ; Make sure this gets out before owner clear
1238 lis r23,hi16(EXT(PerProcTable)) ; Set base PerProcTable
1239 slwi r22,r22,4 ; Find offset to the owner per_proc_entry
1240 ori r23,r23,lo16(EXT(PerProcTable)) ; Set base PerProcTable
1241 li r24,VMXowner ; Displacement to float owner
1242 add r22,r23,r22 ; Point to the owner per_proc_entry
1243 lwz r22,ppe_vaddr(r22) ; Point to the owner per_proc
1245 vmxinvothr: lwarx r23,r24,r22 ; Get the owner
1247 sub r0,r23,r20 ; Subtract one from the other
1248 sub r21,r20,r23 ; Subtract the other from the one
1249 or r21,r21,r0 ; Combine them
1250 srawi r21,r21,31 ; Get a 0 if equal or -1 of not
1251 and r23,r23,r21 ; Make 0 if same, unchanged if not
1252 stwcx. r23,r24,r22 ; Try to invalidate it
1253 bne-- vmxinvothr ; Try again if there was a collision...
1258 ; Now if there is a savearea associated with the popped context, release it.
1259 ; Either way, pop the level to the top stacked context.
1262 lwz r22,VMXsave(r20) ; Get pointer to the first savearea
1263 li r21,0 ; Assume we popped all the way out
1264 mr. r22,r22 ; Is there anything there?
1265 beq++ vmxsetlvl ; No, see if we need to enable...
1267 lwz r21,SAVlevel(r22) ; Get the level of that savearea
1268 cmplw r21,r27 ; Is this the saved copy of the live stuff?
1269 bne vmxsetlvl ; No, leave as is...
1271 lwz r24,SAVprev+4(r22) ; Pick up the previous area
1272 li r21,0 ; Assume we popped all the way out
1273 mr. r24,r24 ; Any more context?
1274 beq-- vmxonlyone ; Nope...
1275 lwz r21,SAVlevel(r24) ; Get the level associated with save
1277 vmxonlyone: stw r24,VMXsave(r20) ; Dequeue this savearea
1279 rlwinm r3,r22,0,0,19 ; Find main savearea header
1281 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1282 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1283 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1284 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1285 stw r8,SAVprev(r22) ; Link the old in (top)
1286 stw r9,SAVprev+4(r22) ; Link the old in (bottom)
1287 xor r3,r22,r3 ; Convert to physical
1288 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1289 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
1292 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1293 li r2,0x3401 ; (TEST/DEBUG)
1294 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1298 vmxsetlvl: stw r21,VMXlevel(r20) ; Save the level
1301 ; Here we check if we are at the right level
1304 vmxchkena: lwz r21,VMXowner(r31) ; Get the ID of the live context
1305 lwz r23,VMXlevel(r26) ; Get the level ID
1306 cmplw r26,r21 ; Do we have the live context?
1307 lwz r24,VMXcpu(r26) ; Get the CPU that the context was last dispatched on
1308 bne-- setena ; No, can not possibly enable...
1309 cmplw r30,r23 ; Are we about to launch the live level?
1310 cmplw cr1,r19,r24 ; Was facility used on this processor last?
1311 bne-- setena ; No, not live...
1312 bne-- cr1,setena ; No, wrong cpu, have to enable later....
1314 lwz r24,VMXsave(r26) ; Get the first savearea
1315 mr. r24,r24 ; Any savearea?
1316 beq++ vmxena ; Nope...
1317 lwz r25,SAVlevel(r24) ; Get the level of savearea
1318 lwz r0,SAVprev+4(r24) ; Get the previous
1319 cmplw r30,r25 ; Is savearea for the level we are launching?
1320 bne++ vmxena ; No, just go enable...
1322 stw r0,VMXsave(r26) ; Pop the chain
1324 rlwinm r3,r24,0,0,19 ; Find main savearea header
1326 lwz r8,quickfret(r31) ; Get the first in quickfret list (top)
1327 lwz r9,quickfret+4(r31) ; Get the first in quickfret list (bottom)
1328 lwz r2,SACvrswap(r3) ; Get the virtual to real conversion (top)
1329 lwz r3,SACvrswap+4(r3) ; Get the virtual to real conversion (bottom)
1330 stw r8,SAVprev(r24) ; Link the old in (top)
1331 stw r9,SAVprev+4(r24) ; Link the old in (bottom)
1332 xor r3,r24,r3 ; Convert to physical
1333 stw r2,quickfret(r31) ; Set the first in quickfret list (top)
1334 stw r3,quickfret+4(r31) ; Set the first in quickfret list (bottom)
1337 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1338 li r2,0x3402 ; (TEST/DEBUG)
1339 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1343 vmxena: oris r29,r29,hi16(MASK(MSR_VEC)) ; Enable facility
1345 setena: lwz r18,umwSpace(r28) ; Get the space ID in case we are launching user
1346 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we about to launch user state?
1347 li r0,0 ; Get set to release quickfret holdoff
1348 crmove cr7_eq,cr0_eq ; Remember if we are going to user state
1349 rlwimi. r20,r29,(((31-floatCngbit)+(MSR_FP_BIT+1))&31),floatCngbit,floatCngbit ; Set flag if we enabled floats
1350 lwz r19,deferctx(r28) ; Get any deferred facility context switch
1351 rlwinm r20,r29,(((31-vectorCngbit)+(MSR_VEC_BIT+1))&31),vectorCngbit,vectorCngbit ; Set flag if we enabled vector
1352 stw r29,savesrr1+4(r27) ; Turn facility on or off
1353 stw r0,holdQFret(r31) ; Release quickfret
1354 oris r18,r18,hi16(umwSwitchAway) ; Set the switch-away bit in case we go to user
1356 beq setenaa ; Neither float nor vector turned on....
1358 lwz r5,ACT_MACT_SPF(r28) ; Get activation copy
1359 lwz r6,spcFlags(r31) ; Get per_proc copy
1360 or r5,r5,r20 ; Set vector/float changed bits in activation
1361 or r6,r6,r20 ; Set vector/float changed bits in per_proc
1362 stw r5,ACT_MACT_SPF(r28) ; Set activation copy
1363 stw r6,spcFlags(r31) ; Set per_proc copy
1365 setenaa: mfdec r24 ; Get decrementer
1366 bf+ cr2_eq,nodefer ; No deferred to switch to...
1368 li r20,0 ; Clear this
1369 stw r26,curctx(r28) ; Make the facility context current
1370 stw r20,deferctx(r28) ; Clear deferred context
1372 nodefer: lwz r22,qactTimer(r28) ; Get high order quick activation timer
1373 mr. r24,r24 ; See if it has popped already...
1374 lwz r23,qactTimer+4(r28) ; Get low order qact timer
1375 ble- chkifuser ; We have popped or are just about to...
1377 segtb: mftbu r20 ; Get the upper time base
1378 mftb r21 ; Get the low
1379 mftbu r19 ; Get upper again
1380 or. r0,r22,r23 ; Any time set?
1381 cmplw cr1,r20,r19 ; Did they change?
1382 beq++ chkifuser ; No time set....
1383 bne-- cr1,segtb ; Timebase ticked, get them again...
1385 subfc r6,r21,r23 ; Subtract current from qact time
1387 subfe r5,r20,r22 ; Finish subtract
1388 subfze r0,r0 ; Get a 0 if qact was bigger than current, -1 otherwise
1389 andc. r12,r5,r0 ; Set 0 if qact has passed
1390 andc r13,r6,r0 ; Set 0 if qact has passed
1391 bne chkifuser ; If high order is non-zero, this is too big for a decrementer
1392 cmplw r13,r24 ; Is this earlier than the decrementer? (logical compare takes care of high bit on)
1393 bge++ chkifuser ; No, do not reset decrementer...
1395 mtdec r13 ; Set our value
1397 chkifuser: addi r4,r28,SYSTEM_TIMER
1399 beq-- cr7,chkifuser1 ; Skip this if we are going to kernel...
1400 stw r18,umwSpace(r28) ; Half-invalidate to force MapUserAddressWindow to reload SRs
1401 addi r4,r28,USER_TIMER
1403 chkifuser1: bl EXT(timer_event)
1408 lwz r20,SAVact(r27) ; (TEST/DEBUG) Make sure our restore
1409 mfsprg r21, 1 ; (TEST/DEBUG) with the current act.
1410 cmpwi r21,0 ; (TEST/DEBUG)
1411 beq-- yeswereok ; (TEST/DEBUG)
1412 cmplw r21,r20 ; (TEST/DEBUG)
1413 beq++ yeswereok ; (TEST/DEBUG)
1415 lis r0,hi16(Choke) ; (TEST/DEBUG) Choke code
1416 ori r0,r0,lo16(Choke) ; (TEST/DEBUG) and the rest
1417 mr r21,r27 ; (TEST/DEBUG) Save the savearea address
1418 li r3,failContext ; (TEST/DEBUG) Bad state code
1419 sc ; (TEST/DEBUG) System ABEND
1424 mr r3,r27 ; Pass savearea back
1425 b EXT(exception_exit) ; We are all done now...
1430 ; Null PPC call - performance testing, does absolutely nothing
1439 li r3,-1 ; Make sure we test no asts
1444 ; Instrumented null PPC call - performance testing, does absolutely nothing
1445 ; Forces various timestamps to be returned.
1450 .globl EXT(ppcNullinst)
1454 li r3,-1 ; Make sure we test no asts
1459 * Here's where we handle the fastpath stuff
1460 * We'll do what we can here because registers are already
1461 * loaded and it will be less confusing that moving them around.
1462 * If we need to though, we'll branch off somewhere's else.
1464 * Registers when we get here:
1466 * r0 = syscall number
1469 * r14 = previous savearea (if any)
1476 fastpath: cmplwi cr3,r0,0x7FF5 ; Is this a null fastpath?
1477 beq-- cr3,fastexutl ; Yes, bail fast...
1478 cmplwi cr3,r0,0x7FF1 ; Is it CthreadSetSelfNumber?
1479 bnelr-- cr3 ; Not a fast path...
1482 * void cthread_set_self(cproc_t p)
1484 * Set's thread state "user_value". In practice this is the thread-local-data-pointer (TLDP),
1485 * though we do not interpret it. This call is mostly used by 32-bit tasks, but we save all 64 bits
1486 * in case a 64-bit task wants to use this facility. They normally do not, because the 64-bit
1487 * ABI reserves r13 for the TLDP.
1489 * This op is invoked as follows:
1490 * li r0, CthreadSetSelfNumber // load the fast-trap number
1491 * sc // invoke fast-trap
1495 CthreadSetSelfNumber:
1496 lwz r3,saver3+0(r4) /* get the TLDP passed in r3 */
1497 lwz r5,saver3+4(r4) /* (all 64 bits, in case this is a 64-bit task) */
1498 stw r3,CTHREAD_SELF+0(r13) /* Remember it in the activation... */
1499 stw r5,CTHREAD_SELF+4(r13)
1500 stw r3,UAW+0(r25) /* ...and in the per-proc */
1504 .globl EXT(fastexit)
1506 fastexutl: mr r3,r4 ; Pass back savearea
1507 b EXT(exception_exit) ; Go back to the caller...
1511 * Here's where we check for a hit on the Blue Box Assist
1512 * Most registers are non-volatile, so be careful here. If we don't
1513 * recognize the trap instruction we go back for regular processing.
1514 * Otherwise we transfer to the assist code.
1520 lwz r0,saveexception(r4) ; Get the exception code
1521 lwz r23,savesrr1+4(r4) ; Get the interrupted MSR
1522 lwz r26,ACT_MACT_BEDA(r13) ; Get Blue Box Descriptor Area
1523 mtcrf 0x18,r23 ; Check what SRR1 says
1524 lwz r24,ACT_MACT_BTS(r13) ; Get the table start
1525 cmplwi r0,T_AST ; Check for T_AST trap
1526 lwz r27,savesrr0+4(r4) ; Get trapped address
1527 crnand cr1_eq,SRR1_PRG_TRAP_BIT,MSR_PR_BIT ; We need both trap and user state
1528 sub r24,r27,r24 ; See how far into it we are
1529 cror cr0_eq,cr0_eq,cr1_eq ; Need to bail if AST or not trap or not user state
1530 cmplwi cr1,r24,BB_MAX_TRAP ; Do we fit in the list?
1531 cror cr0_eq,cr0_eq,cr1_gt ; Also leave it trap not in range
1532 btlr- cr0_eq ; No assist if AST or not trap or not user state or trap not in range
1533 b EXT(atomic_switch_trap) ; Go to the assist...
1536 ; Virtual Machine Monitor
1537 ; Here is where we exit from the emulated context
1538 ; Note that most registers get trashed here
1539 ; R3 and R30 are preserved across the call and hold the activation
1540 ; and savearea respectivily.
1545 exitFromVM: mr r30,r4 ; Get the savearea
1546 mr r3,r13 ; Get the activation
1548 b EXT(vmm_exit) ; Do it to it
1551 .globl EXT(retFromVM)
1554 mfsprg r10,1 ; Get the current activation
1555 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
1556 mr r8,r3 ; Get the activation
1557 lwz r4,SAVprev+4(r30) ; Pick up the previous savearea
1558 mr r3,r30 ; Put savearea in proper register for common code
1559 lwz r11,SAVflags(r30) ; Get the flags of the current savearea
1560 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
1562 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
1564 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
1566 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
1567 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
1568 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
1569 b chkfac ; Go end it all...
1573 ; chandler (note: not a candle maker or tallow merchant)
1575 ; Here is the system choke handler. This is where the system goes
1578 ; We get here as a result of a T_CHOKE exception which is generated
1579 ; by the Choke firmware call or by lowmem_vectors when it detects a
1580 ; fatal error. Examples of where this may be used is when we detect
1581 ; problems in low-level mapping chains, trashed savearea free chains,
1582 ; or stack guardpage violations.
1584 ; Note that we can not set a back chain in the stack when we come
1585 ; here because we are probably here because the chain was corrupt.
1590 .globl EXT(chandler)
1591 LEXT(chandler) ; Choke handler
1594 mfsprg r25,1 ; Get the current activation
1595 lwz r25,ACT_PER_PROC(r25) ; Get the per_proc block
1596 stw r31,traceMask(0) ; Force tracing off right now
1600 lwz r1,PP_DEBSTACKPTR(r25) ; Get debug stack pointer
1601 cmpwi r1,-1 ; Are we already choking?
1602 bne chokefirst ; Nope...
1604 chokespin: addi r31,r31,1 ; Spin and hope for an analyzer connection...
1605 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1606 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1607 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1608 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1609 addi r31,r31,1 ; Spin and hope for an analyzer connection...
1610 b chokespin ; Spin and hope for an analyzer connection...
1612 chokefirst: li r0,-1 ; Set choke value
1613 mr. r1,r1 ; See if we are on debug stack yet
1614 lwz r10,saver1+4(r4) ;
1615 stw r0,PP_DEBSTACKPTR(r25) ; Show we are choking
1616 bne chokestart ; We are not on the debug stack yet...
1618 lwz r2,PP_DEBSTACK_TOP_SS(r25) ; Get debug stack top
1619 sub r11,r2,r10 ; Get stack depth
1621 cmplwi r11,KERNEL_STACK_SIZE-FM_SIZE-TRAP_SPACE_NEEDED ; Check if stack pointer is ok
1622 bgt chokespin ; Bad stack pointer or too little left, just die...
1624 subi r1,r10,FM_REDZONE ; Make a red zone
1626 chokestart: li r0,0 ; Get a zero
1627 stw r0,FM_BACKPTR(r1) ; We now have terminated the back chain
1629 bl EXT(SysChoked) ; Call the "C" phase of this
1630 b chokespin ; Should not be here so just go spin...
1635 ; Savearea chain verification
1640 lis r22,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1641 ori r22,r22,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1642 lwz r23,0(r22) ; (TEST/DEBUG)
1643 mr. r23,r23 ; (TEST/DEBUG)
1644 beqlr- ; (TEST/DEBUG)
1645 mfsprg r20,1 ; Get the current activation
1646 lwz r20,ACT_PER_PROC(r20) ; Get the per_proc block
1647 lwz r21,pfAvailable(r20) ; (TEST/DEBUG)
1648 mr. r21,r21 ; (TEST/DEBUG)
1649 bnelr+ ; (TEST/DEBUG)
1651 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1652 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1656 ;; This code is broken and migration will make the matter even worse
1658 ; Make sure that all savearea chains have the right type on them
1661 lis r28,hi16(EXT(default_pset)) ; (TEST/DEBUG)
1662 lis r27,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1663 ori r28,r28,lo16(EXT(default_pset)) ; (TEST/DEBUG)
1664 ori r27,r27,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1665 li r20,0 ; (TEST/DEBUG)
1666 lwz r26,0(r27) ; (TEST/DEBUG)
1667 lwz r27,psthreadcnt(r28) ; (TEST/DEBUG)
1668 mr. r26,r26 ; (TEST/DEBUG) Have we locked the test out?
1669 lwz r28,psthreads(r28) ; (TEST/DEBUG)
1670 mflr r31 ; (TEST/DEBUG) Save return
1671 bnelr- ; (TEST/DEBUG) Test already triggered, skip...
1672 b fckgo ; (TEST/DEBUG) Join up...
1674 fcknext: mr. r27,r27 ; (TEST/DEBUG) Any more threads?
1675 bne+ fckxxx ; (TEST/DEBUG) Yes...
1677 mtlr r31 ; (TEST/DEBUG) Restore return
1678 blr ; (TEST/DEBUG) Leave...
1680 fckxxx: lwz r28,THREAD_PSTHRN(r28) ; (TEST/DEBUG) Get next thread
1682 fckgo: subi r27,r27,1 ; (TEST/DEBUG) Decrement thread count
1683 lwz r24,THREAD_TOP_ACT(r28) ; (TEST/DEBUG) Get activation for the thread
1684 lwz r20,ACT_MACT_PCB(r24) ; (TEST/DEBUG) Get the normal context
1685 li r21,SAVgeneral ; (TEST/DEBUG) Make sure this is all general context
1686 bl versavetype ; (TEST/DEBUG) Check the chain
1688 lwz r20,facctx+FPUsave(r24) ; (TEST/DEBUG) Get regular floating point
1689 li r21,SAVfloat ; (TEST/DEBUG) Make sure this is all floating point
1690 bl versavetype ; (TEST/DEBUG) Check the chain
1692 lwz r20,facctx+VMXsave(r24) ; (TEST/DEBUG) Get regular vector point
1693 li r21,SAVvector ; (TEST/DEBUG) Make sure this is all vector
1694 bl versavetype ; (TEST/DEBUG) Check the chain
1696 lwz r29,vmmControl(r24) ; (TEST/DEBUG) Get the virtual machine control blocks
1697 mr. r29,r29 ; (TEST/DEBUG) Are there any?
1698 beq+ fcknext ; (TEST/DEBUG) Nope, next thread...
1700 li r22,kVmmMaxContextsPerThread ; (TEST/DEBUG) Get the number of control blocks
1701 subi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get running start
1703 fcknvmm: subi r22,r22,1 ; (TEST/DEBUG) Do all of them
1704 mr. r22,r22 ; (TEST/DEBUG) Are we all done?
1705 addi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get the next entry
1706 blt- fcknext ; (TEST/DEBUG) Yes, check next thread...
1708 lwz r23,vmmFlags(r29) ; (TEST/DEBUG) Get entry flags
1709 rlwinm. r23,r23,0,0,0 ; (TEST/DEBUG) Is this in use?
1710 beq+ fcknvmm ; (TEST/DEBUG) Not in use...
1712 lwz r20,vmmFacCtx+FPUsave(r29) ; (TEST/DEBUG) Get regular floating point
1713 li r21,SAVfloat ; (TEST/DEBUG) Make sure this is all floating point
1714 bl versavetype ; (TEST/DEBUG) Check the chain
1716 lwz r20,vmmFacCtx+VMXsave(r29) ; (TEST/DEBUG) Get regular vector point
1717 li r21,SAVvector ; (TEST/DEBUG) Make sure this is all vector
1718 bl versavetype ; (TEST/DEBUG) Check the chain
1719 b fcknvmm ; (TEST/DEBUG) Get then vmm block...
1722 mr. r20,r20 ; (TEST/DEBUG) Chain done?
1723 beqlr- ; (TEST/DEBUG) Yes...
1725 lwz r23,SAVflags(r20) ; (TEST/DEBUG) Get the flags
1726 rlwinm r23,r23,24,24,31 ; (TEST/DEBUG) Position it
1727 cmplw r23,r21 ; (TEST/DEBUG) Are we the correct type?
1728 beq+ versvok ; (TEST/DEBUG) This one is ok...
1730 lis r22,hi16(EXT(DebugWork)) ; (TEST/DEBUG)
1731 ori r22,r22,lo16(EXT(DebugWork)) ; (TEST/DEBUG)
1732 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1733 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1735 versvok: lwz r20,SAVprev+4(r20) ; (TEST/DEBUG) Get the previous one
1736 b versavetype ; (TEST/DEBUG) Go check its type...