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