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