xref: /netbsd-src/sys/arch/arm/arm/undefined.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: undefined.c,v 1.48 2012/11/12 18:00:35 skrll 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.48 2012/11/12 18:00:35 skrll Exp $");
58 
59 #include <sys/kmem.h>
60 #include <sys/queue.h>
61 #include <sys/signal.h>
62 #include <sys/systm.h>
63 #include <sys/proc.h>
64 #include <sys/syslog.h>
65 #include <sys/vmmeter.h>
66 #include <sys/cpu.h>
67 #ifdef FAST_FPE
68 #include <sys/acct.h>
69 #endif
70 #include <sys/userret.h>
71 
72 #include <uvm/uvm_extern.h>
73 
74 #include <machine/frame.h>
75 #include <machine/pcb.h>
76 #include <machine/trap.h>
77 
78 #include <arm/undefined.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 LIST_HEAD(, undefined_handler) undefined_handlers[NUM_UNKNOWN_HANDLERS];
94 
95 
96 void *
97 install_coproc_handler(int coproc, undef_handler_t handler)
98 {
99 	struct undefined_handler *uh;
100 
101 	KASSERT(coproc >= 0 && coproc < NUM_UNKNOWN_HANDLERS);
102 	KASSERT(handler != NULL); /* Used to be legal. */
103 
104 	uh = kmem_alloc(sizeof(*uh), KM_SLEEP);
105 	uh->uh_handler = handler;
106 	install_coproc_handler_static(coproc, uh);
107 	return uh;
108 }
109 
110 void
111 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
112 {
113 
114 	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
115 }
116 
117 void
118 remove_coproc_handler(void *cookie)
119 {
120 	struct undefined_handler *uh = cookie;
121 
122 	LIST_REMOVE(uh, uh_link);
123 	kmem_free(uh, sizeof(*uh));
124 }
125 
126 static struct evcnt cp15_ev =
127     EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "cpu0", "undefined cp15 insn traps");
128 EVCNT_ATTACH_STATIC(cp15_ev);
129 
130 static int
131 cp15_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
132 {
133 	struct lwp * const l = curlwp;
134 
135 #ifdef THUMB_CODE
136 	if (frame->tf_spsr & PSR_T_bit)
137 		return 1;
138 #endif
139 	if (code != FAULT_USER)
140 		return 1;
141 
142 	/*
143 	 * Don't overwrite sp, pc, etc.
144 	 */
145 	const u_int regno = (insn >> 12) & 15;
146 	if (regno > 12)
147 		return 1;
148 
149 	/*
150 	 * Get a pointer to the register used in the instruction to be emulated.
151 	 */
152 	register_t * const regp = &frame->tf_r0 + regno;
153 
154 	/*
155 	 * Handle MRC p15, 0, <Rd>, c13, c0, 3 (Read User read-only thread id)
156 	 */
157 	if ((insn & 0xffff0fff) == 0xee1d0f70) {
158 		*regp = (uintptr_t)l->l_private;
159 		frame->tf_pc += INSN_SIZE;
160 		cp15_ev.ev_count++;
161 		return 0;
162 	}
163 
164 	/*
165 	 * Handle {MRC,MCR} p15, 0, <Rd>, c13, c0, 2 (User read/write thread id)
166 	 */
167 	if ((insn & 0xffef0fff) == 0xee0d0f50) {
168 		struct pcb * const pcb = lwp_getpcb(l);
169 		if (insn & 0x00100000)
170 			*regp = pcb->pcb_user_pid_rw;
171 		else
172 			pcb->pcb_user_pid_rw = *regp;
173 		frame->tf_pc += INSN_SIZE;
174 		cp15_ev.ev_count++;
175 		return 0;
176 	}
177 
178 	return 1;
179 }
180 
181 static int
182 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
183 {
184 	struct lwp * const l = curlwp;
185 
186 #ifdef THUMB_CODE
187 	if (frame->tf_spsr & PSR_T_bit) {
188 		if (insn == GDB_THUMB_BREAKPOINT)
189 			goto bkpt;
190 	}
191 	else
192 #endif
193 	{
194 		if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
195 #ifdef THUMB_CODE
196 		bkpt:
197 #endif
198 			if (code == FAULT_USER) {
199 				ksiginfo_t ksi;
200 
201 				KSI_INIT_TRAP(&ksi);
202 				ksi.ksi_signo = SIGTRAP;
203 				ksi.ksi_code = TRAP_BRKPT;
204 				ksi.ksi_addr = (uint32_t *)addr;
205 				ksi.ksi_trap = 0;
206 				trapsignal(l, &ksi);
207 				return 0;
208 			}
209 #ifdef KGDB
210 			return !kgdb_trap(T_BREAKPOINT, frame);
211 #endif
212 		}
213 	}
214 	return 1;
215 }
216 
217 static struct undefined_handler cp15_uh;
218 static struct undefined_handler gdb_uh;
219 #ifdef THUMB_CODE
220 static struct undefined_handler gdb_uh_thumb;
221 #endif
222 
223 void
224 undefined_init(void)
225 {
226 	int loop;
227 
228 	/* Not actually necessary -- the initialiser is just NULL */
229 	for (loop = 0; loop < NUM_UNKNOWN_HANDLERS; ++loop)
230 		LIST_INIT(&undefined_handlers[loop]);
231 
232 	/* Install handler for CP15 emulation */
233 	cp15_uh.uh_handler = cp15_trapper;
234 	install_coproc_handler_static(SYSTEM_COPROC, &cp15_uh);
235 
236 	/* Install handler for GDB breakpoints */
237 	gdb_uh.uh_handler = gdb_trapper;
238 	install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &gdb_uh);
239 #ifdef THUMB_CODE
240 	gdb_uh_thumb.uh_handler = gdb_trapper;
241 	install_coproc_handler_static(THUMB_UNKNOWN_HANDLER, &gdb_uh_thumb);
242 #endif
243 }
244 
245 static struct evcnt und_ev =
246     EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "cpu0", "undefined insn traps");
247 EVCNT_ATTACH_STATIC(und_ev);
248 
249 void
250 undefinedinstruction(trapframe_t *frame)
251 {
252 	struct lwp *l;
253 	u_int fault_pc;
254 	int fault_instruction;
255 	int fault_code;
256 	int coprocessor;
257 	int user;
258 	struct undefined_handler *uh;
259 #ifdef VERBOSE_ARM32
260 	int s;
261 #endif
262 
263 	und_ev.ev_count++;
264 
265 	/* Enable interrupts if they were enabled before the exception. */
266 #ifdef acorn26
267 	if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0)
268 		int_on();
269 #else
270 	restore_interrupts(frame->tf_spsr & IF32_bits);
271 #endif
272 
273 #ifndef acorn26
274 #ifdef THUMB_CODE
275 	if (frame->tf_spsr & PSR_T_bit)
276 		frame->tf_pc -= THUMB_INSN_SIZE;
277 	else
278 #endif
279 	{
280 		frame->tf_pc -= INSN_SIZE;
281 	}
282 #endif
283 
284 #ifdef __PROG26
285 	fault_pc = frame->tf_r15 & R15_PC;
286 #else
287 	fault_pc = frame->tf_pc;
288 #endif
289 
290 	/* Get the current lwp/proc structure or lwp0/proc0 if there is none. */
291 	l = curlwp;
292 
293 #ifdef __PROG26
294 	if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) {
295 #else
296 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
297 #endif
298 		user = 1;
299 		LWP_CACHE_CREDS(l, l->l_proc);
300 	} else
301 		user = 0;
302 
303 
304 #ifdef THUMB_CODE
305 	if (frame->tf_spsr & PSR_T_bit) {
306 		fault_instruction = fusword((void *)(fault_pc & ~1));
307 	}
308 	else
309 #endif
310 	{
311 		/*
312 		 * Make sure the program counter is correctly aligned so we
313 		 * don't take an alignment fault trying to read the opcode.
314 		 */
315 		if (__predict_false((fault_pc & 3) != 0)) {
316 			ksiginfo_t ksi;
317 			/* Give the user an illegal instruction signal. */
318 			KSI_INIT_TRAP(&ksi);
319 			ksi.ksi_signo = SIGILL;
320 			ksi.ksi_code = ILL_ILLOPC;
321 			ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc;
322 			trapsignal(l, &ksi);
323 			userret(l);
324 			return;
325 		}
326 	 	/*
327 		 * Should use fuword() here .. but in the interests of
328 		 * squeezing every  bit of speed we will just use
329 		 * ReadWord(). We know the instruction can be read
330 		 * as was just executed so this will never fail unless
331 		 * the kernel is screwed up in which case it does
332 		 * not really matter does it ?
333 		 */
334 
335 		fault_instruction = *(uint32_t *)fault_pc;
336 	}
337 
338 	/* Update vmmeter statistics */
339 	curcpu()->ci_data.cpu_ntrap++;
340 
341 #ifdef THUMB_CODE
342 	if (frame->tf_spsr & PSR_T_bit) {
343 		coprocessor = THUMB_UNKNOWN_HANDLER;
344 	}
345 	else
346 #endif
347 	{
348 		/* Check for coprocessor instruction */
349 
350 		/*
351 		 * According to the datasheets you only need to look at
352 		 * bit 27 of the instruction to tell the difference
353 		 * between and undefined instruction and a coprocessor
354 		 * instruction following an undefined instruction trap.
355 		 *
356 		 * ARMv5 adds undefined instructions in the NV space,
357 		 * even when bit 27 is set.
358 		 */
359 
360 		if ((fault_instruction & (1 << 27)) != 0
361 		    && (fault_instruction & 0xf0000000) != 0xf0000000)
362 			coprocessor = (fault_instruction >> 8) & 0x0f;
363 		else
364 			coprocessor = CORE_UNKNOWN_HANDLER;
365 	}
366 
367 	if (user) {
368 		/*
369 		 * Modify the fault_code to reflect the USR/SVC state at
370 		 * time of fault.
371 		 */
372 		fault_code = FAULT_USER;
373 		lwp_settrapframe(l, frame);
374 	} else
375 		fault_code = 0;
376 
377 	/* OK this is were we do something about the instruction. */
378 	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
379 	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
380 			       fault_code) == 0)
381 		    break;
382 
383 	if (uh == NULL) {
384 		/* Fault has not been handled */
385 		ksiginfo_t ksi;
386 
387 #ifdef VERBOSE_ARM32
388 		s = spltty();
389 
390 		if ((fault_instruction & 0x0f000010) == 0x0e000000) {
391 			printf("CDP\n");
392 			disassemble(fault_pc);
393 		} else if ((fault_instruction & 0x0e000000) == 0x0c000000) {
394 			printf("LDC/STC\n");
395 			disassemble(fault_pc);
396 		} else if ((fault_instruction & 0x0f000010) == 0x0e000010) {
397 			printf("MRC/MCR\n");
398 			disassemble(fault_pc);
399 		} else if ((fault_instruction & ~INSN_COND_MASK)
400 			 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) {
401 			printf("Undefined instruction\n");
402 			disassemble(fault_pc);
403 		}
404 
405 		splx(s);
406 #endif
407 
408 		if ((fault_code & FAULT_USER) == 0) {
409 #ifdef DDB
410 			db_printf("Undefined instruction in kernel\n");
411 			kdb_trap(T_FAULT, frame);
412 #else
413 			panic("undefined instruction in kernel");
414 #endif
415 		}
416 		KSI_INIT_TRAP(&ksi);
417 		ksi.ksi_signo = SIGILL;
418 		ksi.ksi_code = ILL_ILLOPC;
419 		ksi.ksi_addr = (uint32_t *)fault_pc;
420 		ksi.ksi_trap = fault_instruction;
421 		trapsignal(l, &ksi);
422 	}
423 
424 	if ((fault_code & FAULT_USER) == 0)
425 		return;
426 
427 #ifdef FAST_FPE
428 	/* Optimised exit code */
429 	{
430 		/*
431 		 * Check for reschedule request, at the moment there is only
432 		 * 1 ast so this code should always be run
433 		 */
434 		if (curcpu()->ci_want_resched) {
435 			/*
436 			 * We are being preempted.
437 			 */
438 			preempt();
439 		}
440 
441 		/* Invoke MI userret code */
442 		mi_userret(l);
443 	}
444 #else
445 	userret(l);
446 #endif
447 }
448