]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/hw_exception.s
xnu-792.tar.gz
[apple/xnu.git] / osfmk / ppc / hw_exception.s
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25
26/* Low level routines dealing with exception entry and exit.
27 * There are various types of exception:
28 *
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).
32 *
33 * The code for the FPU disabled handler (lazy fpu) is in cswtch.s
34 */
35
36#include <debug.h>
37#include <mach_assert.h>
38#include <mach/exception_types.h>
55e303ae 39#include <mach/kern_return.h>
1c79356b
A
40#include <mach/ppc/vm_param.h>
41
42#include <assym.s>
43
44#include <ppc/asm.h>
45#include <ppc/proc_reg.h>
46#include <ppc/trap.h>
47#include <ppc/exception.h>
9bccf70c 48#include <ppc/savearea.h>
1c79356b
A
49
50
51#define VERIFYSAVE 0
52#define FPVECDBG 0
55e303ae
A
53#define INSTRUMENT 0
54
1c79356b
A
55/*
56 * thandler(type)
57 *
58 * ENTRY: VM switched ON
59 * Interrupts OFF
60 * R3 contains exception code
61 * R4 points to the saved context (virtual address)
62 * Everything is saved in savearea
63 */
64
65/*
66 * If pcb.ksp == 0 then the kernel stack is already busy,
9bccf70c 67 * we make a stack frame
1c79356b
A
68 * leaving enough space for the 'red zone' in case the
69 * trapped thread was in the middle of saving state below
70 * its stack pointer.
71 *
9bccf70c 72 * otherwise we make a stack frame and
1c79356b
A
73 * the kernel stack (setting pcb.ksp to 0)
74 *
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
77 */
78
1c79356b
A
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 :-)
84 */
85
86#define TRAP_SPACE_NEEDED FM_REDZONE+(2*FM_SIZE)+256
87
1c79356b
A
88 .text
89
0b4e3aa0
A
90 .align 5
91 .globl EXT(thandler)
9bccf70c 92LEXT(thandler) ; Trap handler
0b4e3aa0 93
91447636
A
94 mfsprg r13,1 ; Get the current activation
95 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
1c79356b 96
9bccf70c 97 lwz r1,PP_ISTACKPTR(r25) ; Get interrupt stack pointer
1c79356b 98
9bccf70c 99 cmpwi cr0,r1,0 ; Are we on interrupt stack?
91447636 100 mr r6,r13
9bccf70c 101 beq- cr0,EXT(ihandler) ; If on interrupt stack, treat this as interrupt...
9bccf70c
A
102 lwz r26,ACT_MACT_SPF(r13) ; Get special flags
103 lwz r8,ACT_MACT_PCB(r13) ; Get the last savearea used
0b4e3aa0 104 rlwinm. r26,r26,0,bbThreadbit,bbThreadbit ; Do we have Blue Box Assist active?
9bccf70c
A
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
55e303ae 108 stw r8,SAVprev+4(r4) ; Queue the new save area in the front
1c79356b
A
109
110#if VERIFYSAVE
9bccf70c 111 bl versave ; (TEST/DEBUG)
1c79356b
A
112#endif
113
9bccf70c
A
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
55e303ae 117 lwz r26,saver1+4(r4) ; Get the stack at interrupt time
1c79356b 118
9bccf70c 119 bne+ cr1,.L_kstackfree ; We are not on kernel stack yet...
1c79356b 120
9bccf70c 121 subi r1,r26,FM_REDZONE ; Make a red zone on interrupt time kernel stack
1c79356b
A
122
123.L_kstackfree:
91447636 124 lwz r31,savesrr1+4(r4) ; Pick up the entry MSR
9bccf70c
A
125 sub r9,r1,r9 ; Get displacment into the kernel stack
126 li r0,0 ; Make this 0
55e303ae 127 rlwinm. r0,r9,0,28,31 ; Verify that we have a 16-byte aligned stack (and get a 0)
9bccf70c
A
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
1c79356b 130
9bccf70c 131 stw r0,ACT_MACT_KSP(r13) ; Show that we have taken the stack
1c79356b
A
132
133.L_state_on_kstack:
9bccf70c 134 lwz r9,savevrsave(r4) ; Get the VRSAVE register
55e303ae 135 bne-- kernelStackUnaligned ; Stack is unaligned...
91447636 136 rlwinm. r6,r31,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
9bccf70c 137 subi r1,r1,FM_SIZE ; Push a header onto the current stack
55e303ae 138 bgt-- cr2,kernelStackBad ; Kernel stack is bogus...
9bccf70c
A
139
140kernelStackNotBad: ; Vector was off
55e303ae 141 beq++ tvecoff ; Vector off, do not save vrsave...
9bccf70c
A
142 stw r9,liveVRS(r25) ; Set the live value
143
144tvecoff: stw r26,FM_BACKPTR(r1) ; Link back to the previous frame
1c79356b
A
145
146#if DEBUG
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
150 */
55e303ae 151 lwz r3,savesrr0+4(r4) ; Get the point of interruption
9bccf70c
A
152 stw r3,FM_LR_SAVE(r1) ; save old instr ptr as LR value
153 stwu r1, -FM_SIZE(r1) ; and make new frame
1c79356b
A
154#endif /* DEBUG */
155
91447636
A
156 mr r30,r4
157 lwz r3,SAVtime+4(r4)
158 addi r4,r13,SYSTEM_TIMER
159 bl EXT(timer_event)
1c79356b
A
160
161/* call trap handler proper, with
91447636
A
162 * ARG0 = type
163 * ARG1 = saved_state ptr
164 * ARG2 = dsisr
165 * ARG3 = dar
1c79356b
A
166 */
167
91447636
A
168 mr r4,r30
169 lwz r3,saveexception(r30) ; Get the exception code
9bccf70c 170 lwz r0,ACT_MACT_SPF(r13) ; Get the special flags
1c79356b 171
9bccf70c 172 addi r5,r3,-T_DATA_ACCESS ; Adjust to start of range
1c79356b 173 rlwinm. r0,r0,0,runningVMbit,runningVMbit ; Are we in VM state? (cr0_eq == 0 if yes)
9bccf70c 174 cmplwi cr2,r5,T_TRACE-T_DATA_ACCESS ; Are we still in range? (cr_gt if not)
1c79356b 175
9bccf70c 176 lwz r5,savedsisr(r4) ; Get the saved DSISR
1c79356b 177
9bccf70c 178 crnor cr7_eq,cr0_eq,cr2_gt ; We should intercept if in VM and is a true trap (cr7_eq == 1 if yes)
91447636 179 rlwinm. r0,r31,0,MSR_PR_BIT,MSR_PR_BIT ; Are we trapping from supervisor state? (cr0_eq == 1 if yes)
1c79356b 180
9bccf70c 181 cmpi cr2,r3,T_PREEMPT ; Is this a preemption?
55e303ae
A
182
183 beq-- .L_check_VM
184 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
185.L_check_VM:
1c79356b 186
9bccf70c 187 crandc cr0_eq,cr7_eq,cr0_eq ; Do not intercept if we are in the kernel (cr0_eq == 1 if yes)
1c79356b 188
55e303ae
A
189 lwz r6,savedar(r4) ; Get the DAR (top)
190 lwz r7,savedar+4(r4) ; Get the DAR (bottom)
1c79356b 191
55e303ae 192 beq- cr2,.L_call_trap ; Do not turn on interrupts for T_PREEMPT
9bccf70c 193 beq- exitFromVM ; Any true trap but T_MACHINE_CHECK exits us from the VM...
1c79356b
A
194
195/* syscall exception might warp here if there's nothing left
196 * to do except generate a trap
197 */
198
199.L_call_trap:
1c79356b
A
200
201 bl EXT(trap)
202
55e303ae 203 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 204 mfmsr r7 ; Get the MSR
55e303ae
A
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
9bccf70c 207 mtmsr r7 ; Disable for interrupts
91447636
A
208 mfsprg r8,1 ; Get the current activation
209 lwz r10,ACT_PER_PROC(r8) ; Get the per_proc block
1c79356b 210/*
1c79356b
A
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.
1c79356b
A
214 */
215
216thread_return:
9bccf70c 217 lwz r11,SAVflags(r3) ; Get the flags of the current savearea
55e303ae
A
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
d7e50217 221 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
55e303ae 222 rlwinm. r0,r0,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going to the user?
91447636 223 mr r1,r8
9bccf70c 224 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
55e303ae
A
225
226 lwz r5,THREAD_KERNEL_STACK(r1) ; Get the base pointer to the stack
9bccf70c 227 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
55e303ae 228 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
1c79356b 229
55e303ae 230 beq-- chkfac ; We are not leaving the kernel yet...
1c79356b 231
9bccf70c
A
232 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
233 b chkfac ; Go end it all...
1c79356b
A
234
235
0b4e3aa0
A
236;
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.
239;
240
241kernelStackBad:
242
9bccf70c 243 lwz r3,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
0b4e3aa0 244 subi r3,r3,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
9bccf70c 245 sub r3,r1,r3 ; Get displacement into debug stack
0b4e3aa0 246 cmplwi cr2,r3,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
9bccf70c 247 blt+ cr2,kernelStackNotBad ; Yeah, that is ok too...
0b4e3aa0 248
9bccf70c
A
249 lis r0,hi16(Choke) ; Choke code
250 ori r0,r0,lo16(Choke) ; and the rest
251 li r3,failStack ; Bad stack code
252 sc ; System ABEND
0b4e3aa0 253
55e303ae
A
254kernelStackUnaligned:
255 lis r0,hi16(Choke) ; Choke code
256 ori r0,r0,lo16(Choke) ; and the rest
257 li r3,failUnalignedStk ; Unaligned stack code
258 sc ; System ABEND
259
1c79356b
A
260
261/*
262 * shandler(type)
263 *
264 * ENTRY: VM switched ON
265 * Interrupts OFF
266 * R3 contains exception code
267 * R4 points to the saved context (virtual address)
268 * Everything is saved in savearea
269 */
270
271/*
272 * If pcb.ksp == 0 then the kernel stack is already busy,
273 * this is an error - jump to the debugger entry
274 *
275 * otherwise depending upon the type of
276 * syscall, look it up in the kernel table
277 * or pass it to the server.
278 *
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.
281 */
282
283/*
284 * NOTE:
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
289 */
290
0b4e3aa0
A
291 .align 5
292 .globl EXT(shandler)
9bccf70c
A
293LEXT(shandler) ; System call handler
294
55e303ae 295 lwz r7,savesrr1+4(r4) ; Get the SRR1 value
91447636
A
296 mfsprg r13,1 ; Get the current activation
297 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
55e303ae 298 lwz r0,saver0+4(r4) ; Get the original syscall number
9bccf70c
A
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?
9bccf70c 302 lwz r9,savevrsave(r4) ; Get the VRsave register
55e303ae 303 beq-- EXT(ihandler) ; On interrupt stack, not allowed...
1c79356b 304 rlwinm. r6,r7,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
91447636 305 mr r16,r13
1c79356b 306
55e303ae 307 beq++ svecoff ; Vector off, do not save vrsave...
9bccf70c
A
308 stw r9,liveVRS(r25) ; Set the live value
309;
1c79356b 310; Check if SCs are being redirected for the BlueBox or to VMM
9bccf70c 311;
1c79356b 312
9bccf70c 313svecoff: lwz r6,ACT_MACT_SPF(r13) ; Pick up activation special flags
55e303ae
A
314 mtcrf 0x40,r6 ; Check special flags
315 mtcrf 0x01,r6 ; Check special flags
9bccf70c 316 crmove cr6_eq,runningVMbit ; Remember if we are in VMM
55e303ae 317 bne++ cr6,sVMchecked ; Not running VM
d7e50217
A
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:
322 bne sVMchecked
55e303ae 323 lwz r26,saver3+4(r4) ; Get the original syscall number
d7e50217
A
324 cmpwi cr6,r26,kvmmExitToHost ; vmm_exit_to_host request
325sVMchecked:
55e303ae 326 bf++ bbNoMachSCbit,noassist ; Take branch if SCs are not redirected
9bccf70c
A
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...
1c79356b 329
9bccf70c
A
330noassist: cmplwi r15,0x7000 ; Do we have a fast path trap?
331 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
55e303ae 332 beql fastpath ; We think it is a fastpath...
1c79356b 333
9bccf70c 334 lwz r1,ACT_MACT_KSP(r13) ; Get the kernel stack pointer
1c79356b 335#if DEBUG
9bccf70c
A
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?
1c79356b
A
339#endif /* DEBUG */
340
9bccf70c 341 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
55e303ae 342 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
9bccf70c 343 li r0,0 ; Clear this out
55e303ae 344 stw r14,SAVprev+4(r4) ; Queue the new save area in the front
9bccf70c 345 stw r13,SAVact(r4) ; Point the savearea at its activation
1c79356b
A
346
347#if VERIFYSAVE
9bccf70c 348 bl versave ; (TEST/DEBUG)
1c79356b
A
349#endif
350
55e303ae 351 lwz r15,saver1+4(r4) ; Grab interrupt time stack
9bccf70c 352 mr r30,r4 ; Save pointer to the new context savearea
9bccf70c
A
353 stw r0,ACT_MACT_KSP(r13) ; Mark stack as busy with 0 val
354 stw r15,FM_BACKPTR(r1) ; Link stack frame backwards
91447636
A
355
356 lwz r3,SAVtime+4(r30)
357 addi r4,r13,SYSTEM_TIMER
358 bl EXT(timer_event)
1c79356b
A
359
360#if DEBUG
9bccf70c
A
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
364 */
55e303ae 365 lwz r8,savesrr0+4(r30) ; Get the point of interruption
9bccf70c
A
366 stw r8,FM_LR_SAVE(r1) ; Save old instr ptr as LR value
367 stwu r1, -FM_SIZE(r1) ; and make new frame
1c79356b
A
368#endif /* DEBUG */
369
91447636
A
370 mr r4,r30
371
de355530 372 lwz r15,SAVflags(r30) ; Get the savearea flags
55e303ae
A
373 lwz r0,saver0+4(r30) ; Get R0 back
374 mfmsr r11 ; Get the MSR
91447636 375 stwu r1,-(FM_SIZE+ARG_SIZE+MUNGE_ARGS_SIZE)(r1) ; Make a stack frame
9bccf70c 376 ori r11,r11,lo16(MASK(MSR_EE)) ; Turn on interruption enabled bit
de355530 377 rlwinm r10,r0,0,0,19 ; Keep only the top part
55e303ae 378 oris r15,r15,SAVsyscall >> 16 ; Mark that it this is a syscall
9bccf70c
A
379 cmplwi r10,0x6000 ; Is it the special ppc-only guy?
380 stw r15,SAVflags(r30) ; Save syscall marker
55e303ae 381 beq-- cr6,exitFromVM ; It is time to exit from alternate context...
9bccf70c 382
55e303ae 383 beq-- ppcscall ; Call the ppc-only system call handler...
9bccf70c 384
55e303ae 385 mr. r0,r0 ; What kind is it?
9bccf70c
A
386 mtmsr r11 ; Enable interruptions
387
55e303ae 388 blt-- .L_kernel_syscall ; System call number if negative, this is a mach call...
9bccf70c 389
55e303ae 390 lwz r8,ACT_TASK(r13) ; Get our task
9bccf70c 391 cmpwi cr0,r0,0x7FFA ; Special blue box call?
55e303ae 392 beq-- .L_notify_interrupt_syscall ; Yeah, call it...
9bccf70c 393
9bccf70c 394 lwz r7,TASK_SYSCALLS_UNIX(r8) ; Get the current count
9bccf70c
A
395 mr r3,r30 ; Get PCB/savearea
396 mr r4,r13 ; current activation
55e303ae
A
397 addi r7,r7,1 ; Bump it
398 stw r7,TASK_SYSCALLS_UNIX(r8) ; Save it
9bccf70c 399 bl EXT(unix_syscall) ; Check out unix...
1c79356b
A
400
401.L_call_server_syscall_exception:
9bccf70c 402 li r3,EXC_SYSCALL ; doexception(EXC_SYSCALL, num, 1)
1c79356b
A
403
404.L_call_server_exception:
9bccf70c 405 mr r4,r0 ; Set syscall selector
1c79356b 406 li r5,1
9bccf70c 407 b EXT(doexception) ; Go away, never to return...
1c79356b
A
408
409.L_notify_interrupt_syscall:
55e303ae 410 lwz r3,saver3+4(r30) ; Get the new PC address to pass in
1c79356b 411 bl EXT(syscall_notify_interrupt)
55e303ae
A
412/*
413 * Ok, return from C function, R3 = return value
414 *
415 * saved state is still in R30 and the active thread is in R16 .
416 */
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
1c79356b
A
420
421;
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.
429;
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.
433;
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
438;
439; Note: the last option is intended for special diagnostics calls that
440; want the thread to return and execute before checking for preemption.
441;
9bccf70c
A
442; NOTE: Both R16 (thread) and R30 (savearea) need to be preserved over this call!!!!
443;
444
445 .align 5
1c79356b 446
9bccf70c
A
447ppcscall: 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
1c79356b 451 bgt- .L_call_server_syscall_exception ; Bogus call...
9bccf70c 452 lwzx r11,r10,r11 ; Get function address
1c79356b
A
453
454;
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.
458;
459
9bccf70c
A
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
55e303ae 463 mtctr r11 ; Set the function address
1c79356b 464 beq- .L_call_server_syscall_exception ; Disabled call...
55e303ae
A
465#if INSTRUMENT
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
474#endif
475 bctrl ; Call it
9bccf70c 476
1c79356b 477 .globl EXT(ppcscret)
9bccf70c 478
1c79356b 479LEXT(ppcscret)
9bccf70c
A
480 mr. r3,r3 ; See what we should do
481 mr r31,r16 ; Restore the current thread pointer
1c79356b 482 bgt+ .L_thread_syscall_ret_check_ast ; Take normal AST checking return....
91447636
A
483 mfsprg r10,1 ; Get the current activation
484 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
9bccf70c 485 blt+ .L_thread_syscall_return ; Return, but no ASTs....
55e303ae 486 lwz r0,saver0+4(r30) ; Restore the system call number
1c79356b
A
487 b .L_call_server_syscall_exception ; Go to common exit...
488
489
55e303ae
A
490
491/*
492 * we get here for mach system calls
493 * when kdebug tracing is enabled
494 */
495
496ksystrace:
497 mr r4,r30 ; Pass in saved state
498 bl EXT(syscall_trace)
499
500 cmplw r31,r29 ; Is this syscall in the table?
501 add r31,r27,r28 ; Point right to the syscall table entry
502
503 bge- .L_call_server_syscall_exception ; The syscall number is invalid
504
91447636
A
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
510
511.L_ksystrace_munge:
512 cmplwi r0,0 ; do we have a munger to call?
55e303ae 513 mtctr r0 ; Set the function call address
91447636
A
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
518
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
523 bctrl
524
55e303ae
A
525 mr r4,r30 ; Pass in the savearea
526 bl EXT(syscall_trace_end) ; Trace the exit of the system call
527 b .L_mach_return
528
529
530
1c79356b
A
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)
537 */
1c79356b 538
55e303ae 539 .align 5
1c79356b 540
9bccf70c
A
541.L_kernel_syscall:
542;
543; Call a function that can print out our syscall info
544; Note that we don t care about any volatiles yet
545;
55e303ae
A
546 lwz r10,ACT_TASK(r13) ; Get our task
547 lwz r0,saver0+4(r30)
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
552
553 lwz r7,TASK_SYSCALLS_MACH(r10) ; Get the current count
554 neg r31,r0 ; Make this positive
91447636
A
555 mr r3,r31 ; save it
556 slwi r27,r3,4 ; multiply by 16
557 slwi r3,r3,2 ; and the original by 4
55e303ae 558 ori r28,r28,lo16(EXT(mach_trap_table)) ; Get address of table
91447636 559 add r27,r27,r3 ; for a total of 20x (5 words/entry)
55e303ae
A
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
564
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
567
568 bge-- .L_call_server_syscall_exception ; The syscall number is invalid
de355530 569
91447636
A
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
575
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
583
584.L_kernel_syscall_trapcall:
585 lwz r0,MACH_TRAP_FUNCTION(r31) ; Pick up the function address
55e303ae 586 mtctr r0 ; Set the function call address
91447636
A
587 addi r3,r1,FM_ARG0+ARG_SIZE ; Pointer to munged args
588 bctrl
589
1c79356b
A
590
591/*
9bccf70c 592 * Ok, return from C function, R3 = return value
1c79356b
A
593 *
594 * get the active thread's PCB pointer and thus pointer to user state
55e303ae 595 * saved state is still in R30 and the active thread is in R16
1c79356b
A
596 */
597
91447636
A
598.L_mach_return:
599 srawi r0,r3,31 ; properly extend the return code
600 cmpi cr0,r3,KERN_INVALID_ARGUMENT ; deal with invalid system calls
55e303ae 601 mr r31,r16 ; Move the current thread pointer
91447636
A
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
55e303ae
A
605.L_mach_invalid_arg:
606
607
608/* 'standard' syscall returns here - INTERRUPTS ARE STILL ON
609 * the syscall may perform a thread_set_syscall_return
1c79356b
A
610 * followed by a thread_exception_return, ending up
611 * at thread_syscall_return below, with SS_R3 having
612 * been set up already
55e303ae
A
613 *
614 * When we are here, r31 should point to the current thread,
1c79356b 615 * r30 should point to the current pcb
55e303ae
A
616 * r3 contains value that we're going to return to the user
617 * which has already been stored back into the save area
1c79356b 618 */
55e303ae 619
1c79356b 620.L_thread_syscall_ret_check_ast:
55e303ae 621 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 622 mfmsr r12 ; Get the current MSR
55e303ae
A
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
9bccf70c 625 mtmsr r12 ; Turn interruptions off
1c79356b 626
91447636
A
627 mfsprg r10,1 ; Get the current activation
628 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
1c79356b
A
629
630/* Check to see if there's an outstanding AST */
631
91447636 632 lwz r4,PP_PENDING_AST(r10)
9bccf70c 633 cmpi cr0,r4, 0 ; Any pending asts?
55e303ae 634 beq++ cr0,.L_syscall_no_ast ; Nope...
1c79356b
A
635
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
639 */
640
641#if DEBUG
642/* debug assert - make sure that we're not returning to kernel */
55e303ae 643 lwz r3,savesrr1+4(r30)
1c79356b 644 andi. r3,r3,MASK(MSR_PR)
55e303ae 645 bne++ scrnotkern ; returning to user level, check
1c79356b 646
9bccf70c
A
647 lis r0,hi16(Choke) ; Choke code
648 ori r0,r0,lo16(Choke) ; and the rest
649 li r3,failContext ; Bad state code
650 sc ; System ABEND
0b4e3aa0 651
9bccf70c 652scrnotkern:
1c79356b
A
653#endif /* DEBUG */
654
91447636 655 lis r3,hi16(AST_ALL) ; Set ast flags
9bccf70c 656 li r4,1 ; Set interrupt allowed
91447636 657 ori r3,r3,lo16(AST_ALL)
9bccf70c
A
658 bl EXT(ast_taken) ; Process the pending ast
659 b .L_thread_syscall_ret_check_ast ; Go see if there was another...
1c79356b 660
55e303ae
A
661.L_mach_invalid_ret:
662/*
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
667 */
91447636
A
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
55e303ae 673 lis r28,hi16(EXT(mach_trap_table)) ; Get address of table
91447636 674 add r27,r27,r4 ; for a total of 20x (5 words/entry)
55e303ae
A
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
683
684
1c79356b
A
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).
688 *
689 * In particular for us,
690 * we still have r31 points to the current thread,
691 * r30 points to the current pcb
692 */
693
9bccf70c
A
694 .align 5
695
1c79356b
A
696.L_syscall_no_ast:
697.L_thread_syscall_return:
698
9bccf70c 699 mr r3,r30 ; Get savearea to the correct register for common exit
de355530
A
700
701 lwz r11,SAVflags(r30) ; Get the flags
702 lwz r5,THREAD_KERNEL_STACK(r31) ; Get the base pointer to the stack
55e303ae 703 lwz r4,SAVprev+4(r30) ; Get the previous save area
de355530 704 rlwinm r11,r11,0,15,13 ; Clear the syscall flag
55e303ae 705 mfsprg r8,1 ; Now find the current activation
9bccf70c 706 addi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Reset to empty
55e303ae 707 stw r11,SAVflags(r30) ; Stick back the flags
9bccf70c 708 stw r5,ACT_MACT_KSP(r8) ; Save the empty stack pointer
55e303ae 709 stw r4,ACT_MACT_PCB(r8) ; Save previous save area
9bccf70c 710 b chkfac ; Go end it all...
1c79356b 711
1c79356b
A
712/*
713 * thread_exception_return()
714 *
715 * Return to user mode directly from within a system call.
716 */
717
0b4e3aa0
A
718 .align 5
719 .globl EXT(thread_bootstrap_return)
720LEXT(thread_bootstrap_return) ; NOTE: THIS IS GOING AWAY IN A FEW DAYS....
721
722 .globl EXT(thread_exception_return)
723LEXT(thread_exception_return) ; Directly return to user mode
1c79356b
A
724
725.L_thread_exc_ret_check_ast:
55e303ae 726 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 727 mfmsr r3 ; Get the MSR
55e303ae
A
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
9bccf70c 730 mtmsr r3 ; Disable interrupts
1c79356b
A
731
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.
736*/
737
91447636
A
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)
1c79356b 741 cmpi cr0,r4, 0
55e303ae 742 beq+ cr0,.L_exc_ret_no_ast
1c79356b 743
9bccf70c
A
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
747 */
de355530 748
91447636 749 lis r3,hi16(AST_ALL)
de355530 750 li r4,1
91447636 751 ori r3,r3,lo16(AST_ALL)
1c79356b
A
752
753 bl EXT(ast_taken)
9bccf70c 754 b .L_thread_exc_ret_check_ast ; check for a second AST (rare)
1c79356b
A
755
756/* arriving here, interrupts should be disabled */
757/* Get the active thread's PCB pointer to restore regs
758 */
759.L_exc_ret_no_ast:
760
9bccf70c 761 mfsprg r30,1 ; Get the currrent activation
91447636 762 mr r31,r30
9bccf70c 763
1c79356b 764 lwz r30,ACT_MACT_PCB(r30)
9bccf70c
A
765 mr. r30,r30 ; Is there any context yet?
766 beq- makeDummyCtx ; No, hack one up...
1c79356b
A
767#if DEBUG
768/*
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
771 */
772
55e303ae 773 lwz r3,savesrr1+4(r30)
1c79356b 774 andi. r3,r3,MASK(MSR_PR)
9bccf70c 775 bne+ ret_user2 ; We are ok...
1c79356b 776
9bccf70c
A
777 lis r0,hi16(Choke) ; Choke code
778 ori r0,r0,lo16(Choke) ; and the rest
779 li r3,failContext ; Bad state code
780 sc ; System ABEND
0b4e3aa0 781
1c79356b
A
782ret_user2:
783#endif /* DEBUG */
784
9bccf70c 785/* If the system call flag isn't set, then we came from a trap,
1c79356b
A
786 * so warp into the return_from_trap (thread_return) routine,
787 * which takes PCB pointer in R3, not in r30!
788 */
9bccf70c 789 lwz r0,SAVflags(r30) ; Grab the savearea flags
de355530 790 andis. r0,r0,SAVsyscall>>16 ; Are we returning from a syscall?
55e303ae
A
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...
9bccf70c 793 b .L_thread_syscall_return ; Join up with the system call return...
1c79356b
A
794
795;
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...
9bccf70c
A
799;
800
801 .align 5
1c79356b
A
802
803makeDummyCtx:
9bccf70c
A
804 bl EXT(save_get) ; Get a save_area
805 li r4,SAVgeneral ; Get the general context type
806 li r0,0 ; Get a 0
807 stb r4,SAVflags+2(r3) ; Set type
55e303ae 808 addi r2,r3,savefpscr+4 ; Point past what we are clearing
9bccf70c 809 mr r4,r3 ; Save the start
1c79356b 810
9bccf70c
A
811cleardummy: stw r0,0(r4) ; Clear stuff
812 addi r4,r4,4 ; Next word
813 cmplw r4,r2 ; Still some more?
814 blt+ cleardummy ; Yeah...
1c79356b
A
815
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
55e303ae 818 stw r2,savesrr1+4(r3) ; Set the default user MSR
1c79356b 819
9bccf70c 820 b thread_return ; Go let em try to execute, hah!
1c79356b
A
821
822/*
823 * ihandler(type)
824 *
825 * ENTRY: VM switched ON
826 * Interrupts OFF
827 * R3 contains exception code
828 * R4 points to the saved context (virtual address)
829 * Everything is saved in savearea
830 *
831 */
832
0b4e3aa0
A
833 .align 5
834 .globl EXT(ihandler)
9bccf70c 835LEXT(ihandler) ; Interrupt handler */
1c79356b
A
836
837/*
838 * get the value of istackptr, if it's zero then we're already on the
9bccf70c 839 * interrupt stack.
1c79356b
A
840 */
841
55e303ae 842 lwz r10,savesrr1+4(r4) ; Get SRR1
9bccf70c 843 lwz r7,savevrsave(r4) ; Get the VRSAVE register
91447636
A
844 mfsprg r13,1 ; Get the current activation
845 lwz r25,ACT_PER_PROC(r13) ; Get the per_proc block
9bccf70c 846 li r14,0 ; Zero this for now
91447636 847 rlwinm. r16,r10,0,MSR_VEC_BIT,MSR_VEC_BIT ; Was vector on?
9bccf70c 848 lwz r1,PP_ISTACKPTR(r25) ; Get the interrupt stack
55e303ae 849 li r16,0 ; Zero this for now
9bccf70c
A
850
851 beq+ ivecoff ; Vector off, do not save vrsave...
852 stw r7,liveVRS(r25) ; Set the live value
853
de355530 854ivecoff: li r0,0 ; Get a constant 0
55e303ae
A
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
91447636 858 mr r16,r13
9bccf70c 859 lwz r14,ACT_MACT_PCB(r13) ; Now point to the PCB
55e303ae
A
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
9bccf70c 862 stw r13,SAVact(r4) ; Point the savearea at its activation
9bccf70c 863 stw r4,ACT_MACT_PCB(r13) ; Point to our savearea
55e303ae
A
864 beq cr2,ifromk
865 stw r4,ACT_MACT_UPCB(r13) ; Store user savearea
9bccf70c 866
55e303ae 867ifromk: bne .L_istackfree ; Nope...
1c79356b
A
868
869/* We're already on the interrupt stack, get back the old
870 * stack pointer and make room for a frame
871 */
872
9bccf70c
A
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...
0b4e3aa0 879
9bccf70c 880 lwz r5,PP_DEBSTACK_TOP_SS(r25) ; Pick up debug stack top
0b4e3aa0 881 subi r5,r5,KERNEL_STACK_SIZE-FM_SIZE ; Adjust to start of stack
9bccf70c 882 sub r5,r1,r5 ; Get displacement into debug stack
0b4e3aa0 883 cmplwi cr2,r5,KERNEL_STACK_SIZE-FM_SIZE ; Check if we are on debug stack
55e303ae 884 blt+ cr2,ihsetback ; Yeah, that is ok too...
0b4e3aa0 885
9bccf70c
A
886 lis r0,hi16(Choke) ; Choke code
887 ori r0,r0,lo16(Choke) ; and the rest
888 li r3,failStack ; Bad stack code
889 sc ; System ABEND
0b4e3aa0 890
55e303ae
A
891intUnalignedStk:
892 lis r0,hi16(Choke) ; Choke code
893 ori r0,r0,lo16(Choke) ; and the rest
894 li r3,failUnalignedStk ; Unaligned stack code
895 sc ; System ABEND
896
0b4e3aa0
A
897 .align 5
898
1c79356b 899.L_istackfree:
55e303ae
A
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...
9bccf70c 903 stw r0,PP_ISTACKPTR(r25) ; Mark the stack in use
55e303ae 904 oris r10,r10,hi16(SAVrststk) ; Indicate we reset stack when we return from this one
9bccf70c 905 stw r10,SAVflags(r4) ; Stick it back
1c79356b 906
9bccf70c
A
907/*
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.
913 */
1c79356b 914
9bccf70c
A
915ihsetback: subi r1,r1,FM_SIZE ; Make a new frame
916 stw r9,FM_BACKPTR(r1) ; Point back to previous stackptr
1c79356b
A
917
918#if VERIFYSAVE
9bccf70c
A
919 beq- cr1,ihbootnover ; (TEST/DEBUG)
920 bl versave ; (TEST/DEBUG)
921ihbootnover: ; (TEST/DEBUG)
1c79356b
A
922#endif
923
924#if 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
928 */
55e303ae 929 lwz r5,savesrr0+4(r4) ; Get interrupt address
9bccf70c
A
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
1c79356b
A
932#endif /* DEBUG */
933
91447636
A
934 mr r31,r3
935 mr r30,r4
936
937 lwz r3,SAVtime+4(r4)
938 addi r4,r13,SYSTEM_TIMER
939 bl EXT(timer_event)
940
941 mr r3,r31
942 mr r4,r30
943 lwz r5,savedsisr(r30) ; Get the DSISR
944 lwz r6,savedar+4(r30) ; Get the DAR
1c79356b
A
945
946 bl EXT(interrupt)
947
948
949/* interrupt() returns a pointer to the saved state in r3
950 *
951 * Ok, back from C. Disable interrupts while we restore things
952 */
953 .globl EXT(ihandler_ret)
954
9bccf70c 955LEXT(ihandler_ret) ; Marks our return point from debugger entry
1c79356b 956
55e303ae 957 lis r10,hi16(MASK(MSR_VEC)) ; Get the vector enable
9bccf70c 958 mfmsr r0 ; Get our MSR
55e303ae
A
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
9bccf70c 961 mtmsr r0 ; Make sure interrupts are disabled
91447636
A
962 mfsprg r8,1 ; Get the current activation
963 lwz r10,ACT_PER_PROC(r8) ; Get the per_proc block
1c79356b 964
9bccf70c 965 lwz r7,SAVflags(r3) ; Pick up the flags
55e303ae 966 lwz r9,SAVprev+4(r3) ; Get previous save area
9bccf70c 967 cmplwi cr1,r8,0 ; Are we still initializing?
55e303ae 968 lwz r12,savesrr1+4(r3) ; Get the MSR we will load on return
55e303ae 969 andis. r11,r7,hi16(SAVrststk) ; Is this the first on the stack?
9bccf70c 970 stw r9,ACT_MACT_PCB(r8) ; Point to previous context savearea
55e303ae 971 mr r4,r3 ; Move the savearea pointer
9bccf70c 972 beq .L_no_int_ast2 ; Get going if not the top-o-stack...
1c79356b
A
973
974
975/* We're the last frame on the stack. Restore istackptr to empty state.
976 *
977 * Check for ASTs if one of the below is true:
978 * returning to user mode
979 * returning to a kloaded server
980 */
9bccf70c
A
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
55e303ae 984 lwz r3,ACT_PREEMPT_CNT(r8) ; Get preemption level
9bccf70c
A
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
91447636 989 lwz r11,PP_PENDING_AST(r10) ; Get the pending AST mask
9bccf70c
A
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...
1c79356b
A
994 b .L_call_thandler
995
996.L_kernel_int_ast:
9bccf70c
A
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...
1c79356b
A
1000
1001/*
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.
1007 */
1008
9bccf70c
A
1009.L_call_thandler:
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...
1c79356b
A
1012
1013.L_no_int_ast:
9bccf70c
A
1014 mr r3,r4 ; Get into the right register for common code
1015
1c79356b 1016.L_no_int_ast2:
9bccf70c
A
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
55e303ae 1020 beq-- cr1,chkfac ; Jump away if we are in init...
9bccf70c
A
1021
1022 lwz r4,ACT_MACT_PCB(r8) ; Get the new level marker
1c79356b
A
1023
1024
1025;
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
1028; exited.
1029;
1030; It also enables the facility if its context is live
1031; Requires:
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
1036;
9bccf70c
A
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.
1040;
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.
1044;
1c79356b 1045
55e303ae 1046chkfac: lwz r29,savesrr1+4(r3) ; Get the current MSR
de355530 1047 mr. r28,r8 ; Are we still in boot?
55e303ae
A
1048 mr r31,r10 ; Move per_proc address
1049 mr r30,r4 ; Preserve new level
9bccf70c 1050 mr r27,r3 ; Save the old level
55e303ae 1051 beq-- chkenax ; Yeah, skip it all...
1c79356b 1052
9bccf70c
A
1053 rlwinm. r0,r29,0,MSR_PR_BIT,MSR_PR_BIT ; Are we going into user state?
1054
9bccf70c
A
1055 lwz r20,curctx(r28) ; Get our current context
1056 lwz r26,deferctx(r28) ; Get any deferred context switch
55e303ae 1057 li r0,1 ; Get set to hold off quickfret
9bccf70c
A
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
de355530 1063 lhz r19,PP_CPU_NUMBER(r31) ; Get our CPU number
55e303ae
A
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...
1c79356b 1067
9bccf70c
A
1068;
1069; First clean up any live context we are returning from
1070;
1071
1072 lwz r22,FPUcpu(r20) ; Get CPU this context was last dispatched on
1073
1074 stw r19,FPUcpu(r20) ; Claim context for us
1075
1076 eieio ; Make sure this gets out before owner clear
1077
91447636
A
1078#if ppeSize != 16
1079#error per_proc_entry is not 16bytes in size
55e303ae
A
1080#endif
1081
91447636
A
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
0b4e3aa0 1088
9bccf70c 1089fpuinvothr: lwarx r23,r24,r22 ; Get the owner
55e303ae
A
1090
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...
1098
1099 isync
0b4e3aa0 1100
9bccf70c
A
1101;
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.
1104;
0b4e3aa0 1105
9bccf70c
A
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?
55e303ae 1109 beq++ fpusetlvl ; No, see if we need to enable...
9bccf70c
A
1110
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...
1114
55e303ae 1115 lwz r24,SAVprev+4(r22) ; Pick up the previous area
9bccf70c
A
1116 li r21,0 ; Assume we popped all the way out
1117 mr. r24,r24 ; Any more context stacked?
55e303ae 1118 beq-- fpuonlyone ; Nope...
9bccf70c
A
1119 lwz r21,SAVlevel(r24) ; Get the level associated with save
1120
1121fpuonlyone: stw r24,FPUsave(r20) ; Dequeue this savearea
1122
1123 rlwinm r3,r22,0,0,19 ; Find main savearea header
55e303ae
A
1124
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)
9bccf70c 1131 xor r3,r22,r3 ; Convert to physical
55e303ae
A
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)
9bccf70c
A
1134
1135#if FPVECDBG
1136 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1137 li r2,0x3301 ; (TEST/DEBUG)
1138 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1139 sc ; (TEST/DEBUG)
1140#endif
1141
9bccf70c
A
1142fpusetlvl: stw r21,FPUlevel(r20) ; Save the level
1143
1c79356b 1144;
9bccf70c
A
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.
1c79356b 1149;
0b4e3aa0 1150
55e303ae 1151fpuchkena: bt-- cr2_eq,fpuhasdfrd ; Skip if deferred, R26 already set up...
9bccf70c
A
1152 mr r26,r20 ; Use the non-deferred value
1153
55e303ae
A
1154fpuhasdfrd:
1155#if 0
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)
1163
1164fpulvl0: 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)
1171
1172fpulvl0b: mr. r21,r21 ; (TEST/DEBUG) Is there a forward chain?
1173 beq++ fpunusrstt ; (TEST/DEBUG) Nope...
1174 BREAKPOINT_TRAP ; (TEST/DEBUG)
1175
1176fpunusrstt: ; (TEST/DEBUG)
1177#endif
1178
1179 lwz r21,FPUowner(r31) ; Get the ID of the live context
9bccf70c 1180 lwz r23,FPUlevel(r26) ; Get the level ID
de355530 1181 lwz r24,FPUcpu(r26) ; Get the CPU that the context was last dispatched on
55e303ae 1182 cmplw cr3,r26,r21 ; Do we have the live context?
9bccf70c 1183 cmplw r30,r23 ; Are we about to launch the live level?
55e303ae 1184 bne-- cr3,chkvec ; No, can not possibly enable...
9bccf70c 1185 cmplw cr1,r19,r24 ; Was facility used on this processor last?
55e303ae
A
1186 bne-- chkvec ; No, not live...
1187 bne-- cr1,chkvec ; No, wrong cpu, have to enable later....
9bccf70c
A
1188
1189 lwz r24,FPUsave(r26) ; Get the first savearea
1190 mr. r24,r24 ; Any savearea?
55e303ae 1191 beq++ fpuena ; Nope...
9bccf70c 1192 lwz r25,SAVlevel(r24) ; Get the level of savearea
55e303ae 1193 lwz r0,SAVprev+4(r24) ; Get the previous
9bccf70c 1194 cmplw r30,r25 ; Is savearea for the level we are launching?
55e303ae 1195 bne++ fpuena ; No, just go enable...
9bccf70c
A
1196
1197 stw r0,FPUsave(r26) ; Pop the chain
1198
1199 rlwinm r3,r24,0,0,19 ; Find main savearea header
55e303ae
A
1200
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)
9bccf70c 1207 xor r3,r24,r3 ; Convert to physical
55e303ae
A
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)
1c79356b
A
1210
1211#if FPVECDBG
9bccf70c
A
1212 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1213 li r2,0x3302 ; (TEST/DEBUG)
1214 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1215 sc ; (TEST/DEBUG)
1216#endif
1c79356b 1217
9bccf70c 1218fpuena: ori r29,r29,lo16(MASK(MSR_FP)) ; Enable facility
1c79356b 1219
9bccf70c 1220chkvec:
1c79356b 1221
9bccf70c
A
1222 lwz r21,VMXlevel(r20) ; Get the facility level
1223
1224 cmplw r27,r21 ; Are we returning from the active level?
1225 bne+ vmxchkena ; Nope...
1226
1227
1228;
1229; First clean up any live context we are returning from
1230;
1c79356b 1231
9bccf70c
A
1232 lwz r22,VMXcpu(r20) ; Get CPU this context was last dispatched on
1233
1234 stw r19,VMXcpu(r20) ; Claim context for us
1235
1236 eieio ; Make sure this gets out before owner clear
1237
91447636
A
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
9bccf70c
A
1244
1245vmxinvothr: lwarx r23,r24,r22 ; Get the owner
55e303ae
A
1246
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...
1254
1255 isync
0b4e3aa0 1256
9bccf70c
A
1257;
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.
1260;
1261
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?
55e303ae 1265 beq++ vmxsetlvl ; No, see if we need to enable...
9bccf70c
A
1266
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...
1270
55e303ae 1271 lwz r24,SAVprev+4(r22) ; Pick up the previous area
9bccf70c
A
1272 li r21,0 ; Assume we popped all the way out
1273 mr. r24,r24 ; Any more context?
55e303ae 1274 beq-- vmxonlyone ; Nope...
9bccf70c
A
1275 lwz r21,SAVlevel(r24) ; Get the level associated with save
1276
1277vmxonlyone: stw r24,VMXsave(r20) ; Dequeue this savearea
1278
1279 rlwinm r3,r22,0,0,19 ; Find main savearea header
55e303ae
A
1280
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)
ab86ba33 1287 xor r3,r22,r3 ; Convert to physical
55e303ae
A
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)
9bccf70c
A
1290
1291#if FPVECDBG
1292 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1293 li r2,0x3401 ; (TEST/DEBUG)
1294 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1295 sc ; (TEST/DEBUG)
1296#endif
9bccf70c
A
1297
1298vmxsetlvl: stw r21,VMXlevel(r20) ; Save the level
0b4e3aa0 1299
9bccf70c
A
1300;
1301; Here we check if we are at the right level
1302;
1303
1304vmxchkena: 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
55e303ae 1308 bne-- setena ; No, can not possibly enable...
9bccf70c
A
1309 cmplw r30,r23 ; Are we about to launch the live level?
1310 cmplw cr1,r19,r24 ; Was facility used on this processor last?
55e303ae
A
1311 bne-- setena ; No, not live...
1312 bne-- cr1,setena ; No, wrong cpu, have to enable later....
9bccf70c
A
1313
1314 lwz r24,VMXsave(r26) ; Get the first savearea
1315 mr. r24,r24 ; Any savearea?
55e303ae 1316 beq++ vmxena ; Nope...
9bccf70c 1317 lwz r25,SAVlevel(r24) ; Get the level of savearea
55e303ae 1318 lwz r0,SAVprev+4(r24) ; Get the previous
9bccf70c 1319 cmplw r30,r25 ; Is savearea for the level we are launching?
55e303ae 1320 bne++ vmxena ; No, just go enable...
9bccf70c
A
1321
1322 stw r0,VMXsave(r26) ; Pop the chain
1323
1324 rlwinm r3,r24,0,0,19 ; Find main savearea header
55e303ae
A
1325
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)
9bccf70c 1332 xor r3,r24,r3 ; Convert to physical
55e303ae
A
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)
9bccf70c
A
1335
1336#if FPVECDBG
1337 lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
1338 li r2,0x3402 ; (TEST/DEBUG)
1339 oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
1340 sc ; (TEST/DEBUG)
1341#endif
1342
9bccf70c 1343vmxena: oris r29,r29,hi16(MASK(MSR_VEC)) ; Enable facility
0b4e3aa0 1344
91447636 1345setena: lwz r18,umwSpace(r28) ; Get the space ID in case we are launching user
55e303ae
A
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
9bccf70c 1348 crmove cr7_eq,cr0_eq ; Remember if we are going to user state
de355530 1349 rlwimi. r20,r29,(((31-floatCngbit)+(MSR_FP_BIT+1))&31),floatCngbit,floatCngbit ; Set flag if we enabled floats
55e303ae
A
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
91447636 1354 oris r18,r18,hi16(umwSwitchAway) ; Set the switch-away bit in case we go to user
55e303ae 1355
9bccf70c
A
1356 beq setenaa ; Neither float nor vector turned on....
1357
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
1364
1365setenaa: mfdec r24 ; Get decrementer
1366 bf+ cr2_eq,nodefer ; No deferred to switch to...
1367
1368 li r20,0 ; Clear this
1369 stw r26,curctx(r28) ; Make the facility context current
1370 stw r20,deferctx(r28) ; Clear deferred context
1371
1372nodefer: 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
55e303ae 1375 ble- chkifuser ; We have popped or are just about to...
9bccf70c
A
1376
1377segtb: 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?
55e303ae
A
1382 beq++ chkifuser ; No time set....
1383 bne-- cr1,segtb ; Timebase ticked, get them again...
9bccf70c
A
1384
1385 subfc r6,r21,r23 ; Subtract current from qact time
1386 li r0,0 ; Make a 0
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
55e303ae 1391 bne chkifuser ; If high order is non-zero, this is too big for a decrementer
9bccf70c 1392 cmplw r13,r24 ; Is this earlier than the decrementer? (logical compare takes care of high bit on)
55e303ae 1393 bge++ chkifuser ; No, do not reset decrementer...
1c79356b 1394
9bccf70c 1395 mtdec r13 ; Set our value
1c79356b 1396
91447636
A
1397chkifuser: addi r4,r28,SYSTEM_TIMER
1398 mftb r3
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
1402
1403chkifuser1: bl EXT(timer_event)
55e303ae 1404
9bccf70c 1405chkenax:
1c79356b 1406
1c79356b 1407#if DEBUG
9bccf70c 1408 lwz r20,SAVact(r27) ; (TEST/DEBUG) Make sure our restore
55e303ae 1409 mfsprg r21, 1 ; (TEST/DEBUG) with the current act.
9bccf70c 1410 cmpwi r21,0 ; (TEST/DEBUG)
55e303ae 1411 beq-- yeswereok ; (TEST/DEBUG)
9bccf70c 1412 cmplw r21,r20 ; (TEST/DEBUG)
55e303ae 1413 beq++ yeswereok ; (TEST/DEBUG)
9bccf70c
A
1414
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
1c79356b
A
1420
1421yeswereok:
1422#endif
1423
55e303ae 1424 mr r3,r27 ; Pass savearea back
9bccf70c 1425 b EXT(exception_exit) ; We are all done now...
1c79356b
A
1426
1427
1428
55e303ae
A
1429;
1430; Null PPC call - performance testing, does absolutely nothing
1431;
1432
1433 .align 5
1434
1435 .globl EXT(ppcNull)
1436
1437LEXT(ppcNull)
1438
1439 li r3,-1 ; Make sure we test no asts
1440 blr
1441
1442
1443;
1444; Instrumented null PPC call - performance testing, does absolutely nothing
1445; Forces various timestamps to be returned.
1446;
1447
1448 .align 5
1449
1450 .globl EXT(ppcNullinst)
1451
1452LEXT(ppcNullinst)
1453
1454 li r3,-1 ; Make sure we test no asts
1455 blr
1456
1457
1c79356b
A
1458/*
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.
1463 *
1464 * Registers when we get here:
1465 *
1466 * r0 = syscall number
1467 * r4 = savearea/pcb
1468 * r13 = activation
1469 * r14 = previous savearea (if any)
1470 * r16 = thread
1471 * r25 = per_proc
1472 */
1473
0b4e3aa0
A
1474 .align 5
1475
55e303ae
A
1476fastpath: 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...
1c79356b
A
1480
1481/*
1482 * void cthread_set_self(cproc_t p)
1483 *
91447636
A
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.
1c79356b
A
1488 *
1489 * This op is invoked as follows:
1490 * li r0, CthreadSetSelfNumber // load the fast-trap number
1491 * sc // invoke fast-trap
1492 * blr
1c79356b
A
1493 */
1494
1495CthreadSetSelfNumber:
91447636
A
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 */
1501 stw r5,UAW+4(r25)
1c79356b
A
1502
1503
1504 .globl EXT(fastexit)
1505EXT(fastexit):
55e303ae
A
1506fastexutl: mr r3,r4 ; Pass back savearea
1507 b EXT(exception_exit) ; Go back to the caller...
1c79356b
A
1508
1509
1510/*
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.
1515 */
1516
0b4e3aa0
A
1517 .align 5
1518
1c79356b 1519checkassist:
0b4e3aa0 1520 lwz r0,saveexception(r4) ; Get the exception code
55e303ae 1521 lwz r23,savesrr1+4(r4) ; Get the interrupted MSR
0b4e3aa0
A
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
55e303ae 1526 lwz r27,savesrr0+4(r4) ; Get trapped address
0b4e3aa0
A
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...
1c79356b
A
1534
1535;
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.
1541;
1542
0b4e3aa0
A
1543 .align 5
1544
1c79356b
A
1545exitFromVM: mr r30,r4 ; Get the savearea
1546 mr r3,r13 ; Get the activation
1547
1548 b EXT(vmm_exit) ; Do it to it
1549
1550 .align 5
1551 .globl EXT(retFromVM)
1552
1553LEXT(retFromVM)
91447636
A
1554 mfsprg r10,1 ; Get the current activation
1555 lwz r10,ACT_PER_PROC(r10) ; Get the per_proc block
1c79356b 1556 mr r8,r3 ; Get the activation
55e303ae 1557 lwz r4,SAVprev+4(r30) ; Pick up the previous savearea
1c79356b
A
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
91447636 1561 mr r1,r8
1c79356b
A
1562 stw r11,SAVflags(r3) ; Save back the flags (with reset stack cleared)
1563
1564 stw r4,ACT_MACT_PCB(r8) ; Point to the previous savearea (or 0 if none)
1565
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...
1570
1571
0b4e3aa0
A
1572;
1573; chandler (note: not a candle maker or tallow merchant)
1574;
1575; Here is the system choke handler. This is where the system goes
1576; to die.
1577;
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.
1583;
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.
1586;
1c79356b 1587
0b4e3aa0
A
1588
1589 .align 5
1590 .globl EXT(chandler)
55e303ae 1591LEXT(chandler) ; Choke handler
0b4e3aa0 1592
55e303ae 1593 li r31,0 ; Get a 0
91447636
A
1594 mfsprg r25,1 ; Get the current activation
1595 lwz r25,ACT_PER_PROC(r25) ; Get the per_proc block
55e303ae 1596 stw r31,traceMask(0) ; Force tracing off right now
0b4e3aa0
A
1597
1598
0b4e3aa0
A
1599
1600 lwz r1,PP_DEBSTACKPTR(r25) ; Get debug stack pointer
1601 cmpwi r1,-1 ; Are we already choking?
1602 bne chokefirst ; Nope...
1c79356b 1603
0b4e3aa0
A
1604chokespin: 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...
1c79356b 1611
0b4e3aa0
A
1612chokefirst: li r0,-1 ; Set choke value
1613 mr. r1,r1 ; See if we are on debug stack yet
55e303ae 1614 lwz r10,saver1+4(r4) ;
0b4e3aa0
A
1615 stw r0,PP_DEBSTACKPTR(r25) ; Show we are choking
1616 bne chokestart ; We are not on the debug stack yet...
1c79356b 1617
0b4e3aa0
A
1618 lwz r2,PP_DEBSTACK_TOP_SS(r25) ; Get debug stack top
1619 sub r11,r2,r10 ; Get stack depth
1c79356b 1620
0b4e3aa0
A
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...
1c79356b 1623
0b4e3aa0
A
1624 subi r1,r10,FM_REDZONE ; Make a red zone
1625
1626chokestart: li r0,0 ; Get a zero
1627 stw r0,FM_BACKPTR(r1) ; We now have terminated the back chain
1628
1629 bl EXT(SysChoked) ; Call the "C" phase of this
1630 b chokespin ; Should not be here so just go spin...
1631
1c79356b
A
1632
1633#if VERIFYSAVE
1634;
1635; Savearea chain verification
1636;
1637
1638versave:
55e303ae
A
1639#if 0
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)
91447636
A
1645 mfsprg r20,1 ; Get the current activation
1646 lwz r20,ACT_PER_PROC(r20) ; Get the per_proc block
55e303ae
A
1647 lwz r21,pfAvailable(r20) ; (TEST/DEBUG)
1648 mr. r21,r21 ; (TEST/DEBUG)
1649 bnelr+ ; (TEST/DEBUG)
1650
1651 stw r22,0(r22) ; (TEST/DEBUG) Lock out more checks
1652 BREAKPOINT_TRAP ; (TEST/DEBUG) Get into debugger
1653#endif
1654
1655#if 0
1656 ;; This code is broken and migration will make the matter even worse
1c79356b 1657;
9bccf70c 1658; Make sure that all savearea chains have the right type on them
1c79356b
A
1659;
1660
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)
9bccf70c 1668 mr. r26,r26 ; (TEST/DEBUG) Have we locked the test out?
1c79356b 1669 lwz r28,psthreads(r28) ; (TEST/DEBUG)
9bccf70c
A
1670 mflr r31 ; (TEST/DEBUG) Save return
1671 bnelr- ; (TEST/DEBUG) Test already triggered, skip...
1672 b fckgo ; (TEST/DEBUG) Join up...
1c79356b 1673
9bccf70c
A
1674fcknext: mr. r27,r27 ; (TEST/DEBUG) Any more threads?
1675 bne+ fckxxx ; (TEST/DEBUG) Yes...
1676
1677 mtlr r31 ; (TEST/DEBUG) Restore return
1678 blr ; (TEST/DEBUG) Leave...
1c79356b 1679
9bccf70c 1680fckxxx: lwz r28,THREAD_PSTHRN(r28) ; (TEST/DEBUG) Get next thread
1c79356b 1681
9bccf70c
A
1682fckgo: 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
1c79356b 1687
9bccf70c
A
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
1c79356b 1691
9bccf70c
A
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
1c79356b 1695
9bccf70c
A
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...
1c79356b 1699
9bccf70c
A
1700 li r22,kVmmMaxContextsPerThread ; (TEST/DEBUG) Get the number of control blocks
1701 subi r29,r29,vmmCEntrySize ; (TEST/DEBUG) Get running start
1702
1703fcknvmm: 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...
1707
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...
1711
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
1715
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...
1720
1721versavetype:
1722 mr. r20,r20 ; (TEST/DEBUG) Chain done?
1723 beqlr- ; (TEST/DEBUG) Yes...
1724
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...
1729
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
1734
55e303ae 1735versvok: lwz r20,SAVprev+4(r20) ; (TEST/DEBUG) Get the previous one
9bccf70c 1736 b versavetype ; (TEST/DEBUG) Go check its type...
1c79356b
A
1737#endif
1738
de355530 1739
1c79356b 1740#endif