Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[firefly-linux-kernel-4.4.55.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_NATIVE) && 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_NATIVE && 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, r10)         /* 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_PPC_BOOK3S_64
452 BEGIN_FTR_SECTION
453         /*
454          * Back up the TAR across context switches.  Note that the TAR is not
455          * available for use in the kernel.  (To provide this, the TAR should
456          * be backed up/restored on exception entry/exit instead, and be in
457          * pt_regs.  FIXME, this should be in pt_regs anyway (for debug).)
458          */
459         mfspr   r0,SPRN_TAR
460         std     r0,THREAD_TAR(r3)
461 END_FTR_SECTION_IFSET(CPU_FTR_BCTAR)
462 #endif
463
464 #ifdef CONFIG_SMP
465         /* We need a sync somewhere here to make sure that if the
466          * previous task gets rescheduled on another CPU, it sees all
467          * stores it has performed on this one.
468          */
469         sync
470 #endif /* CONFIG_SMP */
471
472         /*
473          * If we optimise away the clear of the reservation in system
474          * calls because we know the CPU tracks the address of the
475          * reservation, then we need to clear it here to cover the
476          * case that the kernel context switch path has no larx
477          * instructions.
478          */
479 BEGIN_FTR_SECTION
480         ldarx   r6,0,r1
481 END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
482
483         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
484         std     r6,PACACURRENT(r13)     /* Set new 'current' */
485
486         ld      r8,KSP(r4)      /* new stack pointer */
487 #ifdef CONFIG_PPC_BOOK3S
488 BEGIN_FTR_SECTION
489   BEGIN_FTR_SECTION_NESTED(95)
490         clrrdi  r6,r8,28        /* get its ESID */
491         clrrdi  r9,r1,28        /* get current sp ESID */
492   FTR_SECTION_ELSE_NESTED(95)
493         clrrdi  r6,r8,40        /* get its 1T ESID */
494         clrrdi  r9,r1,40        /* get current sp 1T ESID */
495   ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
496 FTR_SECTION_ELSE
497         b       2f
498 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
499         clrldi. r0,r6,2         /* is new ESID c00000000? */
500         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
501         cror    eq,4*cr1+eq,eq
502         beq     2f              /* if yes, don't slbie it */
503
504         /* Bolt in the new stack SLB entry */
505         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
506         oris    r0,r6,(SLB_ESID_V)@h
507         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
508 BEGIN_FTR_SECTION
509         li      r9,MMU_SEGSIZE_1T       /* insert B field */
510         oris    r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
511         rldimi  r7,r9,SLB_VSID_SSIZE_SHIFT,0
512 END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
513
514         /* Update the last bolted SLB.  No write barriers are needed
515          * here, provided we only update the current CPU's SLB shadow
516          * buffer.
517          */
518         ld      r9,PACA_SLBSHADOWPTR(r13)
519         li      r12,0
520         std     r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
521         std     r7,SLBSHADOW_STACKVSID(r9)  /* Save VSID */
522         std     r0,SLBSHADOW_STACKESID(r9)  /* Save ESID */
523
524         /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
525          * we have 1TB segments, the only CPUs known to have the errata
526          * only support less than 1TB of system memory and we'll never
527          * actually hit this code path.
528          */
529
530         slbie   r6
531         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
532         slbmte  r7,r0
533         isync
534 2:
535 #endif /* !CONFIG_PPC_BOOK3S */
536
537         CURRENT_THREAD_INFO(r7, r8)  /* base of new stack */
538         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
539            because we don't need to leave the 288-byte ABI gap at the
540            top of the kernel stack. */
541         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
542
543         mr      r1,r8           /* start using new stack pointer */
544         std     r7,PACAKSAVE(r13)
545
546 #ifdef CONFIG_PPC_BOOK3S_64
547 BEGIN_FTR_SECTION
548         ld      r0,THREAD_TAR(r4)
549         mtspr   SPRN_TAR,r0
550 END_FTR_SECTION_IFSET(CPU_FTR_BCTAR)
551 #endif
552
553 #ifdef CONFIG_ALTIVEC
554 BEGIN_FTR_SECTION
555         ld      r0,THREAD_VRSAVE(r4)
556         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
557 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
558 #endif /* CONFIG_ALTIVEC */
559 #ifdef CONFIG_PPC64
560 BEGIN_FTR_SECTION
561         lwz     r6,THREAD_DSCR_INHERIT(r4)
562         ld      r7,DSCR_DEFAULT@toc(2)
563         ld      r0,THREAD_DSCR(r4)
564         cmpwi   r6,0
565         bne     1f
566         ld      r0,0(r7)
567 1:      cmpd    r0,r25
568         beq     2f
569         mtspr   SPRN_DSCR,r0
570 2:
571 END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
572 #endif
573
574         ld      r6,_CCR(r1)
575         mtcrf   0xFF,r6
576
577         /* r3-r13 are destroyed -- Cort */
578         REST_8GPRS(14, r1)
579         REST_10GPRS(22, r1)
580
581         /* convert old thread to its task_struct for return value */
582         addi    r3,r3,-THREAD
583         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
584         mtlr    r7
585         addi    r1,r1,SWITCH_FRAME_SIZE
586         blr
587
588         .align  7
589 _GLOBAL(ret_from_except)
590         ld      r11,_TRAP(r1)
591         andi.   r0,r11,1
592         bne     .ret_from_except_lite
593         REST_NVGPRS(r1)
594
595 _GLOBAL(ret_from_except_lite)
596         /*
597          * Disable interrupts so that current_thread_info()->flags
598          * can't change between when we test it and when we return
599          * from the interrupt.
600          */
601 #ifdef CONFIG_PPC_BOOK3E
602         wrteei  0
603 #else
604         ld      r10,PACAKMSR(r13) /* Get kernel MSR without EE */
605         mtmsrd  r10,1             /* Update machine state */
606 #endif /* CONFIG_PPC_BOOK3E */
607
608         CURRENT_THREAD_INFO(r9, r1)
609         ld      r3,_MSR(r1)
610         ld      r4,TI_FLAGS(r9)
611         andi.   r3,r3,MSR_PR
612         beq     resume_kernel
613
614         /* Check current_thread_info()->flags */
615         andi.   r0,r4,_TIF_USER_WORK_MASK
616         beq     restore
617
618         andi.   r0,r4,_TIF_NEED_RESCHED
619         beq     1f
620         bl      .restore_interrupts
621         bl      .schedule
622         b       .ret_from_except_lite
623
624 1:      bl      .save_nvgprs
625         bl      .restore_interrupts
626         addi    r3,r1,STACK_FRAME_OVERHEAD
627         bl      .do_notify_resume
628         b       .ret_from_except
629
630 resume_kernel:
631         /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
632         CURRENT_THREAD_INFO(r9, r1)
633         ld      r8,TI_FLAGS(r9)
634         andis.  r8,r8,_TIF_EMULATE_STACK_STORE@h
635         beq+    1f
636
637         addi    r8,r1,INT_FRAME_SIZE    /* Get the kprobed function entry */
638
639         lwz     r3,GPR1(r1)
640         subi    r3,r3,INT_FRAME_SIZE    /* dst: Allocate a trampoline exception frame */
641         mr      r4,r1                   /* src:  current exception frame */
642         mr      r1,r3                   /* Reroute the trampoline frame to r1 */
643
644         /* Copy from the original to the trampoline. */
645         li      r5,INT_FRAME_SIZE/8     /* size: INT_FRAME_SIZE */
646         li      r6,0                    /* start offset: 0 */
647         mtctr   r5
648 2:      ldx     r0,r6,r4
649         stdx    r0,r6,r3
650         addi    r6,r6,8
651         bdnz    2b
652
653         /* Do real store operation to complete stwu */
654         lwz     r5,GPR1(r1)
655         std     r8,0(r5)
656
657         /* Clear _TIF_EMULATE_STACK_STORE flag */
658         lis     r11,_TIF_EMULATE_STACK_STORE@h
659         addi    r5,r9,TI_FLAGS
660 0:      ldarx   r4,0,r5
661         andc    r4,r4,r11
662         stdcx.  r4,0,r5
663         bne-    0b
664 1:
665
666 #ifdef CONFIG_PREEMPT
667         /* Check if we need to preempt */
668         andi.   r0,r4,_TIF_NEED_RESCHED
669         beq+    restore
670         /* Check that preempt_count() == 0 and interrupts are enabled */
671         lwz     r8,TI_PREEMPT(r9)
672         cmpwi   cr1,r8,0
673         ld      r0,SOFTE(r1)
674         cmpdi   r0,0
675         crandc  eq,cr1*4+eq,eq
676         bne     restore
677
678         /*
679          * Here we are preempting the current task. We want to make
680          * sure we are soft-disabled first
681          */
682         SOFT_DISABLE_INTS(r3,r4)
683 1:      bl      .preempt_schedule_irq
684
685         /* Re-test flags and eventually loop */
686         CURRENT_THREAD_INFO(r9, r1)
687         ld      r4,TI_FLAGS(r9)
688         andi.   r0,r4,_TIF_NEED_RESCHED
689         bne     1b
690
691         /*
692          * arch_local_irq_restore() from preempt_schedule_irq above may
693          * enable hard interrupt but we really should disable interrupts
694          * when we return from the interrupt, and so that we don't get
695          * interrupted after loading SRR0/1.
696          */
697 #ifdef CONFIG_PPC_BOOK3E
698         wrteei  0
699 #else
700         ld      r10,PACAKMSR(r13) /* Get kernel MSR without EE */
701         mtmsrd  r10,1             /* Update machine state */
702 #endif /* CONFIG_PPC_BOOK3E */
703 #endif /* CONFIG_PREEMPT */
704
705         .globl  fast_exc_return_irq
706 fast_exc_return_irq:
707 restore:
708         /*
709          * This is the main kernel exit path. First we check if we
710          * are about to re-enable interrupts
711          */
712         ld      r5,SOFTE(r1)
713         lbz     r6,PACASOFTIRQEN(r13)
714         cmpwi   cr0,r5,0
715         beq     restore_irq_off
716
717         /* We are enabling, were we already enabled ? Yes, just return */
718         cmpwi   cr0,r6,1
719         beq     cr0,do_restore
720
721         /*
722          * We are about to soft-enable interrupts (we are hard disabled
723          * at this point). We check if there's anything that needs to
724          * be replayed first.
725          */
726         lbz     r0,PACAIRQHAPPENED(r13)
727         cmpwi   cr0,r0,0
728         bne-    restore_check_irq_replay
729
730         /*
731          * Get here when nothing happened while soft-disabled, just
732          * soft-enable and move-on. We will hard-enable as a side
733          * effect of rfi
734          */
735 restore_no_replay:
736         TRACE_ENABLE_INTS
737         li      r0,1
738         stb     r0,PACASOFTIRQEN(r13);
739
740         /*
741          * Final return path. BookE is handled in a different file
742          */
743 do_restore:
744 #ifdef CONFIG_PPC_BOOK3E
745         b       .exception_return_book3e
746 #else
747         /*
748          * Clear the reservation. If we know the CPU tracks the address of
749          * the reservation then we can potentially save some cycles and use
750          * a larx. On POWER6 and POWER7 this is significantly faster.
751          */
752 BEGIN_FTR_SECTION
753         stdcx.  r0,0,r1         /* to clear the reservation */
754 FTR_SECTION_ELSE
755         ldarx   r4,0,r1
756 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
757
758         /*
759          * Some code path such as load_up_fpu or altivec return directly
760          * here. They run entirely hard disabled and do not alter the
761          * interrupt state. They also don't use lwarx/stwcx. and thus
762          * are known not to leave dangling reservations.
763          */
764         .globl  fast_exception_return
765 fast_exception_return:
766         ld      r3,_MSR(r1)
767         ld      r4,_CTR(r1)
768         ld      r0,_LINK(r1)
769         mtctr   r4
770         mtlr    r0
771         ld      r4,_XER(r1)
772         mtspr   SPRN_XER,r4
773
774         REST_8GPRS(5, r1)
775
776         andi.   r0,r3,MSR_RI
777         beq-    unrecov_restore
778
779         /*
780          * Clear RI before restoring r13.  If we are returning to
781          * userspace and we take an exception after restoring r13,
782          * we end up corrupting the userspace r13 value.
783          */
784         ld      r4,PACAKMSR(r13) /* Get kernel MSR without EE */
785         andc    r4,r4,r0         /* r0 contains MSR_RI here */
786         mtmsrd  r4,1
787
788 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
789         /* TM debug */
790         std     r3, PACATMSCRATCH(r13) /* Stash returned-to MSR */
791 #endif
792         /*
793          * r13 is our per cpu area, only restore it if we are returning to
794          * userspace the value stored in the stack frame may belong to
795          * another CPU.
796          */
797         andi.   r0,r3,MSR_PR
798         beq     1f
799         ACCOUNT_CPU_USER_EXIT(r2, r4)
800         RESTORE_PPR(r2, r4)
801         REST_GPR(13, r1)
802 1:
803         mtspr   SPRN_SRR1,r3
804
805         ld      r2,_CCR(r1)
806         mtcrf   0xFF,r2
807         ld      r2,_NIP(r1)
808         mtspr   SPRN_SRR0,r2
809
810         ld      r0,GPR0(r1)
811         ld      r2,GPR2(r1)
812         ld      r3,GPR3(r1)
813         ld      r4,GPR4(r1)
814         ld      r1,GPR1(r1)
815
816         rfid
817         b       .       /* prevent speculative execution */
818
819 #endif /* CONFIG_PPC_BOOK3E */
820
821         /*
822          * We are returning to a context with interrupts soft disabled.
823          *
824          * However, we may also about to hard enable, so we need to
825          * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
826          * or that bit can get out of sync and bad things will happen
827          */
828 restore_irq_off:
829         ld      r3,_MSR(r1)
830         lbz     r7,PACAIRQHAPPENED(r13)
831         andi.   r0,r3,MSR_EE
832         beq     1f
833         rlwinm  r7,r7,0,~PACA_IRQ_HARD_DIS
834         stb     r7,PACAIRQHAPPENED(r13)
835 1:      li      r0,0
836         stb     r0,PACASOFTIRQEN(r13);
837         TRACE_DISABLE_INTS
838         b       do_restore
839
840         /*
841          * Something did happen, check if a re-emit is needed
842          * (this also clears paca->irq_happened)
843          */
844 restore_check_irq_replay:
845         /* XXX: We could implement a fast path here where we check
846          * for irq_happened being just 0x01, in which case we can
847          * clear it and return. That means that we would potentially
848          * miss a decrementer having wrapped all the way around.
849          *
850          * Still, this might be useful for things like hash_page
851          */
852         bl      .__check_irq_replay
853         cmpwi   cr0,r3,0
854         beq     restore_no_replay
855  
856         /*
857          * We need to re-emit an interrupt. We do so by re-using our
858          * existing exception frame. We first change the trap value,
859          * but we need to ensure we preserve the low nibble of it
860          */
861         ld      r4,_TRAP(r1)
862         clrldi  r4,r4,60
863         or      r4,r4,r3
864         std     r4,_TRAP(r1)
865
866         /*
867          * Then find the right handler and call it. Interrupts are
868          * still soft-disabled and we keep them that way.
869         */
870         cmpwi   cr0,r3,0x500
871         bne     1f
872         addi    r3,r1,STACK_FRAME_OVERHEAD;
873         bl      .do_IRQ
874         b       .ret_from_except
875 1:      cmpwi   cr0,r3,0x900
876         bne     1f
877         addi    r3,r1,STACK_FRAME_OVERHEAD;
878         bl      .timer_interrupt
879         b       .ret_from_except
880 #ifdef CONFIG_PPC_DOORBELL
881 1:
882 #ifdef CONFIG_PPC_BOOK3E
883         cmpwi   cr0,r3,0x280
884 #else
885         BEGIN_FTR_SECTION
886                 cmpwi   cr0,r3,0xe80
887         FTR_SECTION_ELSE
888                 cmpwi   cr0,r3,0xa00
889         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
890 #endif /* CONFIG_PPC_BOOK3E */
891         bne     1f
892         addi    r3,r1,STACK_FRAME_OVERHEAD;
893         bl      .doorbell_exception
894         b       .ret_from_except
895 #endif /* CONFIG_PPC_DOORBELL */
896 1:      b       .ret_from_except /* What else to do here ? */
897  
898 unrecov_restore:
899         addi    r3,r1,STACK_FRAME_OVERHEAD
900         bl      .unrecoverable_exception
901         b       unrecov_restore
902
903 #ifdef CONFIG_PPC_RTAS
904 /*
905  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
906  * called with the MMU off.
907  *
908  * In addition, we need to be in 32b mode, at least for now.
909  * 
910  * Note: r3 is an input parameter to rtas, so don't trash it...
911  */
912 _GLOBAL(enter_rtas)
913         mflr    r0
914         std     r0,16(r1)
915         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
916
917         /* Because RTAS is running in 32b mode, it clobbers the high order half
918          * of all registers that it saves.  We therefore save those registers
919          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
920          */
921         SAVE_GPR(2, r1)                 /* Save the TOC */
922         SAVE_GPR(13, r1)                /* Save paca */
923         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
924         SAVE_10GPRS(22, r1)             /* ditto */
925
926         mfcr    r4
927         std     r4,_CCR(r1)
928         mfctr   r5
929         std     r5,_CTR(r1)
930         mfspr   r6,SPRN_XER
931         std     r6,_XER(r1)
932         mfdar   r7
933         std     r7,_DAR(r1)
934         mfdsisr r8
935         std     r8,_DSISR(r1)
936
937         /* Temporary workaround to clear CR until RTAS can be modified to
938          * ignore all bits.
939          */
940         li      r0,0
941         mtcr    r0
942
943 #ifdef CONFIG_BUG       
944         /* There is no way it is acceptable to get here with interrupts enabled,
945          * check it with the asm equivalent of WARN_ON
946          */
947         lbz     r0,PACASOFTIRQEN(r13)
948 1:      tdnei   r0,0
949         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
950 #endif
951         
952         /* Hard-disable interrupts */
953         mfmsr   r6
954         rldicl  r7,r6,48,1
955         rotldi  r7,r7,16
956         mtmsrd  r7,1
957
958         /* Unfortunately, the stack pointer and the MSR are also clobbered,
959          * so they are saved in the PACA which allows us to restore
960          * our original state after RTAS returns.
961          */
962         std     r1,PACAR1(r13)
963         std     r6,PACASAVEDMSR(r13)
964
965         /* Setup our real return addr */        
966         LOAD_REG_ADDR(r4,.rtas_return_loc)
967         clrldi  r4,r4,2                 /* convert to realmode address */
968         mtlr    r4
969
970         li      r0,0
971         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
972         andc    r0,r6,r0
973         
974         li      r9,1
975         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
976         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
977         andc    r6,r0,r9
978         sync                            /* disable interrupts so SRR0/1 */
979         mtmsrd  r0                      /* don't get trashed */
980
981         LOAD_REG_ADDR(r4, rtas)
982         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
983         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
984         
985         mtspr   SPRN_SRR0,r5
986         mtspr   SPRN_SRR1,r6
987         rfid
988         b       .       /* prevent speculative execution */
989
990 _STATIC(rtas_return_loc)
991         /* relocation is off at this point */
992         GET_PACA(r4)
993         clrldi  r4,r4,2                 /* convert to realmode address */
994
995         bcl     20,31,$+4
996 0:      mflr    r3
997         ld      r3,(1f-0b)(r3)          /* get &.rtas_restore_regs */
998
999         mfmsr   r6
1000         li      r0,MSR_RI
1001         andc    r6,r6,r0
1002         sync    
1003         mtmsrd  r6
1004         
1005         ld      r1,PACAR1(r4)           /* Restore our SP */
1006         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
1007
1008         mtspr   SPRN_SRR0,r3
1009         mtspr   SPRN_SRR1,r4
1010         rfid
1011         b       .       /* prevent speculative execution */
1012
1013         .align  3
1014 1:      .llong  .rtas_restore_regs
1015
1016 _STATIC(rtas_restore_regs)
1017         /* relocation is on at this point */
1018         REST_GPR(2, r1)                 /* Restore the TOC */
1019         REST_GPR(13, r1)                /* Restore paca */
1020         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
1021         REST_10GPRS(22, r1)             /* ditto */
1022
1023         GET_PACA(r13)
1024
1025         ld      r4,_CCR(r1)
1026         mtcr    r4
1027         ld      r5,_CTR(r1)
1028         mtctr   r5
1029         ld      r6,_XER(r1)
1030         mtspr   SPRN_XER,r6
1031         ld      r7,_DAR(r1)
1032         mtdar   r7
1033         ld      r8,_DSISR(r1)
1034         mtdsisr r8
1035
1036         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
1037         ld      r0,16(r1)               /* get return address */
1038
1039         mtlr    r0
1040         blr                             /* return to caller */
1041
1042 #endif /* CONFIG_PPC_RTAS */
1043
1044 _GLOBAL(enter_prom)
1045         mflr    r0
1046         std     r0,16(r1)
1047         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
1048
1049         /* Because PROM is running in 32b mode, it clobbers the high order half
1050          * of all registers that it saves.  We therefore save those registers
1051          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
1052          */
1053         SAVE_GPR(2, r1)
1054         SAVE_GPR(13, r1)
1055         SAVE_8GPRS(14, r1)
1056         SAVE_10GPRS(22, r1)
1057         mfcr    r10
1058         mfmsr   r11
1059         std     r10,_CCR(r1)
1060         std     r11,_MSR(r1)
1061
1062         /* Get the PROM entrypoint */
1063         mtlr    r4
1064
1065         /* Switch MSR to 32 bits mode
1066          */
1067 #ifdef CONFIG_PPC_BOOK3E
1068         rlwinm  r11,r11,0,1,31
1069         mtmsr   r11
1070 #else /* CONFIG_PPC_BOOK3E */
1071         mfmsr   r11
1072         li      r12,1
1073         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
1074         andc    r11,r11,r12
1075         li      r12,1
1076         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
1077         andc    r11,r11,r12
1078         mtmsrd  r11
1079 #endif /* CONFIG_PPC_BOOK3E */
1080         isync
1081
1082         /* Enter PROM here... */
1083         blrl
1084
1085         /* Just make sure that r1 top 32 bits didn't get
1086          * corrupt by OF
1087          */
1088         rldicl  r1,r1,0,32
1089
1090         /* Restore the MSR (back to 64 bits) */
1091         ld      r0,_MSR(r1)
1092         MTMSRD(r0)
1093         isync
1094
1095         /* Restore other registers */
1096         REST_GPR(2, r1)
1097         REST_GPR(13, r1)
1098         REST_8GPRS(14, r1)
1099         REST_10GPRS(22, r1)
1100         ld      r4,_CCR(r1)
1101         mtcr    r4
1102         
1103         addi    r1,r1,PROM_FRAME_SIZE
1104         ld      r0,16(r1)
1105         mtlr    r0
1106         blr
1107
1108 #ifdef CONFIG_FUNCTION_TRACER
1109 #ifdef CONFIG_DYNAMIC_FTRACE
1110 _GLOBAL(mcount)
1111 _GLOBAL(_mcount)
1112         blr
1113
1114 _GLOBAL(ftrace_caller)
1115         /* Taken from output of objdump from lib64/glibc */
1116         mflr    r3
1117         ld      r11, 0(r1)
1118         stdu    r1, -112(r1)
1119         std     r3, 128(r1)
1120         ld      r4, 16(r11)
1121         subi    r3, r3, MCOUNT_INSN_SIZE
1122 .globl ftrace_call
1123 ftrace_call:
1124         bl      ftrace_stub
1125         nop
1126 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1127 .globl ftrace_graph_call
1128 ftrace_graph_call:
1129         b       ftrace_graph_stub
1130 _GLOBAL(ftrace_graph_stub)
1131 #endif
1132         ld      r0, 128(r1)
1133         mtlr    r0
1134         addi    r1, r1, 112
1135 _GLOBAL(ftrace_stub)
1136         blr
1137 #else
1138 _GLOBAL(mcount)
1139         blr
1140
1141 _GLOBAL(_mcount)
1142         /* Taken from output of objdump from lib64/glibc */
1143         mflr    r3
1144         ld      r11, 0(r1)
1145         stdu    r1, -112(r1)
1146         std     r3, 128(r1)
1147         ld      r4, 16(r11)
1148
1149         subi    r3, r3, MCOUNT_INSN_SIZE
1150         LOAD_REG_ADDR(r5,ftrace_trace_function)
1151         ld      r5,0(r5)
1152         ld      r5,0(r5)
1153         mtctr   r5
1154         bctrl
1155         nop
1156
1157
1158 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1159         b       ftrace_graph_caller
1160 #endif
1161         ld      r0, 128(r1)
1162         mtlr    r0
1163         addi    r1, r1, 112
1164 _GLOBAL(ftrace_stub)
1165         blr
1166
1167 #endif /* CONFIG_DYNAMIC_FTRACE */
1168
1169 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1170 _GLOBAL(ftrace_graph_caller)
1171         /* load r4 with local address */
1172         ld      r4, 128(r1)
1173         subi    r4, r4, MCOUNT_INSN_SIZE
1174
1175         /* get the parent address */
1176         ld      r11, 112(r1)
1177         addi    r3, r11, 16
1178
1179         bl      .prepare_ftrace_return
1180         nop
1181
1182         ld      r0, 128(r1)
1183         mtlr    r0
1184         addi    r1, r1, 112
1185         blr
1186
1187 _GLOBAL(return_to_handler)
1188         /* need to save return values */
1189         std     r4,  -24(r1)
1190         std     r3,  -16(r1)
1191         std     r31, -8(r1)
1192         mr      r31, r1
1193         stdu    r1, -112(r1)
1194
1195         bl      .ftrace_return_to_handler
1196         nop
1197
1198         /* return value has real return address */
1199         mtlr    r3
1200
1201         ld      r1, 0(r1)
1202         ld      r4,  -24(r1)
1203         ld      r3,  -16(r1)
1204         ld      r31, -8(r1)
1205
1206         /* Jump back to real return address */
1207         blr
1208
1209 _GLOBAL(mod_return_to_handler)
1210         /* need to save return values */
1211         std     r4,  -32(r1)
1212         std     r3,  -24(r1)
1213         /* save TOC */
1214         std     r2,  -16(r1)
1215         std     r31, -8(r1)
1216         mr      r31, r1
1217         stdu    r1, -112(r1)
1218
1219         /*
1220          * We are in a module using the module's TOC.
1221          * Switch to our TOC to run inside the core kernel.
1222          */
1223         ld      r2, PACATOC(r13)
1224
1225         bl      .ftrace_return_to_handler
1226         nop
1227
1228         /* return value has real return address */
1229         mtlr    r3
1230
1231         ld      r1, 0(r1)
1232         ld      r4,  -32(r1)
1233         ld      r3,  -24(r1)
1234         ld      r2,  -16(r1)
1235         ld      r31, -8(r1)
1236
1237         /* Jump back to real return address */
1238         blr
1239 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1240 #endif /* CONFIG_FUNCTION_TRACER */