]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/undefined.c
MFH
[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/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
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71
72 #include <machine/armreg.h>
73 #include <machine/asm.h>
74 #include <machine/cpu.h>
75 #include <machine/frame.h>
76 #include <machine/undefined.h>
77 #include <machine/trap.h>
78
79 #include <machine/disassem.h>
80
81 #ifdef DDB
82 #include <ddb/db_output.h>
83 #endif
84
85 #ifdef KDB
86 #include <machine/db_machdep.h>
87 #endif
88
89 #define ARM_COPROC_INSN(insn)   (((insn) & (1 << 27)) != 0)
90 #define ARM_VFP_INSN(insn)      ((((insn) & 0xfe000000) == 0xf2000000) || \
91     (((insn) & 0xff100000) == 0xf4000000))
92 #define ARM_COPROC(insn)        (((insn) >> 8) & 0xf)
93
94 #define THUMB_32BIT_INSN(insn)  ((insn) >= 0xe800)
95 #define THUMB_COPROC_INSN(insn) (((insn) & (3 << 26)) == (3 << 26))
96 #define THUMB_COPROC_UNDEFINED(insn) (((insn) & 0x3e << 20) == 0)
97 #define THUMB_VFP_INSN(insn)    (((insn) & (3 << 24)) == (3 << 24))
98 #define THUMB_COPROC(insn)      (((insn) >> 8) & 0xf)
99
100 #define COPROC_VFP      10
101
102 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
103
104 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
105
106
107 void *
108 install_coproc_handler(int coproc, undef_handler_t handler)
109 {
110         struct undefined_handler *uh;
111
112         KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
113         KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
114
115         /* XXX: M_TEMP??? */
116         uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
117         uh->uh_handler = handler;
118         install_coproc_handler_static(coproc, uh);
119         return uh;
120 }
121
122 void
123 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
124 {
125
126         LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
127 }
128
129 void
130 remove_coproc_handler(void *cookie)
131 {
132         struct undefined_handler *uh = cookie;
133
134         LIST_REMOVE(uh, uh_link);
135         free(uh, M_TEMP);
136 }
137
138
139 static int
140 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
141 {
142         struct thread *td;
143         ksiginfo_t ksi;
144
145         td = (curthread == NULL) ? &thread0 : curthread;
146
147         if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
148                 if (code == FAULT_USER) {
149                         ksiginfo_init_trap(&ksi);
150                         ksi.ksi_signo = SIGTRAP;
151                         ksi.ksi_code = TRAP_BRKPT;
152                         ksi.ksi_addr = (u_int32_t *)addr;
153                         trapsignal(td, &ksi);
154                         return 0;
155                 }
156 #if 0
157 #ifdef KGDB
158                 return !kgdb_trap(T_BREAKPOINT, frame);
159 #endif
160 #endif
161         }
162         return 1;
163 }
164
165 static struct undefined_handler gdb_uh;
166
167 void
168 undefined_init()
169 {
170         int loop;
171
172         /* Not actually necessary -- the initialiser is just NULL */
173         for (loop = 0; loop < MAX_COPROCS; ++loop)
174                 LIST_INIT(&undefined_handlers[loop]);
175
176         /* Install handler for GDB breakpoints */
177         gdb_uh.uh_handler = gdb_trapper;
178         install_coproc_handler_static(0, &gdb_uh);
179 }
180
181
182 void
183 undefinedinstruction(struct trapframe *frame)
184 {
185         struct thread *td;
186         u_int fault_pc;
187         int fault_instruction;
188         int fault_code;
189         int coprocessor;
190         struct undefined_handler *uh;
191         int error;
192 #ifdef VERBOSE_ARM32
193         int s;
194 #endif
195         ksiginfo_t ksi;
196
197         /* Enable interrupts if they were enabled before the exception. */
198         if (__predict_true(frame->tf_spsr & PSR_I) == 0)
199                 enable_interrupts(PSR_I);
200         if (__predict_true(frame->tf_spsr & PSR_F) == 0)
201                 enable_interrupts(PSR_F);
202
203         PCPU_INC(cnt.v_trap);
204
205         fault_pc = frame->tf_pc;
206
207         /*
208          * Get the current thread/proc structure or thread0/proc0 if there is
209          * none.
210          */
211         td = curthread == NULL ? &thread0 : curthread;
212
213         coprocessor = 0;
214         if ((frame->tf_spsr & PSR_T) == 0) {
215                 /*
216                  * Make sure the program counter is correctly aligned so we
217                  * don't take an alignment fault trying to read the opcode.
218                  */
219                 if (__predict_false((fault_pc & 3) != 0)) {
220                         ksiginfo_init_trap(&ksi);
221                         ksi.ksi_signo = SIGILL;
222                         ksi.ksi_code = ILL_ILLADR;
223                         ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
224                         trapsignal(td, &ksi);
225                         userret(td, frame);
226                         return;
227                 }
228
229                 /*
230                  * Should use fuword() here .. but in the interests of
231                  * squeezing every bit of speed we will just use ReadWord().
232                  * We know the instruction can be read as was just executed
233                  * so this will never fail unless the kernel is screwed up
234                  * in which case it does not really matter does it ?
235                  */
236
237                 fault_instruction = *(u_int32_t *)fault_pc;
238
239                 /* Check for coprocessor instruction */
240
241                 /*
242                  * According to the datasheets you only need to look at bit
243                  * 27 of the instruction to tell the difference between and
244                  * undefined instruction and a coprocessor instruction
245                  * following an undefined instruction trap.
246                  */
247
248                 if (ARM_COPROC_INSN(fault_instruction))
249                         coprocessor = ARM_COPROC(fault_instruction);
250                 else {          /* check for special instructions */
251                         if (ARM_VFP_INSN(fault_instruction))
252                                 coprocessor = COPROC_VFP; /* vfp / simd */
253                 }
254         } else {
255 #if __ARM_ARCH >= 7
256                 fault_instruction = *(uint16_t *)fault_pc;
257                 if (THUMB_32BIT_INSN(fault_instruction)) {
258                         fault_instruction <<= 16;
259                         fault_instruction |= *(uint16_t *)(fault_pc + 2);
260
261                         /*
262                          * Is it a Coprocessor, Advanced SIMD, or
263                          * Floating-point instruction.
264                          */
265                         if (THUMB_COPROC_INSN(fault_instruction)) {
266                                 if (THUMB_COPROC_UNDEFINED(fault_instruction)) {
267                                         /* undefined insn */
268                                 } else if (THUMB_VFP_INSN(fault_instruction))
269                                         coprocessor = COPROC_VFP;
270                                 else
271                                         coprocessor =
272                                             THUMB_COPROC(fault_instruction);
273                         }
274                 }
275 #else
276                 /*
277                  * No support for Thumb-2 on this cpu
278                  */
279                 ksiginfo_init_trap(&ksi);
280                 ksi.ksi_signo = SIGILL;
281                 ksi.ksi_code = ILL_ILLADR;
282                 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
283                 trapsignal(td, &ksi);
284                 userret(td, frame);
285                 return;
286 #endif
287         }
288
289         if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
290                 /*
291                  * Modify the fault_code to reflect the USR/SVC state at
292                  * time of fault.
293                  */
294                 fault_code = FAULT_USER;
295                 td->td_frame = frame;
296         } else
297                 fault_code = 0;
298
299         /* OK this is were we do something about the instruction. */
300         LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
301             if (uh->uh_handler(fault_pc, fault_instruction, frame,
302                                fault_code) == 0)
303                     break;
304
305         if (fault_code & FAULT_USER) {
306                 /* TODO: No support for ptrace from Thumb-2 */
307                 if ((frame->tf_spsr & PSR_T) == 0 &&
308                     fault_instruction == PTRACE_BREAKPOINT) {
309                         PROC_LOCK(td->td_proc);
310                         _PHOLD(td->td_proc);
311                         error = ptrace_clear_single_step(td);
312                         _PRELE(td->td_proc);
313                         PROC_UNLOCK(td->td_proc);
314                         if (error != 0) {
315                                 ksiginfo_init_trap(&ksi);
316                                 ksi.ksi_signo = SIGILL;
317                                 ksi.ksi_code = ILL_ILLOPC;
318                                 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
319                                 trapsignal(td, &ksi);
320                         }
321                         return;
322                 }
323         }
324
325         if (uh == NULL && (fault_code & FAULT_USER)) {
326                 /* Fault has not been handled */
327                 ksiginfo_init_trap(&ksi);
328                 ksi.ksi_signo = SIGILL;
329                 ksi.ksi_code = ILL_ILLOPC;
330                 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
331                 trapsignal(td, &ksi);
332         }
333
334         if ((fault_code & FAULT_USER) == 0) {
335                 if (fault_instruction == KERNEL_BREAKPOINT) {
336 #ifdef KDB
337                         kdb_trap(T_BREAKPOINT, 0, frame);
338 #else
339                         printf("No debugger in kernel.\n");
340 #endif
341                         return;
342                 }
343                 else
344                         panic("Undefined instruction in kernel.\n");
345         }
346
347         userret(td, frame);
348 }