xref: /netbsd-src/sys/arch/arm/arm/undefined.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Ben Harris.
5  * Copyright (c) 1995 Mark Brinicombe.
6  * Copyright (c) 1995 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Brini.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * undefined.c
41  *
42  * Fault handler
43  *
44  * Created      : 06/01/95
45  */
46 
47 #define FAST_FPE
48 
49 #include "opt_ddb.h"
50 #include "opt_kgdb.h"
51 
52 #include <sys/param.h>
53 #ifdef KGDB
54 #include <sys/kgdb.h>
55 #endif
56 
57 __KERNEL_RCSID(0, "$NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $");
58 
59 #include <sys/malloc.h>
60 #include <sys/queue.h>
61 #include <sys/signal.h>
62 #include <sys/systm.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65 #include <sys/syslog.h>
66 #include <sys/vmmeter.h>
67 #include <sys/savar.h>
68 #ifdef FAST_FPE
69 #include <sys/acct.h>
70 #endif
71 #include <sys/userret.h>
72 
73 #include <uvm/uvm_extern.h>
74 
75 #include <machine/cpu.h>
76 #include <machine/frame.h>
77 #include <arm/undefined.h>
78 #include <machine/trap.h>
79 
80 #include <arch/arm/arm/disassem.h>
81 
82 #ifdef DDB
83 #include <ddb/db_output.h>
84 #include <machine/db_machdep.h>
85 #endif
86 
87 #ifdef acorn26
88 #include <machine/machdep.h>
89 #endif
90 
91 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
92 
93 #ifdef FAST_FPE
94 extern int want_resched;
95 #endif
96 
97 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
98 
99 
100 void *
101 install_coproc_handler(int coproc, undef_handler_t handler)
102 {
103 	struct undefined_handler *uh;
104 
105 	KASSERT(coproc >= 0 && coproc < MAX_COPROCS);
106 	KASSERT(handler != NULL); /* Used to be legal. */
107 
108 	/* XXX: M_TEMP??? */
109 	MALLOC(uh, struct undefined_handler *, sizeof(*uh), M_TEMP, M_WAITOK);
110 	uh->uh_handler = handler;
111 	install_coproc_handler_static(coproc, uh);
112 	return uh;
113 }
114 
115 void
116 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
117 {
118 
119 	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
120 }
121 
122 void
123 remove_coproc_handler(void *cookie)
124 {
125 	struct undefined_handler *uh = cookie;
126 
127 	LIST_REMOVE(uh, uh_link);
128 	FREE(uh, M_TEMP);
129 }
130 
131 
132 static int
133 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
134 {
135 	struct lwp *l;
136 	l = (curlwp == NULL) ? &lwp0 : curlwp;
137 
138 	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
139 		if (code == FAULT_USER) {
140 			ksiginfo_t ksi;
141 
142 			KSI_INIT_TRAP(&ksi);
143 			ksi.ksi_signo = SIGTRAP;
144 			ksi.ksi_code = TRAP_BRKPT;
145 			ksi.ksi_addr = (u_int32_t *)addr;
146 			ksi.ksi_trap = 0;
147 			KERNEL_PROC_LOCK(l->l_proc);
148 			trapsignal(l, &ksi);
149 			KERNEL_PROC_UNLOCK(l->l_proc);
150 			return 0;
151 		}
152 #ifdef KGDB
153 		return !kgdb_trap(T_BREAKPOINT, frame);
154 #endif
155 	}
156 	return 1;
157 }
158 
159 static struct undefined_handler gdb_uh;
160 
161 void
162 undefined_init()
163 {
164 	int loop;
165 
166 	/* Not actually necessary -- the initialiser is just NULL */
167 	for (loop = 0; loop < MAX_COPROCS; ++loop)
168 		LIST_INIT(&undefined_handlers[loop]);
169 
170 	/* Install handler for GDB breakpoints */
171 	gdb_uh.uh_handler = gdb_trapper;
172 	install_coproc_handler_static(0, &gdb_uh);
173 }
174 
175 
176 void
177 undefinedinstruction(trapframe_t *frame)
178 {
179 	struct lwp *l;
180 	u_int fault_pc;
181 	int fault_instruction;
182 	int fault_code;
183 	int coprocessor;
184 	struct undefined_handler *uh;
185 #ifdef VERBOSE_ARM32
186 	int s;
187 #endif
188 
189 	/* Enable interrupts if they were enabled before the exception. */
190 #ifdef acorn26
191 	if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0)
192 		int_on();
193 #else
194 	if (!(frame->tf_spsr & I32_bit))
195 		enable_interrupts(I32_bit);
196 #endif
197 
198 #ifndef acorn26
199 	frame->tf_pc -= INSN_SIZE;
200 #endif
201 
202 #ifdef __PROG26
203 	fault_pc = frame->tf_r15 & R15_PC;
204 #else
205 	fault_pc = frame->tf_pc;
206 #endif
207 
208 	/* Get the current lwp/proc structure or lwp0/proc0 if there is none. */
209 	l = curlwp == NULL ? &lwp0 : curlwp;
210 
211 	/*
212 	 * Make sure the program counter is correctly aligned so we
213 	 * don't take an alignment fault trying to read the opcode.
214 	 */
215 	if (__predict_false((fault_pc & 3) != 0)) {
216 		ksiginfo_t ksi;
217 		/* Give the user an illegal instruction signal. */
218 		KSI_INIT_TRAP(&ksi);
219 		ksi.ksi_signo = SIGILL;
220 		ksi.ksi_code = ILL_ILLOPC;
221 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
222 		KERNEL_PROC_LOCK(l);
223 		trapsignal(l, &ksi);
224 		KERNEL_PROC_UNLOCK(l);
225 		userret(l);
226 		return;
227 	}
228 
229 	/*
230 	 * Should use fuword() here .. but in the interests of squeezing every
231 	 * bit of speed we will just use ReadWord(). We know the instruction
232 	 * can be read as was just executed so this will never fail unless the
233 	 * kernel is screwed up in which case it does not really matter does
234 	 * it ?
235 	 */
236 
237 	fault_instruction = *(u_int32_t *)fault_pc;
238 
239 	/* Update vmmeter statistics */
240 	uvmexp.traps++;
241 
242 	/* Check for coprocessor instruction */
243 
244 	/*
245 	 * According to the datasheets you only need to look at bit 27 of the
246 	 * instruction to tell the difference between and undefined
247 	 * instruction and a coprocessor instruction following an undefined
248 	 * instruction trap.
249 	 */
250 
251 	if ((fault_instruction & (1 << 27)) != 0)
252 		coprocessor = (fault_instruction >> 8) & 0x0f;
253 	else
254 		coprocessor = 0;
255 
256 #ifdef __PROG26
257 	if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) {
258 #else
259 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
260 #endif
261 		/*
262 		 * Modify the fault_code to reflect the USR/SVC state at
263 		 * time of fault.
264 		 */
265 		fault_code = FAULT_USER;
266 		l->l_addr->u_pcb.pcb_tf = frame;
267 	} else
268 		fault_code = 0;
269 
270 	/* OK this is were we do something about the instruction. */
271 	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
272 	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
273 			       fault_code) == 0)
274 		    break;
275 
276 	if (uh == NULL) {
277 		/* Fault has not been handled */
278 		ksiginfo_t ksi;
279 
280 #ifdef VERBOSE_ARM32
281 		s = spltty();
282 
283 		if ((fault_instruction & 0x0f000010) == 0x0e000000) {
284 			printf("CDP\n");
285 			disassemble(fault_pc);
286 		} else if ((fault_instruction & 0x0e000000) == 0x0c000000) {
287 			printf("LDC/STC\n");
288 			disassemble(fault_pc);
289 		} else if ((fault_instruction & 0x0f000010) == 0x0e000010) {
290 			printf("MRC/MCR\n");
291 			disassemble(fault_pc);
292 		} else if ((fault_instruction & ~INSN_COND_MASK)
293 			 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) {
294 			printf("Undefined instruction\n");
295 			disassemble(fault_pc);
296 		}
297 
298 		splx(s);
299 #endif
300 
301 		if ((fault_code & FAULT_USER) == 0) {
302 #ifdef DDB
303 			db_printf("Undefined instruction in kernel\n");
304 			kdb_trap(T_FAULT, frame);
305 #else
306 			panic("undefined instruction in kernel");
307 #endif
308 		}
309 		KSI_INIT_TRAP(&ksi);
310 		ksi.ksi_signo = SIGILL;
311 		ksi.ksi_code = ILL_ILLOPC;
312 		ksi.ksi_addr = (u_int32_t *)fault_pc;
313 		ksi.ksi_trap = fault_instruction;
314 		KERNEL_PROC_LOCK(l);
315 		trapsignal(l, &ksi);
316 		KERNEL_PROC_UNLOCK(l);
317 	}
318 
319 	if ((fault_code & FAULT_USER) == 0)
320 		return;
321 
322 #ifdef FAST_FPE
323 	/* Optimised exit code */
324 	{
325 
326 		/*
327 		 * Check for reschedule request, at the moment there is only
328 		 * 1 ast so this code should always be run
329 		 */
330 
331 		if (want_resched) {
332 			/*
333 			 * We are being preempted.
334 			 */
335 			preempt(0);
336 		}
337 
338 		/* Invoke MI userret code */
339 		mi_userret(l);
340 
341 		l->l_priority = l->l_usrpri;
342 
343 		curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
344 	}
345 
346 #else
347 	userret(l);
348 #endif
349 }
350