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