fault.c revision 1.40 1 1.40 scw /* $NetBSD: fault.c,v 1.40 2003/11/14 00:21:30 scw Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.27 scw * Copyright 2003 Wasabi Systems, Inc.
5 1.27 scw * All rights reserved.
6 1.27 scw *
7 1.27 scw * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 1.27 scw *
9 1.27 scw * Redistribution and use in source and binary forms, with or without
10 1.27 scw * modification, are permitted provided that the following conditions
11 1.27 scw * are met:
12 1.27 scw * 1. Redistributions of source code must retain the above copyright
13 1.27 scw * notice, this list of conditions and the following disclaimer.
14 1.27 scw * 2. Redistributions in binary form must reproduce the above copyright
15 1.27 scw * notice, this list of conditions and the following disclaimer in the
16 1.27 scw * documentation and/or other materials provided with the distribution.
17 1.27 scw * 3. All advertising materials mentioning features or use of this software
18 1.27 scw * must display the following acknowledgement:
19 1.27 scw * This product includes software developed for the NetBSD Project by
20 1.27 scw * Wasabi Systems, Inc.
21 1.27 scw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.27 scw * or promote products derived from this software without specific prior
23 1.27 scw * written permission.
24 1.27 scw *
25 1.27 scw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.27 scw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.27 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.27 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.27 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.27 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.27 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.27 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.27 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.27 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.27 scw * POSSIBILITY OF SUCH DAMAGE.
36 1.27 scw */
37 1.27 scw /*
38 1.1 chris * Copyright (c) 1994-1997 Mark Brinicombe.
39 1.1 chris * Copyright (c) 1994 Brini.
40 1.1 chris * All rights reserved.
41 1.1 chris *
42 1.1 chris * This code is derived from software written for Brini by Mark Brinicombe
43 1.1 chris *
44 1.1 chris * Redistribution and use in source and binary forms, with or without
45 1.1 chris * modification, are permitted provided that the following conditions
46 1.1 chris * are met:
47 1.1 chris * 1. Redistributions of source code must retain the above copyright
48 1.1 chris * notice, this list of conditions and the following disclaimer.
49 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 chris * notice, this list of conditions and the following disclaimer in the
51 1.1 chris * documentation and/or other materials provided with the distribution.
52 1.1 chris * 3. All advertising materials mentioning features or use of this software
53 1.1 chris * must display the following acknowledgement:
54 1.1 chris * This product includes software developed by Brini.
55 1.1 chris * 4. The name of the company nor the name of the author may be used to
56 1.1 chris * endorse or promote products derived from this software without specific
57 1.1 chris * prior written permission.
58 1.1 chris *
59 1.1 chris * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
60 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 1.1 chris * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 chris * SUCH DAMAGE.
70 1.1 chris *
71 1.1 chris * RiscBSD kernel project
72 1.1 chris *
73 1.1 chris * fault.c
74 1.1 chris *
75 1.1 chris * Fault handlers
76 1.1 chris *
77 1.1 chris * Created : 28/11/94
78 1.1 chris */
79 1.1 chris
80 1.1 chris #include "opt_ddb.h"
81 1.28 briggs #include "opt_kgdb.h"
82 1.1 chris
83 1.1 chris #include <sys/types.h>
84 1.40 scw __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.40 2003/11/14 00:21:30 scw Exp $");
85 1.21 bjh21
86 1.1 chris #include <sys/param.h>
87 1.1 chris #include <sys/systm.h>
88 1.1 chris #include <sys/proc.h>
89 1.33 agc #include <sys/savar.h>
90 1.1 chris #include <sys/user.h>
91 1.1 chris #include <sys/kernel.h>
92 1.1 chris
93 1.1 chris #include <uvm/uvm_extern.h>
94 1.18 thorpej
95 1.18 thorpej #include <arm/cpuconf.h>
96 1.1 chris
97 1.1 chris #include <machine/frame.h>
98 1.5 thorpej #include <arm/arm32/katelib.h>
99 1.1 chris #include <machine/cpu.h>
100 1.2 matt #include <machine/intr.h>
101 1.28 briggs #if defined(DDB) || defined(KGDB)
102 1.1 chris #include <machine/db_machdep.h>
103 1.28 briggs #ifdef KGDB
104 1.28 briggs #include <sys/kgdb.h>
105 1.28 briggs #endif
106 1.28 briggs #if !defined(DDB)
107 1.28 briggs #define kdb_trap kgdb_trap
108 1.28 briggs #endif
109 1.1 chris #endif
110 1.1 chris
111 1.1 chris #include <arch/arm/arm/disassem.h>
112 1.7 chris #include <arm/arm32/machdep.h>
113 1.7 chris
114 1.1 chris extern char fusubailout[];
115 1.1 chris
116 1.27 scw #ifdef DEBUG
117 1.27 scw int last_fault_code; /* For the benefit of pmap_fault_fixup() */
118 1.27 scw #endif
119 1.27 scw
120 1.39 scw #if defined(CPU_ARM3) || defined(CPU_ARM6) || \
121 1.39 scw defined(CPU_ARM7) || defined(CPU_ARM7TDMI)
122 1.39 scw /* These CPUs may need data/prefetch abort fixups */
123 1.39 scw #define CPU_ABORT_FIXUP_REQUIRED
124 1.39 scw #endif
125 1.7 chris
126 1.39 scw struct data_abort {
127 1.39 scw int (*func)(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
128 1.39 scw const char *desc;
129 1.39 scw };
130 1.1 chris
131 1.39 scw static int dab_fatal(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
132 1.39 scw static int dab_align(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
133 1.39 scw static int dab_buserr(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
134 1.39 scw
135 1.39 scw static const struct data_abort data_aborts[] = {
136 1.39 scw {dab_fatal, "Vector Exception"},
137 1.39 scw {dab_align, "Alignment Fault 1"},
138 1.39 scw {dab_fatal, "Terminal Exception"},
139 1.39 scw {dab_align, "Alignment Fault 3"},
140 1.39 scw {dab_buserr, "External Linefetch Abort (S)"},
141 1.39 scw {NULL, "Translation Fault (S)"},
142 1.39 scw {dab_buserr, "External Linefetch Abort (P)"},
143 1.39 scw {NULL, "Translation Fault (P)"},
144 1.39 scw {dab_buserr, "External Non-Linefetch Abort (S)"},
145 1.39 scw {NULL, "Domain Fault (S)"},
146 1.39 scw {dab_buserr, "External Non-Linefetch Abort (P)"},
147 1.39 scw {NULL, "Domain Fault (P)"},
148 1.39 scw {dab_buserr, "External Translation Abort (L1)"},
149 1.39 scw {NULL, "Permission Fault (S)"},
150 1.39 scw {dab_buserr, "External Translation Abort (L2)"},
151 1.39 scw {NULL, "Permission Fault (P)"}
152 1.39 scw };
153 1.1 chris
154 1.39 scw /* Determine if a fault came from user mode */
155 1.39 scw #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
156 1.39 scw
157 1.39 scw /* Determine if 'x' is a permission fault */
158 1.39 scw #define IS_PERMISSION_FAULT(x) \
159 1.39 scw (((1 << ((x) & FAULT_TYPE_MASK)) & \
160 1.39 scw ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
161 1.1 chris
162 1.39 scw #if 0
163 1.39 scw /* maybe one day we'll do emulations */
164 1.39 scw #define TRAPSIGNAL(l,k) (*(l)->l_proc->p_emul->e_trapsignal)((l), (k))
165 1.39 scw #else
166 1.39 scw #define TRAPSIGNAL(l,k) trapsignal((l), (k))
167 1.1 chris #endif
168 1.3 thorpej
169 1.39 scw static __inline void
170 1.39 scw call_trapsignal(struct lwp *l, ksiginfo_t *ksi)
171 1.3 thorpej {
172 1.3 thorpej
173 1.39 scw KERNEL_PROC_LOCK(l->l_proc);
174 1.39 scw TRAPSIGNAL(l, ksi);
175 1.39 scw KERNEL_PROC_UNLOCK(l->l_proc);
176 1.39 scw }
177 1.3 thorpej
178 1.39 scw static __inline int
179 1.39 scw data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l)
180 1.39 scw {
181 1.39 scw #ifdef CPU_ABORT_FIXUP_REQUIRED
182 1.39 scw int error;
183 1.3 thorpej
184 1.39 scw /* Call the cpu specific data abort fixup routine */
185 1.39 scw error = cpu_dataabt_fixup(tf);
186 1.39 scw if (__predict_true(error != ABORT_FIXUP_FAILED))
187 1.39 scw return (error);
188 1.3 thorpej
189 1.39 scw /*
190 1.39 scw * Oops, couldn't fix up the instruction
191 1.39 scw */
192 1.39 scw printf("data_abort_fixup: fixup for %s mode data abort failed.\n",
193 1.39 scw TRAP_USERMODE(tf) ? "user" : "kernel");
194 1.39 scw printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
195 1.39 scw *((u_int *)tf->tf_pc));
196 1.39 scw disassemble(tf->tf_pc);
197 1.39 scw
198 1.39 scw /* Die now if this happened in kernel mode */
199 1.39 scw if (!TRAP_USERMODE(tf))
200 1.39 scw dab_fatal(tf, fsr, far, l, NULL);
201 1.3 thorpej
202 1.39 scw return (error);
203 1.39 scw #else
204 1.39 scw return (ABORT_FIXUP_OK);
205 1.39 scw #endif /* CPU_ABORT_FIXUP_REQUIRED */
206 1.3 thorpej }
207 1.3 thorpej
208 1.1 chris void
209 1.39 scw data_abort_handler(trapframe_t *tf)
210 1.1 chris {
211 1.39 scw struct vm_map *map;
212 1.39 scw struct pcb *pcb;
213 1.26 thorpej struct lwp *l;
214 1.39 scw u_int user, far, fsr;
215 1.39 scw vm_prot_t ftype;
216 1.1 chris void *onfault;
217 1.27 scw vaddr_t va;
218 1.39 scw int error;
219 1.34 matt ksiginfo_t ksi;
220 1.3 thorpej
221 1.39 scw /* Grab FAR/FSR before enabling interrupts */
222 1.39 scw far = cpu_faultaddress();
223 1.39 scw fsr = cpu_faultstatus();
224 1.1 chris
225 1.39 scw /* Update vmmeter statistics */
226 1.39 scw uvmexp.traps++;
227 1.1 chris
228 1.39 scw /* Re-enable interrupts if they were enabled previously */
229 1.39 scw if (__predict_false((tf->tf_spsr & I32_bit) == 0))
230 1.1 chris enable_interrupts(I32_bit);
231 1.1 chris
232 1.39 scw /* Get the current lwp structure or lwp0 if there is none */
233 1.39 scw l = (curlwp != NULL) ? curlwp : &lwp0;
234 1.1 chris
235 1.39 scw /* Data abort came from user mode? */
236 1.39 scw user = TRAP_USERMODE(tf);
237 1.1 chris
238 1.39 scw /* Grab the current pcb */
239 1.39 scw pcb = &l->l_addr->u_pcb;
240 1.1 chris
241 1.39 scw /* Invoke the appropriate handler, if necessary */
242 1.39 scw if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
243 1.39 scw if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
244 1.39 scw l, &ksi))
245 1.39 scw goto do_trapsignal;
246 1.39 scw goto out;
247 1.39 scw }
248 1.1 chris
249 1.1 chris /*
250 1.39 scw * At this point, we're dealing with one of the following data aborts:
251 1.39 scw *
252 1.39 scw * FAULT_TRANS_S - Translation -- Section
253 1.39 scw * FAULT_TRANS_P - Translation -- Page
254 1.39 scw * FAULT_DOMAIN_S - Domain -- Section
255 1.39 scw * FAULT_DOMAIN_P - Domain -- Page
256 1.39 scw * FAULT_PERM_S - Permission -- Section
257 1.39 scw * FAULT_PERM_P - Permission -- Page
258 1.39 scw *
259 1.39 scw * These are the main virtual memory-related faults signalled by
260 1.39 scw * the MMU.
261 1.1 chris */
262 1.1 chris
263 1.1 chris /* fusubailout is used by [fs]uswintr to avoid page faulting */
264 1.39 scw if (__predict_false(pcb->pcb_onfault == fusubailout)) {
265 1.39 scw tf->tf_r0 = EFAULT;
266 1.39 scw tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
267 1.1 chris return;
268 1.1 chris }
269 1.1 chris
270 1.39 scw if (user)
271 1.39 scw l->l_addr->u_pcb.pcb_tf = tf;
272 1.1 chris
273 1.39 scw /* See if the cpu state needs to be fixed up */
274 1.39 scw switch (data_abort_fixup(tf, fsr, far, l)) {
275 1.39 scw case ABORT_FIXUP_RETURN:
276 1.1 chris return;
277 1.39 scw case ABORT_FIXUP_FAILED:
278 1.39 scw /* Deliver a SIGILL to the process */
279 1.39 scw KSI_INIT_TRAP(&ksi);
280 1.39 scw ksi.ksi_signo = SIGILL;
281 1.39 scw ksi.ksi_code = ILL_ILLOPC;
282 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
283 1.39 scw ksi.ksi_trap = fsr;
284 1.39 scw goto do_trapsignal;
285 1.39 scw default:
286 1.39 scw break;
287 1.40 scw }
288 1.40 scw
289 1.40 scw /*
290 1.40 scw * Make sure the Program Counter is sane. We could fall foul of
291 1.40 scw * someone executing Thumb code, in which case the PC might not
292 1.40 scw * be word-aligned. This would cause a kernel alignment fault
293 1.40 scw * further down if we have to decode the current instruction.
294 1.40 scw * XXX: It would be nice to be able to support Thumb at some point.
295 1.40 scw */
296 1.40 scw if (__predict_false((tf->tf_pc & 3) != 0)) {
297 1.40 scw if (user) {
298 1.40 scw /*
299 1.40 scw * Give the user an illegal instruction signal.
300 1.40 scw */
301 1.40 scw /* Deliver a SIGILL to the process */
302 1.40 scw KSI_INIT_TRAP(&ksi);
303 1.40 scw ksi.ksi_signo = SIGILL;
304 1.40 scw ksi.ksi_code = ILL_ILLOPC;
305 1.40 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
306 1.40 scw ksi.ksi_trap = fsr;
307 1.40 scw goto do_trapsignal;
308 1.40 scw }
309 1.40 scw
310 1.40 scw /*
311 1.40 scw * The kernel never executes Thumb code.
312 1.40 scw */
313 1.40 scw printf("\ndata_abort_fault: Misaligned Kernel-mode "
314 1.40 scw "Program Counter\n");
315 1.40 scw dab_fatal(tf, fsr, far, l, NULL);
316 1.27 scw }
317 1.27 scw
318 1.39 scw va = trunc_page((vaddr_t)far);
319 1.1 chris
320 1.27 scw /*
321 1.27 scw * It is only a kernel address space fault iff:
322 1.27 scw * 1. user == 0 and
323 1.27 scw * 2. pcb_onfault not set or
324 1.27 scw * 3. pcb_onfault set but supervisor space fault
325 1.27 scw * The last can occur during an exec() copyin where the
326 1.27 scw * argument space is lazy-allocated.
327 1.27 scw */
328 1.39 scw if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
329 1.39 scw (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
330 1.39 scw map = kernel_map;
331 1.39 scw
332 1.27 scw /* Was the fault due to the FPE/IPKDB ? */
333 1.39 scw if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
334 1.35 thorpej KSI_INIT_TRAP(&ksi);
335 1.34 matt ksi.ksi_signo = SIGSEGV;
336 1.39 scw ksi.ksi_code = SEGV_ACCERR;
337 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
338 1.39 scw ksi.ksi_trap = fsr;
339 1.27 scw
340 1.27 scw /*
341 1.27 scw * Force exit via userret()
342 1.39 scw * This is necessary as the FPE is an extension to
343 1.39 scw * userland that actually runs in a priveledged mode
344 1.39 scw * but uses USR mode permissions for its accesses.
345 1.27 scw */
346 1.39 scw user = 1;
347 1.39 scw goto do_trapsignal;
348 1.27 scw }
349 1.32 cl } else {
350 1.39 scw map = &l->l_proc->p_vmspace->vm_map;
351 1.32 cl if (l->l_flag & L_SA) {
352 1.39 scw KDASSERT(l->l_proc->p_sa != NULL);
353 1.39 scw l->l_proc->p_sa->sa_vp_faultaddr = (vaddr_t)far;
354 1.32 cl l->l_flag |= L_SA_PAGEFAULT;
355 1.32 cl }
356 1.32 cl }
357 1.1 chris
358 1.27 scw /*
359 1.27 scw * We need to know whether the page should be mapped
360 1.27 scw * as R or R/W. The MMU does not give us the info as
361 1.27 scw * to whether the fault was caused by a read or a write.
362 1.39 scw *
363 1.39 scw * However, we know that a permission fault can only be
364 1.39 scw * the result of a write to a read-only location, so
365 1.39 scw * we can deal with those quickly.
366 1.39 scw *
367 1.39 scw * Otherwise we need to disassemble the instruction
368 1.39 scw * responsible to determine if it was a write.
369 1.27 scw */
370 1.39 scw if (IS_PERMISSION_FAULT(fsr))
371 1.27 scw ftype = VM_PROT_WRITE;
372 1.39 scw else {
373 1.39 scw u_int insn = ReadWord(tf->tf_pc);
374 1.39 scw
375 1.39 scw if (((insn & 0x0c100000) == 0x04000000) || /* STR/STRB */
376 1.39 scw ((insn & 0x0e1000b0) == 0x000000b0) || /* STRH/STRD */
377 1.39 scw ((insn & 0x0a100000) == 0x08000000)) /* STM/CDT */
378 1.39 scw ftype = VM_PROT_WRITE;
379 1.39 scw else
380 1.39 scw if ((insn & 0x0fb00ff0) == 0x01000090) /* SWP */
381 1.39 scw ftype = VM_PROT_READ | VM_PROT_WRITE;
382 1.39 scw else
383 1.39 scw ftype = VM_PROT_READ;
384 1.39 scw }
385 1.39 scw
386 1.39 scw /*
387 1.39 scw * See if the fault is as a result of ref/mod emulation,
388 1.39 scw * or domain mismatch.
389 1.39 scw */
390 1.39 scw #ifdef DEBUG
391 1.39 scw last_fault_code = fsr;
392 1.1 chris #endif
393 1.29 scw if (pmap_fault_fixup(map->pmap, va, ftype, user))
394 1.27 scw goto out;
395 1.1 chris
396 1.39 scw #ifdef DIAGNOSTIC
397 1.39 scw if (__predict_false(current_intr_depth > 0)) {
398 1.39 scw printf("\nNon-emulated page fault with intr_depth > 0\n");
399 1.39 scw dab_fatal(tf, fsr, far, l, NULL);
400 1.27 scw }
401 1.39 scw #endif
402 1.1 chris
403 1.27 scw onfault = pcb->pcb_onfault;
404 1.27 scw pcb->pcb_onfault = NULL;
405 1.39 scw error = uvm_fault(map, va, 0, ftype);
406 1.27 scw pcb->pcb_onfault = onfault;
407 1.39 scw
408 1.32 cl if (map != kernel_map)
409 1.32 cl l->l_flag &= ~L_SA_PAGEFAULT;
410 1.39 scw
411 1.39 scw if (__predict_true(error == 0)) {
412 1.39 scw if (user)
413 1.39 scw uvm_grow(l->l_proc, va); /* Record any stack growth */
414 1.27 scw goto out;
415 1.27 scw }
416 1.39 scw
417 1.27 scw if (user == 0) {
418 1.27 scw if (pcb->pcb_onfault) {
419 1.39 scw tf->tf_r0 = error;
420 1.39 scw tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
421 1.39 scw return;
422 1.1 chris }
423 1.39 scw
424 1.39 scw printf("\nuvm_fault(%p, %lx, %x, 0) -> %x\n", map, va, ftype,
425 1.39 scw error);
426 1.39 scw dab_fatal(tf, fsr, far, l, NULL);
427 1.27 scw }
428 1.1 chris
429 1.39 scw if (error == ENOMEM) {
430 1.39 scw printf("UVM: pid %d (%s), uid %d killed: "
431 1.39 scw "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
432 1.39 scw (l->l_proc->p_cred && l->l_proc->p_ucred) ?
433 1.39 scw l->l_proc->p_ucred->cr_uid : -1);
434 1.39 scw }
435 1.34 matt
436 1.35 thorpej KSI_INIT_TRAP(&ksi);
437 1.34 matt ksi.ksi_signo = SIGSEGV;
438 1.39 scw ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR;
439 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
440 1.39 scw ksi.ksi_trap = fsr;
441 1.39 scw ksi.ksi_errno = error;
442 1.39 scw
443 1.39 scw do_trapsignal:
444 1.39 scw call_trapsignal(l, &ksi);
445 1.39 scw out:
446 1.39 scw /* If returning to user mode, make sure to invoke userret() */
447 1.39 scw if (user)
448 1.39 scw userret(l);
449 1.39 scw }
450 1.39 scw
451 1.39 scw /*
452 1.39 scw * dab_fatal() handles the following data aborts:
453 1.39 scw *
454 1.39 scw * FAULT_WRTBUF_0 - Vector Exception
455 1.39 scw * FAULT_WRTBUF_1 - Terminal Exception
456 1.39 scw *
457 1.39 scw * We should never see these on a properly functioning system.
458 1.39 scw *
459 1.39 scw * This function is also called by the other handlers if they
460 1.39 scw * detect a fatal problem.
461 1.39 scw *
462 1.39 scw * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
463 1.39 scw */
464 1.39 scw static int
465 1.39 scw dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
466 1.39 scw {
467 1.39 scw const char *mode;
468 1.39 scw
469 1.39 scw mode = TRAP_USERMODE(tf) ? "user" : "kernel";
470 1.39 scw
471 1.39 scw if (l != NULL) {
472 1.39 scw printf("Fatal %s mode data abort: '%s'\n", mode,
473 1.39 scw data_aborts[fsr & FAULT_TYPE_MASK].desc);
474 1.39 scw printf("\ttrapframe: %p, PC: 0x%08x, ", tf, tf->tf_pc);
475 1.39 scw if ((fsr & FAULT_IMPRECISE) == 0)
476 1.39 scw printf("FSR: 0x%x, FAR: 0x%08x\n", fsr, far);
477 1.39 scw else
478 1.39 scw printf("Imprecise fault. FSR/FAR invalid\n");
479 1.39 scw } else {
480 1.39 scw printf("Fatal %s mode prefetch abort\n", mode);
481 1.39 scw printf("\ttrapframe: %p, PC: 0x%08x\n", tf, tf->tf_pc);
482 1.34 matt }
483 1.34 matt
484 1.39 scw #if defined(DDB) || defined(KGDB)
485 1.39 scw kdb_trap(T_FAULT, tf);
486 1.34 matt #endif
487 1.39 scw panic("Fatal abort");
488 1.39 scw /*NOTREACHED*/
489 1.39 scw }
490 1.39 scw
491 1.39 scw /*
492 1.39 scw * dab_align() handles the following data aborts:
493 1.39 scw *
494 1.39 scw * FAULT_ALIGN_0 - Alignment fault
495 1.39 scw * FAULT_ALIGN_0 - Alignment fault
496 1.39 scw *
497 1.39 scw * These faults are fatal if they happen in kernel mode. Otherwise, we
498 1.39 scw * deliver a bus error to the process.
499 1.39 scw */
500 1.39 scw static int
501 1.39 scw dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
502 1.39 scw {
503 1.39 scw
504 1.39 scw /* Alignment faults are always fatal if they occur in kernel mode */
505 1.39 scw if (!TRAP_USERMODE(tf))
506 1.39 scw dab_fatal(tf, fsr, far, l, NULL);
507 1.39 scw
508 1.39 scw /* pcb_onfault *must* be NULL at this point */
509 1.39 scw KDASSERT(l->l_addr->u_pcb.pcb_onfault == NULL);
510 1.39 scw
511 1.39 scw /* See if the cpu state needs to be fixed up */
512 1.39 scw (void) data_abort_fixup(tf, fsr, far, l);
513 1.39 scw
514 1.39 scw /* Deliver a bus error signal to the process */
515 1.39 scw KSI_INIT_TRAP(ksi);
516 1.39 scw ksi->ksi_signo = SIGBUS;
517 1.39 scw ksi->ksi_code = BUS_ADRALN;
518 1.39 scw ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
519 1.39 scw ksi->ksi_trap = fsr;
520 1.39 scw
521 1.39 scw l->l_addr->u_pcb.pcb_tf = tf;
522 1.39 scw
523 1.39 scw return (1);
524 1.39 scw }
525 1.39 scw
526 1.39 scw /*
527 1.39 scw * dab_buserr() handles the following data aborts:
528 1.39 scw *
529 1.39 scw * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
530 1.39 scw * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
531 1.39 scw * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
532 1.39 scw * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
533 1.39 scw * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
534 1.39 scw * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
535 1.39 scw *
536 1.39 scw * If pcb_onfault is set, flag the fault and return to the handler.
537 1.39 scw * If the fault occurred in user mode, give the process a SIGBUS.
538 1.39 scw *
539 1.39 scw * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
540 1.39 scw * can be flagged as imprecise in the FSR. This causes a real headache
541 1.39 scw * since some of the machine state is lost. In this case, tf->tf_pc
542 1.39 scw * may not actually point to the offending instruction. In fact, if
543 1.39 scw * we've taken a double abort fault, it generally points somewhere near
544 1.39 scw * the top of "data_abort_entry" in exception.S.
545 1.39 scw *
546 1.39 scw * In all other cases, these data aborts are considered fatal.
547 1.39 scw */
548 1.39 scw static int
549 1.39 scw dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
550 1.39 scw ksiginfo_t *ksi)
551 1.39 scw {
552 1.39 scw struct pcb *pcb = &l->l_addr->u_pcb;
553 1.39 scw
554 1.39 scw #ifdef __XSCALE__
555 1.39 scw if ((fsr & FAULT_IMPRECISE) != 0 &&
556 1.39 scw (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
557 1.39 scw /*
558 1.39 scw * Oops, an imprecise, double abort fault. We've lost the
559 1.39 scw * r14_abt/spsr_abt values corresponding to the original
560 1.39 scw * abort, and the spsr saved in the trapframe indicates
561 1.39 scw * ABT mode.
562 1.39 scw */
563 1.39 scw tf->tf_spsr &= ~PSR_MODE;
564 1.39 scw
565 1.39 scw /*
566 1.39 scw * We use a simple heuristic to determine if the double abort
567 1.39 scw * happened as a result of a kernel or user mode access.
568 1.39 scw * If the current trapframe is at the top of the kernel stack,
569 1.39 scw * the fault _must_ have come from user mode.
570 1.39 scw */
571 1.39 scw if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) {
572 1.39 scw /*
573 1.39 scw * Kernel mode. We're either about to die a
574 1.39 scw * spectacular death, or pcb_onfault will come
575 1.39 scw * to our rescue. Either way, the current value
576 1.39 scw * of tf->tf_pc is irrelevant.
577 1.39 scw */
578 1.39 scw tf->tf_spsr |= PSR_SVC32_MODE;
579 1.39 scw if (pcb->pcb_onfault == NULL)
580 1.39 scw printf("\nKernel mode double abort!\n");
581 1.39 scw } else {
582 1.39 scw /*
583 1.39 scw * User mode. We've lost the program counter at the
584 1.39 scw * time of the fault (not that it was accurate anyway;
585 1.39 scw * it's not called an imprecise fault for nothing).
586 1.39 scw * About all we can do is copy r14_usr to tf_pc and
587 1.39 scw * hope for the best. The process is about to get a
588 1.39 scw * SIGBUS, so it's probably history anyway.
589 1.39 scw */
590 1.39 scw tf->tf_spsr |= PSR_USR32_MODE;
591 1.39 scw tf->tf_pc = tf->tf_usr_lr;
592 1.39 scw }
593 1.39 scw }
594 1.39 scw
595 1.39 scw /* FAR is invalid for imprecise exceptions */
596 1.39 scw if ((fsr & FAULT_IMPRECISE) != 0)
597 1.39 scw far = 0;
598 1.39 scw #endif /* __XSCALE__ */
599 1.39 scw
600 1.39 scw if (pcb->pcb_onfault) {
601 1.39 scw KDASSERT(TRAP_USERMODE(tf) == 0);
602 1.39 scw tf->tf_r0 = EFAULT;
603 1.39 scw tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
604 1.39 scw return (0);
605 1.39 scw }
606 1.39 scw
607 1.39 scw /* See if the cpu state needs to be fixed up */
608 1.39 scw (void) data_abort_fixup(tf, fsr, far, l);
609 1.39 scw
610 1.39 scw /*
611 1.39 scw * At this point, if the fault happened in kernel mode, we're toast
612 1.39 scw */
613 1.39 scw if (!TRAP_USERMODE(tf))
614 1.39 scw dab_fatal(tf, fsr, far, l, NULL);
615 1.39 scw
616 1.39 scw /* Deliver a bus error signal to the process */
617 1.39 scw KSI_INIT_TRAP(ksi);
618 1.39 scw ksi->ksi_signo = SIGBUS;
619 1.39 scw ksi->ksi_code = BUS_ADRERR;
620 1.39 scw ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
621 1.39 scw ksi->ksi_trap = fsr;
622 1.39 scw
623 1.39 scw l->l_addr->u_pcb.pcb_tf = tf;
624 1.27 scw
625 1.39 scw return (1);
626 1.1 chris }
627 1.1 chris
628 1.39 scw static __inline int
629 1.39 scw prefetch_abort_fixup(trapframe_t *tf)
630 1.39 scw {
631 1.39 scw #ifdef CPU_ABORT_FIXUP_REQUIRED
632 1.39 scw int error;
633 1.39 scw
634 1.39 scw /* Call the cpu specific prefetch abort fixup routine */
635 1.39 scw error = cpu_prefetchabt_fixup(tf);
636 1.39 scw if (__predict_true(error != ABORT_FIXUP_FAILED))
637 1.39 scw return (error);
638 1.39 scw
639 1.39 scw /*
640 1.39 scw * Oops, couldn't fix up the instruction
641 1.39 scw */
642 1.39 scw printf(
643 1.39 scw "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
644 1.39 scw TRAP_USERMODE(tf) ? "user" : "kernel");
645 1.39 scw printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
646 1.39 scw *((u_int *)tf->tf_pc));
647 1.39 scw disassemble(tf->tf_pc);
648 1.39 scw
649 1.39 scw /* Die now if this happened in kernel mode */
650 1.39 scw if (!TRAP_USERMODE(tf))
651 1.39 scw dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
652 1.39 scw
653 1.39 scw return (error);
654 1.39 scw #else
655 1.39 scw return (ABORT_FIXUP_OK);
656 1.39 scw #endif /* CPU_ABORT_FIXUP_REQUIRED */
657 1.39 scw }
658 1.1 chris
659 1.1 chris /*
660 1.39 scw * void prefetch_abort_handler(trapframe_t *tf)
661 1.1 chris *
662 1.1 chris * Abort handler called when instruction execution occurs at
663 1.1 chris * a non existent or restricted (access permissions) memory page.
664 1.1 chris * If the address is invalid and we were in SVC mode then panic as
665 1.1 chris * the kernel should never prefetch abort.
666 1.1 chris * If the address is invalid and the page is mapped then the user process
667 1.1 chris * does no have read permission so send it a signal.
668 1.1 chris * Otherwise fault the page in and try again.
669 1.1 chris */
670 1.1 chris void
671 1.39 scw prefetch_abort_handler(trapframe_t *tf)
672 1.1 chris {
673 1.26 thorpej struct lwp *l;
674 1.14 thorpej struct vm_map *map;
675 1.14 thorpej vaddr_t fault_pc, va;
676 1.39 scw ksiginfo_t ksi;
677 1.1 chris int error;
678 1.39 scw
679 1.39 scw /* Update vmmeter statistics */
680 1.39 scw uvmexp.traps++;
681 1.1 chris
682 1.1 chris /*
683 1.1 chris * Enable IRQ's (disabled by the abort) This always comes
684 1.1 chris * from user mode so we know interrupts were not disabled.
685 1.1 chris * But we check anyway.
686 1.1 chris */
687 1.39 scw if (__predict_false((tf->tf_spsr & I32_bit) == 0))
688 1.1 chris enable_interrupts(I32_bit);
689 1.1 chris
690 1.39 scw /* See if the cpu state needs to be fixed up */
691 1.39 scw switch (prefetch_abort_fixup(tf)) {
692 1.39 scw case ABORT_FIXUP_RETURN:
693 1.1 chris return;
694 1.39 scw case ABORT_FIXUP_FAILED:
695 1.39 scw /* Deliver a SIGILL to the process */
696 1.39 scw KSI_INIT_TRAP(&ksi);
697 1.39 scw ksi.ksi_signo = SIGILL;
698 1.39 scw ksi.ksi_code = ILL_ILLOPC;
699 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc;
700 1.39 scw l = curlwp;
701 1.39 scw l->l_addr->u_pcb.pcb_tf = tf;
702 1.39 scw goto do_trapsignal;
703 1.39 scw default:
704 1.39 scw break;
705 1.1 chris }
706 1.1 chris
707 1.39 scw /* Prefetch aborts cannot happen in kernel mode */
708 1.39 scw if (__predict_false(!TRAP_USERMODE(tf)))
709 1.39 scw dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
710 1.1 chris
711 1.4 thorpej /* Get fault address */
712 1.39 scw fault_pc = tf->tf_pc;
713 1.39 scw l = curlwp;
714 1.39 scw l->l_addr->u_pcb.pcb_tf = tf;
715 1.14 thorpej
716 1.1 chris /* Ok validate the address, can only execute in USER space */
717 1.39 scw if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
718 1.39 scw (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
719 1.35 thorpej KSI_INIT_TRAP(&ksi);
720 1.34 matt ksi.ksi_signo = SIGSEGV;
721 1.34 matt ksi.ksi_code = SEGV_ACCERR;
722 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
723 1.34 matt ksi.ksi_trap = fault_pc;
724 1.39 scw goto do_trapsignal;
725 1.39 scw }
726 1.34 matt
727 1.39 scw map = &l->l_proc->p_vmspace->vm_map;
728 1.39 scw va = trunc_page(fault_pc);
729 1.1 chris
730 1.27 scw /*
731 1.27 scw * See if the pmap can handle this fault on its own...
732 1.27 scw */
733 1.39 scw #ifdef DEBUG
734 1.39 scw last_fault_code = -1;
735 1.39 scw #endif
736 1.29 scw if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
737 1.39 scw goto out;
738 1.27 scw
739 1.39 scw #ifdef DIAGNOSTIC
740 1.39 scw if (__predict_false(current_intr_depth > 0)) {
741 1.39 scw printf("\nNon-emulated prefetch abort with intr_depth > 0\n");
742 1.39 scw dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
743 1.39 scw }
744 1.1 chris #endif
745 1.39 scw
746 1.39 scw error = uvm_fault(map, va, 0, VM_PROT_READ);
747 1.39 scw if (__predict_true(error == 0))
748 1.39 scw goto out;
749 1.39 scw
750 1.39 scw if (error == ENOMEM) {
751 1.39 scw printf("UVM: pid %d (%s), uid %d killed: "
752 1.39 scw "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
753 1.39 scw (l->l_proc->p_cred && l->l_proc->p_ucred) ?
754 1.39 scw l->l_proc->p_ucred->cr_uid : -1);
755 1.1 chris }
756 1.1 chris
757 1.35 thorpej KSI_INIT_TRAP(&ksi);
758 1.34 matt ksi.ksi_signo = SIGSEGV;
759 1.39 scw ksi.ksi_code = SEGV_MAPERR;
760 1.39 scw ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
761 1.39 scw ksi.ksi_trap = fault_pc;
762 1.34 matt ksi.ksi_errno = error;
763 1.39 scw
764 1.39 scw do_trapsignal:
765 1.39 scw call_trapsignal(l, &ksi);
766 1.39 scw
767 1.39 scw out:
768 1.39 scw userret(l);
769 1.39 scw }
770 1.39 scw
771 1.39 scw /*
772 1.39 scw * Tentatively read an 8, 16, or 32-bit value from 'addr'.
773 1.39 scw * If the read succeeds, the value is written to 'rptr' and zero is returned.
774 1.39 scw * Else, return EFAULT.
775 1.39 scw */
776 1.39 scw int
777 1.39 scw badaddr_read(void *addr, size_t size, void *rptr)
778 1.39 scw {
779 1.39 scw extern int badaddr_read_1(const uint8_t *, uint8_t *);
780 1.39 scw extern int badaddr_read_2(const uint16_t *, uint16_t *);
781 1.39 scw extern int badaddr_read_4(const uint32_t *, uint32_t *);
782 1.39 scw union {
783 1.39 scw uint8_t v1;
784 1.39 scw uint16_t v2;
785 1.39 scw uint32_t v4;
786 1.39 scw } u;
787 1.39 scw int rv;
788 1.39 scw
789 1.39 scw cpu_drain_writebuf();
790 1.39 scw
791 1.39 scw /* Read from the test address. */
792 1.39 scw switch (size) {
793 1.39 scw case sizeof(uint8_t):
794 1.39 scw rv = badaddr_read_1(addr, &u.v1);
795 1.39 scw if (rv == 0 && rptr)
796 1.39 scw *(uint8_t *) rptr = u.v1;
797 1.39 scw break;
798 1.39 scw
799 1.39 scw case sizeof(uint16_t):
800 1.39 scw rv = badaddr_read_2(addr, &u.v2);
801 1.39 scw if (rv == 0 && rptr)
802 1.39 scw *(uint16_t *) rptr = u.v2;
803 1.39 scw break;
804 1.39 scw
805 1.39 scw case sizeof(uint32_t):
806 1.39 scw rv = badaddr_read_4(addr, &u.v4);
807 1.39 scw if (rv == 0 && rptr)
808 1.39 scw *(uint32_t *) rptr = u.v4;
809 1.39 scw break;
810 1.39 scw
811 1.39 scw default:
812 1.39 scw panic("badaddr: invalid size (%lu)", (u_long) size);
813 1.34 matt }
814 1.39 scw
815 1.39 scw /* Return EFAULT if the address was invalid, else zero */
816 1.39 scw return (rv);
817 1.1 chris }
818