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