GNU Linux-libre 4.19.304-gnu1
[releases.git] / arch / powerpc / kernel / exceptions-64s.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This file contains the 64-bit "server" PowerPC variant
4  * of the low level exception handling including exception
5  * vectors, exception return, part of the slb and stab
6  * handling and other fixed offset specific things.
7  *
8  * This file is meant to be #included from head_64.S due to
9  * position dependent assembly.
10  *
11  * Most of this originates from head_64.S and thus has the same
12  * copyright history.
13  *
14  */
15
16 #include <asm/hw_irq.h>
17 #include <asm/exception-64s.h>
18 #include <asm/ptrace.h>
19 #include <asm/cpuidle.h>
20 #include <asm/head-64.h>
21 #include <asm/feature-fixups.h>
22
23 /*
24  * There are a few constraints to be concerned with.
25  * - Real mode exceptions code/data must be located at their physical location.
26  * - Virtual mode exceptions must be mapped at their 0xc000... location.
27  * - Fixed location code must not call directly beyond the __end_interrupts
28  *   area when built with CONFIG_RELOCATABLE. LOAD_HANDLER / bctr sequence
29  *   must be used.
30  * - LOAD_HANDLER targets must be within first 64K of physical 0 /
31  *   virtual 0xc00...
32  * - Conditional branch targets must be within +/-32K of caller.
33  *
34  * "Virtual exceptions" run with relocation on (MSR_IR=1, MSR_DR=1), and
35  * therefore don't have to run in physically located code or rfid to
36  * virtual mode kernel code. However on relocatable kernels they do have
37  * to branch to KERNELBASE offset because the rest of the kernel (outside
38  * the exception vectors) may be located elsewhere.
39  *
40  * Virtual exceptions correspond with physical, except their entry points
41  * are offset by 0xc000000000000000 and also tend to get an added 0x4000
42  * offset applied. Virtual exceptions are enabled with the Alternate
43  * Interrupt Location (AIL) bit set in the LPCR. However this does not
44  * guarantee they will be delivered virtually. Some conditions (see the ISA)
45  * cause exceptions to be delivered in real mode.
46  *
47  * It's impossible to receive interrupts below 0x300 via AIL.
48  *
49  * KVM: None of the virtual exceptions are from the guest. Anything that
50  * escalated to HV=1 from HV=0 is delivered via real mode handlers.
51  *
52  *
53  * We layout physical memory as follows:
54  * 0x0000 - 0x00ff : Secondary processor spin code
55  * 0x0100 - 0x18ff : Real mode pSeries interrupt vectors
56  * 0x1900 - 0x3fff : Real mode trampolines
57  * 0x4000 - 0x58ff : Relon (IR=1,DR=1) mode pSeries interrupt vectors
58  * 0x5900 - 0x6fff : Relon mode trampolines
59  * 0x7000 - 0x7fff : FWNMI data area
60  * 0x8000 -   .... : Common interrupt handlers, remaining early
61  *                   setup code, rest of kernel.
62  *
63  * We could reclaim 0x4000-0x42ff for real mode trampolines if the space
64  * is necessary. Until then it's more consistent to explicitly put VIRT_NONE
65  * vectors there.
66  */
67 OPEN_FIXED_SECTION(real_vectors,        0x0100, 0x1900)
68 OPEN_FIXED_SECTION(real_trampolines,    0x1900, 0x4000)
69 OPEN_FIXED_SECTION(virt_vectors,        0x4000, 0x5900)
70 OPEN_FIXED_SECTION(virt_trampolines,    0x5900, 0x7000)
71 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
72 /*
73  * Data area reserved for FWNMI option.
74  * This address (0x7000) is fixed by the RPA.
75  * pseries and powernv need to keep the whole page from
76  * 0x7000 to 0x8000 free for use by the firmware
77  */
78 ZERO_FIXED_SECTION(fwnmi_page,          0x7000, 0x8000)
79 OPEN_TEXT_SECTION(0x8000)
80 #else
81 OPEN_TEXT_SECTION(0x7000)
82 #endif
83
84 USE_FIXED_SECTION(real_vectors)
85
86 /*
87  * This is the start of the interrupt handlers for pSeries
88  * This code runs with relocation off.
89  * Code from here to __end_interrupts gets copied down to real
90  * address 0x100 when we are running a relocatable kernel.
91  * Therefore any relative branches in this section must only
92  * branch to labels in this section.
93  */
94         .globl __start_interrupts
95 __start_interrupts:
96
97 /* No virt vectors corresponding with 0x0..0x100 */
98 EXC_VIRT_NONE(0x4000, 0x100)
99
100
101 #ifdef CONFIG_PPC_P7_NAP
102         /*
103          * If running native on arch 2.06 or later, check if we are waking up
104          * from nap/sleep/winkle, and branch to idle handler. This tests SRR1
105          * bits 46:47. A non-0 value indicates that we are coming from a power
106          * saving state. The idle wakeup handler initially runs in real mode,
107          * but we branch to the 0xc000... address so we can turn on relocation
108          * with mtmsr.
109          */
110 #define IDLETEST(n)                                                     \
111         BEGIN_FTR_SECTION ;                                             \
112         mfspr   r10,SPRN_SRR1 ;                                         \
113         rlwinm. r10,r10,47-31,30,31 ;                                   \
114         beq-    1f ;                                                    \
115         cmpwi   cr3,r10,2 ;                                             \
116         BRANCH_TO_C000(r10, system_reset_idle_common) ;                 \
117 1:                                                                      \
118         KVMTEST_PR(n) ;                                                 \
119         END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
120 #else
121 #define IDLETEST NOTEST
122 #endif
123
124 EXC_REAL_BEGIN(system_reset, 0x100, 0x100)
125         SET_SCRATCH0(r13)
126         /*
127          * MSR_RI is not enabled, because PACA_EXNMI and nmi stack is
128          * being used, so a nested NMI exception would corrupt it.
129          */
130         EXCEPTION_PROLOG_NORI(PACA_EXNMI, system_reset_common, EXC_STD,
131                               IDLETEST, 0x100)
132
133 EXC_REAL_END(system_reset, 0x100, 0x100)
134 EXC_VIRT_NONE(0x4100, 0x100)
135 TRAMP_KVM(PACA_EXNMI, 0x100)
136
137 #ifdef CONFIG_PPC_P7_NAP
138 EXC_COMMON_BEGIN(system_reset_idle_common)
139         mfspr   r12,SPRN_SRR1
140         b       pnv_powersave_wakeup
141 #endif
142
143 /*
144  * Set IRQS_ALL_DISABLED unconditionally so arch_irqs_disabled does
145  * the right thing. We do not want to reconcile because that goes
146  * through irq tracing which we don't want in NMI.
147  *
148  * Save PACAIRQHAPPENED because some code will do a hard disable
149  * (e.g., xmon). So we want to restore this back to where it was
150  * when we return. DAR is unused in the stack, so save it there.
151  */
152 #define ADD_RECONCILE_NMI                                               \
153         li      r10,IRQS_ALL_DISABLED;                                  \
154         stb     r10,PACAIRQSOFTMASK(r13);                               \
155         lbz     r10,PACAIRQHAPPENED(r13);                               \
156         std     r10,_DAR(r1)
157
158 EXC_COMMON_BEGIN(system_reset_common)
159         /*
160          * Increment paca->in_nmi then enable MSR_RI. SLB or MCE will be able
161          * to recover, but nested NMI will notice in_nmi and not recover
162          * because of the use of the NMI stack. in_nmi reentrancy is tested in
163          * system_reset_exception.
164          */
165         lhz     r10,PACA_IN_NMI(r13)
166         addi    r10,r10,1
167         sth     r10,PACA_IN_NMI(r13)
168         li      r10,MSR_RI
169         mtmsrd  r10,1
170
171         mr      r10,r1
172         ld      r1,PACA_NMI_EMERG_SP(r13)
173         subi    r1,r1,INT_FRAME_SIZE
174         EXCEPTION_COMMON_NORET_STACK(PACA_EXNMI, 0x100,
175                         system_reset, system_reset_exception,
176                         ADD_NVGPRS;ADD_RECONCILE_NMI)
177
178         /* This (and MCE) can be simplified with mtmsrd L=1 */
179         /* Clear MSR_RI before setting SRR0 and SRR1. */
180         li      r0,MSR_RI
181         mfmsr   r9
182         andc    r9,r9,r0
183         mtmsrd  r9,1
184
185         /*
186          * MSR_RI is clear, now we can decrement paca->in_nmi.
187          */
188         lhz     r10,PACA_IN_NMI(r13)
189         subi    r10,r10,1
190         sth     r10,PACA_IN_NMI(r13)
191
192         /*
193          * Restore soft mask settings.
194          */
195         ld      r10,_DAR(r1)
196         stb     r10,PACAIRQHAPPENED(r13)
197         ld      r10,SOFTE(r1)
198         stb     r10,PACAIRQSOFTMASK(r13)
199
200         /*
201          * Keep below code in synch with MACHINE_CHECK_HANDLER_WINDUP.
202          * Should share common bits...
203          */
204
205         /* Move original SRR0 and SRR1 into the respective regs */
206         ld      r9,_MSR(r1)
207         mtspr   SPRN_SRR1,r9
208         ld      r3,_NIP(r1)
209         mtspr   SPRN_SRR0,r3
210         ld      r9,_CTR(r1)
211         mtctr   r9
212         ld      r9,_XER(r1)
213         mtxer   r9
214         ld      r9,_LINK(r1)
215         mtlr    r9
216         REST_GPR(0, r1)
217         REST_8GPRS(2, r1)
218         REST_GPR(10, r1)
219         ld      r11,_CCR(r1)
220         mtcr    r11
221         REST_GPR(11, r1)
222         REST_2GPRS(12, r1)
223         /* restore original r1. */
224         ld      r1,GPR1(r1)
225         RFI_TO_USER_OR_KERNEL
226
227 #ifdef CONFIG_PPC_PSERIES
228 /*
229  * Vectors for the FWNMI option.  Share common code.
230  */
231 TRAMP_REAL_BEGIN(system_reset_fwnmi)
232         SET_SCRATCH0(r13)               /* save r13 */
233         /* See comment at system_reset exception */
234         EXCEPTION_PROLOG_NORI(PACA_EXNMI, system_reset_common, EXC_STD,
235                               NOTEST, 0x100)
236 #endif /* CONFIG_PPC_PSERIES */
237
238
239 EXC_REAL_BEGIN(machine_check, 0x200, 0x100)
240         /* This is moved out of line as it can be patched by FW, but
241          * some code path might still want to branch into the original
242          * vector
243          */
244         SET_SCRATCH0(r13)               /* save r13 */
245         EXCEPTION_PROLOG_0(PACA_EXMC)
246 BEGIN_FTR_SECTION
247         b       machine_check_powernv_early
248 FTR_SECTION_ELSE
249         b       machine_check_pSeries_0
250 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
251 EXC_REAL_END(machine_check, 0x200, 0x100)
252 EXC_VIRT_NONE(0x4200, 0x100)
253 TRAMP_REAL_BEGIN(machine_check_powernv_early)
254 BEGIN_FTR_SECTION
255         EXCEPTION_PROLOG_1(PACA_EXMC, NOTEST, 0x200)
256         /*
257          * Register contents:
258          * R13          = PACA
259          * R9           = CR
260          * Original R9 to R13 is saved on PACA_EXMC
261          *
262          * Switch to mc_emergency stack and handle re-entrancy (we limit
263          * the nested MCE upto level 4 to avoid stack overflow).
264          * Save MCE registers srr1, srr0, dar and dsisr and then set ME=1
265          *
266          * We use paca->in_mce to check whether this is the first entry or
267          * nested machine check. We increment paca->in_mce to track nested
268          * machine checks.
269          *
270          * If this is the first entry then set stack pointer to
271          * paca->mc_emergency_sp, otherwise r1 is already pointing to
272          * stack frame on mc_emergency stack.
273          *
274          * NOTE: We are here with MSR_ME=0 (off), which means we risk a
275          * checkstop if we get another machine check exception before we do
276          * rfid with MSR_ME=1.
277          *
278          * This interrupt can wake directly from idle. If that is the case,
279          * the machine check is handled then the idle wakeup code is called
280          * to restore state.
281          */
282         mr      r11,r1                  /* Save r1 */
283         lhz     r10,PACA_IN_MCE(r13)
284         cmpwi   r10,0                   /* Are we in nested machine check */
285         bne     0f                      /* Yes, we are. */
286         /* First machine check entry */
287         ld      r1,PACAMCEMERGSP(r13)   /* Use MC emergency stack */
288 0:      subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame */
289         addi    r10,r10,1               /* increment paca->in_mce */
290         sth     r10,PACA_IN_MCE(r13)
291         /* Limit nested MCE to level 4 to avoid stack overflow */
292         cmpwi   r10,MAX_MCE_DEPTH
293         bgt     2f                      /* Check if we hit limit of 4 */
294         std     r11,GPR1(r1)            /* Save r1 on the stack. */
295         std     r11,0(r1)               /* make stack chain pointer */
296         mfspr   r11,SPRN_SRR0           /* Save SRR0 */
297         std     r11,_NIP(r1)
298         mfspr   r11,SPRN_SRR1           /* Save SRR1 */
299         std     r11,_MSR(r1)
300         mfspr   r11,SPRN_DAR            /* Save DAR */
301         std     r11,_DAR(r1)
302         mfspr   r11,SPRN_DSISR          /* Save DSISR */
303         std     r11,_DSISR(r1)
304         std     r9,_CCR(r1)             /* Save CR in stackframe */
305         /* Save r9 through r13 from EXMC save area to stack frame. */
306         EXCEPTION_PROLOG_COMMON_2(PACA_EXMC)
307         mfmsr   r11                     /* get MSR value */
308         ori     r11,r11,MSR_ME          /* turn on ME bit */
309         ori     r11,r11,MSR_RI          /* turn on RI bit */
310         LOAD_HANDLER(r12, machine_check_handle_early)
311 1:      mtspr   SPRN_SRR0,r12
312         mtspr   SPRN_SRR1,r11
313         RFI_TO_KERNEL
314         b       .       /* prevent speculative execution */
315 2:
316         /* Stack overflow. Stay on emergency stack and panic.
317          * Keep the ME bit off while panic-ing, so that if we hit
318          * another machine check we checkstop.
319          */
320         addi    r1,r1,INT_FRAME_SIZE    /* go back to previous stack frame */
321         ld      r11,PACAKMSR(r13)
322         LOAD_HANDLER(r12, unrecover_mce)
323         li      r10,MSR_ME
324         andc    r11,r11,r10             /* Turn off MSR_ME */
325         b       1b
326         b       .       /* prevent speculative execution */
327 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
328
329 TRAMP_REAL_BEGIN(machine_check_pSeries)
330         .globl machine_check_fwnmi
331 machine_check_fwnmi:
332         SET_SCRATCH0(r13)               /* save r13 */
333         EXCEPTION_PROLOG_0(PACA_EXMC)
334 machine_check_pSeries_0:
335         EXCEPTION_PROLOG_1(PACA_EXMC, KVMTEST_PR, 0x200)
336         /*
337          * MSR_RI is not enabled, because PACA_EXMC is being used, so a
338          * nested machine check corrupts it. machine_check_common enables
339          * MSR_RI.
340          */
341         EXCEPTION_PROLOG_2_NORI(machine_check_common, EXC_STD)
342
343 TRAMP_KVM_SKIP(PACA_EXMC, 0x200)
344
345 EXC_COMMON_BEGIN(machine_check_common)
346         /*
347          * Machine check is different because we use a different
348          * save area: PACA_EXMC instead of PACA_EXGEN.
349          */
350         mfspr   r10,SPRN_DAR
351         std     r10,PACA_EXMC+EX_DAR(r13)
352         mfspr   r10,SPRN_DSISR
353         stw     r10,PACA_EXMC+EX_DSISR(r13)
354         EXCEPTION_PROLOG_COMMON(0x200, PACA_EXMC)
355         FINISH_NAP
356         RECONCILE_IRQ_STATE(r10, r11)
357         ld      r3,PACA_EXMC+EX_DAR(r13)
358         lwz     r4,PACA_EXMC+EX_DSISR(r13)
359         /* Enable MSR_RI when finished with PACA_EXMC */
360         li      r10,MSR_RI
361         mtmsrd  r10,1
362         std     r3,_DAR(r1)
363         std     r4,_DSISR(r1)
364         bl      save_nvgprs
365         addi    r3,r1,STACK_FRAME_OVERHEAD
366         bl      machine_check_exception
367         b       ret_from_except
368
369 #define MACHINE_CHECK_HANDLER_WINDUP                    \
370         /* Clear MSR_RI before setting SRR0 and SRR1. */\
371         li      r0,MSR_RI;                              \
372         mfmsr   r9;             /* get MSR value */     \
373         andc    r9,r9,r0;                               \
374         mtmsrd  r9,1;           /* Clear MSR_RI */      \
375         /* Move original SRR0 and SRR1 into the respective regs */      \
376         ld      r9,_MSR(r1);                            \
377         mtspr   SPRN_SRR1,r9;                           \
378         ld      r3,_NIP(r1);                            \
379         mtspr   SPRN_SRR0,r3;                           \
380         ld      r9,_CTR(r1);                            \
381         mtctr   r9;                                     \
382         ld      r9,_XER(r1);                            \
383         mtxer   r9;                                     \
384         ld      r9,_LINK(r1);                           \
385         mtlr    r9;                                     \
386         REST_GPR(0, r1);                                \
387         REST_8GPRS(2, r1);                              \
388         REST_GPR(10, r1);                               \
389         ld      r11,_CCR(r1);                           \
390         mtcr    r11;                                    \
391         /* Decrement paca->in_mce. */                   \
392         lhz     r12,PACA_IN_MCE(r13);                   \
393         subi    r12,r12,1;                              \
394         sth     r12,PACA_IN_MCE(r13);                   \
395         REST_GPR(11, r1);                               \
396         REST_2GPRS(12, r1);                             \
397         /* restore original r1. */                      \
398         ld      r1,GPR1(r1)
399
400 #ifdef CONFIG_PPC_P7_NAP
401 /*
402  * This is an idle wakeup. Low level machine check has already been
403  * done. Queue the event then call the idle code to do the wake up.
404  */
405 EXC_COMMON_BEGIN(machine_check_idle_common)
406         bl      machine_check_queue_event
407
408         /*
409          * We have not used any non-volatile GPRs here, and as a rule
410          * most exception code including machine check does not.
411          * Therefore PACA_NAPSTATELOST does not need to be set. Idle
412          * wakeup will restore volatile registers.
413          *
414          * Load the original SRR1 into r3 for pnv_powersave_wakeup_mce.
415          *
416          * Then decrement MCE nesting after finishing with the stack.
417          */
418         ld      r3,_MSR(r1)
419
420         lhz     r11,PACA_IN_MCE(r13)
421         subi    r11,r11,1
422         sth     r11,PACA_IN_MCE(r13)
423
424         /* Turn off the RI bit because SRR1 is used by idle wakeup code. */
425         /* Recoverability could be improved by reducing the use of SRR1. */
426         li      r11,0
427         mtmsrd  r11,1
428
429         b       pnv_powersave_wakeup_mce
430 #endif
431         /*
432          * Handle machine check early in real mode. We come here with
433          * ME=1, MMU (IR=0 and DR=0) off and using MC emergency stack.
434          */
435 EXC_COMMON_BEGIN(machine_check_handle_early)
436         std     r0,GPR0(r1)     /* Save r0 */
437         EXCEPTION_PROLOG_COMMON_3(0x200)
438         bl      save_nvgprs
439         addi    r3,r1,STACK_FRAME_OVERHEAD
440         bl      machine_check_early
441         std     r3,RESULT(r1)   /* Save result */
442         ld      r12,_MSR(r1)
443
444 #ifdef  CONFIG_PPC_P7_NAP
445         /*
446          * Check if thread was in power saving mode. We come here when any
447          * of the following is true:
448          * a. thread wasn't in power saving mode
449          * b. thread was in power saving mode with no state loss,
450          *    supervisor state loss or hypervisor state loss.
451          *
452          * Go back to nap/sleep/winkle mode again if (b) is true.
453          */
454         BEGIN_FTR_SECTION
455         rlwinm. r11,r12,47-31,30,31
456         bne     machine_check_idle_common
457         END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
458 #endif
459
460         /*
461          * Check if we are coming from hypervisor userspace. If yes then we
462          * continue in host kernel in V mode to deliver the MC event.
463          */
464         rldicl. r11,r12,4,63            /* See if MC hit while in HV mode. */
465         beq     5f
466         andi.   r11,r12,MSR_PR          /* See if coming from user. */
467         bne     9f                      /* continue in V mode if we are. */
468
469 5:
470 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
471         /*
472          * We are coming from kernel context. Check if we are coming from
473          * guest. if yes, then we can continue. We will fall through
474          * do_kvm_200->kvmppc_interrupt to deliver the MC event to guest.
475          */
476         lbz     r11,HSTATE_IN_GUEST(r13)
477         cmpwi   r11,0                   /* Check if coming from guest */
478         bne     9f                      /* continue if we are. */
479 #endif
480         /*
481          * At this point we are not sure about what context we come from.
482          * Queue up the MCE event and return from the interrupt.
483          * But before that, check if this is an un-recoverable exception.
484          * If yes, then stay on emergency stack and panic.
485          */
486         andi.   r11,r12,MSR_RI
487         bne     2f
488 1:      mfspr   r11,SPRN_SRR0
489         LOAD_HANDLER(r10,unrecover_mce)
490         mtspr   SPRN_SRR0,r10
491         ld      r10,PACAKMSR(r13)
492         /*
493          * We are going down. But there are chances that we might get hit by
494          * another MCE during panic path and we may run into unstable state
495          * with no way out. Hence, turn ME bit off while going down, so that
496          * when another MCE is hit during panic path, system will checkstop
497          * and hypervisor will get restarted cleanly by SP.
498          */
499         li      r3,MSR_ME
500         andc    r10,r10,r3              /* Turn off MSR_ME */
501         mtspr   SPRN_SRR1,r10
502         RFI_TO_KERNEL
503         b       .
504 2:
505         /*
506          * Check if we have successfully handled/recovered from error, if not
507          * then stay on emergency stack and panic.
508          */
509         ld      r3,RESULT(r1)   /* Load result */
510         cmpdi   r3,0            /* see if we handled MCE successfully */
511
512         beq     1b              /* if !handled then panic */
513         /*
514          * Return from MC interrupt.
515          * Queue up the MCE event so that we can log it later, while
516          * returning from kernel or opal call.
517          */
518         bl      machine_check_queue_event
519         MACHINE_CHECK_HANDLER_WINDUP
520         RFI_TO_USER_OR_KERNEL
521 9:
522         /* Deliver the machine check to host kernel in V mode. */
523 BEGIN_FTR_SECTION
524         ld      r10,ORIG_GPR3(r1)
525         mtspr   SPRN_CFAR,r10
526 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
527         MACHINE_CHECK_HANDLER_WINDUP
528         b       machine_check_pSeries
529
530 EXC_COMMON_BEGIN(unrecover_mce)
531         /* Invoke machine_check_exception to print MCE event and panic. */
532         addi    r3,r1,STACK_FRAME_OVERHEAD
533         bl      machine_check_exception
534         /*
535          * We will not reach here. Even if we did, there is no way out. Call
536          * unrecoverable_exception and die.
537          */
538 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
539         bl      unrecoverable_exception
540         b       1b
541
542
543 EXC_REAL_OOL(data_access, 0x300, 0x80)
544 EXC_VIRT(data_access, 0x4300, 0x80, 0x300)
545 TRAMP_KVM_SKIP(PACA_EXGEN, 0x300)
546
547 EXC_COMMON_BEGIN(data_access_common)
548         /*
549          * Here r13 points to the paca, r9 contains the saved CR,
550          * SRR0 and SRR1 are saved in r11 and r12,
551          * r9 - r13 are saved in paca->exgen.
552          */
553         mfspr   r10,SPRN_DAR
554         std     r10,PACA_EXGEN+EX_DAR(r13)
555         mfspr   r10,SPRN_DSISR
556         stw     r10,PACA_EXGEN+EX_DSISR(r13)
557         EXCEPTION_PROLOG_COMMON(0x300, PACA_EXGEN)
558         RECONCILE_IRQ_STATE(r10, r11)
559         ld      r12,_MSR(r1)
560         ld      r3,PACA_EXGEN+EX_DAR(r13)
561         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
562         li      r5,0x300
563         std     r3,_DAR(r1)
564         std     r4,_DSISR(r1)
565 BEGIN_MMU_FTR_SECTION
566         b       do_hash_page            /* Try to handle as hpte fault */
567 MMU_FTR_SECTION_ELSE
568         b       handle_page_fault
569 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
570
571
572 EXC_REAL_BEGIN(data_access_slb, 0x380, 0x80)
573         SET_SCRATCH0(r13)
574         EXCEPTION_PROLOG_0(PACA_EXSLB)
575         b tramp_data_access_slb
576 EXC_REAL_END(data_access_slb, 0x380, 0x80)
577
578 TRAMP_REAL_BEGIN(tramp_data_access_slb)
579         EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST_PR, 0x380)
580         mr      r12,r3  /* save r3 */
581         mfspr   r3,SPRN_DAR
582         mfspr   r11,SPRN_SRR1
583         crset   4*cr6+eq
584         BRANCH_TO_COMMON(r10, slb_miss_common)
585
586 EXC_VIRT_BEGIN(data_access_slb, 0x4380, 0x80)
587         SET_SCRATCH0(r13)
588         EXCEPTION_PROLOG_0(PACA_EXSLB)
589         EXCEPTION_PROLOG_1(PACA_EXSLB, NOTEST, 0x380)
590         mr      r12,r3  /* save r3 */
591         mfspr   r3,SPRN_DAR
592         mfspr   r11,SPRN_SRR1
593         crset   4*cr6+eq
594         BRANCH_TO_COMMON(r10, slb_miss_common)
595 EXC_VIRT_END(data_access_slb, 0x4380, 0x80)
596 TRAMP_KVM_SKIP(PACA_EXSLB, 0x380)
597
598
599 EXC_REAL_OOL(instruction_access, 0x400, 0x80)
600 EXC_VIRT(instruction_access, 0x4400, 0x80, 0x400)
601 TRAMP_KVM(PACA_EXGEN, 0x400)
602
603 EXC_COMMON_BEGIN(instruction_access_common)
604         EXCEPTION_PROLOG_COMMON(0x400, PACA_EXGEN)
605         RECONCILE_IRQ_STATE(r10, r11)
606         ld      r12,_MSR(r1)
607         ld      r3,_NIP(r1)
608         andis.  r4,r12,DSISR_SRR1_MATCH_64S@h
609         li      r5,0x400
610         std     r3,_DAR(r1)
611         std     r4,_DSISR(r1)
612 BEGIN_MMU_FTR_SECTION
613         b       do_hash_page            /* Try to handle as hpte fault */
614 MMU_FTR_SECTION_ELSE
615         b       handle_page_fault
616 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
617
618
619 EXC_REAL_BEGIN(instruction_access_slb, 0x480, 0x80)
620         SET_SCRATCH0(r13)
621         EXCEPTION_PROLOG_0(PACA_EXSLB)
622         b tramp_instruction_access_slb
623 EXC_REAL_END(instruction_access_slb, 0x480, 0x80)
624
625 TRAMP_REAL_BEGIN(tramp_instruction_access_slb)
626         EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST_PR, 0x480)
627         mr      r12,r3  /* save r3 */
628         mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
629         mfspr   r11,SPRN_SRR1
630         crclr   4*cr6+eq
631         BRANCH_TO_COMMON(r10, slb_miss_common)
632
633 EXC_VIRT_BEGIN(instruction_access_slb, 0x4480, 0x80)
634         SET_SCRATCH0(r13)
635         EXCEPTION_PROLOG_0(PACA_EXSLB)
636         EXCEPTION_PROLOG_1(PACA_EXSLB, NOTEST, 0x480)
637         mr      r12,r3  /* save r3 */
638         mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
639         mfspr   r11,SPRN_SRR1
640         crclr   4*cr6+eq
641         BRANCH_TO_COMMON(r10, slb_miss_common)
642 EXC_VIRT_END(instruction_access_slb, 0x4480, 0x80)
643 TRAMP_KVM(PACA_EXSLB, 0x480)
644
645
646 /*
647  * This handler is used by the 0x380 and 0x480 SLB miss interrupts, as well as
648  * the virtual mode 0x4380 and 0x4480 interrupts if AIL is enabled.
649  */
650 EXC_COMMON_BEGIN(slb_miss_common)
651         /*
652          * r13 points to the PACA, r9 contains the saved CR,
653          * r12 contains the saved r3,
654          * r11 contain the saved SRR1, SRR0 is still ready for return
655          * r3 has the faulting address
656          * r9 - r13 are saved in paca->exslb.
657          * cr6.eq is set for a D-SLB miss, clear for a I-SLB miss
658          * We assume we aren't going to take any exceptions during this
659          * procedure.
660          */
661         mflr    r10
662         stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
663         std     r10,PACA_EXSLB+EX_LR(r13)       /* save LR */
664
665         andi.   r9,r11,MSR_PR   // Check for exception from userspace
666         cmpdi   cr4,r9,MSR_PR   // And save the result in CR4 for later
667
668         /*
669          * Test MSR_RI before calling slb_allocate_realmode, because the
670          * MSR in r11 gets clobbered. However we still want to allocate
671          * SLB in case MSR_RI=0, to minimise the risk of getting stuck in
672          * recursive SLB faults. So use cr5 for this, which is preserved.
673          */
674         andi.   r11,r11,MSR_RI  /* check for unrecoverable exception */
675         cmpdi   cr5,r11,MSR_RI
676
677         crset   4*cr0+eq
678 #ifdef CONFIG_PPC_BOOK3S_64
679 BEGIN_MMU_FTR_SECTION
680         bl      slb_allocate
681 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)
682 #endif
683
684         ld      r10,PACA_EXSLB+EX_LR(r13)
685         lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
686         mtlr    r10
687
688         /*
689          * Large address, check whether we have to allocate new contexts.
690          */
691         beq-    8f
692
693         bne-    cr5,2f          /* if unrecoverable exception, oops */
694
695         /* All done -- return from exception. */
696
697         bne     cr4,1f          /* returning to kernel */
698
699         mtcrf   0x80,r9
700         mtcrf   0x08,r9         /* MSR[PR] indication is in cr4 */
701         mtcrf   0x04,r9         /* MSR[RI] indication is in cr5 */
702         mtcrf   0x02,r9         /* I/D indication is in cr6 */
703         mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
704
705         RESTORE_CTR(r9, PACA_EXSLB)
706         RESTORE_PPR_PACA(PACA_EXSLB, r9)
707         mr      r3,r12
708         ld      r9,PACA_EXSLB+EX_R9(r13)
709         ld      r10,PACA_EXSLB+EX_R10(r13)
710         ld      r11,PACA_EXSLB+EX_R11(r13)
711         ld      r12,PACA_EXSLB+EX_R12(r13)
712         ld      r13,PACA_EXSLB+EX_R13(r13)
713         RFI_TO_USER
714         b       .       /* prevent speculative execution */
715 1:
716         mtcrf   0x80,r9
717         mtcrf   0x08,r9         /* MSR[PR] indication is in cr4 */
718         mtcrf   0x04,r9         /* MSR[RI] indication is in cr5 */
719         mtcrf   0x02,r9         /* I/D indication is in cr6 */
720         mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
721
722         RESTORE_CTR(r9, PACA_EXSLB)
723         RESTORE_PPR_PACA(PACA_EXSLB, r9)
724         mr      r3,r12
725         ld      r9,PACA_EXSLB+EX_R9(r13)
726         ld      r10,PACA_EXSLB+EX_R10(r13)
727         ld      r11,PACA_EXSLB+EX_R11(r13)
728         ld      r12,PACA_EXSLB+EX_R12(r13)
729         ld      r13,PACA_EXSLB+EX_R13(r13)
730         RFI_TO_KERNEL
731         b       .       /* prevent speculative execution */
732
733
734 2:      std     r3,PACA_EXSLB+EX_DAR(r13)
735         mr      r3,r12
736         mfspr   r11,SPRN_SRR0
737         mfspr   r12,SPRN_SRR1
738         LOAD_HANDLER(r10,unrecov_slb)
739         mtspr   SPRN_SRR0,r10
740         ld      r10,PACAKMSR(r13)
741         mtspr   SPRN_SRR1,r10
742         RFI_TO_KERNEL
743         b       .
744
745 8:      std     r3,PACA_EXSLB+EX_DAR(r13)
746         mr      r3,r12
747         mfspr   r11,SPRN_SRR0
748         mfspr   r12,SPRN_SRR1
749         LOAD_HANDLER(r10, large_addr_slb)
750         mtspr   SPRN_SRR0,r10
751         ld      r10,PACAKMSR(r13)
752         mtspr   SPRN_SRR1,r10
753         RFI_TO_KERNEL
754         b       .
755
756 EXC_COMMON_BEGIN(unrecov_slb)
757         EXCEPTION_PROLOG_COMMON(0x4100, PACA_EXSLB)
758         RECONCILE_IRQ_STATE(r10, r11)
759         bl      save_nvgprs
760 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
761         bl      unrecoverable_exception
762         b       1b
763
764 EXC_COMMON_BEGIN(large_addr_slb)
765         EXCEPTION_PROLOG_COMMON(0x380, PACA_EXSLB)
766         RECONCILE_IRQ_STATE(r10, r11)
767         ld      r3, PACA_EXSLB+EX_DAR(r13)
768         std     r3, _DAR(r1)
769         beq     cr6, 2f
770         li      r10, 0x481              /* fix trap number for I-SLB miss */
771         std     r10, _TRAP(r1)
772 2:      bl      save_nvgprs
773         addi    r3, r1, STACK_FRAME_OVERHEAD
774         bl      slb_miss_large_addr
775         b       ret_from_except
776
777 EXC_REAL_BEGIN(hardware_interrupt, 0x500, 0x100)
778         .globl hardware_interrupt_hv;
779 hardware_interrupt_hv:
780         BEGIN_FTR_SECTION
781                 MASKABLE_EXCEPTION_HV(0x500, hardware_interrupt_common, IRQS_DISABLED)
782         FTR_SECTION_ELSE
783                 MASKABLE_EXCEPTION(0x500, hardware_interrupt_common, IRQS_DISABLED)
784         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
785 EXC_REAL_END(hardware_interrupt, 0x500, 0x100)
786
787 EXC_VIRT_BEGIN(hardware_interrupt, 0x4500, 0x100)
788         .globl hardware_interrupt_relon_hv;
789 hardware_interrupt_relon_hv:
790         BEGIN_FTR_SECTION
791                 MASKABLE_RELON_EXCEPTION_HV(0x500, hardware_interrupt_common,
792                                             IRQS_DISABLED)
793         FTR_SECTION_ELSE
794                 __MASKABLE_RELON_EXCEPTION(0x500, hardware_interrupt_common,
795                                            EXC_STD, SOFTEN_TEST_PR, IRQS_DISABLED)
796         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
797 EXC_VIRT_END(hardware_interrupt, 0x4500, 0x100)
798
799 TRAMP_KVM(PACA_EXGEN, 0x500)
800 TRAMP_KVM_HV(PACA_EXGEN, 0x500)
801 EXC_COMMON_ASYNC(hardware_interrupt_common, 0x500, do_IRQ)
802
803
804 EXC_REAL(alignment, 0x600, 0x100)
805 EXC_VIRT(alignment, 0x4600, 0x100, 0x600)
806 TRAMP_KVM(PACA_EXGEN, 0x600)
807 EXC_COMMON_BEGIN(alignment_common)
808         mfspr   r10,SPRN_DAR
809         std     r10,PACA_EXGEN+EX_DAR(r13)
810         mfspr   r10,SPRN_DSISR
811         stw     r10,PACA_EXGEN+EX_DSISR(r13)
812         EXCEPTION_PROLOG_COMMON(0x600, PACA_EXGEN)
813         ld      r3,PACA_EXGEN+EX_DAR(r13)
814         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
815         std     r3,_DAR(r1)
816         std     r4,_DSISR(r1)
817         bl      save_nvgprs
818         RECONCILE_IRQ_STATE(r10, r11)
819         addi    r3,r1,STACK_FRAME_OVERHEAD
820         bl      alignment_exception
821         b       ret_from_except
822
823
824 EXC_REAL(program_check, 0x700, 0x100)
825 EXC_VIRT(program_check, 0x4700, 0x100, 0x700)
826 TRAMP_KVM(PACA_EXGEN, 0x700)
827 EXC_COMMON_BEGIN(program_check_common)
828         /*
829          * It's possible to receive a TM Bad Thing type program check with
830          * userspace register values (in particular r1), but with SRR1 reporting
831          * that we came from the kernel. Normally that would confuse the bad
832          * stack logic, and we would report a bad kernel stack pointer. Instead
833          * we switch to the emergency stack if we're taking a TM Bad Thing from
834          * the kernel.
835          */
836         li      r10,MSR_PR              /* Build a mask of MSR_PR ..    */
837         oris    r10,r10,0x200000@h      /* .. and SRR1_PROGTM           */
838         and     r10,r10,r12             /* Mask SRR1 with that.         */
839         srdi    r10,r10,8               /* Shift it so we can compare   */
840         cmpldi  r10,(0x200000 >> 8)     /* .. with an immediate.        */
841         bne 1f                          /* If != go to normal path.     */
842
843         /* SRR1 had PR=0 and SRR1_PROGTM=1, so use the emergency stack  */
844         andi.   r10,r12,MSR_PR;         /* Set CR0 correctly for label  */
845                                         /* 3 in EXCEPTION_PROLOG_COMMON */
846         mr      r10,r1                  /* Save r1                      */
847         ld      r1,PACAEMERGSP(r13)     /* Use emergency stack          */
848         subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
849         b 3f                            /* Jump into the macro !!       */
850 1:      EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
851         bl      save_nvgprs
852         RECONCILE_IRQ_STATE(r10, r11)
853         addi    r3,r1,STACK_FRAME_OVERHEAD
854         bl      program_check_exception
855         b       ret_from_except
856
857
858 EXC_REAL(fp_unavailable, 0x800, 0x100)
859 EXC_VIRT(fp_unavailable, 0x4800, 0x100, 0x800)
860 TRAMP_KVM(PACA_EXGEN, 0x800)
861 EXC_COMMON_BEGIN(fp_unavailable_common)
862         EXCEPTION_PROLOG_COMMON(0x800, PACA_EXGEN)
863         bne     1f                      /* if from user, just load it up */
864         bl      save_nvgprs
865         RECONCILE_IRQ_STATE(r10, r11)
866         addi    r3,r1,STACK_FRAME_OVERHEAD
867         bl      kernel_fp_unavailable_exception
868         BUG_OPCODE
869 1:
870 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
871 BEGIN_FTR_SECTION
872         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
873          * transaction), go do TM stuff
874          */
875         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
876         bne-    2f
877 END_FTR_SECTION_IFSET(CPU_FTR_TM)
878 #endif
879         bl      load_up_fpu
880         b       fast_exception_return
881 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
882 2:      /* User process was in a transaction */
883         bl      save_nvgprs
884         RECONCILE_IRQ_STATE(r10, r11)
885         addi    r3,r1,STACK_FRAME_OVERHEAD
886         bl      fp_unavailable_tm
887         b       ret_from_except
888 #endif
889
890
891 EXC_REAL_OOL_MASKABLE(decrementer, 0x900, 0x80, IRQS_DISABLED)
892 EXC_VIRT_OOL_MASKABLE(decrementer, 0x4900, 0x80, 0x900, IRQS_DISABLED)
893 TRAMP_KVM(PACA_EXGEN, 0x900)
894 EXC_COMMON_ASYNC(decrementer_common, 0x900, timer_interrupt)
895
896
897 EXC_REAL_OOL_HV(hdecrementer, 0x980, 0x80)
898 EXC_VIRT_OOL_HV(hdecrementer, 0x4980, 0x80, 0x980)
899 TRAMP_KVM_HV(PACA_EXGEN, 0x980)
900 EXC_COMMON(hdecrementer_common, 0x980, hdec_interrupt)
901
902
903 EXC_REAL_MASKABLE(doorbell_super, 0xa00, 0x100, IRQS_DISABLED)
904 EXC_VIRT_MASKABLE(doorbell_super, 0x4a00, 0x100, 0xa00, IRQS_DISABLED)
905 TRAMP_KVM(PACA_EXGEN, 0xa00)
906 #ifdef CONFIG_PPC_DOORBELL
907 EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, doorbell_exception)
908 #else
909 EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, unknown_exception)
910 #endif
911
912
913 EXC_REAL(trap_0b, 0xb00, 0x100)
914 EXC_VIRT(trap_0b, 0x4b00, 0x100, 0xb00)
915 TRAMP_KVM(PACA_EXGEN, 0xb00)
916 EXC_COMMON(trap_0b_common, 0xb00, unknown_exception)
917
918 /*
919  * system call / hypercall (0xc00, 0x4c00)
920  *
921  * The system call exception is invoked with "sc 0" and does not alter HV bit.
922  * There is support for kernel code to invoke system calls but there are no
923  * in-tree users.
924  *
925  * The hypercall is invoked with "sc 1" and sets HV=1.
926  *
927  * In HPT, sc 1 always goes to 0xc00 real mode. In RADIX, sc 1 can go to
928  * 0x4c00 virtual mode.
929  *
930  * Call convention:
931  *
932  * syscall register convention is in Documentation/powerpc/syscall64-abi.txt
933  *
934  * For hypercalls, the register convention is as follows:
935  * r0 volatile
936  * r1-2 nonvolatile
937  * r3 volatile parameter and return value for status
938  * r4-r10 volatile input and output value
939  * r11 volatile hypercall number and output value
940  * r12 volatile input and output value
941  * r13-r31 nonvolatile
942  * LR nonvolatile
943  * CTR volatile
944  * XER volatile
945  * CR0-1 CR5-7 volatile
946  * CR2-4 nonvolatile
947  * Other registers nonvolatile
948  *
949  * The intersection of volatile registers that don't contain possible
950  * inputs is: cr0, xer, ctr. We may use these as scratch regs upon entry
951  * without saving, though xer is not a good idea to use, as hardware may
952  * interpret some bits so it may be costly to change them.
953  */
954 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
955         /*
956          * There is a little bit of juggling to get syscall and hcall
957          * working well. Save r13 in ctr to avoid using SPRG scratch
958          * register.
959          *
960          * Userspace syscalls have already saved the PPR, hcalls must save
961          * it before setting HMT_MEDIUM.
962          */
963 #define SYSCALL_KVMTEST                                                 \
964         mtctr   r13;                                                    \
965         GET_PACA(r13);                                                  \
966         std     r10,PACA_EXGEN+EX_R10(r13);                             \
967         INTERRUPT_TO_KERNEL;                                            \
968         KVMTEST_PR(0xc00); /* uses r10, branch to do_kvm_0xc00_system_call */ \
969         HMT_MEDIUM;                                                     \
970         mfctr   r9;
971
972 #else
973 #define SYSCALL_KVMTEST                                                 \
974         HMT_MEDIUM;                                                     \
975         mr      r9,r13;                                                 \
976         GET_PACA(r13);                                                  \
977         INTERRUPT_TO_KERNEL;
978 #endif
979         
980 #define LOAD_SYSCALL_HANDLER(reg)                                       \
981         __LOAD_HANDLER(reg, system_call_common)
982
983 /*
984  * After SYSCALL_KVMTEST, we reach here with PACA in r13, r13 in r9,
985  * and HMT_MEDIUM.
986  */
987 #define SYSCALL_REAL                                            \
988         mfspr   r11,SPRN_SRR0 ;                                 \
989         mfspr   r12,SPRN_SRR1 ;                                 \
990         LOAD_SYSCALL_HANDLER(r10) ;                             \
991         mtspr   SPRN_SRR0,r10 ;                                 \
992         ld      r10,PACAKMSR(r13) ;                             \
993         mtspr   SPRN_SRR1,r10 ;                                 \
994         RFI_TO_KERNEL ;                                         \
995         b       . ;     /* prevent speculative execution */
996
997 #ifdef CONFIG_PPC_FAST_ENDIAN_SWITCH
998 #define SYSCALL_FASTENDIAN_TEST                                 \
999 BEGIN_FTR_SECTION                                               \
1000         cmpdi   r0,0x1ebe ;                                     \
1001         beq-    1f ;                                            \
1002 END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE)                          \
1003
1004 #define SYSCALL_FASTENDIAN                                      \
1005         /* Fast LE/BE switch system call */                     \
1006 1:      mfspr   r12,SPRN_SRR1 ;                                 \
1007         xori    r12,r12,MSR_LE ;                                \
1008         mtspr   SPRN_SRR1,r12 ;                                 \
1009         mr      r13,r9 ;                                        \
1010         RFI_TO_USER ;   /* return to userspace */               \
1011         b       . ;     /* prevent speculative execution */
1012 #else
1013 #define SYSCALL_FASTENDIAN_TEST
1014 #define SYSCALL_FASTENDIAN
1015 #endif /* CONFIG_PPC_FAST_ENDIAN_SWITCH */
1016
1017 #if defined(CONFIG_RELOCATABLE)
1018         /*
1019          * We can't branch directly so we do it via the CTR which
1020          * is volatile across system calls.
1021          */
1022 #define SYSCALL_VIRT                                            \
1023         LOAD_SYSCALL_HANDLER(r10) ;                             \
1024         mtctr   r10 ;                                           \
1025         mfspr   r11,SPRN_SRR0 ;                                 \
1026         mfspr   r12,SPRN_SRR1 ;                                 \
1027         li      r10,MSR_RI ;                                    \
1028         mtmsrd  r10,1 ;                                         \
1029         bctr ;
1030 #else
1031         /* We can branch directly */
1032 #define SYSCALL_VIRT                                            \
1033         mfspr   r11,SPRN_SRR0 ;                                 \
1034         mfspr   r12,SPRN_SRR1 ;                                 \
1035         li      r10,MSR_RI ;                                    \
1036         mtmsrd  r10,1 ;                 /* Set RI (EE=0) */     \
1037         b       system_call_common ;
1038 #endif
1039
1040 EXC_REAL_BEGIN(system_call, 0xc00, 0x100)
1041         SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
1042         SYSCALL_FASTENDIAN_TEST
1043         SYSCALL_REAL
1044         SYSCALL_FASTENDIAN
1045 EXC_REAL_END(system_call, 0xc00, 0x100)
1046
1047 EXC_VIRT_BEGIN(system_call, 0x4c00, 0x100)
1048         SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
1049         SYSCALL_FASTENDIAN_TEST
1050         SYSCALL_VIRT
1051         SYSCALL_FASTENDIAN
1052 EXC_VIRT_END(system_call, 0x4c00, 0x100)
1053
1054 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1055         /*
1056          * This is a hcall, so register convention is as above, with these
1057          * differences:
1058          * r13 = PACA
1059          * ctr = orig r13
1060          * orig r10 saved in PACA
1061          */
1062 TRAMP_KVM_BEGIN(do_kvm_0xc00)
1063          /*
1064           * Save the PPR (on systems that support it) before changing to
1065           * HMT_MEDIUM. That allows the KVM code to save that value into the
1066           * guest state (it is the guest's PPR value).
1067           */
1068         OPT_GET_SPR(r10, SPRN_PPR, CPU_FTR_HAS_PPR)
1069         HMT_MEDIUM
1070         OPT_SAVE_REG_TO_PACA(PACA_EXGEN+EX_PPR, r10, CPU_FTR_HAS_PPR)
1071         mfctr   r10
1072         SET_SCRATCH0(r10)
1073         std     r9,PACA_EXGEN+EX_R9(r13)
1074         mfcr    r9
1075         KVM_HANDLER(PACA_EXGEN, EXC_STD, 0xc00)
1076 #endif
1077
1078
1079 EXC_REAL(single_step, 0xd00, 0x100)
1080 EXC_VIRT(single_step, 0x4d00, 0x100, 0xd00)
1081 TRAMP_KVM(PACA_EXGEN, 0xd00)
1082 EXC_COMMON(single_step_common, 0xd00, single_step_exception)
1083
1084 EXC_REAL_OOL_HV(h_data_storage, 0xe00, 0x20)
1085 EXC_VIRT_OOL_HV(h_data_storage, 0x4e00, 0x20, 0xe00)
1086 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0xe00)
1087 EXC_COMMON_BEGIN(h_data_storage_common)
1088         mfspr   r10,SPRN_HDAR
1089         std     r10,PACA_EXGEN+EX_DAR(r13)
1090         mfspr   r10,SPRN_HDSISR
1091         stw     r10,PACA_EXGEN+EX_DSISR(r13)
1092         EXCEPTION_PROLOG_COMMON(0xe00, PACA_EXGEN)
1093         bl      save_nvgprs
1094         RECONCILE_IRQ_STATE(r10, r11)
1095         addi    r3,r1,STACK_FRAME_OVERHEAD
1096         bl      unknown_exception
1097         b       ret_from_except
1098
1099
1100 EXC_REAL_OOL_HV(h_instr_storage, 0xe20, 0x20)
1101 EXC_VIRT_OOL_HV(h_instr_storage, 0x4e20, 0x20, 0xe20)
1102 TRAMP_KVM_HV(PACA_EXGEN, 0xe20)
1103 EXC_COMMON(h_instr_storage_common, 0xe20, unknown_exception)
1104
1105
1106 EXC_REAL_OOL_HV(emulation_assist, 0xe40, 0x20)
1107 EXC_VIRT_OOL_HV(emulation_assist, 0x4e40, 0x20, 0xe40)
1108 TRAMP_KVM_HV(PACA_EXGEN, 0xe40)
1109 EXC_COMMON(emulation_assist_common, 0xe40, emulation_assist_interrupt)
1110
1111
1112 /*
1113  * hmi_exception trampoline is a special case. It jumps to hmi_exception_early
1114  * first, and then eventaully from there to the trampoline to get into virtual
1115  * mode.
1116  */
1117 __EXC_REAL_OOL_HV_DIRECT(hmi_exception, 0xe60, 0x20, hmi_exception_early)
1118 __TRAMP_REAL_OOL_MASKABLE_HV(hmi_exception, 0xe60, IRQS_DISABLED)
1119 EXC_VIRT_NONE(0x4e60, 0x20)
1120 TRAMP_KVM_HV(PACA_EXGEN, 0xe60)
1121 TRAMP_REAL_BEGIN(hmi_exception_early)
1122         EXCEPTION_PROLOG_1(PACA_EXGEN, KVMTEST_HV, 0xe60)
1123         mr      r10,r1                  /* Save r1 */
1124         ld      r1,PACAEMERGSP(r13)     /* Use emergency stack for realmode */
1125         subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
1126         mfspr   r11,SPRN_HSRR0          /* Save HSRR0 */
1127         mfspr   r12,SPRN_HSRR1          /* Save HSRR1 */
1128         EXCEPTION_PROLOG_COMMON_1()
1129         EXCEPTION_PROLOG_COMMON_2(PACA_EXGEN)
1130         EXCEPTION_PROLOG_COMMON_3(0xe60)
1131         addi    r3,r1,STACK_FRAME_OVERHEAD
1132         BRANCH_LINK_TO_FAR(DOTSYM(hmi_exception_realmode)) /* Function call ABI */
1133         cmpdi   cr0,r3,0
1134
1135         /* Windup the stack. */
1136         /* Move original HSRR0 and HSRR1 into the respective regs */
1137         ld      r9,_MSR(r1)
1138         mtspr   SPRN_HSRR1,r9
1139         ld      r3,_NIP(r1)
1140         mtspr   SPRN_HSRR0,r3
1141         ld      r9,_CTR(r1)
1142         mtctr   r9
1143         ld      r9,_XER(r1)
1144         mtxer   r9
1145         ld      r9,_LINK(r1)
1146         mtlr    r9
1147         REST_GPR(0, r1)
1148         REST_8GPRS(2, r1)
1149         REST_GPR(10, r1)
1150         ld      r11,_CCR(r1)
1151         REST_2GPRS(12, r1)
1152         bne     1f
1153         mtcr    r11
1154         REST_GPR(11, r1)
1155         ld      r1,GPR1(r1)
1156         HRFI_TO_USER_OR_KERNEL
1157
1158 1:      mtcr    r11
1159         REST_GPR(11, r1)
1160         ld      r1,GPR1(r1)
1161
1162         /*
1163          * Go to virtual mode and pull the HMI event information from
1164          * firmware.
1165          */
1166         .globl hmi_exception_after_realmode
1167 hmi_exception_after_realmode:
1168         SET_SCRATCH0(r13)
1169         EXCEPTION_PROLOG_0(PACA_EXGEN)
1170         b       tramp_real_hmi_exception
1171
1172 EXC_COMMON_BEGIN(hmi_exception_common)
1173 EXCEPTION_COMMON(PACA_EXGEN, 0xe60, hmi_exception_common, handle_hmi_exception,
1174         ret_from_except, FINISH_NAP;ADD_NVGPRS;ADD_RECONCILE;RUNLATCH_ON)
1175
1176 EXC_REAL_OOL_MASKABLE_HV(h_doorbell, 0xe80, 0x20, IRQS_DISABLED)
1177 EXC_VIRT_OOL_MASKABLE_HV(h_doorbell, 0x4e80, 0x20, 0xe80, IRQS_DISABLED)
1178 TRAMP_KVM_HV(PACA_EXGEN, 0xe80)
1179 #ifdef CONFIG_PPC_DOORBELL
1180 EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, doorbell_exception)
1181 #else
1182 EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, unknown_exception)
1183 #endif
1184
1185
1186 EXC_REAL_OOL_MASKABLE_HV(h_virt_irq, 0xea0, 0x20, IRQS_DISABLED)
1187 EXC_VIRT_OOL_MASKABLE_HV(h_virt_irq, 0x4ea0, 0x20, 0xea0, IRQS_DISABLED)
1188 TRAMP_KVM_HV(PACA_EXGEN, 0xea0)
1189 EXC_COMMON_ASYNC(h_virt_irq_common, 0xea0, do_IRQ)
1190
1191
1192 EXC_REAL_NONE(0xec0, 0x20)
1193 EXC_VIRT_NONE(0x4ec0, 0x20)
1194 EXC_REAL_NONE(0xee0, 0x20)
1195 EXC_VIRT_NONE(0x4ee0, 0x20)
1196
1197
1198 EXC_REAL_OOL_MASKABLE(performance_monitor, 0xf00, 0x20, IRQS_PMI_DISABLED)
1199 EXC_VIRT_OOL_MASKABLE(performance_monitor, 0x4f00, 0x20, 0xf00, IRQS_PMI_DISABLED)
1200 TRAMP_KVM(PACA_EXGEN, 0xf00)
1201 EXC_COMMON_ASYNC(performance_monitor_common, 0xf00, performance_monitor_exception)
1202
1203
1204 EXC_REAL_OOL(altivec_unavailable, 0xf20, 0x20)
1205 EXC_VIRT_OOL(altivec_unavailable, 0x4f20, 0x20, 0xf20)
1206 TRAMP_KVM(PACA_EXGEN, 0xf20)
1207 EXC_COMMON_BEGIN(altivec_unavailable_common)
1208         EXCEPTION_PROLOG_COMMON(0xf20, PACA_EXGEN)
1209 #ifdef CONFIG_ALTIVEC
1210 BEGIN_FTR_SECTION
1211         beq     1f
1212 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1213   BEGIN_FTR_SECTION_NESTED(69)
1214         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1215          * transaction), go do TM stuff
1216          */
1217         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1218         bne-    2f
1219   END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1220 #endif
1221         bl      load_up_altivec
1222         b       fast_exception_return
1223 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1224 2:      /* User process was in a transaction */
1225         bl      save_nvgprs
1226         RECONCILE_IRQ_STATE(r10, r11)
1227         addi    r3,r1,STACK_FRAME_OVERHEAD
1228         bl      altivec_unavailable_tm
1229         b       ret_from_except
1230 #endif
1231 1:
1232 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1233 #endif
1234         bl      save_nvgprs
1235         RECONCILE_IRQ_STATE(r10, r11)
1236         addi    r3,r1,STACK_FRAME_OVERHEAD
1237         bl      altivec_unavailable_exception
1238         b       ret_from_except
1239
1240
1241 EXC_REAL_OOL(vsx_unavailable, 0xf40, 0x20)
1242 EXC_VIRT_OOL(vsx_unavailable, 0x4f40, 0x20, 0xf40)
1243 TRAMP_KVM(PACA_EXGEN, 0xf40)
1244 EXC_COMMON_BEGIN(vsx_unavailable_common)
1245         EXCEPTION_PROLOG_COMMON(0xf40, PACA_EXGEN)
1246 #ifdef CONFIG_VSX
1247 BEGIN_FTR_SECTION
1248         beq     1f
1249 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1250   BEGIN_FTR_SECTION_NESTED(69)
1251         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1252          * transaction), go do TM stuff
1253          */
1254         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1255         bne-    2f
1256   END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1257 #endif
1258         b       load_up_vsx
1259 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1260 2:      /* User process was in a transaction */
1261         bl      save_nvgprs
1262         RECONCILE_IRQ_STATE(r10, r11)
1263         addi    r3,r1,STACK_FRAME_OVERHEAD
1264         bl      vsx_unavailable_tm
1265         b       ret_from_except
1266 #endif
1267 1:
1268 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1269 #endif
1270         bl      save_nvgprs
1271         RECONCILE_IRQ_STATE(r10, r11)
1272         addi    r3,r1,STACK_FRAME_OVERHEAD
1273         bl      vsx_unavailable_exception
1274         b       ret_from_except
1275
1276
1277 EXC_REAL_OOL(facility_unavailable, 0xf60, 0x20)
1278 EXC_VIRT_OOL(facility_unavailable, 0x4f60, 0x20, 0xf60)
1279 TRAMP_KVM(PACA_EXGEN, 0xf60)
1280 EXC_COMMON(facility_unavailable_common, 0xf60, facility_unavailable_exception)
1281
1282
1283 EXC_REAL_OOL_HV(h_facility_unavailable, 0xf80, 0x20)
1284 EXC_VIRT_OOL_HV(h_facility_unavailable, 0x4f80, 0x20, 0xf80)
1285 TRAMP_KVM_HV(PACA_EXGEN, 0xf80)
1286 EXC_COMMON(h_facility_unavailable_common, 0xf80, facility_unavailable_exception)
1287
1288
1289 EXC_REAL_NONE(0xfa0, 0x20)
1290 EXC_VIRT_NONE(0x4fa0, 0x20)
1291 EXC_REAL_NONE(0xfc0, 0x20)
1292 EXC_VIRT_NONE(0x4fc0, 0x20)
1293 EXC_REAL_NONE(0xfe0, 0x20)
1294 EXC_VIRT_NONE(0x4fe0, 0x20)
1295
1296 EXC_REAL_NONE(0x1000, 0x100)
1297 EXC_VIRT_NONE(0x5000, 0x100)
1298 EXC_REAL_NONE(0x1100, 0x100)
1299 EXC_VIRT_NONE(0x5100, 0x100)
1300
1301 #ifdef CONFIG_CBE_RAS
1302 EXC_REAL_HV(cbe_system_error, 0x1200, 0x100)
1303 EXC_VIRT_NONE(0x5200, 0x100)
1304 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1200)
1305 EXC_COMMON(cbe_system_error_common, 0x1200, cbe_system_error_exception)
1306 #else /* CONFIG_CBE_RAS */
1307 EXC_REAL_NONE(0x1200, 0x100)
1308 EXC_VIRT_NONE(0x5200, 0x100)
1309 #endif
1310
1311
1312 EXC_REAL(instruction_breakpoint, 0x1300, 0x100)
1313 EXC_VIRT(instruction_breakpoint, 0x5300, 0x100, 0x1300)
1314 TRAMP_KVM_SKIP(PACA_EXGEN, 0x1300)
1315 EXC_COMMON(instruction_breakpoint_common, 0x1300, instruction_breakpoint_exception)
1316
1317 EXC_REAL_NONE(0x1400, 0x100)
1318 EXC_VIRT_NONE(0x5400, 0x100)
1319
1320 EXC_REAL_BEGIN(denorm_exception_hv, 0x1500, 0x100)
1321         mtspr   SPRN_SPRG_HSCRATCH0,r13
1322         EXCEPTION_PROLOG_0(PACA_EXGEN)
1323         EXCEPTION_PROLOG_1(PACA_EXGEN, NOTEST, 0x1500)
1324
1325 #ifdef CONFIG_PPC_DENORMALISATION
1326         mfspr   r10,SPRN_HSRR1
1327         andis.  r10,r10,(HSRR1_DENORM)@h /* denorm? */
1328         bne+    denorm_assist
1329 #endif
1330
1331         KVMTEST_HV(0x1500)
1332         EXCEPTION_PROLOG_2(denorm_common, EXC_HV)
1333 EXC_REAL_END(denorm_exception_hv, 0x1500, 0x100)
1334
1335 #ifdef CONFIG_PPC_DENORMALISATION
1336 EXC_VIRT_BEGIN(denorm_exception, 0x5500, 0x100)
1337         b       exc_real_0x1500_denorm_exception_hv
1338 EXC_VIRT_END(denorm_exception, 0x5500, 0x100)
1339 #else
1340 EXC_VIRT_NONE(0x5500, 0x100)
1341 #endif
1342
1343 TRAMP_KVM_HV(PACA_EXGEN, 0x1500)
1344
1345 #ifdef CONFIG_PPC_DENORMALISATION
1346 TRAMP_REAL_BEGIN(denorm_assist)
1347 BEGIN_FTR_SECTION
1348 /*
1349  * To denormalise we need to move a copy of the register to itself.
1350  * For POWER6 do that here for all FP regs.
1351  */
1352         mfmsr   r10
1353         ori     r10,r10,(MSR_FP|MSR_FE0|MSR_FE1)
1354         xori    r10,r10,(MSR_FE0|MSR_FE1)
1355         mtmsrd  r10
1356         sync
1357
1358 #define FMR2(n)  fmr (n), (n) ; fmr n+1, n+1
1359 #define FMR4(n)  FMR2(n) ; FMR2(n+2)
1360 #define FMR8(n)  FMR4(n) ; FMR4(n+4)
1361 #define FMR16(n) FMR8(n) ; FMR8(n+8)
1362 #define FMR32(n) FMR16(n) ; FMR16(n+16)
1363         FMR32(0)
1364
1365 FTR_SECTION_ELSE
1366 /*
1367  * To denormalise we need to move a copy of the register to itself.
1368  * For POWER7 do that here for the first 32 VSX registers only.
1369  */
1370         mfmsr   r10
1371         oris    r10,r10,MSR_VSX@h
1372         mtmsrd  r10
1373         sync
1374
1375 #define XVCPSGNDP2(n) XVCPSGNDP(n,n,n) ; XVCPSGNDP(n+1,n+1,n+1)
1376 #define XVCPSGNDP4(n) XVCPSGNDP2(n) ; XVCPSGNDP2(n+2)
1377 #define XVCPSGNDP8(n) XVCPSGNDP4(n) ; XVCPSGNDP4(n+4)
1378 #define XVCPSGNDP16(n) XVCPSGNDP8(n) ; XVCPSGNDP8(n+8)
1379 #define XVCPSGNDP32(n) XVCPSGNDP16(n) ; XVCPSGNDP16(n+16)
1380         XVCPSGNDP32(0)
1381
1382 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_206)
1383
1384 BEGIN_FTR_SECTION
1385         b       denorm_done
1386 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1387 /*
1388  * To denormalise we need to move a copy of the register to itself.
1389  * For POWER8 we need to do that for all 64 VSX registers
1390  */
1391         XVCPSGNDP32(32)
1392 denorm_done:
1393         mfspr   r11,SPRN_HSRR0
1394         subi    r11,r11,4
1395         mtspr   SPRN_HSRR0,r11
1396         mtcrf   0x80,r9
1397         ld      r9,PACA_EXGEN+EX_R9(r13)
1398         RESTORE_PPR_PACA(PACA_EXGEN, r10)
1399 BEGIN_FTR_SECTION
1400         ld      r10,PACA_EXGEN+EX_CFAR(r13)
1401         mtspr   SPRN_CFAR,r10
1402 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1403         ld      r10,PACA_EXGEN+EX_R10(r13)
1404         ld      r11,PACA_EXGEN+EX_R11(r13)
1405         ld      r12,PACA_EXGEN+EX_R12(r13)
1406         ld      r13,PACA_EXGEN+EX_R13(r13)
1407         HRFI_TO_UNKNOWN
1408         b       .
1409 #endif
1410
1411 EXC_COMMON(denorm_common, 0x1500, unknown_exception)
1412
1413
1414 #ifdef CONFIG_CBE_RAS
1415 EXC_REAL_HV(cbe_maintenance, 0x1600, 0x100)
1416 EXC_VIRT_NONE(0x5600, 0x100)
1417 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1600)
1418 EXC_COMMON(cbe_maintenance_common, 0x1600, cbe_maintenance_exception)
1419 #else /* CONFIG_CBE_RAS */
1420 EXC_REAL_NONE(0x1600, 0x100)
1421 EXC_VIRT_NONE(0x5600, 0x100)
1422 #endif
1423
1424
1425 EXC_REAL(altivec_assist, 0x1700, 0x100)
1426 EXC_VIRT(altivec_assist, 0x5700, 0x100, 0x1700)
1427 TRAMP_KVM(PACA_EXGEN, 0x1700)
1428 #ifdef CONFIG_ALTIVEC
1429 EXC_COMMON(altivec_assist_common, 0x1700, altivec_assist_exception)
1430 #else
1431 EXC_COMMON(altivec_assist_common, 0x1700, unknown_exception)
1432 #endif
1433
1434
1435 #ifdef CONFIG_CBE_RAS
1436 EXC_REAL_HV(cbe_thermal, 0x1800, 0x100)
1437 EXC_VIRT_NONE(0x5800, 0x100)
1438 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1800)
1439 EXC_COMMON(cbe_thermal_common, 0x1800, cbe_thermal_exception)
1440 #else /* CONFIG_CBE_RAS */
1441 EXC_REAL_NONE(0x1800, 0x100)
1442 EXC_VIRT_NONE(0x5800, 0x100)
1443 #endif
1444
1445 #ifdef CONFIG_PPC_WATCHDOG
1446
1447 #define MASKED_DEC_HANDLER_LABEL 3f
1448
1449 #define MASKED_DEC_HANDLER(_H)                          \
1450 3: /* soft-nmi */                                       \
1451         std     r12,PACA_EXGEN+EX_R12(r13);             \
1452         GET_SCRATCH0(r10);                              \
1453         std     r10,PACA_EXGEN+EX_R13(r13);             \
1454         EXCEPTION_PROLOG_2(soft_nmi_common, _H)
1455
1456 /*
1457  * Branch to soft_nmi_interrupt using the emergency stack. The emergency
1458  * stack is one that is usable by maskable interrupts so long as MSR_EE
1459  * remains off. It is used for recovery when something has corrupted the
1460  * normal kernel stack, for example. The "soft NMI" must not use the process
1461  * stack because we want irq disabled sections to avoid touching the stack
1462  * at all (other than PMU interrupts), so use the emergency stack for this,
1463  * and run it entirely with interrupts hard disabled.
1464  */
1465 EXC_COMMON_BEGIN(soft_nmi_common)
1466         mr      r10,r1
1467         ld      r1,PACAEMERGSP(r13)
1468         subi    r1,r1,INT_FRAME_SIZE
1469         EXCEPTION_COMMON_NORET_STACK(PACA_EXGEN, 0x900,
1470                         system_reset, soft_nmi_interrupt,
1471                         ADD_NVGPRS;ADD_RECONCILE)
1472         b       ret_from_except
1473
1474 #else /* CONFIG_PPC_WATCHDOG */
1475 #define MASKED_DEC_HANDLER_LABEL 2f /* normal return */
1476 #define MASKED_DEC_HANDLER(_H)
1477 #endif /* CONFIG_PPC_WATCHDOG */
1478
1479 /*
1480  * An interrupt came in while soft-disabled. We set paca->irq_happened, then:
1481  * - If it was a decrementer interrupt, we bump the dec to max and and return.
1482  * - If it was a doorbell we return immediately since doorbells are edge
1483  *   triggered and won't automatically refire.
1484  * - If it was a HMI we return immediately since we handled it in realmode
1485  *   and it won't refire.
1486  * - Else it is one of PACA_IRQ_MUST_HARD_MASK, so hard disable and return.
1487  * This is called with r10 containing the value to OR to the paca field.
1488  */
1489 #define MASKED_INTERRUPT(_H)                            \
1490 masked_##_H##interrupt:                                 \
1491         std     r11,PACA_EXGEN+EX_R11(r13);             \
1492         lbz     r11,PACAIRQHAPPENED(r13);               \
1493         or      r11,r11,r10;                            \
1494         stb     r11,PACAIRQHAPPENED(r13);               \
1495         cmpwi   r10,PACA_IRQ_DEC;                       \
1496         bne     1f;                                     \
1497         lis     r10,0x7fff;                             \
1498         ori     r10,r10,0xffff;                         \
1499         mtspr   SPRN_DEC,r10;                           \
1500         b       MASKED_DEC_HANDLER_LABEL;               \
1501 1:      andi.   r10,r10,PACA_IRQ_MUST_HARD_MASK;        \
1502         beq     2f;                                     \
1503         mfspr   r10,SPRN_##_H##SRR1;                    \
1504         xori    r10,r10,MSR_EE; /* clear MSR_EE */      \
1505         mtspr   SPRN_##_H##SRR1,r10;                    \
1506         ori     r11,r11,PACA_IRQ_HARD_DIS;              \
1507         stb     r11,PACAIRQHAPPENED(r13);               \
1508 2:      /* done */                                      \
1509         mtcrf   0x80,r9;                                \
1510         std     r1,PACAR1(r13);                         \
1511         ld      r9,PACA_EXGEN+EX_R9(r13);               \
1512         ld      r10,PACA_EXGEN+EX_R10(r13);             \
1513         ld      r11,PACA_EXGEN+EX_R11(r13);             \
1514         /* returns to kernel where r13 must be set up, so don't restore it */ \
1515         ##_H##RFI_TO_KERNEL;                            \
1516         b       .;                                      \
1517         MASKED_DEC_HANDLER(_H)
1518
1519 TRAMP_REAL_BEGIN(stf_barrier_fallback)
1520         std     r9,PACA_EXRFI+EX_R9(r13)
1521         std     r10,PACA_EXRFI+EX_R10(r13)
1522         sync
1523         ld      r9,PACA_EXRFI+EX_R9(r13)
1524         ld      r10,PACA_EXRFI+EX_R10(r13)
1525         ori     31,31,0
1526         .rept 14
1527         b       1f
1528 1:
1529         .endr
1530         blr
1531
1532 /* Clobbers r10, r11, ctr */
1533 .macro L1D_DISPLACEMENT_FLUSH
1534         ld      r10,PACA_RFI_FLUSH_FALLBACK_AREA(r13)
1535         ld      r11,PACA_L1D_FLUSH_SIZE(r13)
1536         srdi    r11,r11,(7 + 3) /* 128 byte lines, unrolled 8x */
1537         mtctr   r11
1538         DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r11) /* Stop prefetch streams */
1539
1540         /* order ld/st prior to dcbt stop all streams with flushing */
1541         sync
1542
1543         /*
1544          * The load addresses are at staggered offsets within cachelines,
1545          * which suits some pipelines better (on others it should not
1546          * hurt).
1547          */
1548 1:
1549         ld      r11,(0x80 + 8)*0(r10)
1550         ld      r11,(0x80 + 8)*1(r10)
1551         ld      r11,(0x80 + 8)*2(r10)
1552         ld      r11,(0x80 + 8)*3(r10)
1553         ld      r11,(0x80 + 8)*4(r10)
1554         ld      r11,(0x80 + 8)*5(r10)
1555         ld      r11,(0x80 + 8)*6(r10)
1556         ld      r11,(0x80 + 8)*7(r10)
1557         addi    r10,r10,0x80*8
1558         bdnz    1b
1559 .endm
1560
1561 TRAMP_REAL_BEGIN(entry_flush_fallback)
1562         std     r9,PACA_EXRFI+EX_R9(r13)
1563         std     r10,PACA_EXRFI+EX_R10(r13)
1564         std     r11,PACA_EXRFI+EX_R11(r13)
1565         mfctr   r9
1566         L1D_DISPLACEMENT_FLUSH
1567         mtctr   r9
1568         ld      r9,PACA_EXRFI+EX_R9(r13)
1569         ld      r10,PACA_EXRFI+EX_R10(r13)
1570         ld      r11,PACA_EXRFI+EX_R11(r13)
1571         blr
1572
1573 TRAMP_REAL_BEGIN(rfi_flush_fallback)
1574         SET_SCRATCH0(r13);
1575         GET_PACA(r13);
1576         std     r1,PACA_EXRFI+EX_R12(r13)
1577         ld      r1,PACAKSAVE(r13)
1578         std     r9,PACA_EXRFI+EX_R9(r13)
1579         std     r10,PACA_EXRFI+EX_R10(r13)
1580         std     r11,PACA_EXRFI+EX_R11(r13)
1581         mfctr   r9
1582         L1D_DISPLACEMENT_FLUSH
1583         mtctr   r9
1584         ld      r9,PACA_EXRFI+EX_R9(r13)
1585         ld      r10,PACA_EXRFI+EX_R10(r13)
1586         ld      r11,PACA_EXRFI+EX_R11(r13)
1587         ld      r1,PACA_EXRFI+EX_R12(r13)
1588         GET_SCRATCH0(r13);
1589         rfid
1590
1591 TRAMP_REAL_BEGIN(hrfi_flush_fallback)
1592         SET_SCRATCH0(r13);
1593         GET_PACA(r13);
1594         std     r1,PACA_EXRFI+EX_R12(r13)
1595         ld      r1,PACAKSAVE(r13)
1596         std     r9,PACA_EXRFI+EX_R9(r13)
1597         std     r10,PACA_EXRFI+EX_R10(r13)
1598         std     r11,PACA_EXRFI+EX_R11(r13)
1599         mfctr   r9
1600         L1D_DISPLACEMENT_FLUSH
1601         mtctr   r9
1602         ld      r9,PACA_EXRFI+EX_R9(r13)
1603         ld      r10,PACA_EXRFI+EX_R10(r13)
1604         ld      r11,PACA_EXRFI+EX_R11(r13)
1605         ld      r1,PACA_EXRFI+EX_R12(r13)
1606         GET_SCRATCH0(r13);
1607         hrfid
1608
1609 USE_TEXT_SECTION()
1610
1611 _GLOBAL(do_uaccess_flush)
1612         UACCESS_FLUSH_FIXUP_SECTION
1613         nop
1614         nop
1615         nop
1616         blr
1617         L1D_DISPLACEMENT_FLUSH
1618         blr
1619 _ASM_NOKPROBE_SYMBOL(do_uaccess_flush)
1620 EXPORT_SYMBOL(do_uaccess_flush)
1621
1622 /*
1623  * Real mode exceptions actually use this too, but alternate
1624  * instruction code patches (which end up in the common .text area)
1625  * cannot reach these if they are put there.
1626  */
1627 USE_FIXED_SECTION(virt_trampolines)
1628         MASKED_INTERRUPT()
1629         MASKED_INTERRUPT(H)
1630
1631 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1632 TRAMP_REAL_BEGIN(kvmppc_skip_interrupt)
1633         /*
1634          * Here all GPRs are unchanged from when the interrupt happened
1635          * except for r13, which is saved in SPRG_SCRATCH0.
1636          */
1637         mfspr   r13, SPRN_SRR0
1638         addi    r13, r13, 4
1639         mtspr   SPRN_SRR0, r13
1640         GET_SCRATCH0(r13)
1641         RFI_TO_KERNEL
1642         b       .
1643
1644 TRAMP_REAL_BEGIN(kvmppc_skip_Hinterrupt)
1645         /*
1646          * Here all GPRs are unchanged from when the interrupt happened
1647          * except for r13, which is saved in SPRG_SCRATCH0.
1648          */
1649         mfspr   r13, SPRN_HSRR0
1650         addi    r13, r13, 4
1651         mtspr   SPRN_HSRR0, r13
1652         GET_SCRATCH0(r13)
1653         HRFI_TO_KERNEL
1654         b       .
1655 #endif
1656
1657 /*
1658  * Ensure that any handlers that get invoked from the exception prologs
1659  * above are below the first 64KB (0x10000) of the kernel image because
1660  * the prologs assemble the addresses of these handlers using the
1661  * LOAD_HANDLER macro, which uses an ori instruction.
1662  */
1663
1664 /*** Common interrupt handlers ***/
1665
1666
1667         /*
1668          * Relocation-on interrupts: A subset of the interrupts can be delivered
1669          * with IR=1/DR=1, if AIL==2 and MSR.HV won't be changed by delivering
1670          * it.  Addresses are the same as the original interrupt addresses, but
1671          * offset by 0xc000000000004000.
1672          * It's impossible to receive interrupts below 0x300 via this mechanism.
1673          * KVM: None of these traps are from the guest ; anything that escalated
1674          * to HV=1 from HV=0 is delivered via real mode handlers.
1675          */
1676
1677         /*
1678          * This uses the standard macro, since the original 0x300 vector
1679          * only has extra guff for STAB-based processors -- which never
1680          * come here.
1681          */
1682
1683 EXC_COMMON_BEGIN(ppc64_runlatch_on_trampoline)
1684         b       __ppc64_runlatch_on
1685
1686 USE_FIXED_SECTION(virt_trampolines)
1687         /*
1688          * The __end_interrupts marker must be past the out-of-line (OOL)
1689          * handlers, so that they are copied to real address 0x100 when running
1690          * a relocatable kernel. This ensures they can be reached from the short
1691          * trampoline handlers (like 0x4f00, 0x4f20, etc.) which branch
1692          * directly, without using LOAD_HANDLER().
1693          */
1694         .align  7
1695         .globl  __end_interrupts
1696 __end_interrupts:
1697 DEFINE_FIXED_SYMBOL(__end_interrupts)
1698
1699 #ifdef CONFIG_PPC_970_NAP
1700 EXC_COMMON_BEGIN(power4_fixup_nap)
1701         andc    r9,r9,r10
1702         std     r9,TI_LOCAL_FLAGS(r11)
1703         ld      r10,_LINK(r1)           /* make idle task do the */
1704         std     r10,_NIP(r1)            /* equivalent of a blr */
1705         blr
1706 #endif
1707
1708 CLOSE_FIXED_SECTION(real_vectors);
1709 CLOSE_FIXED_SECTION(real_trampolines);
1710 CLOSE_FIXED_SECTION(virt_vectors);
1711 CLOSE_FIXED_SECTION(virt_trampolines);
1712
1713 USE_TEXT_SECTION()
1714
1715 /*
1716  * Hash table stuff
1717  */
1718         .balign IFETCH_ALIGN_BYTES
1719 do_hash_page:
1720 #ifdef CONFIG_PPC_BOOK3S_64
1721         lis     r0,(DSISR_BAD_FAULT_64S | DSISR_DABRMATCH | DSISR_KEYFAULT)@h
1722         ori     r0,r0,DSISR_BAD_FAULT_64S@l
1723         and.    r0,r4,r0                /* weird error? */
1724         bne-    handle_page_fault       /* if not, try to insert a HPTE */
1725         CURRENT_THREAD_INFO(r11, r1)
1726         lwz     r0,TI_PREEMPT(r11)      /* If we're in an "NMI" */
1727         andis.  r0,r0,NMI_MASK@h        /* (i.e. an irq when soft-disabled) */
1728         bne     77f                     /* then don't call hash_page now */
1729
1730         /*
1731          * r3 contains the faulting address
1732          * r4 msr
1733          * r5 contains the trap number
1734          * r6 contains dsisr
1735          *
1736          * at return r3 = 0 for success, 1 for page fault, negative for error
1737          */
1738         mr      r4,r12
1739         ld      r6,_DSISR(r1)
1740         bl      __hash_page             /* build HPTE if possible */
1741         cmpdi   r3,0                    /* see if __hash_page succeeded */
1742
1743         /* Success */
1744         beq     fast_exc_return_irq     /* Return from exception on success */
1745
1746         /* Error */
1747         blt-    13f
1748
1749         /* Reload DSISR into r4 for the DABR check below */
1750         ld      r4,_DSISR(r1)
1751 #endif /* CONFIG_PPC_BOOK3S_64 */
1752
1753 /* Here we have a page fault that hash_page can't handle. */
1754 handle_page_fault:
1755 11:     andis.  r0,r4,DSISR_DABRMATCH@h
1756         bne-    handle_dabr_fault
1757         ld      r4,_DAR(r1)
1758         ld      r5,_DSISR(r1)
1759         addi    r3,r1,STACK_FRAME_OVERHEAD
1760         bl      do_page_fault
1761         cmpdi   r3,0
1762         beq+    ret_from_except_lite
1763         bl      save_nvgprs
1764         mr      r5,r3
1765         addi    r3,r1,STACK_FRAME_OVERHEAD
1766         lwz     r4,_DAR(r1)
1767         bl      bad_page_fault
1768         b       ret_from_except
1769
1770 /* We have a data breakpoint exception - handle it */
1771 handle_dabr_fault:
1772         bl      save_nvgprs
1773         ld      r4,_DAR(r1)
1774         ld      r5,_DSISR(r1)
1775         addi    r3,r1,STACK_FRAME_OVERHEAD
1776         bl      do_break
1777         /*
1778          * do_break() may have changed the NV GPRS while handling a breakpoint.
1779          * If so, we need to restore them with their updated values. Don't use
1780          * ret_from_except_lite here.
1781          */
1782         b       ret_from_except
1783
1784
1785 #ifdef CONFIG_PPC_BOOK3S_64
1786 /* We have a page fault that hash_page could handle but HV refused
1787  * the PTE insertion
1788  */
1789 13:     bl      save_nvgprs
1790         mr      r5,r3
1791         addi    r3,r1,STACK_FRAME_OVERHEAD
1792         ld      r4,_DAR(r1)
1793         bl      low_hash_fault
1794         b       ret_from_except
1795 #endif
1796
1797 /*
1798  * We come here as a result of a DSI at a point where we don't want
1799  * to call hash_page, such as when we are accessing memory (possibly
1800  * user memory) inside a PMU interrupt that occurred while interrupts
1801  * were soft-disabled.  We want to invoke the exception handler for
1802  * the access, or panic if there isn't a handler.
1803  */
1804 77:     bl      save_nvgprs
1805         mr      r4,r3
1806         addi    r3,r1,STACK_FRAME_OVERHEAD
1807         li      r5,SIGSEGV
1808         bl      bad_page_fault
1809         b       ret_from_except
1810
1811 /*
1812  * Here we have detected that the kernel stack pointer is bad.
1813  * R9 contains the saved CR, r13 points to the paca,
1814  * r10 contains the (bad) kernel stack pointer,
1815  * r11 and r12 contain the saved SRR0 and SRR1.
1816  * We switch to using an emergency stack, save the registers there,
1817  * and call kernel_bad_stack(), which panics.
1818  */
1819 bad_stack:
1820         ld      r1,PACAEMERGSP(r13)
1821         subi    r1,r1,64+INT_FRAME_SIZE
1822         std     r9,_CCR(r1)
1823         std     r10,GPR1(r1)
1824         std     r11,_NIP(r1)
1825         std     r12,_MSR(r1)
1826         mfspr   r11,SPRN_DAR
1827         mfspr   r12,SPRN_DSISR
1828         std     r11,_DAR(r1)
1829         std     r12,_DSISR(r1)
1830         mflr    r10
1831         mfctr   r11
1832         mfxer   r12
1833         std     r10,_LINK(r1)
1834         std     r11,_CTR(r1)
1835         std     r12,_XER(r1)
1836         SAVE_GPR(0,r1)
1837         SAVE_GPR(2,r1)
1838         ld      r10,EX_R3(r3)
1839         std     r10,GPR3(r1)
1840         SAVE_GPR(4,r1)
1841         SAVE_4GPRS(5,r1)
1842         ld      r9,EX_R9(r3)
1843         ld      r10,EX_R10(r3)
1844         SAVE_2GPRS(9,r1)
1845         ld      r9,EX_R11(r3)
1846         ld      r10,EX_R12(r3)
1847         ld      r11,EX_R13(r3)
1848         std     r9,GPR11(r1)
1849         std     r10,GPR12(r1)
1850         std     r11,GPR13(r1)
1851 BEGIN_FTR_SECTION
1852         ld      r10,EX_CFAR(r3)
1853         std     r10,ORIG_GPR3(r1)
1854 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1855         SAVE_8GPRS(14,r1)
1856         SAVE_10GPRS(22,r1)
1857         lhz     r12,PACA_TRAP_SAVE(r13)
1858         std     r12,_TRAP(r1)
1859         addi    r11,r1,INT_FRAME_SIZE
1860         std     r11,0(r1)
1861         li      r12,0
1862         std     r12,0(r11)
1863         ld      r2,PACATOC(r13)
1864         ld      r11,exception_marker@toc(r2)
1865         std     r12,RESULT(r1)
1866         std     r11,STACK_FRAME_OVERHEAD-16(r1)
1867 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
1868         bl      kernel_bad_stack
1869         b       1b
1870 _ASM_NOKPROBE_SYMBOL(bad_stack);
1871
1872 /*
1873  * When doorbell is triggered from system reset wakeup, the message is
1874  * not cleared, so it would fire again when EE is enabled.
1875  *
1876  * When coming from local_irq_enable, there may be the same problem if
1877  * we were hard disabled.
1878  *
1879  * Execute msgclr to clear pending exceptions before handling it.
1880  */
1881 h_doorbell_common_msgclr:
1882         LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
1883         PPC_MSGCLR(3)
1884         b       h_doorbell_common
1885
1886 doorbell_super_common_msgclr:
1887         LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
1888         PPC_MSGCLRP(3)
1889         b       doorbell_super_common
1890
1891 /*
1892  * Called from arch_local_irq_enable when an interrupt needs
1893  * to be resent. r3 contains 0x500, 0x900, 0xa00 or 0xe80 to indicate
1894  * which kind of interrupt. MSR:EE is already off. We generate a
1895  * stackframe like if a real interrupt had happened.
1896  *
1897  * Note: While MSR:EE is off, we need to make sure that _MSR
1898  * in the generated frame has EE set to 1 or the exception
1899  * handler will not properly re-enable them.
1900  *
1901  * Note that we don't specify LR as the NIP (return address) for
1902  * the interrupt because that would unbalance the return branch
1903  * predictor.
1904  */
1905 _GLOBAL(__replay_interrupt)
1906         /* We are going to jump to the exception common code which
1907          * will retrieve various register values from the PACA which
1908          * we don't give a damn about, so we don't bother storing them.
1909          */
1910         mfmsr   r12
1911         LOAD_REG_ADDR(r11, replay_interrupt_return)
1912         mfcr    r9
1913         ori     r12,r12,MSR_EE
1914         cmpwi   r3,0x900
1915         beq     decrementer_common
1916         cmpwi   r3,0x500
1917 BEGIN_FTR_SECTION
1918         beq     h_virt_irq_common
1919 FTR_SECTION_ELSE
1920         beq     hardware_interrupt_common
1921 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_300)
1922         cmpwi   r3,0xf00
1923         beq     performance_monitor_common
1924 BEGIN_FTR_SECTION
1925         cmpwi   r3,0xa00
1926         beq     h_doorbell_common_msgclr
1927         cmpwi   r3,0xe60
1928         beq     hmi_exception_common
1929 FTR_SECTION_ELSE
1930         cmpwi   r3,0xa00
1931         beq     doorbell_super_common_msgclr
1932 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
1933 replay_interrupt_return:
1934         blr
1935
1936 _ASM_NOKPROBE_SYMBOL(__replay_interrupt)