]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/trap_subr64.S
Upgrade Unbound to 1.7.0. More to follow.
[FreeBSD/FreeBSD.git] / sys / powerpc / aim / trap_subr64.S
1 /* $FreeBSD$ */
2 /* $NetBSD: trap_subr.S,v 1.20 2002/04/22 23:20:08 kleink Exp $ */
3
4 /*-
5  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6  * Copyright (C) 1995, 1996 TooLs GmbH.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * NOTICE: This is not a standalone file.  to use it, #include it in
37  * your port's locore.S, like so:
38  *
39  *      #include <powerpc/aim/trap_subr.S>
40  */
41
42 /* Locate the per-CPU data structure */
43 #define GET_CPUINFO(r)  \
44         mfsprg0  r
45 #define GET_TOCBASE(r)  \
46         lis     r,DMAP_BASE_ADDRESS@highesta;   /* To real-mode alias/dmap */ \
47         sldi    r,r,32;                                                 \
48         ori     r,r,TRAP_TOCBASE;       /* Magic address for TOC */     \
49         ld      r,0(r)
50
51 /*
52  * Restore SRs for a pmap
53  *
54  * Requires that r28-r31 be scratch, with r28 initialized to the SLB cache
55  */
56
57 /*
58  * User SRs are loaded through a pointer to the current pmap.
59  */
60 restore_usersrs:
61         GET_CPUINFO(%r28)
62         ld      %r28,PC_USERSLB(%r28)
63         cmpdi   %r28, 0                 /* If user SLB pointer NULL, exit */
64         beqlr
65
66         li      %r29, 0                 /* Set the counter to zero */
67
68         slbia
69         slbmfee %r31,%r29               
70         clrrdi  %r31,%r31,28
71         slbie   %r31
72 1:      ld      %r31, 0(%r28)           /* Load SLB entry pointer */
73         cmpdi   %r31, 0                 /* If NULL, stop */
74         beqlr
75
76         ld      %r30, 0(%r31)           /* Load SLBV */
77         ld      %r31, 8(%r31)           /* Load SLBE */
78         or      %r31, %r31, %r29        /*  Set SLBE slot */
79         slbmte  %r30, %r31              /* Install SLB entry */
80
81         addi    %r28, %r28, 8           /* Advance pointer */
82         addi    %r29, %r29, 1
83         b       1b                      /* Repeat */
84
85 /*
86  * Kernel SRs are loaded directly from the PCPU fields
87  */
88 restore_kernsrs:
89         GET_CPUINFO(%r28)
90         addi    %r28,%r28,PC_KERNSLB
91         ld      %r29,16(%r28)           /* One past USER_SLB_SLOT */
92         cmpdi   %r28,0
93         beqlr                           /* If first kernel entry is invalid,
94                                          * SLBs not in use, so exit early */
95
96         /* Otherwise, set up SLBs */
97         li      %r29, 0                 /* Set the counter to zero */
98
99         slbia
100         slbmfee %r31,%r29               
101         clrrdi  %r31,%r31,28
102         slbie   %r31
103 1:      cmpdi   %r29, USER_SLB_SLOT     /* Skip the user slot */
104         beq-    2f
105
106         ld      %r31, 8(%r28)           /* Load SLBE */
107         cmpdi   %r31, 0                 /* If SLBE is not valid, stop */
108         beqlr
109         ld      %r30, 0(%r28)           /* Load SLBV  */
110         slbmte  %r30, %r31              /* Install SLB entry */
111
112 2:      addi    %r28, %r28, 16          /* Advance pointer */
113         addi    %r29, %r29, 1
114         cmpdi   %r29, 64                /* Repeat if we are not at the end */
115         blt     1b 
116         blr
117
118 /*
119  * FRAME_SETUP assumes:
120  *      SPRG1           SP (1)
121  *      SPRG3           trap type
122  *      savearea        r27-r31,DAR,DSISR   (DAR & DSISR only for DSI traps)
123  *      r28             LR
124  *      r29             CR
125  *      r30             scratch
126  *      r31             scratch
127  *      r1              kernel stack
128  *      SRR0/1          as at start of trap
129  *
130  * NOTE: SPRG1 is never used while the MMU is on, making it safe to reuse
131  * in any real-mode fault handler, including those handling double faults.
132  */
133 #define FRAME_SETUP(savearea)                                           \
134 /* Have to enable translation to allow access of kernel stack: */       \
135         GET_CPUINFO(%r31);                                              \
136         mfsrr0  %r30;                                                   \
137         std     %r30,(savearea+CPUSAVE_SRR0)(%r31);     /* save SRR0 */ \
138         mfsrr1  %r30;                                                   \
139         std     %r30,(savearea+CPUSAVE_SRR1)(%r31);     /* save SRR1 */ \
140         mfsprg1 %r31;                   /* get saved SP (clears SPRG1) */ \
141         mfmsr   %r30;                                                   \
142         ori     %r30,%r30,(PSL_DR|PSL_IR|PSL_RI)@l; /* relocation on */ \
143         mtmsr   %r30;                   /* stack can now be accessed */ \
144         isync;                                                          \
145         stdu    %r31,-(FRAMELEN+288)(%r1); /* save it in the callframe */ \
146         std     %r0, FRAME_0+48(%r1);   /* save r0 in the trapframe */  \
147         std     %r31,FRAME_1+48(%r1);   /* save SP   "      "       */  \
148         std     %r2, FRAME_2+48(%r1);   /* save r2   "      "       */  \
149         std     %r28,FRAME_LR+48(%r1);  /* save LR   "      "       */  \
150         std     %r29,FRAME_CR+48(%r1);  /* save CR   "      "       */  \
151         GET_CPUINFO(%r2);                                               \
152         ld      %r27,(savearea+CPUSAVE_R27)(%r2); /* get saved r27 */   \
153         ld      %r28,(savearea+CPUSAVE_R28)(%r2); /* get saved r28 */   \
154         ld      %r29,(savearea+CPUSAVE_R29)(%r2); /* get saved r29 */   \
155         ld      %r30,(savearea+CPUSAVE_R30)(%r2); /* get saved r30 */   \
156         ld      %r31,(savearea+CPUSAVE_R31)(%r2); /* get saved r31 */   \
157         std     %r3,  FRAME_3+48(%r1);  /* save r3-r31 */               \
158         std     %r4,  FRAME_4+48(%r1);                                  \
159         std     %r5,  FRAME_5+48(%r1);                                  \
160         std     %r6,  FRAME_6+48(%r1);                                  \
161         std     %r7,  FRAME_7+48(%r1);                                  \
162         std     %r8,  FRAME_8+48(%r1);                                  \
163         std     %r9,  FRAME_9+48(%r1);                                  \
164         std     %r10, FRAME_10+48(%r1);                                 \
165         std     %r11, FRAME_11+48(%r1);                                 \
166         std     %r12, FRAME_12+48(%r1);                                 \
167         std     %r13, FRAME_13+48(%r1);                                 \
168         std     %r14, FRAME_14+48(%r1);                                 \
169         std     %r15, FRAME_15+48(%r1);                                 \
170         std     %r16, FRAME_16+48(%r1);                                 \
171         std     %r17, FRAME_17+48(%r1);                                 \
172         std     %r18, FRAME_18+48(%r1);                                 \
173         std     %r19, FRAME_19+48(%r1);                                 \
174         std     %r20, FRAME_20+48(%r1);                                 \
175         std     %r21, FRAME_21+48(%r1);                                 \
176         std     %r22, FRAME_22+48(%r1);                                 \
177         std     %r23, FRAME_23+48(%r1);                                 \
178         std     %r24, FRAME_24+48(%r1);                                 \
179         std     %r25, FRAME_25+48(%r1);                                 \
180         std     %r26, FRAME_26+48(%r1);                                 \
181         std     %r27, FRAME_27+48(%r1);                                 \
182         std     %r28, FRAME_28+48(%r1);                                 \
183         std     %r29, FRAME_29+48(%r1);                                 \
184         std     %r30, FRAME_30+48(%r1);                                 \
185         std     %r31, FRAME_31+48(%r1);                                 \
186         ld      %r28,(savearea+CPUSAVE_AIM_DAR)(%r2);  /* saved DAR */  \
187         ld      %r29,(savearea+CPUSAVE_AIM_DSISR)(%r2);/* saved DSISR */\
188         ld      %r30,(savearea+CPUSAVE_SRR0)(%r2); /* saved SRR0 */     \
189         ld      %r31,(savearea+CPUSAVE_SRR1)(%r2); /* saved SRR1 */     \
190         mfxer   %r3;                                                    \
191         mfctr   %r4;                                                    \
192         mfsprg3 %r5;                                                    \
193         std     %r3, FRAME_XER+48(1);   /* save xer/ctr/exc */          \
194         std     %r4, FRAME_CTR+48(1);                                   \
195         std     %r5, FRAME_EXC+48(1);                                   \
196         std     %r28,FRAME_AIM_DAR+48(1);                               \
197         std     %r29,FRAME_AIM_DSISR+48(1); /* save dsisr/srr0/srr1 */  \
198         std     %r30,FRAME_SRR0+48(1);                                  \
199         std     %r31,FRAME_SRR1+48(1);                                  \
200         ld      %r13,PC_CURTHREAD(%r2)  /* set kernel curthread */
201
202 #define FRAME_LEAVE(savearea)                                           \
203 /* Disable exceptions: */                                               \
204         mfmsr   %r2;                                                    \
205         andi.   %r2,%r2,~PSL_EE@l;                                      \
206         mtmsr   %r2;                                                    \
207         isync;                                                          \
208 /* Now restore regs: */                                                 \
209         ld      %r2,FRAME_SRR0+48(%r1);                                 \
210         ld      %r3,FRAME_SRR1+48(%r1);                                 \
211         ld      %r4,FRAME_CTR+48(%r1);                                  \
212         ld      %r5,FRAME_XER+48(%r1);                                  \
213         ld      %r6,FRAME_LR+48(%r1);                                   \
214         GET_CPUINFO(%r7);                                               \
215         std     %r2,(savearea+CPUSAVE_SRR0)(%r7); /* save SRR0 */       \
216         std     %r3,(savearea+CPUSAVE_SRR1)(%r7); /* save SRR1 */       \
217         ld      %r7,FRAME_CR+48(%r1);                                   \
218         mtctr   %r4;                                                    \
219         mtxer   %r5;                                                    \
220         mtlr    %r6;                                                    \
221         mtsprg2 %r7;                    /* save cr */                   \
222         ld      %r31,FRAME_31+48(%r1);   /* restore r0-31 */            \
223         ld      %r30,FRAME_30+48(%r1);                                  \
224         ld      %r29,FRAME_29+48(%r1);                                  \
225         ld      %r28,FRAME_28+48(%r1);                                  \
226         ld      %r27,FRAME_27+48(%r1);                                  \
227         ld      %r26,FRAME_26+48(%r1);                                  \
228         ld      %r25,FRAME_25+48(%r1);                                  \
229         ld      %r24,FRAME_24+48(%r1);                                  \
230         ld      %r23,FRAME_23+48(%r1);                                  \
231         ld      %r22,FRAME_22+48(%r1);                                  \
232         ld      %r21,FRAME_21+48(%r1);                                  \
233         ld      %r20,FRAME_20+48(%r1);                                  \
234         ld      %r19,FRAME_19+48(%r1);                                  \
235         ld      %r18,FRAME_18+48(%r1);                                  \
236         ld      %r17,FRAME_17+48(%r1);                                  \
237         ld      %r16,FRAME_16+48(%r1);                                  \
238         ld      %r15,FRAME_15+48(%r1);                                  \
239         ld      %r14,FRAME_14+48(%r1);                                  \
240         ld      %r13,FRAME_13+48(%r1);                                  \
241         ld      %r12,FRAME_12+48(%r1);                                  \
242         ld      %r11,FRAME_11+48(%r1);                                  \
243         ld      %r10,FRAME_10+48(%r1);                                  \
244         ld      %r9, FRAME_9+48(%r1);                                   \
245         ld      %r8, FRAME_8+48(%r1);                                   \
246         ld      %r7, FRAME_7+48(%r1);                                   \
247         ld      %r6, FRAME_6+48(%r1);                                   \
248         ld      %r5, FRAME_5+48(%r1);                                   \
249         ld      %r4, FRAME_4+48(%r1);                                   \
250         ld      %r3, FRAME_3+48(%r1);                                   \
251         ld      %r2, FRAME_2+48(%r1);                                   \
252         ld      %r0, FRAME_0+48(%r1);                                   \
253         ld      %r1, FRAME_1+48(%r1);                                   \
254 /* Can't touch %r1 from here on */                                      \
255         mtsprg3 %r3;                    /* save r3 */                   \
256 /* Disable translation, machine check and recoverability: */            \
257         mfmsr   %r3;                                                    \
258         andi.   %r3,%r3,~(PSL_DR|PSL_IR|PSL_ME|PSL_RI)@l;               \
259         mtmsr   %r3;                                                    \
260         isync;                                                          \
261 /* Decide whether we return to user mode: */                            \
262         GET_CPUINFO(%r3);                                               \
263         ld      %r3,(savearea+CPUSAVE_SRR1)(%r3);                       \
264         mtcr    %r3;                                                    \
265         bf      17,1f;                  /* branch if PSL_PR is false */ \
266 /* Restore user SRs */                                                  \
267         GET_CPUINFO(%r3);                                               \
268         std     %r27,(savearea+CPUSAVE_R27)(%r3);                       \
269         std     %r28,(savearea+CPUSAVE_R28)(%r3);                       \
270         std     %r29,(savearea+CPUSAVE_R29)(%r3);                       \
271         std     %r30,(savearea+CPUSAVE_R30)(%r3);                       \
272         std     %r31,(savearea+CPUSAVE_R31)(%r3);                       \
273         mflr    %r27;                   /* preserve LR */               \
274         bl      restore_usersrs;        /* uses r28-r31 */              \
275         mtlr    %r27;                                                   \
276         ld      %r31,(savearea+CPUSAVE_R31)(%r3);                       \
277         ld      %r30,(savearea+CPUSAVE_R30)(%r3);                       \
278         ld      %r29,(savearea+CPUSAVE_R29)(%r3);                       \
279         ld      %r28,(savearea+CPUSAVE_R28)(%r3);                       \
280         ld      %r27,(savearea+CPUSAVE_R27)(%r3);                       \
281 1:      mfsprg2 %r3;                    /* restore cr */                \
282         mtcr    %r3;                                                    \
283         GET_CPUINFO(%r3);                                               \
284         ld      %r3,(savearea+CPUSAVE_SRR0)(%r3); /* restore srr0 */    \
285         mtsrr0  %r3;                                                    \
286         GET_CPUINFO(%r3);                                               \
287         ld      %r3,(savearea+CPUSAVE_SRR1)(%r3); /* restore srr1 */    \
288         mtsrr1  %r3;                                                    \
289         mfsprg3 %r3                     /* restore r3 */
290
291 #ifdef KDTRACE_HOOKS
292         .data
293         .globl  dtrace_invop_calltrap_addr
294         .align  8
295         .type   dtrace_invop_calltrap_addr, @object
296         .size   dtrace_invop_calltrap_addr, 8
297 dtrace_invop_calltrap_addr:
298         .word   0
299         .word   0
300
301         .text
302 #endif
303
304 /*
305  * Processor reset exception handler. These are typically
306  * the first instructions the processor executes after a
307  * software reset. We do this in two bits so that we are
308  * not still hanging around in the trap handling region
309  * once the MMU is turned on.
310  */
311         .globl  CNAME(rstcode), CNAME(rstcodeend), CNAME(cpu_reset_handler)
312         .globl  CNAME(cpu_wakeup_handler)
313         .p2align 3
314 CNAME(rstcode):
315         /*
316          * Check if this is software reset or
317          * processor is waking up from power saving mode
318          * It is software reset when 46:47 = 0b00
319          */
320         mfsrr1  %r9                     /* Load SRR1 into r9 */
321         andis.  %r9,%r9,0x3             /* Logic AND with 46:47 bits */
322         beq     2f                      /* Branch if software reset */
323         bl      1f
324         .llong  cpu_wakeup_handler
325
326         /* It is software reset */
327
328         /* Explicitly set MSR[SF] */
329 2:      mfmsr   %r9
330         li      %r8,1
331         insrdi  %r9,%r8,1,0
332         mtmsrd  %r9
333         isync
334
335         bl      1f
336         .llong  cpu_reset_handler /* Make sure to maintain 8-byte alignment */
337
338 1:      mflr    %r9
339         ld      %r9,0(%r9)
340         mtlr    %r9
341
342         blr
343 CNAME(rstcodeend):
344
345 cpu_reset_handler:
346         GET_TOCBASE(%r2)
347
348         ld      %r1,TOC_REF(tmpstk)(%r2)        /* get new SP */
349         addi    %r1,%r1,(TMPSTKSZ-48)
350
351         bl      CNAME(cpudep_ap_early_bootstrap) /* Set PCPU */
352         nop
353         lis     %r3,1@l
354         bl      CNAME(pmap_cpu_bootstrap)       /* Turn on virtual memory */
355         nop
356         bl      CNAME(cpudep_ap_bootstrap)      /* Set up PCPU and stack */
357         nop
358         mr      %r1,%r3                         /* Use new stack */
359         bl      CNAME(cpudep_ap_setup)
360         nop
361         GET_CPUINFO(%r5)
362         ld      %r3,(PC_RESTORE)(%r5)
363         cmpldi  %cr0,%r3,0
364         beq     %cr0,2f
365         nop
366         li      %r4,1
367         bl      CNAME(longjmp)
368         nop
369 2:
370 #ifdef SMP
371         bl      CNAME(machdep_ap_bootstrap)     /* And away! */
372         nop
373 #endif
374
375         /* Should not be reached */
376 9:
377         b       9b
378
379 cpu_wakeup_handler:
380         GET_TOCBASE(%r2)
381
382         /* Check for false wake up due to badly SRR1 set (eg. by OPAL) */
383         ld      %r3,TOC_REF(can_wakeup)(%r2)
384         ld      %r3,0(%r3)
385         cmpdi   %r3,0
386         beq     cpu_reset_handler
387
388         /* Turn on MMU after return from interrupt */
389         mfsrr1  %r3
390         ori     %r3,%r3,(PSL_IR | PSL_DR)
391         mtsrr1  %r3
392
393         /* Turn on MMU (needed to access PCB) */
394         mfmsr   %r3
395         ori     %r3,%r3,(PSL_IR | PSL_DR)
396         mtmsr   %r3
397         isync
398
399         mfsprg0 %r3
400
401         ld      %r3,PC_CURTHREAD(%r3)   /* Get current thread */
402         ld      %r3,TD_PCB(%r3)         /* Get PCB of current thread */
403         ld      %r12,PCB_CONTEXT(%r3)   /* Load the non-volatile GP regs. */
404         ld      %r13,PCB_CONTEXT+1*8(%r3)
405         ld      %r14,PCB_CONTEXT+2*8(%r3)
406         ld      %r15,PCB_CONTEXT+3*8(%r3)
407         ld      %r16,PCB_CONTEXT+4*8(%r3)
408         ld      %r17,PCB_CONTEXT+5*8(%r3)
409         ld      %r18,PCB_CONTEXT+6*8(%r3)
410         ld      %r19,PCB_CONTEXT+7*8(%r3)
411         ld      %r20,PCB_CONTEXT+8*8(%r3)
412         ld      %r21,PCB_CONTEXT+9*8(%r3)
413         ld      %r22,PCB_CONTEXT+10*8(%r3)
414         ld      %r23,PCB_CONTEXT+11*8(%r3)
415         ld      %r24,PCB_CONTEXT+12*8(%r3)
416         ld      %r25,PCB_CONTEXT+13*8(%r3)
417         ld      %r26,PCB_CONTEXT+14*8(%r3)
418         ld      %r27,PCB_CONTEXT+15*8(%r3)
419         ld      %r28,PCB_CONTEXT+16*8(%r3)
420         ld      %r29,PCB_CONTEXT+17*8(%r3)
421         ld      %r30,PCB_CONTEXT+18*8(%r3)
422         ld      %r31,PCB_CONTEXT+19*8(%r3)
423         ld      %r5,PCB_CR(%r3)         /* Load the condition register */
424         mtcr    %r5
425         ld      %r5,PCB_LR(%r3)         /* Load the link register */
426         mtsrr0  %r5
427         ld      %r1,PCB_SP(%r3)         /* Load the stack pointer */
428         ld      %r2,PCB_TOC(%r3)        /* Load the TOC pointer */
429
430         rfid
431
432 /*
433  * This code gets copied to all the trap vectors
434  * (except ISI/DSI, ALI, and the interrupts). Has to fit in 8 instructions!
435  */
436
437         .globl  CNAME(trapcode),CNAME(trapcodeend)
438         .p2align 3
439 CNAME(trapcode):
440         mtsprg1 %r1                     /* save SP */
441         mflr    %r1                     /* Save the old LR in r1 */
442         mtsprg2 %r1                     /* And then in SPRG2 */
443         ld      %r1,TRAP_GENTRAP(0)
444         mtlr    %r1
445         li      %r1, 0xe0               /* How to get the vector from LR */
446         blrl                            /* Branch to generictrap */
447 CNAME(trapcodeend):
448
449 /*
450  * For SLB misses: do special things for the kernel
451  *
452  * Note: SPRG1 is always safe to overwrite any time the MMU is on, which is
453  * the only time this can be called.
454  */
455         .globl  CNAME(slbtrap),CNAME(slbtrapend)
456         .p2align 3
457 CNAME(slbtrap):
458         mtsprg1 %r1                     /* save SP */
459         GET_CPUINFO(%r1)
460         std     %r2,(PC_SLBSAVE+16)(%r1)
461         mfcr    %r2                     /* save CR */
462         std     %r2,(PC_SLBSAVE+104)(%r1)
463         mfsrr1  %r2                     /* test kernel mode */
464         mtcr    %r2
465         bf      17,2f                   /* branch if PSL_PR is false */
466         /* User mode */
467         ld      %r2,(PC_SLBSAVE+104)(%r1) /* Restore CR */
468         mtcr    %r2
469         ld      %r2,(PC_SLBSAVE+16)(%r1) /* Restore R2 */
470         mflr    %r1                     /* Save the old LR in r1 */
471         mtsprg2 %r1                     /* And then in SPRG2 */
472                                         /* 52 bytes so far */
473         bl      1f
474         .llong  generictrap
475 1:      mflr    %r1
476         ld      %r1,0(%r1)
477         mtlr    %r1
478         li      %r1, 0x80               /* How to get the vector from LR */
479         blrl                            /* Branch to generictrap */
480                                         /* 84 bytes */
481 2:      mflr    %r2                     /* Save the old LR in r2 */
482         nop
483         bl      3f                      /* Begin dance to jump to kern_slbtrap*/
484         .llong  kern_slbtrap
485 3:      mflr    %r1
486         ld      %r1,0(%r1)
487         mtlr    %r1
488         GET_CPUINFO(%r1)
489         blrl                            /* 124 bytes -- 4 to spare */
490 CNAME(slbtrapend):
491
492 kern_slbtrap:
493         std     %r2,(PC_SLBSAVE+136)(%r1) /* old LR */
494         std     %r3,(PC_SLBSAVE+24)(%r1) /* save R3 */
495
496         /* Check if this needs to be handled as a regular trap (userseg miss) */
497         mflr    %r2
498         andi.   %r2,%r2,0xff80
499         cmpwi   %r2,0x380
500         bne     1f
501         mfdar   %r2
502         b       2f
503 1:      mfsrr0  %r2
504 2:      /* r2 now contains the fault address */
505         lis     %r3,SEGMENT_MASK@highesta
506         ori     %r3,%r3,SEGMENT_MASK@highera
507         sldi    %r3,%r3,32
508         oris    %r3,%r3,SEGMENT_MASK@ha
509         ori     %r3,%r3,SEGMENT_MASK@l
510         and     %r2,%r2,%r3     /* R2 = segment base address */
511         lis     %r3,USER_ADDR@highesta
512         ori     %r3,%r3,USER_ADDR@highera
513         sldi    %r3,%r3,32
514         oris    %r3,%r3,USER_ADDR@ha
515         ori     %r3,%r3,USER_ADDR@l
516         cmpd    %r2,%r3         /* Compare fault base to USER_ADDR */
517         bne     3f
518
519         /* User seg miss, handle as a regular trap */
520         ld      %r2,(PC_SLBSAVE+104)(%r1) /* Restore CR */
521         mtcr    %r2
522         ld      %r2,(PC_SLBSAVE+16)(%r1) /* Restore R2,R3 */
523         ld      %r3,(PC_SLBSAVE+24)(%r1)
524         ld      %r1,(PC_SLBSAVE+136)(%r1) /* Save the old LR in r1 */
525         mtsprg2 %r1                     /* And then in SPRG2 */
526         li      %r1, 0x80               /* How to get the vector from LR */
527         b       generictrap             /* Retain old LR using b */
528         
529 3:      /* Real kernel SLB miss */
530         std     %r0,(PC_SLBSAVE+0)(%r1) /* free all volatile regs */
531         mfsprg1 %r2                     /* Old R1 */
532         std     %r2,(PC_SLBSAVE+8)(%r1)
533         /* R2,R3 already saved */
534         std     %r4,(PC_SLBSAVE+32)(%r1)
535         std     %r5,(PC_SLBSAVE+40)(%r1)
536         std     %r6,(PC_SLBSAVE+48)(%r1)
537         std     %r7,(PC_SLBSAVE+56)(%r1)
538         std     %r8,(PC_SLBSAVE+64)(%r1)
539         std     %r9,(PC_SLBSAVE+72)(%r1)
540         std     %r10,(PC_SLBSAVE+80)(%r1)
541         std     %r11,(PC_SLBSAVE+88)(%r1)
542         std     %r12,(PC_SLBSAVE+96)(%r1)
543         /* CR already saved */
544         mfxer   %r2                     /* save XER */
545         std     %r2,(PC_SLBSAVE+112)(%r1)
546         mflr    %r2                     /* save LR (SP already saved) */
547         std     %r2,(PC_SLBSAVE+120)(%r1)
548         mfctr   %r2                     /* save CTR */
549         std     %r2,(PC_SLBSAVE+128)(%r1)
550
551         /* Call handler */
552         addi    %r1,%r1,PC_SLBSTACK-48+1024
553         li      %r2,~15
554         and     %r1,%r1,%r2
555         GET_TOCBASE(%r2)
556         mflr    %r3
557         andi.   %r3,%r3,0xff80
558         mfdar   %r4
559         mfsrr0  %r5
560         bl      handle_kernel_slb_spill
561         nop
562
563         /* Save r28-31, restore r4-r12 */
564         GET_CPUINFO(%r1)
565         ld      %r4,(PC_SLBSAVE+32)(%r1)
566         ld      %r5,(PC_SLBSAVE+40)(%r1)
567         ld      %r6,(PC_SLBSAVE+48)(%r1)
568         ld      %r7,(PC_SLBSAVE+56)(%r1)
569         ld      %r8,(PC_SLBSAVE+64)(%r1)
570         ld      %r9,(PC_SLBSAVE+72)(%r1)
571         ld      %r10,(PC_SLBSAVE+80)(%r1)
572         ld      %r11,(PC_SLBSAVE+88)(%r1)
573         ld      %r12,(PC_SLBSAVE+96)(%r1)
574         std     %r28,(PC_SLBSAVE+64)(%r1)
575         std     %r29,(PC_SLBSAVE+72)(%r1)
576         std     %r30,(PC_SLBSAVE+80)(%r1)
577         std     %r31,(PC_SLBSAVE+88)(%r1)
578
579         /* Restore kernel mapping */
580         bl      restore_kernsrs
581
582         /* Restore remaining registers */
583         ld      %r28,(PC_SLBSAVE+64)(%r1)
584         ld      %r29,(PC_SLBSAVE+72)(%r1)
585         ld      %r30,(PC_SLBSAVE+80)(%r1)
586         ld      %r31,(PC_SLBSAVE+88)(%r1)
587
588         ld      %r2,(PC_SLBSAVE+104)(%r1)
589         mtcr    %r2
590         ld      %r2,(PC_SLBSAVE+112)(%r1)
591         mtxer   %r2
592         ld      %r2,(PC_SLBSAVE+120)(%r1)
593         mtlr    %r2
594         ld      %r2,(PC_SLBSAVE+128)(%r1)
595         mtctr   %r2
596         ld      %r2,(PC_SLBSAVE+136)(%r1)
597         mtlr    %r2
598
599         /* Restore r0-r3 */
600         ld      %r0,(PC_SLBSAVE+0)(%r1)
601         ld      %r2,(PC_SLBSAVE+16)(%r1)
602         ld      %r3,(PC_SLBSAVE+24)(%r1)
603         mfsprg1 %r1
604
605         /* Back to whatever we were doing */
606         rfid
607
608 /*
609  * For ALI: has to save DSISR and DAR
610  */
611         .globl  CNAME(alitrap),CNAME(aliend)
612 CNAME(alitrap):
613         mtsprg1 %r1                     /* save SP */
614         GET_CPUINFO(%r1)
615         std     %r27,(PC_TEMPSAVE+CPUSAVE_R27)(%r1)     /* free r27-r31 */
616         std     %r28,(PC_TEMPSAVE+CPUSAVE_R28)(%r1)
617         std     %r29,(PC_TEMPSAVE+CPUSAVE_R29)(%r1)
618         std     %r30,(PC_TEMPSAVE+CPUSAVE_R30)(%r1)
619         std     %r31,(PC_TEMPSAVE+CPUSAVE_R31)(%r1)
620         mfdar   %r30
621         mfdsisr %r31
622         std     %r30,(PC_TEMPSAVE+CPUSAVE_AIM_DAR)(%r1)
623         std     %r31,(PC_TEMPSAVE+CPUSAVE_AIM_DSISR)(%r1)
624         mfsprg1 %r1                     /* restore SP, in case of branch */
625         mflr    %r28                    /* save LR */
626         mfcr    %r29                    /* save CR */
627
628         /* Begin dance to branch to s_trap in a bit */
629         b       1f
630         .p2align 3
631 1:      nop
632         bl      1f
633         .llong  s_trap
634 1:      mflr    %r31
635         ld      %r31,0(%r31)
636         mtlr    %r31
637
638         /* Put our exception vector in SPRG3 */
639         li      %r31, EXC_ALI
640         mtsprg3 %r31
641
642         /* Test whether we already had PR set */
643         mfsrr1  %r31
644         mtcr    %r31
645         blrl
646 CNAME(aliend):
647
648 /*
649  * Similar to the above for DSI
650  * Has to handle standard pagetable spills
651  */
652         .globl  CNAME(dsitrap),CNAME(dsiend)
653         .p2align 3
654 CNAME(dsitrap):
655         mtsprg1 %r1                     /* save SP */
656         GET_CPUINFO(%r1)
657         std     %r27,(PC_DISISAVE+CPUSAVE_R27)(%r1)     /* free r27-r31 */
658         std     %r28,(PC_DISISAVE+CPUSAVE_R28)(%r1)
659         std     %r29,(PC_DISISAVE+CPUSAVE_R29)(%r1)
660         std     %r30,(PC_DISISAVE+CPUSAVE_R30)(%r1)
661         std     %r31,(PC_DISISAVE+CPUSAVE_R31)(%r1)
662         mfcr    %r29                    /* save CR */
663         mfxer   %r30                    /* save XER */
664         mtsprg2 %r30                    /* in SPRG2 */
665         mfsrr1  %r31                    /* test kernel mode */
666         mtcr    %r31
667         mflr    %r28                    /* save LR (SP already saved) */
668         bl      1f                      /* Begin branching to disitrap */
669         .llong  disitrap
670 1:      mflr    %r1
671         ld      %r1,0(%r1)
672         mtlr    %r1
673         blrl                            /* Branch to generictrap */
674 CNAME(dsiend):
675
676 /*
677  * Preamble code for DSI/ISI traps
678  */
679 disitrap:
680         /* Write the trap vector to SPRG3 by computing LR & 0xff00 */
681         mflr    %r1
682         andi.   %r1,%r1,0xff00
683         mtsprg3 %r1
684         
685         GET_CPUINFO(%r1)
686         ld      %r31,(PC_DISISAVE+CPUSAVE_R27)(%r1)
687         std     %r31,(PC_TEMPSAVE+CPUSAVE_R27)(%r1)
688         ld      %r30,(PC_DISISAVE+CPUSAVE_R28)(%r1)
689         std     %r30,(PC_TEMPSAVE+CPUSAVE_R28)(%r1)
690         ld      %r31,(PC_DISISAVE+CPUSAVE_R29)(%r1)
691         std     %r31,(PC_TEMPSAVE+CPUSAVE_R29)(%r1)
692         ld      %r30,(PC_DISISAVE+CPUSAVE_R30)(%r1)
693         std     %r30,(PC_TEMPSAVE+CPUSAVE_R30)(%r1)
694         ld      %r31,(PC_DISISAVE+CPUSAVE_R31)(%r1)
695         std     %r31,(PC_TEMPSAVE+CPUSAVE_R31)(%r1)
696         mfdar   %r30
697         mfdsisr %r31
698         std     %r30,(PC_TEMPSAVE+CPUSAVE_AIM_DAR)(%r1)
699         std     %r31,(PC_TEMPSAVE+CPUSAVE_AIM_DSISR)(%r1)
700
701 #ifdef KDB
702         /* Try to detect a kernel stack overflow */
703         mfsrr1  %r31
704         mtcr    %r31
705         bt      17,realtrap             /* branch is user mode */
706         mfsprg1 %r31                    /* get old SP */
707         clrrdi  %r31,%r31,12            /* Round SP down to nearest page */
708         sub.    %r30,%r31,%r30          /* SP - DAR */
709         bge     1f
710         neg     %r30,%r30               /* modulo value */
711 1:      cmpldi  %cr0,%r30,4096          /* is DAR within a page of SP? */
712         bge     %cr0,realtrap           /* no, too far away. */
713
714         /* Now convert this DSI into a DDB trap.  */
715         GET_CPUINFO(%r1)
716         ld      %r30,(PC_TEMPSAVE+CPUSAVE_AIM_DAR)(%r1) /* get DAR */
717         std     %r30,(PC_DBSAVE  +CPUSAVE_AIM_DAR)(%r1) /* save DAR */
718         ld      %r30,(PC_TEMPSAVE+CPUSAVE_AIM_DSISR)(%r1) /* get DSISR */
719         std     %r30,(PC_DBSAVE  +CPUSAVE_AIM_DSISR)(%r1) /* save DSISR */
720         ld      %r31,(PC_DISISAVE+CPUSAVE_R27)(%r1) /* get  r27 */
721         std     %r31,(PC_DBSAVE  +CPUSAVE_R27)(%r1) /* save r27 */
722         ld      %r30,(PC_DISISAVE+CPUSAVE_R28)(%r1) /* get  r28 */
723         std     %r30,(PC_DBSAVE  +CPUSAVE_R28)(%r1) /* save r28 */
724         ld      %r31,(PC_DISISAVE+CPUSAVE_R29)(%r1) /* get  r29 */
725         std     %r31,(PC_DBSAVE  +CPUSAVE_R29)(%r1) /* save r29 */
726         ld      %r30,(PC_DISISAVE+CPUSAVE_R30)(%r1) /* get  r30 */
727         std     %r30,(PC_DBSAVE  +CPUSAVE_R30)(%r1) /* save r30 */
728         ld      %r31,(PC_DISISAVE+CPUSAVE_R31)(%r1) /* get  r31 */
729         std     %r31,(PC_DBSAVE  +CPUSAVE_R31)(%r1) /* save r31 */
730         b       dbtrap
731 #endif
732
733         /* XXX need stack probe here */
734 realtrap:
735 /* Test whether we already had PR set */
736         mfsrr1  %r1
737         mtcr    %r1
738         mfsprg1 %r1                     /* restore SP (might have been
739                                            overwritten) */
740         bf      17,k_trap               /* branch if PSL_PR is false */
741         GET_CPUINFO(%r1)
742         ld      %r1,PC_CURPCB(%r1)
743         mr      %r27,%r28               /* Save LR, r29 */
744         mtsprg2 %r29
745         bl      restore_kernsrs         /* enable kernel mapping */
746         mfsprg2 %r29
747         mr      %r28,%r27
748         b       s_trap
749
750 /*
751  * generictrap does some standard setup for trap handling to minimize
752  * the code that need be installed in the actual vectors. It expects
753  * the following conditions.
754  * 
755  * R1 - Trap vector = LR & (0xff00 | R1)
756  * SPRG1 - Original R1 contents
757  * SPRG2 - Original LR
758  */
759
760         .globl  CNAME(generictrap)
761 generictrap:
762         /* Save R1 for computing the exception vector */
763         mtsprg3 %r1
764
765         /* Save interesting registers */
766         GET_CPUINFO(%r1)
767         std     %r27,(PC_TEMPSAVE+CPUSAVE_R27)(%r1)     /* free r27-r31 */
768         std     %r28,(PC_TEMPSAVE+CPUSAVE_R28)(%r1)
769         std     %r29,(PC_TEMPSAVE+CPUSAVE_R29)(%r1)
770         std     %r30,(PC_TEMPSAVE+CPUSAVE_R30)(%r1)
771         std     %r31,(PC_TEMPSAVE+CPUSAVE_R31)(%r1)
772         mfdar   %r30
773         std     %r30,(PC_TEMPSAVE+CPUSAVE_AIM_DAR)(%r1)
774         mfsprg1 %r1                     /* restore SP, in case of branch */
775         mfsprg2 %r28                    /* save LR */
776         mfcr    %r29                    /* save CR */
777
778         /* Compute the exception vector from the link register */
779         mfsprg3 %r31
780         ori     %r31,%r31,0xff00
781         mflr    %r30
782         addi    %r30,%r30,-4 /* The branch instruction, not the next */
783         and     %r30,%r30,%r31
784         mtsprg3 %r30
785
786         /* Test whether we already had PR set */
787         mfsrr1  %r31
788         mtcr    %r31
789
790 s_trap:
791         bf      17,k_trap               /* branch if PSL_PR is false */
792         GET_CPUINFO(%r1)
793 u_trap:
794         ld      %r1,PC_CURPCB(%r1)
795         mr      %r27,%r28               /* Save LR, r29 */
796         mtsprg2 %r29
797         bl      restore_kernsrs         /* enable kernel mapping */
798         mfsprg2 %r29
799         mr      %r28,%r27
800
801 /*
802  * Now the common trap catching code.
803  */
804 k_trap:
805         FRAME_SETUP(PC_TEMPSAVE)
806 /* Call C interrupt dispatcher: */
807 trapagain:
808         GET_TOCBASE(%r2)
809         addi    %r3,%r1,48
810         bl      CNAME(powerpc_interrupt)
811         nop
812
813         .globl  CNAME(trapexit) /* backtrace code sentinel */
814 CNAME(trapexit):
815 /* Disable interrupts: */
816         mfmsr   %r3
817         andi.   %r3,%r3,~PSL_EE@l
818         mtmsr   %r3
819         isync
820 /* Test AST pending: */
821         ld      %r5,FRAME_SRR1+48(%r1)
822         mtcr    %r5
823         bf      17,1f                   /* branch if PSL_PR is false */
824
825         GET_CPUINFO(%r3)                /* get per-CPU pointer */
826         lwz     %r4, TD_FLAGS(%r13)     /* get thread flags value */
827         lis     %r5, (TDF_ASTPENDING|TDF_NEEDRESCHED)@h
828         ori     %r5,%r5, (TDF_ASTPENDING|TDF_NEEDRESCHED)@l
829         and.    %r4,%r4,%r5
830         beq     1f
831         mfmsr   %r3                     /* re-enable interrupts */
832         ori     %r3,%r3,PSL_EE@l
833         mtmsr   %r3
834         isync
835         GET_TOCBASE(%r2)
836         addi    %r3,%r1,48
837         bl      CNAME(ast)
838         nop
839         .globl  CNAME(asttrapexit)      /* backtrace code sentinel #2 */
840 CNAME(asttrapexit):
841         b       trapexit                /* test ast ret value ? */
842 1:
843         FRAME_LEAVE(PC_TEMPSAVE)
844         rfid
845
846 #if defined(KDB)
847 /*
848  * Deliberate entry to dbtrap
849  */
850 ASENTRY_NOPROF(breakpoint)
851         mtsprg1 %r1
852         mfmsr   %r3
853         mtsrr1  %r3
854         andi.   %r3,%r3,~(PSL_EE|PSL_ME)@l
855         mtmsr   %r3                     /* disable interrupts */
856         isync
857         GET_CPUINFO(%r3)
858         std     %r27,(PC_DBSAVE+CPUSAVE_R27)(%r3)
859         std     %r28,(PC_DBSAVE+CPUSAVE_R28)(%r3)
860         std     %r29,(PC_DBSAVE+CPUSAVE_R29)(%r3)
861         std     %r30,(PC_DBSAVE+CPUSAVE_R30)(%r3)
862         std     %r31,(PC_DBSAVE+CPUSAVE_R31)(%r3)
863         mflr    %r28
864         li      %r29,EXC_BPT
865         mtlr    %r29
866         mfcr    %r29
867         mtsrr0  %r28
868
869 /*
870  * Now the kdb trap catching code.
871  */
872 dbtrap:
873         /* Write the trap vector to SPRG3 by computing LR & 0xff00 */
874         mflr    %r1
875         andi.   %r1,%r1,0xff00
876         mtsprg3 %r1
877
878         ld      %r1,TRAP_TOCBASE(0)             /* get new SP */
879         ld      %r1,TOC_REF(tmpstk)(%r1)
880         addi    %r1,%r1,(TMPSTKSZ-48)
881
882         FRAME_SETUP(PC_DBSAVE)
883 /* Call C trap code: */
884         GET_TOCBASE(%r2)
885         addi    %r3,%r1,48
886         bl      CNAME(db_trap_glue)
887         nop
888         or.     %r3,%r3,%r3
889         bne     dbleave
890 /* This wasn't for KDB, so switch to real trap: */
891         ld      %r3,FRAME_EXC+48(%r1)   /* save exception */
892         GET_CPUINFO(%r4)
893         std     %r3,(PC_DBSAVE+CPUSAVE_R31)(%r4)
894         FRAME_LEAVE(PC_DBSAVE)
895         mtsprg1 %r1                     /* prepare for entrance to realtrap */
896         GET_CPUINFO(%r1)
897         std     %r27,(PC_TEMPSAVE+CPUSAVE_R27)(%r1)
898         std     %r28,(PC_TEMPSAVE+CPUSAVE_R28)(%r1)
899         std     %r29,(PC_TEMPSAVE+CPUSAVE_R29)(%r1)
900         std     %r30,(PC_TEMPSAVE+CPUSAVE_R30)(%r1)
901         std     %r31,(PC_TEMPSAVE+CPUSAVE_R31)(%r1)
902         mflr    %r28
903         mfcr    %r29
904         ld      %r31,(PC_DBSAVE+CPUSAVE_R31)(%r1)
905         mtsprg3 %r31                    /* SPRG3 was clobbered by FRAME_LEAVE */
906         mfsprg1 %r1
907         b       realtrap
908 dbleave:
909         FRAME_LEAVE(PC_DBSAVE)
910         rfid
911
912 /*
913  * In case of KDB we want a separate trap catcher for it
914  */
915         .globl  CNAME(dblow),CNAME(dbend)
916         .p2align 3
917 CNAME(dblow):
918         mtsprg1 %r1                     /* save SP */
919         mtsprg2 %r29                    /* save r29 */
920         mfcr    %r29                    /* save CR in r29 */
921         mfsrr1  %r1
922         mtcr    %r1
923         bf      17,1f                   /* branch if privileged */
924
925         /* Unprivileged case */
926         mtcr    %r29                    /* put the condition register back */
927         mfsprg2 %r29                    /* ... and r29 */
928         mflr    %r1                     /* save LR */
929         mtsprg2 %r1                     /* And then in SPRG2 */
930
931         ld      %r1, TRAP_GENTRAP(0)    /* Get branch address */
932         mtlr    %r1
933         li      %r1, 0                  /* How to get the vector from LR */
934         blrl                            /* Branch to generictrap */
935
936 1:
937         GET_CPUINFO(%r1)
938         std     %r27,(PC_DBSAVE+CPUSAVE_R27)(%r1)       /* free r27 */
939         std     %r28,(PC_DBSAVE+CPUSAVE_R28)(%r1)       /* free r28 */
940         mfsprg2 %r28                            /* r29 holds cr...  */
941         std     %r28,(PC_DBSAVE+CPUSAVE_R29)(%r1)       /* free r29 */
942         std     %r30,(PC_DBSAVE+CPUSAVE_R30)(%r1)       /* free r30 */
943         std     %r31,(PC_DBSAVE+CPUSAVE_R31)(%r1)       /* free r31 */
944         mflr    %r28                                    /* save LR */
945         nop                                             /* alignment */
946         bl      9f                                      /* Begin branch */
947         .llong  dbtrap
948 9:      mflr    %r1
949         ld      %r1,0(%r1)
950         mtlr    %r1
951         blrl                            /* Branch to generictrap */
952 CNAME(dbend):
953 #endif /* KDB */