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