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