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