xref: /netbsd-src/sys/arch/arm/arm/undefined.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: undefined.c,v 1.17 2003/04/28 01:54:49 briggs 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.17 2003/04/28 01:54:49 briggs 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 
72 #include <uvm/uvm_extern.h>
73 
74 #include <machine/cpu.h>
75 #include <machine/frame.h>
76 #include <arm/undefined.h>
77 #include <machine/trap.h>
78 
79 #include <arch/arm/arm/disassem.h>
80 
81 #ifdef acorn26
82 #include <machine/machdep.h>
83 #endif
84 
85 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
86 
87 #ifdef FAST_FPE
88 extern int want_resched;
89 #endif
90 
91 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
92 
93 
94 void *
95 install_coproc_handler(int coproc, undef_handler_t handler)
96 {
97 	struct undefined_handler *uh;
98 
99 	KASSERT(coproc >= 0 && coproc < MAX_COPROCS);
100 	KASSERT(handler != NULL); /* Used to be legal. */
101 
102 	/* XXX: M_TEMP??? */
103 	MALLOC(uh, struct undefined_handler *, sizeof(*uh), M_TEMP, M_WAITOK);
104 	uh->uh_handler = handler;
105 	install_coproc_handler_static(coproc, uh);
106 	return uh;
107 }
108 
109 void
110 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
111 {
112 
113 	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
114 }
115 
116 void
117 remove_coproc_handler(void *cookie)
118 {
119 	struct undefined_handler *uh = cookie;
120 
121 	LIST_REMOVE(uh, uh_link);
122 	FREE(uh, M_TEMP);
123 }
124 
125 
126 static int
127 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
128 {
129 
130 	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
131 		if (code == FAULT_USER) {
132 			trapsignal(curlwp, SIGTRAP, 0);
133 			return 0;
134 		}
135 #ifdef KGDB
136 		return !kgdb_trap(T_BREAKPOINT, frame);
137 #endif
138 	}
139 	return 1;
140 }
141 
142 static struct undefined_handler gdb_uh;
143 
144 void
145 undefined_init()
146 {
147 	int loop;
148 
149 	/* Not actually necessary -- the initialiser is just NULL */
150 	for (loop = 0; loop < MAX_COPROCS; ++loop)
151 		LIST_INIT(&undefined_handlers[loop]);
152 
153 	/* Install handler for GDB breakpoints */
154 	gdb_uh.uh_handler = gdb_trapper;
155 	install_coproc_handler_static(0, &gdb_uh);
156 }
157 
158 
159 void
160 undefinedinstruction(trapframe_t *frame)
161 {
162 	struct proc *p;
163 	struct lwp *l;
164 	u_int fault_pc;
165 	int fault_instruction;
166 	int fault_code;
167 	int coprocessor;
168 	struct undefined_handler *uh;
169 #ifdef VERBOSE_ARM32
170 	int s;
171 #endif
172 
173 	/* Enable interrupts if they were enabled before the exception. */
174 #ifdef acorn26
175 	if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0)
176 		int_on();
177 #else
178 	if (!(frame->tf_spsr & I32_bit))
179 		enable_interrupts(I32_bit);
180 #endif
181 
182 #ifndef acorn26
183 	frame->tf_pc -= INSN_SIZE;
184 #endif
185 
186 #ifdef __PROG26
187 	fault_pc = frame->tf_r15 & R15_PC;
188 #else
189 	fault_pc = frame->tf_pc;
190 #endif
191 
192 	/*
193 	 * Should use fuword() here .. but in the interests of squeezing every
194 	 * bit of speed we will just use ReadWord(). We know the instruction
195 	 * can be read as was just executed so this will never fail unless the
196 	 * kernel is screwed up in which case it does not really matter does
197 	 * it ?
198 	 */
199 
200 	fault_instruction = *(u_int32_t *)fault_pc;
201 
202 	/* Update vmmeter statistics */
203 	uvmexp.traps++;
204 
205 	/* Check for coprocessor instruction */
206 
207 	/*
208 	 * According to the datasheets you only need to look at bit 27 of the
209 	 * instruction to tell the difference between and undefined
210 	 * instruction and a coprocessor instruction following an undefined
211 	 * instruction trap.
212 	 */
213 
214 	if ((fault_instruction & (1 << 27)) != 0)
215 		coprocessor = (fault_instruction >> 8) & 0x0f;
216 	else
217 		coprocessor = 0;
218 
219 	/* Get the current lwp/proc structure or lwp0/proc0 if there is none. */
220 	l = curlwp == NULL ? &lwp0 : curlwp;
221 	p = l->l_proc;
222 
223 #ifdef __PROG26
224 	if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) {
225 #else
226 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
227 #endif
228 		/*
229 		 * Modify the fault_code to reflect the USR/SVC state at
230 		 * time of fault.
231 		 */
232 		fault_code = FAULT_USER;
233 		l->l_addr->u_pcb.pcb_tf = frame;
234 	} else
235 		fault_code = 0;
236 
237 	/* OK this is were we do something about the instruction. */
238 	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
239 	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
240 			       fault_code) == 0)
241 		    break;
242 
243 	if (uh == NULL) {
244 		/* Fault has not been handled */
245 
246 #ifdef VERBOSE_ARM32
247 		s = spltty();
248 
249 		if ((fault_instruction & 0x0f000010) == 0x0e000000) {
250 			printf("CDP\n");
251 			disassemble(fault_pc);
252 		} else if ((fault_instruction & 0x0e000000) == 0x0c000000) {
253 			printf("LDC/STC\n");
254 			disassemble(fault_pc);
255 		} else if ((fault_instruction & 0x0f000010) == 0x0e000010) {
256 			printf("MRC/MCR\n");
257 			disassemble(fault_pc);
258 		} else if ((fault_instruction & ~INSN_COND_MASK)
259 			 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) {
260 			printf("Undefined instruction\n");
261 			disassemble(fault_pc);
262 		}
263 
264 		splx(s);
265 #endif
266 
267 		if ((fault_code & FAULT_USER) == 0) {
268 			printf("Undefined instruction in kernel\n");
269 #ifdef DDB
270 			Debugger();
271 #endif
272 		}
273 
274 		trapsignal(l, SIGILL, fault_instruction);
275 	}
276 
277 	if ((fault_code & FAULT_USER) == 0)
278 		return;
279 
280 #ifdef FAST_FPE
281 	/* Optimised exit code */
282 	{
283 		int sig;
284 
285 		/*
286 		 * Check for reschedule request, at the moment there is only
287 		 * 1 ast so this code should always be run
288 		 */
289 
290 		if (want_resched) {
291 			/*
292 			 * We are being preempted.
293 			 */
294 			preempt(0);
295 		}
296 
297 		/* take pending signals */
298 		while ((sig = (CURSIG(l))) != 0) {
299 			postsig(sig);
300 		}
301 
302 		/* Invoke per-process kernel-exit handling, if any */
303 		if (p->p_userret)
304 			(p->p_userret)(l, p->p_userret_arg);
305 
306 		/* Invoke any pending upcalls. */
307 		while (l->l_flag & L_SA_UPCALL)
308 			sa_upcall_userret(l);
309 
310 		l->l_priority = l->l_usrpri;
311 
312 		curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
313 	}
314 
315 #else
316 	userret(p);
317 #endif
318 }
319