GNU Linux-libre 5.15.72-gnu
[releases.git] / arch / powerpc / kernel / entry_32.S
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  PowerPC version
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).
12  *
13  *  This file contains the system call entry code, context switch
14  *  code, and exception/interrupt return code for PowerPC.
15  */
16
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/sys.h>
20 #include <linux/threads.h>
21 #include <asm/reg.h>
22 #include <asm/page.h>
23 #include <asm/mmu.h>
24 #include <asm/cputable.h>
25 #include <asm/thread_info.h>
26 #include <asm/ppc_asm.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/unistd.h>
29 #include <asm/ptrace.h>
30 #include <asm/export.h>
31 #include <asm/feature-fixups.h>
32 #include <asm/barrier.h>
33 #include <asm/kup.h>
34 #include <asm/bug.h>
35 #include <asm/interrupt.h>
36
37 #include "head_32.h"
38
39 /*
40  * powerpc relies on return from interrupt/syscall being context synchronising
41  * (which rfi is) to support ARCH_HAS_MEMBARRIER_SYNC_CORE without additional
42  * synchronisation instructions.
43  */
44
45 /*
46  * Align to 4k in order to ensure that all functions modyfing srr0/srr1
47  * fit into one page in order to not encounter a TLB miss between the
48  * modification of srr0/srr1 and the associated rfi.
49  */
50         .align  12
51
52 #if defined(CONFIG_PPC_BOOK3S_32) || defined(CONFIG_E500)
53         .globl  prepare_transfer_to_handler
54 prepare_transfer_to_handler:
55         /* if from kernel, check interrupted DOZE/NAP mode */
56         lwz     r12,TI_LOCAL_FLAGS(r2)
57         mtcrf   0x01,r12
58         bt-     31-TLF_NAPPING,4f
59         bt-     31-TLF_SLEEPING,7f
60         blr
61
62 4:      rlwinm  r12,r12,0,~_TLF_NAPPING
63         stw     r12,TI_LOCAL_FLAGS(r2)
64         b       power_save_ppc32_restore
65
66 7:      rlwinm  r12,r12,0,~_TLF_SLEEPING
67         stw     r12,TI_LOCAL_FLAGS(r2)
68         lwz     r9,_MSR(r11)            /* if sleeping, clear MSR.EE */
69         rlwinm  r9,r9,0,~MSR_EE
70         lwz     r12,_LINK(r11)          /* and return to address in LR */
71         lwz     r2, GPR2(r11)
72         b       fast_exception_return
73 _ASM_NOKPROBE_SYMBOL(prepare_transfer_to_handler)
74 #endif /* CONFIG_PPC_BOOK3S_32 || CONFIG_E500 */
75
76         .globl  transfer_to_syscall
77 transfer_to_syscall:
78         stw     r11, GPR1(r1)
79         stw     r11, 0(r1)
80         mflr    r12
81         stw     r12, _LINK(r1)
82 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
83         rlwinm  r9,r9,0,14,12           /* clear MSR_WE (necessary?) */
84 #endif
85         lis     r12,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
86         SAVE_GPR(2, r1)
87         addi    r12,r12,STACK_FRAME_REGS_MARKER@l
88         stw     r9,_MSR(r1)
89         li      r2, INTERRUPT_SYSCALL
90         stw     r12,8(r1)
91         stw     r2,_TRAP(r1)
92         SAVE_GPR(0, r1)
93         SAVE_GPRS(3, 8, r1)
94         addi    r2,r10,-THREAD
95         SAVE_NVGPRS(r1)
96
97         /* Calling convention has r9 = orig r0, r10 = regs */
98         addi    r10,r1,STACK_FRAME_OVERHEAD
99         mr      r9,r0
100         bl      system_call_exception
101
102 ret_from_syscall:
103         addi    r4,r1,STACK_FRAME_OVERHEAD
104         li      r5,0
105         bl      syscall_exit_prepare
106 #ifdef CONFIG_PPC_47x
107         lis     r4,icache_44x_need_flush@ha
108         lwz     r5,icache_44x_need_flush@l(r4)
109         cmplwi  cr0,r5,0
110         bne-    2f
111 #endif /* CONFIG_PPC_47x */
112         lwz     r4,_LINK(r1)
113         lwz     r5,_CCR(r1)
114         mtlr    r4
115         lwz     r7,_NIP(r1)
116         lwz     r8,_MSR(r1)
117         cmpwi   r3,0
118         lwz     r3,GPR3(r1)
119 syscall_exit_finish:
120         mtspr   SPRN_SRR0,r7
121         mtspr   SPRN_SRR1,r8
122
123         bne     3f
124         mtcr    r5
125
126 1:      lwz     r2,GPR2(r1)
127         lwz     r1,GPR1(r1)
128         rfi
129 #ifdef CONFIG_40x
130         b .     /* Prevent prefetch past rfi */
131 #endif
132
133 3:      mtcr    r5
134         lwz     r4,_CTR(r1)
135         lwz     r5,_XER(r1)
136         REST_NVGPRS(r1)
137         mtctr   r4
138         mtxer   r5
139         lwz     r0,GPR0(r1)
140         lwz     r3,GPR3(r1)
141         REST_GPRS(4, 11, r1)
142         lwz     r12,GPR12(r1)
143         b       1b
144
145 #ifdef CONFIG_44x
146 2:      li      r7,0
147         iccci   r0,r0
148         stw     r7,icache_44x_need_flush@l(r4)
149         b       1b
150 #endif  /* CONFIG_44x */
151
152         .globl  ret_from_fork
153 ret_from_fork:
154         REST_NVGPRS(r1)
155         bl      schedule_tail
156         li      r3,0
157         b       ret_from_syscall
158
159         .globl  ret_from_kernel_thread
160 ret_from_kernel_thread:
161         REST_NVGPRS(r1)
162         bl      schedule_tail
163         mtctr   r14
164         mr      r3,r15
165         PPC440EP_ERR42
166         bctrl
167         li      r3,0
168         b       ret_from_syscall
169
170 /*
171  * This routine switches between two different tasks.  The process
172  * state of one is saved on its kernel stack.  Then the state
173  * of the other is restored from its kernel stack.  The memory
174  * management hardware is updated to the second process's state.
175  * Finally, we can return to the second process.
176  * On entry, r3 points to the THREAD for the current task, r4
177  * points to the THREAD for the new task.
178  *
179  * This routine is always called with interrupts disabled.
180  *
181  * Note: there are two ways to get to the "going out" portion
182  * of this code; either by coming in via the entry (_switch)
183  * or via "fork" which must set up an environment equivalent
184  * to the "_switch" path.  If you change this , you'll have to
185  * change the fork code also.
186  *
187  * The code which creates the new task context is in 'copy_thread'
188  * in arch/ppc/kernel/process.c
189  */
190 _GLOBAL(_switch)
191         stwu    r1,-INT_FRAME_SIZE(r1)
192         mflr    r0
193         stw     r0,INT_FRAME_SIZE+4(r1)
194         /* r3-r12 are caller saved -- Cort */
195         SAVE_NVGPRS(r1)
196         stw     r0,_NIP(r1)     /* Return to switch caller */
197         mfcr    r10
198         stw     r10,_CCR(r1)
199         stw     r1,KSP(r3)      /* Set old stack pointer */
200
201 #ifdef CONFIG_SMP
202         /* We need a sync somewhere here to make sure that if the
203          * previous task gets rescheduled on another CPU, it sees all
204          * stores it has performed on this one.
205          */
206         sync
207 #endif /* CONFIG_SMP */
208
209         tophys(r0,r4)
210         mtspr   SPRN_SPRG_THREAD,r0     /* Update current THREAD phys addr */
211         lwz     r1,KSP(r4)      /* Load new stack pointer */
212
213         /* save the old current 'last' for return value */
214         mr      r3,r2
215         addi    r2,r4,-THREAD   /* Update current */
216
217         lwz     r0,_CCR(r1)
218         mtcrf   0xFF,r0
219         /* r3-r12 are destroyed -- Cort */
220         REST_NVGPRS(r1)
221
222         lwz     r4,_NIP(r1)     /* Return to _switch caller in new task */
223         mtlr    r4
224         addi    r1,r1,INT_FRAME_SIZE
225         blr
226
227         .globl  fast_exception_return
228 fast_exception_return:
229 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
230         andi.   r10,r9,MSR_RI           /* check for recoverable interrupt */
231         beq     3f                      /* if not, we've got problems */
232 #endif
233
234 2:      REST_GPRS(3, 6, r11)
235         lwz     r10,_CCR(r11)
236         REST_GPRS(1, 2, r11)
237         mtcr    r10
238         lwz     r10,_LINK(r11)
239         mtlr    r10
240         /* Clear the exception_marker on the stack to avoid confusing stacktrace */
241         li      r10, 0
242         stw     r10, 8(r11)
243         REST_GPR(10, r11)
244 #if defined(CONFIG_PPC_8xx) && defined(CONFIG_PERF_EVENTS)
245         mtspr   SPRN_NRI, r0
246 #endif
247         mtspr   SPRN_SRR1,r9
248         mtspr   SPRN_SRR0,r12
249         REST_GPR(9, r11)
250         REST_GPR(12, r11)
251         lwz     r11,GPR11(r11)
252         rfi
253 #ifdef CONFIG_40x
254         b .     /* Prevent prefetch past rfi */
255 #endif
256 _ASM_NOKPROBE_SYMBOL(fast_exception_return)
257
258 /* aargh, a nonrecoverable interrupt, panic */
259 /* aargh, we don't know which trap this is */
260 3:
261         li      r10,-1
262         stw     r10,_TRAP(r11)
263         prepare_transfer_to_handler
264         bl      unrecoverable_exception
265         trap    /* should not get here */
266
267         .globl interrupt_return
268 interrupt_return:
269         lwz     r4,_MSR(r1)
270         addi    r3,r1,STACK_FRAME_OVERHEAD
271         andi.   r0,r4,MSR_PR
272         beq     .Lkernel_interrupt_return
273         bl      interrupt_exit_user_prepare
274         cmpwi   r3,0
275         bne-    .Lrestore_nvgprs
276
277 .Lfast_user_interrupt_return:
278         lwz     r11,_NIP(r1)
279         lwz     r12,_MSR(r1)
280         mtspr   SPRN_SRR0,r11
281         mtspr   SPRN_SRR1,r12
282
283 BEGIN_FTR_SECTION
284         stwcx.  r0,0,r1         /* to clear the reservation */
285 FTR_SECTION_ELSE
286         lwarx   r0,0,r1
287 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
288
289         lwz     r3,_CCR(r1)
290         lwz     r4,_LINK(r1)
291         lwz     r5,_CTR(r1)
292         lwz     r6,_XER(r1)
293         li      r0,0
294
295         /*
296          * Leaving a stale exception_marker on the stack can confuse
297          * the reliable stack unwinder later on. Clear it.
298          */
299         stw     r0,8(r1)
300         REST_GPRS(7, 12, r1)
301
302         mtcr    r3
303         mtlr    r4
304         mtctr   r5
305         mtspr   SPRN_XER,r6
306
307         REST_GPRS(2, 6, r1)
308         REST_GPR(0, r1)
309         REST_GPR(1, r1)
310         rfi
311 #ifdef CONFIG_40x
312         b .     /* Prevent prefetch past rfi */
313 #endif
314
315 .Lrestore_nvgprs:
316         REST_NVGPRS(r1)
317         b       .Lfast_user_interrupt_return
318
319 .Lkernel_interrupt_return:
320         bl      interrupt_exit_kernel_prepare
321
322 .Lfast_kernel_interrupt_return:
323         cmpwi   cr1,r3,0
324         lwz     r11,_NIP(r1)
325         lwz     r12,_MSR(r1)
326         mtspr   SPRN_SRR0,r11
327         mtspr   SPRN_SRR1,r12
328
329 BEGIN_FTR_SECTION
330         stwcx.  r0,0,r1         /* to clear the reservation */
331 FTR_SECTION_ELSE
332         lwarx   r0,0,r1
333 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
334
335         lwz     r3,_LINK(r1)
336         lwz     r4,_CTR(r1)
337         lwz     r5,_XER(r1)
338         lwz     r6,_CCR(r1)
339         li      r0,0
340
341         REST_GPRS(7, 12, r1)
342
343         mtlr    r3
344         mtctr   r4
345         mtspr   SPRN_XER,r5
346
347         /*
348          * Leaving a stale exception_marker on the stack can confuse
349          * the reliable stack unwinder later on. Clear it.
350          */
351         stw     r0,8(r1)
352
353         REST_GPRS(2, 5, r1)
354
355         bne-    cr1,1f /* emulate stack store */
356         mtcr    r6
357         REST_GPR(6, r1)
358         REST_GPR(0, r1)
359         REST_GPR(1, r1)
360         rfi
361 #ifdef CONFIG_40x
362         b .     /* Prevent prefetch past rfi */
363 #endif
364
365 1:      /*
366          * Emulate stack store with update. New r1 value was already calculated
367          * and updated in our interrupt regs by emulate_loadstore, but we can't
368          * store the previous value of r1 to the stack before re-loading our
369          * registers from it, otherwise they could be clobbered.  Use
370          * SPRG Scratch0 as temporary storage to hold the store
371          * data, as interrupts are disabled here so it won't be clobbered.
372          */
373         mtcr    r6
374 #ifdef CONFIG_BOOKE
375         mtspr   SPRN_SPRG_WSCRATCH0, r9
376 #else
377         mtspr   SPRN_SPRG_SCRATCH0, r9
378 #endif
379         addi    r9,r1,INT_FRAME_SIZE /* get original r1 */
380         REST_GPR(6, r1)
381         REST_GPR(0, r1)
382         REST_GPR(1, r1)
383         stw     r9,0(r1) /* perform store component of stwu */
384 #ifdef CONFIG_BOOKE
385         mfspr   r9, SPRN_SPRG_RSCRATCH0
386 #else
387         mfspr   r9, SPRN_SPRG_SCRATCH0
388 #endif
389         rfi
390 #ifdef CONFIG_40x
391         b .     /* Prevent prefetch past rfi */
392 #endif
393 _ASM_NOKPROBE_SYMBOL(interrupt_return)
394
395 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
396
397 /*
398  * Returning from a critical interrupt in user mode doesn't need
399  * to be any different from a normal exception.  For a critical
400  * interrupt in the kernel, we just return (without checking for
401  * preemption) since the interrupt may have happened at some crucial
402  * place (e.g. inside the TLB miss handler), and because we will be
403  * running with r1 pointing into critical_stack, not the current
404  * process's kernel stack (and therefore current_thread_info() will
405  * give the wrong answer).
406  * We have to restore various SPRs that may have been in use at the
407  * time of the critical interrupt.
408  *
409  */
410 #ifdef CONFIG_40x
411 #define PPC_40x_TURN_OFF_MSR_DR                                             \
412         /* avoid any possible TLB misses here by turning off MSR.DR, we     \
413          * assume the instructions here are mapped by a pinned TLB entry */ \
414         li      r10,MSR_IR;                                                 \
415         mtmsr   r10;                                                        \
416         isync;                                                              \
417         tophys(r1, r1);
418 #else
419 #define PPC_40x_TURN_OFF_MSR_DR
420 #endif
421
422 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi)     \
423         REST_NVGPRS(r1);                                                \
424         lwz     r3,_MSR(r1);                                            \
425         andi.   r3,r3,MSR_PR;                                           \
426         bne     interrupt_return;                                       \
427         lwz     r0,GPR0(r1);                                            \
428         lwz     r2,GPR2(r1);                                            \
429         REST_GPRS(3, 8, r1);                                            \
430         lwz     r10,_XER(r1);                                           \
431         lwz     r11,_CTR(r1);                                           \
432         mtspr   SPRN_XER,r10;                                           \
433         mtctr   r11;                                                    \
434         stwcx.  r0,0,r1;                /* to clear the reservation */  \
435         lwz     r11,_LINK(r1);                                          \
436         mtlr    r11;                                                    \
437         lwz     r10,_CCR(r1);                                           \
438         mtcrf   0xff,r10;                                               \
439         PPC_40x_TURN_OFF_MSR_DR;                                        \
440         lwz     r9,_DEAR(r1);                                           \
441         lwz     r10,_ESR(r1);                                           \
442         mtspr   SPRN_DEAR,r9;                                           \
443         mtspr   SPRN_ESR,r10;                                           \
444         lwz     r11,_NIP(r1);                                           \
445         lwz     r12,_MSR(r1);                                           \
446         mtspr   exc_lvl_srr0,r11;                                       \
447         mtspr   exc_lvl_srr1,r12;                                       \
448         lwz     r9,GPR9(r1);                                            \
449         lwz     r12,GPR12(r1);                                          \
450         lwz     r10,GPR10(r1);                                          \
451         lwz     r11,GPR11(r1);                                          \
452         lwz     r1,GPR1(r1);                                            \
453         exc_lvl_rfi;                                                    \
454         b       .;              /* prevent prefetch past exc_lvl_rfi */
455
456 #define RESTORE_xSRR(exc_lvl_srr0, exc_lvl_srr1)                        \
457         lwz     r9,_##exc_lvl_srr0(r1);                                 \
458         lwz     r10,_##exc_lvl_srr1(r1);                                \
459         mtspr   SPRN_##exc_lvl_srr0,r9;                                 \
460         mtspr   SPRN_##exc_lvl_srr1,r10;
461
462 #if defined(CONFIG_PPC_BOOK3E_MMU)
463 #ifdef CONFIG_PHYS_64BIT
464 #define RESTORE_MAS7                                                    \
465         lwz     r11,MAS7(r1);                                           \
466         mtspr   SPRN_MAS7,r11;
467 #else
468 #define RESTORE_MAS7
469 #endif /* CONFIG_PHYS_64BIT */
470 #define RESTORE_MMU_REGS                                                \
471         lwz     r9,MAS0(r1);                                            \
472         lwz     r10,MAS1(r1);                                           \
473         lwz     r11,MAS2(r1);                                           \
474         mtspr   SPRN_MAS0,r9;                                           \
475         lwz     r9,MAS3(r1);                                            \
476         mtspr   SPRN_MAS1,r10;                                          \
477         lwz     r10,MAS6(r1);                                           \
478         mtspr   SPRN_MAS2,r11;                                          \
479         mtspr   SPRN_MAS3,r9;                                           \
480         mtspr   SPRN_MAS6,r10;                                          \
481         RESTORE_MAS7;
482 #elif defined(CONFIG_44x)
483 #define RESTORE_MMU_REGS                                                \
484         lwz     r9,MMUCR(r1);                                           \
485         mtspr   SPRN_MMUCR,r9;
486 #else
487 #define RESTORE_MMU_REGS
488 #endif
489
490 #ifdef CONFIG_40x
491         .globl  ret_from_crit_exc
492 ret_from_crit_exc:
493         lis     r9,crit_srr0@ha;
494         lwz     r9,crit_srr0@l(r9);
495         lis     r10,crit_srr1@ha;
496         lwz     r10,crit_srr1@l(r10);
497         mtspr   SPRN_SRR0,r9;
498         mtspr   SPRN_SRR1,r10;
499         RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
500 _ASM_NOKPROBE_SYMBOL(ret_from_crit_exc)
501 #endif /* CONFIG_40x */
502
503 #ifdef CONFIG_BOOKE
504         .globl  ret_from_crit_exc
505 ret_from_crit_exc:
506         RESTORE_xSRR(SRR0,SRR1);
507         RESTORE_MMU_REGS;
508         RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
509 _ASM_NOKPROBE_SYMBOL(ret_from_crit_exc)
510
511         .globl  ret_from_debug_exc
512 ret_from_debug_exc:
513         RESTORE_xSRR(SRR0,SRR1);
514         RESTORE_xSRR(CSRR0,CSRR1);
515         RESTORE_MMU_REGS;
516         RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, PPC_RFDI)
517 _ASM_NOKPROBE_SYMBOL(ret_from_debug_exc)
518
519         .globl  ret_from_mcheck_exc
520 ret_from_mcheck_exc:
521         RESTORE_xSRR(SRR0,SRR1);
522         RESTORE_xSRR(CSRR0,CSRR1);
523         RESTORE_xSRR(DSRR0,DSRR1);
524         RESTORE_MMU_REGS;
525         RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, PPC_RFMCI)
526 _ASM_NOKPROBE_SYMBOL(ret_from_mcheck_exc)
527 #endif /* CONFIG_BOOKE */
528 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
529
530 /*
531  * PROM code for specific machines follows.  Put it
532  * here so it's easy to add arch-specific sections later.
533  * -- Cort
534  */
535 #ifdef CONFIG_PPC_RTAS
536 /*
537  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
538  * called with the MMU off.
539  */
540 _GLOBAL(enter_rtas)
541         stwu    r1,-INT_FRAME_SIZE(r1)
542         mflr    r0
543         stw     r0,INT_FRAME_SIZE+4(r1)
544         LOAD_REG_ADDR(r4, rtas)
545         lis     r6,1f@ha        /* physical return address for rtas */
546         addi    r6,r6,1f@l
547         tophys(r6,r6)
548         lwz     r8,RTASENTRY(r4)
549         lwz     r4,RTASBASE(r4)
550         mfmsr   r9
551         stw     r9,8(r1)
552         LOAD_REG_IMMEDIATE(r0,MSR_KERNEL)
553         mtmsr   r0      /* disable interrupts so SRR0/1 don't get trashed */
554         li      r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
555         mtlr    r6
556         stw     r1, THREAD + RTAS_SP(r2)
557         mtspr   SPRN_SRR0,r8
558         mtspr   SPRN_SRR1,r9
559         rfi
560 1:
561         lis     r8, 1f@h
562         ori     r8, r8, 1f@l
563         LOAD_REG_IMMEDIATE(r9,MSR_KERNEL)
564         mtspr   SPRN_SRR0,r8
565         mtspr   SPRN_SRR1,r9
566         rfi                     /* Reactivate MMU translation */
567 1:
568         lwz     r8,INT_FRAME_SIZE+4(r1) /* get return address */
569         lwz     r9,8(r1)        /* original msr value */
570         addi    r1,r1,INT_FRAME_SIZE
571         li      r0,0
572         stw     r0, THREAD + RTAS_SP(r2)
573         mtlr    r8
574         mtmsr   r9
575         blr                     /* return to caller */
576 _ASM_NOKPROBE_SYMBOL(enter_rtas)
577 #endif /* CONFIG_PPC_RTAS */