blob: ba72ddbe52389d846320a2cf19db78894a82a2da [file] [log] [blame]
Paul Mackerras9994a332005-10-10 22:36:14 +10001/*
Paul Mackerras9994a332005-10-10 22:36:14 +10002 * 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
Paul Mackerras9994a332005-10-10 22:36:14 +100021#include <linux/errno.h>
22#include <asm/unistd.h>
23#include <asm/processor.h>
24#include <asm/page.h>
25#include <asm/mmu.h>
26#include <asm/thread_info.h>
27#include <asm/ppc_asm.h>
28#include <asm/asm-offsets.h>
29#include <asm/cputable.h>
Stephen Rothwell3f639ee2006-09-25 18:19:00 +100030#include <asm/firmware.h>
David Woodhouse007d88d2007-01-01 18:45:34 +000031#include <asm/bug.h>
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +100032#include <asm/ptrace.h>
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100033#include <asm/irqflags.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034#include <asm/ftrace.h>
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110035#include <asm/hw_irq.h>
Paul Mackerras9994a332005-10-10 22:36:14 +100036
37/*
38 * System calls.
39 */
40 .section ".toc","aw"
41.SYS_CALL_TABLE:
42 .tc .sys_call_table[TC],.sys_call_table
43
44/* This value is used to mark exception frames on the stack. */
45exception_marker:
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +100046 .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
Paul Mackerras9994a332005-10-10 22:36:14 +100047
48 .section ".text"
49 .align 7
50
51#undef SHOW_SYSCALLS
52
53 .globl system_call_common
54system_call_common:
55 andi. r10,r12,MSR_PR
56 mr r10,r1
57 addi r1,r1,-INT_FRAME_SIZE
58 beq- 1f
59 ld r1,PACAKSAVE(r13)
601: std r10,0(r1)
61 std r11,_NIP(r1)
62 std r12,_MSR(r1)
63 std r0,GPR0(r1)
64 std r10,GPR1(r1)
Paul Mackerrasc6622f62006-02-24 10:06:59 +110065 ACCOUNT_CPU_USER_ENTRY(r10, r11)
Paul Mackerrasab598b62008-11-30 11:49:45 +000066 /*
67 * This "crclr so" clears CR0.SO, which is the error indication on
68 * return from this system call. There must be no cmp instruction
69 * between it and the "mfcr r9" below, otherwise if XER.SO is set,
70 * CR0.SO will get set, causing all system calls to appear to fail.
71 */
72 crclr so
Paul Mackerras9994a332005-10-10 22:36:14 +100073 std r2,GPR2(r1)
74 std r3,GPR3(r1)
75 std r4,GPR4(r1)
76 std r5,GPR5(r1)
77 std r6,GPR6(r1)
78 std r7,GPR7(r1)
79 std r8,GPR8(r1)
80 li r11,0
81 std r11,GPR9(r1)
82 std r11,GPR10(r1)
83 std r11,GPR11(r1)
84 std r11,GPR12(r1)
Anton Blanchard823df432012-04-04 18:24:29 +000085 std r11,_XER(r1)
Anton Blanchard82087412012-04-04 18:26:39 +000086 std r11,_CTR(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +100087 std r9,GPR13(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +100088 mfcr r9
89 mflr r10
90 li r11,0xc01
91 std r9,_CCR(r1)
92 std r10,_LINK(r1)
93 std r11,_TRAP(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +100094 std r3,ORIG_GPR3(r1)
95 ld r2,PACATOC(r13)
96 addi r9,r1,STACK_FRAME_OVERHEAD
97 ld r11,exception_marker@toc(r2)
98 std r11,-16(r9) /* "regshere" marker */
Paul Mackerrascf9efce2010-08-26 19:56:43 +000099#if defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR)
100BEGIN_FW_FTR_SECTION
101 beq 33f
102 /* if from user, see if there are any DTL entries to process */
103 ld r10,PACALPPACAPTR(r13) /* get ptr to VPA */
104 ld r11,PACA_DTL_RIDX(r13) /* get log read index */
105 ld r10,LPPACA_DTLIDX(r10) /* get log write index */
106 cmpd cr1,r11,r10
107 beq+ cr1,33f
108 bl .accumulate_stolen_time
109 REST_GPR(0,r1)
110 REST_4GPRS(3,r1)
111 REST_2GPRS(7,r1)
112 addi r9,r1,STACK_FRAME_OVERHEAD
11333:
114END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
115#endif /* CONFIG_VIRT_CPU_ACCOUNTING && CONFIG_PPC_SPLPAR */
116
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100117 /*
118 * A syscall should always be called with interrupts enabled
119 * so we just unconditionally hard-enable here. When some kind
120 * of irq tracing is used, we additionally check that condition
121 * is correct
122 */
123#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
124 lbz r10,PACASOFTIRQEN(r13)
125 xori r10,r10,1
1261: tdnei r10,0
127 EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
128#endif
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000129
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000130#ifdef CONFIG_PPC_BOOK3E
131 wrteei 1
132#else
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100133 ld r11,PACAKMSR(r13)
Paul Mackerras9994a332005-10-10 22:36:14 +1000134 ori r11,r11,MSR_EE
135 mtmsrd r11,1
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000136#endif /* CONFIG_PPC_BOOK3E */
Paul Mackerras9994a332005-10-10 22:36:14 +1000137
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100138 /* We do need to set SOFTE in the stack frame or the return
139 * from interrupt will be painful
140 */
141 li r10,1
142 std r10,SOFTE(r1)
143
Paul Mackerras9994a332005-10-10 22:36:14 +1000144#ifdef SHOW_SYSCALLS
145 bl .do_show_syscall
146 REST_GPR(0,r1)
147 REST_4GPRS(3,r1)
148 REST_2GPRS(7,r1)
149 addi r9,r1,STACK_FRAME_OVERHEAD
150#endif
151 clrrdi r11,r1,THREAD_SHIFT
Paul Mackerras9994a332005-10-10 22:36:14 +1000152 ld r10,TI_FLAGS(r11)
Paul Mackerras9994a332005-10-10 22:36:14 +1000153 andi. r11,r10,_TIF_SYSCALL_T_OR_A
154 bne- syscall_dotrace
Anton Blanchardd14299d2012-04-04 18:23:27 +0000155.Lsyscall_dotrace_cont:
Paul Mackerras9994a332005-10-10 22:36:14 +1000156 cmpldi 0,r0,NR_syscalls
157 bge- syscall_enosys
158
159system_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
17415:
175 slwi r0,r0,4
176 ldx r10,r11,r0 /* Fetch system call handler [ptr] */
177 mtctr r10
178 bctrl /* Call handler */
179
180syscall_exit:
Paul Mackerras9994a332005-10-10 22:36:14 +1000181 std r3,RESULT(r1)
David Woodhouse401d1f02005-11-15 18:52:18 +0000182#ifdef SHOW_SYSCALLS
183 bl .do_show_syscall_exit
184 ld r3,RESULT(r1)
185#endif
Paul Mackerras9994a332005-10-10 22:36:14 +1000186 clrrdi r12,r1,THREAD_SHIFT
Paul Mackerras9994a332005-10-10 22:36:14 +1000187
Paul Mackerras9994a332005-10-10 22:36:14 +1000188 ld r8,_MSR(r1)
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000189#ifdef CONFIG_PPC_BOOK3S
190 /* No MSR:RI on BookE */
Paul Mackerras9994a332005-10-10 22:36:14 +1000191 andi. r10,r8,MSR_RI
192 beq- unrecov_restore
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000193#endif
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100194 /*
195 * Disable interrupts so current_thread_info()->flags can't change,
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000196 * and so that we don't get interrupted after loading SRR0/1.
197 */
198#ifdef CONFIG_PPC_BOOK3E
199 wrteei 0
200#else
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100201 ld r10,PACAKMSR(r13)
Paul Mackerras9994a332005-10-10 22:36:14 +1000202 mtmsrd r10,1
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000203#endif /* CONFIG_PPC_BOOK3E */
204
Paul Mackerras9994a332005-10-10 22:36:14 +1000205 ld r9,TI_FLAGS(r12)
David Woodhouse401d1f02005-11-15 18:52:18 +0000206 li r11,-_LAST_ERRNO
Paul Mackerras1bd79332006-03-08 13:24:22 +1100207 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
Paul Mackerras9994a332005-10-10 22:36:14 +1000208 bne- syscall_exit_work
David Woodhouse401d1f02005-11-15 18:52:18 +0000209 cmpld r3,r11
210 ld r5,_CCR(r1)
211 bge- syscall_error
Anton Blanchardd14299d2012-04-04 18:23:27 +0000212.Lsyscall_error_cont:
Paul Mackerras9994a332005-10-10 22:36:14 +1000213 ld r7,_NIP(r1)
Anton Blanchardf89451f2010-08-11 01:40:27 +0000214BEGIN_FTR_SECTION
Paul Mackerras9994a332005-10-10 22:36:14 +1000215 stdcx. r0,0,r1 /* to clear the reservation */
Anton Blanchardf89451f2010-08-11 01:40:27 +0000216END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
Paul Mackerras9994a332005-10-10 22:36:14 +1000217 andi. r6,r8,MSR_PR
218 ld r4,_LINK(r1)
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100219 /*
220 * Clear RI before restoring r13. If we are returning to
221 * userspace and we take an exception after restoring r13,
222 * we end up corrupting the userspace r13 value.
223 */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000224#ifdef CONFIG_PPC_BOOK3S
225 /* No MSR:RI on BookE */
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100226 li r12,MSR_RI
227 andc r11,r10,r12
228 mtmsrd r11,1 /* clear MSR.RI */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000229#endif /* CONFIG_PPC_BOOK3S */
230
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100231 beq- 1f
232 ACCOUNT_CPU_USER_EXIT(r11, r12)
233 ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
Paul Mackerras9994a332005-10-10 22:36:14 +10002341: ld r2,GPR2(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +1000235 ld r1,GPR1(r1)
236 mtlr r4
237 mtcr r5
238 mtspr SPRN_SRR0,r7
239 mtspr SPRN_SRR1,r8
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000240 RFI
Paul Mackerras9994a332005-10-10 22:36:14 +1000241 b . /* prevent speculative execution */
242
David Woodhouse401d1f02005-11-15 18:52:18 +0000243syscall_error:
Paul Mackerras9994a332005-10-10 22:36:14 +1000244 oris r5,r5,0x1000 /* Set SO bit in CR */
David Woodhouse401d1f02005-11-15 18:52:18 +0000245 neg r3,r3
Paul Mackerras9994a332005-10-10 22:36:14 +1000246 std r5,_CCR(r1)
Anton Blanchardd14299d2012-04-04 18:23:27 +0000247 b .Lsyscall_error_cont
David Woodhouse401d1f02005-11-15 18:52:18 +0000248
Paul Mackerras9994a332005-10-10 22:36:14 +1000249/* Traced system call support */
250syscall_dotrace:
251 bl .save_nvgprs
252 addi r3,r1,STACK_FRAME_OVERHEAD
253 bl .do_syscall_trace_enter
Roland McGrath4f72c422008-07-27 16:51:03 +1000254 /*
255 * Restore argument registers possibly just changed.
256 * We use the return value of do_syscall_trace_enter
257 * for the call number to look up in the table (r0).
258 */
259 mr r0,r3
Paul Mackerras9994a332005-10-10 22:36:14 +1000260 ld r3,GPR3(r1)
261 ld r4,GPR4(r1)
262 ld r5,GPR5(r1)
263 ld r6,GPR6(r1)
264 ld r7,GPR7(r1)
265 ld r8,GPR8(r1)
266 addi r9,r1,STACK_FRAME_OVERHEAD
267 clrrdi r10,r1,THREAD_SHIFT
268 ld r10,TI_FLAGS(r10)
Anton Blanchardd14299d2012-04-04 18:23:27 +0000269 b .Lsyscall_dotrace_cont
Paul Mackerras9994a332005-10-10 22:36:14 +1000270
David Woodhouse401d1f02005-11-15 18:52:18 +0000271syscall_enosys:
272 li r3,-ENOSYS
273 b syscall_exit
274
275syscall_exit_work:
276 /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
277 If TIF_NOERROR is set, just save r3 as it is. */
278
279 andi. r0,r9,_TIF_RESTOREALL
Paul Mackerras1bd79332006-03-08 13:24:22 +1100280 beq+ 0f
281 REST_NVGPRS(r1)
282 b 2f
2830: cmpld r3,r11 /* r10 is -LAST_ERRNO */
David Woodhouse401d1f02005-11-15 18:52:18 +0000284 blt+ 1f
285 andi. r0,r9,_TIF_NOERROR
286 bne- 1f
287 ld r5,_CCR(r1)
288 neg r3,r3
289 oris r5,r5,0x1000 /* Set SO bit in CR */
290 std r5,_CCR(r1)
2911: std r3,GPR3(r1)
2922: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
293 beq 4f
294
Paul Mackerras1bd79332006-03-08 13:24:22 +1100295 /* Clear per-syscall TIF flags if any are set. */
David Woodhouse401d1f02005-11-15 18:52:18 +0000296
297 li r11,_TIF_PERSYSCALL_MASK
298 addi r12,r12,TI_FLAGS
2993: ldarx r10,0,r12
300 andc r10,r10,r11
301 stdcx. r10,0,r12
302 bne- 3b
303 subi r12,r12,TI_FLAGS
Paul Mackerras1bd79332006-03-08 13:24:22 +1100304
3054: /* Anything else left to do? */
306 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
David Woodhouse401d1f02005-11-15 18:52:18 +0000307 beq .ret_from_except_lite
308
309 /* Re-enable interrupts */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000310#ifdef CONFIG_PPC_BOOK3E
311 wrteei 1
312#else
Benjamin Herrenschmidt1421ae02012-03-01 15:40:23 +1100313 ld r10,PACAKMSR(r13)
David Woodhouse401d1f02005-11-15 18:52:18 +0000314 ori r10,r10,MSR_EE
315 mtmsrd r10,1
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000316#endif /* CONFIG_PPC_BOOK3E */
David Woodhouse401d1f02005-11-15 18:52:18 +0000317
Paul Mackerras1bd79332006-03-08 13:24:22 +1100318 bl .save_nvgprs
Paul Mackerras9994a332005-10-10 22:36:14 +1000319 addi r3,r1,STACK_FRAME_OVERHEAD
320 bl .do_syscall_trace_leave
Paul Mackerras1bd79332006-03-08 13:24:22 +1100321 b .ret_from_except
Paul Mackerras9994a332005-10-10 22:36:14 +1000322
323/* Save non-volatile GPRs, if not already saved. */
324_GLOBAL(save_nvgprs)
325 ld r11,_TRAP(r1)
326 andi. r0,r11,1
327 beqlr-
328 SAVE_NVGPRS(r1)
329 clrrdi r0,r11,1
330 std r0,_TRAP(r1)
331 blr
332
David Woodhouse401d1f02005-11-15 18:52:18 +0000333
Paul Mackerras9994a332005-10-10 22:36:14 +1000334/*
335 * The sigsuspend and rt_sigsuspend system calls can call do_signal
336 * and thus put the process into the stopped state where we might
337 * want to examine its user state with ptrace. Therefore we need
338 * to save all the nonvolatile registers (r14 - r31) before calling
339 * the C code. Similarly, fork, vfork and clone need the full
340 * register state on the stack so that it can be copied to the child.
341 */
Paul Mackerras9994a332005-10-10 22:36:14 +1000342
343_GLOBAL(ppc_fork)
344 bl .save_nvgprs
345 bl .sys_fork
346 b syscall_exit
347
348_GLOBAL(ppc_vfork)
349 bl .save_nvgprs
350 bl .sys_vfork
351 b syscall_exit
352
353_GLOBAL(ppc_clone)
354 bl .save_nvgprs
355 bl .sys_clone
356 b syscall_exit
357
Paul Mackerras1bd79332006-03-08 13:24:22 +1100358_GLOBAL(ppc32_swapcontext)
359 bl .save_nvgprs
360 bl .compat_sys_swapcontext
361 b syscall_exit
362
363_GLOBAL(ppc64_swapcontext)
364 bl .save_nvgprs
365 bl .sys_swapcontext
366 b syscall_exit
367
Paul Mackerras9994a332005-10-10 22:36:14 +1000368_GLOBAL(ret_from_fork)
369 bl .schedule_tail
370 REST_NVGPRS(r1)
371 li r3,0
372 b syscall_exit
373
374/*
375 * This routine switches between two different tasks. The process
376 * state of one is saved on its kernel stack. Then the state
377 * of the other is restored from its kernel stack. The memory
378 * management hardware is updated to the second process's state.
379 * Finally, we can return to the second process, via ret_from_except.
380 * On entry, r3 points to the THREAD for the current task, r4
381 * points to the THREAD for the new task.
382 *
383 * Note: there are two ways to get to the "going out" portion
384 * of this code; either by coming in via the entry (_switch)
385 * or via "fork" which must set up an environment equivalent
386 * to the "_switch" path. If you change this you'll have to change
387 * the fork code also.
388 *
389 * The code which creates the new task context is in 'copy_thread'
Jon Mason2ef94812006-01-23 10:58:20 -0600390 * in arch/powerpc/kernel/process.c
Paul Mackerras9994a332005-10-10 22:36:14 +1000391 */
392 .align 7
393_GLOBAL(_switch)
394 mflr r0
395 std r0,16(r1)
396 stdu r1,-SWITCH_FRAME_SIZE(r1)
397 /* r3-r13 are caller saved -- Cort */
398 SAVE_8GPRS(14, r1)
399 SAVE_10GPRS(22, r1)
400 mflr r20 /* Return to switch caller */
401 mfmsr r22
402 li r0, MSR_FP
Michael Neulingce48b212008-06-25 14:07:18 +1000403#ifdef CONFIG_VSX
404BEGIN_FTR_SECTION
405 oris r0,r0,MSR_VSX@h /* Disable VSX */
406END_FTR_SECTION_IFSET(CPU_FTR_VSX)
407#endif /* CONFIG_VSX */
Paul Mackerras9994a332005-10-10 22:36:14 +1000408#ifdef CONFIG_ALTIVEC
409BEGIN_FTR_SECTION
410 oris r0,r0,MSR_VEC@h /* Disable altivec */
411 mfspr r24,SPRN_VRSAVE /* save vrsave register value */
412 std r24,THREAD_VRSAVE(r3)
413END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
414#endif /* CONFIG_ALTIVEC */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000415#ifdef CONFIG_PPC64
416BEGIN_FTR_SECTION
417 mfspr r25,SPRN_DSCR
418 std r25,THREAD_DSCR(r3)
419END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
420#endif
Paul Mackerras9994a332005-10-10 22:36:14 +1000421 and. r0,r0,r22
422 beq+ 1f
423 andc r22,r22,r0
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000424 MTMSRD(r22)
Paul Mackerras9994a332005-10-10 22:36:14 +1000425 isync
4261: std r20,_NIP(r1)
427 mfcr r23
428 std r23,_CCR(r1)
429 std r1,KSP(r3) /* Set old stack pointer */
430
431#ifdef CONFIG_SMP
432 /* We need a sync somewhere here to make sure that if the
433 * previous task gets rescheduled on another CPU, it sees all
434 * stores it has performed on this one.
435 */
436 sync
437#endif /* CONFIG_SMP */
438
Anton Blanchardf89451f2010-08-11 01:40:27 +0000439 /*
440 * If we optimise away the clear of the reservation in system
441 * calls because we know the CPU tracks the address of the
442 * reservation, then we need to clear it here to cover the
443 * case that the kernel context switch path has no larx
444 * instructions.
445 */
446BEGIN_FTR_SECTION
447 ldarx r6,0,r1
448END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
449
Paul Mackerras9994a332005-10-10 22:36:14 +1000450 addi r6,r4,-THREAD /* Convert THREAD to 'current' */
451 std r6,PACACURRENT(r13) /* Set new 'current' */
452
453 ld r8,KSP(r4) /* new stack pointer */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000454#ifdef CONFIG_PPC_BOOK3S
Paul Mackerras9994a332005-10-10 22:36:14 +1000455BEGIN_FTR_SECTION
Michael Ellermanc2303282008-06-24 11:33:05 +1000456 BEGIN_FTR_SECTION_NESTED(95)
Paul Mackerras9994a332005-10-10 22:36:14 +1000457 clrrdi r6,r8,28 /* get its ESID */
458 clrrdi r9,r1,28 /* get current sp ESID */
Michael Ellermanc2303282008-06-24 11:33:05 +1000459 FTR_SECTION_ELSE_NESTED(95)
Paul Mackerras1189be62007-10-11 20:37:10 +1000460 clrrdi r6,r8,40 /* get its 1T ESID */
461 clrrdi r9,r1,40 /* get current sp 1T ESID */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000462 ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
Michael Ellermanc2303282008-06-24 11:33:05 +1000463FTR_SECTION_ELSE
464 b 2f
Matt Evans44ae3ab2011-04-06 19:48:50 +0000465ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
Paul Mackerras9994a332005-10-10 22:36:14 +1000466 clrldi. r0,r6,2 /* is new ESID c00000000? */
467 cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
468 cror eq,4*cr1+eq,eq
469 beq 2f /* if yes, don't slbie it */
470
471 /* Bolt in the new stack SLB entry */
472 ld r7,KSP_VSID(r4) /* Get new stack's VSID */
473 oris r0,r6,(SLB_ESID_V)@h
474 ori r0,r0,(SLB_NUM_BOLTED-1)@l
Paul Mackerras1189be62007-10-11 20:37:10 +1000475BEGIN_FTR_SECTION
476 li r9,MMU_SEGSIZE_1T /* insert B field */
477 oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
478 rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
Matt Evans44ae3ab2011-04-06 19:48:50 +0000479END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
Michael Neuling2f6093c2006-08-07 16:19:19 +1000480
Michael Neuling00efee72007-08-24 16:58:37 +1000481 /* Update the last bolted SLB. No write barriers are needed
482 * here, provided we only update the current CPU's SLB shadow
483 * buffer.
484 */
Michael Neuling2f6093c2006-08-07 16:19:19 +1000485 ld r9,PACA_SLBSHADOWPTR(r13)
Michael Neuling11a27ad2006-08-09 17:00:30 +1000486 li r12,0
487 std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
488 std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
489 std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
Michael Neuling2f6093c2006-08-07 16:19:19 +1000490
Matt Evans44ae3ab2011-04-06 19:48:50 +0000491 /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
Olof Johanssonf66bce52007-10-16 00:58:59 +1000492 * we have 1TB segments, the only CPUs known to have the errata
493 * only support less than 1TB of system memory and we'll never
494 * actually hit this code path.
495 */
496
Paul Mackerras9994a332005-10-10 22:36:14 +1000497 slbie r6
498 slbie r6 /* Workaround POWER5 < DD2.1 issue */
499 slbmte r7,r0
500 isync
Paul Mackerras9994a332005-10-10 22:36:14 +10005012:
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000502#endif /* !CONFIG_PPC_BOOK3S */
503
Paul Mackerras9994a332005-10-10 22:36:14 +1000504 clrrdi r7,r8,THREAD_SHIFT /* base of new stack */
505 /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
506 because we don't need to leave the 288-byte ABI gap at the
507 top of the kernel stack. */
508 addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
509
510 mr r1,r8 /* start using new stack pointer */
511 std r7,PACAKSAVE(r13)
512
513 ld r6,_CCR(r1)
514 mtcrf 0xFF,r6
515
516#ifdef CONFIG_ALTIVEC
517BEGIN_FTR_SECTION
518 ld r0,THREAD_VRSAVE(r4)
519 mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
520END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
521#endif /* CONFIG_ALTIVEC */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000522#ifdef CONFIG_PPC64
523BEGIN_FTR_SECTION
524 ld r0,THREAD_DSCR(r4)
525 cmpd r0,r25
526 beq 1f
527 mtspr SPRN_DSCR,r0
5281:
529END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
530#endif
Paul Mackerras9994a332005-10-10 22:36:14 +1000531
532 /* r3-r13 are destroyed -- Cort */
533 REST_8GPRS(14, r1)
534 REST_10GPRS(22, r1)
535
536 /* convert old thread to its task_struct for return value */
537 addi r3,r3,-THREAD
538 ld r7,_NIP(r1) /* Return to _switch caller in new task */
539 mtlr r7
540 addi r1,r1,SWITCH_FRAME_SIZE
541 blr
542
543 .align 7
544_GLOBAL(ret_from_except)
545 ld r11,_TRAP(r1)
546 andi. r0,r11,1
547 bne .ret_from_except_lite
548 REST_NVGPRS(r1)
549
550_GLOBAL(ret_from_except_lite)
551 /*
552 * Disable interrupts so that current_thread_info()->flags
553 * can't change between when we test it and when we return
554 * from the interrupt.
555 */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000556#ifdef CONFIG_PPC_BOOK3E
557 wrteei 0
558#else
Benjamin Herrenschmidtd9ada912012-03-02 11:33:52 +1100559 ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
560 mtmsrd r10,1 /* Update machine state */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000561#endif /* CONFIG_PPC_BOOK3E */
Paul Mackerras9994a332005-10-10 22:36:14 +1000562
563#ifdef CONFIG_PREEMPT
564 clrrdi r9,r1,THREAD_SHIFT /* current_thread_info() */
565 li r0,_TIF_NEED_RESCHED /* bits to check */
566 ld r3,_MSR(r1)
567 ld r4,TI_FLAGS(r9)
568 /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
569 rlwimi r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
570 and. r0,r4,r0 /* check NEED_RESCHED and maybe SIGPENDING */
571 bne do_work
572
573#else /* !CONFIG_PREEMPT */
574 ld r3,_MSR(r1) /* Returning to user mode? */
575 andi. r3,r3,MSR_PR
576 beq restore /* if not, just restore regs and return */
577
578 /* Check current_thread_info()->flags */
579 clrrdi r9,r1,THREAD_SHIFT
580 ld r4,TI_FLAGS(r9)
581 andi. r0,r4,_TIF_USER_WORK_MASK
582 bne do_work
Benjamin Herrenschmidt4f8cf362012-02-28 13:44:58 +1100583#endif /* !CONFIG_PREEMPT */
Paul Mackerras9994a332005-10-10 22:36:14 +1000584
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100585 .globl fast_exc_return_irq
586fast_exc_return_irq:
Paul Mackerras9994a332005-10-10 22:36:14 +1000587restore:
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100588 /*
589 * This is the main kernel exit path, we first check if we
590 * have to change our interrupt state.
591 */
Michael Ellerman01f3880d2008-07-16 14:21:34 +1000592 ld r5,SOFTE(r1)
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100593 lbz r6,PACASOFTIRQEN(r13)
594 cmpwi cr1,r5,0
595 cmpw cr0,r5,r6
596 beq cr0,4f
Paul Mackerras9994a332005-10-10 22:36:14 +1000597
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100598 /* We do, handle disable first, which is easy */
599 bne cr1,3f;
600 li r0,0
601 stb r0,PACASOFTIRQEN(r13);
602 TRACE_DISABLE_INTS
603 b 4f
Paul Mackerrasb0a779d2006-10-18 10:11:22 +1000604
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +11006053: /*
606 * We are about to soft-enable interrupts (we are hard disabled
607 * at this point). We check if there's anything that needs to
608 * be replayed first.
609 */
610 lbz r0,PACAIRQHAPPENED(r13)
611 cmpwi cr0,r0,0
612 bne- restore_check_irq_replay
613
614 /*
615 * Get here when nothing happened while soft-disabled, just
616 * soft-enable and move-on. We will hard-enable as a side
617 * effect of rfi
618 */
619restore_no_replay:
620 TRACE_ENABLE_INTS
621 li r0,1
622 stb r0,PACASOFTIRQEN(r13);
623
624 /*
625 * Final return path. BookE is handled in a different file
626 */
6274:
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000628#ifdef CONFIG_PPC_BOOK3E
629 b .exception_return_book3e
630#else
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100631 /*
632 * Clear the reservation. If we know the CPU tracks the address of
633 * the reservation then we can potentially save some cycles and use
634 * a larx. On POWER6 and POWER7 this is significantly faster.
635 */
636BEGIN_FTR_SECTION
637 stdcx. r0,0,r1 /* to clear the reservation */
638FTR_SECTION_ELSE
639 ldarx r4,0,r1
640ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
641
642 /*
643 * Some code path such as load_up_fpu or altivec return directly
644 * here. They run entirely hard disabled and do not alter the
645 * interrupt state. They also don't use lwarx/stwcx. and thus
646 * are known not to leave dangling reservations.
647 */
648 .globl fast_exception_return
649fast_exception_return:
650 ld r3,_MSR(r1)
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100651 ld r4,_CTR(r1)
652 ld r0,_LINK(r1)
653 mtctr r4
654 mtlr r0
655 ld r4,_XER(r1)
656 mtspr SPRN_XER,r4
657
658 REST_8GPRS(5, r1)
659
660 andi. r0,r3,MSR_RI
661 beq- unrecov_restore
662
Anton Blanchardf89451f2010-08-11 01:40:27 +0000663 /*
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100664 * Clear RI before restoring r13. If we are returning to
665 * userspace and we take an exception after restoring r13,
666 * we end up corrupting the userspace r13 value.
667 */
Benjamin Herrenschmidtd9ada912012-03-02 11:33:52 +1100668 ld r4,PACAKMSR(r13) /* Get kernel MSR without EE */
669 andc r4,r4,r0 /* r0 contains MSR_RI here */
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100670 mtmsrd r4,1
Paul Mackerras9994a332005-10-10 22:36:14 +1000671
672 /*
673 * r13 is our per cpu area, only restore it if we are returning to
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100674 * userspace the value stored in the stack frame may belong to
675 * another CPU.
Paul Mackerras9994a332005-10-10 22:36:14 +1000676 */
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100677 andi. r0,r3,MSR_PR
Paul Mackerras9994a332005-10-10 22:36:14 +1000678 beq 1f
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100679 ACCOUNT_CPU_USER_EXIT(r2, r4)
Paul Mackerras9994a332005-10-10 22:36:14 +1000680 REST_GPR(13, r1)
6811:
Paul Mackerrase56a6e22007-02-07 13:13:26 +1100682 mtspr SPRN_SRR1,r3
Paul Mackerras9994a332005-10-10 22:36:14 +1000683
684 ld r2,_CCR(r1)
685 mtcrf 0xFF,r2
686 ld r2,_NIP(r1)
687 mtspr SPRN_SRR0,r2
688
689 ld r0,GPR0(r1)
690 ld r2,GPR2(r1)
691 ld r3,GPR3(r1)
692 ld r4,GPR4(r1)
693 ld r1,GPR1(r1)
694
695 rfid
696 b . /* prevent speculative execution */
697
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000698#endif /* CONFIG_PPC_BOOK3E */
699
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100700 /*
701 * Something did happen, check if a re-emit is needed
702 * (this also clears paca->irq_happened)
703 */
704restore_check_irq_replay:
705 /* XXX: We could implement a fast path here where we check
706 * for irq_happened being just 0x01, in which case we can
707 * clear it and return. That means that we would potentially
708 * miss a decrementer having wrapped all the way around.
709 *
710 * Still, this might be useful for things like hash_page
711 */
712 bl .__check_irq_replay
713 cmpwi cr0,r3,0
714 beq restore_no_replay
715
716 /*
717 * We need to re-emit an interrupt. We do so by re-using our
718 * existing exception frame. We first change the trap value,
719 * but we need to ensure we preserve the low nibble of it
720 */
721 ld r4,_TRAP(r1)
722 clrldi r4,r4,60
723 or r4,r4,r3
724 std r4,_TRAP(r1)
725
726 /*
727 * Then find the right handler and call it. Interrupts are
728 * still soft-disabled and we keep them that way.
729 */
730 cmpwi cr0,r3,0x500
731 bne 1f
732 addi r3,r1,STACK_FRAME_OVERHEAD;
733 bl .do_IRQ
734 b .ret_from_except
7351: cmpwi cr0,r3,0x900
736 bne 1f
737 addi r3,r1,STACK_FRAME_OVERHEAD;
738 bl .timer_interrupt
739 b .ret_from_except
740#ifdef CONFIG_PPC_BOOK3E
7411: cmpwi cr0,r3,0x280
742 bne 1f
743 addi r3,r1,STACK_FRAME_OVERHEAD;
744 bl .doorbell_exception
745 b .ret_from_except
746#endif /* CONFIG_PPC_BOOK3E */
7471: b .ret_from_except /* What else to do here ? */
748
Paul Mackerras9994a332005-10-10 22:36:14 +1000749do_work:
750#ifdef CONFIG_PREEMPT
751 andi. r0,r3,MSR_PR /* Returning to user mode? */
752 bne user_work
753 /* Check that preempt_count() == 0 and interrupts are enabled */
754 lwz r8,TI_PREEMPT(r9)
755 cmpwi cr1,r8,0
Paul Mackerras9994a332005-10-10 22:36:14 +1000756 ld r0,SOFTE(r1)
757 cmpdi r0,0
Paul Mackerras9994a332005-10-10 22:36:14 +1000758 crandc eq,cr1*4+eq,eq
759 bne restore
Benjamin Herrenschmidt4f917ba2009-10-26 19:41:17 +0000760
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100761 /*
762 * Here we are preempting the current task. We want to make
763 * sure we are soft-disabled first
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +1000764 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100765 SOFT_DISABLE_INTS(r3,r4)
Benjamin Herrenschmidt4f917ba2009-10-26 19:41:17 +00007661: bl .preempt_schedule_irq
767
768 /* Hard-disable interrupts again (and update PACA) */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000769#ifdef CONFIG_PPC_BOOK3E
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000770 wrteei 0
771#else
Benjamin Herrenschmidtd9ada912012-03-02 11:33:52 +1100772 ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
Paul Mackerras9994a332005-10-10 22:36:14 +1000773 mtmsrd r10,1
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000774#endif /* CONFIG_PPC_BOOK3E */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100775 li r0,PACA_IRQ_HARD_DIS
776 stb r0,PACAIRQHAPPENED(r13)
Benjamin Herrenschmidt4f917ba2009-10-26 19:41:17 +0000777
778 /* Re-test flags and eventually loop */
779 clrrdi r9,r1,THREAD_SHIFT
Paul Mackerras9994a332005-10-10 22:36:14 +1000780 ld r4,TI_FLAGS(r9)
781 andi. r0,r4,_TIF_NEED_RESCHED
782 bne 1b
783 b restore
784
785user_work:
Benjamin Herrenschmidt4f917ba2009-10-26 19:41:17 +0000786#endif /* CONFIG_PREEMPT */
787
Paul Mackerras9994a332005-10-10 22:36:14 +1000788 /* Enable interrupts */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000789#ifdef CONFIG_PPC_BOOK3E
790 wrteei 1
791#else
Paul Mackerras9994a332005-10-10 22:36:14 +1000792 ori r10,r10,MSR_EE
793 mtmsrd r10,1
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000794#endif /* CONFIG_PPC_BOOK3E */
Paul Mackerras9994a332005-10-10 22:36:14 +1000795
796 andi. r0,r4,_TIF_NEED_RESCHED
797 beq 1f
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100798 bl .restore_interrupts
Paul Mackerras9994a332005-10-10 22:36:14 +1000799 bl .schedule
800 b .ret_from_except_lite
801
8021: bl .save_nvgprs
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100803 bl .restore_interrupts
Roland McGrath7d6d6372008-07-27 16:52:52 +1000804 addi r3,r1,STACK_FRAME_OVERHEAD
Benjamin Herrenschmidt18b246f2012-02-22 16:48:32 +1100805 bl .do_notify_resume
Paul Mackerras9994a332005-10-10 22:36:14 +1000806 b .ret_from_except
807
808unrecov_restore:
809 addi r3,r1,STACK_FRAME_OVERHEAD
810 bl .unrecoverable_exception
811 b unrecov_restore
812
813#ifdef CONFIG_PPC_RTAS
814/*
815 * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
816 * called with the MMU off.
817 *
818 * In addition, we need to be in 32b mode, at least for now.
819 *
820 * Note: r3 is an input parameter to rtas, so don't trash it...
821 */
822_GLOBAL(enter_rtas)
823 mflr r0
824 std r0,16(r1)
825 stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
826
827 /* Because RTAS is running in 32b mode, it clobbers the high order half
828 * of all registers that it saves. We therefore save those registers
829 * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
830 */
831 SAVE_GPR(2, r1) /* Save the TOC */
832 SAVE_GPR(13, r1) /* Save paca */
833 SAVE_8GPRS(14, r1) /* Save the non-volatiles */
834 SAVE_10GPRS(22, r1) /* ditto */
835
836 mfcr r4
837 std r4,_CCR(r1)
838 mfctr r5
839 std r5,_CTR(r1)
840 mfspr r6,SPRN_XER
841 std r6,_XER(r1)
842 mfdar r7
843 std r7,_DAR(r1)
844 mfdsisr r8
845 std r8,_DSISR(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +1000846
Mike Kravetz9fe901d2006-03-27 15:20:00 -0800847 /* Temporary workaround to clear CR until RTAS can be modified to
848 * ignore all bits.
849 */
850 li r0,0
851 mtcr r0
852
David Woodhouse007d88d2007-01-01 18:45:34 +0000853#ifdef CONFIG_BUG
Paul Mackerras9994a332005-10-10 22:36:14 +1000854 /* There is no way it is acceptable to get here with interrupts enabled,
855 * check it with the asm equivalent of WARN_ON
856 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000857 lbz r0,PACASOFTIRQEN(r13)
Paul Mackerras9994a332005-10-10 22:36:14 +10008581: tdnei r0,0
David Woodhouse007d88d2007-01-01 18:45:34 +0000859 EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
860#endif
861
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000862 /* Hard-disable interrupts */
863 mfmsr r6
864 rldicl r7,r6,48,1
865 rotldi r7,r7,16
866 mtmsrd r7,1
867
Paul Mackerras9994a332005-10-10 22:36:14 +1000868 /* Unfortunately, the stack pointer and the MSR are also clobbered,
869 * so they are saved in the PACA which allows us to restore
870 * our original state after RTAS returns.
871 */
872 std r1,PACAR1(r13)
873 std r6,PACASAVEDMSR(r13)
874
875 /* Setup our real return addr */
David Gibsone58c3492006-01-13 14:56:25 +1100876 LOAD_REG_ADDR(r4,.rtas_return_loc)
877 clrldi r4,r4,2 /* convert to realmode address */
Paul Mackerras9994a332005-10-10 22:36:14 +1000878 mtlr r4
879
880 li r0,0
881 ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
882 andc r0,r6,r0
883
884 li r9,1
885 rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
Anton Blanchard44c9f3c2010-02-07 19:37:29 +0000886 ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
Paul Mackerras9994a332005-10-10 22:36:14 +1000887 andc r6,r0,r9
Paul Mackerras9994a332005-10-10 22:36:14 +1000888 sync /* disable interrupts so SRR0/1 */
889 mtmsrd r0 /* don't get trashed */
890
David Gibsone58c3492006-01-13 14:56:25 +1100891 LOAD_REG_ADDR(r4, rtas)
Paul Mackerras9994a332005-10-10 22:36:14 +1000892 ld r5,RTASENTRY(r4) /* get the rtas->entry value */
893 ld r4,RTASBASE(r4) /* get the rtas->base value */
894
895 mtspr SPRN_SRR0,r5
896 mtspr SPRN_SRR1,r6
897 rfid
898 b . /* prevent speculative execution */
899
900_STATIC(rtas_return_loc)
901 /* relocation is off at this point */
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +1100902 GET_PACA(r4)
David Gibsone58c3492006-01-13 14:56:25 +1100903 clrldi r4,r4,2 /* convert to realmode address */
Paul Mackerras9994a332005-10-10 22:36:14 +1000904
Paul Mackerrase31aa452008-08-30 11:41:12 +1000905 bcl 20,31,$+4
9060: mflr r3
907 ld r3,(1f-0b)(r3) /* get &.rtas_restore_regs */
908
Paul Mackerras9994a332005-10-10 22:36:14 +1000909 mfmsr r6
910 li r0,MSR_RI
911 andc r6,r6,r0
912 sync
913 mtmsrd r6
914
915 ld r1,PACAR1(r4) /* Restore our SP */
Paul Mackerras9994a332005-10-10 22:36:14 +1000916 ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
917
918 mtspr SPRN_SRR0,r3
919 mtspr SPRN_SRR1,r4
920 rfid
921 b . /* prevent speculative execution */
922
Paul Mackerrase31aa452008-08-30 11:41:12 +1000923 .align 3
9241: .llong .rtas_restore_regs
925
Paul Mackerras9994a332005-10-10 22:36:14 +1000926_STATIC(rtas_restore_regs)
927 /* relocation is on at this point */
928 REST_GPR(2, r1) /* Restore the TOC */
929 REST_GPR(13, r1) /* Restore paca */
930 REST_8GPRS(14, r1) /* Restore the non-volatiles */
931 REST_10GPRS(22, r1) /* ditto */
932
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +1100933 GET_PACA(r13)
Paul Mackerras9994a332005-10-10 22:36:14 +1000934
935 ld r4,_CCR(r1)
936 mtcr r4
937 ld r5,_CTR(r1)
938 mtctr r5
939 ld r6,_XER(r1)
940 mtspr SPRN_XER,r6
941 ld r7,_DAR(r1)
942 mtdar r7
943 ld r8,_DSISR(r1)
944 mtdsisr r8
Paul Mackerras9994a332005-10-10 22:36:14 +1000945
946 addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
947 ld r0,16(r1) /* get return address */
948
949 mtlr r0
950 blr /* return to caller */
951
952#endif /* CONFIG_PPC_RTAS */
953
Paul Mackerras9994a332005-10-10 22:36:14 +1000954_GLOBAL(enter_prom)
955 mflr r0
956 std r0,16(r1)
957 stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
958
959 /* Because PROM is running in 32b mode, it clobbers the high order half
960 * of all registers that it saves. We therefore save those registers
961 * PROM might touch to the stack. (r0, r3-r13 are caller saved)
962 */
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +0000963 SAVE_GPR(2, r1)
Paul Mackerras9994a332005-10-10 22:36:14 +1000964 SAVE_GPR(13, r1)
965 SAVE_8GPRS(14, r1)
966 SAVE_10GPRS(22, r1)
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +0000967 mfcr r10
Paul Mackerras9994a332005-10-10 22:36:14 +1000968 mfmsr r11
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +0000969 std r10,_CCR(r1)
Paul Mackerras9994a332005-10-10 22:36:14 +1000970 std r11,_MSR(r1)
971
972 /* Get the PROM entrypoint */
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +0000973 mtlr r4
Paul Mackerras9994a332005-10-10 22:36:14 +1000974
975 /* Switch MSR to 32 bits mode
976 */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000977#ifdef CONFIG_PPC_BOOK3E
978 rlwinm r11,r11,0,1,31
979 mtmsr r11
980#else /* CONFIG_PPC_BOOK3E */
Paul Mackerras9994a332005-10-10 22:36:14 +1000981 mfmsr r11
982 li r12,1
983 rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
984 andc r11,r11,r12
985 li r12,1
986 rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
987 andc r11,r11,r12
988 mtmsrd r11
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000989#endif /* CONFIG_PPC_BOOK3E */
Paul Mackerras9994a332005-10-10 22:36:14 +1000990 isync
991
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +0000992 /* Enter PROM here... */
Paul Mackerras9994a332005-10-10 22:36:14 +1000993 blrl
994
995 /* Just make sure that r1 top 32 bits didn't get
996 * corrupt by OF
997 */
998 rldicl r1,r1,0,32
999
1000 /* Restore the MSR (back to 64 bits) */
1001 ld r0,_MSR(r1)
Benjamin Herrenschmidt6c171992009-07-23 23:15:07 +00001002 MTMSRD(r0)
Paul Mackerras9994a332005-10-10 22:36:14 +10001003 isync
1004
1005 /* Restore other registers */
1006 REST_GPR(2, r1)
1007 REST_GPR(13, r1)
1008 REST_8GPRS(14, r1)
1009 REST_10GPRS(22, r1)
1010 ld r4,_CCR(r1)
1011 mtcr r4
Paul Mackerras9994a332005-10-10 22:36:14 +10001012
1013 addi r1,r1,PROM_FRAME_SIZE
1014 ld r0,16(r1)
1015 mtlr r0
1016 blr
Steven Rostedt4e491d12008-05-14 23:49:44 -04001017
Steven Rostedt606576c2008-10-06 19:06:12 -04001018#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt4e491d12008-05-14 23:49:44 -04001019#ifdef CONFIG_DYNAMIC_FTRACE
1020_GLOBAL(mcount)
1021_GLOBAL(_mcount)
Steven Rostedt4e491d12008-05-14 23:49:44 -04001022 blr
1023
1024_GLOBAL(ftrace_caller)
1025 /* Taken from output of objdump from lib64/glibc */
1026 mflr r3
1027 ld r11, 0(r1)
1028 stdu r1, -112(r1)
1029 std r3, 128(r1)
1030 ld r4, 16(r11)
Abhishek Sagar395a59d2008-06-21 23:47:27 +05301031 subi r3, r3, MCOUNT_INSN_SIZE
Steven Rostedt4e491d12008-05-14 23:49:44 -04001032.globl ftrace_call
1033ftrace_call:
1034 bl ftrace_stub
1035 nop
Steven Rostedt46542882009-02-10 22:19:54 -08001036#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1037.globl ftrace_graph_call
1038ftrace_graph_call:
1039 b ftrace_graph_stub
1040_GLOBAL(ftrace_graph_stub)
1041#endif
Steven Rostedt4e491d12008-05-14 23:49:44 -04001042 ld r0, 128(r1)
1043 mtlr r0
1044 addi r1, r1, 112
1045_GLOBAL(ftrace_stub)
1046 blr
1047#else
1048_GLOBAL(mcount)
1049 blr
1050
1051_GLOBAL(_mcount)
1052 /* Taken from output of objdump from lib64/glibc */
1053 mflr r3
1054 ld r11, 0(r1)
1055 stdu r1, -112(r1)
1056 std r3, 128(r1)
1057 ld r4, 16(r11)
1058
Abhishek Sagar395a59d2008-06-21 23:47:27 +05301059 subi r3, r3, MCOUNT_INSN_SIZE
Steven Rostedt4e491d12008-05-14 23:49:44 -04001060 LOAD_REG_ADDR(r5,ftrace_trace_function)
1061 ld r5,0(r5)
1062 ld r5,0(r5)
1063 mtctr r5
1064 bctrl
Steven Rostedt4e491d12008-05-14 23:49:44 -04001065 nop
Steven Rostedt6794c782009-02-09 21:10:27 -08001066
1067
1068#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1069 b ftrace_graph_caller
1070#endif
Steven Rostedt4e491d12008-05-14 23:49:44 -04001071 ld r0, 128(r1)
1072 mtlr r0
1073 addi r1, r1, 112
1074_GLOBAL(ftrace_stub)
1075 blr
1076
Steven Rostedt6794c782009-02-09 21:10:27 -08001077#endif /* CONFIG_DYNAMIC_FTRACE */
1078
1079#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt46542882009-02-10 22:19:54 -08001080_GLOBAL(ftrace_graph_caller)
Steven Rostedt6794c782009-02-09 21:10:27 -08001081 /* load r4 with local address */
1082 ld r4, 128(r1)
1083 subi r4, r4, MCOUNT_INSN_SIZE
1084
1085 /* get the parent address */
1086 ld r11, 112(r1)
1087 addi r3, r11, 16
1088
1089 bl .prepare_ftrace_return
1090 nop
1091
1092 ld r0, 128(r1)
1093 mtlr r0
1094 addi r1, r1, 112
1095 blr
1096
1097_GLOBAL(return_to_handler)
1098 /* need to save return values */
Steven Rostedtbb725342009-02-11 12:45:49 -08001099 std r4, -24(r1)
1100 std r3, -16(r1)
1101 std r31, -8(r1)
1102 mr r31, r1
1103 stdu r1, -112(r1)
1104
1105 bl .ftrace_return_to_handler
1106 nop
1107
1108 /* return value has real return address */
1109 mtlr r3
1110
1111 ld r1, 0(r1)
1112 ld r4, -24(r1)
1113 ld r3, -16(r1)
1114 ld r31, -8(r1)
1115
1116 /* Jump back to real return address */
1117 blr
1118
1119_GLOBAL(mod_return_to_handler)
1120 /* need to save return values */
Steven Rostedt6794c782009-02-09 21:10:27 -08001121 std r4, -32(r1)
1122 std r3, -24(r1)
1123 /* save TOC */
1124 std r2, -16(r1)
1125 std r31, -8(r1)
1126 mr r31, r1
1127 stdu r1, -112(r1)
1128
Steven Rostedtbb725342009-02-11 12:45:49 -08001129 /*
1130 * We are in a module using the module's TOC.
1131 * Switch to our TOC to run inside the core kernel.
1132 */
Steven Rostedtbe10ab12009-09-15 08:30:14 -07001133 ld r2, PACATOC(r13)
Steven Rostedt6794c782009-02-09 21:10:27 -08001134
1135 bl .ftrace_return_to_handler
1136 nop
1137
1138 /* return value has real return address */
1139 mtlr r3
1140
1141 ld r1, 0(r1)
1142 ld r4, -32(r1)
1143 ld r3, -24(r1)
1144 ld r2, -16(r1)
1145 ld r31, -8(r1)
1146
1147 /* Jump back to real return address */
1148 blr
1149#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1150#endif /* CONFIG_FUNCTION_TRACER */