xref: /openbmc/linux/arch/powerpc/kernel/head_32.h (revision a5907065)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __HEAD_32_H__
3 #define __HEAD_32_H__
4 
5 #include <asm/ptrace.h>	/* for STACK_FRAME_REGS_MARKER */
6 
7 /*
8  * Exception entry code.  This code runs with address translation
9  * turned off, i.e. using physical addresses.
10  * We assume sprg3 has the physical address of the current
11  * task's thread_struct.
12  */
13 .macro EXCEPTION_PROLOG handle_dar_dsisr=0
14 	EXCEPTION_PROLOG_0	handle_dar_dsisr=\handle_dar_dsisr
15 	EXCEPTION_PROLOG_1
16 	EXCEPTION_PROLOG_2	handle_dar_dsisr=\handle_dar_dsisr
17 .endm
18 
19 .macro EXCEPTION_PROLOG_0 handle_dar_dsisr=0
20 	mtspr	SPRN_SPRG_SCRATCH0,r10
21 	mtspr	SPRN_SPRG_SCRATCH1,r11
22 #ifdef CONFIG_VMAP_STACK
23 	mfspr	r10, SPRN_SPRG_THREAD
24 	.if	\handle_dar_dsisr
25 	mfspr	r11, SPRN_DAR
26 	stw	r11, DAR(r10)
27 	mfspr	r11, SPRN_DSISR
28 	stw	r11, DSISR(r10)
29 	.endif
30 	mfspr	r11, SPRN_SRR0
31 	stw	r11, SRR0(r10)
32 #endif
33 	mfspr	r11, SPRN_SRR1		/* check whether user or kernel */
34 #ifdef CONFIG_VMAP_STACK
35 	stw	r11, SRR1(r10)
36 #endif
37 	mfcr	r10
38 	andi.	r11, r11, MSR_PR
39 .endm
40 
41 .macro EXCEPTION_PROLOG_1 for_rtas=0
42 #ifdef CONFIG_VMAP_STACK
43 	mtspr	SPRN_SPRG_SCRATCH2,r1
44 	subi	r1, r1, INT_FRAME_SIZE		/* use r1 if kernel */
45 	beq	1f
46 	mfspr	r1,SPRN_SPRG_THREAD
47 	lwz	r1,TASK_STACK-THREAD(r1)
48 	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
49 1:
50 	mtcrf	0x3f, r1
51 	bt	32 - THREAD_ALIGN_SHIFT, stack_overflow
52 #else
53 	subi	r11, r1, INT_FRAME_SIZE		/* use r1 if kernel */
54 	beq	1f
55 	mfspr	r11,SPRN_SPRG_THREAD
56 	lwz	r11,TASK_STACK-THREAD(r11)
57 	addi	r11, r11, THREAD_SIZE - INT_FRAME_SIZE
58 1:	tophys(r11, r11)
59 #endif
60 .endm
61 
62 .macro EXCEPTION_PROLOG_2 handle_dar_dsisr=0
63 #ifdef CONFIG_VMAP_STACK
64 	li	r11, MSR_KERNEL & ~(MSR_IR | MSR_RI) /* can take DTLB miss */
65 	mtmsr	r11
66 	isync
67 	mfspr	r11, SPRN_SPRG_SCRATCH2
68 	stw	r11,GPR1(r1)
69 	stw	r11,0(r1)
70 	mr	r11, r1
71 #else
72 	stw	r1,GPR1(r11)
73 	stw	r1,0(r11)
74 	tovirt(r1, r11)		/* set new kernel sp */
75 #endif
76 	stw	r10,_CCR(r11)		/* save registers */
77 	stw	r12,GPR12(r11)
78 	stw	r9,GPR9(r11)
79 	mfspr	r10,SPRN_SPRG_SCRATCH0
80 	mfspr	r12,SPRN_SPRG_SCRATCH1
81 	stw	r10,GPR10(r11)
82 	stw	r12,GPR11(r11)
83 	mflr	r10
84 	stw	r10,_LINK(r11)
85 #ifdef CONFIG_VMAP_STACK
86 	mfspr	r12, SPRN_SPRG_THREAD
87 	tovirt(r12, r12)
88 	.if	\handle_dar_dsisr
89 	lwz	r10, DAR(r12)
90 	stw	r10, _DAR(r11)
91 	lwz	r10, DSISR(r12)
92 	stw	r10, _DSISR(r11)
93 	.endif
94 	lwz	r9, SRR1(r12)
95 	lwz	r12, SRR0(r12)
96 #else
97 	mfspr	r12,SPRN_SRR0
98 	mfspr	r9,SPRN_SRR1
99 #endif
100 #ifdef CONFIG_40x
101 	rlwinm	r9,r9,0,14,12		/* clear MSR_WE (necessary?) */
102 #else
103 #ifdef CONFIG_VMAP_STACK
104 	li	r10, MSR_KERNEL & ~MSR_IR /* can take exceptions */
105 #else
106 	li	r10,MSR_KERNEL & ~(MSR_IR|MSR_DR) /* can take exceptions */
107 #endif
108 	mtmsr	r10			/* (except for mach check in rtas) */
109 #endif
110 	stw	r0,GPR0(r11)
111 	lis	r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
112 	addi	r10,r10,STACK_FRAME_REGS_MARKER@l
113 	stw	r10,8(r11)
114 	SAVE_4GPRS(3, r11)
115 	SAVE_2GPRS(7, r11)
116 .endm
117 
118 .macro SYSCALL_ENTRY trapno
119 	mfspr	r9, SPRN_SRR1
120 	mfspr	r10, SPRN_SRR0
121 	LOAD_REG_IMMEDIATE(r11, MSR_KERNEL)		/* can take exceptions */
122 	lis	r12, 1f@h
123 	ori	r12, r12, 1f@l
124 	mtspr	SPRN_SRR1, r11
125 	mtspr	SPRN_SRR0, r12
126 	mfspr	r12,SPRN_SPRG_THREAD
127 	mr	r11, r1
128 	lwz	r1,TASK_STACK-THREAD(r12)
129 	tovirt(r12, r12)
130 	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
131 	rfi
132 1:
133 	stw	r11,GPR1(r1)
134 	stw	r11,0(r1)
135 	mr	r11, r1
136 	stw	r10,_NIP(r11)
137 	mflr	r10
138 	stw	r10, _LINK(r11)
139 	mfcr	r10
140 	rlwinm	r10,r10,0,4,2	/* Clear SO bit in CR */
141 	stw	r10,_CCR(r11)		/* save registers */
142 #ifdef CONFIG_40x
143 	rlwinm	r9,r9,0,14,12		/* clear MSR_WE (necessary?) */
144 #endif
145 	lis	r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */
146 	stw	r2,GPR2(r11)
147 	addi	r10,r10,STACK_FRAME_REGS_MARKER@l
148 	stw	r9,_MSR(r11)
149 	li	r2, \trapno
150 	stw	r10,8(r11)
151 	stw	r2,_TRAP(r11)
152 	SAVE_GPR(0, r11)
153 	SAVE_4GPRS(3, r11)
154 	SAVE_2GPRS(7, r11)
155 	addi	r2,r12,-THREAD
156 	b	transfer_to_syscall		/* jump to handler */
157 .endm
158 
159 .macro save_dar_dsisr_on_stack reg1, reg2, sp
160 #ifndef CONFIG_VMAP_STACK
161 	mfspr	\reg1, SPRN_DAR
162 	mfspr	\reg2, SPRN_DSISR
163 	stw	\reg1, _DAR(\sp)
164 	stw	\reg2, _DSISR(\sp)
165 #endif
166 .endm
167 
168 .macro get_and_save_dar_dsisr_on_stack reg1, reg2, sp
169 #ifdef CONFIG_VMAP_STACK
170 	lwz	\reg1, _DAR(\sp)
171 	lwz	\reg2, _DSISR(\sp)
172 #else
173 	save_dar_dsisr_on_stack \reg1, \reg2, \sp
174 #endif
175 .endm
176 
177 .macro tovirt_vmstack dst, src
178 #ifdef CONFIG_VMAP_STACK
179 	tovirt(\dst, \src)
180 #else
181 	.ifnc	\dst, \src
182 	mr	\dst, \src
183 	.endif
184 #endif
185 .endm
186 
187 .macro tovirt_novmstack dst, src
188 #ifndef CONFIG_VMAP_STACK
189 	tovirt(\dst, \src)
190 #else
191 	.ifnc	\dst, \src
192 	mr	\dst, \src
193 	.endif
194 #endif
195 .endm
196 
197 .macro tophys_novmstack dst, src
198 #ifndef CONFIG_VMAP_STACK
199 	tophys(\dst, \src)
200 #else
201 	.ifnc	\dst, \src
202 	mr	\dst, \src
203 	.endif
204 #endif
205 .endm
206 
207 /*
208  * Note: code which follows this uses cr0.eq (set if from kernel),
209  * r11, r12 (SRR0), and r9 (SRR1).
210  *
211  * Note2: once we have set r1 we are in a position to take exceptions
212  * again, and we could thus set MSR:RI at that point.
213  */
214 
215 /*
216  * Exception vectors.
217  */
218 #ifdef CONFIG_PPC_BOOK3S
219 #define	START_EXCEPTION(n, label)		\
220 	. = n;					\
221 	DO_KVM n;				\
222 label:
223 
224 #else
225 #define	START_EXCEPTION(n, label)		\
226 	. = n;					\
227 label:
228 
229 #endif
230 
231 #define EXCEPTION(n, label, hdlr, xfer)		\
232 	START_EXCEPTION(n, label)		\
233 	EXCEPTION_PROLOG;			\
234 	addi	r3,r1,STACK_FRAME_OVERHEAD;	\
235 	xfer(n, hdlr)
236 
237 #define EXC_XFER_TEMPLATE(hdlr, trap, msr, tfer, ret)		\
238 	li	r10,trap;					\
239 	stw	r10,_TRAP(r11);					\
240 	LOAD_REG_IMMEDIATE(r10, msr);				\
241 	bl	tfer;						\
242 	.long	hdlr;						\
243 	.long	ret
244 
245 #define EXC_XFER_STD(n, hdlr)		\
246 	EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, transfer_to_handler_full,	\
247 			  ret_from_except_full)
248 
249 #define EXC_XFER_LITE(n, hdlr)		\
250 	EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, transfer_to_handler, \
251 			  ret_from_except)
252 
253 .macro vmap_stack_overflow_exception
254 #ifdef CONFIG_VMAP_STACK
255 #ifdef CONFIG_SMP
256 	mfspr	r1, SPRN_SPRG_THREAD
257 	lwz	r1, TASK_CPU - THREAD(r1)
258 	slwi	r1, r1, 3
259 	addis	r1, r1, emergency_ctx@ha
260 #else
261 	lis	r1, emergency_ctx@ha
262 #endif
263 	lwz	r1, emergency_ctx@l(r1)
264 	cmpwi	cr1, r1, 0
265 	bne	cr1, 1f
266 	lis	r1, init_thread_union@ha
267 	addi	r1, r1, init_thread_union@l
268 1:	addi	r1, r1, THREAD_SIZE - INT_FRAME_SIZE
269 	EXCEPTION_PROLOG_2
270 	SAVE_NVGPRS(r11)
271 	addi	r3, r1, STACK_FRAME_OVERHEAD
272 	EXC_XFER_STD(0, stack_overflow_exception)
273 #endif
274 .endm
275 
276 #endif /* __HEAD_32_H__ */
277