xref: /freebsd/sys/arm/arm/undefined.c (revision 148a8da8)
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$");
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) >= 0xe800)
98 #define	THUMB_COPROC_INSN(insn)	(((insn) & (3 << 26)) == (3 << 26))
99 #define	THUMB_COPROC_UNDEFINED(insn) (((insn) & 0x3e << 20) == 0)
100 #define	THUMB_VFP_INSN(insn)	(((insn) & (3 << 24)) == (3 << 24))
101 #define	THUMB_COPROC(insn)	(((insn) >> 8) & 0xf)
102 
103 #define	COPROC_VFP	10
104 
105 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
106 
107 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
108 
109 
110 void *
111 install_coproc_handler(int coproc, undef_handler_t handler)
112 {
113 	struct undefined_handler *uh;
114 
115 	KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
116 	KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
117 
118 	/* XXX: M_TEMP??? */
119 	uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
120 	uh->uh_handler = handler;
121 	install_coproc_handler_static(coproc, uh);
122 	return uh;
123 }
124 
125 void
126 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
127 {
128 
129 	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
130 }
131 
132 void
133 remove_coproc_handler(void *cookie)
134 {
135 	struct undefined_handler *uh = cookie;
136 
137 	LIST_REMOVE(uh, uh_link);
138 	free(uh, M_TEMP);
139 }
140 
141 
142 static int
143 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
144 {
145 	struct thread *td;
146 	ksiginfo_t ksi;
147 	int error;
148 
149 	td = (curthread == NULL) ? &thread0 : curthread;
150 
151 	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
152 		if (code == FAULT_USER) {
153 			ksiginfo_init_trap(&ksi);
154 			ksi.ksi_signo = SIGTRAP;
155 			ksi.ksi_code = TRAP_BRKPT;
156 			ksi.ksi_addr = (u_int32_t *)addr;
157 			trapsignal(td, &ksi);
158 			return 0;
159 		}
160 #if 0
161 #ifdef KGDB
162 		return !kgdb_trap(T_BREAKPOINT, frame);
163 #endif
164 #endif
165 	}
166 
167 	if (code == FAULT_USER) {
168 		/* TODO: No support for ptrace from Thumb-2 */
169 		if ((frame->tf_spsr & PSR_T) == 0 &&
170 		    insn == PTRACE_BREAKPOINT) {
171 			PROC_LOCK(td->td_proc);
172 			_PHOLD(td->td_proc);
173 			error = ptrace_clear_single_step(td);
174 			_PRELE(td->td_proc);
175 			PROC_UNLOCK(td->td_proc);
176 			if (error == 0) {
177 				ksiginfo_init_trap(&ksi);
178 				ksi.ksi_signo = SIGTRAP;
179 				ksi.ksi_code = TRAP_TRACE;
180 				ksi.ksi_addr = (u_int32_t *)addr;
181 				trapsignal(td, &ksi);
182 				return (0);
183 			}
184 		}
185 	}
186 
187 	return 1;
188 }
189 
190 static struct undefined_handler gdb_uh;
191 
192 void
193 undefined_init(void)
194 {
195 	int loop;
196 
197 	/* Not actually necessary -- the initialiser is just NULL */
198 	for (loop = 0; loop < MAX_COPROCS; ++loop)
199 		LIST_INIT(&undefined_handlers[loop]);
200 
201 	/* Install handler for GDB breakpoints */
202 	gdb_uh.uh_handler = gdb_trapper;
203 	install_coproc_handler_static(0, &gdb_uh);
204 }
205 
206 
207 void
208 undefinedinstruction(struct trapframe *frame)
209 {
210 	struct thread *td;
211 	u_int fault_pc;
212 	int fault_instruction;
213 	int fault_code;
214 	int coprocessor;
215 	struct undefined_handler *uh;
216 #ifdef VERBOSE_ARM32
217 	int s;
218 #endif
219 	ksiginfo_t ksi;
220 
221 	/* Enable interrupts if they were enabled before the exception. */
222 	if (__predict_true(frame->tf_spsr & PSR_I) == 0)
223 		enable_interrupts(PSR_I);
224 	if (__predict_true(frame->tf_spsr & PSR_F) == 0)
225 		enable_interrupts(PSR_F);
226 
227 	VM_CNT_INC(v_trap);
228 
229 	fault_pc = frame->tf_pc;
230 
231 	/*
232 	 * Get the current thread/proc structure or thread0/proc0 if there is
233 	 * none.
234 	 */
235 	td = curthread == NULL ? &thread0 : curthread;
236 
237 	coprocessor = 0;
238 	if ((frame->tf_spsr & PSR_T) == 0) {
239 		/*
240 		 * Make sure the program counter is correctly aligned so we
241 		 * don't take an alignment fault trying to read the opcode.
242 		 */
243 		if (__predict_false((fault_pc & 3) != 0)) {
244 			ksiginfo_init_trap(&ksi);
245 			ksi.ksi_signo = SIGILL;
246 			ksi.ksi_code = ILL_ILLADR;
247 			ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
248 			trapsignal(td, &ksi);
249 			userret(td, frame);
250 			return;
251 		}
252 
253 		/*
254 		 * Should use fuword() here .. but in the interests of
255 		 * squeezing every bit of speed we will just use ReadWord().
256 		 * We know the instruction can be read as was just executed
257 		 * so this will never fail unless the kernel is screwed up
258 		 * in which case it does not really matter does it ?
259 		 */
260 
261 		fault_instruction = *(u_int32_t *)fault_pc;
262 
263 		/* Check for coprocessor instruction */
264 
265 		/*
266 		 * According to the datasheets you only need to look at bit
267 		 * 27 of the instruction to tell the difference between and
268 		 * undefined instruction and a coprocessor instruction
269 		 * following an undefined instruction trap.
270 		 */
271 
272 		if (ARM_COPROC_INSN(fault_instruction))
273 			coprocessor = ARM_COPROC(fault_instruction);
274 		else {          /* check for special instructions */
275 			if (ARM_VFP_INSN(fault_instruction))
276 				coprocessor = COPROC_VFP; /* vfp / simd */
277 		}
278 	} else {
279 #if __ARM_ARCH >= 7
280 		fault_instruction = *(uint16_t *)fault_pc;
281 		if (THUMB_32BIT_INSN(fault_instruction)) {
282 			fault_instruction <<= 16;
283 			fault_instruction |= *(uint16_t *)(fault_pc + 2);
284 
285 			/*
286 			 * Is it a Coprocessor, Advanced SIMD, or
287 			 * Floating-point instruction.
288 			 */
289 			if (THUMB_COPROC_INSN(fault_instruction)) {
290 				if (THUMB_COPROC_UNDEFINED(fault_instruction)) {
291 					/* undefined insn */
292 				} else if (THUMB_VFP_INSN(fault_instruction))
293 					coprocessor = COPROC_VFP;
294 				else
295 					coprocessor =
296 					    THUMB_COPROC(fault_instruction);
297 			}
298 		}
299 #else
300 		/*
301 		 * No support for Thumb-2 on this cpu
302 		 */
303 		ksiginfo_init_trap(&ksi);
304 		ksi.ksi_signo = SIGILL;
305 		ksi.ksi_code = ILL_ILLADR;
306 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
307 		trapsignal(td, &ksi);
308 		userret(td, frame);
309 		return;
310 #endif
311 	}
312 
313 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
314 		/*
315 		 * Modify the fault_code to reflect the USR/SVC state at
316 		 * time of fault.
317 		 */
318 		fault_code = FAULT_USER;
319 		td->td_frame = frame;
320 	} else
321 		fault_code = 0;
322 
323 	/* OK this is were we do something about the instruction. */
324 	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
325 	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
326 			       fault_code) == 0)
327 		    break;
328 
329 	if (uh == NULL && (fault_code & FAULT_USER)) {
330 		/* Fault has not been handled */
331 		ksiginfo_init_trap(&ksi);
332 		ksi.ksi_signo = SIGILL;
333 		ksi.ksi_code = ILL_ILLOPC;
334 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
335 		trapsignal(td, &ksi);
336 	}
337 
338 	if ((fault_code & FAULT_USER) == 0) {
339 		if (fault_instruction == KERNEL_BREAKPOINT) {
340 #ifdef KDB
341 			kdb_trap(T_BREAKPOINT, 0, frame);
342 #else
343 			printf("No debugger in kernel.\n");
344 #endif
345 			return;
346 		}
347 		else
348 			panic("Undefined instruction in kernel.\n");
349 	}
350 
351 	userret(td, frame);
352 }
353