]> CyberLeo.Net >> Repos - FreeBSD/releng/10.1.git/blob - sys/arm/arm/undefined.c
Fix OpenSSL SSLv2 ciphersuite downgrade vulnerability.
[FreeBSD/releng/10.1.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/asm.h>
73 #include <machine/cpu.h>
74 #include <machine/frame.h>
75 #include <machine/undefined.h>
76 #include <machine/trap.h>
77
78 #include <machine/disassem.h>
79
80 #ifdef DDB
81 #include <ddb/db_output.h>
82 #endif
83
84 #ifdef KDB
85 #include <machine/db_machdep.h>
86 #endif
87
88 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
89
90 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
91
92
93 void *
94 install_coproc_handler(int coproc, undef_handler_t handler)
95 {
96         struct undefined_handler *uh;
97
98         KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
99         KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
100
101         /* XXX: M_TEMP??? */
102         uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
103         uh->uh_handler = handler;
104         install_coproc_handler_static(coproc, uh);
105         return uh;
106 }
107
108 void
109 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
110 {
111
112         LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
113 }
114
115 void
116 remove_coproc_handler(void *cookie)
117 {
118         struct undefined_handler *uh = cookie;
119
120         LIST_REMOVE(uh, uh_link);
121         free(uh, M_TEMP);
122 }
123
124
125 static int
126 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
127 {
128         struct thread *td;
129         ksiginfo_t ksi;
130
131         td = (curthread == NULL) ? &thread0 : curthread;
132
133         if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
134                 if (code == FAULT_USER) {
135                         ksiginfo_init_trap(&ksi);
136                         ksi.ksi_signo = SIGTRAP;
137                         ksi.ksi_code = TRAP_BRKPT;
138                         ksi.ksi_addr = (u_int32_t *)addr;
139                         trapsignal(td, &ksi);
140                         return 0;
141                 }
142 #if 0
143 #ifdef KGDB
144                 return !kgdb_trap(T_BREAKPOINT, frame);
145 #endif
146 #endif
147         }
148         return 1;
149 }
150
151 static struct undefined_handler gdb_uh;
152
153 void
154 undefined_init()
155 {
156         int loop;
157
158         /* Not actually necessary -- the initialiser is just NULL */
159         for (loop = 0; loop < MAX_COPROCS; ++loop)
160                 LIST_INIT(&undefined_handlers[loop]);
161
162         /* Install handler for GDB breakpoints */
163         gdb_uh.uh_handler = gdb_trapper;
164         install_coproc_handler_static(0, &gdb_uh);
165 }
166
167
168 void
169 undefinedinstruction(struct trapframe *frame)
170 {
171         struct thread *td;
172         u_int fault_pc;
173         int fault_instruction;
174         int fault_code;
175         int coprocessor;
176         struct undefined_handler *uh;
177 #ifdef VERBOSE_ARM32
178         int s;
179 #endif
180         ksiginfo_t ksi;
181
182         /* Enable interrupts if they were enabled before the exception. */
183         if (!(frame->tf_spsr & I32_bit))
184                 enable_interrupts(I32_bit|F32_bit);
185
186         PCPU_INC(cnt.v_trap);
187
188         fault_pc = frame->tf_pc;
189
190         /*
191          * Get the current thread/proc structure or thread0/proc0 if there is
192          * none.
193          */
194         td = curthread == NULL ? &thread0 : curthread;
195
196         /*
197          * Make sure the program counter is correctly aligned so we
198          * don't take an alignment fault trying to read the opcode.
199          */
200         if (__predict_false((fault_pc & 3) != 0)) {
201                 ksiginfo_init_trap(&ksi);
202                 ksi.ksi_signo = SIGILL;
203                 ksi.ksi_code = ILL_ILLADR;
204                 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
205                 trapsignal(td, &ksi);
206                 userret(td, frame);
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         coprocessor = 0;
234         if ((fault_instruction & (1 << 27)) != 0)
235                 coprocessor = (fault_instruction >> 8) & 0x0f;
236 #ifdef VFP
237         else {          /* check for special instructions */
238                 if (((fault_instruction & 0xfe000000) == 0xf2000000) ||
239                     ((fault_instruction & 0xff100000) == 0xf4000000))
240                         coprocessor = 10;       /* vfp / simd */
241         }
242 #endif  /* VFP */
243
244         if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
245                 /*
246                  * Modify the fault_code to reflect the USR/SVC state at
247                  * time of fault.
248                  */
249                 fault_code = FAULT_USER;
250                 td->td_frame = frame;
251         } else
252                 fault_code = 0;
253
254         /* OK this is were we do something about the instruction. */
255         LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
256             if (uh->uh_handler(fault_pc, fault_instruction, frame,
257                                fault_code) == 0)
258                     break;
259
260         if (fault_code & FAULT_USER && fault_instruction == PTRACE_BREAKPOINT) {
261                 PROC_LOCK(td->td_proc);
262                 _PHOLD(td->td_proc);
263                 ptrace_clear_single_step(td);
264                 _PRELE(td->td_proc);
265                 PROC_UNLOCK(td->td_proc);
266                 return;
267         }
268
269         if (uh == NULL && (fault_code & FAULT_USER)) {
270                 /* Fault has not been handled */
271                 ksiginfo_init_trap(&ksi);
272                 ksi.ksi_signo = SIGILL;
273                 ksi.ksi_code = ILL_ILLOPC;
274                 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
275                 trapsignal(td, &ksi);
276         }
277
278         if ((fault_code & FAULT_USER) == 0) {
279                 if (fault_instruction == KERNEL_BREAKPOINT) {
280 #ifdef KDB
281                         kdb_trap(T_BREAKPOINT, 0, frame);
282 #else
283                         printf("No debugger in kernel.\n");
284 #endif
285                         return;
286                 } else
287                         panic("Undefined instruction in kernel.\n");
288         }
289
290         userret(td, frame);
291 }