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