vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc / kernel / entry.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/config.h>
23 #include <linux/errno.h>
24 #include <linux/sys.h>
25 #include <linux/threads.h>
26 #include <asm/processor.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/offsets.h>
33 #include <asm/unistd.h>
34
35 #undef SHOW_SYSCALLS
36 #undef SHOW_SYSCALLS_TASK
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 #ifdef CONFIG_BOOKE
48 #define COR     r8      /* Critical Offset Register (COR) */
49 #define BOOKE_LOAD_COR  lis COR,crit_save@ha
50 #define BOOKE_REST_COR  mfspr COR,SPRG2
51 #define BOOKE_SAVE_COR  mtspr SPRG2,COR
52 #else
53 #define COR     0
54 #define BOOKE_LOAD_COR
55 #define BOOKE_REST_COR
56 #define BOOKE_SAVE_COR
57 #endif
58
59 #ifdef CONFIG_BOOKE
60         .globl  mcheck_transfer_to_handler
61 mcheck_transfer_to_handler:
62         mtspr   SPRG6W,r8
63         lis     r8,mcheck_save@ha
64         lwz     r0,mcheck_r10@l(r8)
65         stw     r0,GPR10(r11)
66         lwz     r0,mcheck_r11@l(r8)
67         stw     r0,GPR11(r11)
68         mfspr   r8,SPRG6R
69         b       transfer_to_handler_full
70 #endif
71
72 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
73         .globl  crit_transfer_to_handler
74 crit_transfer_to_handler:
75         BOOKE_SAVE_COR
76         BOOKE_LOAD_COR
77         lwz     r0,crit_r10@l(COR)
78         stw     r0,GPR10(r11)
79         lwz     r0,crit_r11@l(COR)
80         stw     r0,GPR11(r11)
81         BOOKE_REST_COR
82         /* fall through */
83 #endif
84
85 /*
86  * This code finishes saving the registers to the exception frame
87  * and jumps to the appropriate handler for the exception, turning
88  * on address translation.
89  * Note that we rely on the caller having set cr0.eq iff the exception
90  * occurred in kernel mode (i.e. MSR:PR = 0).
91  */
92         .globl  transfer_to_handler_full
93 transfer_to_handler_full:
94         SAVE_NVGPRS(r11)
95         /* fall through */
96
97         .globl  transfer_to_handler
98 transfer_to_handler:
99         stw     r2,GPR2(r11)
100         stw     r12,_NIP(r11)
101         stw     r9,_MSR(r11)
102         andi.   r2,r9,MSR_PR
103         mfctr   r12
104         mfspr   r2,XER
105         stw     r12,_CTR(r11)
106         stw     r2,_XER(r11)
107         mfspr   r12,SPRG3
108         addi    r2,r12,-THREAD
109         tovirt(r2,r2)                   /* set r2 to current */
110         beq     2f                      /* if from user, fix up THREAD.regs */
111         addi    r11,r1,STACK_FRAME_OVERHEAD
112         stw     r11,PT_REGS(r12)
113 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
114         /* Check to see if the dbcr0 register is set up to debug.  Use the
115            single-step bit to do this. */
116         lwz     r12,THREAD_DBCR0(r12)
117         andis.  r12,r12,DBCR0_IC@h
118         beq+    3f
119         /* From user and task is ptraced - load up global dbcr0 */
120         li      r12,-1                  /* clear all pending debug events */
121         mtspr   SPRN_DBSR,r12
122         lis     r11,global_dbcr0@ha
123         tophys(r11,r11)
124         addi    r11,r11,global_dbcr0@l
125         lwz     r12,0(r11)
126         mtspr   SPRN_DBCR0,r12
127         lwz     r12,4(r11)
128         addi    r12,r12,-1
129         stw     r12,4(r11)
130 #endif
131         b       3f
132 2:      /* if from kernel, check interrupted DOZE/NAP mode and
133          * check for stack overflow
134          */
135 #ifdef CONFIG_6xx
136         mfspr   r11,SPRN_HID0
137         mtcr    r11
138 BEGIN_FTR_SECTION
139         bt-     8,power_save_6xx_restore        /* Check DOZE */
140 END_FTR_SECTION_IFSET(CPU_FTR_CAN_DOZE)
141 BEGIN_FTR_SECTION
142         bt-     9,power_save_6xx_restore        /* Check NAP */
143 END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)
144 #endif /* CONFIG_6xx */
145         .globl transfer_to_handler_cont
146 transfer_to_handler_cont:
147         lwz     r11,THREAD_INFO-THREAD(r12)
148         cmplw   r1,r11                  /* if r1 <= current->thread_info */
149         ble-    stack_ovf               /* then the kernel stack overflowed */
150 3:
151         mflr    r9
152         lwz     r11,0(r9)               /* virtual address of handler */
153         lwz     r9,4(r9)                /* where to go when done */
154         FIX_SRR1(r10,r12)
155         mtspr   SRR0,r11
156         mtspr   SRR1,r10
157         mtlr    r9
158         SYNC
159         RFI                             /* jump to handler, enable MMU */
160
161 /*
162  * On kernel stack overflow, load up an initial stack pointer
163  * and call StackOverflow(regs), which should not return.
164  */
165 stack_ovf:
166         /* sometimes we use a statically-allocated stack, which is OK. */
167         lis     r11,_end@h
168         ori     r11,r11,_end@l
169         cmplw   r1,r11
170         ble     3b                      /* r1 <= &_end is OK */
171         SAVE_NVGPRS(r11)
172         addi    r3,r1,STACK_FRAME_OVERHEAD
173         lis     r1,init_thread_union@ha
174         addi    r1,r1,init_thread_union@l
175         addi    r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
176         lis     r9,StackOverflow@ha
177         addi    r9,r9,StackOverflow@l
178         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
179         FIX_SRR1(r10,r12)
180         mtspr   SRR0,r9
181         mtspr   SRR1,r10
182         SYNC
183         RFI
184
185 /*
186  * Handle a system call.
187  */
188         .stabs  "arch/ppc/kernel/",N_SO,0,0,0f
189         .stabs  "entry.S",N_SO,0,0,0f
190 0:
191
192 _GLOBAL(DoSyscall)
193         stw     r0,THREAD+LAST_SYSCALL(r2)
194         stw     r3,ORIG_GPR3(r1)
195         li      r12,0
196         stw     r12,RESULT(r1)
197         lwz     r11,_CCR(r1)    /* Clear SO bit in CR */
198         rlwinm  r11,r11,0,4,2
199         stw     r11,_CCR(r1)
200 #ifdef SHOW_SYSCALLS
201         bl      do_show_syscall
202 #endif /* SHOW_SYSCALLS */
203         rlwinm  r10,r1,0,0,18   /* current_thread_info() */
204         lwz     r11,TI_LOCAL_FLAGS(r10)
205         rlwinm  r11,r11,0,~_TIFL_FORCE_NOERROR
206         stw     r11,TI_LOCAL_FLAGS(r10)
207         lwz     r11,TI_FLAGS(r10)
208         andi.   r11,r11,_TIF_SYSCALL_TRACE
209         bne-    syscall_dotrace
210 syscall_dotrace_cont:
211         cmplwi  0,r0,NR_syscalls
212         lis     r10,sys_call_table@h
213         ori     r10,r10,sys_call_table@l
214         slwi    r0,r0,2
215         bge-    66f
216         lwzx    r10,r10,r0      /* Fetch system call handler [ptr] */
217         mtlr    r10
218         addi    r9,r1,STACK_FRAME_OVERHEAD
219         blrl                    /* Call handler */
220         .globl  ret_from_syscall
221 ret_from_syscall:
222 #ifdef SHOW_SYSCALLS
223         bl      do_show_syscall_exit
224 #endif
225         mr      r6,r3
226         li      r11,-_LAST_ERRNO
227         cmplw   0,r3,r11
228         rlwinm  r12,r1,0,0,18   /* current_thread_info() */
229         blt+    30f
230         lwz     r11,TI_LOCAL_FLAGS(r12)
231         andi.   r11,r11,_TIFL_FORCE_NOERROR
232         bne     30f
233         neg     r3,r3
234         lwz     r10,_CCR(r1)    /* Set SO bit in CR */
235         oris    r10,r10,0x1000
236         stw     r10,_CCR(r1)
237
238         /* disable interrupts so current_thread_info()->flags can't change */
239 30:     LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
240         SYNC
241         MTMSRD(r10)
242         lwz     r9,TI_FLAGS(r12)
243         andi.   r0,r9,(_TIF_SYSCALL_TRACE|_TIF_SIGPENDING|_TIF_NEED_RESCHED)
244         bne-    syscall_exit_work
245 syscall_exit_cont:
246 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
247         /* If the process has its own DBCR0 value, load it up.  The single
248            step bit tells us that dbcr0 should be loaded. */
249         lwz     r0,THREAD+THREAD_DBCR0(r2)
250         andis.  r10,r0,DBCR0_IC@h
251         bnel-   load_dbcr0
252 #endif
253         stwcx.  r0,0,r1                 /* to clear the reservation */
254         lwz     r4,_LINK(r1)
255         lwz     r5,_CCR(r1)
256         mtlr    r4
257         mtcr    r5
258         lwz     r7,_NIP(r1)
259         lwz     r8,_MSR(r1)
260         FIX_SRR1(r8, r0)
261         lwz     r2,GPR2(r1)
262         lwz     r1,GPR1(r1)
263         mtspr   SRR0,r7
264         mtspr   SRR1,r8
265         SYNC
266         RFI
267
268 66:     li      r3,-ENOSYS
269         b       ret_from_syscall
270
271         .globl  ret_from_fork
272 ret_from_fork:
273         REST_NVGPRS(r1)
274         bl      schedule_tail
275         li      r3,0
276         b       ret_from_syscall
277
278 /* Traced system call support */
279 syscall_dotrace:
280         SAVE_NVGPRS(r1)
281         li      r0,0xc00
282         stw     r0,TRAP(r1)
283         bl      do_syscall_trace
284         lwz     r0,GPR0(r1)     /* Restore original registers */
285         lwz     r3,GPR3(r1)
286         lwz     r4,GPR4(r1)
287         lwz     r5,GPR5(r1)
288         lwz     r6,GPR6(r1)
289         lwz     r7,GPR7(r1)
290         lwz     r8,GPR8(r1)
291         REST_NVGPRS(r1)
292         b       syscall_dotrace_cont
293
294 syscall_exit_work:
295         stw     r6,RESULT(r1)   /* Save result */
296         stw     r3,GPR3(r1)     /* Update return value */
297         andi.   r0,r9,_TIF_SYSCALL_TRACE
298         beq     5f
299         ori     r10,r10,MSR_EE
300         SYNC
301         MTMSRD(r10)             /* re-enable interrupts */
302         lwz     r4,TRAP(r1)
303         andi.   r4,r4,1
304         beq     4f
305         SAVE_NVGPRS(r1)
306         li      r4,0xc00
307         stw     r4,TRAP(r1)
308 4:
309         bl      do_syscall_trace
310         REST_NVGPRS(r1)
311 2:
312         lwz     r3,GPR3(r1)
313         LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
314         SYNC
315         MTMSRD(r10)             /* disable interrupts again */
316         rlwinm  r12,r1,0,0,18   /* current_thread_info() */
317         lwz     r9,TI_FLAGS(r12)
318 5:
319         andi.   r0,r9,_TIF_NEED_RESCHED
320         bne     1f
321         lwz     r5,_MSR(r1)
322         andi.   r5,r5,MSR_PR
323         beq     syscall_exit_cont
324         andi.   r0,r9,_TIF_SIGPENDING
325         beq     syscall_exit_cont
326         b       do_user_signal
327 1:
328         ori     r10,r10,MSR_EE
329         SYNC
330         MTMSRD(r10)             /* re-enable interrupts */
331         bl      schedule
332         b       2b
333
334 #ifdef SHOW_SYSCALLS
335 do_show_syscall:
336 #ifdef SHOW_SYSCALLS_TASK
337         lis     r11,show_syscalls_task@ha
338         lwz     r11,show_syscalls_task@l(r11)
339         cmp     0,r2,r11
340         bnelr
341 #endif
342         stw     r31,GPR31(r1)
343         mflr    r31
344         lis     r3,7f@ha
345         addi    r3,r3,7f@l
346         lwz     r4,GPR0(r1)
347         lwz     r5,GPR3(r1)
348         lwz     r6,GPR4(r1)
349         lwz     r7,GPR5(r1)
350         lwz     r8,GPR6(r1)
351         lwz     r9,GPR7(r1)
352         bl      printk
353         lis     r3,77f@ha
354         addi    r3,r3,77f@l
355         lwz     r4,GPR8(r1)
356         mr      r5,r2
357         bl      printk
358         lwz     r0,GPR0(r1)
359         lwz     r3,GPR3(r1)
360         lwz     r4,GPR4(r1)
361         lwz     r5,GPR5(r1)
362         lwz     r6,GPR6(r1)
363         lwz     r7,GPR7(r1)
364         lwz     r8,GPR8(r1)
365         mtlr    r31
366         lwz     r31,GPR31(r1)
367         blr
368
369 do_show_syscall_exit:
370 #ifdef SHOW_SYSCALLS_TASK
371         lis     r11,show_syscalls_task@ha
372         lwz     r11,show_syscalls_task@l(r11)
373         cmp     0,r2,r11
374         bnelr
375 #endif
376         stw     r31,GPR31(r1)
377         mflr    r31
378         stw     r3,RESULT(r1)   /* Save result */
379         mr      r4,r3
380         lis     r3,79f@ha
381         addi    r3,r3,79f@l
382         bl      printk
383         lwz     r3,RESULT(r1)
384         mtlr    r31
385         lwz     r31,GPR31(r1)
386         blr
387
388 7:      .string "syscall %d(%x, %x, %x, %x, %x, "
389 77:     .string "%x), current=%p\n"
390 79:     .string " -> %x\n"
391         .align  2,0
392
393 #ifdef SHOW_SYSCALLS_TASK
394         .data
395         .globl  show_syscalls_task
396 show_syscalls_task:
397         .long   -1
398         .text
399 #endif
400 #endif /* SHOW_SYSCALLS */
401
402 /*
403  * The sigsuspend and rt_sigsuspend system calls can call do_signal
404  * and thus put the process into the stopped state where we might
405  * want to examine its user state with ptrace.  Therefore we need
406  * to save all the nonvolatile registers (r13 - r31) before calling
407  * the C code.
408  */
409         .globl  ppc_sigsuspend
410 ppc_sigsuspend:
411         SAVE_NVGPRS(r1)
412         lwz     r0,TRAP(r1)
413         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
414         stw     r0,TRAP(r1)             /* register set saved */
415         b       sys_sigsuspend
416
417         .globl  ppc_rt_sigsuspend
418 ppc_rt_sigsuspend:
419         SAVE_NVGPRS(r1)
420         lwz     r0,TRAP(r1)
421         rlwinm  r0,r0,0,0,30
422         stw     r0,TRAP(r1)
423         b       sys_rt_sigsuspend
424
425         .globl  ppc_fork
426 ppc_fork:
427         SAVE_NVGPRS(r1)
428         lwz     r0,TRAP(r1)
429         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
430         stw     r0,TRAP(r1)             /* register set saved */
431         b       sys_fork
432
433         .globl  ppc_vfork
434 ppc_vfork:
435         SAVE_NVGPRS(r1)
436         lwz     r0,TRAP(r1)
437         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
438         stw     r0,TRAP(r1)             /* register set saved */
439         b       sys_vfork
440
441         .globl  ppc_clone
442 ppc_clone:
443         SAVE_NVGPRS(r1)
444         lwz     r0,TRAP(r1)
445         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
446         stw     r0,TRAP(r1)             /* register set saved */
447         b       sys_clone
448
449         .globl  ppc_swapcontext
450 ppc_swapcontext:
451         SAVE_NVGPRS(r1)
452         lwz     r0,TRAP(r1)
453         rlwinm  r0,r0,0,0,30            /* clear LSB to indicate full */
454         stw     r0,TRAP(r1)             /* register set saved */
455         b       sys_swapcontext
456
457 /*
458  * Top-level page fault handling.
459  * This is in assembler because if do_page_fault tells us that
460  * it is a bad kernel page fault, we want to save the non-volatile
461  * registers before calling bad_page_fault.
462  */
463         .globl  handle_page_fault
464 handle_page_fault:
465         stw     r4,_DAR(r1)
466         addi    r3,r1,STACK_FRAME_OVERHEAD
467         bl      do_page_fault
468         cmpwi   r3,0
469         beq+    ret_from_except
470         SAVE_NVGPRS(r1)
471         lwz     r0,TRAP(r1)
472         clrrwi  r0,r0,1
473         stw     r0,TRAP(r1)
474         mr      r5,r3
475         addi    r3,r1,STACK_FRAME_OVERHEAD
476         lwz     r4,_DAR(r1)
477         bl      bad_page_fault
478         b       ret_from_except_full
479
480 /*
481  * This routine switches between two different tasks.  The process
482  * state of one is saved on its kernel stack.  Then the state
483  * of the other is restored from its kernel stack.  The memory
484  * management hardware is updated to the second process's state.
485  * Finally, we can return to the second process.
486  * On entry, r3 points to the THREAD for the current task, r4
487  * points to the THREAD for the new task.
488  *
489  * This routine is always called with interrupts disabled.
490  *
491  * Note: there are two ways to get to the "going out" portion
492  * of this code; either by coming in via the entry (_switch)
493  * or via "fork" which must set up an environment equivalent
494  * to the "_switch" path.  If you change this , you'll have to
495  * change the fork code also.
496  *
497  * The code which creates the new task context is in 'copy_thread'
498  * in arch/ppc/kernel/process.c
499  */
500 _GLOBAL(_switch)
501         stwu    r1,-INT_FRAME_SIZE(r1)
502         mflr    r0
503         stw     r0,INT_FRAME_SIZE+4(r1)
504         /* r3-r12 are caller saved -- Cort */
505         SAVE_NVGPRS(r1)
506         stw     r0,_NIP(r1)     /* Return to switch caller */
507         mfmsr   r11
508         li      r0,MSR_FP       /* Disable floating-point */
509 #ifdef CONFIG_ALTIVEC
510 BEGIN_FTR_SECTION
511         oris    r0,r0,MSR_VEC@h /* Disable altivec */
512         mfspr   r12,SPRN_VRSAVE /* save vrsave register value */
513         stw     r12,THREAD+THREAD_VRSAVE(r2)
514 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
515 #endif /* CONFIG_ALTIVEC */
516 #ifdef CONFIG_SPE
517         oris    r0,r0,MSR_SPE@h  /* Disable SPE */
518         mfspr   r12,SPRN_SPEFSCR /* save spefscr register value */
519         stw     r12,THREAD+THREAD_SPEFSCR(r2)
520 #endif /* CONFIG_SPE */
521         and.    r0,r0,r11       /* FP or altivec or SPE enabled? */
522         beq+    1f
523         andc    r11,r11,r0
524         MTMSRD(r11)
525         isync
526 1:      stw     r11,_MSR(r1)
527         mfcr    r10
528         stw     r10,_CCR(r1)
529         stw     r1,KSP(r3)      /* Set old stack pointer */
530
531 #ifdef CONFIG_SMP
532         /* We need a sync somewhere here to make sure that if the
533          * previous task gets rescheduled on another CPU, it sees all
534          * stores it has performed on this one.
535          */
536         sync
537 #endif /* CONFIG_SMP */
538
539         tophys(r0,r4)
540         CLR_TOP32(r0)
541         mtspr   SPRG3,r0        /* Update current THREAD phys addr */
542         lwz     r1,KSP(r4)      /* Load new stack pointer */
543
544         /* save the old current 'last' for return value */
545         mr      r3,r2
546         addi    r2,r4,-THREAD   /* Update current */
547
548 #ifdef CONFIG_ALTIVEC
549 BEGIN_FTR_SECTION
550         lwz     r0,THREAD+THREAD_VRSAVE(r2)
551         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
552 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
553 #endif /* CONFIG_ALTIVEC */
554 #ifdef CONFIG_SPE
555         lwz     r0,THREAD+THREAD_SPEFSCR(r2)
556         mtspr   SPRN_SPEFSCR,r0         /* restore SPEFSCR reg */
557 #endif /* CONFIG_SPE */
558
559         lwz     r0,_CCR(r1)
560         mtcrf   0xFF,r0
561         /* r3-r12 are destroyed -- Cort */
562         REST_NVGPRS(r1)
563
564         lwz     r4,_NIP(r1)     /* Return to _switch caller in new task */
565         mtlr    r4
566         addi    r1,r1,INT_FRAME_SIZE
567         blr
568
569         .globl  sigreturn_exit
570 sigreturn_exit:
571         subi    r1,r3,STACK_FRAME_OVERHEAD
572         rlwinm  r12,r1,0,0,18   /* current_thread_info() */
573         lwz     r9,TI_FLAGS(r12)
574         andi.   r0,r9,_TIF_SYSCALL_TRACE
575         bnel-   do_syscall_trace
576         /* fall through */
577
578         .globl  ret_from_except_full
579 ret_from_except_full:
580         REST_NVGPRS(r1)
581         /* fall through */
582
583         .globl  ret_from_except
584 ret_from_except:
585         /* Hard-disable interrupts so that current_thread_info()->flags
586          * can't change between when we test it and when we return
587          * from the interrupt. */
588         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
589         SYNC                    /* Some chip revs have problems here... */
590         MTMSRD(r10)             /* disable interrupts */
591
592         lwz     r3,_MSR(r1)     /* Returning to user mode? */
593         andi.   r0,r3,MSR_PR
594         beq     resume_kernel
595
596 user_exc_return:                /* r10 contains MSR_KERNEL here */
597         /* Check current_thread_info()->flags */
598         rlwinm  r9,r1,0,0,18
599         lwz     r9,TI_FLAGS(r9)
600         andi.   r0,r9,(_TIF_SIGPENDING|_TIF_NEED_RESCHED)
601         bne     do_work
602
603 restore_user:
604 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
605         /* Check whether this process has its own DBCR0 value.  The single
606            step bit tells us that dbcr0 should be loaded. */
607         lwz     r0,THREAD+THREAD_DBCR0(r2)
608         andis.  r10,r0,DBCR0_IC@h
609         bnel-   load_dbcr0
610 #endif
611
612 #ifdef CONFIG_PREEMPT
613         b       restore
614
615 /* N.B. the only way to get here is from the beq following ret_from_except. */
616 resume_kernel:
617         /* check current_thread_info->preempt_count */
618         rlwinm  r9,r1,0,0,18
619         lwz     r0,TI_PREEMPT(r9)
620         cmpwi   0,r0,0          /* if non-zero, just restore regs and return */
621         bne     restore
622         lwz     r0,TI_FLAGS(r9)
623         andi.   r0,r0,_TIF_NEED_RESCHED
624         beq+    restore
625         andi.   r0,r3,MSR_EE    /* interrupts off? */
626         beq     restore         /* don't schedule if so */
627 1:      bl      preempt_schedule_irq
628         rlwinm  r9,r1,0,0,18
629         lwz     r3,TI_FLAGS(r9)
630         andi.   r0,r3,_TIF_NEED_RESCHED
631         bne-    1b
632 #else
633 resume_kernel:
634 #endif /* CONFIG_PREEMPT */
635
636         /* interrupts are hard-disabled at this point */
637 restore:
638         lwz     r0,GPR0(r1)
639         lwz     r2,GPR2(r1)
640         REST_4GPRS(3, r1)
641         REST_2GPRS(7, r1)
642
643         lwz     r10,_XER(r1)
644         lwz     r11,_CTR(r1)
645         mtspr   XER,r10
646         mtctr   r11
647
648         PPC405_ERR77(0,r1)
649         stwcx.  r0,0,r1                 /* to clear the reservation */
650
651 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
652         lwz     r9,_MSR(r1)
653         andi.   r10,r9,MSR_RI           /* check if this exception occurred */
654         beql    nonrecoverable          /* at a bad place (MSR:RI = 0) */
655
656         lwz     r10,_CCR(r1)
657         lwz     r11,_LINK(r1)
658         mtcrf   0xFF,r10
659         mtlr    r11
660
661         /*
662          * Once we put values in SRR0 and SRR1, we are in a state
663          * where exceptions are not recoverable, since taking an
664          * exception will trash SRR0 and SRR1.  Therefore we clear the
665          * MSR:RI bit to indicate this.  If we do take an exception,
666          * we can't return to the point of the exception but we
667          * can restart the exception exit path at the label
668          * exc_exit_restart below.  -- paulus
669          */
670         LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
671         SYNC
672         MTMSRD(r10)             /* clear the RI bit */
673         .globl exc_exit_restart
674 exc_exit_restart:
675         lwz     r9,_MSR(r1)
676         lwz     r12,_NIP(r1)
677         FIX_SRR1(r9,r10)
678         mtspr   SRR0,r12
679         mtspr   SRR1,r9
680         REST_4GPRS(9, r1)
681         lwz     r1,GPR1(r1)
682         .globl exc_exit_restart_end
683 exc_exit_restart_end:
684         SYNC
685         RFI
686
687 #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
688         /*
689          * This is a bit different on 4xx/Book-E because it doesn't have
690          * the RI bit in the MSR.
691          * The TLB miss handler checks if we have interrupted
692          * the exception exit path and restarts it if so
693          * (well maybe one day it will... :).
694          */
695         lwz     r11,_LINK(r1)
696         mtlr    r11
697         lwz     r10,_CCR(r1)
698         mtcrf   0xff,r10
699         REST_2GPRS(9, r1)
700         .globl exc_exit_restart
701 exc_exit_restart:
702         lwz     r11,_NIP(r1)
703         lwz     r12,_MSR(r1)
704 exc_exit_start:
705         mtspr   SRR0,r11
706         mtspr   SRR1,r12
707         REST_2GPRS(11, r1)
708         lwz     r1,GPR1(r1)
709         .globl exc_exit_restart_end
710 exc_exit_restart_end:
711         PPC405_ERR77_SYNC
712         rfi
713         b       .                       /* prevent prefetch past rfi */
714
715 /*
716  * Returning from a critical interrupt in user mode doesn't need
717  * to be any different from a normal exception.  For a critical
718  * interrupt in the kernel, we just return (without checking for
719  * preemption) since the interrupt may have happened at some crucial
720  * place (e.g. inside the TLB miss handler), and because we will be
721  * running with r1 pointing into critical_stack, not the current
722  * process's kernel stack (and therefore current_thread_info() will
723  * give the wrong answer).
724  * We have to restore various SPRs that may have been in use at the
725  * time of the critical interrupt.
726  *
727  * Note that SPRG6 is used for machine check on CONFIG_BOOKE parts and
728  * thus not saved in the critical handler
729  */
730         .globl  ret_from_crit_exc
731 ret_from_crit_exc:
732         REST_NVGPRS(r1)
733         lwz     r3,_MSR(r1)
734         andi.   r3,r3,MSR_PR
735         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
736         bne     user_exc_return
737
738         lwz     r0,GPR0(r1)
739         lwz     r2,GPR2(r1)
740         REST_4GPRS(3, r1)
741         REST_2GPRS(7, r1)
742
743         lwz     r10,_XER(r1)
744         lwz     r11,_CTR(r1)
745         mtspr   XER,r10
746         mtctr   r11
747
748         PPC405_ERR77(0,r1)
749         stwcx.  r0,0,r1                 /* to clear the reservation */
750
751         lwz     r11,_LINK(r1)
752         mtlr    r11
753         lwz     r10,_CCR(r1)
754         mtcrf   0xff,r10
755 #ifdef CONFIG_40x
756         /* avoid any possible TLB misses here by turning off MSR.DR, we
757          * assume the instructions here are mapped by a pinned TLB entry */
758         li      r10,MSR_IR
759         mtmsr   r10
760         isync
761         tophys(r1, r1)
762 #endif
763         lwz     r9,_DEAR(r1)
764         lwz     r10,_ESR(r1)
765         mtspr   SPRN_DEAR,r9
766         mtspr   SPRN_ESR,r10
767         lwz     r11,_NIP(r1)
768         lwz     r12,_MSR(r1)
769         mtspr   CSRR0,r11
770         mtspr   CSRR1,r12
771         lwz     r9,GPR9(r1)
772         lwz     r12,GPR12(r1)
773         BOOKE_SAVE_COR
774         BOOKE_LOAD_COR
775         lwz     r10,crit_sprg0@l(COR)
776         mtspr   SPRN_SPRG0,r10
777         lwz     r10,crit_sprg1@l(COR)
778         mtspr   SPRN_SPRG1,r10
779         lwz     r10,crit_sprg4@l(COR)
780         mtspr   SPRN_SPRG4,r10
781         lwz     r10,crit_sprg5@l(COR)
782         mtspr   SPRN_SPRG5,r10
783 #ifdef CONFIG_40x
784         lwz     r10,crit_sprg6@l(COR)
785         mtspr   SPRN_SPRG6,r10
786 #endif
787         lwz     r10,crit_sprg7@l(COR)
788         mtspr   SPRN_SPRG7,r10
789         lwz     r10,crit_srr0@l(COR)
790         mtspr   SRR0,r10
791         lwz     r10,crit_srr1@l(COR)
792         mtspr   SRR1,r10
793         lwz     r10,crit_pid@l(COR)
794         mtspr   SPRN_PID,r10
795         lwz     r10,GPR10(r1)
796         lwz     r11,GPR11(r1)
797         lwz     r1,GPR1(r1)
798         BOOKE_REST_COR
799         PPC405_ERR77_SYNC
800         rfci
801         b       .               /* prevent prefetch past rfci */
802
803 #ifdef CONFIG_BOOKE
804 /*
805  * Return from a machine check interrupt, similar to a critical
806  * interrupt.
807  */
808         .globl  ret_from_mcheck_exc
809 ret_from_mcheck_exc:
810         REST_NVGPRS(r1)
811         lwz     r3,_MSR(r1)
812         andi.   r3,r3,MSR_PR
813         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
814         bne     user_exc_return
815
816         lwz     r0,GPR0(r1)
817         lwz     r2,GPR2(r1)
818         REST_4GPRS(3, r1)
819         REST_2GPRS(7, r1)
820
821         lwz     r10,_XER(r1)
822         lwz     r11,_CTR(r1)
823         mtspr   XER,r10
824         mtctr   r11
825
826         stwcx.  r0,0,r1                 /* to clear the reservation */
827
828         lwz     r11,_LINK(r1)
829         mtlr    r11
830         lwz     r10,_CCR(r1)
831         mtcrf   0xff,r10
832         lwz     r9,_DEAR(r1)
833         lwz     r10,_ESR(r1)
834         mtspr   SPRN_DEAR,r9
835         mtspr   SPRN_ESR,r10
836         lwz     r11,_NIP(r1)
837         lwz     r12,_MSR(r1)
838         mtspr   MCSRR0,r11
839         mtspr   MCSRR1,r12
840         lwz     r9,GPR9(r1)
841         lwz     r12,GPR12(r1)
842         mtspr   SPRG6W,r8
843         lis     r8,mcheck_save@ha
844         lwz     r10,mcheck_sprg0@l(r8)
845         mtspr   SPRN_SPRG0,r10
846         lwz     r10,mcheck_sprg1@l(r8)
847         mtspr   SPRN_SPRG1,r10
848         lwz     r10,mcheck_sprg4@l(r8)
849         mtspr   SPRN_SPRG4,r10
850         lwz     r10,mcheck_sprg5@l(r8)
851         mtspr   SPRN_SPRG5,r10
852         lwz     r10,mcheck_sprg7@l(r8)
853         mtspr   SPRN_SPRG7,r10
854         lwz     r10,mcheck_srr0@l(r8)
855         mtspr   SRR0,r10
856         lwz     r10,mcheck_srr1@l(r8)
857         mtspr   SRR1,r10
858         lwz     r10,mcheck_csrr0@l(r8)
859         mtspr   CSRR0,r10
860         lwz     r10,mcheck_csrr1@l(r8)
861         mtspr   CSRR1,r10
862         lwz     r10,mcheck_pid@l(r8)
863         mtspr   SPRN_PID,r10
864         lwz     r10,GPR10(r1)
865         lwz     r11,GPR11(r1)
866         lwz     r1,GPR1(r1)
867         mfspr   r8,SPRG6R
868         RFMCI
869 #endif /* CONFIG_BOOKE */
870
871 /*
872  * Load the DBCR0 value for a task that is being ptraced,
873  * having first saved away the global DBCR0.  Note that r0
874  * has the dbcr0 value to set upon entry to this.
875  */
876 load_dbcr0:
877         mfmsr   r10             /* first disable debug exceptions */
878         rlwinm  r10,r10,0,~MSR_DE
879         mtmsr   r10
880         isync
881         mfspr   r10,SPRN_DBCR0
882         lis     r11,global_dbcr0@ha
883         addi    r11,r11,global_dbcr0@l
884         stw     r10,0(r11)
885         mtspr   SPRN_DBCR0,r0
886         lwz     r10,4(r11)
887         addi    r10,r10,1
888         stw     r10,4(r11)
889         li      r11,-1
890         mtspr   SPRN_DBSR,r11   /* clear all pending debug events */
891         blr
892
893         .comm   global_dbcr0,8
894 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
895
896 do_work:                        /* r10 contains MSR_KERNEL here */
897         andi.   r0,r9,_TIF_NEED_RESCHED
898         beq     do_user_signal
899
900 do_resched:                     /* r10 contains MSR_KERNEL here */
901         ori     r10,r10,MSR_EE
902         SYNC
903         MTMSRD(r10)             /* hard-enable interrupts */
904         bl      schedule
905 recheck:
906         LOAD_MSR_KERNEL(r10,MSR_KERNEL)
907         SYNC
908         MTMSRD(r10)             /* disable interrupts */
909         rlwinm  r9,r1,0,0,18
910         lwz     r9,TI_FLAGS(r9)
911         andi.   r0,r9,_TIF_NEED_RESCHED
912         bne-    do_resched
913         andi.   r0,r9,_TIF_SIGPENDING
914         beq     restore_user
915 do_user_signal:                 /* r10 contains MSR_KERNEL here */
916         ori     r10,r10,MSR_EE
917         SYNC
918         MTMSRD(r10)             /* hard-enable interrupts */
919         /* save r13-r31 in the exception frame, if not already done */
920         lwz     r3,TRAP(r1)
921         andi.   r0,r3,1
922         beq     2f
923         SAVE_NVGPRS(r1)
924         rlwinm  r3,r3,0,0,30
925         stw     r3,TRAP(r1)
926 2:      li      r3,0
927         addi    r4,r1,STACK_FRAME_OVERHEAD
928         bl      do_signal
929         REST_NVGPRS(r1)
930         b       recheck
931
932 /*
933  * We come here when we are at the end of handling an exception
934  * that occurred at a place where taking an exception will lose
935  * state information, such as the contents of SRR0 and SRR1.
936  */
937 nonrecoverable:
938         lis     r10,exc_exit_restart_end@ha
939         addi    r10,r10,exc_exit_restart_end@l
940         cmplw   r12,r10
941         bge     3f
942         lis     r11,exc_exit_restart@ha
943         addi    r11,r11,exc_exit_restart@l
944         cmplw   r12,r11
945         blt     3f
946         lis     r10,ee_restarts@ha
947         lwz     r12,ee_restarts@l(r10)
948         addi    r12,r12,1
949         stw     r12,ee_restarts@l(r10)
950         mr      r12,r11         /* restart at exc_exit_restart */
951         blr
952 3:      /* OK, we can't recover, kill this process */
953         /* but the 601 doesn't implement the RI bit, so assume it's OK */
954 BEGIN_FTR_SECTION
955         blr
956 END_FTR_SECTION_IFSET(CPU_FTR_601)
957         lwz     r3,TRAP(r1)
958         andi.   r0,r3,1
959         beq     4f
960         SAVE_NVGPRS(r1)
961         rlwinm  r3,r3,0,0,30
962         stw     r3,TRAP(r1)
963 4:      addi    r3,r1,STACK_FRAME_OVERHEAD
964         bl      nonrecoverable_exception
965         /* shouldn't return */
966         b       4b
967
968         .comm   ee_restarts,4
969
970 /*
971  * PROM code for specific machines follows.  Put it
972  * here so it's easy to add arch-specific sections later.
973  * -- Cort
974  */
975 #ifdef CONFIG_PPC_OF
976 /*
977  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
978  * called with the MMU off.
979  */
980 _GLOBAL(enter_rtas)
981         stwu    r1,-INT_FRAME_SIZE(r1)
982         mflr    r0
983         stw     r0,INT_FRAME_SIZE+4(r1)
984         lis     r4,rtas_data@ha
985         lwz     r4,rtas_data@l(r4)
986         lis     r6,1f@ha        /* physical return address for rtas */
987         addi    r6,r6,1f@l
988         tophys(r6,r6)
989         tophys(r7,r1)
990         lis     r8,rtas_entry@ha
991         lwz     r8,rtas_entry@l(r8)
992         mfmsr   r9
993         stw     r9,8(r1)
994         LOAD_MSR_KERNEL(r0,MSR_KERNEL)
995         SYNC                    /* disable interrupts so SRR0/1 */
996         MTMSRD(r0)              /* don't get trashed */
997         li      r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
998         mtlr    r6
999         CLR_TOP32(r7)
1000         mtspr   SPRG2,r7
1001         mtspr   SRR0,r8
1002         mtspr   SRR1,r9
1003         RFI
1004 1:      tophys(r9,r1)
1005         lwz     r8,INT_FRAME_SIZE+4(r9) /* get return address */
1006         lwz     r9,8(r9)        /* original msr value */
1007         FIX_SRR1(r9,r0)
1008         addi    r1,r1,INT_FRAME_SIZE
1009         li      r0,0
1010         mtspr   SPRG2,r0
1011         mtspr   SRR0,r8
1012         mtspr   SRR1,r9
1013         RFI                     /* return to caller */
1014
1015         .globl  machine_check_in_rtas
1016 machine_check_in_rtas:
1017         twi     31,0,0
1018         /* XXX load up BATs and panic */
1019
1020 #endif /* CONFIG_PPC_OF */