xref: /openbmc/linux/arch/powerpc/kernel/head_32.h (revision 2c59e5104821c5720e88bafa9e522f8bea9ce8fa)
18a23fdecSChristophe Leroy /* SPDX-License-Identifier: GPL-2.0 */
28a23fdecSChristophe Leroy #ifndef __HEAD_32_H__
38a23fdecSChristophe Leroy #define __HEAD_32_H__
48a23fdecSChristophe Leroy 
58a23fdecSChristophe Leroy #include <asm/ptrace.h>	/* for STACK_FRAME_REGS_MARKER */
68a23fdecSChristophe Leroy 
78a23fdecSChristophe Leroy /*
88a23fdecSChristophe Leroy  * Exception entry code.  This code runs with address translation
98a23fdecSChristophe Leroy  * turned off, i.e. using physical addresses.
108a23fdecSChristophe Leroy  * We assume sprg3 has the physical address of the current
118a23fdecSChristophe Leroy  * task's thread_struct.
128a23fdecSChristophe Leroy  */
1302847487SChristophe Leroy .macro EXCEPTION_PROLOG handle_dar_dsisr=0
1402847487SChristophe Leroy 	EXCEPTION_PROLOG_0	handle_dar_dsisr=\handle_dar_dsisr
151f1c4d01SChristophe Leroy 	EXCEPTION_PROLOG_1
1602847487SChristophe Leroy 	EXCEPTION_PROLOG_2	handle_dar_dsisr=\handle_dar_dsisr
171f1c4d01SChristophe Leroy .endm
181f1c4d01SChristophe Leroy 
1902847487SChristophe Leroy .macro EXCEPTION_PROLOG_0 handle_dar_dsisr=0
208a23fdecSChristophe Leroy 	mtspr	SPRN_SPRG_SCRATCH0,r10
218a23fdecSChristophe Leroy 	mtspr	SPRN_SPRG_SCRATCH1,r11
2202847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
2302847487SChristophe Leroy 	mfspr	r10, SPRN_SPRG_THREAD
2402847487SChristophe Leroy 	.if	\handle_dar_dsisr
2502847487SChristophe Leroy 	mfspr	r11, SPRN_DAR
2602847487SChristophe Leroy 	stw	r11, DAR(r10)
2702847487SChristophe Leroy 	mfspr	r11, SPRN_DSISR
2802847487SChristophe Leroy 	stw	r11, DSISR(r10)
2902847487SChristophe Leroy 	.endif
3002847487SChristophe Leroy 	mfspr	r11, SPRN_SRR0
3102847487SChristophe Leroy 	stw	r11, SRR0(r10)
3202847487SChristophe Leroy #endif
335ae8fabcSChristophe Leroy 	mfspr	r11, SPRN_SRR1		/* check whether user or kernel */
3402847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
3502847487SChristophe Leroy 	stw	r11, SRR1(r10)
3602847487SChristophe Leroy #endif
378a23fdecSChristophe Leroy 	mfcr	r10
385ae8fabcSChristophe Leroy 	andi.	r11, r11, MSR_PR
398a23fdecSChristophe Leroy .endm
408a23fdecSChristophe Leroy 
41cd08f109SChristophe Leroy .macro EXCEPTION_PROLOG_1 for_rtas=0
42da7bb43aSChristophe Leroy #ifdef CONFIG_VMAP_STACK
43d2e00603SChristophe Leroy 	mtspr	SPRN_SPRG_SCRATCH2,r1
44da7bb43aSChristophe Leroy 	subi	r1, r1, INT_FRAME_SIZE		/* use r1 if kernel */
45da7bb43aSChristophe Leroy 	beq	1f
46da7bb43aSChristophe Leroy 	mfspr	r1,SPRN_SPRG_THREAD
47da7bb43aSChristophe Leroy 	lwz	r1,TASK_STACK-THREAD(r1)
48da7bb43aSChristophe Leroy 	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
496285f9cfSChristophe Leroy 1:
503642eb21SChristophe Leroy 	mtcrf	0x3f, r1
516285f9cfSChristophe Leroy 	bt	32 - THREAD_ALIGN_SHIFT, stack_overflow
52da7bb43aSChristophe Leroy #else
5302847487SChristophe Leroy 	subi	r11, r1, INT_FRAME_SIZE		/* use r1 if kernel */
548a23fdecSChristophe Leroy 	beq	1f
558a23fdecSChristophe Leroy 	mfspr	r11,SPRN_SPRG_THREAD
568a23fdecSChristophe Leroy 	lwz	r11,TASK_STACK-THREAD(r11)
5702847487SChristophe Leroy 	addi	r11, r11, THREAD_SIZE - INT_FRAME_SIZE
586285f9cfSChristophe Leroy 1:	tophys(r11, r11)
593978eb78SChristophe Leroy #endif
608a23fdecSChristophe Leroy .endm
618a23fdecSChristophe Leroy 
6202847487SChristophe Leroy .macro EXCEPTION_PROLOG_2 handle_dar_dsisr=0
63c118c730SChristophe Leroy #ifdef CONFIG_VMAP_STACK
64d2e00603SChristophe Leroy 	li	r11, MSR_KERNEL & ~(MSR_IR | MSR_RI) /* can take DTLB miss */
65d2e00603SChristophe Leroy 	mtmsr	r11
66c118c730SChristophe Leroy 	isync
67d2e00603SChristophe Leroy 	mfspr	r11, SPRN_SPRG_SCRATCH2
68da7bb43aSChristophe Leroy 	stw	r11,GPR1(r1)
69da7bb43aSChristophe Leroy 	stw	r11,0(r1)
70da7bb43aSChristophe Leroy 	mr	r11, r1
71da7bb43aSChristophe Leroy #else
72da7bb43aSChristophe Leroy 	stw	r1,GPR1(r11)
73da7bb43aSChristophe Leroy 	stw	r1,0(r11)
74da7bb43aSChristophe Leroy 	tovirt(r1, r11)		/* set new kernel sp */
75da7bb43aSChristophe Leroy #endif
76d2e00603SChristophe Leroy 	stw	r10,_CCR(r11)		/* save registers */
778a23fdecSChristophe Leroy 	stw	r12,GPR12(r11)
788a23fdecSChristophe Leroy 	stw	r9,GPR9(r11)
79d2e00603SChristophe Leroy 	mfspr	r10,SPRN_SPRG_SCRATCH0
808a23fdecSChristophe Leroy 	mfspr	r12,SPRN_SPRG_SCRATCH1
81d2e00603SChristophe Leroy 	stw	r10,GPR10(r11)
828a23fdecSChristophe Leroy 	stw	r12,GPR11(r11)
838a23fdecSChristophe Leroy 	mflr	r10
848a23fdecSChristophe Leroy 	stw	r10,_LINK(r11)
8502847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
8602847487SChristophe Leroy 	mfspr	r12, SPRN_SPRG_THREAD
8702847487SChristophe Leroy 	tovirt(r12, r12)
8802847487SChristophe Leroy 	.if	\handle_dar_dsisr
8902847487SChristophe Leroy 	lwz	r10, DAR(r12)
9002847487SChristophe Leroy 	stw	r10, _DAR(r11)
9102847487SChristophe Leroy 	lwz	r10, DSISR(r12)
9202847487SChristophe Leroy 	stw	r10, _DSISR(r11)
9302847487SChristophe Leroy 	.endif
9402847487SChristophe Leroy 	lwz	r9, SRR1(r12)
9502847487SChristophe Leroy 	lwz	r12, SRR0(r12)
9602847487SChristophe Leroy #else
978a23fdecSChristophe Leroy 	mfspr	r12,SPRN_SRR0
988a23fdecSChristophe Leroy 	mfspr	r9,SPRN_SRR1
9902847487SChristophe Leroy #endif
10090f204b9SChristophe Leroy #ifdef CONFIG_40x
10190f204b9SChristophe Leroy 	rlwinm	r9,r9,0,14,12		/* clear MSR_WE (necessary?) */
10290f204b9SChristophe Leroy #else
10302847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
10402847487SChristophe Leroy 	li	r10, MSR_KERNEL & ~MSR_IR /* can take exceptions */
10502847487SChristophe Leroy #else
1068a23fdecSChristophe Leroy 	li	r10,MSR_KERNEL & ~(MSR_IR|MSR_DR) /* can take exceptions */
10702847487SChristophe Leroy #endif
10839bccfd1SChristophe Leroy 	mtmsr	r10			/* (except for mach check in rtas) */
10990f204b9SChristophe Leroy #endif
1108a23fdecSChristophe Leroy 	stw	r0,GPR0(r11)
1118a23fdecSChristophe Leroy 	lis	r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
1128a23fdecSChristophe Leroy 	addi	r10,r10,STACK_FRAME_REGS_MARKER@l
1138a23fdecSChristophe Leroy 	stw	r10,8(r11)
1148a23fdecSChristophe Leroy 	SAVE_4GPRS(3, r11)
1158a23fdecSChristophe Leroy 	SAVE_2GPRS(7, r11)
1168a23fdecSChristophe Leroy .endm
1178a23fdecSChristophe Leroy 
118b86fb888SChristophe Leroy .macro SYSCALL_ENTRY trapno
1199e270862SChristophe Leroy 	mfspr	r9, SPRN_SRR1
120*2c59e510SChristophe Leroy 	mfspr	r10, SPRN_SRR0
121d5c24398SChristophe Leroy 	andi.	r11, r9, MSR_PR
122*2c59e510SChristophe Leroy 	beq-	99f
123*2c59e510SChristophe Leroy 	LOAD_REG_IMMEDIATE(r11, MSR_KERNEL)		/* can take exceptions */
124*2c59e510SChristophe Leroy 	lis	r12, 1f@h
125*2c59e510SChristophe Leroy 	ori	r12, r12, 1f@l
126*2c59e510SChristophe Leroy 	mtspr	SPRN_SRR1, r11
127*2c59e510SChristophe Leroy 	mtspr	SPRN_SRR0, r12
128*2c59e510SChristophe Leroy 	mfspr	r12,SPRN_SPRG_THREAD
129d5c24398SChristophe Leroy 	mr	r11, r1
130d5c24398SChristophe Leroy 	lwz	r1,TASK_STACK-THREAD(r12)
131*2c59e510SChristophe Leroy 	tovirt(r12, r12)
132d5c24398SChristophe Leroy 	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
13376249ddcSChristophe Leroy 	rfi
13476249ddcSChristophe Leroy 1:
135d5c24398SChristophe Leroy 	stw	r11,GPR1(r1)
136d5c24398SChristophe Leroy 	stw	r11,0(r1)
137d5c24398SChristophe Leroy 	mr	r11, r1
138*2c59e510SChristophe Leroy 	stw	r10,_NIP(r11)
1399e270862SChristophe Leroy 	mflr	r10
1409e270862SChristophe Leroy 	stw	r10, _LINK(r11)
141c06f0affSChristophe Leroy 	mfcr	r10
142c06f0affSChristophe Leroy 	rlwinm	r10,r10,0,4,2	/* Clear SO bit in CR */
143c06f0affSChristophe Leroy 	stw	r10,_CCR(r11)		/* save registers */
144b86fb888SChristophe Leroy #ifdef CONFIG_40x
145b86fb888SChristophe Leroy 	rlwinm	r9,r9,0,14,12		/* clear MSR_WE (necessary?) */
146b86fb888SChristophe Leroy #endif
147b86fb888SChristophe Leroy 	lis	r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
148b86fb888SChristophe Leroy 	stw	r2,GPR2(r11)
149b86fb888SChristophe Leroy 	addi	r10,r10,STACK_FRAME_REGS_MARKER@l
150b86fb888SChristophe Leroy 	stw	r9,_MSR(r11)
151b86fb888SChristophe Leroy 	li	r2, \trapno + 1
152b86fb888SChristophe Leroy 	stw	r10,8(r11)
153b86fb888SChristophe Leroy 	stw	r2,_TRAP(r11)
154b86fb888SChristophe Leroy 	SAVE_GPR(0, r11)
155b86fb888SChristophe Leroy 	SAVE_4GPRS(3, r11)
156b86fb888SChristophe Leroy 	SAVE_2GPRS(7, r11)
157b86fb888SChristophe Leroy 	addi	r11,r1,STACK_FRAME_OVERHEAD
158b86fb888SChristophe Leroy 	addi	r2,r12,-THREAD
159b86fb888SChristophe Leroy 	stw	r11,PT_REGS(r12)
160b86fb888SChristophe Leroy #if defined(CONFIG_40x)
161b86fb888SChristophe Leroy 	/* Check to see if the dbcr0 register is set up to debug.  Use the
162b86fb888SChristophe Leroy 	   internal debug mode bit to do this. */
163b86fb888SChristophe Leroy 	lwz	r12,THREAD_DBCR0(r12)
164b86fb888SChristophe Leroy 	andis.	r12,r12,DBCR0_IDM@h
165b86fb888SChristophe Leroy #endif
166b86fb888SChristophe Leroy 	ACCOUNT_CPU_USER_ENTRY(r2, r11, r12)
167b86fb888SChristophe Leroy #if defined(CONFIG_40x)
168b86fb888SChristophe Leroy 	beq+	3f
169b86fb888SChristophe Leroy 	/* From user and task is ptraced - load up global dbcr0 */
170b86fb888SChristophe Leroy 	li	r12,-1			/* clear all pending debug events */
171b86fb888SChristophe Leroy 	mtspr	SPRN_DBSR,r12
172b86fb888SChristophe Leroy 	lis	r11,global_dbcr0@ha
173b86fb888SChristophe Leroy 	addi	r11,r11,global_dbcr0@l
174b86fb888SChristophe Leroy 	lwz	r12,0(r11)
175b86fb888SChristophe Leroy 	mtspr	SPRN_DBCR0,r12
176b86fb888SChristophe Leroy 	lwz	r12,4(r11)
177b86fb888SChristophe Leroy 	addi	r12,r12,-1
178b86fb888SChristophe Leroy 	stw	r12,4(r11)
179b86fb888SChristophe Leroy #endif
180b86fb888SChristophe Leroy 
181b86fb888SChristophe Leroy 3:
182b86fb888SChristophe Leroy #ifdef CONFIG_TRACE_IRQFLAGS
183b86fb888SChristophe Leroy 	/*
184b86fb888SChristophe Leroy 	 * If MSR is changing we need to keep interrupts disabled at this point
185b86fb888SChristophe Leroy 	 * otherwise we might risk taking an interrupt before we tell lockdep
186b86fb888SChristophe Leroy 	 * they are enabled.
187b86fb888SChristophe Leroy 	 */
188ba18025fSChristophe Leroy 	LOAD_REG_IMMEDIATE(r10, MSR_KERNEL)
189b86fb888SChristophe Leroy 	rlwimi	r10, r9, 0, MSR_EE
190b86fb888SChristophe Leroy #else
191ba18025fSChristophe Leroy 	LOAD_REG_IMMEDIATE(r10, MSR_KERNEL | MSR_EE)
192b86fb888SChristophe Leroy #endif
19376249ddcSChristophe Leroy 	mtmsr	r10
19476249ddcSChristophe Leroy 	b	transfer_to_syscall		/* jump to handler */
1959e270862SChristophe Leroy 99:	b	ret_from_kernel_syscall
196b86fb888SChristophe Leroy .endm
197b86fb888SChristophe Leroy 
198c9c84fd9SChristophe Leroy .macro save_dar_dsisr_on_stack reg1, reg2, sp
19902847487SChristophe Leroy #ifndef CONFIG_VMAP_STACK
200c9c84fd9SChristophe Leroy 	mfspr	\reg1, SPRN_DAR
201c9c84fd9SChristophe Leroy 	mfspr	\reg2, SPRN_DSISR
202c9c84fd9SChristophe Leroy 	stw	\reg1, _DAR(\sp)
203c9c84fd9SChristophe Leroy 	stw	\reg2, _DSISR(\sp)
20402847487SChristophe Leroy #endif
205c9c84fd9SChristophe Leroy .endm
206c9c84fd9SChristophe Leroy 
207c9c84fd9SChristophe Leroy .macro get_and_save_dar_dsisr_on_stack reg1, reg2, sp
20802847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
20902847487SChristophe Leroy 	lwz	\reg1, _DAR(\sp)
21002847487SChristophe Leroy 	lwz	\reg2, _DSISR(\sp)
21102847487SChristophe Leroy #else
212c9c84fd9SChristophe Leroy 	save_dar_dsisr_on_stack \reg1, \reg2, \sp
21302847487SChristophe Leroy #endif
21402847487SChristophe Leroy .endm
21502847487SChristophe Leroy 
21602847487SChristophe Leroy .macro tovirt_vmstack dst, src
21702847487SChristophe Leroy #ifdef CONFIG_VMAP_STACK
21802847487SChristophe Leroy 	tovirt(\dst, \src)
21902847487SChristophe Leroy #else
22002847487SChristophe Leroy 	.ifnc	\dst, \src
22102847487SChristophe Leroy 	mr	\dst, \src
22202847487SChristophe Leroy 	.endif
22302847487SChristophe Leroy #endif
22402847487SChristophe Leroy .endm
22502847487SChristophe Leroy 
22602847487SChristophe Leroy .macro tovirt_novmstack dst, src
22702847487SChristophe Leroy #ifndef CONFIG_VMAP_STACK
22802847487SChristophe Leroy 	tovirt(\dst, \src)
22902847487SChristophe Leroy #else
23002847487SChristophe Leroy 	.ifnc	\dst, \src
23102847487SChristophe Leroy 	mr	\dst, \src
23202847487SChristophe Leroy 	.endif
23302847487SChristophe Leroy #endif
23402847487SChristophe Leroy .endm
23502847487SChristophe Leroy 
23602847487SChristophe Leroy .macro tophys_novmstack dst, src
23702847487SChristophe Leroy #ifndef CONFIG_VMAP_STACK
23802847487SChristophe Leroy 	tophys(\dst, \src)
23902847487SChristophe Leroy #else
24002847487SChristophe Leroy 	.ifnc	\dst, \src
24102847487SChristophe Leroy 	mr	\dst, \src
24202847487SChristophe Leroy 	.endif
24302847487SChristophe Leroy #endif
244c9c84fd9SChristophe Leroy .endm
245c9c84fd9SChristophe Leroy 
2468a23fdecSChristophe Leroy /*
2478a23fdecSChristophe Leroy  * Note: code which follows this uses cr0.eq (set if from kernel),
2488a23fdecSChristophe Leroy  * r11, r12 (SRR0), and r9 (SRR1).
2498a23fdecSChristophe Leroy  *
2508a23fdecSChristophe Leroy  * Note2: once we have set r1 we are in a position to take exceptions
2518a23fdecSChristophe Leroy  * again, and we could thus set MSR:RI at that point.
2528a23fdecSChristophe Leroy  */
2538a23fdecSChristophe Leroy 
2548a23fdecSChristophe Leroy /*
2558a23fdecSChristophe Leroy  * Exception vectors.
2568a23fdecSChristophe Leroy  */
2578a23fdecSChristophe Leroy #ifdef CONFIG_PPC_BOOK3S
2588a23fdecSChristophe Leroy #define	START_EXCEPTION(n, label)		\
2598a23fdecSChristophe Leroy 	. = n;					\
2608a23fdecSChristophe Leroy 	DO_KVM n;				\
2618a23fdecSChristophe Leroy label:
2628a23fdecSChristophe Leroy 
2638a23fdecSChristophe Leroy #else
2648a23fdecSChristophe Leroy #define	START_EXCEPTION(n, label)		\
2658a23fdecSChristophe Leroy 	. = n;					\
2668a23fdecSChristophe Leroy label:
2678a23fdecSChristophe Leroy 
2688a23fdecSChristophe Leroy #endif
2698a23fdecSChristophe Leroy 
2708a23fdecSChristophe Leroy #define EXCEPTION(n, label, hdlr, xfer)		\
2718a23fdecSChristophe Leroy 	START_EXCEPTION(n, label)		\
2728a23fdecSChristophe Leroy 	EXCEPTION_PROLOG;			\
2738a23fdecSChristophe Leroy 	addi	r3,r1,STACK_FRAME_OVERHEAD;	\
2748a23fdecSChristophe Leroy 	xfer(n, hdlr)
2758a23fdecSChristophe Leroy 
2761ae99b4bSChristophe Leroy #define EXC_XFER_TEMPLATE(hdlr, trap, msr, tfer, ret)		\
2778a23fdecSChristophe Leroy 	li	r10,trap;					\
2788a23fdecSChristophe Leroy 	stw	r10,_TRAP(r11);					\
279ba18025fSChristophe Leroy 	LOAD_REG_IMMEDIATE(r10, msr);				\
2808a23fdecSChristophe Leroy 	bl	tfer;						\
2818a23fdecSChristophe Leroy 	.long	hdlr;						\
2828a23fdecSChristophe Leroy 	.long	ret
2838a23fdecSChristophe Leroy 
2848a23fdecSChristophe Leroy #define EXC_XFER_STD(n, hdlr)		\
2851ae99b4bSChristophe Leroy 	EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, transfer_to_handler_full,	\
2868a23fdecSChristophe Leroy 			  ret_from_except_full)
2878a23fdecSChristophe Leroy 
2888a23fdecSChristophe Leroy #define EXC_XFER_LITE(n, hdlr)		\
2891ae99b4bSChristophe Leroy 	EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, transfer_to_handler, \
2908a23fdecSChristophe Leroy 			  ret_from_except)
2918a23fdecSChristophe Leroy 
2923978eb78SChristophe Leroy .macro vmap_stack_overflow_exception
2933978eb78SChristophe Leroy #ifdef CONFIG_VMAP_STACK
2943978eb78SChristophe Leroy #ifdef CONFIG_SMP
295da7bb43aSChristophe Leroy 	mfspr	r1, SPRN_SPRG_THREAD
296da7bb43aSChristophe Leroy 	lwz	r1, TASK_CPU - THREAD(r1)
297da7bb43aSChristophe Leroy 	slwi	r1, r1, 3
298da7bb43aSChristophe Leroy 	addis	r1, r1, emergency_ctx@ha
2993978eb78SChristophe Leroy #else
300da7bb43aSChristophe Leroy 	lis	r1, emergency_ctx@ha
3013978eb78SChristophe Leroy #endif
302da7bb43aSChristophe Leroy 	lwz	r1, emergency_ctx@l(r1)
303da7bb43aSChristophe Leroy 	cmpwi	cr1, r1, 0
3043978eb78SChristophe Leroy 	bne	cr1, 1f
305da7bb43aSChristophe Leroy 	lis	r1, init_thread_union@ha
306da7bb43aSChristophe Leroy 	addi	r1, r1, init_thread_union@l
307da7bb43aSChristophe Leroy 1:	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
3083978eb78SChristophe Leroy 	EXCEPTION_PROLOG_2
3093978eb78SChristophe Leroy 	SAVE_NVGPRS(r11)
3103978eb78SChristophe Leroy 	addi	r3, r1, STACK_FRAME_OVERHEAD
3113978eb78SChristophe Leroy 	EXC_XFER_STD(0, stack_overflow_exception)
3123978eb78SChristophe Leroy #endif
3133978eb78SChristophe Leroy .endm
3143978eb78SChristophe Leroy 
3158a23fdecSChristophe Leroy #endif /* __HEAD_32_H__ */
316