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