]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/kernel/entry_64.S
12eb95a80ce98c902eb60bae75f0cbae958d5655
[linux-2.6-omap-h63xx.git] / arch / powerpc / kernel / entry_64.S
1 /*
2  *  PowerPC version 
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
5  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
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 #include <linux/errno.h>
22 #include <asm/unistd.h>
23 #include <asm/processor.h>
24 #include <asm/page.h>
25 #include <asm/mmu.h>
26 #include <asm/thread_info.h>
27 #include <asm/ppc_asm.h>
28 #include <asm/asm-offsets.h>
29 #include <asm/cputable.h>
30 #include <asm/firmware.h>
31 #include <asm/bug.h>
32 #include <asm/ptrace.h>
33 #include <asm/irqflags.h>
34
35 /*
36  * System calls.
37  */
38         .section        ".toc","aw"
39 .SYS_CALL_TABLE:
40         .tc .sys_call_table[TC],.sys_call_table
41
42 /* This value is used to mark exception frames on the stack. */
43 exception_marker:
44         .tc     ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
45
46         .section        ".text"
47         .align 7
48
49 #undef SHOW_SYSCALLS
50
51         .globl system_call_common
52 system_call_common:
53         andi.   r10,r12,MSR_PR
54         mr      r10,r1
55         addi    r1,r1,-INT_FRAME_SIZE
56         beq-    1f
57         ld      r1,PACAKSAVE(r13)
58 1:      std     r10,0(r1)
59         crclr   so
60         std     r11,_NIP(r1)
61         std     r12,_MSR(r1)
62         std     r0,GPR0(r1)
63         std     r10,GPR1(r1)
64         ACCOUNT_CPU_USER_ENTRY(r10, r11)
65         std     r2,GPR2(r1)
66         std     r3,GPR3(r1)
67         std     r4,GPR4(r1)
68         std     r5,GPR5(r1)
69         std     r6,GPR6(r1)
70         std     r7,GPR7(r1)
71         std     r8,GPR8(r1)
72         li      r11,0
73         std     r11,GPR9(r1)
74         std     r11,GPR10(r1)
75         std     r11,GPR11(r1)
76         std     r11,GPR12(r1)
77         std     r9,GPR13(r1)
78         mfcr    r9
79         mflr    r10
80         li      r11,0xc01
81         std     r9,_CCR(r1)
82         std     r10,_LINK(r1)
83         std     r11,_TRAP(r1)
84         mfxer   r9
85         mfctr   r10
86         std     r9,_XER(r1)
87         std     r10,_CTR(r1)
88         std     r3,ORIG_GPR3(r1)
89         ld      r2,PACATOC(r13)
90         addi    r9,r1,STACK_FRAME_OVERHEAD
91         ld      r11,exception_marker@toc(r2)
92         std     r11,-16(r9)             /* "regshere" marker */
93 #ifdef CONFIG_TRACE_IRQFLAGS
94         bl      .trace_hardirqs_on
95         REST_GPR(0,r1)
96         REST_4GPRS(3,r1)
97         REST_2GPRS(7,r1)
98         addi    r9,r1,STACK_FRAME_OVERHEAD
99         ld      r12,_MSR(r1)
100 #endif /* CONFIG_TRACE_IRQFLAGS */
101         li      r10,1
102         stb     r10,PACASOFTIRQEN(r13)
103         stb     r10,PACAHARDIRQEN(r13)
104         std     r10,SOFTE(r1)
105 #ifdef CONFIG_PPC_ISERIES
106 BEGIN_FW_FTR_SECTION
107         /* Hack for handling interrupts when soft-enabling on iSeries */
108         cmpdi   cr1,r0,0x5555           /* syscall 0x5555 */
109         andi.   r10,r12,MSR_PR          /* from kernel */
110         crand   4*cr0+eq,4*cr1+eq,4*cr0+eq
111         bne     2f
112         b       hardware_interrupt_entry
113 2:
114 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
115 #endif /* CONFIG_PPC_ISERIES */
116         mfmsr   r11
117         ori     r11,r11,MSR_EE
118         mtmsrd  r11,1
119
120 #ifdef SHOW_SYSCALLS
121         bl      .do_show_syscall
122         REST_GPR(0,r1)
123         REST_4GPRS(3,r1)
124         REST_2GPRS(7,r1)
125         addi    r9,r1,STACK_FRAME_OVERHEAD
126 #endif
127         clrrdi  r11,r1,THREAD_SHIFT
128         ld      r10,TI_FLAGS(r11)
129         andi.   r11,r10,_TIF_SYSCALL_T_OR_A
130         bne-    syscall_dotrace
131 syscall_dotrace_cont:
132         cmpldi  0,r0,NR_syscalls
133         bge-    syscall_enosys
134
135 system_call:                    /* label this so stack traces look sane */
136 /*
137  * Need to vector to 32 Bit or default sys_call_table here,
138  * based on caller's run-mode / personality.
139  */
140         ld      r11,.SYS_CALL_TABLE@toc(2)
141         andi.   r10,r10,_TIF_32BIT
142         beq     15f
143         addi    r11,r11,8       /* use 32-bit syscall entries */
144         clrldi  r3,r3,32
145         clrldi  r4,r4,32
146         clrldi  r5,r5,32
147         clrldi  r6,r6,32
148         clrldi  r7,r7,32
149         clrldi  r8,r8,32
150 15:
151         slwi    r0,r0,4
152         ldx     r10,r11,r0      /* Fetch system call handler [ptr] */
153         mtctr   r10
154         bctrl                   /* Call handler */
155
156 syscall_exit:
157         std     r3,RESULT(r1)
158 #ifdef SHOW_SYSCALLS
159         bl      .do_show_syscall_exit
160         ld      r3,RESULT(r1)
161 #endif
162         clrrdi  r12,r1,THREAD_SHIFT
163
164         /* disable interrupts so current_thread_info()->flags can't change,
165            and so that we don't get interrupted after loading SRR0/1. */
166         ld      r8,_MSR(r1)
167         andi.   r10,r8,MSR_RI
168         beq-    unrecov_restore
169         mfmsr   r10
170         rldicl  r10,r10,48,1
171         rotldi  r10,r10,16
172         mtmsrd  r10,1
173         ld      r9,TI_FLAGS(r12)
174         li      r11,-_LAST_ERRNO
175         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
176         bne-    syscall_exit_work
177         cmpld   r3,r11
178         ld      r5,_CCR(r1)
179         bge-    syscall_error
180 syscall_error_cont:
181         ld      r7,_NIP(r1)
182         stdcx.  r0,0,r1                 /* to clear the reservation */
183         andi.   r6,r8,MSR_PR
184         ld      r4,_LINK(r1)
185         /*
186          * Clear RI before restoring r13.  If we are returning to
187          * userspace and we take an exception after restoring r13,
188          * we end up corrupting the userspace r13 value.
189          */
190         li      r12,MSR_RI
191         andc    r11,r10,r12
192         mtmsrd  r11,1                   /* clear MSR.RI */
193         beq-    1f
194         ACCOUNT_CPU_USER_EXIT(r11, r12)
195         ld      r13,GPR13(r1)   /* only restore r13 if returning to usermode */
196 1:      ld      r2,GPR2(r1)
197         ld      r1,GPR1(r1)
198         mtlr    r4
199         mtcr    r5
200         mtspr   SPRN_SRR0,r7
201         mtspr   SPRN_SRR1,r8
202         rfid
203         b       .       /* prevent speculative execution */
204
205 syscall_error:  
206         oris    r5,r5,0x1000    /* Set SO bit in CR */
207         neg     r3,r3
208         std     r5,_CCR(r1)
209         b       syscall_error_cont
210         
211 /* Traced system call support */
212 syscall_dotrace:
213         bl      .save_nvgprs
214         addi    r3,r1,STACK_FRAME_OVERHEAD
215         bl      .do_syscall_trace_enter
216         ld      r0,GPR0(r1)     /* Restore original registers */
217         ld      r3,GPR3(r1)
218         ld      r4,GPR4(r1)
219         ld      r5,GPR5(r1)
220         ld      r6,GPR6(r1)
221         ld      r7,GPR7(r1)
222         ld      r8,GPR8(r1)
223         addi    r9,r1,STACK_FRAME_OVERHEAD
224         clrrdi  r10,r1,THREAD_SHIFT
225         ld      r10,TI_FLAGS(r10)
226         b       syscall_dotrace_cont
227
228 syscall_enosys:
229         li      r3,-ENOSYS
230         b       syscall_exit
231         
232 syscall_exit_work:
233         /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
234          If TIF_NOERROR is set, just save r3 as it is. */
235
236         andi.   r0,r9,_TIF_RESTOREALL
237         beq+    0f
238         REST_NVGPRS(r1)
239         b       2f
240 0:      cmpld   r3,r11          /* r10 is -LAST_ERRNO */
241         blt+    1f
242         andi.   r0,r9,_TIF_NOERROR
243         bne-    1f
244         ld      r5,_CCR(r1)
245         neg     r3,r3
246         oris    r5,r5,0x1000    /* Set SO bit in CR */
247         std     r5,_CCR(r1)
248 1:      std     r3,GPR3(r1)
249 2:      andi.   r0,r9,(_TIF_PERSYSCALL_MASK)
250         beq     4f
251
252         /* Clear per-syscall TIF flags if any are set.  */
253
254         li      r11,_TIF_PERSYSCALL_MASK
255         addi    r12,r12,TI_FLAGS
256 3:      ldarx   r10,0,r12
257         andc    r10,r10,r11
258         stdcx.  r10,0,r12
259         bne-    3b
260         subi    r12,r12,TI_FLAGS
261
262 4:      /* Anything else left to do? */
263         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
264         beq     .ret_from_except_lite
265
266         /* Re-enable interrupts */
267         mfmsr   r10
268         ori     r10,r10,MSR_EE
269         mtmsrd  r10,1
270
271         bl      .save_nvgprs
272         addi    r3,r1,STACK_FRAME_OVERHEAD
273         bl      .do_syscall_trace_leave
274         b       .ret_from_except
275
276 /* Save non-volatile GPRs, if not already saved. */
277 _GLOBAL(save_nvgprs)
278         ld      r11,_TRAP(r1)
279         andi.   r0,r11,1
280         beqlr-
281         SAVE_NVGPRS(r1)
282         clrrdi  r0,r11,1
283         std     r0,_TRAP(r1)
284         blr
285
286         
287 /*
288  * The sigsuspend and rt_sigsuspend system calls can call do_signal
289  * and thus put the process into the stopped state where we might
290  * want to examine its user state with ptrace.  Therefore we need
291  * to save all the nonvolatile registers (r14 - r31) before calling
292  * the C code.  Similarly, fork, vfork and clone need the full
293  * register state on the stack so that it can be copied to the child.
294  */
295
296 _GLOBAL(ppc_fork)
297         bl      .save_nvgprs
298         bl      .sys_fork
299         b       syscall_exit
300
301 _GLOBAL(ppc_vfork)
302         bl      .save_nvgprs
303         bl      .sys_vfork
304         b       syscall_exit
305
306 _GLOBAL(ppc_clone)
307         bl      .save_nvgprs
308         bl      .sys_clone
309         b       syscall_exit
310
311 _GLOBAL(ppc32_swapcontext)
312         bl      .save_nvgprs
313         bl      .compat_sys_swapcontext
314         b       syscall_exit
315
316 _GLOBAL(ppc64_swapcontext)
317         bl      .save_nvgprs
318         bl      .sys_swapcontext
319         b       syscall_exit
320
321 _GLOBAL(ret_from_fork)
322         bl      .schedule_tail
323         REST_NVGPRS(r1)
324         li      r3,0
325         b       syscall_exit
326
327 /*
328  * This routine switches between two different tasks.  The process
329  * state of one is saved on its kernel stack.  Then the state
330  * of the other is restored from its kernel stack.  The memory
331  * management hardware is updated to the second process's state.
332  * Finally, we can return to the second process, via ret_from_except.
333  * On entry, r3 points to the THREAD for the current task, r4
334  * points to the THREAD for the new task.
335  *
336  * Note: there are two ways to get to the "going out" portion
337  * of this code; either by coming in via the entry (_switch)
338  * or via "fork" which must set up an environment equivalent
339  * to the "_switch" path.  If you change this you'll have to change
340  * the fork code also.
341  *
342  * The code which creates the new task context is in 'copy_thread'
343  * in arch/powerpc/kernel/process.c 
344  */
345         .align  7
346 _GLOBAL(_switch)
347         mflr    r0
348         std     r0,16(r1)
349         stdu    r1,-SWITCH_FRAME_SIZE(r1)
350         /* r3-r13 are caller saved -- Cort */
351         SAVE_8GPRS(14, r1)
352         SAVE_10GPRS(22, r1)
353         mflr    r20             /* Return to switch caller */
354         mfmsr   r22
355         li      r0, MSR_FP
356 #ifdef CONFIG_VSX
357 BEGIN_FTR_SECTION
358         oris    r0,r0,MSR_VSX@h /* Disable VSX */
359 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
360 #endif /* CONFIG_VSX */
361 #ifdef CONFIG_ALTIVEC
362 BEGIN_FTR_SECTION
363         oris    r0,r0,MSR_VEC@h /* Disable altivec */
364         mfspr   r24,SPRN_VRSAVE /* save vrsave register value */
365         std     r24,THREAD_VRSAVE(r3)
366 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
367 #endif /* CONFIG_ALTIVEC */
368         and.    r0,r0,r22
369         beq+    1f
370         andc    r22,r22,r0
371         mtmsrd  r22
372         isync
373 1:      std     r20,_NIP(r1)
374         mfcr    r23
375         std     r23,_CCR(r1)
376         std     r1,KSP(r3)      /* Set old stack pointer */
377
378 #ifdef CONFIG_SMP
379         /* We need a sync somewhere here to make sure that if the
380          * previous task gets rescheduled on another CPU, it sees all
381          * stores it has performed on this one.
382          */
383         sync
384 #endif /* CONFIG_SMP */
385
386         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
387         std     r6,PACACURRENT(r13)     /* Set new 'current' */
388
389         ld      r8,KSP(r4)      /* new stack pointer */
390 BEGIN_FTR_SECTION
391   BEGIN_FTR_SECTION_NESTED(95)
392         clrrdi  r6,r8,28        /* get its ESID */
393         clrrdi  r9,r1,28        /* get current sp ESID */
394   FTR_SECTION_ELSE_NESTED(95)
395         clrrdi  r6,r8,40        /* get its 1T ESID */
396         clrrdi  r9,r1,40        /* get current sp 1T ESID */
397   ALT_FTR_SECTION_END_NESTED_IFCLR(CPU_FTR_1T_SEGMENT, 95)
398 FTR_SECTION_ELSE
399         b       2f
400 ALT_FTR_SECTION_END_IFSET(CPU_FTR_SLB)
401         clrldi. r0,r6,2         /* is new ESID c00000000? */
402         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
403         cror    eq,4*cr1+eq,eq
404         beq     2f              /* if yes, don't slbie it */
405
406         /* Bolt in the new stack SLB entry */
407         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
408         oris    r0,r6,(SLB_ESID_V)@h
409         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
410 BEGIN_FTR_SECTION
411         li      r9,MMU_SEGSIZE_1T       /* insert B field */
412         oris    r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
413         rldimi  r7,r9,SLB_VSID_SSIZE_SHIFT,0
414 END_FTR_SECTION_IFSET(CPU_FTR_1T_SEGMENT)
415
416         /* Update the last bolted SLB.  No write barriers are needed
417          * here, provided we only update the current CPU's SLB shadow
418          * buffer.
419          */
420         ld      r9,PACA_SLBSHADOWPTR(r13)
421         li      r12,0
422         std     r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
423         std     r7,SLBSHADOW_STACKVSID(r9)  /* Save VSID */
424         std     r0,SLBSHADOW_STACKESID(r9)  /* Save ESID */
425
426         /* No need to check for CPU_FTR_NO_SLBIE_B here, since when
427          * we have 1TB segments, the only CPUs known to have the errata
428          * only support less than 1TB of system memory and we'll never
429          * actually hit this code path.
430          */
431
432         slbie   r6
433         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
434         slbmte  r7,r0
435         isync
436
437 2:
438         clrrdi  r7,r8,THREAD_SHIFT      /* base of new stack */
439         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
440            because we don't need to leave the 288-byte ABI gap at the
441            top of the kernel stack. */
442         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
443
444         mr      r1,r8           /* start using new stack pointer */
445         std     r7,PACAKSAVE(r13)
446
447         ld      r6,_CCR(r1)
448         mtcrf   0xFF,r6
449
450 #ifdef CONFIG_ALTIVEC
451 BEGIN_FTR_SECTION
452         ld      r0,THREAD_VRSAVE(r4)
453         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
454 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
455 #endif /* CONFIG_ALTIVEC */
456
457         /* r3-r13 are destroyed -- Cort */
458         REST_8GPRS(14, r1)
459         REST_10GPRS(22, r1)
460
461         /* convert old thread to its task_struct for return value */
462         addi    r3,r3,-THREAD
463         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
464         mtlr    r7
465         addi    r1,r1,SWITCH_FRAME_SIZE
466         blr
467
468         .align  7
469 _GLOBAL(ret_from_except)
470         ld      r11,_TRAP(r1)
471         andi.   r0,r11,1
472         bne     .ret_from_except_lite
473         REST_NVGPRS(r1)
474
475 _GLOBAL(ret_from_except_lite)
476         /*
477          * Disable interrupts so that current_thread_info()->flags
478          * can't change between when we test it and when we return
479          * from the interrupt.
480          */
481         mfmsr   r10             /* Get current interrupt state */
482         rldicl  r9,r10,48,1     /* clear MSR_EE */
483         rotldi  r9,r9,16
484         mtmsrd  r9,1            /* Update machine state */
485
486 #ifdef CONFIG_PREEMPT
487         clrrdi  r9,r1,THREAD_SHIFT      /* current_thread_info() */
488         li      r0,_TIF_NEED_RESCHED    /* bits to check */
489         ld      r3,_MSR(r1)
490         ld      r4,TI_FLAGS(r9)
491         /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
492         rlwimi  r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
493         and.    r0,r4,r0        /* check NEED_RESCHED and maybe SIGPENDING */
494         bne     do_work
495
496 #else /* !CONFIG_PREEMPT */
497         ld      r3,_MSR(r1)     /* Returning to user mode? */
498         andi.   r3,r3,MSR_PR
499         beq     restore         /* if not, just restore regs and return */
500
501         /* Check current_thread_info()->flags */
502         clrrdi  r9,r1,THREAD_SHIFT
503         ld      r4,TI_FLAGS(r9)
504         andi.   r0,r4,_TIF_USER_WORK_MASK
505         bne     do_work
506 #endif
507
508 restore:
509         ld      r5,SOFTE(r1)
510 #ifdef CONFIG_PPC_ISERIES
511 BEGIN_FW_FTR_SECTION
512         cmpdi   0,r5,0
513         beq     4f
514         /* Check for pending interrupts (iSeries) */
515         ld      r3,PACALPPACAPTR(r13)
516         ld      r3,LPPACAANYINT(r3)
517         cmpdi   r3,0
518         beq+    4f                      /* skip do_IRQ if no interrupts */
519
520         li      r3,0
521         stb     r3,PACASOFTIRQEN(r13)   /* ensure we are soft-disabled */
522 #ifdef CONFIG_TRACE_IRQFLAGS
523         bl      .trace_hardirqs_off
524         mfmsr   r10
525 #endif
526         ori     r10,r10,MSR_EE
527         mtmsrd  r10                     /* hard-enable again */
528         addi    r3,r1,STACK_FRAME_OVERHEAD
529         bl      .do_IRQ
530         b       .ret_from_except_lite           /* loop back and handle more */
531 4:
532 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
533 #endif
534         TRACE_AND_RESTORE_IRQ(r5);
535
536         /* extract EE bit and use it to restore paca->hard_enabled */
537         ld      r3,_MSR(r1)
538         rldicl  r4,r3,49,63             /* r0 = (r3 >> 15) & 1 */
539         stb     r4,PACAHARDIRQEN(r13)
540
541         ld      r4,_CTR(r1)
542         ld      r0,_LINK(r1)
543         mtctr   r4
544         mtlr    r0
545         ld      r4,_XER(r1)
546         mtspr   SPRN_XER,r4
547
548         REST_8GPRS(5, r1)
549
550         andi.   r0,r3,MSR_RI
551         beq-    unrecov_restore
552
553         stdcx.  r0,0,r1         /* to clear the reservation */
554
555         /*
556          * Clear RI before restoring r13.  If we are returning to
557          * userspace and we take an exception after restoring r13,
558          * we end up corrupting the userspace r13 value.
559          */
560         mfmsr   r4
561         andc    r4,r4,r0        /* r0 contains MSR_RI here */
562         mtmsrd  r4,1
563
564         /*
565          * r13 is our per cpu area, only restore it if we are returning to
566          * userspace
567          */
568         andi.   r0,r3,MSR_PR
569         beq     1f
570         ACCOUNT_CPU_USER_EXIT(r2, r4)
571         REST_GPR(13, r1)
572 1:
573         mtspr   SPRN_SRR1,r3
574
575         ld      r2,_CCR(r1)
576         mtcrf   0xFF,r2
577         ld      r2,_NIP(r1)
578         mtspr   SPRN_SRR0,r2
579
580         ld      r0,GPR0(r1)
581         ld      r2,GPR2(r1)
582         ld      r3,GPR3(r1)
583         ld      r4,GPR4(r1)
584         ld      r1,GPR1(r1)
585
586         rfid
587         b       .       /* prevent speculative execution */
588
589 do_work:
590 #ifdef CONFIG_PREEMPT
591         andi.   r0,r3,MSR_PR    /* Returning to user mode? */
592         bne     user_work
593         /* Check that preempt_count() == 0 and interrupts are enabled */
594         lwz     r8,TI_PREEMPT(r9)
595         cmpwi   cr1,r8,0
596         ld      r0,SOFTE(r1)
597         cmpdi   r0,0
598         crandc  eq,cr1*4+eq,eq
599         bne     restore
600         /* here we are preempting the current task */
601 1:
602 #ifdef CONFIG_TRACE_IRQFLAGS
603         bl      .trace_hardirqs_on
604         /* Note: we just clobbered r10 which used to contain the previous
605          * MSR before the hard-disabling done by the caller of do_work.
606          * We don't have that value anymore, but it doesn't matter as
607          * we will hard-enable unconditionally, we can just reload the
608          * current MSR into r10
609          */
610         mfmsr   r10
611 #endif /* CONFIG_TRACE_IRQFLAGS */
612         li      r0,1
613         stb     r0,PACASOFTIRQEN(r13)
614         stb     r0,PACAHARDIRQEN(r13)
615         ori     r10,r10,MSR_EE
616         mtmsrd  r10,1           /* reenable interrupts */
617         bl      .preempt_schedule
618         mfmsr   r10
619         clrrdi  r9,r1,THREAD_SHIFT
620         rldicl  r10,r10,48,1    /* disable interrupts again */
621         rotldi  r10,r10,16
622         mtmsrd  r10,1
623         ld      r4,TI_FLAGS(r9)
624         andi.   r0,r4,_TIF_NEED_RESCHED
625         bne     1b
626         b       restore
627
628 user_work:
629 #endif
630         /* Enable interrupts */
631         ori     r10,r10,MSR_EE
632         mtmsrd  r10,1
633
634         andi.   r0,r4,_TIF_NEED_RESCHED
635         beq     1f
636         bl      .schedule
637         b       .ret_from_except_lite
638
639 1:      bl      .save_nvgprs
640         li      r3,0
641         addi    r4,r1,STACK_FRAME_OVERHEAD
642         bl      .do_signal
643         b       .ret_from_except
644
645 unrecov_restore:
646         addi    r3,r1,STACK_FRAME_OVERHEAD
647         bl      .unrecoverable_exception
648         b       unrecov_restore
649
650 #ifdef CONFIG_PPC_RTAS
651 /*
652  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
653  * called with the MMU off.
654  *
655  * In addition, we need to be in 32b mode, at least for now.
656  * 
657  * Note: r3 is an input parameter to rtas, so don't trash it...
658  */
659 _GLOBAL(enter_rtas)
660         mflr    r0
661         std     r0,16(r1)
662         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
663
664         /* Because RTAS is running in 32b mode, it clobbers the high order half
665          * of all registers that it saves.  We therefore save those registers
666          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
667          */
668         SAVE_GPR(2, r1)                 /* Save the TOC */
669         SAVE_GPR(13, r1)                /* Save paca */
670         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
671         SAVE_10GPRS(22, r1)             /* ditto */
672
673         mfcr    r4
674         std     r4,_CCR(r1)
675         mfctr   r5
676         std     r5,_CTR(r1)
677         mfspr   r6,SPRN_XER
678         std     r6,_XER(r1)
679         mfdar   r7
680         std     r7,_DAR(r1)
681         mfdsisr r8
682         std     r8,_DSISR(r1)
683         mfsrr0  r9
684         std     r9,_SRR0(r1)
685         mfsrr1  r10
686         std     r10,_SRR1(r1)
687
688         /* Temporary workaround to clear CR until RTAS can be modified to
689          * ignore all bits.
690          */
691         li      r0,0
692         mtcr    r0
693
694 #ifdef CONFIG_BUG       
695         /* There is no way it is acceptable to get here with interrupts enabled,
696          * check it with the asm equivalent of WARN_ON
697          */
698         lbz     r0,PACASOFTIRQEN(r13)
699 1:      tdnei   r0,0
700         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
701 #endif
702         
703         /* Hard-disable interrupts */
704         mfmsr   r6
705         rldicl  r7,r6,48,1
706         rotldi  r7,r7,16
707         mtmsrd  r7,1
708
709         /* Unfortunately, the stack pointer and the MSR are also clobbered,
710          * so they are saved in the PACA which allows us to restore
711          * our original state after RTAS returns.
712          */
713         std     r1,PACAR1(r13)
714         std     r6,PACASAVEDMSR(r13)
715
716         /* Setup our real return addr */        
717         LOAD_REG_ADDR(r4,.rtas_return_loc)
718         clrldi  r4,r4,2                 /* convert to realmode address */
719         mtlr    r4
720
721         li      r0,0
722         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
723         andc    r0,r6,r0
724         
725         li      r9,1
726         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
727         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP
728         andc    r6,r0,r9
729         ori     r6,r6,MSR_RI
730         sync                            /* disable interrupts so SRR0/1 */
731         mtmsrd  r0                      /* don't get trashed */
732
733         LOAD_REG_ADDR(r4, rtas)
734         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
735         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
736         
737         mtspr   SPRN_SRR0,r5
738         mtspr   SPRN_SRR1,r6
739         rfid
740         b       .       /* prevent speculative execution */
741
742 _STATIC(rtas_return_loc)
743         /* relocation is off at this point */
744         mfspr   r4,SPRN_SPRG3           /* Get PACA */
745         clrldi  r4,r4,2                 /* convert to realmode address */
746
747         mfmsr   r6
748         li      r0,MSR_RI
749         andc    r6,r6,r0
750         sync    
751         mtmsrd  r6
752         
753         ld      r1,PACAR1(r4)           /* Restore our SP */
754         LOAD_REG_IMMEDIATE(r3,.rtas_restore_regs)
755         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
756
757         mtspr   SPRN_SRR0,r3
758         mtspr   SPRN_SRR1,r4
759         rfid
760         b       .       /* prevent speculative execution */
761
762 _STATIC(rtas_restore_regs)
763         /* relocation is on at this point */
764         REST_GPR(2, r1)                 /* Restore the TOC */
765         REST_GPR(13, r1)                /* Restore paca */
766         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
767         REST_10GPRS(22, r1)             /* ditto */
768
769         mfspr   r13,SPRN_SPRG3
770
771         ld      r4,_CCR(r1)
772         mtcr    r4
773         ld      r5,_CTR(r1)
774         mtctr   r5
775         ld      r6,_XER(r1)
776         mtspr   SPRN_XER,r6
777         ld      r7,_DAR(r1)
778         mtdar   r7
779         ld      r8,_DSISR(r1)
780         mtdsisr r8
781         ld      r9,_SRR0(r1)
782         mtsrr0  r9
783         ld      r10,_SRR1(r1)
784         mtsrr1  r10
785
786         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
787         ld      r0,16(r1)               /* get return address */
788
789         mtlr    r0
790         blr                             /* return to caller */
791
792 #endif /* CONFIG_PPC_RTAS */
793
794 _GLOBAL(enter_prom)
795         mflr    r0
796         std     r0,16(r1)
797         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
798
799         /* Because PROM is running in 32b mode, it clobbers the high order half
800          * of all registers that it saves.  We therefore save those registers
801          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
802          */
803         SAVE_8GPRS(2, r1)
804         SAVE_GPR(13, r1)
805         SAVE_8GPRS(14, r1)
806         SAVE_10GPRS(22, r1)
807         mfcr    r4
808         std     r4,_CCR(r1)
809         mfctr   r5
810         std     r5,_CTR(r1)
811         mfspr   r6,SPRN_XER
812         std     r6,_XER(r1)
813         mfdar   r7
814         std     r7,_DAR(r1)
815         mfdsisr r8
816         std     r8,_DSISR(r1)
817         mfsrr0  r9
818         std     r9,_SRR0(r1)
819         mfsrr1  r10
820         std     r10,_SRR1(r1)
821         mfmsr   r11
822         std     r11,_MSR(r1)
823
824         /* Get the PROM entrypoint */
825         ld      r0,GPR4(r1)
826         mtlr    r0
827
828         /* Switch MSR to 32 bits mode
829          */
830         mfmsr   r11
831         li      r12,1
832         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
833         andc    r11,r11,r12
834         li      r12,1
835         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
836         andc    r11,r11,r12
837         mtmsrd  r11
838         isync
839
840         /* Restore arguments & enter PROM here... */
841         ld      r3,GPR3(r1)
842         blrl
843
844         /* Just make sure that r1 top 32 bits didn't get
845          * corrupt by OF
846          */
847         rldicl  r1,r1,0,32
848
849         /* Restore the MSR (back to 64 bits) */
850         ld      r0,_MSR(r1)
851         mtmsrd  r0
852         isync
853
854         /* Restore other registers */
855         REST_GPR(2, r1)
856         REST_GPR(13, r1)
857         REST_8GPRS(14, r1)
858         REST_10GPRS(22, r1)
859         ld      r4,_CCR(r1)
860         mtcr    r4
861         ld      r5,_CTR(r1)
862         mtctr   r5
863         ld      r6,_XER(r1)
864         mtspr   SPRN_XER,r6
865         ld      r7,_DAR(r1)
866         mtdar   r7
867         ld      r8,_DSISR(r1)
868         mtdsisr r8
869         ld      r9,_SRR0(r1)
870         mtsrr0  r9
871         ld      r10,_SRR1(r1)
872         mtsrr1  r10
873         
874         addi    r1,r1,PROM_FRAME_SIZE
875         ld      r0,16(r1)
876         mtlr    r0
877         blr