1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Rewritten by Cort Dougan (cort@fsmlabs.com) for PReP
6 * Copyright (C) 1996 Cort Dougan <cort@fsmlabs.com>
7 * Adapted for Power Macintosh by Paul Mackerras.
8 * Low-level exception handlers and MMU support
9 * rewritten by Paul Mackerras.
10 * Copyright (C) 1996 Paul Mackerras.
11 * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
13 * This file contains the system call entry code, context switch
14 * code, and exception/interrupt return code for PowerPC.
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/sys.h>
20 #include <linux/threads.h>
21 #include <linux/linkage.h>
26 #include <asm/cputable.h>
27 #include <asm/thread_info.h>
28 #include <asm/ppc_asm.h>
29 #include <asm/asm-offsets.h>
30 #include <asm/unistd.h>
31 #include <asm/ptrace.h>
32 #include <asm/feature-fixups.h>
33 #include <asm/barrier.h>
36 #include <asm/interrupt.h>
41 * powerpc relies on return from interrupt/syscall being context synchronising
42 * (which rfi is) to support ARCH_HAS_MEMBARRIER_SYNC_CORE without additional
43 * synchronisation instructions.
47 * Align to 4k in order to ensure that all functions modyfing srr0/srr1
48 * fit into one page in order to not encounter a TLB miss between the
49 * modification of srr0/srr1 and the associated rfi.
53 #if defined(CONFIG_PPC_BOOK3S_32) || defined(CONFIG_PPC_E500)
54 .globl prepare_transfer_to_handler
55 prepare_transfer_to_handler:
56 /* if from kernel, check interrupted DOZE/NAP mode */
57 lwz r12,TI_LOCAL_FLAGS(r2)
60 bt- 31-TLF_SLEEPING,7f
63 4: rlwinm r12,r12,0,~_TLF_NAPPING
64 stw r12,TI_LOCAL_FLAGS(r2)
65 b power_save_ppc32_restore
67 7: rlwinm r12,r12,0,~_TLF_SLEEPING
68 stw r12,TI_LOCAL_FLAGS(r2)
69 lwz r9,_MSR(r11) /* if sleeping, clear MSR.EE */
70 rlwinm r9,r9,0,~MSR_EE
71 lwz r12,_LINK(r11) /* and return to address in LR */
73 b fast_exception_return
74 _ASM_NOKPROBE_SYMBOL(prepare_transfer_to_handler)
75 #endif /* CONFIG_PPC_BOOK3S_32 || CONFIG_PPC_E500 */
77 #if defined(CONFIG_PPC_KUEP) && defined(CONFIG_PPC_BOOK3S_32)
78 SYM_FUNC_START(__kuep_lock)
79 lwz r9, THREAD+THSR0(r2)
80 update_user_segments_by_4 r9, r10, r11, r12
82 SYM_FUNC_END(__kuep_lock)
84 SYM_FUNC_START_LOCAL(__kuep_unlock)
85 lwz r9, THREAD+THSR0(r2)
87 update_user_segments_by_4 r9, r10, r11, r12
89 SYM_FUNC_END(__kuep_unlock)
104 .globl transfer_to_syscall
106 stw r3, ORIG_GPR3(r1)
112 rlwinm r9,r9,0,14,12 /* clear MSR_WE (necessary?) */
114 lis r12,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
116 addi r12,r12,STACK_FRAME_REGS_MARKER@l
118 li r2, INTERRUPT_SYSCALL
119 stw r12,STACK_INT_FRAME_MARKER(r1)
127 /* Calling convention has r3 = regs, r4 = orig r0 */
128 addi r3,r1,STACK_INT_FRAME_REGS
130 bl system_call_exception
133 addi r4,r1,STACK_INT_FRAME_REGS
135 bl syscall_exit_prepare
136 #ifdef CONFIG_PPC_47x
137 lis r4,icache_44x_need_flush@ha
138 lwz r5,icache_44x_need_flush@l(r4)
140 bne- .L44x_icache_flush
141 #endif /* CONFIG_PPC_47x */
142 .L44x_icache_flush_return:
176 stw r7,icache_44x_need_flush@l(r4)
177 b .L44x_icache_flush_return
178 #endif /* CONFIG_44x */
184 li r3,0 /* fork() return value */
187 .globl ret_from_kernel_user_thread
188 ret_from_kernel_user_thread:
197 .globl start_kernel_thread
205 * This must not return. We actually want to BUG here, not WARN,
206 * because BUG will exit the process which is what the kernel thread
207 * should have done, which may give some hope of continuing.
210 EMIT_BUG_ENTRY 100b,__FILE__,__LINE__,0
212 .globl fast_exception_return
213 fast_exception_return:
215 andi. r10,r9,MSR_RI /* check for recoverable interrupt */
216 beq 3f /* if not, we've got problems */
224 /* Clear the exception marker on the stack to avoid confusing stacktrace */
228 #if defined(CONFIG_PPC_8xx) && defined(CONFIG_PERF_EVENTS)
237 _ASM_NOKPROBE_SYMBOL(fast_exception_return)
239 /* aargh, a nonrecoverable interrupt, panic */
240 /* aargh, we don't know which trap this is */
244 prepare_transfer_to_handler
245 bl unrecoverable_exception
246 trap /* should not get here */
248 .globl interrupt_return
251 addi r3,r1,STACK_INT_FRAME_REGS
253 beq .Lkernel_interrupt_return
254 bl interrupt_exit_user_prepare
257 bne- .Lrestore_nvgprs
259 .Lfast_user_interrupt_return:
266 stwcx. r0,0,r1 /* to clear the reservation */
269 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
278 * Leaving a stale exception marker on the stack can confuse
279 * the reliable stack unwinder later on. Clear it.
296 b .Lfast_user_interrupt_return
298 .Lkernel_interrupt_return:
299 bl interrupt_exit_kernel_prepare
301 .Lfast_kernel_interrupt_return:
309 stwcx. r0,0,r1 /* to clear the reservation */
312 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
327 * Leaving a stale exception marker on the stack can confuse
328 * the reliable stack unwinder later on. Clear it.
334 bne- cr1,1f /* emulate stack store */
342 * Emulate stack store with update. New r1 value was already calculated
343 * and updated in our interrupt regs by emulate_loadstore, but we can't
344 * store the previous value of r1 to the stack before re-loading our
345 * registers from it, otherwise they could be clobbered. Use
346 * SPRG Scratch0 as temporary storage to hold the store
347 * data, as interrupts are disabled here so it won't be clobbered.
351 mtspr SPRN_SPRG_WSCRATCH0, r9
353 mtspr SPRN_SPRG_SCRATCH0, r9
355 addi r9,r1,INT_FRAME_SIZE /* get original r1 */
359 stw r9,0(r1) /* perform store component of stwu */
361 mfspr r9, SPRN_SPRG_RSCRATCH0
363 mfspr r9, SPRN_SPRG_SCRATCH0
366 _ASM_NOKPROBE_SYMBOL(interrupt_return)
371 * Returning from a critical interrupt in user mode doesn't need
372 * to be any different from a normal exception. For a critical
373 * interrupt in the kernel, we just return (without checking for
374 * preemption) since the interrupt may have happened at some crucial
375 * place (e.g. inside the TLB miss handler), and because we will be
376 * running with r1 pointing into critical_stack, not the current
377 * process's kernel stack (and therefore current_thread_info() will
378 * give the wrong answer).
379 * We have to restore various SPRs that may have been in use at the
380 * time of the critical interrupt.
384 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
387 andi. r3,r3,MSR_PR; \
388 bne interrupt_return; \
390 REST_GPRS(2, 8, r1); \
393 mtspr SPRN_XER,r10; \
395 stwcx. r0,0,r1; /* to clear the reservation */ \
402 mtspr SPRN_DEAR,r9; \
403 mtspr SPRN_ESR,r10; \
406 mtspr exc_lvl_srr0,r11; \
407 mtspr exc_lvl_srr1,r12; \
408 REST_GPRS(9, 12, r1); \
411 b .; /* prevent prefetch past exc_lvl_rfi */
413 #define RESTORE_xSRR(exc_lvl_srr0, exc_lvl_srr1) \
414 lwz r9,_##exc_lvl_srr0(r1); \
415 lwz r10,_##exc_lvl_srr1(r1); \
416 mtspr SPRN_##exc_lvl_srr0,r9; \
417 mtspr SPRN_##exc_lvl_srr1,r10;
419 #if defined(CONFIG_PPC_E500)
420 #ifdef CONFIG_PHYS_64BIT
421 #define RESTORE_MAS7 \
426 #endif /* CONFIG_PHYS_64BIT */
427 #define RESTORE_MMU_REGS \
431 mtspr SPRN_MAS0,r9; \
433 mtspr SPRN_MAS1,r10; \
435 mtspr SPRN_MAS2,r11; \
436 mtspr SPRN_MAS3,r9; \
437 mtspr SPRN_MAS6,r10; \
439 #elif defined(CONFIG_44x)
440 #define RESTORE_MMU_REGS \
444 #define RESTORE_MMU_REGS
447 .globl ret_from_crit_exc
449 RESTORE_xSRR(SRR0,SRR1);
451 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
452 _ASM_NOKPROBE_SYMBOL(ret_from_crit_exc)
454 .globl ret_from_debug_exc
456 RESTORE_xSRR(SRR0,SRR1);
457 RESTORE_xSRR(CSRR0,CSRR1);
459 RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, PPC_RFDI)
460 _ASM_NOKPROBE_SYMBOL(ret_from_debug_exc)
462 .globl ret_from_mcheck_exc
464 RESTORE_xSRR(SRR0,SRR1);
465 RESTORE_xSRR(CSRR0,CSRR1);
466 RESTORE_xSRR(DSRR0,DSRR1);
468 RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, PPC_RFMCI)
469 _ASM_NOKPROBE_SYMBOL(ret_from_mcheck_exc)
470 #endif /* CONFIG_BOOKE */