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