1 /*	$NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2001 Ben Harris.
7  * Copyright (c) 1995 Mark Brinicombe.
8  * Copyright (c) 1995 Brini.
9  * All rights reserved.
10  *
11  * This code is derived from software written for Brini by Mark Brinicombe
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by Brini.
24  * 4. The name of the company nor the name of the author may be used to
25  *    endorse or promote products derived from this software without specific
26  *    prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
32  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * RiscBSD kernel project
41  *
42  * undefined.c
43  *
44  * Fault handler
45  *
46  * Created      : 06/01/95
47  */
48 
49 
50 #include "opt_ddb.h"
51 
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD: stable/12/sys/arm/arm/undefined.c 372763 2022-11-18 10:03:21Z cognet $");
54 
55 #include <sys/param.h>
56 #include <sys/malloc.h>
57 #include <sys/queue.h>
58 #include <sys/signal.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/syslog.h>
62 #include <sys/vmmeter.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/signalvar.h>
66 #include <sys/ptrace.h>
67 #include <sys/vmmeter.h>
68 #ifdef KDB
69 #include <sys/kdb.h>
70 #endif
71 
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 
75 #include <machine/armreg.h>
76 #include <machine/asm.h>
77 #include <machine/cpu.h>
78 #include <machine/frame.h>
79 #include <machine/undefined.h>
80 #include <machine/trap.h>
81 
82 #include <machine/disassem.h>
83 
84 #ifdef DDB
85 #include <ddb/db_output.h>
86 #endif
87 
88 #ifdef KDB
89 #include <machine/db_machdep.h>
90 #endif
91 
92 #define	ARM_COPROC_INSN(insn)	(((insn) & (1 << 27)) != 0)
93 #define	ARM_VFP_INSN(insn)	((((insn) & 0xfe000000) == 0xf2000000) || \
94 				 (((insn) & 0xff100000) == 0xf4000000))
95 #define	ARM_COPROC(insn)	(((insn) >> 8) & 0xf)
96 
97 #define	THUMB_32BIT_INSN(insn)	((((insn) & 0xe000) == 0xe000) && \
98 				 (((insn) & 0x1800) != 0))
99 /*
100  * Coprocessor, Advanced SIMD, and
101  * Floating-point instructions on page A6-251
102  * OP1 == 01 OR 11
103  * OP2 == 1xxxxxx
104  */
105 #define	THUMB_COPROC_INSN(insn)	(((insn) & (3 << 26)) == (3 << 26))
106 /*
107  * Advanced SIMD element or structure
108  * load/store instructions on page A7-275
109  * OP1 == 11
110  * OP2 == 001xxx0
111 */
112 #define	THUMB_VFP_INSN(insn)	(((insn) & (0x1F1 << 20)) == (0x190 << 20))
113 #define	THUMB_COPROC(insn)	(((insn) >> 8) & 0xf)
114 
115 #define	COPROC_VFP	10
116 
117 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
118 
119 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
120 
121 
122 void *
install_coproc_handler(int coproc,undef_handler_t handler)123 install_coproc_handler(int coproc, undef_handler_t handler)
124 {
125 	struct undefined_handler *uh;
126 
127 	KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
128 	KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
129 
130 	/* XXX: M_TEMP??? */
131 	uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
132 	uh->uh_handler = handler;
133 	install_coproc_handler_static(coproc, uh);
134 	return uh;
135 }
136 
137 void
install_coproc_handler_static(int coproc,struct undefined_handler * uh)138 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
139 {
140 
141 	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
142 }
143 
144 void
remove_coproc_handler(void * cookie)145 remove_coproc_handler(void *cookie)
146 {
147 	struct undefined_handler *uh = cookie;
148 
149 	LIST_REMOVE(uh, uh_link);
150 	free(uh, M_TEMP);
151 }
152 
153 
154 static int
gdb_trapper(u_int addr,u_int insn,struct trapframe * frame,int code)155 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
156 {
157 	struct thread *td;
158 	ksiginfo_t ksi;
159 	int error;
160 
161 	td = (curthread == NULL) ? &thread0 : curthread;
162 
163 	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
164 		if (code == FAULT_USER) {
165 			ksiginfo_init_trap(&ksi);
166 			ksi.ksi_signo = SIGTRAP;
167 			ksi.ksi_code = TRAP_BRKPT;
168 			ksi.ksi_addr = (u_int32_t *)addr;
169 			trapsignal(td, &ksi);
170 			return 0;
171 		}
172 #if 0
173 #ifdef KGDB
174 		return !kgdb_trap(T_BREAKPOINT, frame);
175 #endif
176 #endif
177 	}
178 
179 	if (code == FAULT_USER) {
180 		/* TODO: No support for ptrace from Thumb-2 */
181 		if ((frame->tf_spsr & PSR_T) == 0 &&
182 		    insn == PTRACE_BREAKPOINT) {
183 			PROC_LOCK(td->td_proc);
184 			_PHOLD(td->td_proc);
185 			error = ptrace_clear_single_step(td);
186 			_PRELE(td->td_proc);
187 			PROC_UNLOCK(td->td_proc);
188 			if (error == 0) {
189 				ksiginfo_init_trap(&ksi);
190 				ksi.ksi_signo = SIGTRAP;
191 				ksi.ksi_code = TRAP_TRACE;
192 				ksi.ksi_addr = (u_int32_t *)addr;
193 				trapsignal(td, &ksi);
194 				return (0);
195 			}
196 		}
197 	}
198 
199 	return 1;
200 }
201 
202 static struct undefined_handler gdb_uh;
203 
204 void
undefined_init(void)205 undefined_init(void)
206 {
207 	int loop;
208 
209 	/* Not actually necessary -- the initialiser is just NULL */
210 	for (loop = 0; loop < MAX_COPROCS; ++loop)
211 		LIST_INIT(&undefined_handlers[loop]);
212 
213 	/* Install handler for GDB breakpoints */
214 	gdb_uh.uh_handler = gdb_trapper;
215 	install_coproc_handler_static(0, &gdb_uh);
216 }
217 
218 
219 void
undefinedinstruction(struct trapframe * frame)220 undefinedinstruction(struct trapframe *frame)
221 {
222 	struct thread *td;
223 	u_int fault_pc;
224 	int fault_instruction;
225 	int fault_code;
226 	int coprocessor;
227 	struct undefined_handler *uh;
228 #ifdef VERBOSE_ARM32
229 	int s;
230 #endif
231 	ksiginfo_t ksi;
232 
233 	/* Enable interrupts if they were enabled before the exception. */
234 	if (__predict_true(frame->tf_spsr & PSR_I) == 0)
235 		enable_interrupts(PSR_I);
236 	if (__predict_true(frame->tf_spsr & PSR_F) == 0)
237 		enable_interrupts(PSR_F);
238 
239 	VM_CNT_INC(v_trap);
240 
241 	fault_pc = frame->tf_pc;
242 
243 	/*
244 	 * Get the current thread/proc structure or thread0/proc0 if there is
245 	 * none.
246 	 */
247 	td = curthread == NULL ? &thread0 : curthread;
248 
249 	coprocessor = 0;
250 	if ((frame->tf_spsr & PSR_T) == 0) {
251 		/*
252 		 * Make sure the program counter is correctly aligned so we
253 		 * don't take an alignment fault trying to read the opcode.
254 		 */
255 		if (__predict_false((fault_pc & 3) != 0)) {
256 			ksiginfo_init_trap(&ksi);
257 			ksi.ksi_signo = SIGILL;
258 			ksi.ksi_code = ILL_ILLADR;
259 			ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
260 			trapsignal(td, &ksi);
261 			userret(td, frame);
262 			return;
263 		}
264 
265 		/*
266 		 * Should use fuword() here .. but in the interests of
267 		 * squeezing every bit of speed we will just use ReadWord().
268 		 * We know the instruction can be read as was just executed
269 		 * so this will never fail unless the kernel is screwed up
270 		 * in which case it does not really matter does it ?
271 		 */
272 
273 		fault_instruction = *(u_int32_t *)fault_pc;
274 
275 		/* Check for coprocessor instruction */
276 
277 		/*
278 		 * According to the datasheets you only need to look at bit
279 		 * 27 of the instruction to tell the difference between and
280 		 * undefined instruction and a coprocessor instruction
281 		 * following an undefined instruction trap.
282 		 */
283 
284 		if (ARM_COPROC_INSN(fault_instruction))
285 			coprocessor = ARM_COPROC(fault_instruction);
286 		else {          /* check for special instructions */
287 			if (ARM_VFP_INSN(fault_instruction))
288 				coprocessor = COPROC_VFP; /* vfp / simd */
289 		}
290 	} else {
291 #if __ARM_ARCH >= 7
292 		fault_instruction = *(uint16_t *)fault_pc;
293 		if (THUMB_32BIT_INSN(fault_instruction)) {
294 			fault_instruction <<= 16;
295 			fault_instruction |= *(uint16_t *)(fault_pc + 2);
296 
297 			/* Coprocessor, Advanced SIMD and Floating-point */
298 			if (THUMB_COPROC_INSN(fault_instruction))
299 				coprocessor = THUMB_COPROC(fault_instruction);
300 			else {
301 				/* Advanced SIMD load/store */
302 				if (THUMB_VFP_INSN(fault_instruction))
303 					coprocessor = COPROC_VFP; /* SIMD */
304 			}
305 		}
306 #else
307 		/*
308 		 * No support for Thumb-2 on this cpu
309 		 */
310 		ksiginfo_init_trap(&ksi);
311 		ksi.ksi_signo = SIGILL;
312 		ksi.ksi_code = ILL_ILLADR;
313 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
314 		trapsignal(td, &ksi);
315 		userret(td, frame);
316 		return;
317 #endif
318 	}
319 
320 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
321 		/*
322 		 * Modify the fault_code to reflect the USR/SVC state at
323 		 * time of fault.
324 		 */
325 		fault_code = FAULT_USER;
326 		td->td_frame = frame;
327 	} else
328 		fault_code = 0;
329 
330 	/* OK this is were we do something about the instruction. */
331 	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
332 	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
333 			       fault_code) == 0)
334 		    break;
335 
336 	if (uh == NULL && (fault_code & FAULT_USER)) {
337 		/* Fault has not been handled */
338 		ksiginfo_init_trap(&ksi);
339 		ksi.ksi_signo = SIGILL;
340 		ksi.ksi_code = ILL_ILLOPC;
341 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
342 		trapsignal(td, &ksi);
343 	}
344 
345 	if ((fault_code & FAULT_USER) == 0) {
346 		if (fault_instruction == KERNEL_BREAKPOINT) {
347 #ifdef KDB
348 			kdb_trap(T_BREAKPOINT, 0, frame);
349 #else
350 			printf("No debugger in kernel.\n");
351 #endif
352 			return;
353 		}
354 		else
355 			panic("Undefined instruction in kernel.\n");
356 	}
357 
358 	userret(td, frame);
359 }
360