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