c73e909470e3a5650fdfc06ef817e1abf0581e75
[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 /* PACA save area offsets (exgen, exmc, etc) */
25 #define EX_R9           0
26 #define EX_R10          8
27 #define EX_R11          16
28 #define EX_R12          24
29 #define EX_R13          32
30 #define EX_DAR          40
31 #define EX_DSISR        48
32 #define EX_CCR          52
33 #define EX_CFAR         56
34 #define EX_PPR          64
35 #if defined(CONFIG_RELOCATABLE)
36 #define EX_CTR          72
37 .if EX_SIZE != 10
38         .error "EX_SIZE is wrong"
39 .endif
40 #else
41 .if EX_SIZE != 9
42         .error "EX_SIZE is wrong"
43 .endif
44 #endif
45
46 /*
47  * We're short on space and time in the exception prolog, so we can't
48  * use the normal LOAD_REG_IMMEDIATE macro to load the address of label.
49  * Instead we get the base of the kernel from paca->kernelbase and or in the low
50  * part of label. This requires that the label be within 64KB of kernelbase, and
51  * that kernelbase be 64K aligned.
52  */
53 #define LOAD_HANDLER(reg, label)                                        \
54         ld      reg,PACAKBASE(r13);     /* get high part of &label */   \
55         ori     reg,reg,FIXED_SYMBOL_ABS_ADDR(label)
56
57 #define __LOAD_HANDLER(reg, label)                                      \
58         ld      reg,PACAKBASE(r13);                                     \
59         ori     reg,reg,(ABS_ADDR(label))@l
60
61 /*
62  * Branches from unrelocated code (e.g., interrupts) to labels outside
63  * head-y require >64K offsets.
64  */
65 #define __LOAD_FAR_HANDLER(reg, label)                                  \
66         ld      reg,PACAKBASE(r13);                                     \
67         ori     reg,reg,(ABS_ADDR(label))@l;                            \
68         addis   reg,reg,(ABS_ADDR(label))@h
69
70 /* Exception register prefixes */
71 #define EXC_HV          1
72 #define EXC_STD         0
73
74 #if defined(CONFIG_RELOCATABLE)
75 /*
76  * If we support interrupts with relocation on AND we're a relocatable kernel,
77  * we need to use CTR to get to the 2nd level handler.  So, save/restore it
78  * when required.
79  */
80 #define SAVE_CTR(reg, area)     mfctr   reg ;   std     reg,area+EX_CTR(r13)
81 #define GET_CTR(reg, area)                      ld      reg,area+EX_CTR(r13)
82 #define RESTORE_CTR(reg, area)  ld      reg,area+EX_CTR(r13) ; mtctr reg
83 #else
84 /* ...else CTR is unused and in register. */
85 #define SAVE_CTR(reg, area)
86 #define GET_CTR(reg, area)      mfctr   reg
87 #define RESTORE_CTR(reg, area)
88 #endif
89
90 /*
91  * PPR save/restore macros used in exceptions-64s.S
92  * Used for P7 or later processors
93  */
94 #define SAVE_PPR(area, ra)                                              \
95 BEGIN_FTR_SECTION_NESTED(940)                                           \
96         ld      ra,area+EX_PPR(r13);    /* Read PPR from paca */        \
97         std     ra,_PPR(r1);                                            \
98 END_FTR_SECTION_NESTED(CPU_FTR_HAS_PPR,CPU_FTR_HAS_PPR,940)
99
100 #define RESTORE_PPR_PACA(area, ra)                                      \
101 BEGIN_FTR_SECTION_NESTED(941)                                           \
102         ld      ra,area+EX_PPR(r13);                                    \
103         mtspr   SPRN_PPR,ra;                                            \
104 END_FTR_SECTION_NESTED(CPU_FTR_HAS_PPR,CPU_FTR_HAS_PPR,941)
105
106 /*
107  * Get an SPR into a register if the CPU has the given feature
108  */
109 #define OPT_GET_SPR(ra, spr, ftr)                                       \
110 BEGIN_FTR_SECTION_NESTED(943)                                           \
111         mfspr   ra,spr;                                                 \
112 END_FTR_SECTION_NESTED(ftr,ftr,943)
113
114 /*
115  * Set an SPR from a register if the CPU has the given feature
116  */
117 #define OPT_SET_SPR(ra, spr, ftr)                                       \
118 BEGIN_FTR_SECTION_NESTED(943)                                           \
119         mtspr   spr,ra;                                                 \
120 END_FTR_SECTION_NESTED(ftr,ftr,943)
121
122 /*
123  * Save a register to the PACA if the CPU has the given feature
124  */
125 #define OPT_SAVE_REG_TO_PACA(offset, ra, ftr)                           \
126 BEGIN_FTR_SECTION_NESTED(943)                                           \
127         std     ra,offset(r13);                                         \
128 END_FTR_SECTION_NESTED(ftr,ftr,943)
129
130 .macro EXCEPTION_PROLOG_0 area
131         SET_SCRATCH0(r13)                       /* save r13 */
132         GET_PACA(r13)
133         std     r9,\area\()+EX_R9(r13)          /* save r9 */
134         OPT_GET_SPR(r9, SPRN_PPR, CPU_FTR_HAS_PPR)
135         HMT_MEDIUM
136         std     r10,\area\()+EX_R10(r13)        /* save r10 - r12 */
137         OPT_GET_SPR(r10, SPRN_CFAR, CPU_FTR_CFAR)
138 .endm
139
140 .macro EXCEPTION_PROLOG_1 hsrr, area, kvm, vec, dar, dsisr, bitmask
141         OPT_SAVE_REG_TO_PACA(\area\()+EX_PPR, r9, CPU_FTR_HAS_PPR)
142         OPT_SAVE_REG_TO_PACA(\area\()+EX_CFAR, r10, CPU_FTR_CFAR)
143         INTERRUPT_TO_KERNEL
144         SAVE_CTR(r10, \area\())
145         mfcr    r9
146         .if \kvm
147                 KVMTEST \hsrr \vec
148         .endif
149         .if \bitmask
150                 lbz     r10,PACAIRQSOFTMASK(r13)
151                 andi.   r10,r10,\bitmask
152                 /* Associate vector numbers with bits in paca->irq_happened */
153                 .if \vec == 0x500 || \vec == 0xea0
154                 li      r10,PACA_IRQ_EE
155                 .elseif \vec == 0x900
156                 li      r10,PACA_IRQ_DEC
157                 .elseif \vec == 0xa00 || \vec == 0xe80
158                 li      r10,PACA_IRQ_DBELL
159                 .elseif \vec == 0xe60
160                 li      r10,PACA_IRQ_HMI
161                 .elseif \vec == 0xf00
162                 li      r10,PACA_IRQ_PMI
163                 .else
164                 .abort "Bad maskable vector"
165                 .endif
166
167                 .if \hsrr
168                 bne     masked_Hinterrupt
169                 .else
170                 bne     masked_interrupt
171                 .endif
172         .endif
173
174         std     r11,\area\()+EX_R11(r13)
175         std     r12,\area\()+EX_R12(r13)
176
177         /*
178          * DAR/DSISR, SCRATCH0 must be read before setting MSR[RI],
179          * because a d-side MCE will clobber those registers so is
180          * not recoverable if they are live.
181          */
182         GET_SCRATCH0(r10)
183         std     r10,\area\()+EX_R13(r13)
184         .if \dar
185         mfspr   r10,SPRN_DAR
186         std     r10,\area\()+EX_DAR(r13)
187         .endif
188         .if \dsisr
189         mfspr   r10,SPRN_DSISR
190         stw     r10,\area\()+EX_DSISR(r13)
191         .endif
192 .endm
193
194 .macro EXCEPTION_PROLOG_2_REAL label, hsrr, set_ri
195         ld      r10,PACAKMSR(r13)       /* get MSR value for kernel */
196         .if ! \set_ri
197         xori    r10,r10,MSR_RI          /* Clear MSR_RI */
198         .endif
199         .if \hsrr
200         mfspr   r11,SPRN_HSRR0          /* save HSRR0 */
201         mfspr   r12,SPRN_HSRR1          /* and HSRR1 */
202         mtspr   SPRN_HSRR1,r10
203         .else
204         mfspr   r11,SPRN_SRR0           /* save SRR0 */
205         mfspr   r12,SPRN_SRR1           /* and SRR1 */
206         mtspr   SPRN_SRR1,r10
207         .endif
208         LOAD_HANDLER(r10, \label\())
209         .if \hsrr
210         mtspr   SPRN_HSRR0,r10
211         HRFI_TO_KERNEL
212         .else
213         mtspr   SPRN_SRR0,r10
214         RFI_TO_KERNEL
215         .endif
216         b       .       /* prevent speculative execution */
217 .endm
218
219 .macro EXCEPTION_PROLOG_2_VIRT label, hsrr
220 #ifdef CONFIG_RELOCATABLE
221         .if \hsrr
222         mfspr   r11,SPRN_HSRR0  /* save HSRR0 */
223         .else
224         mfspr   r11,SPRN_SRR0   /* save SRR0 */
225         .endif
226         LOAD_HANDLER(r12, \label\())
227         mtctr   r12
228         .if \hsrr
229         mfspr   r12,SPRN_HSRR1  /* and HSRR1 */
230         .else
231         mfspr   r12,SPRN_SRR1   /* and HSRR1 */
232         .endif
233         li      r10,MSR_RI
234         mtmsrd  r10,1           /* Set RI (EE=0) */
235         bctr
236 #else
237         .if \hsrr
238         mfspr   r11,SPRN_HSRR0          /* save HSRR0 */
239         mfspr   r12,SPRN_HSRR1          /* and HSRR1 */
240         .else
241         mfspr   r11,SPRN_SRR0           /* save SRR0 */
242         mfspr   r12,SPRN_SRR1           /* and SRR1 */
243         .endif
244         li      r10,MSR_RI
245         mtmsrd  r10,1                   /* Set RI (EE=0) */
246         b       \label
247 #endif
248 .endm
249
250 /*
251  * Branch to label using its 0xC000 address. This results in instruction
252  * address suitable for MSR[IR]=0 or 1, which allows relocation to be turned
253  * on using mtmsr rather than rfid.
254  *
255  * This could set the 0xc bits for !RELOCATABLE as an immediate, rather than
256  * load KBASE for a slight optimisation.
257  */
258 #define BRANCH_TO_C000(reg, label)                                      \
259         __LOAD_HANDLER(reg, label);                                     \
260         mtctr   reg;                                                    \
261         bctr
262
263 #ifdef CONFIG_RELOCATABLE
264 #define BRANCH_LINK_TO_FAR(label)                                       \
265         __LOAD_FAR_HANDLER(r12, label);                                 \
266         mtctr   r12;                                                    \
267         bctrl
268
269 #else
270 #define BRANCH_LINK_TO_FAR(label)                                       \
271         bl      label
272 #endif
273
274 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
275 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
276 /*
277  * If hv is possible, interrupts come into to the hv version
278  * of the kvmppc_interrupt code, which then jumps to the PR handler,
279  * kvmppc_interrupt_pr, if the guest is a PR guest.
280  */
281 #define kvmppc_interrupt kvmppc_interrupt_hv
282 #else
283 #define kvmppc_interrupt kvmppc_interrupt_pr
284 #endif
285
286 .macro KVMTEST hsrr, n
287         lbz     r10,HSTATE_IN_GUEST(r13)
288         cmpwi   r10,0
289         .if \hsrr
290         bne     do_kvm_H\n
291         .else
292         bne     do_kvm_\n
293         .endif
294 .endm
295
296 .macro KVM_HANDLER area, hsrr, n, skip
297         .if \skip
298         cmpwi   r10,KVM_GUEST_MODE_SKIP
299         beq     89f
300         .else
301 BEGIN_FTR_SECTION_NESTED(947)
302         ld      r10,\area+EX_CFAR(r13)
303         std     r10,HSTATE_CFAR(r13)
304 END_FTR_SECTION_NESTED(CPU_FTR_CFAR,CPU_FTR_CFAR,947)
305         .endif
306
307 BEGIN_FTR_SECTION_NESTED(948)
308         ld      r10,\area+EX_PPR(r13)
309         std     r10,HSTATE_PPR(r13)
310 END_FTR_SECTION_NESTED(CPU_FTR_HAS_PPR,CPU_FTR_HAS_PPR,948)
311         ld      r10,\area+EX_R10(r13)
312         std     r12,HSTATE_SCRATCH0(r13)
313         sldi    r12,r9,32
314         /* HSRR variants have the 0x2 bit added to their trap number */
315         .if \hsrr
316         ori     r12,r12,(\n + 0x2)
317         .else
318         ori     r12,r12,(\n)
319         .endif
320
321 #ifdef CONFIG_RELOCATABLE
322         /*
323          * KVM requires __LOAD_FAR_HANDLER beause kvmppc_interrupt lives
324          * outside the head section. CONFIG_RELOCATABLE KVM expects CTR
325          * to be saved in HSTATE_SCRATCH1.
326          */
327         mfctr   r9
328         std     r9,HSTATE_SCRATCH1(r13)
329         __LOAD_FAR_HANDLER(r9, kvmppc_interrupt)
330         mtctr   r9
331         ld      r9,\area+EX_R9(r13)
332         bctr
333 #else
334         ld      r9,\area+EX_R9(r13)
335         b       kvmppc_interrupt
336 #endif
337
338
339         .if \skip
340 89:     mtocrf  0x80,r9
341         ld      r9,\area+EX_R9(r13)
342         ld      r10,\area+EX_R10(r13)
343         .if \hsrr
344         b       kvmppc_skip_Hinterrupt
345         .else
346         b       kvmppc_skip_interrupt
347         .endif
348         .endif
349 .endm
350
351 #else
352 .macro KVMTEST hsrr, n
353 .endm
354 .macro KVM_HANDLER area, hsrr, n, skip
355 .endm
356 #endif
357
358 #define EXCEPTION_PROLOG_COMMON_1()                                        \
359         std     r9,_CCR(r1);            /* save CR in stackframe        */ \
360         std     r11,_NIP(r1);           /* save SRR0 in stackframe      */ \
361         std     r12,_MSR(r1);           /* save SRR1 in stackframe      */ \
362         std     r10,0(r1);              /* make stack chain pointer     */ \
363         std     r0,GPR0(r1);            /* save r0 in stackframe        */ \
364         std     r10,GPR1(r1);           /* save r1 in stackframe        */ \
365
366 /* Save original regs values from save area to stack frame. */
367 #define EXCEPTION_PROLOG_COMMON_2(area)                                    \
368         ld      r9,area+EX_R9(r13);     /* move r9, r10 to stackframe   */ \
369         ld      r10,area+EX_R10(r13);                                      \
370         std     r9,GPR9(r1);                                               \
371         std     r10,GPR10(r1);                                             \
372         ld      r9,area+EX_R11(r13);    /* move r11 - r13 to stackframe */ \
373         ld      r10,area+EX_R12(r13);                                      \
374         ld      r11,area+EX_R13(r13);                                      \
375         std     r9,GPR11(r1);                                              \
376         std     r10,GPR12(r1);                                             \
377         std     r11,GPR13(r1);                                             \
378 BEGIN_FTR_SECTION_NESTED(66);                                              \
379         ld      r10,area+EX_CFAR(r13);                                     \
380         std     r10,ORIG_GPR3(r1);                                         \
381 END_FTR_SECTION_NESTED(CPU_FTR_CFAR, CPU_FTR_CFAR, 66);                    \
382         GET_CTR(r10, area);                                                \
383         std     r10,_CTR(r1);
384
385 #define EXCEPTION_PROLOG_COMMON_3(trap)                                    \
386         std     r2,GPR2(r1);            /* save r2 in stackframe        */ \
387         SAVE_4GPRS(3, r1);              /* save r3 - r6 in stackframe   */ \
388         SAVE_2GPRS(7, r1);              /* save r7, r8 in stackframe    */ \
389         mflr    r9;                     /* Get LR, later save to stack  */ \
390         ld      r2,PACATOC(r13);        /* get kernel TOC into r2       */ \
391         std     r9,_LINK(r1);                                              \
392         lbz     r10,PACAIRQSOFTMASK(r13);                                  \
393         mfspr   r11,SPRN_XER;           /* save XER in stackframe       */ \
394         std     r10,SOFTE(r1);                                             \
395         std     r11,_XER(r1);                                              \
396         li      r9,(trap)+1;                                               \
397         std     r9,_TRAP(r1);           /* set trap number              */ \
398         li      r10,0;                                                     \
399         ld      r11,exception_marker@toc(r2);                              \
400         std     r10,RESULT(r1);         /* clear regs->result           */ \
401         std     r11,STACK_FRAME_OVERHEAD-16(r1); /* mark the frame      */
402
403 /*
404  * On entry r13 points to the paca, r9-r13 are saved in the paca,
405  * r9 contains the saved CR, r11 and r12 contain the saved SRR0 and
406  * SRR1, and relocation is on.
407  */
408 #define EXCEPTION_COMMON(area, trap)                                       \
409         andi.   r10,r12,MSR_PR;         /* See if coming from user      */ \
410         mr      r10,r1;                 /* Save r1                      */ \
411         subi    r1,r1,INT_FRAME_SIZE;   /* alloc frame on kernel stack  */ \
412         beq-    1f;                                                        \
413         ld      r1,PACAKSAVE(r13);      /* kernel stack to use          */ \
414 1:      tdgei   r1,-INT_FRAME_SIZE;     /* trap if r1 is in userspace   */ \
415         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0;                             \
416 3:      EXCEPTION_PROLOG_COMMON_1();                                       \
417         kuap_save_amr_and_lock r9, r10, cr1, cr0;                          \
418         beq     4f;                     /* if from kernel mode          */ \
419         ACCOUNT_CPU_USER_ENTRY(r13, r9, r10);                              \
420         SAVE_PPR(area, r9);                                                \
421 4:      EXCEPTION_PROLOG_COMMON_2(area);                                   \
422         EXCEPTION_PROLOG_COMMON_3(trap);                                   \
423         ACCOUNT_STOLEN_TIME
424
425 /*
426  * Exception where stack is already set in r1, r1 is saved in r10.
427  * PPR save and CPU accounting is not done (for some reason).
428  */
429 #define EXCEPTION_COMMON_STACK(area, trap)                      \
430         EXCEPTION_PROLOG_COMMON_1();                            \
431         kuap_save_amr_and_lock r9, r10, cr1;                    \
432         EXCEPTION_PROLOG_COMMON_2(area);                        \
433         EXCEPTION_PROLOG_COMMON_3(trap)
434
435 /*
436  * Restore all registers including H/SRR0/1 saved in a stack frame of a
437  * standard exception.
438  */
439 .macro EXCEPTION_RESTORE_REGS hsrr
440         /* Move original SRR0 and SRR1 into the respective regs */
441         ld      r9,_MSR(r1)
442         .if \hsrr
443         mtspr   SPRN_HSRR1,r9
444         .else
445         mtspr   SPRN_SRR1,r9
446         .endif
447         ld      r9,_NIP(r1)
448         .if \hsrr
449         mtspr   SPRN_HSRR0,r9
450         .else
451         mtspr   SPRN_SRR0,r9
452         .endif
453         ld      r9,_CTR(r1)
454         mtctr   r9
455         ld      r9,_XER(r1)
456         mtxer   r9
457         ld      r9,_LINK(r1)
458         mtlr    r9
459         ld      r9,_CCR(r1)
460         mtcr    r9
461         REST_8GPRS(2, r1)
462         REST_4GPRS(10, r1)
463         REST_GPR(0, r1)
464         /* restore original r1. */
465         ld      r1,GPR1(r1)
466 .endm
467
468 #define RUNLATCH_ON                             \
469 BEGIN_FTR_SECTION                               \
470         ld      r3, PACA_THREAD_INFO(r13);      \
471         ld      r4,TI_LOCAL_FLAGS(r3);          \
472         andi.   r0,r4,_TLF_RUNLATCH;            \
473         beql    ppc64_runlatch_on_trampoline;   \
474 END_FTR_SECTION_IFSET(CPU_FTR_CTRL)
475
476 /*
477  * When the idle code in power4_idle puts the CPU into NAP mode,
478  * it has to do so in a loop, and relies on the external interrupt
479  * and decrementer interrupt entry code to get it out of the loop.
480  * It sets the _TLF_NAPPING bit in current_thread_info()->local_flags
481  * to signal that it is in the loop and needs help to get out.
482  */
483 #ifdef CONFIG_PPC_970_NAP
484 #define FINISH_NAP                              \
485 BEGIN_FTR_SECTION                               \
486         ld      r11, PACA_THREAD_INFO(r13);     \
487         ld      r9,TI_LOCAL_FLAGS(r11);         \
488         andi.   r10,r9,_TLF_NAPPING;            \
489         bnel    power4_fixup_nap;               \
490 END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)
491 #else
492 #define FINISH_NAP
493 #endif
494
495 /*
496  * Following are the BOOK3S exception handler helper macros.
497  * Handlers come in a number of types, and each type has a number of varieties.
498  *
499  * EXC_REAL_*     - real, unrelocated exception vectors
500  * EXC_VIRT_*     - virt (AIL), unrelocated exception vectors
501  * TRAMP_REAL_*   - real, unrelocated helpers (virt can call these)
502  * TRAMP_VIRT_*   - virt, unreloc helpers (in practice, real can use)
503  * TRAMP_KVM      - KVM handlers that get put into real, unrelocated
504  * EXC_COMMON     - virt, relocated common handlers
505  *
506  * The EXC handlers are given a name, and branch to name_common, or the
507  * appropriate KVM or masking function. Vector handler verieties are as
508  * follows:
509  *
510  * EXC_{REAL|VIRT}_BEGIN/END - used to open-code the exception
511  *
512  * EXC_{REAL|VIRT}  - standard exception
513  *
514  * EXC_{REAL|VIRT}_suffix
515  *     where _suffix is:
516  *   - _MASKABLE               - maskable exception
517  *   - _OOL                    - out of line with trampoline to common handler
518  *   - _HV                     - HV exception
519  *
520  * There can be combinations, e.g., EXC_VIRT_OOL_MASKABLE_HV
521  *
522  * The one unusual case is __EXC_REAL_OOL_HV_DIRECT, which is
523  * an OOL vector that branches to a specified handler rather than the usual
524  * trampoline that goes to common. It, and other underscore macros, should
525  * be used with care.
526  *
527  * KVM handlers come in the following verieties:
528  * TRAMP_KVM
529  * TRAMP_KVM_SKIP
530  * TRAMP_KVM_HV
531  * TRAMP_KVM_HV_SKIP
532  *
533  * COMMON handlers come in the following verieties:
534  * EXC_COMMON_BEGIN/END - used to open-code the handler
535  * EXC_COMMON
536  * EXC_COMMON_ASYNC
537  *
538  * TRAMP_REAL and TRAMP_VIRT can be used with BEGIN/END. KVM
539  * and OOL handlers are implemented as types of TRAMP and TRAMP_VIRT handlers.
540  */
541
542 #define __EXC_REAL(name, start, size, area)                             \
543         EXC_REAL_BEGIN(name, start, size);                              \
544         EXCEPTION_PROLOG_0 area ;                                       \
545         EXCEPTION_PROLOG_1 EXC_STD, area, 1, start, 0, 0, 0 ;           \
546         EXCEPTION_PROLOG_2_REAL name##_common, EXC_STD, 1 ;             \
547         EXC_REAL_END(name, start, size)
548
549 #define EXC_REAL(name, start, size)                                     \
550         __EXC_REAL(name, start, size, PACA_EXGEN)
551
552 #define __EXC_VIRT(name, start, size, realvec, area)                    \
553         EXC_VIRT_BEGIN(name, start, size);                              \
554         EXCEPTION_PROLOG_0 area ;                                       \
555         EXCEPTION_PROLOG_1 EXC_STD, area, 0, realvec, 0, 0, 0;          \
556         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_STD ;                \
557         EXC_VIRT_END(name, start, size)
558
559 #define EXC_VIRT(name, start, size, realvec)                            \
560         __EXC_VIRT(name, start, size, realvec, PACA_EXGEN)
561
562 #define EXC_REAL_MASKABLE(name, start, size, bitmask)                   \
563         EXC_REAL_BEGIN(name, start, size);                              \
564         EXCEPTION_PROLOG_0 PACA_EXGEN ;                                 \
565         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, start, 0, 0, bitmask ; \
566         EXCEPTION_PROLOG_2_REAL name##_common, EXC_STD, 1 ;             \
567         EXC_REAL_END(name, start, size)
568
569 #define EXC_VIRT_MASKABLE(name, start, size, realvec, bitmask)          \
570         EXC_VIRT_BEGIN(name, start, size);                              \
571         EXCEPTION_PROLOG_0 PACA_EXGEN ;                                 \
572         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 0, realvec, 0, 0, bitmask ; \
573         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_STD ;                \
574         EXC_VIRT_END(name, start, size)
575
576 #define EXC_REAL_HV(name, start, size)                                  \
577         EXC_REAL_BEGIN(name, start, size);                              \
578         EXCEPTION_PROLOG_0 PACA_EXGEN;                                  \
579         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, start, 0, 0, 0 ;      \
580         EXCEPTION_PROLOG_2_REAL name##_common, EXC_HV, 1 ;              \
581         EXC_REAL_END(name, start, size)
582
583 #define EXC_VIRT_HV(name, start, size, realvec)                         \
584         EXC_VIRT_BEGIN(name, start, size);                              \
585         EXCEPTION_PROLOG_0 PACA_EXGEN;                                  \
586         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, realvec, 0, 0, 0 ;    \
587         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_HV ;                 \
588         EXC_VIRT_END(name, start, size)
589
590 #define __EXC_REAL_OOL(name, start, size)                               \
591         EXC_REAL_BEGIN(name, start, size);                              \
592         EXCEPTION_PROLOG_0 PACA_EXGEN ;                                 \
593         b       tramp_real_##name ;                                     \
594         EXC_REAL_END(name, start, size)
595
596 #define __TRAMP_REAL_OOL(name, vec)                                     \
597         TRAMP_REAL_BEGIN(tramp_real_##name);                            \
598         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, vec, 0, 0, 0 ;       \
599         EXCEPTION_PROLOG_2_REAL name##_common, EXC_STD, 1
600
601 #define EXC_REAL_OOL(name, start, size)                                 \
602         __EXC_REAL_OOL(name, start, size);                              \
603         __TRAMP_REAL_OOL(name, start)
604
605 #define __EXC_REAL_OOL_MASKABLE(name, start, size)                      \
606         __EXC_REAL_OOL(name, start, size)
607
608 #define __TRAMP_REAL_OOL_MASKABLE(name, vec, bitmask)                   \
609         TRAMP_REAL_BEGIN(tramp_real_##name);                            \
610         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, vec, 0, 0, bitmask ; \
611         EXCEPTION_PROLOG_2_REAL name##_common, EXC_STD, 1
612
613 #define EXC_REAL_OOL_MASKABLE(name, start, size, bitmask)               \
614         __EXC_REAL_OOL_MASKABLE(name, start, size);                     \
615         __TRAMP_REAL_OOL_MASKABLE(name, start, bitmask)
616
617 #define __EXC_REAL_OOL_HV_DIRECT(name, start, size, handler)            \
618         EXC_REAL_BEGIN(name, start, size);                              \
619         EXCEPTION_PROLOG_0 PACA_EXGEN ;                                 \
620         b       handler;                                                \
621         EXC_REAL_END(name, start, size)
622
623 #define __EXC_REAL_OOL_HV(name, start, size)                            \
624         __EXC_REAL_OOL(name, start, size)
625
626 #define __TRAMP_REAL_OOL_HV(name, vec)                                  \
627         TRAMP_REAL_BEGIN(tramp_real_##name);                            \
628         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, vec, 0, 0, 0 ;        \
629         EXCEPTION_PROLOG_2_REAL name##_common, EXC_HV, 1
630
631 #define EXC_REAL_OOL_HV(name, start, size)                              \
632         __EXC_REAL_OOL_HV(name, start, size);                           \
633         __TRAMP_REAL_OOL_HV(name, start)
634
635 #define __EXC_REAL_OOL_MASKABLE_HV(name, start, size)                   \
636         __EXC_REAL_OOL(name, start, size)
637
638 #define __TRAMP_REAL_OOL_MASKABLE_HV(name, vec, bitmask)                \
639         TRAMP_REAL_BEGIN(tramp_real_##name);                            \
640         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, vec, 0, 0, bitmask ;  \
641         EXCEPTION_PROLOG_2_REAL name##_common, EXC_HV, 1
642
643 #define EXC_REAL_OOL_MASKABLE_HV(name, start, size, bitmask)            \
644         __EXC_REAL_OOL_MASKABLE_HV(name, start, size);                  \
645         __TRAMP_REAL_OOL_MASKABLE_HV(name, start, bitmask)
646
647 #define __EXC_VIRT_OOL(name, start, size)                               \
648         EXC_VIRT_BEGIN(name, start, size);                              \
649         EXCEPTION_PROLOG_0 PACA_EXGEN ;                                 \
650         b       tramp_virt_##name;                                      \
651         EXC_VIRT_END(name, start, size)
652
653 #define __TRAMP_VIRT_OOL(name, realvec)                                 \
654         TRAMP_VIRT_BEGIN(tramp_virt_##name);                            \
655         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 0, vec, 0, 0, 0 ;       \
656         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_STD
657
658 #define EXC_VIRT_OOL(name, start, size, realvec)                        \
659         __EXC_VIRT_OOL(name, start, size);                              \
660         __TRAMP_VIRT_OOL(name, realvec)
661
662 #define __EXC_VIRT_OOL_MASKABLE(name, start, size)                      \
663         __EXC_VIRT_OOL(name, start, size)
664
665 #define __TRAMP_VIRT_OOL_MASKABLE(name, realvec, bitmask)               \
666         TRAMP_VIRT_BEGIN(tramp_virt_##name);                            \
667         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 0, realvec, 0, 0, bitmask ; \
668         EXCEPTION_PROLOG_2_REAL name##_common, EXC_STD, 1
669
670 #define EXC_VIRT_OOL_MASKABLE(name, start, size, realvec, bitmask)      \
671         __EXC_VIRT_OOL_MASKABLE(name, start, size);                     \
672         __TRAMP_VIRT_OOL_MASKABLE(name, realvec, bitmask)
673
674 #define __EXC_VIRT_OOL_HV(name, start, size)                            \
675         __EXC_VIRT_OOL(name, start, size)
676
677 #define __TRAMP_VIRT_OOL_HV(name, realvec)                              \
678         TRAMP_VIRT_BEGIN(tramp_virt_##name);                            \
679         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, realvec, 0, 0, 0 ;    \
680         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_HV
681
682 #define EXC_VIRT_OOL_HV(name, start, size, realvec)                     \
683         __EXC_VIRT_OOL_HV(name, start, size);                           \
684         __TRAMP_VIRT_OOL_HV(name, realvec)
685
686 #define __EXC_VIRT_OOL_MASKABLE_HV(name, start, size)                   \
687         __EXC_VIRT_OOL(name, start, size)
688
689 #define __TRAMP_VIRT_OOL_MASKABLE_HV(name, realvec, bitmask)            \
690         TRAMP_VIRT_BEGIN(tramp_virt_##name);                            \
691         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, realvec, 0, 0, bitmask ; \
692         EXCEPTION_PROLOG_2_VIRT name##_common, EXC_HV
693
694 #define EXC_VIRT_OOL_MASKABLE_HV(name, start, size, realvec, bitmask)   \
695         __EXC_VIRT_OOL_MASKABLE_HV(name, start, size);                  \
696         __TRAMP_VIRT_OOL_MASKABLE_HV(name, realvec, bitmask)
697
698 #define TRAMP_KVM(area, n)                                              \
699         TRAMP_KVM_BEGIN(do_kvm_##n);                                    \
700         KVM_HANDLER area, EXC_STD, n, 0
701
702 #define TRAMP_KVM_SKIP(area, n)                                         \
703         TRAMP_KVM_BEGIN(do_kvm_##n);                                    \
704         KVM_HANDLER area, EXC_STD, n, 1
705
706 #define TRAMP_KVM_HV(area, n)                                           \
707         TRAMP_KVM_BEGIN(do_kvm_H##n);                                   \
708         KVM_HANDLER area, EXC_HV, n, 0
709
710 #define TRAMP_KVM_HV_SKIP(area, n)                                      \
711         TRAMP_KVM_BEGIN(do_kvm_H##n);                                   \
712         KVM_HANDLER area, EXC_HV, n, 1
713
714 #define EXC_COMMON(name, realvec, hdlr)                                 \
715         EXC_COMMON_BEGIN(name);                                         \
716         EXCEPTION_COMMON(PACA_EXGEN, realvec);                          \
717         bl      save_nvgprs;                                            \
718         RECONCILE_IRQ_STATE(r10, r11);                                  \
719         addi    r3,r1,STACK_FRAME_OVERHEAD;                             \
720         bl      hdlr;                                                   \
721         b       ret_from_except
722
723 /*
724  * Like EXC_COMMON, but for exceptions that can occur in the idle task and
725  * therefore need the special idle handling (finish nap and runlatch)
726  */
727 #define EXC_COMMON_ASYNC(name, realvec, hdlr)                           \
728         EXC_COMMON_BEGIN(name);                                         \
729         EXCEPTION_COMMON(PACA_EXGEN, realvec);                          \
730         FINISH_NAP;                                                     \
731         RECONCILE_IRQ_STATE(r10, r11);                                  \
732         RUNLATCH_ON;                                                    \
733         addi    r3,r1,STACK_FRAME_OVERHEAD;                             \
734         bl      hdlr;                                                   \
735         b       ret_from_except_lite
736
737
738 /*
739  * There are a few constraints to be concerned with.
740  * - Real mode exceptions code/data must be located at their physical location.
741  * - Virtual mode exceptions must be mapped at their 0xc000... location.
742  * - Fixed location code must not call directly beyond the __end_interrupts
743  *   area when built with CONFIG_RELOCATABLE. LOAD_HANDLER / bctr sequence
744  *   must be used.
745  * - LOAD_HANDLER targets must be within first 64K of physical 0 /
746  *   virtual 0xc00...
747  * - Conditional branch targets must be within +/-32K of caller.
748  *
749  * "Virtual exceptions" run with relocation on (MSR_IR=1, MSR_DR=1), and
750  * therefore don't have to run in physically located code or rfid to
751  * virtual mode kernel code. However on relocatable kernels they do have
752  * to branch to KERNELBASE offset because the rest of the kernel (outside
753  * the exception vectors) may be located elsewhere.
754  *
755  * Virtual exceptions correspond with physical, except their entry points
756  * are offset by 0xc000000000000000 and also tend to get an added 0x4000
757  * offset applied. Virtual exceptions are enabled with the Alternate
758  * Interrupt Location (AIL) bit set in the LPCR. However this does not
759  * guarantee they will be delivered virtually. Some conditions (see the ISA)
760  * cause exceptions to be delivered in real mode.
761  *
762  * It's impossible to receive interrupts below 0x300 via AIL.
763  *
764  * KVM: None of the virtual exceptions are from the guest. Anything that
765  * escalated to HV=1 from HV=0 is delivered via real mode handlers.
766  *
767  *
768  * We layout physical memory as follows:
769  * 0x0000 - 0x00ff : Secondary processor spin code
770  * 0x0100 - 0x18ff : Real mode pSeries interrupt vectors
771  * 0x1900 - 0x3fff : Real mode trampolines
772  * 0x4000 - 0x58ff : Relon (IR=1,DR=1) mode pSeries interrupt vectors
773  * 0x5900 - 0x6fff : Relon mode trampolines
774  * 0x7000 - 0x7fff : FWNMI data area
775  * 0x8000 -   .... : Common interrupt handlers, remaining early
776  *                   setup code, rest of kernel.
777  *
778  * We could reclaim 0x4000-0x42ff for real mode trampolines if the space
779  * is necessary. Until then it's more consistent to explicitly put VIRT_NONE
780  * vectors there.
781  */
782 OPEN_FIXED_SECTION(real_vectors,        0x0100, 0x1900)
783 OPEN_FIXED_SECTION(real_trampolines,    0x1900, 0x4000)
784 OPEN_FIXED_SECTION(virt_vectors,        0x4000, 0x5900)
785 OPEN_FIXED_SECTION(virt_trampolines,    0x5900, 0x7000)
786
787 #ifdef CONFIG_PPC_POWERNV
788         .globl start_real_trampolines
789         .globl end_real_trampolines
790         .globl start_virt_trampolines
791         .globl end_virt_trampolines
792 #endif
793
794 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
795 /*
796  * Data area reserved for FWNMI option.
797  * This address (0x7000) is fixed by the RPA.
798  * pseries and powernv need to keep the whole page from
799  * 0x7000 to 0x8000 free for use by the firmware
800  */
801 ZERO_FIXED_SECTION(fwnmi_page,          0x7000, 0x8000)
802 OPEN_TEXT_SECTION(0x8000)
803 #else
804 OPEN_TEXT_SECTION(0x7000)
805 #endif
806
807 USE_FIXED_SECTION(real_vectors)
808
809 /*
810  * This is the start of the interrupt handlers for pSeries
811  * This code runs with relocation off.
812  * Code from here to __end_interrupts gets copied down to real
813  * address 0x100 when we are running a relocatable kernel.
814  * Therefore any relative branches in this section must only
815  * branch to labels in this section.
816  */
817         .globl __start_interrupts
818 __start_interrupts:
819
820 /* No virt vectors corresponding with 0x0..0x100 */
821 EXC_VIRT_NONE(0x4000, 0x100)
822
823
824 EXC_REAL_BEGIN(system_reset, 0x100, 0x100)
825         EXCEPTION_PROLOG_0 PACA_EXNMI
826
827         /* This is EXCEPTION_PROLOG_1 with the idle feature section added */
828         OPT_SAVE_REG_TO_PACA(PACA_EXNMI+EX_PPR, r9, CPU_FTR_HAS_PPR)
829         OPT_SAVE_REG_TO_PACA(PACA_EXNMI+EX_CFAR, r10, CPU_FTR_CFAR)
830         INTERRUPT_TO_KERNEL
831         SAVE_CTR(r10, PACA_EXNMI)
832         mfcr    r9
833
834 #ifdef CONFIG_PPC_P7_NAP
835         /*
836          * If running native on arch 2.06 or later, check if we are waking up
837          * from nap/sleep/winkle, and branch to idle handler. This tests SRR1
838          * bits 46:47. A non-0 value indicates that we are coming from a power
839          * saving state. The idle wakeup handler initially runs in real mode,
840          * but we branch to the 0xc000... address so we can turn on relocation
841          * with mtmsr.
842          */
843 BEGIN_FTR_SECTION
844         mfspr   r10,SPRN_SRR1
845         rlwinm. r10,r10,47-31,30,31
846         beq-    1f
847         cmpwi   cr1,r10,2
848         mfspr   r3,SPRN_SRR1
849         bltlr   cr1     /* no state loss, return to idle caller */
850         BRANCH_TO_C000(r10, system_reset_idle_common)
851 1:
852 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
853 #endif
854
855         KVMTEST EXC_STD 0x100
856         std     r11,PACA_EXNMI+EX_R11(r13)
857         std     r12,PACA_EXNMI+EX_R12(r13)
858         GET_SCRATCH0(r10)
859         std     r10,PACA_EXNMI+EX_R13(r13)
860
861         EXCEPTION_PROLOG_2_REAL system_reset_common, EXC_STD, 0
862         /*
863          * MSR_RI is not enabled, because PACA_EXNMI and nmi stack is
864          * being used, so a nested NMI exception would corrupt it.
865          */
866
867 EXC_REAL_END(system_reset, 0x100, 0x100)
868 EXC_VIRT_NONE(0x4100, 0x100)
869 TRAMP_KVM(PACA_EXNMI, 0x100)
870
871 #ifdef CONFIG_PPC_P7_NAP
872 EXC_COMMON_BEGIN(system_reset_idle_common)
873         /*
874          * This must be a direct branch (without linker branch stub) because
875          * we can not use TOC at this point as r2 may not be restored yet.
876          */
877         b       idle_return_gpr_loss
878 #endif
879
880 EXC_COMMON_BEGIN(system_reset_common)
881         /*
882          * Increment paca->in_nmi then enable MSR_RI. SLB or MCE will be able
883          * to recover, but nested NMI will notice in_nmi and not recover
884          * because of the use of the NMI stack. in_nmi reentrancy is tested in
885          * system_reset_exception.
886          */
887         lhz     r10,PACA_IN_NMI(r13)
888         addi    r10,r10,1
889         sth     r10,PACA_IN_NMI(r13)
890         li      r10,MSR_RI
891         mtmsrd  r10,1
892
893         mr      r10,r1
894         ld      r1,PACA_NMI_EMERG_SP(r13)
895         subi    r1,r1,INT_FRAME_SIZE
896         EXCEPTION_COMMON_STACK(PACA_EXNMI, 0x100)
897         bl      save_nvgprs
898         /*
899          * Set IRQS_ALL_DISABLED unconditionally so arch_irqs_disabled does
900          * the right thing. We do not want to reconcile because that goes
901          * through irq tracing which we don't want in NMI.
902          *
903          * Save PACAIRQHAPPENED because some code will do a hard disable
904          * (e.g., xmon). So we want to restore this back to where it was
905          * when we return. DAR is unused in the stack, so save it there.
906          */
907         li      r10,IRQS_ALL_DISABLED
908         stb     r10,PACAIRQSOFTMASK(r13)
909         lbz     r10,PACAIRQHAPPENED(r13)
910         std     r10,_DAR(r1)
911
912         addi    r3,r1,STACK_FRAME_OVERHEAD
913         bl      system_reset_exception
914
915         /* Clear MSR_RI before setting SRR0 and SRR1. */
916         li      r9,0
917         mtmsrd  r9,1
918
919         /*
920          * MSR_RI is clear, now we can decrement paca->in_nmi.
921          */
922         lhz     r10,PACA_IN_NMI(r13)
923         subi    r10,r10,1
924         sth     r10,PACA_IN_NMI(r13)
925
926         /*
927          * Restore soft mask settings.
928          */
929         ld      r10,_DAR(r1)
930         stb     r10,PACAIRQHAPPENED(r13)
931         ld      r10,SOFTE(r1)
932         stb     r10,PACAIRQSOFTMASK(r13)
933
934         EXCEPTION_RESTORE_REGS EXC_STD
935         RFI_TO_USER_OR_KERNEL
936
937 #ifdef CONFIG_PPC_PSERIES
938 /*
939  * Vectors for the FWNMI option.  Share common code.
940  */
941 TRAMP_REAL_BEGIN(system_reset_fwnmi)
942         /* See comment at system_reset exception, don't turn on RI */
943         EXCEPTION_PROLOG_0 PACA_EXNMI
944         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXNMI, 0, 0x100, 0, 0, 0
945         EXCEPTION_PROLOG_2_REAL system_reset_common, EXC_STD, 0
946
947 #endif /* CONFIG_PPC_PSERIES */
948
949
950 EXC_REAL_BEGIN(machine_check, 0x200, 0x100)
951         /* This is moved out of line as it can be patched by FW, but
952          * some code path might still want to branch into the original
953          * vector
954          */
955         EXCEPTION_PROLOG_0 PACA_EXMC
956 BEGIN_FTR_SECTION
957         b       machine_check_common_early
958 FTR_SECTION_ELSE
959         b       machine_check_pSeries_0
960 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
961 EXC_REAL_END(machine_check, 0x200, 0x100)
962 EXC_VIRT_NONE(0x4200, 0x100)
963 TRAMP_REAL_BEGIN(machine_check_common_early)
964         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXMC, 0, 0x200, 0, 0, 0
965         /*
966          * Register contents:
967          * R13          = PACA
968          * R9           = CR
969          * Original R9 to R13 is saved on PACA_EXMC
970          *
971          * Switch to mc_emergency stack and handle re-entrancy (we limit
972          * the nested MCE upto level 4 to avoid stack overflow).
973          * Save MCE registers srr1, srr0, dar and dsisr and then set ME=1
974          *
975          * We use paca->in_mce to check whether this is the first entry or
976          * nested machine check. We increment paca->in_mce to track nested
977          * machine checks.
978          *
979          * If this is the first entry then set stack pointer to
980          * paca->mc_emergency_sp, otherwise r1 is already pointing to
981          * stack frame on mc_emergency stack.
982          *
983          * NOTE: We are here with MSR_ME=0 (off), which means we risk a
984          * checkstop if we get another machine check exception before we do
985          * rfid with MSR_ME=1.
986          *
987          * This interrupt can wake directly from idle. If that is the case,
988          * the machine check is handled then the idle wakeup code is called
989          * to restore state.
990          */
991         mr      r11,r1                  /* Save r1 */
992         lhz     r10,PACA_IN_MCE(r13)
993         cmpwi   r10,0                   /* Are we in nested machine check */
994         bne     0f                      /* Yes, we are. */
995         /* First machine check entry */
996         ld      r1,PACAMCEMERGSP(r13)   /* Use MC emergency stack */
997 0:      subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame */
998         addi    r10,r10,1               /* increment paca->in_mce */
999         sth     r10,PACA_IN_MCE(r13)
1000         /* Limit nested MCE to level 4 to avoid stack overflow */
1001         cmpwi   r10,MAX_MCE_DEPTH
1002         bgt     2f                      /* Check if we hit limit of 4 */
1003         std     r11,GPR1(r1)            /* Save r1 on the stack. */
1004         std     r11,0(r1)               /* make stack chain pointer */
1005         mfspr   r11,SPRN_SRR0           /* Save SRR0 */
1006         std     r11,_NIP(r1)
1007         mfspr   r11,SPRN_SRR1           /* Save SRR1 */
1008         std     r11,_MSR(r1)
1009         mfspr   r11,SPRN_DAR            /* Save DAR */
1010         std     r11,_DAR(r1)
1011         mfspr   r11,SPRN_DSISR          /* Save DSISR */
1012         std     r11,_DSISR(r1)
1013         std     r9,_CCR(r1)             /* Save CR in stackframe */
1014         /* We don't touch AMR here, we never go to virtual mode */
1015         /* Save r9 through r13 from EXMC save area to stack frame. */
1016         EXCEPTION_PROLOG_COMMON_2(PACA_EXMC)
1017         mfmsr   r11                     /* get MSR value */
1018 BEGIN_FTR_SECTION
1019         ori     r11,r11,MSR_ME          /* turn on ME bit */
1020 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
1021         ori     r11,r11,MSR_RI          /* turn on RI bit */
1022         LOAD_HANDLER(r12, machine_check_handle_early)
1023 1:      mtspr   SPRN_SRR0,r12
1024         mtspr   SPRN_SRR1,r11
1025         RFI_TO_KERNEL
1026         b       .       /* prevent speculative execution */
1027 2:
1028         /* Stack overflow. Stay on emergency stack and panic.
1029          * Keep the ME bit off while panic-ing, so that if we hit
1030          * another machine check we checkstop.
1031          */
1032         addi    r1,r1,INT_FRAME_SIZE    /* go back to previous stack frame */
1033         ld      r11,PACAKMSR(r13)
1034         LOAD_HANDLER(r12, unrecover_mce)
1035         li      r10,MSR_ME
1036         andc    r11,r11,r10             /* Turn off MSR_ME */
1037         b       1b
1038         b       .       /* prevent speculative execution */
1039
1040 TRAMP_REAL_BEGIN(machine_check_pSeries)
1041         .globl machine_check_fwnmi
1042 machine_check_fwnmi:
1043         EXCEPTION_PROLOG_0 PACA_EXMC
1044 BEGIN_FTR_SECTION
1045         b       machine_check_common_early
1046 END_FTR_SECTION_IFCLR(CPU_FTR_HVMODE)
1047 machine_check_pSeries_0:
1048         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXMC, 1, 0x200, 1, 1, 0
1049         /*
1050          * MSR_RI is not enabled, because PACA_EXMC is being used, so a
1051          * nested machine check corrupts it. machine_check_common enables
1052          * MSR_RI.
1053          */
1054         EXCEPTION_PROLOG_2_REAL machine_check_common, EXC_STD, 0
1055
1056 TRAMP_KVM_SKIP(PACA_EXMC, 0x200)
1057
1058 EXC_COMMON_BEGIN(machine_check_common)
1059         /*
1060          * Machine check is different because we use a different
1061          * save area: PACA_EXMC instead of PACA_EXGEN.
1062          */
1063         EXCEPTION_COMMON(PACA_EXMC, 0x200)
1064         FINISH_NAP
1065         RECONCILE_IRQ_STATE(r10, r11)
1066         ld      r3,PACA_EXMC+EX_DAR(r13)
1067         lwz     r4,PACA_EXMC+EX_DSISR(r13)
1068         /* Enable MSR_RI when finished with PACA_EXMC */
1069         li      r10,MSR_RI
1070         mtmsrd  r10,1
1071         std     r3,_DAR(r1)
1072         std     r4,_DSISR(r1)
1073         bl      save_nvgprs
1074         addi    r3,r1,STACK_FRAME_OVERHEAD
1075         bl      machine_check_exception
1076         b       ret_from_except
1077
1078 #define MACHINE_CHECK_HANDLER_WINDUP                    \
1079         /* Clear MSR_RI before setting SRR0 and SRR1. */\
1080         li      r9,0;                                   \
1081         mtmsrd  r9,1;           /* Clear MSR_RI */      \
1082         /* Decrement paca->in_mce now RI is clear. */   \
1083         lhz     r12,PACA_IN_MCE(r13);                   \
1084         subi    r12,r12,1;                              \
1085         sth     r12,PACA_IN_MCE(r13);                   \
1086         EXCEPTION_RESTORE_REGS EXC_STD
1087
1088 #ifdef CONFIG_PPC_P7_NAP
1089 /*
1090  * This is an idle wakeup. Low level machine check has already been
1091  * done. Queue the event then call the idle code to do the wake up.
1092  */
1093 EXC_COMMON_BEGIN(machine_check_idle_common)
1094         bl      machine_check_queue_event
1095
1096         /*
1097          * We have not used any non-volatile GPRs here, and as a rule
1098          * most exception code including machine check does not.
1099          * Therefore PACA_NAPSTATELOST does not need to be set. Idle
1100          * wakeup will restore volatile registers.
1101          *
1102          * Load the original SRR1 into r3 for pnv_powersave_wakeup_mce.
1103          *
1104          * Then decrement MCE nesting after finishing with the stack.
1105          */
1106         ld      r3,_MSR(r1)
1107         ld      r4,_LINK(r1)
1108
1109         lhz     r11,PACA_IN_MCE(r13)
1110         subi    r11,r11,1
1111         sth     r11,PACA_IN_MCE(r13)
1112
1113         mtlr    r4
1114         rlwinm  r10,r3,47-31,30,31
1115         cmpwi   cr1,r10,2
1116         bltlr   cr1     /* no state loss, return to idle caller */
1117         b       idle_return_gpr_loss
1118 #endif
1119         /*
1120          * Handle machine check early in real mode. We come here with
1121          * ME=1, MMU (IR=0 and DR=0) off and using MC emergency stack.
1122          */
1123 EXC_COMMON_BEGIN(machine_check_handle_early)
1124         std     r0,GPR0(r1)     /* Save r0 */
1125         EXCEPTION_PROLOG_COMMON_3(0x200)
1126         bl      save_nvgprs
1127         addi    r3,r1,STACK_FRAME_OVERHEAD
1128         bl      machine_check_early
1129         std     r3,RESULT(r1)   /* Save result */
1130         ld      r12,_MSR(r1)
1131 BEGIN_FTR_SECTION
1132         b       4f
1133 END_FTR_SECTION_IFCLR(CPU_FTR_HVMODE)
1134
1135 #ifdef  CONFIG_PPC_P7_NAP
1136         /*
1137          * Check if thread was in power saving mode. We come here when any
1138          * of the following is true:
1139          * a. thread wasn't in power saving mode
1140          * b. thread was in power saving mode with no state loss,
1141          *    supervisor state loss or hypervisor state loss.
1142          *
1143          * Go back to nap/sleep/winkle mode again if (b) is true.
1144          */
1145 BEGIN_FTR_SECTION
1146         rlwinm. r11,r12,47-31,30,31
1147         bne     machine_check_idle_common
1148 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
1149 #endif
1150
1151         /*
1152          * Check if we are coming from hypervisor userspace. If yes then we
1153          * continue in host kernel in V mode to deliver the MC event.
1154          */
1155         rldicl. r11,r12,4,63            /* See if MC hit while in HV mode. */
1156         beq     5f
1157 4:      andi.   r11,r12,MSR_PR          /* See if coming from user. */
1158         bne     9f                      /* continue in V mode if we are. */
1159
1160 5:
1161 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1162 BEGIN_FTR_SECTION
1163         /*
1164          * We are coming from kernel context. Check if we are coming from
1165          * guest. if yes, then we can continue. We will fall through
1166          * do_kvm_200->kvmppc_interrupt to deliver the MC event to guest.
1167          */
1168         lbz     r11,HSTATE_IN_GUEST(r13)
1169         cmpwi   r11,0                   /* Check if coming from guest */
1170         bne     9f                      /* continue if we are. */
1171 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
1172 #endif
1173         /*
1174          * At this point we are not sure about what context we come from.
1175          * Queue up the MCE event and return from the interrupt.
1176          * But before that, check if this is an un-recoverable exception.
1177          * If yes, then stay on emergency stack and panic.
1178          */
1179         andi.   r11,r12,MSR_RI
1180         bne     2f
1181 1:      mfspr   r11,SPRN_SRR0
1182         LOAD_HANDLER(r10,unrecover_mce)
1183         mtspr   SPRN_SRR0,r10
1184         ld      r10,PACAKMSR(r13)
1185         /*
1186          * We are going down. But there are chances that we might get hit by
1187          * another MCE during panic path and we may run into unstable state
1188          * with no way out. Hence, turn ME bit off while going down, so that
1189          * when another MCE is hit during panic path, system will checkstop
1190          * and hypervisor will get restarted cleanly by SP.
1191          */
1192         li      r3,MSR_ME
1193         andc    r10,r10,r3              /* Turn off MSR_ME */
1194         mtspr   SPRN_SRR1,r10
1195         RFI_TO_KERNEL
1196         b       .
1197 2:
1198         /*
1199          * Check if we have successfully handled/recovered from error, if not
1200          * then stay on emergency stack and panic.
1201          */
1202         ld      r3,RESULT(r1)   /* Load result */
1203         cmpdi   r3,0            /* see if we handled MCE successfully */
1204
1205         beq     1b              /* if !handled then panic */
1206 BEGIN_FTR_SECTION
1207         /*
1208          * Return from MC interrupt.
1209          * Queue up the MCE event so that we can log it later, while
1210          * returning from kernel or opal call.
1211          */
1212         bl      machine_check_queue_event
1213         MACHINE_CHECK_HANDLER_WINDUP
1214         RFI_TO_USER_OR_KERNEL
1215 FTR_SECTION_ELSE
1216         /*
1217          * pSeries: Return from MC interrupt. Before that stay on emergency
1218          * stack and call machine_check_exception to log the MCE event.
1219          */
1220         LOAD_HANDLER(r10,mce_return)
1221         mtspr   SPRN_SRR0,r10
1222         ld      r10,PACAKMSR(r13)
1223         mtspr   SPRN_SRR1,r10
1224         RFI_TO_KERNEL
1225         b       .
1226 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
1227 9:
1228         /* Deliver the machine check to host kernel in V mode. */
1229         MACHINE_CHECK_HANDLER_WINDUP
1230         EXCEPTION_PROLOG_0 PACA_EXMC
1231         b       machine_check_pSeries_0
1232
1233 EXC_COMMON_BEGIN(unrecover_mce)
1234         /* Invoke machine_check_exception to print MCE event and panic. */
1235         addi    r3,r1,STACK_FRAME_OVERHEAD
1236         bl      machine_check_exception
1237         /*
1238          * We will not reach here. Even if we did, there is no way out. Call
1239          * unrecoverable_exception and die.
1240          */
1241 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
1242         bl      unrecoverable_exception
1243         b       1b
1244
1245 EXC_COMMON_BEGIN(mce_return)
1246         /* Invoke machine_check_exception to print MCE event and return. */
1247         addi    r3,r1,STACK_FRAME_OVERHEAD
1248         bl      machine_check_exception
1249         MACHINE_CHECK_HANDLER_WINDUP
1250         RFI_TO_KERNEL
1251         b       .
1252
1253 EXC_REAL_BEGIN(data_access, 0x300, 0x80)
1254         EXCEPTION_PROLOG_0 PACA_EXGEN
1255         b       tramp_real_data_access
1256 EXC_REAL_END(data_access, 0x300, 0x80)
1257
1258 TRAMP_REAL_BEGIN(tramp_real_data_access)
1259         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, 0x300, 1, 1, 0
1260         EXCEPTION_PROLOG_2_REAL data_access_common, EXC_STD, 1
1261
1262 EXC_VIRT_BEGIN(data_access, 0x4300, 0x80)
1263         EXCEPTION_PROLOG_0 PACA_EXGEN
1264         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 0, 0x300, 1, 1, 0
1265 EXCEPTION_PROLOG_2_VIRT data_access_common, EXC_STD
1266 EXC_VIRT_END(data_access, 0x4300, 0x80)
1267
1268 TRAMP_KVM_SKIP(PACA_EXGEN, 0x300)
1269
1270 EXC_COMMON_BEGIN(data_access_common)
1271         /*
1272          * Here r13 points to the paca, r9 contains the saved CR,
1273          * SRR0 and SRR1 are saved in r11 and r12,
1274          * r9 - r13 are saved in paca->exgen.
1275          * EX_DAR and EX_DSISR have saved DAR/DSISR
1276          */
1277         EXCEPTION_COMMON(PACA_EXGEN, 0x300)
1278         RECONCILE_IRQ_STATE(r10, r11)
1279         ld      r12,_MSR(r1)
1280         ld      r3,PACA_EXGEN+EX_DAR(r13)
1281         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
1282         li      r5,0x300
1283         std     r3,_DAR(r1)
1284         std     r4,_DSISR(r1)
1285 BEGIN_MMU_FTR_SECTION
1286         b       do_hash_page            /* Try to handle as hpte fault */
1287 MMU_FTR_SECTION_ELSE
1288         b       handle_page_fault
1289 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
1290
1291
1292 EXC_REAL_BEGIN(data_access_slb, 0x380, 0x80)
1293         EXCEPTION_PROLOG_0 PACA_EXSLB
1294         b       tramp_real_data_access_slb
1295 EXC_REAL_END(data_access_slb, 0x380, 0x80)
1296
1297 TRAMP_REAL_BEGIN(tramp_real_data_access_slb)
1298         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXSLB, 1, 0x380, 1, 0, 0
1299         EXCEPTION_PROLOG_2_REAL data_access_slb_common, EXC_STD, 1
1300
1301 EXC_VIRT_BEGIN(data_access_slb, 0x4380, 0x80)
1302         EXCEPTION_PROLOG_0 PACA_EXSLB
1303         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXSLB, 0, 0x380, 1, 0, 0
1304         EXCEPTION_PROLOG_2_VIRT data_access_slb_common, EXC_STD
1305 EXC_VIRT_END(data_access_slb, 0x4380, 0x80)
1306
1307 TRAMP_KVM_SKIP(PACA_EXSLB, 0x380)
1308
1309 EXC_COMMON_BEGIN(data_access_slb_common)
1310         EXCEPTION_COMMON(PACA_EXSLB, 0x380)
1311         ld      r4,PACA_EXSLB+EX_DAR(r13)
1312         std     r4,_DAR(r1)
1313         addi    r3,r1,STACK_FRAME_OVERHEAD
1314 BEGIN_MMU_FTR_SECTION
1315         /* HPT case, do SLB fault */
1316         bl      do_slb_fault
1317         cmpdi   r3,0
1318         bne-    1f
1319         b       fast_exception_return
1320 1:      /* Error case */
1321 MMU_FTR_SECTION_ELSE
1322         /* Radix case, access is outside page table range */
1323         li      r3,-EFAULT
1324 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
1325         std     r3,RESULT(r1)
1326         bl      save_nvgprs
1327         RECONCILE_IRQ_STATE(r10, r11)
1328         ld      r4,_DAR(r1)
1329         ld      r5,RESULT(r1)
1330         addi    r3,r1,STACK_FRAME_OVERHEAD
1331         bl      do_bad_slb_fault
1332         b       ret_from_except
1333
1334
1335 EXC_REAL(instruction_access, 0x400, 0x80)
1336 EXC_VIRT(instruction_access, 0x4400, 0x80, 0x400)
1337 TRAMP_KVM(PACA_EXGEN, 0x400)
1338
1339 EXC_COMMON_BEGIN(instruction_access_common)
1340         EXCEPTION_COMMON(PACA_EXGEN, 0x400)
1341         RECONCILE_IRQ_STATE(r10, r11)
1342         ld      r12,_MSR(r1)
1343         ld      r3,_NIP(r1)
1344         andis.  r4,r12,DSISR_SRR1_MATCH_64S@h
1345         li      r5,0x400
1346         std     r3,_DAR(r1)
1347         std     r4,_DSISR(r1)
1348 BEGIN_MMU_FTR_SECTION
1349         b       do_hash_page            /* Try to handle as hpte fault */
1350 MMU_FTR_SECTION_ELSE
1351         b       handle_page_fault
1352 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
1353
1354
1355 __EXC_REAL(instruction_access_slb, 0x480, 0x80, PACA_EXSLB)
1356 __EXC_VIRT(instruction_access_slb, 0x4480, 0x80, 0x480, PACA_EXSLB)
1357 TRAMP_KVM(PACA_EXSLB, 0x480)
1358
1359 EXC_COMMON_BEGIN(instruction_access_slb_common)
1360         EXCEPTION_COMMON(PACA_EXSLB, 0x480)
1361         ld      r4,_NIP(r1)
1362         addi    r3,r1,STACK_FRAME_OVERHEAD
1363 BEGIN_MMU_FTR_SECTION
1364         /* HPT case, do SLB fault */
1365         bl      do_slb_fault
1366         cmpdi   r3,0
1367         bne-    1f
1368         b       fast_exception_return
1369 1:      /* Error case */
1370 MMU_FTR_SECTION_ELSE
1371         /* Radix case, access is outside page table range */
1372         li      r3,-EFAULT
1373 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
1374         std     r3,RESULT(r1)
1375         bl      save_nvgprs
1376         RECONCILE_IRQ_STATE(r10, r11)
1377         ld      r4,_NIP(r1)
1378         ld      r5,RESULT(r1)
1379         addi    r3,r1,STACK_FRAME_OVERHEAD
1380         bl      do_bad_slb_fault
1381         b       ret_from_except
1382
1383
1384 EXC_REAL_BEGIN(hardware_interrupt, 0x500, 0x100)
1385         EXCEPTION_PROLOG_0 PACA_EXGEN
1386 BEGIN_FTR_SECTION
1387         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, 0x500, 0, 0, IRQS_DISABLED
1388         EXCEPTION_PROLOG_2_REAL hardware_interrupt_common, EXC_HV, 1
1389 FTR_SECTION_ELSE
1390         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, 0x500, 0, 0, IRQS_DISABLED
1391         EXCEPTION_PROLOG_2_REAL hardware_interrupt_common, EXC_STD, 1
1392 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
1393 EXC_REAL_END(hardware_interrupt, 0x500, 0x100)
1394
1395 EXC_VIRT_BEGIN(hardware_interrupt, 0x4500, 0x100)
1396         EXCEPTION_PROLOG_0 PACA_EXGEN
1397 BEGIN_FTR_SECTION
1398         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, 0x500, 0, 0, IRQS_DISABLED
1399         EXCEPTION_PROLOG_2_VIRT hardware_interrupt_common, EXC_HV
1400 FTR_SECTION_ELSE
1401         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, 0x500, 0, 0, IRQS_DISABLED
1402         EXCEPTION_PROLOG_2_VIRT hardware_interrupt_common, EXC_STD
1403 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
1404 EXC_VIRT_END(hardware_interrupt, 0x4500, 0x100)
1405
1406 TRAMP_KVM(PACA_EXGEN, 0x500)
1407 TRAMP_KVM_HV(PACA_EXGEN, 0x500)
1408 EXC_COMMON_ASYNC(hardware_interrupt_common, 0x500, do_IRQ)
1409
1410
1411 EXC_REAL_BEGIN(alignment, 0x600, 0x100)
1412         EXCEPTION_PROLOG_0 PACA_EXGEN
1413         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 1, 0x600, 1, 1, 0
1414         EXCEPTION_PROLOG_2_REAL alignment_common, EXC_STD, 1
1415 EXC_REAL_END(alignment, 0x600, 0x100)
1416
1417 EXC_VIRT_BEGIN(alignment, 0x4600, 0x100)
1418         EXCEPTION_PROLOG_0 PACA_EXGEN
1419         EXCEPTION_PROLOG_1 EXC_STD, PACA_EXGEN, 0, 0x600, 1, 1, 0
1420         EXCEPTION_PROLOG_2_VIRT alignment_common, EXC_STD
1421 EXC_VIRT_END(alignment, 0x4600, 0x100)
1422
1423 TRAMP_KVM(PACA_EXGEN, 0x600)
1424 EXC_COMMON_BEGIN(alignment_common)
1425         EXCEPTION_COMMON(PACA_EXGEN, 0x600)
1426         ld      r3,PACA_EXGEN+EX_DAR(r13)
1427         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
1428         std     r3,_DAR(r1)
1429         std     r4,_DSISR(r1)
1430         bl      save_nvgprs
1431         RECONCILE_IRQ_STATE(r10, r11)
1432         addi    r3,r1,STACK_FRAME_OVERHEAD
1433         bl      alignment_exception
1434         b       ret_from_except
1435
1436
1437 EXC_REAL(program_check, 0x700, 0x100)
1438 EXC_VIRT(program_check, 0x4700, 0x100, 0x700)
1439 TRAMP_KVM(PACA_EXGEN, 0x700)
1440 EXC_COMMON_BEGIN(program_check_common)
1441         /*
1442          * It's possible to receive a TM Bad Thing type program check with
1443          * userspace register values (in particular r1), but with SRR1 reporting
1444          * that we came from the kernel. Normally that would confuse the bad
1445          * stack logic, and we would report a bad kernel stack pointer. Instead
1446          * we switch to the emergency stack if we're taking a TM Bad Thing from
1447          * the kernel.
1448          */
1449
1450         andi.   r10,r12,MSR_PR
1451         bne     2f                      /* If userspace, go normal path */
1452
1453         andis.  r10,r12,(SRR1_PROGTM)@h
1454         bne     1f                      /* If TM, emergency             */
1455
1456         cmpdi   r1,-INT_FRAME_SIZE      /* check if r1 is in userspace  */
1457         blt     2f                      /* normal path if not           */
1458
1459         /* Use the emergency stack                                      */
1460 1:      andi.   r10,r12,MSR_PR          /* Set CR0 correctly for label  */
1461                                         /* 3 in EXCEPTION_PROLOG_COMMON */
1462         mr      r10,r1                  /* Save r1                      */
1463         ld      r1,PACAEMERGSP(r13)     /* Use emergency stack          */
1464         subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
1465         b 3f                            /* Jump into the macro !!       */
1466 2:
1467         EXCEPTION_COMMON(PACA_EXGEN, 0x700)
1468         bl      save_nvgprs
1469         RECONCILE_IRQ_STATE(r10, r11)
1470         addi    r3,r1,STACK_FRAME_OVERHEAD
1471         bl      program_check_exception
1472         b       ret_from_except
1473
1474
1475 EXC_REAL(fp_unavailable, 0x800, 0x100)
1476 EXC_VIRT(fp_unavailable, 0x4800, 0x100, 0x800)
1477 TRAMP_KVM(PACA_EXGEN, 0x800)
1478 EXC_COMMON_BEGIN(fp_unavailable_common)
1479         EXCEPTION_COMMON(PACA_EXGEN, 0x800)
1480         bne     1f                      /* if from user, just load it up */
1481         bl      save_nvgprs
1482         RECONCILE_IRQ_STATE(r10, r11)
1483         addi    r3,r1,STACK_FRAME_OVERHEAD
1484         bl      kernel_fp_unavailable_exception
1485         BUG_OPCODE
1486 1:
1487 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1488 BEGIN_FTR_SECTION
1489         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1490          * transaction), go do TM stuff
1491          */
1492         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1493         bne-    2f
1494 END_FTR_SECTION_IFSET(CPU_FTR_TM)
1495 #endif
1496         bl      load_up_fpu
1497         b       fast_exception_return
1498 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1499 2:      /* User process was in a transaction */
1500         bl      save_nvgprs
1501         RECONCILE_IRQ_STATE(r10, r11)
1502         addi    r3,r1,STACK_FRAME_OVERHEAD
1503         bl      fp_unavailable_tm
1504         b       ret_from_except
1505 #endif
1506
1507
1508 EXC_REAL_OOL_MASKABLE(decrementer, 0x900, 0x80, IRQS_DISABLED)
1509 EXC_VIRT_MASKABLE(decrementer, 0x4900, 0x80, 0x900, IRQS_DISABLED)
1510 TRAMP_KVM(PACA_EXGEN, 0x900)
1511 EXC_COMMON_ASYNC(decrementer_common, 0x900, timer_interrupt)
1512
1513
1514 EXC_REAL_HV(hdecrementer, 0x980, 0x80)
1515 EXC_VIRT_HV(hdecrementer, 0x4980, 0x80, 0x980)
1516 TRAMP_KVM_HV(PACA_EXGEN, 0x980)
1517 EXC_COMMON(hdecrementer_common, 0x980, hdec_interrupt)
1518
1519
1520 EXC_REAL_MASKABLE(doorbell_super, 0xa00, 0x100, IRQS_DISABLED)
1521 EXC_VIRT_MASKABLE(doorbell_super, 0x4a00, 0x100, 0xa00, IRQS_DISABLED)
1522 TRAMP_KVM(PACA_EXGEN, 0xa00)
1523 #ifdef CONFIG_PPC_DOORBELL
1524 EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, doorbell_exception)
1525 #else
1526 EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, unknown_exception)
1527 #endif
1528
1529
1530 EXC_REAL(trap_0b, 0xb00, 0x100)
1531 EXC_VIRT(trap_0b, 0x4b00, 0x100, 0xb00)
1532 TRAMP_KVM(PACA_EXGEN, 0xb00)
1533 EXC_COMMON(trap_0b_common, 0xb00, unknown_exception)
1534
1535 /*
1536  * system call / hypercall (0xc00, 0x4c00)
1537  *
1538  * The system call exception is invoked with "sc 0" and does not alter HV bit.
1539  * There is support for kernel code to invoke system calls but there are no
1540  * in-tree users.
1541  *
1542  * The hypercall is invoked with "sc 1" and sets HV=1.
1543  *
1544  * In HPT, sc 1 always goes to 0xc00 real mode. In RADIX, sc 1 can go to
1545  * 0x4c00 virtual mode.
1546  *
1547  * Call convention:
1548  *
1549  * syscall register convention is in Documentation/powerpc/syscall64-abi.txt
1550  *
1551  * For hypercalls, the register convention is as follows:
1552  * r0 volatile
1553  * r1-2 nonvolatile
1554  * r3 volatile parameter and return value for status
1555  * r4-r10 volatile input and output value
1556  * r11 volatile hypercall number and output value
1557  * r12 volatile input and output value
1558  * r13-r31 nonvolatile
1559  * LR nonvolatile
1560  * CTR volatile
1561  * XER volatile
1562  * CR0-1 CR5-7 volatile
1563  * CR2-4 nonvolatile
1564  * Other registers nonvolatile
1565  *
1566  * The intersection of volatile registers that don't contain possible
1567  * inputs is: cr0, xer, ctr. We may use these as scratch regs upon entry
1568  * without saving, though xer is not a good idea to use, as hardware may
1569  * interpret some bits so it may be costly to change them.
1570  */
1571 .macro SYSTEM_CALL virt
1572 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1573         /*
1574          * There is a little bit of juggling to get syscall and hcall
1575          * working well. Save r13 in ctr to avoid using SPRG scratch
1576          * register.
1577          *
1578          * Userspace syscalls have already saved the PPR, hcalls must save
1579          * it before setting HMT_MEDIUM.
1580          */
1581         mtctr   r13
1582         GET_PACA(r13)
1583         std     r10,PACA_EXGEN+EX_R10(r13)
1584         INTERRUPT_TO_KERNEL
1585         KVMTEST EXC_STD 0xc00 /* uses r10, branch to do_kvm_0xc00_system_call */
1586         mfctr   r9
1587 #else
1588         mr      r9,r13
1589         GET_PACA(r13)
1590         INTERRUPT_TO_KERNEL
1591 #endif
1592
1593 #ifdef CONFIG_PPC_FAST_ENDIAN_SWITCH
1594 BEGIN_FTR_SECTION
1595         cmpdi   r0,0x1ebe
1596         beq-    1f
1597 END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE)
1598 #endif
1599
1600         /* We reach here with PACA in r13, r13 in r9. */
1601         mfspr   r11,SPRN_SRR0
1602         mfspr   r12,SPRN_SRR1
1603
1604         HMT_MEDIUM
1605
1606         .if ! \virt
1607         __LOAD_HANDLER(r10, system_call_common)
1608         mtspr   SPRN_SRR0,r10
1609         ld      r10,PACAKMSR(r13)
1610         mtspr   SPRN_SRR1,r10
1611         RFI_TO_KERNEL
1612         b       .       /* prevent speculative execution */
1613         .else
1614         li      r10,MSR_RI
1615         mtmsrd  r10,1                   /* Set RI (EE=0) */
1616 #ifdef CONFIG_RELOCATABLE
1617         __LOAD_HANDLER(r10, system_call_common)
1618         mtctr   r10
1619         bctr
1620 #else
1621         b       system_call_common
1622 #endif
1623         .endif
1624
1625 #ifdef CONFIG_PPC_FAST_ENDIAN_SWITCH
1626         /* Fast LE/BE switch system call */
1627 1:      mfspr   r12,SPRN_SRR1
1628         xori    r12,r12,MSR_LE
1629         mtspr   SPRN_SRR1,r12
1630         mr      r13,r9
1631         RFI_TO_USER     /* return to userspace */
1632         b       .       /* prevent speculative execution */
1633 #endif
1634 .endm
1635
1636 EXC_REAL_BEGIN(system_call, 0xc00, 0x100)
1637         SYSTEM_CALL 0
1638 EXC_REAL_END(system_call, 0xc00, 0x100)
1639
1640 EXC_VIRT_BEGIN(system_call, 0x4c00, 0x100)
1641         SYSTEM_CALL 1
1642 EXC_VIRT_END(system_call, 0x4c00, 0x100)
1643
1644 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1645         /*
1646          * This is a hcall, so register convention is as above, with these
1647          * differences:
1648          * r13 = PACA
1649          * ctr = orig r13
1650          * orig r10 saved in PACA
1651          */
1652 TRAMP_KVM_BEGIN(do_kvm_0xc00)
1653          /*
1654           * Save the PPR (on systems that support it) before changing to
1655           * HMT_MEDIUM. That allows the KVM code to save that value into the
1656           * guest state (it is the guest's PPR value).
1657           */
1658         OPT_GET_SPR(r10, SPRN_PPR, CPU_FTR_HAS_PPR)
1659         HMT_MEDIUM
1660         OPT_SAVE_REG_TO_PACA(PACA_EXGEN+EX_PPR, r10, CPU_FTR_HAS_PPR)
1661         mfctr   r10
1662         SET_SCRATCH0(r10)
1663         std     r9,PACA_EXGEN+EX_R9(r13)
1664         mfcr    r9
1665         KVM_HANDLER PACA_EXGEN, EXC_STD, 0xc00, 0
1666 #endif
1667
1668
1669 EXC_REAL(single_step, 0xd00, 0x100)
1670 EXC_VIRT(single_step, 0x4d00, 0x100, 0xd00)
1671 TRAMP_KVM(PACA_EXGEN, 0xd00)
1672 EXC_COMMON(single_step_common, 0xd00, single_step_exception)
1673
1674 EXC_REAL_OOL_HV(h_data_storage, 0xe00, 0x20)
1675 EXC_VIRT_OOL_HV(h_data_storage, 0x4e00, 0x20, 0xe00)
1676 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0xe00)
1677 EXC_COMMON_BEGIN(h_data_storage_common)
1678         mfspr   r10,SPRN_HDAR
1679         std     r10,PACA_EXGEN+EX_DAR(r13)
1680         mfspr   r10,SPRN_HDSISR
1681         stw     r10,PACA_EXGEN+EX_DSISR(r13)
1682         EXCEPTION_COMMON(PACA_EXGEN, 0xe00)
1683         bl      save_nvgprs
1684         RECONCILE_IRQ_STATE(r10, r11)
1685         addi    r3,r1,STACK_FRAME_OVERHEAD
1686 BEGIN_MMU_FTR_SECTION
1687         ld      r4,PACA_EXGEN+EX_DAR(r13)
1688         lwz     r5,PACA_EXGEN+EX_DSISR(r13)
1689         std     r4,_DAR(r1)
1690         std     r5,_DSISR(r1)
1691         li      r5,SIGSEGV
1692         bl      bad_page_fault
1693 MMU_FTR_SECTION_ELSE
1694         bl      unknown_exception
1695 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_TYPE_RADIX)
1696         b       ret_from_except
1697
1698
1699 EXC_REAL_OOL_HV(h_instr_storage, 0xe20, 0x20)
1700 EXC_VIRT_OOL_HV(h_instr_storage, 0x4e20, 0x20, 0xe20)
1701 TRAMP_KVM_HV(PACA_EXGEN, 0xe20)
1702 EXC_COMMON(h_instr_storage_common, 0xe20, unknown_exception)
1703
1704
1705 EXC_REAL_OOL_HV(emulation_assist, 0xe40, 0x20)
1706 EXC_VIRT_OOL_HV(emulation_assist, 0x4e40, 0x20, 0xe40)
1707 TRAMP_KVM_HV(PACA_EXGEN, 0xe40)
1708 EXC_COMMON(emulation_assist_common, 0xe40, emulation_assist_interrupt)
1709
1710
1711 /*
1712  * hmi_exception trampoline is a special case. It jumps to hmi_exception_early
1713  * first, and then eventaully from there to the trampoline to get into virtual
1714  * mode.
1715  */
1716 __EXC_REAL_OOL_HV_DIRECT(hmi_exception, 0xe60, 0x20, hmi_exception_early)
1717 __TRAMP_REAL_OOL_MASKABLE_HV(hmi_exception, 0xe60, IRQS_DISABLED)
1718 EXC_VIRT_NONE(0x4e60, 0x20)
1719 TRAMP_KVM_HV(PACA_EXGEN, 0xe60)
1720 TRAMP_REAL_BEGIN(hmi_exception_early)
1721         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 1, 0xe60, 0, 0, 0
1722         mr      r10,r1                  /* Save r1 */
1723         ld      r1,PACAEMERGSP(r13)     /* Use emergency stack for realmode */
1724         subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
1725         mfspr   r11,SPRN_HSRR0          /* Save HSRR0 */
1726         mfspr   r12,SPRN_HSRR1          /* Save HSRR1 */
1727         EXCEPTION_PROLOG_COMMON_1()
1728         /* We don't touch AMR here, we never go to virtual mode */
1729         EXCEPTION_PROLOG_COMMON_2(PACA_EXGEN)
1730         EXCEPTION_PROLOG_COMMON_3(0xe60)
1731         addi    r3,r1,STACK_FRAME_OVERHEAD
1732         BRANCH_LINK_TO_FAR(DOTSYM(hmi_exception_realmode)) /* Function call ABI */
1733         cmpdi   cr0,r3,0
1734         bne     1f
1735
1736         EXCEPTION_RESTORE_REGS EXC_HV
1737         HRFI_TO_USER_OR_KERNEL
1738
1739 1:
1740         /*
1741          * Go to virtual mode and pull the HMI event information from
1742          * firmware.
1743          */
1744         EXCEPTION_RESTORE_REGS EXC_HV
1745         EXCEPTION_PROLOG_0 PACA_EXGEN
1746         b       tramp_real_hmi_exception
1747
1748 EXC_COMMON_BEGIN(hmi_exception_common)
1749         EXCEPTION_COMMON(PACA_EXGEN, 0xe60)
1750         FINISH_NAP
1751         bl      save_nvgprs
1752         RECONCILE_IRQ_STATE(r10, r11)
1753         RUNLATCH_ON
1754         addi    r3,r1,STACK_FRAME_OVERHEAD
1755         bl      handle_hmi_exception
1756         b       ret_from_except
1757
1758 EXC_REAL_OOL_MASKABLE_HV(h_doorbell, 0xe80, 0x20, IRQS_DISABLED)
1759 EXC_VIRT_OOL_MASKABLE_HV(h_doorbell, 0x4e80, 0x20, 0xe80, IRQS_DISABLED)
1760 TRAMP_KVM_HV(PACA_EXGEN, 0xe80)
1761 #ifdef CONFIG_PPC_DOORBELL
1762 EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, doorbell_exception)
1763 #else
1764 EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, unknown_exception)
1765 #endif
1766
1767
1768 EXC_REAL_OOL_MASKABLE_HV(h_virt_irq, 0xea0, 0x20, IRQS_DISABLED)
1769 EXC_VIRT_OOL_MASKABLE_HV(h_virt_irq, 0x4ea0, 0x20, 0xea0, IRQS_DISABLED)
1770 TRAMP_KVM_HV(PACA_EXGEN, 0xea0)
1771 EXC_COMMON_ASYNC(h_virt_irq_common, 0xea0, do_IRQ)
1772
1773
1774 EXC_REAL_NONE(0xec0, 0x20)
1775 EXC_VIRT_NONE(0x4ec0, 0x20)
1776 EXC_REAL_NONE(0xee0, 0x20)
1777 EXC_VIRT_NONE(0x4ee0, 0x20)
1778
1779
1780 EXC_REAL_OOL_MASKABLE(performance_monitor, 0xf00, 0x20, IRQS_PMI_DISABLED)
1781 EXC_VIRT_OOL_MASKABLE(performance_monitor, 0x4f00, 0x20, 0xf00, IRQS_PMI_DISABLED)
1782 TRAMP_KVM(PACA_EXGEN, 0xf00)
1783 EXC_COMMON_ASYNC(performance_monitor_common, 0xf00, performance_monitor_exception)
1784
1785
1786 EXC_REAL_OOL(altivec_unavailable, 0xf20, 0x20)
1787 EXC_VIRT_OOL(altivec_unavailable, 0x4f20, 0x20, 0xf20)
1788 TRAMP_KVM(PACA_EXGEN, 0xf20)
1789 EXC_COMMON_BEGIN(altivec_unavailable_common)
1790         EXCEPTION_COMMON(PACA_EXGEN, 0xf20)
1791 #ifdef CONFIG_ALTIVEC
1792 BEGIN_FTR_SECTION
1793         beq     1f
1794 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1795   BEGIN_FTR_SECTION_NESTED(69)
1796         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1797          * transaction), go do TM stuff
1798          */
1799         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1800         bne-    2f
1801   END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1802 #endif
1803         bl      load_up_altivec
1804         b       fast_exception_return
1805 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1806 2:      /* User process was in a transaction */
1807         bl      save_nvgprs
1808         RECONCILE_IRQ_STATE(r10, r11)
1809         addi    r3,r1,STACK_FRAME_OVERHEAD
1810         bl      altivec_unavailable_tm
1811         b       ret_from_except
1812 #endif
1813 1:
1814 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1815 #endif
1816         bl      save_nvgprs
1817         RECONCILE_IRQ_STATE(r10, r11)
1818         addi    r3,r1,STACK_FRAME_OVERHEAD
1819         bl      altivec_unavailable_exception
1820         b       ret_from_except
1821
1822
1823 EXC_REAL_OOL(vsx_unavailable, 0xf40, 0x20)
1824 EXC_VIRT_OOL(vsx_unavailable, 0x4f40, 0x20, 0xf40)
1825 TRAMP_KVM(PACA_EXGEN, 0xf40)
1826 EXC_COMMON_BEGIN(vsx_unavailable_common)
1827         EXCEPTION_COMMON(PACA_EXGEN, 0xf40)
1828 #ifdef CONFIG_VSX
1829 BEGIN_FTR_SECTION
1830         beq     1f
1831 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1832   BEGIN_FTR_SECTION_NESTED(69)
1833         /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1834          * transaction), go do TM stuff
1835          */
1836         rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1837         bne-    2f
1838   END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1839 #endif
1840         b       load_up_vsx
1841 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1842 2:      /* User process was in a transaction */
1843         bl      save_nvgprs
1844         RECONCILE_IRQ_STATE(r10, r11)
1845         addi    r3,r1,STACK_FRAME_OVERHEAD
1846         bl      vsx_unavailable_tm
1847         b       ret_from_except
1848 #endif
1849 1:
1850 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1851 #endif
1852         bl      save_nvgprs
1853         RECONCILE_IRQ_STATE(r10, r11)
1854         addi    r3,r1,STACK_FRAME_OVERHEAD
1855         bl      vsx_unavailable_exception
1856         b       ret_from_except
1857
1858
1859 EXC_REAL_OOL(facility_unavailable, 0xf60, 0x20)
1860 EXC_VIRT_OOL(facility_unavailable, 0x4f60, 0x20, 0xf60)
1861 TRAMP_KVM(PACA_EXGEN, 0xf60)
1862 EXC_COMMON(facility_unavailable_common, 0xf60, facility_unavailable_exception)
1863
1864
1865 EXC_REAL_OOL_HV(h_facility_unavailable, 0xf80, 0x20)
1866 EXC_VIRT_OOL_HV(h_facility_unavailable, 0x4f80, 0x20, 0xf80)
1867 TRAMP_KVM_HV(PACA_EXGEN, 0xf80)
1868 EXC_COMMON(h_facility_unavailable_common, 0xf80, facility_unavailable_exception)
1869
1870
1871 EXC_REAL_NONE(0xfa0, 0x20)
1872 EXC_VIRT_NONE(0x4fa0, 0x20)
1873 EXC_REAL_NONE(0xfc0, 0x20)
1874 EXC_VIRT_NONE(0x4fc0, 0x20)
1875 EXC_REAL_NONE(0xfe0, 0x20)
1876 EXC_VIRT_NONE(0x4fe0, 0x20)
1877
1878 EXC_REAL_NONE(0x1000, 0x100)
1879 EXC_VIRT_NONE(0x5000, 0x100)
1880 EXC_REAL_NONE(0x1100, 0x100)
1881 EXC_VIRT_NONE(0x5100, 0x100)
1882
1883 #ifdef CONFIG_CBE_RAS
1884 EXC_REAL_HV(cbe_system_error, 0x1200, 0x100)
1885 EXC_VIRT_NONE(0x5200, 0x100)
1886 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1200)
1887 EXC_COMMON(cbe_system_error_common, 0x1200, cbe_system_error_exception)
1888 #else /* CONFIG_CBE_RAS */
1889 EXC_REAL_NONE(0x1200, 0x100)
1890 EXC_VIRT_NONE(0x5200, 0x100)
1891 #endif
1892
1893
1894 EXC_REAL(instruction_breakpoint, 0x1300, 0x100)
1895 EXC_VIRT(instruction_breakpoint, 0x5300, 0x100, 0x1300)
1896 TRAMP_KVM_SKIP(PACA_EXGEN, 0x1300)
1897 EXC_COMMON(instruction_breakpoint_common, 0x1300, instruction_breakpoint_exception)
1898
1899 EXC_REAL_NONE(0x1400, 0x100)
1900 EXC_VIRT_NONE(0x5400, 0x100)
1901
1902 EXC_REAL_BEGIN(denorm_exception_hv, 0x1500, 0x100)
1903         EXCEPTION_PROLOG_0 PACA_EXGEN
1904         EXCEPTION_PROLOG_1 EXC_HV, PACA_EXGEN, 0, 0x1500, 0, 0, 0
1905
1906 #ifdef CONFIG_PPC_DENORMALISATION
1907         mfspr   r10,SPRN_HSRR1
1908         andis.  r10,r10,(HSRR1_DENORM)@h /* denorm? */
1909         bne+    denorm_assist
1910 #endif
1911
1912         KVMTEST EXC_HV 0x1500
1913         EXCEPTION_PROLOG_2_REAL denorm_common, EXC_HV, 1
1914 EXC_REAL_END(denorm_exception_hv, 0x1500, 0x100)
1915
1916 #ifdef CONFIG_PPC_DENORMALISATION
1917 EXC_VIRT_BEGIN(denorm_exception, 0x5500, 0x100)
1918         b       exc_real_0x1500_denorm_exception_hv
1919 EXC_VIRT_END(denorm_exception, 0x5500, 0x100)
1920 #else
1921 EXC_VIRT_NONE(0x5500, 0x100)
1922 #endif
1923
1924 TRAMP_KVM_HV(PACA_EXGEN, 0x1500)
1925
1926 #ifdef CONFIG_PPC_DENORMALISATION
1927 TRAMP_REAL_BEGIN(denorm_assist)
1928 BEGIN_FTR_SECTION
1929 /*
1930  * To denormalise we need to move a copy of the register to itself.
1931  * For POWER6 do that here for all FP regs.
1932  */
1933         mfmsr   r10
1934         ori     r10,r10,(MSR_FP|MSR_FE0|MSR_FE1)
1935         xori    r10,r10,(MSR_FE0|MSR_FE1)
1936         mtmsrd  r10
1937         sync
1938
1939         .Lreg=0
1940         .rept 32
1941         fmr     .Lreg,.Lreg
1942         .Lreg=.Lreg+1
1943         .endr
1944
1945 FTR_SECTION_ELSE
1946 /*
1947  * To denormalise we need to move a copy of the register to itself.
1948  * For POWER7 do that here for the first 32 VSX registers only.
1949  */
1950         mfmsr   r10
1951         oris    r10,r10,MSR_VSX@h
1952         mtmsrd  r10
1953         sync
1954
1955         .Lreg=0
1956         .rept 32
1957         XVCPSGNDP(.Lreg,.Lreg,.Lreg)
1958         .Lreg=.Lreg+1
1959         .endr
1960
1961 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_206)
1962
1963 BEGIN_FTR_SECTION
1964         b       denorm_done
1965 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1966 /*
1967  * To denormalise we need to move a copy of the register to itself.
1968  * For POWER8 we need to do that for all 64 VSX registers
1969  */
1970         .Lreg=32
1971         .rept 32
1972         XVCPSGNDP(.Lreg,.Lreg,.Lreg)
1973         .Lreg=.Lreg+1
1974         .endr
1975
1976 denorm_done:
1977         mfspr   r11,SPRN_HSRR0
1978         subi    r11,r11,4
1979         mtspr   SPRN_HSRR0,r11
1980         mtcrf   0x80,r9
1981         ld      r9,PACA_EXGEN+EX_R9(r13)
1982         RESTORE_PPR_PACA(PACA_EXGEN, r10)
1983 BEGIN_FTR_SECTION
1984         ld      r10,PACA_EXGEN+EX_CFAR(r13)
1985         mtspr   SPRN_CFAR,r10
1986 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1987         ld      r10,PACA_EXGEN+EX_R10(r13)
1988         ld      r11,PACA_EXGEN+EX_R11(r13)
1989         ld      r12,PACA_EXGEN+EX_R12(r13)
1990         ld      r13,PACA_EXGEN+EX_R13(r13)
1991         HRFI_TO_UNKNOWN
1992         b       .
1993 #endif
1994
1995 EXC_COMMON(denorm_common, 0x1500, unknown_exception)
1996
1997
1998 #ifdef CONFIG_CBE_RAS
1999 EXC_REAL_HV(cbe_maintenance, 0x1600, 0x100)
2000 EXC_VIRT_NONE(0x5600, 0x100)
2001 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1600)
2002 EXC_COMMON(cbe_maintenance_common, 0x1600, cbe_maintenance_exception)
2003 #else /* CONFIG_CBE_RAS */
2004 EXC_REAL_NONE(0x1600, 0x100)
2005 EXC_VIRT_NONE(0x5600, 0x100)
2006 #endif
2007
2008
2009 EXC_REAL(altivec_assist, 0x1700, 0x100)
2010 EXC_VIRT(altivec_assist, 0x5700, 0x100, 0x1700)
2011 TRAMP_KVM(PACA_EXGEN, 0x1700)
2012 #ifdef CONFIG_ALTIVEC
2013 EXC_COMMON(altivec_assist_common, 0x1700, altivec_assist_exception)
2014 #else
2015 EXC_COMMON(altivec_assist_common, 0x1700, unknown_exception)
2016 #endif
2017
2018
2019 #ifdef CONFIG_CBE_RAS
2020 EXC_REAL_HV(cbe_thermal, 0x1800, 0x100)
2021 EXC_VIRT_NONE(0x5800, 0x100)
2022 TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1800)
2023 EXC_COMMON(cbe_thermal_common, 0x1800, cbe_thermal_exception)
2024 #else /* CONFIG_CBE_RAS */
2025 EXC_REAL_NONE(0x1800, 0x100)
2026 EXC_VIRT_NONE(0x5800, 0x100)
2027 #endif
2028
2029 #ifdef CONFIG_PPC_WATCHDOG
2030
2031 #define MASKED_DEC_HANDLER_LABEL 3f
2032
2033 #define MASKED_DEC_HANDLER(_H)                          \
2034 3: /* soft-nmi */                                       \
2035         std     r12,PACA_EXGEN+EX_R12(r13);             \
2036         GET_SCRATCH0(r10);                              \
2037         std     r10,PACA_EXGEN+EX_R13(r13);             \
2038         EXCEPTION_PROLOG_2_REAL soft_nmi_common, _H, 1
2039
2040 /*
2041  * Branch to soft_nmi_interrupt using the emergency stack. The emergency
2042  * stack is one that is usable by maskable interrupts so long as MSR_EE
2043  * remains off. It is used for recovery when something has corrupted the
2044  * normal kernel stack, for example. The "soft NMI" must not use the process
2045  * stack because we want irq disabled sections to avoid touching the stack
2046  * at all (other than PMU interrupts), so use the emergency stack for this,
2047  * and run it entirely with interrupts hard disabled.
2048  */
2049 EXC_COMMON_BEGIN(soft_nmi_common)
2050         mr      r10,r1
2051         ld      r1,PACAEMERGSP(r13)
2052         subi    r1,r1,INT_FRAME_SIZE
2053         EXCEPTION_COMMON_STACK(PACA_EXGEN, 0x900)
2054         bl      save_nvgprs
2055         RECONCILE_IRQ_STATE(r10, r11)
2056         addi    r3,r1,STACK_FRAME_OVERHEAD
2057         bl      soft_nmi_interrupt
2058         b       ret_from_except
2059
2060 #else /* CONFIG_PPC_WATCHDOG */
2061 #define MASKED_DEC_HANDLER_LABEL 2f /* normal return */
2062 #define MASKED_DEC_HANDLER(_H)
2063 #endif /* CONFIG_PPC_WATCHDOG */
2064
2065 /*
2066  * An interrupt came in while soft-disabled. We set paca->irq_happened, then:
2067  * - If it was a decrementer interrupt, we bump the dec to max and and return.
2068  * - If it was a doorbell we return immediately since doorbells are edge
2069  *   triggered and won't automatically refire.
2070  * - If it was a HMI we return immediately since we handled it in realmode
2071  *   and it won't refire.
2072  * - Else it is one of PACA_IRQ_MUST_HARD_MASK, so hard disable and return.
2073  * This is called with r10 containing the value to OR to the paca field.
2074  */
2075 .macro MASKED_INTERRUPT hsrr
2076         .if \hsrr
2077 masked_Hinterrupt:
2078         .else
2079 masked_interrupt:
2080         .endif
2081         std     r11,PACA_EXGEN+EX_R11(r13)
2082         lbz     r11,PACAIRQHAPPENED(r13)
2083         or      r11,r11,r10
2084         stb     r11,PACAIRQHAPPENED(r13)
2085         cmpwi   r10,PACA_IRQ_DEC
2086         bne     1f
2087         lis     r10,0x7fff
2088         ori     r10,r10,0xffff
2089         mtspr   SPRN_DEC,r10
2090         b       MASKED_DEC_HANDLER_LABEL
2091 1:      andi.   r10,r10,PACA_IRQ_MUST_HARD_MASK
2092         beq     2f
2093         .if \hsrr
2094         mfspr   r10,SPRN_HSRR1
2095         xori    r10,r10,MSR_EE  /* clear MSR_EE */
2096         mtspr   SPRN_HSRR1,r10
2097         .else
2098         mfspr   r10,SPRN_SRR1
2099         xori    r10,r10,MSR_EE  /* clear MSR_EE */
2100         mtspr   SPRN_SRR1,r10
2101         .endif
2102         ori     r11,r11,PACA_IRQ_HARD_DIS
2103         stb     r11,PACAIRQHAPPENED(r13)
2104 2:      /* done */
2105         mtcrf   0x80,r9
2106         std     r1,PACAR1(r13)
2107         ld      r9,PACA_EXGEN+EX_R9(r13)
2108         ld      r10,PACA_EXGEN+EX_R10(r13)
2109         ld      r11,PACA_EXGEN+EX_R11(r13)
2110         /* returns to kernel where r13 must be set up, so don't restore it */
2111         .if \hsrr
2112         HRFI_TO_KERNEL
2113         .else
2114         RFI_TO_KERNEL
2115         .endif
2116         b       .
2117         MASKED_DEC_HANDLER(\hsrr\())
2118 .endm
2119
2120 TRAMP_REAL_BEGIN(stf_barrier_fallback)
2121         std     r9,PACA_EXRFI+EX_R9(r13)
2122         std     r10,PACA_EXRFI+EX_R10(r13)
2123         sync
2124         ld      r9,PACA_EXRFI+EX_R9(r13)
2125         ld      r10,PACA_EXRFI+EX_R10(r13)
2126         ori     31,31,0
2127         .rept 14
2128         b       1f
2129 1:
2130         .endr
2131         blr
2132
2133 TRAMP_REAL_BEGIN(rfi_flush_fallback)
2134         SET_SCRATCH0(r13);
2135         GET_PACA(r13);
2136         std     r1,PACA_EXRFI+EX_R12(r13)
2137         ld      r1,PACAKSAVE(r13)
2138         std     r9,PACA_EXRFI+EX_R9(r13)
2139         std     r10,PACA_EXRFI+EX_R10(r13)
2140         std     r11,PACA_EXRFI+EX_R11(r13)
2141         mfctr   r9
2142         ld      r10,PACA_RFI_FLUSH_FALLBACK_AREA(r13)
2143         ld      r11,PACA_L1D_FLUSH_SIZE(r13)
2144         srdi    r11,r11,(7 + 3) /* 128 byte lines, unrolled 8x */
2145         mtctr   r11
2146         DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r11) /* Stop prefetch streams */
2147
2148         /* order ld/st prior to dcbt stop all streams with flushing */
2149         sync
2150
2151         /*
2152          * The load adresses are at staggered offsets within cachelines,
2153          * which suits some pipelines better (on others it should not
2154          * hurt).
2155          */
2156 1:
2157         ld      r11,(0x80 + 8)*0(r10)
2158         ld      r11,(0x80 + 8)*1(r10)
2159         ld      r11,(0x80 + 8)*2(r10)
2160         ld      r11,(0x80 + 8)*3(r10)
2161         ld      r11,(0x80 + 8)*4(r10)
2162         ld      r11,(0x80 + 8)*5(r10)
2163         ld      r11,(0x80 + 8)*6(r10)
2164         ld      r11,(0x80 + 8)*7(r10)
2165         addi    r10,r10,0x80*8
2166         bdnz    1b
2167
2168         mtctr   r9
2169         ld      r9,PACA_EXRFI+EX_R9(r13)
2170         ld      r10,PACA_EXRFI+EX_R10(r13)
2171         ld      r11,PACA_EXRFI+EX_R11(r13)
2172         ld      r1,PACA_EXRFI+EX_R12(r13)
2173         GET_SCRATCH0(r13);
2174         rfid
2175
2176 TRAMP_REAL_BEGIN(hrfi_flush_fallback)
2177         SET_SCRATCH0(r13);
2178         GET_PACA(r13);
2179         std     r1,PACA_EXRFI+EX_R12(r13)
2180         ld      r1,PACAKSAVE(r13)
2181         std     r9,PACA_EXRFI+EX_R9(r13)
2182         std     r10,PACA_EXRFI+EX_R10(r13)
2183         std     r11,PACA_EXRFI+EX_R11(r13)
2184         mfctr   r9
2185         ld      r10,PACA_RFI_FLUSH_FALLBACK_AREA(r13)
2186         ld      r11,PACA_L1D_FLUSH_SIZE(r13)
2187         srdi    r11,r11,(7 + 3) /* 128 byte lines, unrolled 8x */
2188         mtctr   r11
2189         DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r11) /* Stop prefetch streams */
2190
2191         /* order ld/st prior to dcbt stop all streams with flushing */
2192         sync
2193
2194         /*
2195          * The load adresses are at staggered offsets within cachelines,
2196          * which suits some pipelines better (on others it should not
2197          * hurt).
2198          */
2199 1:
2200         ld      r11,(0x80 + 8)*0(r10)
2201         ld      r11,(0x80 + 8)*1(r10)
2202         ld      r11,(0x80 + 8)*2(r10)
2203         ld      r11,(0x80 + 8)*3(r10)
2204         ld      r11,(0x80 + 8)*4(r10)
2205         ld      r11,(0x80 + 8)*5(r10)
2206         ld      r11,(0x80 + 8)*6(r10)
2207         ld      r11,(0x80 + 8)*7(r10)
2208         addi    r10,r10,0x80*8
2209         bdnz    1b
2210
2211         mtctr   r9
2212         ld      r9,PACA_EXRFI+EX_R9(r13)
2213         ld      r10,PACA_EXRFI+EX_R10(r13)
2214         ld      r11,PACA_EXRFI+EX_R11(r13)
2215         ld      r1,PACA_EXRFI+EX_R12(r13)
2216         GET_SCRATCH0(r13);
2217         hrfid
2218
2219 /*
2220  * Real mode exceptions actually use this too, but alternate
2221  * instruction code patches (which end up in the common .text area)
2222  * cannot reach these if they are put there.
2223  */
2224 USE_FIXED_SECTION(virt_trampolines)
2225         MASKED_INTERRUPT EXC_STD
2226         MASKED_INTERRUPT EXC_HV
2227
2228 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2229 TRAMP_REAL_BEGIN(kvmppc_skip_interrupt)
2230         /*
2231          * Here all GPRs are unchanged from when the interrupt happened
2232          * except for r13, which is saved in SPRG_SCRATCH0.
2233          */
2234         mfspr   r13, SPRN_SRR0
2235         addi    r13, r13, 4
2236         mtspr   SPRN_SRR0, r13
2237         GET_SCRATCH0(r13)
2238         RFI_TO_KERNEL
2239         b       .
2240
2241 TRAMP_REAL_BEGIN(kvmppc_skip_Hinterrupt)
2242         /*
2243          * Here all GPRs are unchanged from when the interrupt happened
2244          * except for r13, which is saved in SPRG_SCRATCH0.
2245          */
2246         mfspr   r13, SPRN_HSRR0
2247         addi    r13, r13, 4
2248         mtspr   SPRN_HSRR0, r13
2249         GET_SCRATCH0(r13)
2250         HRFI_TO_KERNEL
2251         b       .
2252 #endif
2253
2254 /*
2255  * Ensure that any handlers that get invoked from the exception prologs
2256  * above are below the first 64KB (0x10000) of the kernel image because
2257  * the prologs assemble the addresses of these handlers using the
2258  * LOAD_HANDLER macro, which uses an ori instruction.
2259  */
2260
2261 /*** Common interrupt handlers ***/
2262
2263
2264         /*
2265          * Relocation-on interrupts: A subset of the interrupts can be delivered
2266          * with IR=1/DR=1, if AIL==2 and MSR.HV won't be changed by delivering
2267          * it.  Addresses are the same as the original interrupt addresses, but
2268          * offset by 0xc000000000004000.
2269          * It's impossible to receive interrupts below 0x300 via this mechanism.
2270          * KVM: None of these traps are from the guest ; anything that escalated
2271          * to HV=1 from HV=0 is delivered via real mode handlers.
2272          */
2273
2274         /*
2275          * This uses the standard macro, since the original 0x300 vector
2276          * only has extra guff for STAB-based processors -- which never
2277          * come here.
2278          */
2279
2280 EXC_COMMON_BEGIN(ppc64_runlatch_on_trampoline)
2281         b       __ppc64_runlatch_on
2282
2283 USE_FIXED_SECTION(virt_trampolines)
2284         /*
2285          * The __end_interrupts marker must be past the out-of-line (OOL)
2286          * handlers, so that they are copied to real address 0x100 when running
2287          * a relocatable kernel. This ensures they can be reached from the short
2288          * trampoline handlers (like 0x4f00, 0x4f20, etc.) which branch
2289          * directly, without using LOAD_HANDLER().
2290          */
2291         .align  7
2292         .globl  __end_interrupts
2293 __end_interrupts:
2294 DEFINE_FIXED_SYMBOL(__end_interrupts)
2295
2296 #ifdef CONFIG_PPC_970_NAP
2297 EXC_COMMON_BEGIN(power4_fixup_nap)
2298         andc    r9,r9,r10
2299         std     r9,TI_LOCAL_FLAGS(r11)
2300         ld      r10,_LINK(r1)           /* make idle task do the */
2301         std     r10,_NIP(r1)            /* equivalent of a blr */
2302         blr
2303 #endif
2304
2305 CLOSE_FIXED_SECTION(real_vectors);
2306 CLOSE_FIXED_SECTION(real_trampolines);
2307 CLOSE_FIXED_SECTION(virt_vectors);
2308 CLOSE_FIXED_SECTION(virt_trampolines);
2309
2310 USE_TEXT_SECTION()
2311
2312 /*
2313  * Hash table stuff
2314  */
2315         .balign IFETCH_ALIGN_BYTES
2316 do_hash_page:
2317 #ifdef CONFIG_PPC_BOOK3S_64
2318         lis     r0,(DSISR_BAD_FAULT_64S | DSISR_DABRMATCH | DSISR_KEYFAULT)@h
2319         ori     r0,r0,DSISR_BAD_FAULT_64S@l
2320         and.    r0,r4,r0                /* weird error? */
2321         bne-    handle_page_fault       /* if not, try to insert a HPTE */
2322         ld      r11, PACA_THREAD_INFO(r13)
2323         lwz     r0,TI_PREEMPT(r11)      /* If we're in an "NMI" */
2324         andis.  r0,r0,NMI_MASK@h        /* (i.e. an irq when soft-disabled) */
2325         bne     77f                     /* then don't call hash_page now */
2326
2327         /*
2328          * r3 contains the faulting address
2329          * r4 msr
2330          * r5 contains the trap number
2331          * r6 contains dsisr
2332          *
2333          * at return r3 = 0 for success, 1 for page fault, negative for error
2334          */
2335         mr      r4,r12
2336         ld      r6,_DSISR(r1)
2337         bl      __hash_page             /* build HPTE if possible */
2338         cmpdi   r3,0                    /* see if __hash_page succeeded */
2339
2340         /* Success */
2341         beq     fast_exc_return_irq     /* Return from exception on success */
2342
2343         /* Error */
2344         blt-    13f
2345
2346         /* Reload DSISR into r4 for the DABR check below */
2347         ld      r4,_DSISR(r1)
2348 #endif /* CONFIG_PPC_BOOK3S_64 */
2349
2350 /* Here we have a page fault that hash_page can't handle. */
2351 handle_page_fault:
2352 11:     andis.  r0,r4,DSISR_DABRMATCH@h
2353         bne-    handle_dabr_fault
2354         ld      r4,_DAR(r1)
2355         ld      r5,_DSISR(r1)
2356         addi    r3,r1,STACK_FRAME_OVERHEAD
2357         bl      do_page_fault
2358         cmpdi   r3,0
2359         beq+    ret_from_except_lite
2360         bl      save_nvgprs
2361         mr      r5,r3
2362         addi    r3,r1,STACK_FRAME_OVERHEAD
2363         lwz     r4,_DAR(r1)
2364         bl      bad_page_fault
2365         b       ret_from_except
2366
2367 /* We have a data breakpoint exception - handle it */
2368 handle_dabr_fault:
2369         bl      save_nvgprs
2370         ld      r4,_DAR(r1)
2371         ld      r5,_DSISR(r1)
2372         addi    r3,r1,STACK_FRAME_OVERHEAD
2373         bl      do_break
2374         /*
2375          * do_break() may have changed the NV GPRS while handling a breakpoint.
2376          * If so, we need to restore them with their updated values. Don't use
2377          * ret_from_except_lite here.
2378          */
2379         b       ret_from_except
2380
2381
2382 #ifdef CONFIG_PPC_BOOK3S_64
2383 /* We have a page fault that hash_page could handle but HV refused
2384  * the PTE insertion
2385  */
2386 13:     bl      save_nvgprs
2387         mr      r5,r3
2388         addi    r3,r1,STACK_FRAME_OVERHEAD
2389         ld      r4,_DAR(r1)
2390         bl      low_hash_fault
2391         b       ret_from_except
2392 #endif
2393
2394 /*
2395  * We come here as a result of a DSI at a point where we don't want
2396  * to call hash_page, such as when we are accessing memory (possibly
2397  * user memory) inside a PMU interrupt that occurred while interrupts
2398  * were soft-disabled.  We want to invoke the exception handler for
2399  * the access, or panic if there isn't a handler.
2400  */
2401 77:     bl      save_nvgprs
2402         mr      r4,r3
2403         addi    r3,r1,STACK_FRAME_OVERHEAD
2404         li      r5,SIGSEGV
2405         bl      bad_page_fault
2406         b       ret_from_except
2407
2408 /*
2409  * When doorbell is triggered from system reset wakeup, the message is
2410  * not cleared, so it would fire again when EE is enabled.
2411  *
2412  * When coming from local_irq_enable, there may be the same problem if
2413  * we were hard disabled.
2414  *
2415  * Execute msgclr to clear pending exceptions before handling it.
2416  */
2417 h_doorbell_common_msgclr:
2418         LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
2419         PPC_MSGCLR(3)
2420         b       h_doorbell_common
2421
2422 doorbell_super_common_msgclr:
2423         LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
2424         PPC_MSGCLRP(3)
2425         b       doorbell_super_common
2426
2427 /*
2428  * Called from arch_local_irq_enable when an interrupt needs
2429  * to be resent. r3 contains 0x500, 0x900, 0xa00 or 0xe80 to indicate
2430  * which kind of interrupt. MSR:EE is already off. We generate a
2431  * stackframe like if a real interrupt had happened.
2432  *
2433  * Note: While MSR:EE is off, we need to make sure that _MSR
2434  * in the generated frame has EE set to 1 or the exception
2435  * handler will not properly re-enable them.
2436  *
2437  * Note that we don't specify LR as the NIP (return address) for
2438  * the interrupt because that would unbalance the return branch
2439  * predictor.
2440  */
2441 _GLOBAL(__replay_interrupt)
2442         /* We are going to jump to the exception common code which
2443          * will retrieve various register values from the PACA which
2444          * we don't give a damn about, so we don't bother storing them.
2445          */
2446         mfmsr   r12
2447         LOAD_REG_ADDR(r11, replay_interrupt_return)
2448         mfcr    r9
2449         ori     r12,r12,MSR_EE
2450         cmpwi   r3,0x900
2451         beq     decrementer_common
2452         cmpwi   r3,0x500
2453 BEGIN_FTR_SECTION
2454         beq     h_virt_irq_common
2455 FTR_SECTION_ELSE
2456         beq     hardware_interrupt_common
2457 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_300)
2458         cmpwi   r3,0xf00
2459         beq     performance_monitor_common
2460 BEGIN_FTR_SECTION
2461         cmpwi   r3,0xa00
2462         beq     h_doorbell_common_msgclr
2463         cmpwi   r3,0xe60
2464         beq     hmi_exception_common
2465 FTR_SECTION_ELSE
2466         cmpwi   r3,0xa00
2467         beq     doorbell_super_common_msgclr
2468 ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
2469 replay_interrupt_return:
2470         blr
2471
2472 _ASM_NOKPROBE_SYMBOL(__replay_interrupt)