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