]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/entry_64.S
Merge branch 'merge' into next
[karo-tx-linux.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 #include <asm/ftrace.h>
35 #include <asm/hw_irq.h>
36
37 /*
38  * System calls.
39  */
40         .section        ".toc","aw"
41 .SYS_CALL_TABLE:
42         .tc .sys_call_table[TC],.sys_call_table
43
44 /* This value is used to mark exception frames on the stack. */
45 exception_marker:
46         .tc     ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
47
48         .section        ".text"
49         .align 7
50
51 #undef SHOW_SYSCALLS
52
53         .globl system_call_common
54 system_call_common:
55         andi.   r10,r12,MSR_PR
56         mr      r10,r1
57         addi    r1,r1,-INT_FRAME_SIZE
58         beq-    1f
59         ld      r1,PACAKSAVE(r13)
60 1:      std     r10,0(r1)
61         std     r11,_NIP(r1)
62         std     r12,_MSR(r1)
63         std     r0,GPR0(r1)
64         std     r10,GPR1(r1)
65         beq     2f                      /* if from kernel mode */
66         ACCOUNT_CPU_USER_ENTRY(r10, r11)
67 2:      std     r2,GPR2(r1)
68         std     r3,GPR3(r1)
69         mfcr    r2
70         std     r4,GPR4(r1)
71         std     r5,GPR5(r1)
72         std     r6,GPR6(r1)
73         std     r7,GPR7(r1)
74         std     r8,GPR8(r1)
75         li      r11,0
76         std     r11,GPR9(r1)
77         std     r11,GPR10(r1)
78         std     r11,GPR11(r1)
79         std     r11,GPR12(r1)
80         std     r11,_XER(r1)
81         std     r11,_CTR(r1)
82         std     r9,GPR13(r1)
83         mflr    r10
84         /*
85          * This clears CR0.SO (bit 28), which is the error indication on
86          * return from this system call.
87          */
88         rldimi  r2,r11,28,(63-28)
89         li      r11,0xc01
90         std     r10,_LINK(r1)
91         std     r11,_TRAP(r1)
92         std     r3,ORIG_GPR3(r1)
93         std     r2,_CCR(r1)
94         ld      r2,PACATOC(r13)
95         addi    r9,r1,STACK_FRAME_OVERHEAD
96         ld      r11,exception_marker@toc(r2)
97         std     r11,-16(r9)             /* "regshere" marker */
98 #if defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR)
99 BEGIN_FW_FTR_SECTION
100         beq     33f
101         /* if from user, see if there are any DTL entries to process */
102         ld      r10,PACALPPACAPTR(r13)  /* get ptr to VPA */
103         ld      r11,PACA_DTL_RIDX(r13)  /* get log read index */
104         ld      r10,LPPACA_DTLIDX(r10)  /* get log write index */
105         cmpd    cr1,r11,r10
106         beq+    cr1,33f
107         bl      .accumulate_stolen_time
108         REST_GPR(0,r1)
109         REST_4GPRS(3,r1)
110         REST_2GPRS(7,r1)
111         addi    r9,r1,STACK_FRAME_OVERHEAD
112 33:
113 END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
114 #endif /* CONFIG_VIRT_CPU_ACCOUNTING && CONFIG_PPC_SPLPAR */
115
116         /*
117          * A syscall should always be called with interrupts enabled
118          * so we just unconditionally hard-enable here. When some kind
119          * of irq tracing is used, we additionally check that condition
120          * is correct
121          */
122 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
123         lbz     r10,PACASOFTIRQEN(r13)
124         xori    r10,r10,1
125 1:      tdnei   r10,0
126         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
127 #endif
128
129 #ifdef CONFIG_PPC_BOOK3E
130         wrteei  1
131 #else
132         ld      r11,PACAKMSR(r13)
133         ori     r11,r11,MSR_EE
134         mtmsrd  r11,1
135 #endif /* CONFIG_PPC_BOOK3E */
136
137         /* We do need to set SOFTE in the stack frame or the return
138          * from interrupt will be painful
139          */
140         li      r10,1
141         std     r10,SOFTE(r1)
142
143 #ifdef SHOW_SYSCALLS
144         bl      .do_show_syscall
145         REST_GPR(0,r1)
146         REST_4GPRS(3,r1)
147         REST_2GPRS(7,r1)
148         addi    r9,r1,STACK_FRAME_OVERHEAD
149 #endif
150         CURRENT_THREAD_INFO(r11, r1)
151         ld      r10,TI_FLAGS(r11)
152         andi.   r11,r10,_TIF_SYSCALL_T_OR_A
153         bne-    syscall_dotrace
154 .Lsyscall_dotrace_cont:
155         cmpldi  0,r0,NR_syscalls
156         bge-    syscall_enosys
157
158 system_call:                    /* label this so stack traces look sane */
159 /*
160  * Need to vector to 32 Bit or default sys_call_table here,
161  * based on caller's run-mode / personality.
162  */
163         ld      r11,.SYS_CALL_TABLE@toc(2)
164         andi.   r10,r10,_TIF_32BIT
165         beq     15f
166         addi    r11,r11,8       /* use 32-bit syscall entries */
167         clrldi  r3,r3,32
168         clrldi  r4,r4,32
169         clrldi  r5,r5,32
170         clrldi  r6,r6,32
171         clrldi  r7,r7,32
172         clrldi  r8,r8,32
173 15:
174         slwi    r0,r0,4
175         ldx     r10,r11,r0      /* Fetch system call handler [ptr] */
176         mtctr   r10
177         bctrl                   /* Call handler */
178
179 syscall_exit:
180         std     r3,RESULT(r1)
181 #ifdef SHOW_SYSCALLS
182         bl      .do_show_syscall_exit
183         ld      r3,RESULT(r1)
184 #endif
185         CURRENT_THREAD_INFO(r12, r1)
186
187         ld      r8,_MSR(r1)
188 #ifdef CONFIG_PPC_BOOK3S
189         /* No MSR:RI on BookE */
190         andi.   r10,r8,MSR_RI
191         beq-    unrecov_restore
192 #endif
193         /*
194          * Disable interrupts so current_thread_info()->flags can't change,
195          * and so that we don't get interrupted after loading SRR0/1.
196          */
197 #ifdef CONFIG_PPC_BOOK3E
198         wrteei  0
199 #else
200         ld      r10,PACAKMSR(r13)
201         /*
202          * For performance reasons we clear RI the same time that we
203          * clear EE. We only need to clear RI just before we restore r13
204          * below, but batching it with EE saves us one expensive mtmsrd call.
205          * We have to be careful to restore RI if we branch anywhere from
206          * here (eg syscall_exit_work).
207          */
208         li      r9,MSR_RI
209         andc    r11,r10,r9
210         mtmsrd  r11,1
211 #endif /* CONFIG_PPC_BOOK3E */
212
213         ld      r9,TI_FLAGS(r12)
214         li      r11,-_LAST_ERRNO
215         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
216         bne-    syscall_exit_work
217         cmpld   r3,r11
218         ld      r5,_CCR(r1)
219         bge-    syscall_error
220 .Lsyscall_error_cont:
221         ld      r7,_NIP(r1)
222 BEGIN_FTR_SECTION
223         stdcx.  r0,0,r1                 /* to clear the reservation */
224 END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
225         andi.   r6,r8,MSR_PR
226         ld      r4,_LINK(r1)
227
228         beq-    1f
229         ACCOUNT_CPU_USER_EXIT(r11, r12)
230         HMT_MEDIUM_LOW_HAS_PPR
231         ld      r13,GPR13(r1)   /* only restore r13 if returning to usermode */
232 1:      ld      r2,GPR2(r1)
233         ld      r1,GPR1(r1)
234         mtlr    r4
235         mtcr    r5
236         mtspr   SPRN_SRR0,r7
237         mtspr   SPRN_SRR1,r8
238         RFI
239         b       .       /* prevent speculative execution */
240
241 syscall_error:  
242         oris    r5,r5,0x1000    /* Set SO bit in CR */
243         neg     r3,r3
244         std     r5,_CCR(r1)
245         b       .Lsyscall_error_cont
246         
247 /* Traced system call support */
248 syscall_dotrace:
249         bl      .save_nvgprs
250         addi    r3,r1,STACK_FRAME_OVERHEAD
251         bl      .do_syscall_trace_enter
252         /*
253          * Restore argument registers possibly just changed.
254          * We use the return value of do_syscall_trace_enter
255          * for the call number to look up in the table (r0).
256          */
257         mr      r0,r3
258         ld      r3,GPR3(r1)
259         ld      r4,GPR4(r1)
260         ld      r5,GPR5(r1)
261         ld      r6,GPR6(r1)
262         ld      r7,GPR7(r1)
263         ld      r8,GPR8(r1)
264         addi    r9,r1,STACK_FRAME_OVERHEAD
265         CURRENT_THREAD_INFO(r10, r1)
266         ld      r10,TI_FLAGS(r10)
267         b       .Lsyscall_dotrace_cont
268
269 syscall_enosys:
270         li      r3,-ENOSYS
271         b       syscall_exit
272         
273 syscall_exit_work:
274 #ifdef CONFIG_PPC_BOOK3S
275         mtmsrd  r10,1           /* Restore RI */
276 #endif
277         /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
278          If TIF_NOERROR is set, just save r3 as it is. */
279
280         andi.   r0,r9,_TIF_RESTOREALL
281         beq+    0f
282         REST_NVGPRS(r1)
283         b       2f
284 0:      cmpld   r3,r11          /* r10 is -LAST_ERRNO */
285         blt+    1f
286         andi.   r0,r9,_TIF_NOERROR
287         bne-    1f
288         ld      r5,_CCR(r1)
289         neg     r3,r3
290         oris    r5,r5,0x1000    /* Set SO bit in CR */
291         std     r5,_CCR(r1)
292 1:      std     r3,GPR3(r1)
293 2:      andi.   r0,r9,(_TIF_PERSYSCALL_MASK)
294         beq     4f
295
296         /* Clear per-syscall TIF flags if any are set.  */
297
298         li      r11,_TIF_PERSYSCALL_MASK
299         addi    r12,r12,TI_FLAGS
300 3:      ldarx   r10,0,r12
301         andc    r10,r10,r11
302         stdcx.  r10,0,r12
303         bne-    3b
304         subi    r12,r12,TI_FLAGS
305
306 4:      /* Anything else left to do? */
307         SET_DEFAULT_THREAD_PPR(r3, r9)          /* Set thread.ppr = 3 */
308         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
309         beq     .ret_from_except_lite
310
311         /* Re-enable interrupts */
312 #ifdef CONFIG_PPC_BOOK3E
313         wrteei  1
314 #else
315         ld      r10,PACAKMSR(r13)
316         ori     r10,r10,MSR_EE
317         mtmsrd  r10,1
318 #endif /* CONFIG_PPC_BOOK3E */
319
320         bl      .save_nvgprs
321         addi    r3,r1,STACK_FRAME_OVERHEAD
322         bl      .do_syscall_trace_leave
323         b       .ret_from_except
324
325 /* Save non-volatile GPRs, if not already saved. */
326 _GLOBAL(save_nvgprs)
327         ld      r11,_TRAP(r1)
328         andi.   r0,r11,1
329         beqlr-
330         SAVE_NVGPRS(r1)
331         clrrdi  r0,r11,1
332         std     r0,_TRAP(r1)
333         blr
334
335         
336 /*
337  * The sigsuspend and rt_sigsuspend system calls can call do_signal
338  * and thus put the process into the stopped state where we might
339  * want to examine its user state with ptrace.  Therefore we need
340  * to save all the nonvolatile registers (r14 - r31) before calling
341  * the C code.  Similarly, fork, vfork and clone need the full
342  * register state on the stack so that it can be copied to the child.
343  */
344
345 _GLOBAL(ppc_fork)
346         bl      .save_nvgprs
347         bl      .sys_fork
348         b       syscall_exit
349
350 _GLOBAL(ppc_vfork)
351         bl      .save_nvgprs
352         bl      .sys_vfork
353         b       syscall_exit
354
355 _GLOBAL(ppc_clone)
356         bl      .save_nvgprs
357         bl      .sys_clone
358         b       syscall_exit
359
360 _GLOBAL(ppc32_swapcontext)
361         bl      .save_nvgprs
362         bl      .compat_sys_swapcontext
363         b       syscall_exit
364
365 _GLOBAL(ppc64_swapcontext)
366         bl      .save_nvgprs
367         bl      .sys_swapcontext
368         b       syscall_exit
369
370 _GLOBAL(ret_from_fork)
371         bl      .schedule_tail
372         REST_NVGPRS(r1)
373         li      r3,0
374         b       syscall_exit
375
376 _GLOBAL(ret_from_kernel_thread)
377         bl      .schedule_tail
378         REST_NVGPRS(r1)
379         li      r3,0
380         std     r3,0(r1)
381         ld      r14, 0(r14)
382         mtlr    r14
383         mr      r3,r15
384         blrl
385         li      r3,0
386         b       syscall_exit
387
388         .section        ".toc","aw"
389 DSCR_DEFAULT:
390         .tc dscr_default[TC],dscr_default
391
392         .section        ".text"
393
394 /*
395  * This routine switches between two different tasks.  The process
396  * state of one is saved on its kernel stack.  Then the state
397  * of the other is restored from its kernel stack.  The memory
398  * management hardware is updated to the second process's state.
399  * Finally, we can return to the second process, via ret_from_except.
400  * On entry, r3 points to the THREAD for the current task, r4
401  * points to the THREAD for the new task.
402  *
403  * Note: there are two ways to get to the "going out" portion
404  * of this code; either by coming in via the entry (_switch)
405  * or via "fork" which must set up an environment equivalent
406  * to the "_switch" path.  If you change this you'll have to change
407  * the fork code also.
408  *
409  * The code which creates the new task context is in 'copy_thread'
410  * in arch/powerpc/kernel/process.c 
411  */
412         .align  7
413 _GLOBAL(_switch)
414         mflr    r0
415         std     r0,16(r1)
416         stdu    r1,-SWITCH_FRAME_SIZE(r1)
417         /* r3-r13 are caller saved -- Cort */
418         SAVE_8GPRS(14, r1)
419         SAVE_10GPRS(22, r1)
420         mflr    r20             /* Return to switch caller */
421         mfmsr   r22
422         li      r0, MSR_FP
423 #ifdef CONFIG_VSX
424 BEGIN_FTR_SECTION
425         oris    r0,r0,MSR_VSX@h /* Disable VSX */
426 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
427 #endif /* CONFIG_VSX */
428 #ifdef CONFIG_ALTIVEC
429 BEGIN_FTR_SECTION
430         oris    r0,r0,MSR_VEC@h /* Disable altivec */
431         mfspr   r24,SPRN_VRSAVE /* save vrsave register value */
432         std     r24,THREAD_VRSAVE(r3)
433 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
434 #endif /* CONFIG_ALTIVEC */
435 #ifdef CONFIG_PPC64
436 BEGIN_FTR_SECTION
437         mfspr   r25,SPRN_DSCR
438         std     r25,THREAD_DSCR(r3)
439 END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
440 #endif
441         and.    r0,r0,r22
442         beq+    1f
443         andc    r22,r22,r0
444         MTMSRD(r22)
445         isync
446 1:      std     r20,_NIP(r1)
447         mfcr    r23
448         std     r23,_CCR(r1)
449         std     r1,KSP(r3)      /* Set old stack pointer */
450
451 #ifdef CONFIG_SMP
452         /* We need a sync somewhere here to make sure that if the
453          * previous task gets rescheduled on another CPU, it sees all
454          * stores it has performed on this one.
455          */
456         sync
457 #endif /* CONFIG_SMP */
458
459         /*
460          * If we optimise away the clear of the reservation in system
461          * calls because we know the CPU tracks the address of the
462          * reservation, then we need to clear it here to cover the
463          * case that the kernel context switch path has no larx
464          * instructions.
465          */
466 BEGIN_FTR_SECTION
467         ldarx   r6,0,r1
468 END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
469
470         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
471         std     r6,PACACURRENT(r13)     /* Set new 'current' */
472
473         ld      r8,KSP(r4)      /* new stack pointer */
474 #ifdef CONFIG_PPC_BOOK3S
475 BEGIN_FTR_SECTION
476   BEGIN_FTR_SECTION_NESTED(95)
477         clrrdi  r6,r8,28        /* get its ESID */
478         clrrdi  r9,r1,28        /* get current sp ESID */
479   FTR_SECTION_ELSE_NESTED(95)
480         clrrdi  r6,r8,40        /* get its 1T ESID */
481         clrrdi  r9,r1,40        /* get current sp 1T ESID */
482   ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
483 FTR_SECTION_ELSE
484         b       2f
485 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
486         clrldi. r0,r6,2         /* is new ESID c00000000? */
487         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
488         cror    eq,4*cr1+eq,eq
489         beq     2f              /* if yes, don't slbie it */
490
491         /* Bolt in the new stack SLB entry */
492         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
493         oris    r0,r6,(SLB_ESID_V)@h
494         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
495 BEGIN_FTR_SECTION
496         li      r9,MMU_SEGSIZE_1T       /* insert B field */
497         oris    r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
498         rldimi  r7,r9,SLB_VSID_SSIZE_SHIFT,0
499 END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
500
501         /* Update the last bolted SLB.  No write barriers are needed
502          * here, provided we only update the current CPU's SLB shadow
503          * buffer.
504          */
505         ld      r9,PACA_SLBSHADOWPTR(r13)
506         li      r12,0
507         std     r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
508         std     r7,SLBSHADOW_STACKVSID(r9)  /* Save VSID */
509         std     r0,SLBSHADOW_STACKESID(r9)  /* Save ESID */
510
511         /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
512          * we have 1TB segments, the only CPUs known to have the errata
513          * only support less than 1TB of system memory and we'll never
514          * actually hit this code path.
515          */
516
517         slbie   r6
518         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
519         slbmte  r7,r0
520         isync
521 2:
522 #endif /* !CONFIG_PPC_BOOK3S */
523
524         CURRENT_THREAD_INFO(r7, r8)  /* base of new stack */
525         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
526            because we don't need to leave the 288-byte ABI gap at the
527            top of the kernel stack. */
528         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
529
530         mr      r1,r8           /* start using new stack pointer */
531         std     r7,PACAKSAVE(r13)
532
533 #ifdef CONFIG_ALTIVEC
534 BEGIN_FTR_SECTION
535         ld      r0,THREAD_VRSAVE(r4)
536         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
537 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
538 #endif /* CONFIG_ALTIVEC */
539 #ifdef CONFIG_PPC64
540 BEGIN_FTR_SECTION
541         lwz     r6,THREAD_DSCR_INHERIT(r4)
542         ld      r7,DSCR_DEFAULT@toc(2)
543         ld      r0,THREAD_DSCR(r4)
544         cmpwi   r6,0
545         bne     1f
546         ld      r0,0(r7)
547 1:      cmpd    r0,r25
548         beq     2f
549         mtspr   SPRN_DSCR,r0
550 2:
551 END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
552 #endif
553
554         ld      r6,_CCR(r1)
555         mtcrf   0xFF,r6
556
557         /* r3-r13 are destroyed -- Cort */
558         REST_8GPRS(14, r1)
559         REST_10GPRS(22, r1)
560
561         /* convert old thread to its task_struct for return value */
562         addi    r3,r3,-THREAD
563         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
564         mtlr    r7
565         addi    r1,r1,SWITCH_FRAME_SIZE
566         blr
567
568         .align  7
569 _GLOBAL(ret_from_except)
570         ld      r11,_TRAP(r1)
571         andi.   r0,r11,1
572         bne     .ret_from_except_lite
573         REST_NVGPRS(r1)
574
575 _GLOBAL(ret_from_except_lite)
576         /*
577          * Disable interrupts so that current_thread_info()->flags
578          * can't change between when we test it and when we return
579          * from the interrupt.
580          */
581 #ifdef CONFIG_PPC_BOOK3E
582         wrteei  0
583 #else
584         ld      r10,PACAKMSR(r13) /* Get kernel MSR without EE */
585         mtmsrd  r10,1             /* Update machine state */
586 #endif /* CONFIG_PPC_BOOK3E */
587
588         CURRENT_THREAD_INFO(r9, r1)
589         ld      r3,_MSR(r1)
590         ld      r4,TI_FLAGS(r9)
591         andi.   r3,r3,MSR_PR
592         beq     resume_kernel
593
594         /* Check current_thread_info()->flags */
595         andi.   r0,r4,_TIF_USER_WORK_MASK
596         beq     restore
597
598         andi.   r0,r4,_TIF_NEED_RESCHED
599         beq     1f
600         bl      .restore_interrupts
601         bl      .schedule
602         b       .ret_from_except_lite
603
604 1:      bl      .save_nvgprs
605         bl      .restore_interrupts
606         addi    r3,r1,STACK_FRAME_OVERHEAD
607         bl      .do_notify_resume
608         b       .ret_from_except
609
610 resume_kernel:
611         /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
612         CURRENT_THREAD_INFO(r9, r1)
613         ld      r8,TI_FLAGS(r9)
614         andis.  r8,r8,_TIF_EMULATE_STACK_STORE@h
615         beq+    1f
616
617         addi    r8,r1,INT_FRAME_SIZE    /* Get the kprobed function entry */
618
619         lwz     r3,GPR1(r1)
620         subi    r3,r3,INT_FRAME_SIZE    /* dst: Allocate a trampoline exception frame */
621         mr      r4,r1                   /* src:  current exception frame */
622         mr      r1,r3                   /* Reroute the trampoline frame to r1 */
623
624         /* Copy from the original to the trampoline. */
625         li      r5,INT_FRAME_SIZE/8     /* size: INT_FRAME_SIZE */
626         li      r6,0                    /* start offset: 0 */
627         mtctr   r5
628 2:      ldx     r0,r6,r4
629         stdx    r0,r6,r3
630         addi    r6,r6,8
631         bdnz    2b
632
633         /* Do real store operation to complete stwu */
634         lwz     r5,GPR1(r1)
635         std     r8,0(r5)
636
637         /* Clear _TIF_EMULATE_STACK_STORE flag */
638         lis     r11,_TIF_EMULATE_STACK_STORE@h
639         addi    r5,r9,TI_FLAGS
640         ldarx   r4,0,r5
641         andc    r4,r4,r11
642         stdcx.  r4,0,r5
643         bne-    0b
644 1:
645
646 #ifdef CONFIG_PREEMPT
647         /* Check if we need to preempt */
648         andi.   r0,r4,_TIF_NEED_RESCHED
649         beq+    restore
650         /* Check that preempt_count() == 0 and interrupts are enabled */
651         lwz     r8,TI_PREEMPT(r9)
652         cmpwi   cr1,r8,0
653         ld      r0,SOFTE(r1)
654         cmpdi   r0,0
655         crandc  eq,cr1*4+eq,eq
656         bne     restore
657
658         /*
659          * Here we are preempting the current task. We want to make
660          * sure we are soft-disabled first
661          */
662         SOFT_DISABLE_INTS(r3,r4)
663 1:      bl      .preempt_schedule_irq
664
665         /* Re-test flags and eventually loop */
666         CURRENT_THREAD_INFO(r9, r1)
667         ld      r4,TI_FLAGS(r9)
668         andi.   r0,r4,_TIF_NEED_RESCHED
669         bne     1b
670
671         /*
672          * arch_local_irq_restore() from preempt_schedule_irq above may
673          * enable hard interrupt but we really should disable interrupts
674          * when we return from the interrupt, and so that we don't get
675          * interrupted after loading SRR0/1.
676          */
677 #ifdef CONFIG_PPC_BOOK3E
678         wrteei  0
679 #else
680         ld      r10,PACAKMSR(r13) /* Get kernel MSR without EE */
681         mtmsrd  r10,1             /* Update machine state */
682 #endif /* CONFIG_PPC_BOOK3E */
683 #endif /* CONFIG_PREEMPT */
684
685         .globl  fast_exc_return_irq
686 fast_exc_return_irq:
687 restore:
688         /*
689          * This is the main kernel exit path. First we check if we
690          * are about to re-enable interrupts
691          */
692         ld      r5,SOFTE(r1)
693         lbz     r6,PACASOFTIRQEN(r13)
694         cmpwi   cr0,r5,0
695         beq     restore_irq_off
696
697         /* We are enabling, were we already enabled ? Yes, just return */
698         cmpwi   cr0,r6,1
699         beq     cr0,do_restore
700
701         /*
702          * We are about to soft-enable interrupts (we are hard disabled
703          * at this point). We check if there's anything that needs to
704          * be replayed first.
705          */
706         lbz     r0,PACAIRQHAPPENED(r13)
707         cmpwi   cr0,r0,0
708         bne-    restore_check_irq_replay
709
710         /*
711          * Get here when nothing happened while soft-disabled, just
712          * soft-enable and move-on. We will hard-enable as a side
713          * effect of rfi
714          */
715 restore_no_replay:
716         TRACE_ENABLE_INTS
717         li      r0,1
718         stb     r0,PACASOFTIRQEN(r13);
719
720         /*
721          * Final return path. BookE is handled in a different file
722          */
723 do_restore:
724 #ifdef CONFIG_PPC_BOOK3E
725         b       .exception_return_book3e
726 #else
727         /*
728          * Clear the reservation. If we know the CPU tracks the address of
729          * the reservation then we can potentially save some cycles and use
730          * a larx. On POWER6 and POWER7 this is significantly faster.
731          */
732 BEGIN_FTR_SECTION
733         stdcx.  r0,0,r1         /* to clear the reservation */
734 FTR_SECTION_ELSE
735         ldarx   r4,0,r1
736 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
737
738         /*
739          * Some code path such as load_up_fpu or altivec return directly
740          * here. They run entirely hard disabled and do not alter the
741          * interrupt state. They also don't use lwarx/stwcx. and thus
742          * are known not to leave dangling reservations.
743          */
744         .globl  fast_exception_return
745 fast_exception_return:
746         ld      r3,_MSR(r1)
747         ld      r4,_CTR(r1)
748         ld      r0,_LINK(r1)
749         mtctr   r4
750         mtlr    r0
751         ld      r4,_XER(r1)
752         mtspr   SPRN_XER,r4
753
754         REST_8GPRS(5, r1)
755
756         andi.   r0,r3,MSR_RI
757         beq-    unrecov_restore
758
759         /*
760          * Clear RI before restoring r13.  If we are returning to
761          * userspace and we take an exception after restoring r13,
762          * we end up corrupting the userspace r13 value.
763          */
764         ld      r4,PACAKMSR(r13) /* Get kernel MSR without EE */
765         andc    r4,r4,r0         /* r0 contains MSR_RI here */
766         mtmsrd  r4,1
767
768         /*
769          * r13 is our per cpu area, only restore it if we are returning to
770          * userspace the value stored in the stack frame may belong to
771          * another CPU.
772          */
773         andi.   r0,r3,MSR_PR
774         beq     1f
775         ACCOUNT_CPU_USER_EXIT(r2, r4)
776         RESTORE_PPR(r2, r4)
777         REST_GPR(13, r1)
778 1:
779         mtspr   SPRN_SRR1,r3
780
781         ld      r2,_CCR(r1)
782         mtcrf   0xFF,r2
783         ld      r2,_NIP(r1)
784         mtspr   SPRN_SRR0,r2
785
786         ld      r0,GPR0(r1)
787         ld      r2,GPR2(r1)
788         ld      r3,GPR3(r1)
789         ld      r4,GPR4(r1)
790         ld      r1,GPR1(r1)
791
792         rfid
793         b       .       /* prevent speculative execution */
794
795 #endif /* CONFIG_PPC_BOOK3E */
796
797         /*
798          * We are returning to a context with interrupts soft disabled.
799          *
800          * However, we may also about to hard enable, so we need to
801          * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
802          * or that bit can get out of sync and bad things will happen
803          */
804 restore_irq_off:
805         ld      r3,_MSR(r1)
806         lbz     r7,PACAIRQHAPPENED(r13)
807         andi.   r0,r3,MSR_EE
808         beq     1f
809         rlwinm  r7,r7,0,~PACA_IRQ_HARD_DIS
810         stb     r7,PACAIRQHAPPENED(r13)
811 1:      li      r0,0
812         stb     r0,PACASOFTIRQEN(r13);
813         TRACE_DISABLE_INTS
814         b       do_restore
815
816         /*
817          * Something did happen, check if a re-emit is needed
818          * (this also clears paca->irq_happened)
819          */
820 restore_check_irq_replay:
821         /* XXX: We could implement a fast path here where we check
822          * for irq_happened being just 0x01, in which case we can
823          * clear it and return. That means that we would potentially
824          * miss a decrementer having wrapped all the way around.
825          *
826          * Still, this might be useful for things like hash_page
827          */
828         bl      .__check_irq_replay
829         cmpwi   cr0,r3,0
830         beq     restore_no_replay
831  
832         /*
833          * We need to re-emit an interrupt. We do so by re-using our
834          * existing exception frame. We first change the trap value,
835          * but we need to ensure we preserve the low nibble of it
836          */
837         ld      r4,_TRAP(r1)
838         clrldi  r4,r4,60
839         or      r4,r4,r3
840         std     r4,_TRAP(r1)
841
842         /*
843          * Then find the right handler and call it. Interrupts are
844          * still soft-disabled and we keep them that way.
845         */
846         cmpwi   cr0,r3,0x500
847         bne     1f
848         addi    r3,r1,STACK_FRAME_OVERHEAD;
849         bl      .do_IRQ
850         b       .ret_from_except
851 1:      cmpwi   cr0,r3,0x900
852         bne     1f
853         addi    r3,r1,STACK_FRAME_OVERHEAD;
854         bl      .timer_interrupt
855         b       .ret_from_except
856 #ifdef CONFIG_PPC_DOORBELL
857 1:
858 #ifdef CONFIG_PPC_BOOK3E
859         cmpwi   cr0,r3,0x280
860 #else
861         BEGIN_FTR_SECTION
862                 cmpwi   cr0,r3,0xe80
863         FTR_SECTION_ELSE
864                 cmpwi   cr0,r3,0xa00
865         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
866 #endif /* CONFIG_PPC_BOOK3E */
867         bne     1f
868         addi    r3,r1,STACK_FRAME_OVERHEAD;
869         bl      .doorbell_exception
870         b       .ret_from_except
871 #endif /* CONFIG_PPC_DOORBELL */
872 1:      b       .ret_from_except /* What else to do here ? */
873  
874 unrecov_restore:
875         addi    r3,r1,STACK_FRAME_OVERHEAD
876         bl      .unrecoverable_exception
877         b       unrecov_restore
878
879 #ifdef CONFIG_PPC_RTAS
880 /*
881  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
882  * called with the MMU off.
883  *
884  * In addition, we need to be in 32b mode, at least for now.
885  * 
886  * Note: r3 is an input parameter to rtas, so don't trash it...
887  */
888 _GLOBAL(enter_rtas)
889         mflr    r0
890         std     r0,16(r1)
891         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
892
893         /* Because RTAS is running in 32b mode, it clobbers the high order half
894          * of all registers that it saves.  We therefore save those registers
895          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
896          */
897         SAVE_GPR(2, r1)                 /* Save the TOC */
898         SAVE_GPR(13, r1)                /* Save paca */
899         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
900         SAVE_10GPRS(22, r1)             /* ditto */
901
902         mfcr    r4
903         std     r4,_CCR(r1)
904         mfctr   r5
905         std     r5,_CTR(r1)
906         mfspr   r6,SPRN_XER
907         std     r6,_XER(r1)
908         mfdar   r7
909         std     r7,_DAR(r1)
910         mfdsisr r8
911         std     r8,_DSISR(r1)
912
913         /* Temporary workaround to clear CR until RTAS can be modified to
914          * ignore all bits.
915          */
916         li      r0,0
917         mtcr    r0
918
919 #ifdef CONFIG_BUG       
920         /* There is no way it is acceptable to get here with interrupts enabled,
921          * check it with the asm equivalent of WARN_ON
922          */
923         lbz     r0,PACASOFTIRQEN(r13)
924 1:      tdnei   r0,0
925         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
926 #endif
927         
928         /* Hard-disable interrupts */
929         mfmsr   r6
930         rldicl  r7,r6,48,1
931         rotldi  r7,r7,16
932         mtmsrd  r7,1
933
934         /* Unfortunately, the stack pointer and the MSR are also clobbered,
935          * so they are saved in the PACA which allows us to restore
936          * our original state after RTAS returns.
937          */
938         std     r1,PACAR1(r13)
939         std     r6,PACASAVEDMSR(r13)
940
941         /* Setup our real return addr */        
942         LOAD_REG_ADDR(r4,.rtas_return_loc)
943         clrldi  r4,r4,2                 /* convert to realmode address */
944         mtlr    r4
945
946         li      r0,0
947         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
948         andc    r0,r6,r0
949         
950         li      r9,1
951         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
952         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
953         andc    r6,r0,r9
954         sync                            /* disable interrupts so SRR0/1 */
955         mtmsrd  r0                      /* don't get trashed */
956
957         LOAD_REG_ADDR(r4, rtas)
958         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
959         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
960         
961         mtspr   SPRN_SRR0,r5
962         mtspr   SPRN_SRR1,r6
963         rfid
964         b       .       /* prevent speculative execution */
965
966 _STATIC(rtas_return_loc)
967         /* relocation is off at this point */
968         GET_PACA(r4)
969         clrldi  r4,r4,2                 /* convert to realmode address */
970
971         bcl     20,31,$+4
972 0:      mflr    r3
973         ld      r3,(1f-0b)(r3)          /* get &.rtas_restore_regs */
974
975         mfmsr   r6
976         li      r0,MSR_RI
977         andc    r6,r6,r0
978         sync    
979         mtmsrd  r6
980         
981         ld      r1,PACAR1(r4)           /* Restore our SP */
982         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
983
984         mtspr   SPRN_SRR0,r3
985         mtspr   SPRN_SRR1,r4
986         rfid
987         b       .       /* prevent speculative execution */
988
989         .align  3
990 1:      .llong  .rtas_restore_regs
991
992 _STATIC(rtas_restore_regs)
993         /* relocation is on at this point */
994         REST_GPR(2, r1)                 /* Restore the TOC */
995         REST_GPR(13, r1)                /* Restore paca */
996         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
997         REST_10GPRS(22, r1)             /* ditto */
998
999         GET_PACA(r13)
1000
1001         ld      r4,_CCR(r1)
1002         mtcr    r4
1003         ld      r5,_CTR(r1)
1004         mtctr   r5
1005         ld      r6,_XER(r1)
1006         mtspr   SPRN_XER,r6
1007         ld      r7,_DAR(r1)
1008         mtdar   r7
1009         ld      r8,_DSISR(r1)
1010         mtdsisr r8
1011
1012         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
1013         ld      r0,16(r1)               /* get return address */
1014
1015         mtlr    r0
1016         blr                             /* return to caller */
1017
1018 #endif /* CONFIG_PPC_RTAS */
1019
1020 _GLOBAL(enter_prom)
1021         mflr    r0
1022         std     r0,16(r1)
1023         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
1024
1025         /* Because PROM is running in 32b mode, it clobbers the high order half
1026          * of all registers that it saves.  We therefore save those registers
1027          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
1028          */
1029         SAVE_GPR(2, r1)
1030         SAVE_GPR(13, r1)
1031         SAVE_8GPRS(14, r1)
1032         SAVE_10GPRS(22, r1)
1033         mfcr    r10
1034         mfmsr   r11
1035         std     r10,_CCR(r1)
1036         std     r11,_MSR(r1)
1037
1038         /* Get the PROM entrypoint */
1039         mtlr    r4
1040
1041         /* Switch MSR to 32 bits mode
1042          */
1043 #ifdef CONFIG_PPC_BOOK3E
1044         rlwinm  r11,r11,0,1,31
1045         mtmsr   r11
1046 #else /* CONFIG_PPC_BOOK3E */
1047         mfmsr   r11
1048         li      r12,1
1049         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
1050         andc    r11,r11,r12
1051         li      r12,1
1052         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
1053         andc    r11,r11,r12
1054         mtmsrd  r11
1055 #endif /* CONFIG_PPC_BOOK3E */
1056         isync
1057
1058         /* Enter PROM here... */
1059         blrl
1060
1061         /* Just make sure that r1 top 32 bits didn't get
1062          * corrupt by OF
1063          */
1064         rldicl  r1,r1,0,32
1065
1066         /* Restore the MSR (back to 64 bits) */
1067         ld      r0,_MSR(r1)
1068         MTMSRD(r0)
1069         isync
1070
1071         /* Restore other registers */
1072         REST_GPR(2, r1)
1073         REST_GPR(13, r1)
1074         REST_8GPRS(14, r1)
1075         REST_10GPRS(22, r1)
1076         ld      r4,_CCR(r1)
1077         mtcr    r4
1078         
1079         addi    r1,r1,PROM_FRAME_SIZE
1080         ld      r0,16(r1)
1081         mtlr    r0
1082         blr
1083
1084 #ifdef CONFIG_FUNCTION_TRACER
1085 #ifdef CONFIG_DYNAMIC_FTRACE
1086 _GLOBAL(mcount)
1087 _GLOBAL(_mcount)
1088         blr
1089
1090 _GLOBAL(ftrace_caller)
1091         /* Taken from output of objdump from lib64/glibc */
1092         mflr    r3
1093         ld      r11, 0(r1)
1094         stdu    r1, -112(r1)
1095         std     r3, 128(r1)
1096         ld      r4, 16(r11)
1097         subi    r3, r3, MCOUNT_INSN_SIZE
1098 .globl ftrace_call
1099 ftrace_call:
1100         bl      ftrace_stub
1101         nop
1102 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1103 .globl ftrace_graph_call
1104 ftrace_graph_call:
1105         b       ftrace_graph_stub
1106 _GLOBAL(ftrace_graph_stub)
1107 #endif
1108         ld      r0, 128(r1)
1109         mtlr    r0
1110         addi    r1, r1, 112
1111 _GLOBAL(ftrace_stub)
1112         blr
1113 #else
1114 _GLOBAL(mcount)
1115         blr
1116
1117 _GLOBAL(_mcount)
1118         /* Taken from output of objdump from lib64/glibc */
1119         mflr    r3
1120         ld      r11, 0(r1)
1121         stdu    r1, -112(r1)
1122         std     r3, 128(r1)
1123         ld      r4, 16(r11)
1124
1125         subi    r3, r3, MCOUNT_INSN_SIZE
1126         LOAD_REG_ADDR(r5,ftrace_trace_function)
1127         ld      r5,0(r5)
1128         ld      r5,0(r5)
1129         mtctr   r5
1130         bctrl
1131         nop
1132
1133
1134 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1135         b       ftrace_graph_caller
1136 #endif
1137         ld      r0, 128(r1)
1138         mtlr    r0
1139         addi    r1, r1, 112
1140 _GLOBAL(ftrace_stub)
1141         blr
1142
1143 #endif /* CONFIG_DYNAMIC_FTRACE */
1144
1145 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1146 _GLOBAL(ftrace_graph_caller)
1147         /* load r4 with local address */
1148         ld      r4, 128(r1)
1149         subi    r4, r4, MCOUNT_INSN_SIZE
1150
1151         /* get the parent address */
1152         ld      r11, 112(r1)
1153         addi    r3, r11, 16
1154
1155         bl      .prepare_ftrace_return
1156         nop
1157
1158         ld      r0, 128(r1)
1159         mtlr    r0
1160         addi    r1, r1, 112
1161         blr
1162
1163 _GLOBAL(return_to_handler)
1164         /* need to save return values */
1165         std     r4,  -24(r1)
1166         std     r3,  -16(r1)
1167         std     r31, -8(r1)
1168         mr      r31, r1
1169         stdu    r1, -112(r1)
1170
1171         bl      .ftrace_return_to_handler
1172         nop
1173
1174         /* return value has real return address */
1175         mtlr    r3
1176
1177         ld      r1, 0(r1)
1178         ld      r4,  -24(r1)
1179         ld      r3,  -16(r1)
1180         ld      r31, -8(r1)
1181
1182         /* Jump back to real return address */
1183         blr
1184
1185 _GLOBAL(mod_return_to_handler)
1186         /* need to save return values */
1187         std     r4,  -32(r1)
1188         std     r3,  -24(r1)
1189         /* save TOC */
1190         std     r2,  -16(r1)
1191         std     r31, -8(r1)
1192         mr      r31, r1
1193         stdu    r1, -112(r1)
1194
1195         /*
1196          * We are in a module using the module's TOC.
1197          * Switch to our TOC to run inside the core kernel.
1198          */
1199         ld      r2, PACATOC(r13)
1200
1201         bl      .ftrace_return_to_handler
1202         nop
1203
1204         /* return value has real return address */
1205         mtlr    r3
1206
1207         ld      r1, 0(r1)
1208         ld      r4,  -32(r1)
1209         ld      r3,  -24(r1)
1210         ld      r2,  -16(r1)
1211         ld      r31, -8(r1)
1212
1213         /* Jump back to real return address */
1214         blr
1215 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1216 #endif /* CONFIG_FUNCTION_TRACER */