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