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)
111 #ifdef CONFIG_BOOKE_OR_40x
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)
141 #endif /* CONFIG_PPC_47x */
161 b . /* Prevent prefetch past rfi */
177 stw r7,icache_44x_need_flush@l(r4)
179 #endif /* CONFIG_44x */
185 li r3,0 /* fork() return value */
188 .globl ret_from_kernel_user_thread
189 ret_from_kernel_user_thread:
198 .globl start_kernel_thread
206 * This must not return. We actually want to BUG here, not WARN,
207 * because BUG will exit the process which is what the kernel thread
208 * should have done, which may give some hope of continuing.
211 EMIT_BUG_ENTRY 100b,__FILE__,__LINE__,0
213 .globl fast_exception_return
214 fast_exception_return:
215 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
216 andi. r10,r9,MSR_RI /* check for recoverable interrupt */
217 beq 3f /* if not, we've got problems */
225 /* Clear the exception marker on the stack to avoid confusing stacktrace */
229 #if defined(CONFIG_PPC_8xx) && defined(CONFIG_PERF_EVENTS)
239 b . /* Prevent prefetch past rfi */
241 _ASM_NOKPROBE_SYMBOL(fast_exception_return)
243 /* aargh, a nonrecoverable interrupt, panic */
244 /* aargh, we don't know which trap this is */
248 prepare_transfer_to_handler
249 bl unrecoverable_exception
250 trap /* should not get here */
252 .globl interrupt_return
255 addi r3,r1,STACK_INT_FRAME_REGS
257 beq .Lkernel_interrupt_return
258 bl interrupt_exit_user_prepare
261 bne- .Lrestore_nvgprs
263 .Lfast_user_interrupt_return:
270 stwcx. r0,0,r1 /* to clear the reservation */
273 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
282 * Leaving a stale exception marker on the stack can confuse
283 * the reliable stack unwinder later on. Clear it.
298 b . /* Prevent prefetch past rfi */
303 b .Lfast_user_interrupt_return
305 .Lkernel_interrupt_return:
306 bl interrupt_exit_kernel_prepare
308 .Lfast_kernel_interrupt_return:
316 stwcx. r0,0,r1 /* to clear the reservation */
319 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
334 * Leaving a stale exception marker on the stack can confuse
335 * the reliable stack unwinder later on. Clear it.
341 bne- cr1,1f /* emulate stack store */
348 b . /* Prevent prefetch past rfi */
352 * Emulate stack store with update. New r1 value was already calculated
353 * and updated in our interrupt regs by emulate_loadstore, but we can't
354 * store the previous value of r1 to the stack before re-loading our
355 * registers from it, otherwise they could be clobbered. Use
356 * SPRG Scratch0 as temporary storage to hold the store
357 * data, as interrupts are disabled here so it won't be clobbered.
361 mtspr SPRN_SPRG_WSCRATCH0, r9
363 mtspr SPRN_SPRG_SCRATCH0, r9
365 addi r9,r1,INT_FRAME_SIZE /* get original r1 */
369 stw r9,0(r1) /* perform store component of stwu */
371 mfspr r9, SPRN_SPRG_RSCRATCH0
373 mfspr r9, SPRN_SPRG_SCRATCH0
377 b . /* Prevent prefetch past rfi */
379 _ASM_NOKPROBE_SYMBOL(interrupt_return)
381 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
384 * Returning from a critical interrupt in user mode doesn't need
385 * to be any different from a normal exception. For a critical
386 * interrupt in the kernel, we just return (without checking for
387 * preemption) since the interrupt may have happened at some crucial
388 * place (e.g. inside the TLB miss handler), and because we will be
389 * running with r1 pointing into critical_stack, not the current
390 * process's kernel stack (and therefore current_thread_info() will
391 * give the wrong answer).
392 * We have to restore various SPRs that may have been in use at the
393 * time of the critical interrupt.
397 #define PPC_40x_TURN_OFF_MSR_DR \
398 /* avoid any possible TLB misses here by turning off MSR.DR, we \
399 * assume the instructions here are mapped by a pinned TLB entry */ \
405 #define PPC_40x_TURN_OFF_MSR_DR
408 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
411 andi. r3,r3,MSR_PR; \
412 bne interrupt_return; \
414 REST_GPRS(2, 8, r1); \
417 mtspr SPRN_XER,r10; \
419 stwcx. r0,0,r1; /* to clear the reservation */ \
424 PPC_40x_TURN_OFF_MSR_DR; \
427 mtspr SPRN_DEAR,r9; \
428 mtspr SPRN_ESR,r10; \
431 mtspr exc_lvl_srr0,r11; \
432 mtspr exc_lvl_srr1,r12; \
433 REST_GPRS(9, 12, r1); \
436 b .; /* prevent prefetch past exc_lvl_rfi */
438 #define RESTORE_xSRR(exc_lvl_srr0, exc_lvl_srr1) \
439 lwz r9,_##exc_lvl_srr0(r1); \
440 lwz r10,_##exc_lvl_srr1(r1); \
441 mtspr SPRN_##exc_lvl_srr0,r9; \
442 mtspr SPRN_##exc_lvl_srr1,r10;
444 #if defined(CONFIG_PPC_E500)
445 #ifdef CONFIG_PHYS_64BIT
446 #define RESTORE_MAS7 \
451 #endif /* CONFIG_PHYS_64BIT */
452 #define RESTORE_MMU_REGS \
456 mtspr SPRN_MAS0,r9; \
458 mtspr SPRN_MAS1,r10; \
460 mtspr SPRN_MAS2,r11; \
461 mtspr SPRN_MAS3,r9; \
462 mtspr SPRN_MAS6,r10; \
464 #elif defined(CONFIG_44x)
465 #define RESTORE_MMU_REGS \
469 #define RESTORE_MMU_REGS
473 .globl ret_from_crit_exc
476 lwz r9,crit_srr0@l(r9);
477 lis r10,crit_srr1@ha;
478 lwz r10,crit_srr1@l(r10);
481 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
482 _ASM_NOKPROBE_SYMBOL(ret_from_crit_exc)
483 #endif /* CONFIG_40x */
486 .globl ret_from_crit_exc
488 RESTORE_xSRR(SRR0,SRR1);
490 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
491 _ASM_NOKPROBE_SYMBOL(ret_from_crit_exc)
493 .globl ret_from_debug_exc
495 RESTORE_xSRR(SRR0,SRR1);
496 RESTORE_xSRR(CSRR0,CSRR1);
498 RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, PPC_RFDI)
499 _ASM_NOKPROBE_SYMBOL(ret_from_debug_exc)
501 .globl ret_from_mcheck_exc
503 RESTORE_xSRR(SRR0,SRR1);
504 RESTORE_xSRR(CSRR0,CSRR1);
505 RESTORE_xSRR(DSRR0,DSRR1);
507 RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, PPC_RFMCI)
508 _ASM_NOKPROBE_SYMBOL(ret_from_mcheck_exc)
509 #endif /* CONFIG_BOOKE */
510 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */