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