GNU Linux-libre 4.14.254-gnu1
[releases.git] / arch / powerpc / kernel / entry_32.S
1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *  Rewritten by Cort Dougan (cort@fsmlabs.com) for PReP
5  *    Copyright (C) 1996 Cort Dougan <cort@fsmlabs.com>
6  *  Adapted for Power Macintosh by Paul Mackerras.
7  *  Low-level exception handlers and MMU support
8  *  rewritten by Paul Mackerras.
9  *    Copyright (C) 1996 Paul Mackerras.
10  *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
11  *
12  *  This file contains the system call entry code, context switch
13  *  code, and exception/interrupt return code for PowerPC.
14  *
15  *  This program is free software; you can redistribute it and/or
16  *  modify it under the terms of the GNU General Public License
17  *  as published by the Free Software Foundation; either version
18  *  2 of the License, or (at your option) any later version.
19  *
20  */
21
22 #include <linux/errno.h>
23 #include <linux/err.h>
24 #include <linux/sys.h>
25 #include <linux/threads.h>
26 #include <asm/reg.h>
27 #include <asm/page.h>
28 #include <asm/mmu.h>
29 #include <asm/cputable.h>
30 #include <asm/thread_info.h>
31 #include <asm/ppc_asm.h>
32 #include <asm/asm-offsets.h>
33 #include <asm/unistd.h>
34 #include <asm/ptrace.h>
35 #include <asm/export.h>
36 #include <asm/barrier.h>
37
38 /*
39  * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
40  */
41 #if MSR_KERNEL >= 0x10000
42 #define LOAD_MSR_KERNEL(r, x)   lis r,(x)@h; ori r,r,(x)@l
43 #else
44 #define LOAD_MSR_KERNEL(r, x)   li r,(x)
45 #endif
46
47 /*
48  * Align to 4k in order to ensure that all functions modyfing srr0/srr1
49  * fit into one page in order to not encounter a TLB miss between the
50  * modification of srr0/srr1 and the associated rfi.
51  */
52         .align  12
53
54 #ifdef CONFIG_BOOKE
55         .globl  mcheck_transfer_to_handler
56 mcheck_transfer_to_handler:
57         mfspr   r0,SPRN_DSRR0
58         stw     r0,_DSRR0(r11)
59         mfspr   r0,SPRN_DSRR1
60         stw     r0,_DSRR1(r11)
61         /* fall through */
62
63         .globl  debug_transfer_to_handler
64 debug_transfer_to_handler:
65         mfspr   r0,SPRN_CSRR0
66         stw     r0,_CSRR0(r11)
67         mfspr   r0,SPRN_CSRR1
68         stw     r0,_CSRR1(r11)
69         /* fall through */
70
71         .globl  crit_transfer_to_handler
72 crit_transfer_to_handler:
73 #ifdef CONFIG_PPC_BOOK3E_MMU
74         mfspr   r0,SPRN_MAS0
75         stw     r0,MAS0(r11)
76         mfspr   r0,SPRN_MAS1
77         stw     r0,MAS1(r11)
78         mfspr   r0,SPRN_MAS2
79         stw     r0,MAS2(r11)
80         mfspr   r0,SPRN_MAS3
81         stw     r0,MAS3(r11)
82         mfspr   r0,SPRN_MAS6
83         stw     r0,MAS6(r11)
84 #ifdef CONFIG_PHYS_64BIT
85         mfspr   r0,SPRN_MAS7
86         stw     r0,MAS7(r11)
87 #endif /* CONFIG_PHYS_64BIT */
88 #endif /* CONFIG_PPC_BOOK3E_MMU */
89 #ifdef CONFIG_44x
90         mfspr   r0,SPRN_MMUCR
91         stw     r0,MMUCR(r11)
92 #endif
93         mfspr   r0,SPRN_SRR0
94         stw     r0,_SRR0(r11)
95         mfspr   r0,SPRN_SRR1
96         stw     r0,_SRR1(r11)
97
98         /* set the stack limit to the current stack
99          * and set the limit to protect the thread_info
100          * struct
101          */
102         mfspr   r8,SPRN_SPRG_THREAD
103         lwz     r0,KSP_LIMIT(r8)
104         stw     r0,SAVED_KSP_LIMIT(r11)
105         rlwimi  r0,r1,0,0,(31-THREAD_SHIFT)
106         stw     r0,KSP_LIMIT(r8)
107         /* fall through */
108 #endif
109
110 #ifdef CONFIG_40x
111         .globl  crit_transfer_to_handler
112 crit_transfer_to_handler:
113         lwz     r0,crit_r10@l(0)
114         stw     r0,GPR10(r11)
115         lwz     r0,crit_r11@l(0)
116         stw     r0,GPR11(r11)
117         mfspr   r0,SPRN_SRR0
118         stw     r0,crit_srr0@l(0)
119         mfspr   r0,SPRN_SRR1
120         stw     r0,crit_srr1@l(0)
121
122         /* set the stack limit to the current stack
123          * and set the limit to protect the thread_info
124          * struct
125          */
126         mfspr   r8,SPRN_SPRG_THREAD
127         lwz     r0,KSP_LIMIT(r8)
128         stw     r0,saved_ksp_limit@l(0)
129         rlwimi  r0,r1,0,0,(31-THREAD_SHIFT)
130         stw     r0,KSP_LIMIT(r8)
131         /* fall through */
132 #endif
133
134 /*
135  * This code finishes saving the registers to the exception frame
136  * and jumps to the appropriate handler for the exception, turning
137  * on address translation.
138  * Note that we rely on the caller having set cr0.eq iff the exception
139  * occurred in kernel mode (i.e. MSR:PR = 0).
140  */
141         .globl  transfer_to_handler_full
142 transfer_to_handler_full:
143         SAVE_NVGPRS(r11)
144         /* fall through */
145
146         .globl  transfer_to_handler
147 transfer_to_handler:
148         stw     r2,GPR2(r11)
149         stw     r12,_NIP(r11)
150         stw     r9,_MSR(r11)
151         andi.   r2,r9,MSR_PR
152         mfctr   r12
153         mfspr   r2,SPRN_XER
154         stw     r12,_CTR(r11)
155         stw     r2,_XER(r11)
156         mfspr   r12,SPRN_SPRG_THREAD
157         addi    r2,r12,-THREAD
158         tovirt(r2,r2)                   /* set r2 to current */
159         beq     2f                      /* if from user, fix up THREAD.regs */
160         addi    r11,r1,STACK_FRAME_OVERHEAD
161         stw     r11,PT_REGS(r12)
162 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
163         /* Check to see if the dbcr0 register is set up to debug.  Use the
164            internal debug mode bit to do this. */
165         lwz     r12,THREAD_DBCR0(r12)
166         andis.  r12,r12,DBCR0_IDM@h
167         beq+    3f
168         /* From user and task is ptraced - load up global dbcr0 */
169         li      r12,-1                  /* clear all pending debug events */
170         mtspr   SPRN_DBSR,r12
171         lis     r11,global_dbcr0@ha
172         tophys(r11,r11)
173         addi    r11,r11,global_dbcr0@l
174 #ifdef CONFIG_SMP
175         CURRENT_THREAD_INFO(r9, r1)
176         lwz     r9,TI_CPU(r9)
177         slwi    r9,r9,3
178         add     r11,r11,r9
179 #endif
180         lwz     r12,0(r11)
181         mtspr   SPRN_DBCR0,r12
182         lwz     r12,4(r11)
183         addi    r12,r12,-1
184         stw     r12,4(r11)
185 #endif
186 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
187         CURRENT_THREAD_INFO(r9, r1)
188         tophys(r9, r9)
189         ACCOUNT_CPU_USER_ENTRY(r9, r11, r12)
190 #endif
191
192         b       3f
193
194 2:      /* if from kernel, check interrupted DOZE/NAP mode and
195          * check for stack overflow
196          */
197         lwz     r9,KSP_LIMIT(r12)
198         cmplw   r1,r9                   /* if r1 <= ksp_limit */
199         ble-    stack_ovf               /* then the kernel stack overflowed */
200 5:
201 #if defined(CONFIG_6xx) || defined(CONFIG_E500)
202         CURRENT_THREAD_INFO(r9, r1)
203         tophys(r9,r9)                   /* check local flags */
204         lwz     r12,TI_LOCAL_FLAGS(r9)
205         mtcrf   0x01,r12
206         bt-     31-TLF_NAPPING,4f
207         bt-     31-TLF_SLEEPING,7f
208 #endif /* CONFIG_6xx || CONFIG_E500 */
209         .globl transfer_to_handler_cont
210 transfer_to_handler_cont:
211 3:
212         mflr    r9
213         lwz     r11,0(r9)               /* virtual address of handler */
214         lwz     r9,4(r9)                /* where to go when done */
215 #ifdef CONFIG_PPC_8xx_PERF_EVENT
216         mtspr   SPRN_NRI, r0
217 #endif
218 #ifdef CONFIG_TRACE_IRQFLAGS
219         lis     r12,reenable_mmu@h
220         ori     r12,r12,reenable_mmu@l
221         mtspr   SPRN_SRR0,r12
222         mtspr   SPRN_SRR1,r10
223         SYNC
224         RFI
225 reenable_mmu:                           /* re-enable mmu so we can */
226         mfmsr   r10
227         lwz     r12,_MSR(r1)
228         xor     r10,r10,r12
229         andi.   r10,r10,MSR_EE          /* Did EE change? */
230         beq     1f
231
232         /*
233          * The trace_hardirqs_off will use CALLER_ADDR0 and CALLER_ADDR1.
234          * If from user mode there is only one stack frame on the stack, and
235          * accessing CALLER_ADDR1 will cause oops. So we need create a dummy
236          * stack frame to make trace_hardirqs_off happy.
237          *
238          * This is handy because we also need to save a bunch of GPRs,
239          * r3 can be different from GPR3(r1) at this point, r9 and r11
240          * contains the old MSR and handler address respectively,
241          * r4 & r5 can contain page fault arguments that need to be passed
242          * along as well. r12, CCR, CTR, XER etc... are left clobbered as
243          * they aren't useful past this point (aren't syscall arguments),
244          * the rest is restored from the exception frame.
245          */
246         stwu    r1,-32(r1)
247         stw     r9,8(r1)
248         stw     r11,12(r1)
249         stw     r3,16(r1)
250         stw     r4,20(r1)
251         stw     r5,24(r1)
252         bl      trace_hardirqs_off
253         lwz     r5,24(r1)
254         lwz     r4,20(r1)
255         lwz     r3,16(r1)
256         lwz     r11,12(r1)
257         lwz     r9,8(r1)
258         addi    r1,r1,32
259         lwz     r0,GPR0(r1)
260         lwz     r6,GPR6(r1)
261         lwz     r7,GPR7(r1)
262         lwz     r8,GPR8(r1)
263 1:      mtctr   r11
264         mtlr    r9
265         bctr                            /* jump to handler */
266 #else /* CONFIG_TRACE_IRQFLAGS */
267         mtspr   SPRN_SRR0,r11
268         mtspr   SPRN_SRR1,r10
269         mtlr    r9
270         SYNC
271         RFI                             /* jump to handler, enable MMU */
272 #endif /* CONFIG_TRACE_IRQFLAGS */
273
274 #if defined (CONFIG_6xx) || defined(CONFIG_E500)
275 4:      rlwinm  r12,r12,0,~_TLF_NAPPING
276         stw     r12,TI_LOCAL_FLAGS(r9)
277         b       power_save_ppc32_restore
278
279 7:      rlwinm  r12,r12,0,~_TLF_SLEEPING
280         stw     r12,TI_LOCAL_FLAGS(r9)
281         lwz     r9,_MSR(r11)            /* if sleeping, clear MSR.EE */
282         rlwinm  r9,r9,0,~MSR_EE
283         lwz     r12,_LINK(r11)          /* and return to address in LR */
284         b       fast_exception_return
285 #endif
286
287 /*
288  * On kernel stack overflow, load up an initial stack pointer
289  * and call StackOverflow(regs), which should not return.
290  */
291 stack_ovf:
292         /* sometimes we use a statically-allocated stack, which is OK. */
293         lis     r12,_end@h
294         ori     r12,r12,_end@l
295         cmplw   r1,r12
296         ble     5b                      /* r1 <= &_end is OK */
297         SAVE_NVGPRS(r11)
298         addi    r3,r1,STACK_FRAME_OVERHEAD
299         lis     r1,init_thread_union@ha
300         addi    r1,r1,init_thread_union@l
301         addi    r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
302         lis     r9,StackOverflow@ha
303         addi    r9,r9,StackOverflow@l
304         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
305 #ifdef CONFIG_PPC_8xx_PERF_EVENT
306         mtspr   SPRN_NRI, r0
307 #endif
308         mtspr   SPRN_SRR0,r9
309         mtspr   SPRN_SRR1,r10
310         SYNC
311         RFI
312
313 /*
314  * Handle a system call.
315  */
316         .stabs  "arch/powerpc/kernel/",N_SO,0,0,0f
317         .stabs  "entry_32.S",N_SO,0,0,0f
318 0:
319
320 _GLOBAL(DoSyscall)
321         stw     r3,ORIG_GPR3(r1)
322         li      r12,0
323         stw     r12,RESULT(r1)
324         lwz     r11,_CCR(r1)    /* Clear SO bit in CR */
325         rlwinm  r11,r11,0,4,2
326         stw     r11,_CCR(r1)
327 #ifdef CONFIG_TRACE_IRQFLAGS
328         /* Return from syscalls can (and generally will) hard enable
329          * interrupts. You aren't supposed to call a syscall with
330          * interrupts disabled in the first place. However, to ensure
331          * that we get it right vs. lockdep if it happens, we force
332          * that hard enable here with appropriate tracing if we see
333          * that we have been called with interrupts off
334          */
335         mfmsr   r11
336         andi.   r12,r11,MSR_EE
337         bne+    1f
338         /* We came in with interrupts disabled, we enable them now */
339         bl      trace_hardirqs_on
340         mfmsr   r11
341         lwz     r0,GPR0(r1)
342         lwz     r3,GPR3(r1)
343         lwz     r4,GPR4(r1)
344         ori     r11,r11,MSR_EE
345         lwz     r5,GPR5(r1)
346         lwz     r6,GPR6(r1)
347         lwz     r7,GPR7(r1)
348         lwz     r8,GPR8(r1)
349         mtmsr   r11
350 1:
351 #endif /* CONFIG_TRACE_IRQFLAGS */
352         CURRENT_THREAD_INFO(r10, r1)
353         lwz     r11,TI_FLAGS(r10)
354         andi.   r11,r11,_TIF_SYSCALL_DOTRACE
355         bne-    syscall_dotrace
356 syscall_dotrace_cont:
357         cmplwi  0,r0,NR_syscalls
358         lis     r10,sys_call_table@h
359         ori     r10,r10,sys_call_table@l
360         slwi    r0,r0,2
361         bge-    66f
362
363         barrier_nospec_asm
364         /*
365          * Prevent the load of the handler below (based on the user-passed
366          * system call number) being speculatively executed until the test
367          * against NR_syscalls and branch to .66f above has
368          * committed.
369          */
370
371         lwzx    r10,r10,r0      /* Fetch system call handler [ptr] */
372         mtlr    r10
373         addi    r9,r1,STACK_FRAME_OVERHEAD
374         PPC440EP_ERR42
375         blrl                    /* Call handler */
376         .globl  ret_from_syscall
377 ret_from_syscall:
378         mr      r6,r3
379         CURRENT_THREAD_INFO(r12, r1)
380         /* disable interrupts so current_thread_info()->flags can't change */
381         LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
382         /* Note: We don't bother telling lockdep about it */
383         SYNC
384         MTMSRD(r10)
385         lwz     r9,TI_FLAGS(r12)
386         li      r8,-MAX_ERRNO
387         andi.   r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
388         bne-    syscall_exit_work
389         cmplw   0,r3,r8
390         blt+    syscall_exit_cont
391         lwz     r11,_CCR(r1)                    /* Load CR */
392         neg     r3,r3
393         oris    r11,r11,0x1000  /* Set SO bit in CR */
394         stw     r11,_CCR(r1)
395 syscall_exit_cont:
396         lwz     r8,_MSR(r1)
397 #ifdef CONFIG_TRACE_IRQFLAGS
398         /* If we are going to return from the syscall with interrupts
399          * off, we trace that here. It shouldn't happen though but we
400          * want to catch the bugger if it does right ?
401          */
402         andi.   r10,r8,MSR_EE
403         bne+    1f
404         stw     r3,GPR3(r1)
405         bl      trace_hardirqs_off
406         lwz     r3,GPR3(r1)
407 1:
408 #endif /* CONFIG_TRACE_IRQFLAGS */
409 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
410         /* If the process has its own DBCR0 value, load it up.  The internal
411            debug mode bit tells us that dbcr0 should be loaded. */
412         lwz     r0,THREAD+THREAD_DBCR0(r2)
413         andis.  r10,r0,DBCR0_IDM@h
414         bnel-   load_dbcr0
415 #endif
416 #ifdef CONFIG_44x
417 BEGIN_MMU_FTR_SECTION
418         lis     r4,icache_44x_need_flush@ha
419         lwz     r5,icache_44x_need_flush@l(r4)
420         cmplwi  cr0,r5,0
421         bne-    2f
422 1:
423 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_47x)
424 #endif /* CONFIG_44x */
425 BEGIN_FTR_SECTION
426         lwarx   r7,0,r1
427 END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
428         stwcx.  r0,0,r1                 /* to clear the reservation */
429 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
430         andi.   r4,r8,MSR_PR
431         beq     3f
432         CURRENT_THREAD_INFO(r4, r1)
433         ACCOUNT_CPU_USER_EXIT(r4, r5, r7)
434 3:
435 #endif
436         lwz     r4,_LINK(r1)
437         lwz     r5,_CCR(r1)
438         mtlr    r4
439         mtcr    r5
440         lwz     r7,_NIP(r1)
441         lwz     r2,GPR2(r1)
442         lwz     r1,GPR1(r1)
443 #ifdef CONFIG_PPC_8xx_PERF_EVENT
444         mtspr   SPRN_NRI, r0
445 #endif
446         mtspr   SPRN_SRR0,r7
447         mtspr   SPRN_SRR1,r8
448         SYNC
449         RFI
450 #ifdef CONFIG_44x
451 2:      li      r7,0
452         iccci   r0,r0
453         stw     r7,icache_44x_need_flush@l(r4)
454         b       1b
455 #endif  /* CONFIG_44x */
456
457 66:     li      r3,-ENOSYS
458         b       ret_from_syscall
459
460         .globl  ret_from_fork
461 ret_from_fork:
462         REST_NVGPRS(r1)
463         bl      schedule_tail
464         li      r3,0
465         b       ret_from_syscall
466
467         .globl  ret_from_kernel_thread
468 ret_from_kernel_thread:
469         REST_NVGPRS(r1)
470         bl      schedule_tail
471         mtlr    r14
472         mr      r3,r15
473         PPC440EP_ERR42
474         blrl
475         li      r3,0
476         b       ret_from_syscall
477
478 /* Traced system call support */
479 syscall_dotrace:
480         SAVE_NVGPRS(r1)
481         li      r0,0xc00
482         stw     r0,_TRAP(r1)
483         addi    r3,r1,STACK_FRAME_OVERHEAD
484         bl      do_syscall_trace_enter
485         /*
486          * Restore argument registers possibly just changed.
487          * We use the return value of do_syscall_trace_enter
488          * for call number to look up in the table (r0).
489          */
490         mr      r0,r3
491         lwz     r3,GPR3(r1)
492         lwz     r4,GPR4(r1)
493         lwz     r5,GPR5(r1)
494         lwz     r6,GPR6(r1)
495         lwz     r7,GPR7(r1)
496         lwz     r8,GPR8(r1)
497         REST_NVGPRS(r1)
498
499         cmplwi  r0,NR_syscalls
500         /* Return code is already in r3 thanks to do_syscall_trace_enter() */
501         bge-    ret_from_syscall
502         b       syscall_dotrace_cont
503
504 syscall_exit_work:
505         andi.   r0,r9,_TIF_RESTOREALL
506         beq+    0f
507         REST_NVGPRS(r1)
508         b       2f
509 0:      cmplw   0,r3,r8
510         blt+    1f
511         andi.   r0,r9,_TIF_NOERROR
512         bne-    1f
513         lwz     r11,_CCR(r1)                    /* Load CR */
514         neg     r3,r3
515         oris    r11,r11,0x1000  /* Set SO bit in CR */
516         stw     r11,_CCR(r1)
517
518 1:      stw     r6,RESULT(r1)   /* Save result */
519         stw     r3,GPR3(r1)     /* Update return value */
520 2:      andi.   r0,r9,(_TIF_PERSYSCALL_MASK)
521         beq     4f
522
523         /* Clear per-syscall TIF flags if any are set.  */
524
525         li      r11,_TIF_PERSYSCALL_MASK
526         addi    r12,r12,TI_FLAGS
527 3:      lwarx   r8,0,r12
528         andc    r8,r8,r11
529 #ifdef CONFIG_IBM405_ERR77
530         dcbt    0,r12
531 #endif
532         stwcx.  r8,0,r12
533         bne-    3b
534         subi    r12,r12,TI_FLAGS
535         
536 4:      /* Anything which requires enabling interrupts? */
537         andi.   r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP)
538         beq     ret_from_except
539
540         /* Re-enable interrupts. There is no need to trace that with
541          * lockdep as we are supposed to have IRQs on at this point
542          */
543         ori     r10,r10,MSR_EE
544         SYNC
545         MTMSRD(r10)
546
547         /* Save NVGPRS if they're not saved already */
548         lwz     r4,_TRAP(r1)
549         andi.   r4,r4,1
550         beq     5f
551         SAVE_NVGPRS(r1)
552         li      r4,0xc00
553         stw     r4,_TRAP(r1)
554 5:
555         addi    r3,r1,STACK_FRAME_OVERHEAD
556         bl      do_syscall_trace_leave
557         b       ret_from_except_full
558
559 /*
560  * The fork/clone functions need to copy the full register set into
561  * the child process. Therefore we need to save all the nonvolatile
562  * registers (r13 - r31) before calling the C code.
563  */
564         .globl  ppc_fork
565 ppc_fork:
566         SAVE_NVGPRS(r1)
567         lwz     r0,_TRAP(r1)
568         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
569         stw     r0,_TRAP(r1)            /* register set saved */
570         b       sys_fork
571
572         .globl  ppc_vfork
573 ppc_vfork:
574         SAVE_NVGPRS(r1)
575         lwz     r0,_TRAP(r1)
576         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
577         stw     r0,_TRAP(r1)            /* register set saved */
578         b       sys_vfork
579
580         .globl  ppc_clone
581 ppc_clone:
582         SAVE_NVGPRS(r1)
583         lwz     r0,_TRAP(r1)
584         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
585         stw     r0,_TRAP(r1)            /* register set saved */
586         b       sys_clone
587
588         .globl  ppc_swapcontext
589 ppc_swapcontext:
590         SAVE_NVGPRS(r1)
591         lwz     r0,_TRAP(r1)
592         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
593         stw     r0,_TRAP(r1)            /* register set saved */
594         b       sys_swapcontext
595
596 /*
597  * Top-level page fault handling.
598  * This is in assembler because if do_page_fault tells us that
599  * it is a bad kernel page fault, we want to save the non-volatile
600  * registers before calling bad_page_fault.
601  */
602         .globl  handle_page_fault
603 handle_page_fault:
604         stw     r4,_DAR(r1)
605         addi    r3,r1,STACK_FRAME_OVERHEAD
606 #ifdef CONFIG_6xx
607         andis.  r0,r5,DSISR_DABRMATCH@h
608         bne-    handle_dabr_fault
609 #endif
610         bl      do_page_fault
611         cmpwi   r3,0
612         beq+    ret_from_except
613         SAVE_NVGPRS(r1)
614         lwz     r0,_TRAP(r1)
615         clrrwi  r0,r0,1
616         stw     r0,_TRAP(r1)
617         mr      r5,r3
618         addi    r3,r1,STACK_FRAME_OVERHEAD
619         lwz     r4,_DAR(r1)
620         bl      bad_page_fault
621         b       ret_from_except_full
622
623 #ifdef CONFIG_6xx
624         /* We have a data breakpoint exception - handle it */
625 handle_dabr_fault:
626         SAVE_NVGPRS(r1)
627         lwz     r0,_TRAP(r1)
628         clrrwi  r0,r0,1
629         stw     r0,_TRAP(r1)
630         bl      do_break
631         b       ret_from_except_full
632 #endif
633
634 /*
635  * This routine switches between two different tasks.  The process
636  * state of one is saved on its kernel stack.  Then the state
637  * of the other is restored from its kernel stack.  The memory
638  * management hardware is updated to the second process's state.
639  * Finally, we can return to the second process.
640  * On entry, r3 points to the THREAD for the current task, r4
641  * points to the THREAD for the new task.
642  *
643  * This routine is always called with interrupts disabled.
644  *
645  * Note: there are two ways to get to the "going out" portion
646  * of this code; either by coming in via the entry (_switch)
647  * or via "fork" which must set up an environment equivalent
648  * to the "_switch" path.  If you change this , you'll have to
649  * change the fork code also.
650  *
651  * The code which creates the new task context is in 'copy_thread'
652  * in arch/ppc/kernel/process.c
653  */
654 _GLOBAL(_switch)
655         stwu    r1,-INT_FRAME_SIZE(r1)
656         mflr    r0
657         stw     r0,INT_FRAME_SIZE+4(r1)
658         /* r3-r12 are caller saved -- Cort */
659         SAVE_NVGPRS(r1)
660         stw     r0,_NIP(r1)     /* Return to switch caller */
661         mfmsr   r11
662         li      r0,MSR_FP       /* Disable floating-point */
663 #ifdef CONFIG_ALTIVEC
664 BEGIN_FTR_SECTION
665         oris    r0,r0,MSR_VEC@h /* Disable altivec */
666         mfspr   r12,SPRN_VRSAVE /* save vrsave register value */
667         stw     r12,THREAD+THREAD_VRSAVE(r2)
668 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
669 #endif /* CONFIG_ALTIVEC */
670 #ifdef CONFIG_SPE
671 BEGIN_FTR_SECTION
672         oris    r0,r0,MSR_SPE@h  /* Disable SPE */
673         mfspr   r12,SPRN_SPEFSCR /* save spefscr register value */
674         stw     r12,THREAD+THREAD_SPEFSCR(r2)
675 END_FTR_SECTION_IFSET(CPU_FTR_SPE)
676 #endif /* CONFIG_SPE */
677         and.    r0,r0,r11       /* FP or altivec or SPE enabled? */
678         beq+    1f
679         andc    r11,r11,r0
680         MTMSRD(r11)
681         isync
682 1:      stw     r11,_MSR(r1)
683         mfcr    r10
684         stw     r10,_CCR(r1)
685         stw     r1,KSP(r3)      /* Set old stack pointer */
686
687 #ifdef CONFIG_SMP
688         /* We need a sync somewhere here to make sure that if the
689          * previous task gets rescheduled on another CPU, it sees all
690          * stores it has performed on this one.
691          */
692         sync
693 #endif /* CONFIG_SMP */
694
695         tophys(r0,r4)
696         mtspr   SPRN_SPRG_THREAD,r0     /* Update current THREAD phys addr */
697         lwz     r1,KSP(r4)      /* Load new stack pointer */
698
699         /* save the old current 'last' for return value */
700         mr      r3,r2
701         addi    r2,r4,-THREAD   /* Update current */
702
703 #ifdef CONFIG_ALTIVEC
704 BEGIN_FTR_SECTION
705         lwz     r0,THREAD+THREAD_VRSAVE(r2)
706         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
707 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
708 #endif /* CONFIG_ALTIVEC */
709 #ifdef CONFIG_SPE
710 BEGIN_FTR_SECTION
711         lwz     r0,THREAD+THREAD_SPEFSCR(r2)
712         mtspr   SPRN_SPEFSCR,r0         /* restore SPEFSCR reg */
713 END_FTR_SECTION_IFSET(CPU_FTR_SPE)
714 #endif /* CONFIG_SPE */
715
716         lwz     r0,_CCR(r1)
717         mtcrf   0xFF,r0
718         /* r3-r12 are destroyed -- Cort */
719         REST_NVGPRS(r1)
720
721         lwz     r4,_NIP(r1)     /* Return to _switch caller in new task */
722         mtlr    r4
723         addi    r1,r1,INT_FRAME_SIZE
724         blr
725
726         .globl  fast_exception_return
727 fast_exception_return:
728 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
729         andi.   r10,r9,MSR_RI           /* check for recoverable interrupt */
730         beq     1f                      /* if not, we've got problems */
731 #endif
732
733 2:      REST_4GPRS(3, r11)
734         lwz     r10,_CCR(r11)
735         REST_GPR(1, r11)
736         mtcr    r10
737         lwz     r10,_LINK(r11)
738         mtlr    r10
739         /* Clear the exception_marker on the stack to avoid confusing stacktrace */
740         li      r10, 0
741         stw     r10, 8(r11)
742         REST_GPR(10, r11)
743 #ifdef CONFIG_PPC_8xx_PERF_EVENT
744         mtspr   SPRN_NRI, r0
745 #endif
746         mtspr   SPRN_SRR1,r9
747         mtspr   SPRN_SRR0,r12
748         REST_GPR(9, r11)
749         REST_GPR(12, r11)
750         lwz     r11,GPR11(r11)
751         SYNC
752         RFI
753
754 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
755 /* check if the exception happened in a restartable section */
756 1:      lis     r3,exc_exit_restart_end@ha
757         addi    r3,r3,exc_exit_restart_end@l
758         cmplw   r12,r3
759         bge     3f
760         lis     r4,exc_exit_restart@ha
761         addi    r4,r4,exc_exit_restart@l
762         cmplw   r12,r4
763         blt     3f
764         lis     r3,fee_restarts@ha
765         tophys(r3,r3)
766         lwz     r5,fee_restarts@l(r3)
767         addi    r5,r5,1
768         stw     r5,fee_restarts@l(r3)
769         mr      r12,r4          /* restart at exc_exit_restart */
770         b       2b
771
772         .section .bss
773         .align  2
774 fee_restarts:
775         .space  4
776         .previous
777
778 /* aargh, a nonrecoverable interrupt, panic */
779 /* aargh, we don't know which trap this is */
780 /* but the 601 doesn't implement the RI bit, so assume it's OK */
781 3:
782 BEGIN_FTR_SECTION
783         b       2b
784 END_FTR_SECTION_IFSET(CPU_FTR_601)
785         li      r10,-1
786         stw     r10,_TRAP(r11)
787         addi    r3,r1,STACK_FRAME_OVERHEAD
788         lis     r10,MSR_KERNEL@h
789         ori     r10,r10,MSR_KERNEL@l
790         bl      transfer_to_handler_full
791         .long   nonrecoverable_exception
792         .long   ret_from_except
793 #endif
794
795         .globl  ret_from_except_full
796 ret_from_except_full:
797         REST_NVGPRS(r1)
798         /* fall through */
799
800         .globl  ret_from_except
801 ret_from_except:
802         /* Hard-disable interrupts so that current_thread_info()->flags
803          * can't change between when we test it and when we return
804          * from the interrupt. */
805         /* Note: We don't bother telling lockdep about it */
806         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
807         SYNC                    /* Some chip revs have problems here... */
808         MTMSRD(r10)             /* disable interrupts */
809
810         lwz     r3,_MSR(r1)     /* Returning to user mode? */
811         andi.   r0,r3,MSR_PR
812         beq     resume_kernel
813
814 user_exc_return:                /* r10 contains MSR_KERNEL here */
815         /* Check current_thread_info()->flags */
816         CURRENT_THREAD_INFO(r9, r1)
817         lwz     r9,TI_FLAGS(r9)
818         andi.   r0,r9,_TIF_USER_WORK_MASK
819         bne     do_work
820
821 restore_user:
822 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
823         /* Check whether this process has its own DBCR0 value.  The internal
824            debug mode bit tells us that dbcr0 should be loaded. */
825         lwz     r0,THREAD+THREAD_DBCR0(r2)
826         andis.  r10,r0,DBCR0_IDM@h
827         bnel-   load_dbcr0
828 #endif
829 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
830         CURRENT_THREAD_INFO(r9, r1)
831         ACCOUNT_CPU_USER_EXIT(r9, r10, r11)
832 #endif
833
834         b       restore
835
836 /* N.B. the only way to get here is from the beq following ret_from_except. */
837 resume_kernel:
838         /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
839         CURRENT_THREAD_INFO(r9, r1)
840         lwz     r8,TI_FLAGS(r9)
841         andis.  r0,r8,_TIF_EMULATE_STACK_STORE@h
842         beq+    1f
843
844         addi    r8,r1,INT_FRAME_SIZE    /* Get the kprobed function entry */
845
846         lwz     r3,GPR1(r1)
847         subi    r3,r3,INT_FRAME_SIZE    /* dst: Allocate a trampoline exception frame */
848         mr      r4,r1                   /* src:  current exception frame */
849         mr      r1,r3                   /* Reroute the trampoline frame to r1 */
850
851         /* Copy from the original to the trampoline. */
852         li      r5,INT_FRAME_SIZE/4     /* size: INT_FRAME_SIZE */
853         li      r6,0                    /* start offset: 0 */
854         mtctr   r5
855 2:      lwzx    r0,r6,r4
856         stwx    r0,r6,r3
857         addi    r6,r6,4
858         bdnz    2b
859
860         /* Do real store operation to complete stwu */
861         lwz     r5,GPR1(r1)
862         stw     r8,0(r5)
863
864         /* Clear _TIF_EMULATE_STACK_STORE flag */
865         lis     r11,_TIF_EMULATE_STACK_STORE@h
866         addi    r5,r9,TI_FLAGS
867 0:      lwarx   r8,0,r5
868         andc    r8,r8,r11
869 #ifdef CONFIG_IBM405_ERR77
870         dcbt    0,r5
871 #endif
872         stwcx.  r8,0,r5
873         bne-    0b
874 1:
875
876 #ifdef CONFIG_PREEMPT
877         /* check current_thread_info->preempt_count */
878         lwz     r0,TI_PREEMPT(r9)
879         cmpwi   0,r0,0          /* if non-zero, just restore regs and return */
880         bne     restore
881         andi.   r8,r8,_TIF_NEED_RESCHED
882         beq+    restore
883         lwz     r3,_MSR(r1)
884         andi.   r0,r3,MSR_EE    /* interrupts off? */
885         beq     restore         /* don't schedule if so */
886 #ifdef CONFIG_TRACE_IRQFLAGS
887         /* Lockdep thinks irqs are enabled, we need to call
888          * preempt_schedule_irq with IRQs off, so we inform lockdep
889          * now that we -did- turn them off already
890          */
891         bl      trace_hardirqs_off
892 #endif
893 1:      bl      preempt_schedule_irq
894         CURRENT_THREAD_INFO(r9, r1)
895         lwz     r3,TI_FLAGS(r9)
896         andi.   r0,r3,_TIF_NEED_RESCHED
897         bne-    1b
898 #ifdef CONFIG_TRACE_IRQFLAGS
899         /* And now, to properly rebalance the above, we tell lockdep they
900          * are being turned back on, which will happen when we return
901          */
902         bl      trace_hardirqs_on
903 #endif
904 #endif /* CONFIG_PREEMPT */
905
906         /* interrupts are hard-disabled at this point */
907 restore:
908 #ifdef CONFIG_44x
909 BEGIN_MMU_FTR_SECTION
910         b       1f
911 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_47x)
912         lis     r4,icache_44x_need_flush@ha
913         lwz     r5,icache_44x_need_flush@l(r4)
914         cmplwi  cr0,r5,0
915         beq+    1f
916         li      r6,0
917         iccci   r0,r0
918         stw     r6,icache_44x_need_flush@l(r4)
919 1:
920 #endif  /* CONFIG_44x */
921
922         lwz     r9,_MSR(r1)
923 #ifdef CONFIG_TRACE_IRQFLAGS
924         /* Lockdep doesn't know about the fact that IRQs are temporarily turned
925          * off in this assembly code while peeking at TI_FLAGS() and such. However
926          * we need to inform it if the exception turned interrupts off, and we
927          * are about to trun them back on.
928          *
929          * The problem here sadly is that we don't know whether the exceptions was
930          * one that turned interrupts off or not. So we always tell lockdep about
931          * turning them on here when we go back to wherever we came from with EE
932          * on, even if that may meen some redudant calls being tracked. Maybe later
933          * we could encode what the exception did somewhere or test the exception
934          * type in the pt_regs but that sounds overkill
935          */
936         andi.   r10,r9,MSR_EE
937         beq     1f
938         /*
939          * Since the ftrace irqsoff latency trace checks CALLER_ADDR1,
940          * which is the stack frame here, we need to force a stack frame
941          * in case we came from user space.
942          */
943         stwu    r1,-32(r1)
944         mflr    r0
945         stw     r0,4(r1)
946         stwu    r1,-32(r1)
947         bl      trace_hardirqs_on
948         lwz     r1,0(r1)
949         lwz     r1,0(r1)
950         lwz     r9,_MSR(r1)
951 1:
952 #endif /* CONFIG_TRACE_IRQFLAGS */
953
954         lwz     r0,GPR0(r1)
955         lwz     r2,GPR2(r1)
956         REST_4GPRS(3, r1)
957         REST_2GPRS(7, r1)
958
959         lwz     r10,_XER(r1)
960         lwz     r11,_CTR(r1)
961         mtspr   SPRN_XER,r10
962         mtctr   r11
963
964         PPC405_ERR77(0,r1)
965 BEGIN_FTR_SECTION
966         lwarx   r11,0,r1
967 END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
968         stwcx.  r0,0,r1                 /* to clear the reservation */
969
970 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
971         andi.   r10,r9,MSR_RI           /* check if this exception occurred */
972         beql    nonrecoverable          /* at a bad place (MSR:RI = 0) */
973
974         lwz     r10,_CCR(r1)
975         lwz     r11,_LINK(r1)
976         mtcrf   0xFF,r10
977         mtlr    r11
978
979         /* Clear the exception_marker on the stack to avoid confusing stacktrace */
980         li      r10, 0
981         stw     r10, 8(r1)
982         /*
983          * Once we put values in SRR0 and SRR1, we are in a state
984          * where exceptions are not recoverable, since taking an
985          * exception will trash SRR0 and SRR1.  Therefore we clear the
986          * MSR:RI bit to indicate this.  If we do take an exception,
987          * we can't return to the point of the exception but we
988          * can restart the exception exit path at the label
989          * exc_exit_restart below.  -- paulus
990          */
991         LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
992         SYNC
993         MTMSRD(r10)             /* clear the RI bit */
994         .globl exc_exit_restart
995 exc_exit_restart:
996         lwz     r12,_NIP(r1)
997 #ifdef CONFIG_PPC_8xx_PERF_EVENT
998         mtspr   SPRN_NRI, r0
999 #endif
1000         mtspr   SPRN_SRR0,r12
1001         mtspr   SPRN_SRR1,r9
1002         REST_4GPRS(9, r1)
1003         lwz     r1,GPR1(r1)
1004         .globl exc_exit_restart_end
1005 exc_exit_restart_end:
1006         SYNC
1007         RFI
1008
1009 #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
1010         /*
1011          * This is a bit different on 4xx/Book-E because it doesn't have
1012          * the RI bit in the MSR.
1013          * The TLB miss handler checks if we have interrupted
1014          * the exception exit path and restarts it if so
1015          * (well maybe one day it will... :).
1016          */
1017         lwz     r11,_LINK(r1)
1018         mtlr    r11
1019         lwz     r10,_CCR(r1)
1020         mtcrf   0xff,r10
1021         /* Clear the exception_marker on the stack to avoid confusing stacktrace */
1022         li      r10, 0
1023         stw     r10, 8(r1)
1024         REST_2GPRS(9, r1)
1025         .globl exc_exit_restart
1026 exc_exit_restart:
1027         lwz     r11,_NIP(r1)
1028         lwz     r12,_MSR(r1)
1029 exc_exit_start:
1030         mtspr   SPRN_SRR0,r11
1031         mtspr   SPRN_SRR1,r12
1032         REST_2GPRS(11, r1)
1033         lwz     r1,GPR1(r1)
1034         .globl exc_exit_restart_end
1035 exc_exit_restart_end:
1036         PPC405_ERR77_SYNC
1037         rfi
1038         b       .                       /* prevent prefetch past rfi */
1039
1040 /*
1041  * Returning from a critical interrupt in user mode doesn't need
1042  * to be any different from a normal exception.  For a critical
1043  * interrupt in the kernel, we just return (without checking for
1044  * preemption) since the interrupt may have happened at some crucial
1045  * place (e.g. inside the TLB miss handler), and because we will be
1046  * running with r1 pointing into critical_stack, not the current
1047  * process's kernel stack (and therefore current_thread_info() will
1048  * give the wrong answer).
1049  * We have to restore various SPRs that may have been in use at the
1050  * time of the critical interrupt.
1051  *
1052  */
1053 #ifdef CONFIG_40x
1054 #define PPC_40x_TURN_OFF_MSR_DR                                             \
1055         /* avoid any possible TLB misses here by turning off MSR.DR, we     \
1056          * assume the instructions here are mapped by a pinned TLB entry */ \
1057         li      r10,MSR_IR;                                                 \
1058         mtmsr   r10;                                                        \
1059         isync;                                                              \
1060         tophys(r1, r1);
1061 #else
1062 #define PPC_40x_TURN_OFF_MSR_DR
1063 #endif
1064
1065 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi)     \
1066         REST_NVGPRS(r1);                                                \
1067         lwz     r3,_MSR(r1);                                            \
1068         andi.   r3,r3,MSR_PR;                                           \
1069         LOAD_MSR_KERNEL(r10,MSR_KERNEL);                                \
1070         bne     user_exc_return;                                        \
1071         lwz     r0,GPR0(r1);                                            \
1072         lwz     r2,GPR2(r1);                                            \
1073         REST_4GPRS(3, r1);                                              \
1074         REST_2GPRS(7, r1);                                              \
1075         lwz     r10,_XER(r1);                                           \
1076         lwz     r11,_CTR(r1);                                           \
1077         mtspr   SPRN_XER,r10;                                           \
1078         mtctr   r11;                                                    \
1079         PPC405_ERR77(0,r1);                                             \
1080         stwcx.  r0,0,r1;                /* to clear the reservation */  \
1081         lwz     r11,_LINK(r1);                                          \
1082         mtlr    r11;                                                    \
1083         lwz     r10,_CCR(r1);                                           \
1084         mtcrf   0xff,r10;                                               \
1085         PPC_40x_TURN_OFF_MSR_DR;                                        \
1086         lwz     r9,_DEAR(r1);                                           \
1087         lwz     r10,_ESR(r1);                                           \
1088         mtspr   SPRN_DEAR,r9;                                           \
1089         mtspr   SPRN_ESR,r10;                                           \
1090         lwz     r11,_NIP(r1);                                           \
1091         lwz     r12,_MSR(r1);                                           \
1092         mtspr   exc_lvl_srr0,r11;                                       \
1093         mtspr   exc_lvl_srr1,r12;                                       \
1094         lwz     r9,GPR9(r1);                                            \
1095         lwz     r12,GPR12(r1);                                          \
1096         lwz     r10,GPR10(r1);                                          \
1097         lwz     r11,GPR11(r1);                                          \
1098         lwz     r1,GPR1(r1);                                            \
1099         PPC405_ERR77_SYNC;                                              \
1100         exc_lvl_rfi;                                                    \
1101         b       .;              /* prevent prefetch past exc_lvl_rfi */
1102
1103 #define RESTORE_xSRR(exc_lvl_srr0, exc_lvl_srr1)                        \
1104         lwz     r9,_##exc_lvl_srr0(r1);                                 \
1105         lwz     r10,_##exc_lvl_srr1(r1);                                \
1106         mtspr   SPRN_##exc_lvl_srr0,r9;                                 \
1107         mtspr   SPRN_##exc_lvl_srr1,r10;
1108
1109 #if defined(CONFIG_PPC_BOOK3E_MMU)
1110 #ifdef CONFIG_PHYS_64BIT
1111 #define RESTORE_MAS7                                                    \
1112         lwz     r11,MAS7(r1);                                           \
1113         mtspr   SPRN_MAS7,r11;
1114 #else
1115 #define RESTORE_MAS7
1116 #endif /* CONFIG_PHYS_64BIT */
1117 #define RESTORE_MMU_REGS                                                \
1118         lwz     r9,MAS0(r1);                                            \
1119         lwz     r10,MAS1(r1);                                           \
1120         lwz     r11,MAS2(r1);                                           \
1121         mtspr   SPRN_MAS0,r9;                                           \
1122         lwz     r9,MAS3(r1);                                            \
1123         mtspr   SPRN_MAS1,r10;                                          \
1124         lwz     r10,MAS6(r1);                                           \
1125         mtspr   SPRN_MAS2,r11;                                          \
1126         mtspr   SPRN_MAS3,r9;                                           \
1127         mtspr   SPRN_MAS6,r10;                                          \
1128         RESTORE_MAS7;
1129 #elif defined(CONFIG_44x)
1130 #define RESTORE_MMU_REGS                                                \
1131         lwz     r9,MMUCR(r1);                                           \
1132         mtspr   SPRN_MMUCR,r9;
1133 #else
1134 #define RESTORE_MMU_REGS
1135 #endif
1136
1137 #ifdef CONFIG_40x
1138         .globl  ret_from_crit_exc
1139 ret_from_crit_exc:
1140         mfspr   r9,SPRN_SPRG_THREAD
1141         lis     r10,saved_ksp_limit@ha;
1142         lwz     r10,saved_ksp_limit@l(r10);
1143         tovirt(r9,r9);
1144         stw     r10,KSP_LIMIT(r9)
1145         lis     r9,crit_srr0@ha;
1146         lwz     r9,crit_srr0@l(r9);
1147         lis     r10,crit_srr1@ha;
1148         lwz     r10,crit_srr1@l(r10);
1149         mtspr   SPRN_SRR0,r9;
1150         mtspr   SPRN_SRR1,r10;
1151         RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
1152 #endif /* CONFIG_40x */
1153
1154 #ifdef CONFIG_BOOKE
1155         .globl  ret_from_crit_exc
1156 ret_from_crit_exc:
1157         mfspr   r9,SPRN_SPRG_THREAD
1158         lwz     r10,SAVED_KSP_LIMIT(r1)
1159         stw     r10,KSP_LIMIT(r9)
1160         RESTORE_xSRR(SRR0,SRR1);
1161         RESTORE_MMU_REGS;
1162         RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
1163
1164         .globl  ret_from_debug_exc
1165 ret_from_debug_exc:
1166         mfspr   r9,SPRN_SPRG_THREAD
1167         lwz     r10,SAVED_KSP_LIMIT(r1)
1168         stw     r10,KSP_LIMIT(r9)
1169         lwz     r9,THREAD_INFO-THREAD(r9)
1170         CURRENT_THREAD_INFO(r10, r1)
1171         lwz     r10,TI_PREEMPT(r10)
1172         stw     r10,TI_PREEMPT(r9)
1173         RESTORE_xSRR(SRR0,SRR1);
1174         RESTORE_xSRR(CSRR0,CSRR1);
1175         RESTORE_MMU_REGS;
1176         RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, PPC_RFDI)
1177
1178         .globl  ret_from_mcheck_exc
1179 ret_from_mcheck_exc:
1180         mfspr   r9,SPRN_SPRG_THREAD
1181         lwz     r10,SAVED_KSP_LIMIT(r1)
1182         stw     r10,KSP_LIMIT(r9)
1183         RESTORE_xSRR(SRR0,SRR1);
1184         RESTORE_xSRR(CSRR0,CSRR1);
1185         RESTORE_xSRR(DSRR0,DSRR1);
1186         RESTORE_MMU_REGS;
1187         RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, PPC_RFMCI)
1188 #endif /* CONFIG_BOOKE */
1189
1190 /*
1191  * Load the DBCR0 value for a task that is being ptraced,
1192  * having first saved away the global DBCR0.  Note that r0
1193  * has the dbcr0 value to set upon entry to this.
1194  */
1195 load_dbcr0:
1196         mfmsr   r10             /* first disable debug exceptions */
1197         rlwinm  r10,r10,0,~MSR_DE
1198         mtmsr   r10
1199         isync
1200         mfspr   r10,SPRN_DBCR0
1201         lis     r11,global_dbcr0@ha
1202         addi    r11,r11,global_dbcr0@l
1203 #ifdef CONFIG_SMP
1204         CURRENT_THREAD_INFO(r9, r1)
1205         lwz     r9,TI_CPU(r9)
1206         slwi    r9,r9,3
1207         add     r11,r11,r9
1208 #endif
1209         stw     r10,0(r11)
1210         mtspr   SPRN_DBCR0,r0
1211         lwz     r10,4(r11)
1212         addi    r10,r10,1
1213         stw     r10,4(r11)
1214         li      r11,-1
1215         mtspr   SPRN_DBSR,r11   /* clear all pending debug events */
1216         blr
1217
1218         .section .bss
1219         .align  4
1220 global_dbcr0:
1221         .space  8*NR_CPUS
1222         .previous
1223 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
1224
1225 do_work:                        /* r10 contains MSR_KERNEL here */
1226         andi.   r0,r9,_TIF_NEED_RESCHED
1227         beq     do_user_signal
1228
1229 do_resched:                     /* r10 contains MSR_KERNEL here */
1230         /* Note: We don't need to inform lockdep that we are enabling
1231          * interrupts here. As far as it knows, they are already enabled
1232          */
1233         ori     r10,r10,MSR_EE
1234         SYNC
1235         MTMSRD(r10)             /* hard-enable interrupts */
1236         bl      schedule
1237 recheck:
1238         /* Note: And we don't tell it we are disabling them again
1239          * neither. Those disable/enable cycles used to peek at
1240          * TI_FLAGS aren't advertised.
1241          */
1242         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
1243         SYNC
1244         MTMSRD(r10)             /* disable interrupts */
1245         CURRENT_THREAD_INFO(r9, r1)
1246         lwz     r9,TI_FLAGS(r9)
1247         andi.   r0,r9,_TIF_NEED_RESCHED
1248         bne-    do_resched
1249         andi.   r0,r9,_TIF_USER_WORK_MASK
1250         beq     restore_user
1251 do_user_signal:                 /* r10 contains MSR_KERNEL here */
1252         ori     r10,r10,MSR_EE
1253         SYNC
1254         MTMSRD(r10)             /* hard-enable interrupts */
1255         /* save r13-r31 in the exception frame, if not already done */
1256         lwz     r3,_TRAP(r1)
1257         andi.   r0,r3,1
1258         beq     2f
1259         SAVE_NVGPRS(r1)
1260         rlwinm  r3,r3,0,0,30
1261         stw     r3,_TRAP(r1)
1262 2:      addi    r3,r1,STACK_FRAME_OVERHEAD
1263         mr      r4,r9
1264         bl      do_notify_resume
1265         REST_NVGPRS(r1)
1266         b       recheck
1267
1268 /*
1269  * We come here when we are at the end of handling an exception
1270  * that occurred at a place where taking an exception will lose
1271  * state information, such as the contents of SRR0 and SRR1.
1272  */
1273 nonrecoverable:
1274         lis     r10,exc_exit_restart_end@ha
1275         addi    r10,r10,exc_exit_restart_end@l
1276         cmplw   r12,r10
1277         bge     3f
1278         lis     r11,exc_exit_restart@ha
1279         addi    r11,r11,exc_exit_restart@l
1280         cmplw   r12,r11
1281         blt     3f
1282         lis     r10,ee_restarts@ha
1283         lwz     r12,ee_restarts@l(r10)
1284         addi    r12,r12,1
1285         stw     r12,ee_restarts@l(r10)
1286         mr      r12,r11         /* restart at exc_exit_restart */
1287         blr
1288 3:      /* OK, we can't recover, kill this process */
1289         /* but the 601 doesn't implement the RI bit, so assume it's OK */
1290 BEGIN_FTR_SECTION
1291         blr
1292 END_FTR_SECTION_IFSET(CPU_FTR_601)
1293         lwz     r3,_TRAP(r1)
1294         andi.   r0,r3,1
1295         beq     4f
1296         SAVE_NVGPRS(r1)
1297         rlwinm  r3,r3,0,0,30
1298         stw     r3,_TRAP(r1)
1299 4:      addi    r3,r1,STACK_FRAME_OVERHEAD
1300         bl      nonrecoverable_exception
1301         /* shouldn't return */
1302         b       4b
1303
1304         .section .bss
1305         .align  2
1306 ee_restarts:
1307         .space  4
1308         .previous
1309
1310 /*
1311  * PROM code for specific machines follows.  Put it
1312  * here so it's easy to add arch-specific sections later.
1313  * -- Cort
1314  */
1315 #ifdef CONFIG_PPC_RTAS
1316 /*
1317  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
1318  * called with the MMU off.
1319  */
1320 _GLOBAL(enter_rtas)
1321         stwu    r1,-INT_FRAME_SIZE(r1)
1322         mflr    r0
1323         stw     r0,INT_FRAME_SIZE+4(r1)
1324         LOAD_REG_ADDR(r4, rtas)
1325         lis     r6,1f@ha        /* physical return address for rtas */
1326         addi    r6,r6,1f@l
1327         tophys(r6,r6)
1328         tophys(r7,r1)
1329         lwz     r8,RTASENTRY(r4)
1330         lwz     r4,RTASBASE(r4)
1331         mfmsr   r9
1332         stw     r9,8(r1)
1333         LOAD_MSR_KERNEL(r0,MSR_KERNEL)
1334         SYNC                    /* disable interrupts so SRR0/1 */
1335         MTMSRD(r0)              /* don't get trashed */
1336         li      r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
1337         mtlr    r6
1338         mtspr   SPRN_SPRG_RTAS,r7
1339         mtspr   SPRN_SRR0,r8
1340         mtspr   SPRN_SRR1,r9
1341         RFI
1342 1:      tophys(r9,r1)
1343         lwz     r8,INT_FRAME_SIZE+4(r9) /* get return address */
1344         lwz     r9,8(r9)        /* original msr value */
1345         addi    r1,r1,INT_FRAME_SIZE
1346         li      r0,0
1347         mtspr   SPRN_SPRG_RTAS,r0
1348         mtspr   SPRN_SRR0,r8
1349         mtspr   SPRN_SRR1,r9
1350         RFI                     /* return to caller */
1351
1352         .globl  machine_check_in_rtas
1353 machine_check_in_rtas:
1354         twi     31,0,0
1355         /* XXX load up BATs and panic */
1356
1357 #endif /* CONFIG_PPC_RTAS */