Merge tag 'regulator-fix-v5.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / arch / powerpc / kernel / entry_64.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@cs.nmt.edu) for PReP
6  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
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 <asm/cache.h>
20 #include <asm/unistd.h>
21 #include <asm/processor.h>
22 #include <asm/page.h>
23 #include <asm/mmu.h>
24 #include <asm/thread_info.h>
25 #include <asm/code-patching-asm.h>
26 #include <asm/ppc_asm.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/cputable.h>
29 #include <asm/firmware.h>
30 #include <asm/bug.h>
31 #include <asm/ptrace.h>
32 #include <asm/irqflags.h>
33 #include <asm/hw_irq.h>
34 #include <asm/context_tracking.h>
35 #include <asm/tm.h>
36 #include <asm/ppc-opcode.h>
37 #include <asm/barrier.h>
38 #include <asm/export.h>
39 #include <asm/asm-compat.h>
40 #ifdef CONFIG_PPC_BOOK3S
41 #include <asm/exception-64s.h>
42 #else
43 #include <asm/exception-64e.h>
44 #endif
45 #include <asm/feature-fixups.h>
46 #include <asm/kup.h>
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 #ifdef CONFIG_COMPAT
56 COMPAT_SYS_CALL_TABLE:
57         .tc compat_sys_call_table[TC],compat_sys_call_table
58 #endif
59
60 /* This value is used to mark exception frames on the stack. */
61 exception_marker:
62         .tc     ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
63
64         .section        ".text"
65         .align 7
66
67 #ifdef CONFIG_PPC_BOOK3S
68 .macro system_call_vectored name trapnr
69         .globl system_call_vectored_\name
70 system_call_vectored_\name:
71 _ASM_NOKPROBE_SYMBOL(system_call_vectored_\name)
72 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
73 BEGIN_FTR_SECTION
74         extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
75         bne     .Ltabort_syscall
76 END_FTR_SECTION_IFSET(CPU_FTR_TM)
77 #endif
78         SCV_INTERRUPT_TO_KERNEL
79         mr      r10,r1
80         ld      r1,PACAKSAVE(r13)
81         std     r10,0(r1)
82         std     r11,_NIP(r1)
83         std     r12,_MSR(r1)
84         std     r0,GPR0(r1)
85         std     r10,GPR1(r1)
86         std     r2,GPR2(r1)
87         ld      r2,PACATOC(r13)
88         mfcr    r12
89         li      r11,0
90         /* Can we avoid saving r3-r8 in common case? */
91         std     r3,GPR3(r1)
92         std     r4,GPR4(r1)
93         std     r5,GPR5(r1)
94         std     r6,GPR6(r1)
95         std     r7,GPR7(r1)
96         std     r8,GPR8(r1)
97         /* Zero r9-r12, this should only be required when restoring all GPRs */
98         std     r11,GPR9(r1)
99         std     r11,GPR10(r1)
100         std     r11,GPR11(r1)
101         std     r11,GPR12(r1)
102         std     r9,GPR13(r1)
103         SAVE_NVGPRS(r1)
104         std     r11,_XER(r1)
105         std     r11,_LINK(r1)
106         std     r11,_CTR(r1)
107
108         li      r11,\trapnr
109         std     r11,_TRAP(r1)
110         std     r12,_CCR(r1)
111         addi    r10,r1,STACK_FRAME_OVERHEAD
112         ld      r11,exception_marker@toc(r2)
113         std     r11,-16(r10)            /* "regshere" marker */
114
115 BEGIN_FTR_SECTION
116         HMT_MEDIUM
117 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
118
119         /*
120          * scv enters with MSR[EE]=1 and is immediately considered soft-masked.
121          * The entry vector already sets PACAIRQSOFTMASK to IRQS_ALL_DISABLED,
122          * and interrupts may be masked and pending already.
123          * system_call_exception() will call trace_hardirqs_off() which means
124          * interrupts could already have been blocked before trace_hardirqs_off,
125          * but this is the best we can do.
126          */
127
128         /* Calling convention has r9 = orig r0, r10 = regs */
129         mr      r9,r0
130         bl      system_call_exception
131
132 .Lsyscall_vectored_\name\()_exit:
133         addi    r4,r1,STACK_FRAME_OVERHEAD
134         li      r5,1 /* scv */
135         bl      syscall_exit_prepare
136
137         ld      r2,_CCR(r1)
138         ld      r4,_NIP(r1)
139         ld      r5,_MSR(r1)
140
141 BEGIN_FTR_SECTION
142         stdcx.  r0,0,r1                 /* to clear the reservation */
143 END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
144
145 BEGIN_FTR_SECTION
146         HMT_MEDIUM_LOW
147 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
148
149         cmpdi   r3,0
150         bne     .Lsyscall_vectored_\name\()_restore_regs
151
152         /* rfscv returns with LR->NIA and CTR->MSR */
153         mtlr    r4
154         mtctr   r5
155
156         /* Could zero these as per ABI, but we may consider a stricter ABI
157          * which preserves these if libc implementations can benefit, so
158          * restore them for now until further measurement is done. */
159         ld      r0,GPR0(r1)
160         ld      r4,GPR4(r1)
161         ld      r5,GPR5(r1)
162         ld      r6,GPR6(r1)
163         ld      r7,GPR7(r1)
164         ld      r8,GPR8(r1)
165         /* Zero volatile regs that may contain sensitive kernel data */
166         li      r9,0
167         li      r10,0
168         li      r11,0
169         li      r12,0
170         mtspr   SPRN_XER,r0
171
172         /*
173          * We don't need to restore AMR on the way back to userspace for KUAP.
174          * The value of AMR only matters while we're in the kernel.
175          */
176         mtcr    r2
177         ld      r2,GPR2(r1)
178         ld      r3,GPR3(r1)
179         ld      r13,GPR13(r1)
180         ld      r1,GPR1(r1)
181         RFSCV_TO_USER
182         b       .       /* prevent speculative execution */
183
184 .Lsyscall_vectored_\name\()_restore_regs:
185         li      r3,0
186         mtmsrd  r3,1
187         mtspr   SPRN_SRR0,r4
188         mtspr   SPRN_SRR1,r5
189
190         ld      r3,_CTR(r1)
191         ld      r4,_LINK(r1)
192         ld      r5,_XER(r1)
193
194         REST_NVGPRS(r1)
195         ld      r0,GPR0(r1)
196         mtcr    r2
197         mtctr   r3
198         mtlr    r4
199         mtspr   SPRN_XER,r5
200         REST_10GPRS(2, r1)
201         REST_2GPRS(12, r1)
202         ld      r1,GPR1(r1)
203         RFI_TO_USER
204 .endm
205
206 system_call_vectored common 0x3000
207 /*
208  * We instantiate another entry copy for the SIGILL variant, with TRAP=0x7ff0
209  * which is tested by system_call_exception when r0 is -1 (as set by vector
210  * entry code).
211  */
212 system_call_vectored sigill 0x7ff0
213
214
215 /*
216  * Entered via kernel return set up by kernel/sstep.c, must match entry regs
217  */
218         .globl system_call_vectored_emulate
219 system_call_vectored_emulate:
220 _ASM_NOKPROBE_SYMBOL(system_call_vectored_emulate)
221         li      r10,IRQS_ALL_DISABLED
222         stb     r10,PACAIRQSOFTMASK(r13)
223         b       system_call_vectored_common
224 #endif
225
226         .balign IFETCH_ALIGN_BYTES
227         .globl system_call_common_real
228 system_call_common_real:
229         ld      r10,PACAKMSR(r13)       /* get MSR value for kernel */
230         mtmsrd  r10
231
232         .balign IFETCH_ALIGN_BYTES
233         .globl system_call_common
234 system_call_common:
235 _ASM_NOKPROBE_SYMBOL(system_call_common)
236 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
237 BEGIN_FTR_SECTION
238         extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
239         bne     .Ltabort_syscall
240 END_FTR_SECTION_IFSET(CPU_FTR_TM)
241 #endif
242         mr      r10,r1
243         ld      r1,PACAKSAVE(r13)
244         std     r10,0(r1)
245         std     r11,_NIP(r1)
246         std     r12,_MSR(r1)
247         std     r0,GPR0(r1)
248         std     r10,GPR1(r1)
249         std     r2,GPR2(r1)
250 #ifdef CONFIG_PPC_FSL_BOOK3E
251 START_BTB_FLUSH_SECTION
252         BTB_FLUSH(r10)
253 END_BTB_FLUSH_SECTION
254 #endif
255         ld      r2,PACATOC(r13)
256         mfcr    r12
257         li      r11,0
258         /* Can we avoid saving r3-r8 in common case? */
259         std     r3,GPR3(r1)
260         std     r4,GPR4(r1)
261         std     r5,GPR5(r1)
262         std     r6,GPR6(r1)
263         std     r7,GPR7(r1)
264         std     r8,GPR8(r1)
265         /* Zero r9-r12, this should only be required when restoring all GPRs */
266         std     r11,GPR9(r1)
267         std     r11,GPR10(r1)
268         std     r11,GPR11(r1)
269         std     r11,GPR12(r1)
270         std     r9,GPR13(r1)
271         SAVE_NVGPRS(r1)
272         std     r11,_XER(r1)
273         std     r11,_CTR(r1)
274         mflr    r10
275
276         /*
277          * This clears CR0.SO (bit 28), which is the error indication on
278          * return from this system call.
279          */
280         rldimi  r12,r11,28,(63-28)
281         li      r11,0xc00
282         std     r10,_LINK(r1)
283         std     r11,_TRAP(r1)
284         std     r12,_CCR(r1)
285         addi    r10,r1,STACK_FRAME_OVERHEAD
286         ld      r11,exception_marker@toc(r2)
287         std     r11,-16(r10)            /* "regshere" marker */
288
289         /*
290          * We always enter kernel from userspace with irq soft-mask enabled and
291          * nothing pending. system_call_exception() will call
292          * trace_hardirqs_off().
293          */
294         li      r11,IRQS_ALL_DISABLED
295         li      r12,PACA_IRQ_HARD_DIS
296         stb     r11,PACAIRQSOFTMASK(r13)
297         stb     r12,PACAIRQHAPPENED(r13)
298
299         /* Calling convention has r9 = orig r0, r10 = regs */
300         mr      r9,r0
301         bl      system_call_exception
302
303 .Lsyscall_exit:
304         addi    r4,r1,STACK_FRAME_OVERHEAD
305         li      r5,0 /* !scv */
306         bl      syscall_exit_prepare
307
308         ld      r2,_CCR(r1)
309         ld      r4,_NIP(r1)
310         ld      r5,_MSR(r1)
311         ld      r6,_LINK(r1)
312
313 BEGIN_FTR_SECTION
314         stdcx.  r0,0,r1                 /* to clear the reservation */
315 END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
316
317         mtspr   SPRN_SRR0,r4
318         mtspr   SPRN_SRR1,r5
319         mtlr    r6
320
321         cmpdi   r3,0
322         bne     .Lsyscall_restore_regs
323         /* Zero volatile regs that may contain sensitive kernel data */
324         li      r0,0
325         li      r4,0
326         li      r5,0
327         li      r6,0
328         li      r7,0
329         li      r8,0
330         li      r9,0
331         li      r10,0
332         li      r11,0
333         li      r12,0
334         mtctr   r0
335         mtspr   SPRN_XER,r0
336 .Lsyscall_restore_regs_cont:
337
338 BEGIN_FTR_SECTION
339         HMT_MEDIUM_LOW
340 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
341
342         /*
343          * We don't need to restore AMR on the way back to userspace for KUAP.
344          * The value of AMR only matters while we're in the kernel.
345          */
346         mtcr    r2
347         ld      r2,GPR2(r1)
348         ld      r3,GPR3(r1)
349         ld      r13,GPR13(r1)
350         ld      r1,GPR1(r1)
351         RFI_TO_USER
352         b       .       /* prevent speculative execution */
353
354 .Lsyscall_restore_regs:
355         ld      r3,_CTR(r1)
356         ld      r4,_XER(r1)
357         REST_NVGPRS(r1)
358         mtctr   r3
359         mtspr   SPRN_XER,r4
360         ld      r0,GPR0(r1)
361         REST_8GPRS(4, r1)
362         ld      r12,GPR12(r1)
363         b       .Lsyscall_restore_regs_cont
364
365 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
366 .Ltabort_syscall:
367         /* Firstly we need to enable TM in the kernel */
368         mfmsr   r10
369         li      r9, 1
370         rldimi  r10, r9, MSR_TM_LG, 63-MSR_TM_LG
371         mtmsrd  r10, 0
372
373         /* tabort, this dooms the transaction, nothing else */
374         li      r9, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT)
375         TABORT(R9)
376
377         /*
378          * Return directly to userspace. We have corrupted user register state,
379          * but userspace will never see that register state. Execution will
380          * resume after the tbegin of the aborted transaction with the
381          * checkpointed register state.
382          */
383         li      r9, MSR_RI
384         andc    r10, r10, r9
385         mtmsrd  r10, 1
386         mtspr   SPRN_SRR0, r11
387         mtspr   SPRN_SRR1, r12
388         RFI_TO_USER
389         b       .       /* prevent speculative execution */
390 #endif
391
392 #ifdef CONFIG_PPC_BOOK3S
393 _GLOBAL(ret_from_fork_scv)
394         bl      schedule_tail
395         REST_NVGPRS(r1)
396         li      r3,0    /* fork() return value */
397         b       .Lsyscall_vectored_common_exit
398 #endif
399
400 _GLOBAL(ret_from_fork)
401         bl      schedule_tail
402         REST_NVGPRS(r1)
403         li      r3,0    /* fork() return value */
404         b       .Lsyscall_exit
405
406 _GLOBAL(ret_from_kernel_thread)
407         bl      schedule_tail
408         REST_NVGPRS(r1)
409         mtctr   r14
410         mr      r3,r15
411 #ifdef PPC64_ELF_ABI_v2
412         mr      r12,r14
413 #endif
414         bctrl
415         li      r3,0
416         b       .Lsyscall_exit
417
418 #ifdef CONFIG_PPC_BOOK3S_64
419
420 #define FLUSH_COUNT_CACHE       \
421 1:      nop;                    \
422         patch_site 1b, patch__call_flush_branch_caches1; \
423 1:      nop;                    \
424         patch_site 1b, patch__call_flush_branch_caches2; \
425 1:      nop;                    \
426         patch_site 1b, patch__call_flush_branch_caches3
427
428 .macro nops number
429         .rept \number
430         nop
431         .endr
432 .endm
433
434 .balign 32
435 .global flush_branch_caches
436 flush_branch_caches:
437         /* Save LR into r9 */
438         mflr    r9
439
440         // Flush the link stack
441         .rept 64
442         bl      .+4
443         .endr
444         b       1f
445         nops    6
446
447         .balign 32
448         /* Restore LR */
449 1:      mtlr    r9
450
451         // If we're just flushing the link stack, return here
452 3:      nop
453         patch_site 3b patch__flush_link_stack_return
454
455         li      r9,0x7fff
456         mtctr   r9
457
458         PPC_BCCTR_FLUSH
459
460 2:      nop
461         patch_site 2b patch__flush_count_cache_return
462
463         nops    3
464
465         .rept 278
466         .balign 32
467         PPC_BCCTR_FLUSH
468         nops    7
469         .endr
470
471         blr
472 #else
473 #define FLUSH_COUNT_CACHE
474 #endif /* CONFIG_PPC_BOOK3S_64 */
475
476 /*
477  * This routine switches between two different tasks.  The process
478  * state of one is saved on its kernel stack.  Then the state
479  * of the other is restored from its kernel stack.  The memory
480  * management hardware is updated to the second process's state.
481  * Finally, we can return to the second process, via interrupt_return.
482  * On entry, r3 points to the THREAD for the current task, r4
483  * points to the THREAD for the new task.
484  *
485  * Note: there are two ways to get to the "going out" portion
486  * of this code; either by coming in via the entry (_switch)
487  * or via "fork" which must set up an environment equivalent
488  * to the "_switch" path.  If you change this you'll have to change
489  * the fork code also.
490  *
491  * The code which creates the new task context is in 'copy_thread'
492  * in arch/powerpc/kernel/process.c 
493  */
494         .align  7
495 _GLOBAL(_switch)
496         mflr    r0
497         std     r0,16(r1)
498         stdu    r1,-SWITCH_FRAME_SIZE(r1)
499         /* r3-r13 are caller saved -- Cort */
500         SAVE_NVGPRS(r1)
501         std     r0,_NIP(r1)     /* Return to switch caller */
502         mfcr    r23
503         std     r23,_CCR(r1)
504         std     r1,KSP(r3)      /* Set old stack pointer */
505
506         kuap_check_amr r9, r10
507
508         FLUSH_COUNT_CACHE       /* Clobbers r9, ctr */
509
510         /*
511          * On SMP kernels, care must be taken because a task may be
512          * scheduled off CPUx and on to CPUy. Memory ordering must be
513          * considered.
514          *
515          * Cacheable stores on CPUx will be visible when the task is
516          * scheduled on CPUy by virtue of the core scheduler barriers
517          * (see "Notes on Program-Order guarantees on SMP systems." in
518          * kernel/sched/core.c).
519          *
520          * Uncacheable stores in the case of involuntary preemption must
521          * be taken care of. The smp_mb__after_spinlock() in __schedule()
522          * is implemented as hwsync on powerpc, which orders MMIO too. So
523          * long as there is an hwsync in the context switch path, it will
524          * be executed on the source CPU after the task has performed
525          * all MMIO ops on that CPU, and on the destination CPU before the
526          * task performs any MMIO ops there.
527          */
528
529         /*
530          * The kernel context switch path must contain a spin_lock,
531          * which contains larx/stcx, which will clear any reservation
532          * of the task being switched.
533          */
534 #ifdef CONFIG_PPC_BOOK3S
535 /* Cancel all explict user streams as they will have no use after context
536  * switch and will stop the HW from creating streams itself
537  */
538         DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r6)
539 #endif
540
541         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
542         std     r6,PACACURRENT(r13)     /* Set new 'current' */
543 #if defined(CONFIG_STACKPROTECTOR)
544         ld      r6, TASK_CANARY(r6)
545         std     r6, PACA_CANARY(r13)
546 #endif
547
548         ld      r8,KSP(r4)      /* new stack pointer */
549 #ifdef CONFIG_PPC_BOOK3S_64
550 BEGIN_MMU_FTR_SECTION
551         b       2f
552 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
553 BEGIN_FTR_SECTION
554         clrrdi  r6,r8,28        /* get its ESID */
555         clrrdi  r9,r1,28        /* get current sp ESID */
556 FTR_SECTION_ELSE
557         clrrdi  r6,r8,40        /* get its 1T ESID */
558         clrrdi  r9,r1,40        /* get current sp 1T ESID */
559 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
560         clrldi. r0,r6,2         /* is new ESID c00000000? */
561         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
562         cror    eq,4*cr1+eq,eq
563         beq     2f              /* if yes, don't slbie it */
564
565         /* Bolt in the new stack SLB entry */
566         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
567         oris    r0,r6,(SLB_ESID_V)@h
568         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
569 BEGIN_FTR_SECTION
570         li      r9,MMU_SEGSIZE_1T       /* insert B field */
571         oris    r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
572         rldimi  r7,r9,SLB_VSID_SSIZE_SHIFT,0
573 END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
574
575         /* Update the last bolted SLB.  No write barriers are needed
576          * here, provided we only update the current CPU's SLB shadow
577          * buffer.
578          */
579         ld      r9,PACA_SLBSHADOWPTR(r13)
580         li      r12,0
581         std     r12,SLBSHADOW_STACKESID(r9)     /* Clear ESID */
582         li      r12,SLBSHADOW_STACKVSID
583         STDX_BE r7,r12,r9                       /* Save VSID */
584         li      r12,SLBSHADOW_STACKESID
585         STDX_BE r0,r12,r9                       /* Save ESID */
586
587         /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
588          * we have 1TB segments, the only CPUs known to have the errata
589          * only support less than 1TB of system memory and we'll never
590          * actually hit this code path.
591          */
592
593         isync
594         slbie   r6
595 BEGIN_FTR_SECTION
596         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
597 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
598         slbmte  r7,r0
599         isync
600 2:
601 #endif /* CONFIG_PPC_BOOK3S_64 */
602
603         clrrdi  r7, r8, THREAD_SHIFT    /* base of new stack */
604         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
605            because we don't need to leave the 288-byte ABI gap at the
606            top of the kernel stack. */
607         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
608
609         /*
610          * PMU interrupts in radix may come in here. They will use r1, not
611          * PACAKSAVE, so this stack switch will not cause a problem. They
612          * will store to the process stack, which may then be migrated to
613          * another CPU. However the rq lock release on this CPU paired with
614          * the rq lock acquire on the new CPU before the stack becomes
615          * active on the new CPU, will order those stores.
616          */
617         mr      r1,r8           /* start using new stack pointer */
618         std     r7,PACAKSAVE(r13)
619
620         ld      r6,_CCR(r1)
621         mtcrf   0xFF,r6
622
623         /* r3-r13 are destroyed -- Cort */
624         REST_NVGPRS(r1)
625
626         /* convert old thread to its task_struct for return value */
627         addi    r3,r3,-THREAD
628         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
629         mtlr    r7
630         addi    r1,r1,SWITCH_FRAME_SIZE
631         blr
632
633         /*
634          * If MSR EE/RI was never enabled, IRQs not reconciled, NVGPRs not
635          * touched, no exit work created, then this can be used.
636          */
637         .balign IFETCH_ALIGN_BYTES
638         .globl fast_interrupt_return
639 fast_interrupt_return:
640 _ASM_NOKPROBE_SYMBOL(fast_interrupt_return)
641         kuap_check_amr r3, r4
642         ld      r5,_MSR(r1)
643         andi.   r0,r5,MSR_PR
644 #ifdef CONFIG_PPC_BOOK3S
645         bne     .Lfast_user_interrupt_return_amr
646         kuap_kernel_restore r3, r4
647         andi.   r0,r5,MSR_RI
648         li      r3,0 /* 0 return value, no EMULATE_STACK_STORE */
649         bne+    .Lfast_kernel_interrupt_return
650         addi    r3,r1,STACK_FRAME_OVERHEAD
651         bl      unrecoverable_exception
652         b       . /* should not get here */
653 #else
654         bne     .Lfast_user_interrupt_return
655         b       .Lfast_kernel_interrupt_return
656 #endif
657
658         .balign IFETCH_ALIGN_BYTES
659         .globl interrupt_return
660 interrupt_return:
661 _ASM_NOKPROBE_SYMBOL(interrupt_return)
662         ld      r4,_MSR(r1)
663         andi.   r0,r4,MSR_PR
664         beq     .Lkernel_interrupt_return
665         addi    r3,r1,STACK_FRAME_OVERHEAD
666         bl      interrupt_exit_user_prepare
667         cmpdi   r3,0
668         bne-    .Lrestore_nvgprs
669
670 #ifdef CONFIG_PPC_BOOK3S
671 .Lfast_user_interrupt_return_amr:
672         kuap_user_restore r3, r4
673 #endif
674 .Lfast_user_interrupt_return:
675         ld      r11,_NIP(r1)
676         ld      r12,_MSR(r1)
677 BEGIN_FTR_SECTION
678         ld      r10,_PPR(r1)
679         mtspr   SPRN_PPR,r10
680 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
681         mtspr   SPRN_SRR0,r11
682         mtspr   SPRN_SRR1,r12
683
684 BEGIN_FTR_SECTION
685         stdcx.  r0,0,r1         /* to clear the reservation */
686 FTR_SECTION_ELSE
687         ldarx   r0,0,r1
688 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
689
690         ld      r3,_CCR(r1)
691         ld      r4,_LINK(r1)
692         ld      r5,_CTR(r1)
693         ld      r6,_XER(r1)
694         li      r0,0
695
696         REST_4GPRS(7, r1)
697         REST_2GPRS(11, r1)
698         REST_GPR(13, r1)
699
700         mtcr    r3
701         mtlr    r4
702         mtctr   r5
703         mtspr   SPRN_XER,r6
704
705         REST_4GPRS(2, r1)
706         REST_GPR(6, r1)
707         REST_GPR(0, r1)
708         REST_GPR(1, r1)
709         RFI_TO_USER
710         b       .       /* prevent speculative execution */
711
712 .Lrestore_nvgprs:
713         REST_NVGPRS(r1)
714         b       .Lfast_user_interrupt_return
715
716         .balign IFETCH_ALIGN_BYTES
717 .Lkernel_interrupt_return:
718         addi    r3,r1,STACK_FRAME_OVERHEAD
719         bl      interrupt_exit_kernel_prepare
720
721 .Lfast_kernel_interrupt_return:
722         cmpdi   cr1,r3,0
723         ld      r11,_NIP(r1)
724         ld      r12,_MSR(r1)
725         mtspr   SPRN_SRR0,r11
726         mtspr   SPRN_SRR1,r12
727
728 BEGIN_FTR_SECTION
729         stdcx.  r0,0,r1         /* to clear the reservation */
730 FTR_SECTION_ELSE
731         ldarx   r0,0,r1
732 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
733
734         ld      r3,_LINK(r1)
735         ld      r4,_CTR(r1)
736         ld      r5,_XER(r1)
737         ld      r6,_CCR(r1)
738         li      r0,0
739
740         REST_4GPRS(7, r1)
741         REST_2GPRS(11, r1)
742
743         mtlr    r3
744         mtctr   r4
745         mtspr   SPRN_XER,r5
746
747         /*
748          * Leaving a stale exception_marker on the stack can confuse
749          * the reliable stack unwinder later on. Clear it.
750          */
751         std     r0,STACK_FRAME_OVERHEAD-16(r1)
752
753         REST_4GPRS(2, r1)
754
755         bne-    cr1,1f /* emulate stack store */
756         mtcr    r6
757         REST_GPR(6, r1)
758         REST_GPR(0, r1)
759         REST_GPR(1, r1)
760         RFI_TO_KERNEL
761         b       .       /* prevent speculative execution */
762
763 1:      /*
764          * Emulate stack store with update. New r1 value was already calculated
765          * and updated in our interrupt regs by emulate_loadstore, but we can't
766          * store the previous value of r1 to the stack before re-loading our
767          * registers from it, otherwise they could be clobbered.  Use
768          * PACA_EXGEN as temporary storage to hold the store data, as
769          * interrupts are disabled here so it won't be clobbered.
770          */
771         mtcr    r6
772         std     r9,PACA_EXGEN+0(r13)
773         addi    r9,r1,INT_FRAME_SIZE /* get original r1 */
774         REST_GPR(6, r1)
775         REST_GPR(0, r1)
776         REST_GPR(1, r1)
777         std     r9,0(r1) /* perform store component of stdu */
778         ld      r9,PACA_EXGEN+0(r13)
779
780         RFI_TO_KERNEL
781         b       .       /* prevent speculative execution */
782
783 #ifdef CONFIG_PPC_RTAS
784 /*
785  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
786  * called with the MMU off.
787  *
788  * In addition, we need to be in 32b mode, at least for now.
789  * 
790  * Note: r3 is an input parameter to rtas, so don't trash it...
791  */
792 _GLOBAL(enter_rtas)
793         mflr    r0
794         std     r0,16(r1)
795         stdu    r1,-SWITCH_FRAME_SIZE(r1) /* Save SP and create stack space. */
796
797         /* Because RTAS is running in 32b mode, it clobbers the high order half
798          * of all registers that it saves.  We therefore save those registers
799          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
800          */
801         SAVE_GPR(2, r1)                 /* Save the TOC */
802         SAVE_GPR(13, r1)                /* Save paca */
803         SAVE_NVGPRS(r1)                 /* Save the non-volatiles */
804
805         mfcr    r4
806         std     r4,_CCR(r1)
807         mfctr   r5
808         std     r5,_CTR(r1)
809         mfspr   r6,SPRN_XER
810         std     r6,_XER(r1)
811         mfdar   r7
812         std     r7,_DAR(r1)
813         mfdsisr r8
814         std     r8,_DSISR(r1)
815
816         /* Temporary workaround to clear CR until RTAS can be modified to
817          * ignore all bits.
818          */
819         li      r0,0
820         mtcr    r0
821
822 #ifdef CONFIG_BUG
823         /* There is no way it is acceptable to get here with interrupts enabled,
824          * check it with the asm equivalent of WARN_ON
825          */
826         lbz     r0,PACAIRQSOFTMASK(r13)
827 1:      tdeqi   r0,IRQS_ENABLED
828         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
829 #endif
830
831         /* Hard-disable interrupts */
832         mfmsr   r6
833         rldicl  r7,r6,48,1
834         rotldi  r7,r7,16
835         mtmsrd  r7,1
836
837         /* Unfortunately, the stack pointer and the MSR are also clobbered,
838          * so they are saved in the PACA which allows us to restore
839          * our original state after RTAS returns.
840          */
841         std     r1,PACAR1(r13)
842         std     r6,PACASAVEDMSR(r13)
843
844         /* Setup our real return addr */        
845         LOAD_REG_ADDR(r4,rtas_return_loc)
846         clrldi  r4,r4,2                 /* convert to realmode address */
847         mtlr    r4
848
849         li      r0,0
850         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
851         andc    r0,r6,r0
852         
853         li      r9,1
854         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
855         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI|MSR_LE
856         andc    r6,r0,r9
857
858 __enter_rtas:
859         sync                            /* disable interrupts so SRR0/1 */
860         mtmsrd  r0                      /* don't get trashed */
861
862         LOAD_REG_ADDR(r4, rtas)
863         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
864         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
865         
866         mtspr   SPRN_SRR0,r5
867         mtspr   SPRN_SRR1,r6
868         RFI_TO_KERNEL
869         b       .       /* prevent speculative execution */
870
871 rtas_return_loc:
872         FIXUP_ENDIAN
873
874         /*
875          * Clear RI and set SF before anything.
876          */
877         mfmsr   r6
878         li      r0,MSR_RI
879         andc    r6,r6,r0
880         sldi    r0,r0,(MSR_SF_LG - MSR_RI_LG)
881         or      r6,r6,r0
882         sync
883         mtmsrd  r6
884
885         /* relocation is off at this point */
886         GET_PACA(r4)
887         clrldi  r4,r4,2                 /* convert to realmode address */
888
889         bcl     20,31,$+4
890 0:      mflr    r3
891         ld      r3,(1f-0b)(r3)          /* get &rtas_restore_regs */
892
893         ld      r1,PACAR1(r4)           /* Restore our SP */
894         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
895
896         mtspr   SPRN_SRR0,r3
897         mtspr   SPRN_SRR1,r4
898         RFI_TO_KERNEL
899         b       .       /* prevent speculative execution */
900 _ASM_NOKPROBE_SYMBOL(__enter_rtas)
901 _ASM_NOKPROBE_SYMBOL(rtas_return_loc)
902
903         .align  3
904 1:      .8byte  rtas_restore_regs
905
906 rtas_restore_regs:
907         /* relocation is on at this point */
908         REST_GPR(2, r1)                 /* Restore the TOC */
909         REST_GPR(13, r1)                /* Restore paca */
910         REST_NVGPRS(r1)                 /* Restore the non-volatiles */
911
912         GET_PACA(r13)
913
914         ld      r4,_CCR(r1)
915         mtcr    r4
916         ld      r5,_CTR(r1)
917         mtctr   r5
918         ld      r6,_XER(r1)
919         mtspr   SPRN_XER,r6
920         ld      r7,_DAR(r1)
921         mtdar   r7
922         ld      r8,_DSISR(r1)
923         mtdsisr r8
924
925         addi    r1,r1,SWITCH_FRAME_SIZE /* Unstack our frame */
926         ld      r0,16(r1)               /* get return address */
927
928         mtlr    r0
929         blr                             /* return to caller */
930
931 #endif /* CONFIG_PPC_RTAS */
932
933 _GLOBAL(enter_prom)
934         mflr    r0
935         std     r0,16(r1)
936         stdu    r1,-SWITCH_FRAME_SIZE(r1) /* Save SP and create stack space */
937
938         /* Because PROM is running in 32b mode, it clobbers the high order half
939          * of all registers that it saves.  We therefore save those registers
940          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
941          */
942         SAVE_GPR(2, r1)
943         SAVE_GPR(13, r1)
944         SAVE_NVGPRS(r1)
945         mfcr    r10
946         mfmsr   r11
947         std     r10,_CCR(r1)
948         std     r11,_MSR(r1)
949
950         /* Put PROM address in SRR0 */
951         mtsrr0  r4
952
953         /* Setup our trampoline return addr in LR */
954         bcl     20,31,$+4
955 0:      mflr    r4
956         addi    r4,r4,(1f - 0b)
957         mtlr    r4
958
959         /* Prepare a 32-bit mode big endian MSR
960          */
961 #ifdef CONFIG_PPC_BOOK3E
962         rlwinm  r11,r11,0,1,31
963         mtsrr1  r11
964         rfi
965 #else /* CONFIG_PPC_BOOK3E */
966         LOAD_REG_IMMEDIATE(r12, MSR_SF | MSR_LE)
967         andc    r11,r11,r12
968         mtsrr1  r11
969         RFI_TO_KERNEL
970 #endif /* CONFIG_PPC_BOOK3E */
971
972 1:      /* Return from OF */
973         FIXUP_ENDIAN
974
975         /* Just make sure that r1 top 32 bits didn't get
976          * corrupt by OF
977          */
978         rldicl  r1,r1,0,32
979
980         /* Restore the MSR (back to 64 bits) */
981         ld      r0,_MSR(r1)
982         MTMSRD(r0)
983         isync
984
985         /* Restore other registers */
986         REST_GPR(2, r1)
987         REST_GPR(13, r1)
988         REST_NVGPRS(r1)
989         ld      r4,_CCR(r1)
990         mtcr    r4
991
992         addi    r1,r1,SWITCH_FRAME_SIZE
993         ld      r0,16(r1)
994         mtlr    r0
995         blr