undefined.c revision 236991
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
48#include "opt_ddb.h"
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: head/sys/arm/arm/undefined.c 236991 2012-06-13 04:59:55Z imp $");
52
53#include <sys/param.h>
54#include <sys/malloc.h>
55#include <sys/queue.h>
56#include <sys/signal.h>
57#include <sys/systm.h>
58#include <sys/proc.h>
59#include <sys/syslog.h>
60#include <sys/vmmeter.h>
61#include <sys/lock.h>
62#include <sys/mutex.h>
63#include <sys/signalvar.h>
64#include <sys/ptrace.h>
65#ifdef KDB
66#include <sys/kdb.h>
67#endif
68#ifdef FAST_FPE
69#include <sys/acct.h>
70#endif
71
72#include <vm/vm.h>
73#include <vm/vm_extern.h>
74
75#include <machine/asm.h>
76#include <machine/cpu.h>
77#include <machine/frame.h>
78#include <machine/undefined.h>
79#include <machine/trap.h>
80
81#include <machine/disassem.h>
82
83#ifdef DDB
84#include <ddb/db_output.h>
85#endif
86
87#ifdef KDB
88#include <machine/db_machdep.h>
89#endif
90
91static int gdb_trapper(u_int, u_int, struct trapframe *, int);
92#ifdef FAST_FPE
93extern int want_resched;
94#endif
95
96LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
97
98
99void *
100install_coproc_handler(int coproc, undef_handler_t handler)
101{
102	struct undefined_handler *uh;
103
104	KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
105	KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
106
107	/* XXX: M_TEMP??? */
108	uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
109	uh->uh_handler = handler;
110	install_coproc_handler_static(coproc, uh);
111	return uh;
112}
113
114void
115install_coproc_handler_static(int coproc, struct undefined_handler *uh)
116{
117
118	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
119}
120
121void
122remove_coproc_handler(void *cookie)
123{
124	struct undefined_handler *uh = cookie;
125
126	LIST_REMOVE(uh, uh_link);
127	free(uh, M_TEMP);
128}
129
130
131static int
132gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
133{
134	struct thread *td;
135	ksiginfo_t ksi;
136
137	td = (curthread == NULL) ? &thread0 : curthread;
138
139	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
140		if (code == FAULT_USER) {
141			ksiginfo_init_trap(&ksi);
142			ksi.ksi_signo = SIGTRAP;
143			ksi.ksi_code = TRAP_BRKPT;
144			ksi.ksi_addr = (u_int32_t *)addr;
145			trapsignal(td, &ksi);
146			return 0;
147		}
148#if 0
149#ifdef KGDB
150		return !kgdb_trap(T_BREAKPOINT, frame);
151#endif
152#endif
153	}
154	return 1;
155}
156
157static struct undefined_handler gdb_uh;
158
159void
160undefined_init()
161{
162	int loop;
163
164	/* Not actually necessary -- the initialiser is just NULL */
165	for (loop = 0; loop < MAX_COPROCS; ++loop)
166		LIST_INIT(&undefined_handlers[loop]);
167
168	/* Install handler for GDB breakpoints */
169	gdb_uh.uh_handler = gdb_trapper;
170	install_coproc_handler_static(0, &gdb_uh);
171}
172
173
174void
175undefinedinstruction(trapframe_t *frame)
176{
177	struct thread *td;
178	u_int fault_pc;
179	int fault_instruction;
180	int fault_code;
181	int coprocessor;
182	struct undefined_handler *uh;
183#ifdef VERBOSE_ARM32
184	int s;
185#endif
186	ksiginfo_t ksi;
187
188	/* Enable interrupts if they were enabled before the exception. */
189	if (!(frame->tf_spsr & I32_bit))
190		enable_interrupts(I32_bit|F32_bit);
191
192	frame->tf_pc -= INSN_SIZE;
193	PCPU_INC(cnt.v_trap);
194
195	fault_pc = frame->tf_pc;
196
197	/*
198	 * Get the current thread/proc structure or thread0/proc0 if there is
199	 * none.
200	 */
201	td = curthread == NULL ? &thread0 : curthread;
202
203	/*
204	 * Make sure the program counter is correctly aligned so we
205	 * don't take an alignment fault trying to read the opcode.
206	 */
207	if (__predict_false((fault_pc & 3) != 0)) {
208		ksiginfo_init_trap(&ksi);
209		ksi.ksi_signo = SIGILL;
210		ksi.ksi_code = ILL_ILLADR;
211		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
212		trapsignal(td, &ksi);
213		userret(td, frame);
214		return;
215	}
216
217	/*
218	 * Should use fuword() here .. but in the interests of squeezing every
219	 * bit of speed we will just use ReadWord(). We know the instruction
220	 * can be read as was just executed so this will never fail unless the
221	 * kernel is screwed up in which case it does not really matter does
222	 * it ?
223	 */
224
225	fault_instruction = *(u_int32_t *)fault_pc;
226
227	/* Update vmmeter statistics */
228#if 0
229	uvmexp.traps++;
230#endif
231	/* Check for coprocessor instruction */
232
233	/*
234	 * According to the datasheets you only need to look at bit 27 of the
235	 * instruction to tell the difference between and undefined
236	 * instruction and a coprocessor instruction following an undefined
237	 * instruction trap.
238	 */
239
240	if ((fault_instruction & (1 << 27)) != 0)
241		coprocessor = (fault_instruction >> 8) & 0x0f;
242	else
243		coprocessor = 0;
244
245	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
246		/*
247		 * Modify the fault_code to reflect the USR/SVC state at
248		 * time of fault.
249		 */
250		fault_code = FAULT_USER;
251		td->td_frame = frame;
252	} else
253		fault_code = 0;
254
255	/* OK this is were we do something about the instruction. */
256	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
257	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
258			       fault_code) == 0)
259		    break;
260
261	if (fault_code & FAULT_USER && fault_instruction == PTRACE_BREAKPOINT) {
262		PROC_LOCK(td->td_proc);
263		_PHOLD(td->td_proc);
264		ptrace_clear_single_step(td);
265		_PRELE(td->td_proc);
266		PROC_UNLOCK(td->td_proc);
267		return;
268	}
269
270	if (uh == NULL && (fault_code & FAULT_USER)) {
271		/* Fault has not been handled */
272		ksiginfo_init_trap(&ksi);
273		ksi.ksi_signo = SIGILL;
274		ksi.ksi_code = ILL_ILLOPC;
275		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
276		trapsignal(td, &ksi);
277	}
278
279	if ((fault_code & FAULT_USER) == 0) {
280		if (fault_instruction == KERNEL_BREAKPOINT) {
281#ifdef KDB
282			kdb_trap(T_BREAKPOINT, 0, frame);
283#else
284			printf("No debugger in kernel.\n");
285#endif
286			return;
287		} else
288			panic("Undefined instruction in kernel.\n");
289	}
290
291#ifdef FAST_FPE
292	/* Optimised exit code */
293	{
294
295		/*
296		 * Check for reschedule request, at the moment there is only
297		 * 1 ast so this code should always be run
298		 */
299
300		if (want_resched) {
301			/*
302			 * We are being preempted.
303			 */
304			preempt(0);
305		}
306
307		/* Invoke MI userret code */
308		mi_userret(td);
309
310#if 0
311		l->l_priority = l->l_usrpri;
312
313		curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
314#endif
315	}
316
317#else
318	userret(td, frame);
319#endif
320}
321