Merge tag 'firewire-updates' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee139...
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / kernel / exceptions-64s.S
1 /*
2  * This file contains the 64-bit "server" PowerPC variant
3  * of the low level exception handling including exception
4  * vectors, exception return, part of the slb and stab
5  * handling and other fixed offset specific things.
6  *
7  * This file is meant to be #included from head_64.S due to
8  * position dependent assembly.
9  *
10  * Most of this originates from head_64.S and thus has the same
11  * copyright history.
12  *
13  */
14
15 #include <asm/hw_irq.h>
16 #include <asm/exception-64s.h>
17 #include <asm/ptrace.h>
18
19 /*
20  * We layout physical memory as follows:
21  * 0x0000 - 0x00ff : Secondary processor spin code
22  * 0x0100 - 0x2fff : pSeries Interrupt prologs
23  * 0x3000 - 0x5fff : interrupt support common interrupt prologs
24  * 0x6000 - 0x6fff : Initial (CPU0) segment table
25  * 0x7000 - 0x7fff : FWNMI data area
26  * 0x8000 -        : Early init and support code
27  */
28
29 /*
30  * This is the start of the interrupt handlers for pSeries
31  * This code runs with relocation off.
32  * Code from here to __end_interrupts gets copied down to real
33  * address 0x100 when we are running a relocatable kernel.
34  * Therefore any relative branches in this section must only
35  * branch to labels in this section.
36  */
37         . = 0x100
38         .globl __start_interrupts
39 __start_interrupts:
40
41         .globl system_reset_pSeries;
42 system_reset_pSeries:
43         HMT_MEDIUM;
44         SET_SCRATCH0(r13)
45 #ifdef CONFIG_PPC_P7_NAP
46 BEGIN_FTR_SECTION
47         /* Running native on arch 2.06 or later, check if we are
48          * waking up from nap. We only handle no state loss and
49          * supervisor state loss. We do -not- handle hypervisor
50          * state loss at this time.
51          */
52         mfspr   r13,SPRN_SRR1
53         rlwinm. r13,r13,47-31,30,31
54         beq     9f
55
56         /* waking up from powersave (nap) state */
57         cmpwi   cr1,r13,2
58         /* Total loss of HV state is fatal, we could try to use the
59          * PIR to locate a PACA, then use an emergency stack etc...
60          * but for now, let's just stay stuck here
61          */
62         bgt     cr1,.
63         GET_PACA(r13)
64
65 #ifdef CONFIG_KVM_BOOK3S_64_HV
66         lbz     r0,PACAPROCSTART(r13)
67         cmpwi   r0,0x80
68         bne     1f
69         li      r0,1
70         stb     r0,PACAPROCSTART(r13)
71         b       kvm_start_guest
72 1:
73 #endif
74
75         beq     cr1,2f
76         b       .power7_wakeup_noloss
77 2:      b       .power7_wakeup_loss
78 9:
79 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
80 #endif /* CONFIG_PPC_P7_NAP */
81         EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, system_reset_common, EXC_STD,
82                                  NOTEST, 0x100)
83
84         . = 0x200
85 machine_check_pSeries_1:
86         /* This is moved out of line as it can be patched by FW, but
87          * some code path might still want to branch into the original
88          * vector
89          */
90         b       machine_check_pSeries
91
92         . = 0x300
93         .globl data_access_pSeries
94 data_access_pSeries:
95         HMT_MEDIUM
96         SET_SCRATCH0(r13)
97 BEGIN_FTR_SECTION
98         b       data_access_check_stab
99 data_access_not_stab:
100 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_SLB)
101         EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, data_access_common, EXC_STD,
102                                  KVMTEST, 0x300)
103
104         . = 0x380
105         .globl data_access_slb_pSeries
106 data_access_slb_pSeries:
107         HMT_MEDIUM
108         SET_SCRATCH0(r13)
109         EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST, 0x380)
110         std     r3,PACA_EXSLB+EX_R3(r13)
111         mfspr   r3,SPRN_DAR
112 #ifdef __DISABLED__
113         /* Keep that around for when we re-implement dynamic VSIDs */
114         cmpdi   r3,0
115         bge     slb_miss_user_pseries
116 #endif /* __DISABLED__ */
117         mfspr   r12,SPRN_SRR1
118 #ifndef CONFIG_RELOCATABLE
119         b       .slb_miss_realmode
120 #else
121         /*
122          * We can't just use a direct branch to .slb_miss_realmode
123          * because the distance from here to there depends on where
124          * the kernel ends up being put.
125          */
126         mfctr   r11
127         ld      r10,PACAKBASE(r13)
128         LOAD_HANDLER(r10, .slb_miss_realmode)
129         mtctr   r10
130         bctr
131 #endif
132
133         STD_EXCEPTION_PSERIES(0x400, 0x400, instruction_access)
134
135         . = 0x480
136         .globl instruction_access_slb_pSeries
137 instruction_access_slb_pSeries:
138         HMT_MEDIUM
139         SET_SCRATCH0(r13)
140         EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST_PR, 0x480)
141         std     r3,PACA_EXSLB+EX_R3(r13)
142         mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
143 #ifdef __DISABLED__
144         /* Keep that around for when we re-implement dynamic VSIDs */
145         cmpdi   r3,0
146         bge     slb_miss_user_pseries
147 #endif /* __DISABLED__ */
148         mfspr   r12,SPRN_SRR1
149 #ifndef CONFIG_RELOCATABLE
150         b       .slb_miss_realmode
151 #else
152         mfctr   r11
153         ld      r10,PACAKBASE(r13)
154         LOAD_HANDLER(r10, .slb_miss_realmode)
155         mtctr   r10
156         bctr
157 #endif
158
159         /* We open code these as we can't have a ". = x" (even with
160          * x = "." within a feature section
161          */
162         . = 0x500;
163         .globl hardware_interrupt_pSeries;
164         .globl hardware_interrupt_hv;
165 hardware_interrupt_pSeries:
166 hardware_interrupt_hv:
167         BEGIN_FTR_SECTION
168                 _MASKABLE_EXCEPTION_PSERIES(0x502, hardware_interrupt,
169                                             EXC_HV, SOFTEN_TEST_HV)
170                 KVM_HANDLER(PACA_EXGEN, EXC_HV, 0x502)
171         FTR_SECTION_ELSE
172                 _MASKABLE_EXCEPTION_PSERIES(0x500, hardware_interrupt,
173                                             EXC_STD, SOFTEN_TEST_HV_201)
174                 KVM_HANDLER(PACA_EXGEN, EXC_STD, 0x500)
175         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
176
177         STD_EXCEPTION_PSERIES(0x600, 0x600, alignment)
178         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x600)
179
180         STD_EXCEPTION_PSERIES(0x700, 0x700, program_check)
181         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x700)
182
183         STD_EXCEPTION_PSERIES(0x800, 0x800, fp_unavailable)
184         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x800)
185
186         MASKABLE_EXCEPTION_PSERIES(0x900, 0x900, decrementer)
187         MASKABLE_EXCEPTION_HV(0x980, 0x982, decrementer)
188
189         STD_EXCEPTION_PSERIES(0xa00, 0xa00, trap_0a)
190         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xa00)
191
192         STD_EXCEPTION_PSERIES(0xb00, 0xb00, trap_0b)
193         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xb00)
194
195         . = 0xc00
196         .globl  system_call_pSeries
197 system_call_pSeries:
198         HMT_MEDIUM
199 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
200         SET_SCRATCH0(r13)
201         GET_PACA(r13)
202         std     r9,PACA_EXGEN+EX_R9(r13)
203         std     r10,PACA_EXGEN+EX_R10(r13)
204         mfcr    r9
205         KVMTEST(0xc00)
206         GET_SCRATCH0(r13)
207 #endif
208 BEGIN_FTR_SECTION
209         cmpdi   r0,0x1ebe
210         beq-    1f
211 END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE)
212         mr      r9,r13
213         GET_PACA(r13)
214         mfspr   r11,SPRN_SRR0
215         mfspr   r12,SPRN_SRR1
216         ld      r10,PACAKBASE(r13)
217         LOAD_HANDLER(r10, system_call_entry)
218         mtspr   SPRN_SRR0,r10
219         ld      r10,PACAKMSR(r13)
220         mtspr   SPRN_SRR1,r10
221         rfid
222         b       .       /* prevent speculative execution */
223
224         KVM_HANDLER(PACA_EXGEN, EXC_STD, 0xc00)
225
226 /* Fast LE/BE switch system call */
227 1:      mfspr   r12,SPRN_SRR1
228         xori    r12,r12,MSR_LE
229         mtspr   SPRN_SRR1,r12
230         rfid            /* return to userspace */
231         b       .
232
233         STD_EXCEPTION_PSERIES(0xd00, 0xd00, single_step)
234         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xd00)
235
236         /* At 0xe??? we have a bunch of hypervisor exceptions, we branch
237          * out of line to handle them
238          */
239         . = 0xe00
240         b       h_data_storage_hv
241         . = 0xe20
242         b       h_instr_storage_hv
243         . = 0xe40
244         b       emulation_assist_hv
245         . = 0xe50
246         b       hmi_exception_hv
247         . = 0xe60
248         b       hmi_exception_hv
249
250         /* We need to deal with the Altivec unavailable exception
251          * here which is at 0xf20, thus in the middle of the
252          * prolog code of the PerformanceMonitor one. A little
253          * trickery is thus necessary
254          */
255 performance_monitor_pSeries_1:
256         . = 0xf00
257         b       performance_monitor_pSeries
258
259 altivec_unavailable_pSeries_1:
260         . = 0xf20
261         b       altivec_unavailable_pSeries
262
263 vsx_unavailable_pSeries_1:
264         . = 0xf40
265         b       vsx_unavailable_pSeries
266
267 #ifdef CONFIG_CBE_RAS
268         STD_EXCEPTION_HV(0x1200, 0x1202, cbe_system_error)
269         KVM_HANDLER_SKIP(PACA_EXGEN, EXC_HV, 0x1202)
270 #endif /* CONFIG_CBE_RAS */
271
272         STD_EXCEPTION_PSERIES(0x1300, 0x1300, instruction_breakpoint)
273         KVM_HANDLER_PR_SKIP(PACA_EXGEN, EXC_STD, 0x1300)
274
275 #ifdef CONFIG_CBE_RAS
276         STD_EXCEPTION_HV(0x1600, 0x1602, cbe_maintenance)
277         KVM_HANDLER_SKIP(PACA_EXGEN, EXC_HV, 0x1602)
278 #endif /* CONFIG_CBE_RAS */
279
280         STD_EXCEPTION_PSERIES(0x1700, 0x1700, altivec_assist)
281         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x1700)
282
283 #ifdef CONFIG_CBE_RAS
284         STD_EXCEPTION_HV(0x1800, 0x1802, cbe_thermal)
285         KVM_HANDLER_SKIP(PACA_EXGEN, EXC_HV, 0x1802)
286 #endif /* CONFIG_CBE_RAS */
287
288         . = 0x3000
289
290 /*** Out of line interrupts support ***/
291
292         /* moved from 0x200 */
293 machine_check_pSeries:
294         .globl machine_check_fwnmi
295 machine_check_fwnmi:
296         HMT_MEDIUM
297         SET_SCRATCH0(r13)               /* save r13 */
298         EXCEPTION_PROLOG_PSERIES(PACA_EXMC, machine_check_common,
299                                  EXC_STD, KVMTEST, 0x200)
300         KVM_HANDLER_SKIP(PACA_EXMC, EXC_STD, 0x200)
301
302         /* moved from 0x300 */
303 data_access_check_stab:
304         GET_PACA(r13)
305         std     r9,PACA_EXSLB+EX_R9(r13)
306         std     r10,PACA_EXSLB+EX_R10(r13)
307         mfspr   r10,SPRN_DAR
308         mfspr   r9,SPRN_DSISR
309         srdi    r10,r10,60
310         rlwimi  r10,r9,16,0x20
311 #ifdef CONFIG_KVM_BOOK3S_PR
312         lbz     r9,HSTATE_IN_GUEST(r13)
313         rlwimi  r10,r9,8,0x300
314 #endif
315         mfcr    r9
316         cmpwi   r10,0x2c
317         beq     do_stab_bolted_pSeries
318         mtcrf   0x80,r9
319         ld      r9,PACA_EXSLB+EX_R9(r13)
320         ld      r10,PACA_EXSLB+EX_R10(r13)
321         b       data_access_not_stab
322 do_stab_bolted_pSeries:
323         std     r11,PACA_EXSLB+EX_R11(r13)
324         std     r12,PACA_EXSLB+EX_R12(r13)
325         GET_SCRATCH0(r10)
326         std     r10,PACA_EXSLB+EX_R13(r13)
327         EXCEPTION_PROLOG_PSERIES_1(.do_stab_bolted, EXC_STD)
328
329         KVM_HANDLER_SKIP(PACA_EXGEN, EXC_STD, 0x300)
330         KVM_HANDLER_SKIP(PACA_EXSLB, EXC_STD, 0x380)
331         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x400)
332         KVM_HANDLER_PR(PACA_EXSLB, EXC_STD, 0x480)
333         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0x900)
334         KVM_HANDLER(PACA_EXGEN, EXC_HV, 0x982)
335
336         .align  7
337         /* moved from 0xe00 */
338         STD_EXCEPTION_HV(., 0xe02, h_data_storage)
339         KVM_HANDLER_SKIP(PACA_EXGEN, EXC_HV, 0xe02)
340         STD_EXCEPTION_HV(., 0xe22, h_instr_storage)
341         KVM_HANDLER(PACA_EXGEN, EXC_HV, 0xe22)
342         STD_EXCEPTION_HV(., 0xe42, emulation_assist)
343         KVM_HANDLER(PACA_EXGEN, EXC_HV, 0xe42)
344         STD_EXCEPTION_HV(., 0xe62, hmi_exception) /* need to flush cache ? */
345         KVM_HANDLER(PACA_EXGEN, EXC_HV, 0xe62)
346
347         /* moved from 0xf00 */
348         STD_EXCEPTION_PSERIES(., 0xf00, performance_monitor)
349         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xf00)
350         STD_EXCEPTION_PSERIES(., 0xf20, altivec_unavailable)
351         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xf20)
352         STD_EXCEPTION_PSERIES(., 0xf40, vsx_unavailable)
353         KVM_HANDLER_PR(PACA_EXGEN, EXC_STD, 0xf40)
354
355 /*
356  * An interrupt came in while soft-disabled. We set paca->irq_happened,
357  * then, if it was a decrementer interrupt, we bump the dec to max and
358  * and return, else we hard disable and return. This is called with
359  * r10 containing the value to OR to the paca field.
360  */
361 #define MASKED_INTERRUPT(_H)                            \
362 masked_##_H##interrupt:                                 \
363         std     r11,PACA_EXGEN+EX_R11(r13);             \
364         lbz     r11,PACAIRQHAPPENED(r13);               \
365         or      r11,r11,r10;                            \
366         stb     r11,PACAIRQHAPPENED(r13);               \
367         andi.   r10,r10,PACA_IRQ_DEC;                   \
368         beq     1f;                                     \
369         lis     r10,0x7fff;                             \
370         ori     r10,r10,0xffff;                         \
371         mtspr   SPRN_DEC,r10;                           \
372         b       2f;                                     \
373 1:      mfspr   r10,SPRN_##_H##SRR1;                    \
374         rldicl  r10,r10,48,1; /* clear MSR_EE */        \
375         rotldi  r10,r10,16;                             \
376         mtspr   SPRN_##_H##SRR1,r10;                    \
377 2:      mtcrf   0x80,r9;                                \
378         ld      r9,PACA_EXGEN+EX_R9(r13);               \
379         ld      r10,PACA_EXGEN+EX_R10(r13);             \
380         ld      r11,PACA_EXGEN+EX_R11(r13);             \
381         GET_SCRATCH0(r13);                              \
382         ##_H##rfid;                                     \
383         b       .
384         
385         MASKED_INTERRUPT()
386         MASKED_INTERRUPT(H)
387
388 /*
389  * Called from arch_local_irq_enable when an interrupt needs
390  * to be resent. r3 contains 0x500 or 0x900 to indicate which
391  * kind of interrupt. MSR:EE is already off. We generate a
392  * stackframe like if a real interrupt had happened.
393  *
394  * Note: While MSR:EE is off, we need to make sure that _MSR
395  * in the generated frame has EE set to 1 or the exception
396  * handler will not properly re-enable them.
397  */
398 _GLOBAL(__replay_interrupt)
399         /* We are going to jump to the exception common code which
400          * will retrieve various register values from the PACA which
401          * we don't give a damn about, so we don't bother storing them.
402          */
403         mfmsr   r12
404         mflr    r11
405         mfcr    r9
406         ori     r12,r12,MSR_EE
407         andi.   r3,r3,0x0800
408         bne     decrementer_common
409         b       hardware_interrupt_common
410
411 #ifdef CONFIG_PPC_PSERIES
412 /*
413  * Vectors for the FWNMI option.  Share common code.
414  */
415         .globl system_reset_fwnmi
416       .align 7
417 system_reset_fwnmi:
418         HMT_MEDIUM
419         SET_SCRATCH0(r13)               /* save r13 */
420         EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, system_reset_common, EXC_STD,
421                                  NOTEST, 0x100)
422
423 #endif /* CONFIG_PPC_PSERIES */
424
425 #ifdef __DISABLED__
426 /*
427  * This is used for when the SLB miss handler has to go virtual,
428  * which doesn't happen for now anymore but will once we re-implement
429  * dynamic VSIDs for shared page tables
430  */
431 slb_miss_user_pseries:
432         std     r10,PACA_EXGEN+EX_R10(r13)
433         std     r11,PACA_EXGEN+EX_R11(r13)
434         std     r12,PACA_EXGEN+EX_R12(r13)
435         GET_SCRATCH0(r10)
436         ld      r11,PACA_EXSLB+EX_R9(r13)
437         ld      r12,PACA_EXSLB+EX_R3(r13)
438         std     r10,PACA_EXGEN+EX_R13(r13)
439         std     r11,PACA_EXGEN+EX_R9(r13)
440         std     r12,PACA_EXGEN+EX_R3(r13)
441         clrrdi  r12,r13,32
442         mfmsr   r10
443         mfspr   r11,SRR0                        /* save SRR0 */
444         ori     r12,r12,slb_miss_user_common@l  /* virt addr of handler */
445         ori     r10,r10,MSR_IR|MSR_DR|MSR_RI
446         mtspr   SRR0,r12
447         mfspr   r12,SRR1                        /* and SRR1 */
448         mtspr   SRR1,r10
449         rfid
450         b       .                               /* prevent spec. execution */
451 #endif /* __DISABLED__ */
452
453         .align  7
454         .globl  __end_interrupts
455 __end_interrupts:
456
457 /*
458  * Code from here down to __end_handlers is invoked from the
459  * exception prologs above.  Because the prologs assemble the
460  * addresses of these handlers using the LOAD_HANDLER macro,
461  * which uses an addi instruction, these handlers must be in
462  * the first 32k of the kernel image.
463  */
464
465 /*** Common interrupt handlers ***/
466
467         STD_EXCEPTION_COMMON(0x100, system_reset, .system_reset_exception)
468
469         /*
470          * Machine check is different because we use a different
471          * save area: PACA_EXMC instead of PACA_EXGEN.
472          */
473         .align  7
474         .globl machine_check_common
475 machine_check_common:
476         EXCEPTION_PROLOG_COMMON(0x200, PACA_EXMC)
477         FINISH_NAP
478         DISABLE_INTS
479         bl      .save_nvgprs
480         addi    r3,r1,STACK_FRAME_OVERHEAD
481         bl      .machine_check_exception
482         b       .ret_from_except
483
484         STD_EXCEPTION_COMMON_ASYNC(0x500, hardware_interrupt, do_IRQ)
485         STD_EXCEPTION_COMMON_ASYNC(0x900, decrementer, .timer_interrupt)
486         STD_EXCEPTION_COMMON(0xa00, trap_0a, .unknown_exception)
487         STD_EXCEPTION_COMMON(0xb00, trap_0b, .unknown_exception)
488         STD_EXCEPTION_COMMON(0xd00, single_step, .single_step_exception)
489         STD_EXCEPTION_COMMON(0xe00, trap_0e, .unknown_exception)
490         STD_EXCEPTION_COMMON(0xe40, emulation_assist, .program_check_exception)
491         STD_EXCEPTION_COMMON(0xe60, hmi_exception, .unknown_exception)
492         STD_EXCEPTION_COMMON_ASYNC(0xf00, performance_monitor, .performance_monitor_exception)
493         STD_EXCEPTION_COMMON(0x1300, instruction_breakpoint, .instruction_breakpoint_exception)
494 #ifdef CONFIG_ALTIVEC
495         STD_EXCEPTION_COMMON(0x1700, altivec_assist, .altivec_assist_exception)
496 #else
497         STD_EXCEPTION_COMMON(0x1700, altivec_assist, .unknown_exception)
498 #endif
499 #ifdef CONFIG_CBE_RAS
500         STD_EXCEPTION_COMMON(0x1200, cbe_system_error, .cbe_system_error_exception)
501         STD_EXCEPTION_COMMON(0x1600, cbe_maintenance, .cbe_maintenance_exception)
502         STD_EXCEPTION_COMMON(0x1800, cbe_thermal, .cbe_thermal_exception)
503 #endif /* CONFIG_CBE_RAS */
504
505         .align  7
506 system_call_entry:
507         b       system_call_common
508
509 ppc64_runlatch_on_trampoline:
510         b       .__ppc64_runlatch_on
511
512 /*
513  * Here we have detected that the kernel stack pointer is bad.
514  * R9 contains the saved CR, r13 points to the paca,
515  * r10 contains the (bad) kernel stack pointer,
516  * r11 and r12 contain the saved SRR0 and SRR1.
517  * We switch to using an emergency stack, save the registers there,
518  * and call kernel_bad_stack(), which panics.
519  */
520 bad_stack:
521         ld      r1,PACAEMERGSP(r13)
522         subi    r1,r1,64+INT_FRAME_SIZE
523         std     r9,_CCR(r1)
524         std     r10,GPR1(r1)
525         std     r11,_NIP(r1)
526         std     r12,_MSR(r1)
527         mfspr   r11,SPRN_DAR
528         mfspr   r12,SPRN_DSISR
529         std     r11,_DAR(r1)
530         std     r12,_DSISR(r1)
531         mflr    r10
532         mfctr   r11
533         mfxer   r12
534         std     r10,_LINK(r1)
535         std     r11,_CTR(r1)
536         std     r12,_XER(r1)
537         SAVE_GPR(0,r1)
538         SAVE_GPR(2,r1)
539         ld      r10,EX_R3(r3)
540         std     r10,GPR3(r1)
541         SAVE_GPR(4,r1)
542         SAVE_4GPRS(5,r1)
543         ld      r9,EX_R9(r3)
544         ld      r10,EX_R10(r3)
545         SAVE_2GPRS(9,r1)
546         ld      r9,EX_R11(r3)
547         ld      r10,EX_R12(r3)
548         ld      r11,EX_R13(r3)
549         std     r9,GPR11(r1)
550         std     r10,GPR12(r1)
551         std     r11,GPR13(r1)
552 BEGIN_FTR_SECTION
553         ld      r10,EX_CFAR(r3)
554         std     r10,ORIG_GPR3(r1)
555 END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
556         SAVE_8GPRS(14,r1)
557         SAVE_10GPRS(22,r1)
558         lhz     r12,PACA_TRAP_SAVE(r13)
559         std     r12,_TRAP(r1)
560         addi    r11,r1,INT_FRAME_SIZE
561         std     r11,0(r1)
562         li      r12,0
563         std     r12,0(r11)
564         ld      r2,PACATOC(r13)
565         ld      r11,exception_marker@toc(r2)
566         std     r12,RESULT(r1)
567         std     r11,STACK_FRAME_OVERHEAD-16(r1)
568 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
569         bl      .kernel_bad_stack
570         b       1b
571
572 /*
573  * Here r13 points to the paca, r9 contains the saved CR,
574  * SRR0 and SRR1 are saved in r11 and r12,
575  * r9 - r13 are saved in paca->exgen.
576  */
577         .align  7
578         .globl data_access_common
579 data_access_common:
580         mfspr   r10,SPRN_DAR
581         std     r10,PACA_EXGEN+EX_DAR(r13)
582         mfspr   r10,SPRN_DSISR
583         stw     r10,PACA_EXGEN+EX_DSISR(r13)
584         EXCEPTION_PROLOG_COMMON(0x300, PACA_EXGEN)
585         DISABLE_INTS
586         ld      r12,_MSR(r1)
587         ld      r3,PACA_EXGEN+EX_DAR(r13)
588         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
589         li      r5,0x300
590         b       .do_hash_page           /* Try to handle as hpte fault */
591
592         .align  7
593         .globl  h_data_storage_common
594 h_data_storage_common:
595         mfspr   r10,SPRN_HDAR
596         std     r10,PACA_EXGEN+EX_DAR(r13)
597         mfspr   r10,SPRN_HDSISR
598         stw     r10,PACA_EXGEN+EX_DSISR(r13)
599         EXCEPTION_PROLOG_COMMON(0xe00, PACA_EXGEN)
600         bl      .save_nvgprs
601         DISABLE_INTS
602         addi    r3,r1,STACK_FRAME_OVERHEAD
603         bl      .unknown_exception
604         b       .ret_from_except
605
606         .align  7
607         .globl instruction_access_common
608 instruction_access_common:
609         EXCEPTION_PROLOG_COMMON(0x400, PACA_EXGEN)
610         DISABLE_INTS
611         ld      r12,_MSR(r1)
612         ld      r3,_NIP(r1)
613         andis.  r4,r12,0x5820
614         li      r5,0x400
615         b       .do_hash_page           /* Try to handle as hpte fault */
616
617         STD_EXCEPTION_COMMON(0xe20, h_instr_storage, .unknown_exception)
618
619 /*
620  * Here is the common SLB miss user that is used when going to virtual
621  * mode for SLB misses, that is currently not used
622  */
623 #ifdef __DISABLED__
624         .align  7
625         .globl  slb_miss_user_common
626 slb_miss_user_common:
627         mflr    r10
628         std     r3,PACA_EXGEN+EX_DAR(r13)
629         stw     r9,PACA_EXGEN+EX_CCR(r13)
630         std     r10,PACA_EXGEN+EX_LR(r13)
631         std     r11,PACA_EXGEN+EX_SRR0(r13)
632         bl      .slb_allocate_user
633
634         ld      r10,PACA_EXGEN+EX_LR(r13)
635         ld      r3,PACA_EXGEN+EX_R3(r13)
636         lwz     r9,PACA_EXGEN+EX_CCR(r13)
637         ld      r11,PACA_EXGEN+EX_SRR0(r13)
638         mtlr    r10
639         beq-    slb_miss_fault
640
641         andi.   r10,r12,MSR_RI          /* check for unrecoverable exception */
642         beq-    unrecov_user_slb
643         mfmsr   r10
644
645 .machine push
646 .machine "power4"
647         mtcrf   0x80,r9
648 .machine pop
649
650         clrrdi  r10,r10,2               /* clear RI before setting SRR0/1 */
651         mtmsrd  r10,1
652
653         mtspr   SRR0,r11
654         mtspr   SRR1,r12
655
656         ld      r9,PACA_EXGEN+EX_R9(r13)
657         ld      r10,PACA_EXGEN+EX_R10(r13)
658         ld      r11,PACA_EXGEN+EX_R11(r13)
659         ld      r12,PACA_EXGEN+EX_R12(r13)
660         ld      r13,PACA_EXGEN+EX_R13(r13)
661         rfid
662         b       .
663
664 slb_miss_fault:
665         EXCEPTION_PROLOG_COMMON(0x380, PACA_EXGEN)
666         ld      r4,PACA_EXGEN+EX_DAR(r13)
667         li      r5,0
668         std     r4,_DAR(r1)
669         std     r5,_DSISR(r1)
670         b       handle_page_fault
671
672 unrecov_user_slb:
673         EXCEPTION_PROLOG_COMMON(0x4200, PACA_EXGEN)
674         DISABLE_INTS
675         bl      .save_nvgprs
676 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
677         bl      .unrecoverable_exception
678         b       1b
679
680 #endif /* __DISABLED__ */
681
682
683 /*
684  * r13 points to the PACA, r9 contains the saved CR,
685  * r12 contain the saved SRR1, SRR0 is still ready for return
686  * r3 has the faulting address
687  * r9 - r13 are saved in paca->exslb.
688  * r3 is saved in paca->slb_r3
689  * We assume we aren't going to take any exceptions during this procedure.
690  */
691 _GLOBAL(slb_miss_realmode)
692         mflr    r10
693 #ifdef CONFIG_RELOCATABLE
694         mtctr   r11
695 #endif
696
697         stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
698         std     r10,PACA_EXSLB+EX_LR(r13)       /* save LR */
699
700         bl      .slb_allocate_realmode
701
702         /* All done -- return from exception. */
703
704         ld      r10,PACA_EXSLB+EX_LR(r13)
705         ld      r3,PACA_EXSLB+EX_R3(r13)
706         lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
707
708         mtlr    r10
709
710         andi.   r10,r12,MSR_RI  /* check for unrecoverable exception */
711         beq-    2f
712
713 .machine        push
714 .machine        "power4"
715         mtcrf   0x80,r9
716         mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
717 .machine        pop
718
719         ld      r9,PACA_EXSLB+EX_R9(r13)
720         ld      r10,PACA_EXSLB+EX_R10(r13)
721         ld      r11,PACA_EXSLB+EX_R11(r13)
722         ld      r12,PACA_EXSLB+EX_R12(r13)
723         ld      r13,PACA_EXSLB+EX_R13(r13)
724         rfid
725         b       .       /* prevent speculative execution */
726
727 2:      mfspr   r11,SPRN_SRR0
728         ld      r10,PACAKBASE(r13)
729         LOAD_HANDLER(r10,unrecov_slb)
730         mtspr   SPRN_SRR0,r10
731         ld      r10,PACAKMSR(r13)
732         mtspr   SPRN_SRR1,r10
733         rfid
734         b       .
735
736 unrecov_slb:
737         EXCEPTION_PROLOG_COMMON(0x4100, PACA_EXSLB)
738         DISABLE_INTS
739         bl      .save_nvgprs
740 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
741         bl      .unrecoverable_exception
742         b       1b
743
744
745 #ifdef CONFIG_PPC_970_NAP
746 power4_fixup_nap:
747         andc    r9,r9,r10
748         std     r9,TI_LOCAL_FLAGS(r11)
749         ld      r10,_LINK(r1)           /* make idle task do the */
750         std     r10,_NIP(r1)            /* equivalent of a blr */
751         blr
752 #endif
753
754         .align  7
755         .globl alignment_common
756 alignment_common:
757         mfspr   r10,SPRN_DAR
758         std     r10,PACA_EXGEN+EX_DAR(r13)
759         mfspr   r10,SPRN_DSISR
760         stw     r10,PACA_EXGEN+EX_DSISR(r13)
761         EXCEPTION_PROLOG_COMMON(0x600, PACA_EXGEN)
762         ld      r3,PACA_EXGEN+EX_DAR(r13)
763         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
764         std     r3,_DAR(r1)
765         std     r4,_DSISR(r1)
766         bl      .save_nvgprs
767         DISABLE_INTS
768         addi    r3,r1,STACK_FRAME_OVERHEAD
769         bl      .alignment_exception
770         b       .ret_from_except
771
772         .align  7
773         .globl program_check_common
774 program_check_common:
775         EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
776         bl      .save_nvgprs
777         DISABLE_INTS
778         addi    r3,r1,STACK_FRAME_OVERHEAD
779         bl      .program_check_exception
780         b       .ret_from_except
781
782         .align  7
783         .globl fp_unavailable_common
784 fp_unavailable_common:
785         EXCEPTION_PROLOG_COMMON(0x800, PACA_EXGEN)
786         bne     1f                      /* if from user, just load it up */
787         bl      .save_nvgprs
788         DISABLE_INTS
789         addi    r3,r1,STACK_FRAME_OVERHEAD
790         bl      .kernel_fp_unavailable_exception
791         BUG_OPCODE
792 1:      bl      .load_up_fpu
793         b       fast_exception_return
794
795         .align  7
796         .globl altivec_unavailable_common
797 altivec_unavailable_common:
798         EXCEPTION_PROLOG_COMMON(0xf20, PACA_EXGEN)
799 #ifdef CONFIG_ALTIVEC
800 BEGIN_FTR_SECTION
801         beq     1f
802         bl      .load_up_altivec
803         b       fast_exception_return
804 1:
805 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
806 #endif
807         bl      .save_nvgprs
808         DISABLE_INTS
809         addi    r3,r1,STACK_FRAME_OVERHEAD
810         bl      .altivec_unavailable_exception
811         b       .ret_from_except
812
813         .align  7
814         .globl vsx_unavailable_common
815 vsx_unavailable_common:
816         EXCEPTION_PROLOG_COMMON(0xf40, PACA_EXGEN)
817 #ifdef CONFIG_VSX
818 BEGIN_FTR_SECTION
819         beq     1f
820         b       .load_up_vsx
821 1:
822 END_FTR_SECTION_IFSET(CPU_FTR_VSX)
823 #endif
824         bl      .save_nvgprs
825         DISABLE_INTS
826         addi    r3,r1,STACK_FRAME_OVERHEAD
827         bl      .vsx_unavailable_exception
828         b       .ret_from_except
829
830         .align  7
831         .globl  __end_handlers
832 __end_handlers:
833
834 /*
835  * Hash table stuff
836  */
837         .align  7
838 _STATIC(do_hash_page)
839         std     r3,_DAR(r1)
840         std     r4,_DSISR(r1)
841
842         andis.  r0,r4,0xa410            /* weird error? */
843         bne-    handle_page_fault       /* if not, try to insert a HPTE */
844         andis.  r0,r4,DSISR_DABRMATCH@h
845         bne-    handle_dabr_fault
846
847 BEGIN_FTR_SECTION
848         andis.  r0,r4,0x0020            /* Is it a segment table fault? */
849         bne-    do_ste_alloc            /* If so handle it */
850 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_SLB)
851
852         clrrdi  r11,r1,THREAD_SHIFT
853         lwz     r0,TI_PREEMPT(r11)      /* If we're in an "NMI" */
854         andis.  r0,r0,NMI_MASK@h        /* (i.e. an irq when soft-disabled) */
855         bne     77f                     /* then don't call hash_page now */
856         /*
857          * We need to set the _PAGE_USER bit if MSR_PR is set or if we are
858          * accessing a userspace segment (even from the kernel). We assume
859          * kernel addresses always have the high bit set.
860          */
861         rlwinm  r4,r4,32-25+9,31-9,31-9 /* DSISR_STORE -> _PAGE_RW */
862         rotldi  r0,r3,15                /* Move high bit into MSR_PR posn */
863         orc     r0,r12,r0               /* MSR_PR | ~high_bit */
864         rlwimi  r4,r0,32-13,30,30       /* becomes _PAGE_USER access bit */
865         ori     r4,r4,1                 /* add _PAGE_PRESENT */
866         rlwimi  r4,r5,22+2,31-2,31-2    /* Set _PAGE_EXEC if trap is 0x400 */
867
868         /*
869          * r3 contains the faulting address
870          * r4 contains the required access permissions
871          * r5 contains the trap number
872          *
873          * at return r3 = 0 for success, 1 for page fault, negative for error
874          */
875         bl      .hash_page              /* build HPTE if possible */
876         cmpdi   r3,0                    /* see if hash_page succeeded */
877
878         /* Success */
879         beq     fast_exc_return_irq     /* Return from exception on success */
880
881         /* Error */
882         blt-    13f
883
884 /* Here we have a page fault that hash_page can't handle. */
885 handle_page_fault:
886 11:     ld      r4,_DAR(r1)
887         ld      r5,_DSISR(r1)
888         addi    r3,r1,STACK_FRAME_OVERHEAD
889         bl      .do_page_fault
890         cmpdi   r3,0
891         beq+    12f
892         bl      .save_nvgprs
893         mr      r5,r3
894         addi    r3,r1,STACK_FRAME_OVERHEAD
895         lwz     r4,_DAR(r1)
896         bl      .bad_page_fault
897         b       .ret_from_except
898
899 /* We have a data breakpoint exception - handle it */
900 handle_dabr_fault:
901         bl      .save_nvgprs
902         ld      r4,_DAR(r1)
903         ld      r5,_DSISR(r1)
904         addi    r3,r1,STACK_FRAME_OVERHEAD
905         bl      .do_dabr
906 12:     b       .ret_from_except_lite
907
908
909 /* We have a page fault that hash_page could handle but HV refused
910  * the PTE insertion
911  */
912 13:     bl      .save_nvgprs
913         mr      r5,r3
914         addi    r3,r1,STACK_FRAME_OVERHEAD
915         ld      r4,_DAR(r1)
916         bl      .low_hash_fault
917         b       .ret_from_except
918
919 /*
920  * We come here as a result of a DSI at a point where we don't want
921  * to call hash_page, such as when we are accessing memory (possibly
922  * user memory) inside a PMU interrupt that occurred while interrupts
923  * were soft-disabled.  We want to invoke the exception handler for
924  * the access, or panic if there isn't a handler.
925  */
926 77:     bl      .save_nvgprs
927         mr      r4,r3
928         addi    r3,r1,STACK_FRAME_OVERHEAD
929         li      r5,SIGSEGV
930         bl      .bad_page_fault
931         b       .ret_from_except
932
933         /* here we have a segment miss */
934 do_ste_alloc:
935         bl      .ste_allocate           /* try to insert stab entry */
936         cmpdi   r3,0
937         bne-    handle_page_fault
938         b       fast_exception_return
939
940 /*
941  * r13 points to the PACA, r9 contains the saved CR,
942  * r11 and r12 contain the saved SRR0 and SRR1.
943  * r9 - r13 are saved in paca->exslb.
944  * We assume we aren't going to take any exceptions during this procedure.
945  * We assume (DAR >> 60) == 0xc.
946  */
947         .align  7
948 _GLOBAL(do_stab_bolted)
949         stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
950         std     r11,PACA_EXSLB+EX_SRR0(r13)     /* save SRR0 in exc. frame */
951
952         /* Hash to the primary group */
953         ld      r10,PACASTABVIRT(r13)
954         mfspr   r11,SPRN_DAR
955         srdi    r11,r11,28
956         rldimi  r10,r11,7,52    /* r10 = first ste of the group */
957
958         /* Calculate VSID */
959         /* This is a kernel address, so protovsid = ESID */
960         ASM_VSID_SCRAMBLE(r11, r9, 256M)
961         rldic   r9,r11,12,16    /* r9 = vsid << 12 */
962
963         /* Search the primary group for a free entry */
964 1:      ld      r11,0(r10)      /* Test valid bit of the current ste    */
965         andi.   r11,r11,0x80
966         beq     2f
967         addi    r10,r10,16
968         andi.   r11,r10,0x70
969         bne     1b
970
971         /* Stick for only searching the primary group for now.          */
972         /* At least for now, we use a very simple random castout scheme */
973         /* Use the TB as a random number ;  OR in 1 to avoid entry 0    */
974         mftb    r11
975         rldic   r11,r11,4,57    /* r11 = (r11 << 4) & 0x70 */
976         ori     r11,r11,0x10
977
978         /* r10 currently points to an ste one past the group of interest */
979         /* make it point to the randomly selected entry                 */
980         subi    r10,r10,128
981         or      r10,r10,r11     /* r10 is the entry to invalidate       */
982
983         isync                   /* mark the entry invalid               */
984         ld      r11,0(r10)
985         rldicl  r11,r11,56,1    /* clear the valid bit */
986         rotldi  r11,r11,8
987         std     r11,0(r10)
988         sync
989
990         clrrdi  r11,r11,28      /* Get the esid part of the ste         */
991         slbie   r11
992
993 2:      std     r9,8(r10)       /* Store the vsid part of the ste       */
994         eieio
995
996         mfspr   r11,SPRN_DAR            /* Get the new esid                     */
997         clrrdi  r11,r11,28      /* Permits a full 32b of ESID           */
998         ori     r11,r11,0x90    /* Turn on valid and kp                 */
999         std     r11,0(r10)      /* Put new entry back into the stab     */
1000
1001         sync
1002
1003         /* All done -- return from exception. */
1004         lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
1005         ld      r11,PACA_EXSLB+EX_SRR0(r13)     /* get saved SRR0 */
1006
1007         andi.   r10,r12,MSR_RI
1008         beq-    unrecov_slb
1009
1010         mtcrf   0x80,r9                 /* restore CR */
1011
1012         mfmsr   r10
1013         clrrdi  r10,r10,2
1014         mtmsrd  r10,1
1015
1016         mtspr   SPRN_SRR0,r11
1017         mtspr   SPRN_SRR1,r12
1018         ld      r9,PACA_EXSLB+EX_R9(r13)
1019         ld      r10,PACA_EXSLB+EX_R10(r13)
1020         ld      r11,PACA_EXSLB+EX_R11(r13)
1021         ld      r12,PACA_EXSLB+EX_R12(r13)
1022         ld      r13,PACA_EXSLB+EX_R13(r13)
1023         rfid
1024         b       .       /* prevent speculative execution */
1025
1026 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
1027 /*
1028  * Data area reserved for FWNMI option.
1029  * This address (0x7000) is fixed by the RPA.
1030  */
1031         .= 0x7000
1032         .globl fwnmi_data_area
1033 fwnmi_data_area:
1034
1035         /* pseries and powernv need to keep the whole page from
1036          * 0x7000 to 0x8000 free for use by the firmware
1037          */
1038         . = 0x8000
1039 #endif /* defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
1040
1041 /* Space for CPU0's segment table */
1042         .balign 4096
1043         .globl initial_stab
1044 initial_stab:
1045         .space  4096
1046
1047 #ifdef CONFIG_PPC_POWERNV
1048 _GLOBAL(opal_mc_secondary_handler)
1049         HMT_MEDIUM
1050         SET_SCRATCH0(r13)
1051         GET_PACA(r13)
1052         clrldi  r3,r3,2
1053         tovirt(r3,r3)
1054         std     r3,PACA_OPAL_MC_EVT(r13)
1055         ld      r13,OPAL_MC_SRR0(r3)
1056         mtspr   SPRN_SRR0,r13
1057         ld      r13,OPAL_MC_SRR1(r3)
1058         mtspr   SPRN_SRR1,r13
1059         ld      r3,OPAL_MC_GPR3(r3)
1060         GET_SCRATCH0(r13)
1061         b       machine_check_pSeries
1062 #endif /* CONFIG_PPC_POWERNV */