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