]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/trap.c
Merge bmake 20151020
[FreeBSD/FreeBSD.git] / sys / arm / arm / trap.c
1 /*      $NetBSD: fault.c,v 1.45 2003/11/20 14:44:36 scw Exp $   */
2
3 /*-
4  * Copyright 2004 Olivier Houchard
5  * Copyright 2003 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Steve C. Woodford for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*-
39  * Copyright (c) 1994-1997 Mark Brinicombe.
40  * Copyright (c) 1994 Brini.
41  * All rights reserved.
42  *
43  * This code is derived from software written for Brini by Mark Brinicombe
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *      This product includes software developed by Brini.
56  * 4. The name of the company nor the name of the author may be used to
57  *    endorse or promote products derived from this software without specific
58  *    prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  * RiscBSD kernel project
73  *
74  * fault.c
75  *
76  * Fault handlers
77  *
78  * Created      : 28/11/94
79  */
80
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/proc.h>
87 #include <sys/lock.h>
88 #include <sys/mutex.h>
89 #include <sys/signalvar.h>
90
91 #include <vm/vm.h>
92 #include <vm/pmap.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_extern.h>
96
97 #include <machine/acle-compat.h>
98 #include <machine/cpu.h>
99 #include <machine/frame.h>
100 #include <machine/machdep.h>
101 #include <machine/pcb.h>
102 #include <machine/vmparam.h>
103
104 #ifdef KDB
105 #include <sys/kdb.h>
106 #endif
107
108 #ifdef KDTRACE_HOOKS
109 #include <sys/dtrace_bsd.h>
110 #endif
111
112 extern char fusubailout[];
113
114 #ifdef DEBUG
115 int last_fault_code;    /* For the benefit of pmap_fault_fixup() */
116 #endif
117
118 struct ksig {
119         int signb;
120         u_long code;
121 };
122 struct data_abort {
123         int (*func)(struct trapframe *, u_int, u_int, struct thread *,
124             struct ksig *);
125         const char *desc;
126 };
127
128 static int dab_fatal(struct trapframe *, u_int, u_int, struct thread *,
129     struct ksig *);
130 static int dab_align(struct trapframe *, u_int, u_int, struct thread *,
131     struct ksig *);
132 static int dab_buserr(struct trapframe *, u_int, u_int, struct thread *,
133     struct ksig *);
134 static void prefetch_abort_handler(struct trapframe *);
135
136 static const struct data_abort data_aborts[] = {
137         {dab_fatal,     "Vector Exception"},
138         {dab_align,     "Alignment Fault 1"},
139         {dab_fatal,     "Terminal Exception"},
140         {dab_align,     "Alignment Fault 3"},
141         {dab_buserr,    "External Linefetch Abort (S)"},
142         {NULL,          "Translation Fault (S)"},
143 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
144         {NULL,          "Translation Flag Fault"},
145 #else
146         {dab_buserr,    "External Linefetch Abort (P)"},
147 #endif
148         {NULL,          "Translation Fault (P)"},
149         {dab_buserr,    "External Non-Linefetch Abort (S)"},
150         {NULL,          "Domain Fault (S)"},
151         {dab_buserr,    "External Non-Linefetch Abort (P)"},
152         {NULL,          "Domain Fault (P)"},
153         {dab_buserr,    "External Translation Abort (L1)"},
154         {NULL,          "Permission Fault (S)"},
155         {dab_buserr,    "External Translation Abort (L2)"},
156         {NULL,          "Permission Fault (P)"}
157 };
158
159 /* Determine if a fault came from user mode */
160 #define TRAP_USERMODE(tf)       ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
161
162 /* Determine if 'x' is a permission fault */
163 #define IS_PERMISSION_FAULT(x)                                  \
164         (((1 << ((x) & FAULT_TYPE_MASK)) &                      \
165           ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
166
167 static __inline void
168 call_trapsignal(struct thread *td, int sig, u_long code)
169 {
170         ksiginfo_t ksi;
171
172         ksiginfo_init_trap(&ksi);
173         ksi.ksi_signo = sig;
174         ksi.ksi_code = (int)code;
175         trapsignal(td, &ksi);
176 }
177
178 void
179 abort_handler(struct trapframe *tf, int type)
180 {
181         struct vm_map *map;
182         struct pcb *pcb;
183         struct thread *td;
184         u_int user, far, fsr;
185         vm_prot_t ftype;
186         void *onfault;
187         vm_offset_t va;
188         int error = 0;
189         struct ksig ksig;
190         struct proc *p;
191
192         if (type == 1)
193                 return (prefetch_abort_handler(tf));
194
195         /* Grab FAR/FSR before enabling interrupts */
196         far = cpu_faultaddress();
197         fsr = cpu_faultstatus();
198 #if 0
199         printf("data abort: fault address=%p (from pc=%p lr=%p)\n",
200                (void*)far, (void*)tf->tf_pc, (void*)tf->tf_svc_lr);
201 #endif
202
203         /* Update vmmeter statistics */
204 #if 0
205         vmexp.traps++;
206 #endif
207
208         td = curthread;
209         p = td->td_proc;
210
211         PCPU_INC(cnt.v_trap);
212         /* Data abort came from user mode? */
213         user = TRAP_USERMODE(tf);
214
215         if (user) {
216                 td->td_pticks = 0;
217                 td->td_frame = tf;
218                 if (td->td_cowgen != td->td_proc->p_cowgen)
219                         thread_cow_update(td);
220
221         }
222         /* Grab the current pcb */
223         pcb = td->td_pcb;
224         /* Re-enable interrupts if they were enabled previously */
225         if (td->td_md.md_spinlock_count == 0) {
226                 if (__predict_true(tf->tf_spsr & PSR_I) == 0)
227                         enable_interrupts(PSR_I);
228                 if (__predict_true(tf->tf_spsr & PSR_F) == 0)
229                         enable_interrupts(PSR_F);
230         }
231
232
233         /* Invoke the appropriate handler, if necessary */
234         if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
235                 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
236                     td, &ksig)) {
237                         goto do_trapsignal;
238                 }
239                 goto out;
240         }
241
242         /*
243          * At this point, we're dealing with one of the following data aborts:
244          *
245          *  FAULT_TRANS_S  - Translation -- Section
246          *  FAULT_TRANS_P  - Translation -- Page
247          *  FAULT_DOMAIN_S - Domain -- Section
248          *  FAULT_DOMAIN_P - Domain -- Page
249          *  FAULT_PERM_S   - Permission -- Section
250          *  FAULT_PERM_P   - Permission -- Page
251          *
252          * These are the main virtual memory-related faults signalled by
253          * the MMU.
254          */
255
256         /* fusubailout is used by [fs]uswintr to avoid page faulting */
257         if (__predict_false(pcb->pcb_onfault == fusubailout)) {
258                 tf->tf_r0 = EFAULT;
259                 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
260                 return;
261         }
262
263         /*
264          * Make sure the Program Counter is sane. We could fall foul of
265          * someone executing Thumb code, in which case the PC might not
266          * be word-aligned. This would cause a kernel alignment fault
267          * further down if we have to decode the current instruction.
268          * XXX: It would be nice to be able to support Thumb at some point.
269          */
270         if (__predict_false((tf->tf_pc & 3) != 0)) {
271                 if (user) {
272                         /*
273                          * Give the user an illegal instruction signal.
274                          */
275                         /* Deliver a SIGILL to the process */
276                         ksig.signb = SIGILL;
277                         ksig.code = 0;
278                         goto do_trapsignal;
279                 }
280
281                 /*
282                  * The kernel never executes Thumb code.
283                  */
284                 printf("\ndata_abort_fault: Misaligned Kernel-mode "
285                     "Program Counter\n");
286                 dab_fatal(tf, fsr, far, td, &ksig);
287         }
288
289         va = trunc_page((vm_offset_t)far);
290
291         /*
292          * It is only a kernel address space fault iff:
293          *      1. user == 0  and
294          *      2. pcb_onfault not set or
295          *      3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
296          */
297         if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
298             (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
299             __predict_true((pcb->pcb_onfault == NULL ||
300              (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
301                 map = kernel_map;
302
303                 /* Was the fault due to the FPE/IPKDB ? */
304                 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
305
306                         /*
307                          * Force exit via userret()
308                          * This is necessary as the FPE is an extension to
309                          * userland that actually runs in a priveledged mode
310                          * but uses USR mode permissions for its accesses.
311                          */
312                         user = 1;
313                         ksig.signb = SIGSEGV;
314                         ksig.code = 0;
315                         goto do_trapsignal;
316                 }
317         } else {
318                 map = &td->td_proc->p_vmspace->vm_map;
319         }
320
321         /*
322          * We need to know whether the page should be mapped as R or R/W.  On
323          * armv6 and later the fault status register indicates whether the
324          * access was a read or write.  Prior to armv6, we know that a
325          * permission fault can only be the result of a write to a read-only
326          * location, so we can deal with those quickly.  Otherwise we need to
327          * disassemble the faulting instruction to determine if it was a write.
328          */
329 #if __ARM_ARCH >= 6
330         ftype = (fsr & FAULT_WNR) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
331 #else
332         if (IS_PERMISSION_FAULT(fsr))
333                 ftype = VM_PROT_WRITE;
334         else {
335                 u_int insn = ReadWord(tf->tf_pc);
336
337                 if (((insn & 0x0c100000) == 0x04000000) ||      /* STR/STRB */
338                     ((insn & 0x0e1000b0) == 0x000000b0) ||      /* STRH/STRD */
339                     ((insn & 0x0a100000) == 0x08000000)) {      /* STM/CDT */
340                         ftype = VM_PROT_WRITE;
341                 } else {
342                         if ((insn & 0x0fb00ff0) == 0x01000090)  /* SWP */
343                                 ftype = VM_PROT_READ | VM_PROT_WRITE;
344                         else
345                                 ftype = VM_PROT_READ;
346                 }
347         }
348 #endif
349
350         /*
351          * See if the fault is as a result of ref/mod emulation,
352          * or domain mismatch.
353          */
354 #ifdef DEBUG
355         last_fault_code = fsr;
356 #endif
357         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK,
358             NULL, "Kernel page fault") != 0)
359                 goto fatal_pagefault;
360
361         if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
362             user)) {
363                 goto out;
364         }
365
366         onfault = pcb->pcb_onfault;
367         pcb->pcb_onfault = NULL;
368         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
369         pcb->pcb_onfault = onfault;
370         if (__predict_true(error == 0))
371                 goto out;
372 fatal_pagefault:
373         if (user == 0) {
374                 if (pcb->pcb_onfault) {
375                         tf->tf_r0 = error;
376                         tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
377                         return;
378                 }
379
380                 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
381                     error);
382                 dab_fatal(tf, fsr, far, td, &ksig);
383         }
384
385
386         if (error == ENOMEM) {
387                 printf("VM: pid %d (%s), uid %d killed: "
388                     "out of swap\n", td->td_proc->p_pid, td->td_name,
389                     (td->td_proc->p_ucred) ?
390                      td->td_proc->p_ucred->cr_uid : -1);
391                 ksig.signb = SIGKILL;
392         } else {
393                 ksig.signb = SIGSEGV;
394         }
395         ksig.code = 0;
396 do_trapsignal:
397         call_trapsignal(td, ksig.signb, ksig.code);
398 out:
399         /* If returning to user mode, make sure to invoke userret() */
400         if (user)
401                 userret(td, tf);
402 }
403
404 /*
405  * dab_fatal() handles the following data aborts:
406  *
407  *  FAULT_WRTBUF_0 - Vector Exception
408  *  FAULT_WRTBUF_1 - Terminal Exception
409  *
410  * We should never see these on a properly functioning system.
411  *
412  * This function is also called by the other handlers if they
413  * detect a fatal problem.
414  *
415  * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
416  */
417 static int
418 dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
419     struct ksig *ksig)
420 {
421         const char *mode;
422
423 #ifdef KDTRACE_HOOKS
424         if (!TRAP_USERMODE(tf)) {
425                 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK))
426                         return (0);
427         }
428 #endif
429
430         mode = TRAP_USERMODE(tf) ? "user" : "kernel";
431
432         disable_interrupts(PSR_I|PSR_F);
433         if (td != NULL) {
434                 printf("Fatal %s mode data abort: '%s'\n", mode,
435                     data_aborts[fsr & FAULT_TYPE_MASK].desc);
436                 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
437                 if ((fsr & FAULT_IMPRECISE) == 0)
438                         printf("%08x, ", far);
439                 else
440                         printf("Invalid,  ");
441                 printf("spsr=%08x\n", tf->tf_spsr);
442         } else {
443                 printf("Fatal %s mode prefetch abort at 0x%08x\n",
444                     mode, tf->tf_pc);
445                 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
446         }
447
448         printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
449             tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
450         printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
451             tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
452         printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
453             tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
454         printf("r12=%08x, ", tf->tf_r12);
455
456         if (TRAP_USERMODE(tf))
457                 printf("usp=%08x, ulr=%08x",
458                     tf->tf_usr_sp, tf->tf_usr_lr);
459         else
460                 printf("ssp=%08x, slr=%08x",
461                     tf->tf_svc_sp, tf->tf_svc_lr);
462         printf(", pc =%08x\n\n", tf->tf_pc);
463
464 #ifdef KDB
465         if (debugger_on_panic || kdb_active)
466                 if (kdb_trap(fsr, 0, tf))
467                         return (0);
468 #endif
469         panic("Fatal abort");
470         /*NOTREACHED*/
471 }
472
473 /*
474  * dab_align() handles the following data aborts:
475  *
476  *  FAULT_ALIGN_0 - Alignment fault
477  *  FAULT_ALIGN_1 - Alignment fault
478  *
479  * These faults are fatal if they happen in kernel mode. Otherwise, we
480  * deliver a bus error to the process.
481  */
482 static int
483 dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
484     struct ksig *ksig)
485 {
486
487         /* Alignment faults are always fatal if they occur in kernel mode */
488         if (!TRAP_USERMODE(tf)) {
489                 if (!td || !td->td_pcb->pcb_onfault)
490                         dab_fatal(tf, fsr, far, td, ksig);
491                 tf->tf_r0 = EFAULT;
492                 tf->tf_pc = (int)td->td_pcb->pcb_onfault;
493                 return (0);
494         }
495
496         /* pcb_onfault *must* be NULL at this point */
497
498         /* Deliver a bus error signal to the process */
499         ksig->code = 0;
500         ksig->signb = SIGBUS;
501         td->td_frame = tf;
502
503         return (1);
504 }
505
506 /*
507  * dab_buserr() handles the following data aborts:
508  *
509  *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
510  *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
511  *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
512  *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
513  *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
514  *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
515  *
516  * If pcb_onfault is set, flag the fault and return to the handler.
517  * If the fault occurred in user mode, give the process a SIGBUS.
518  *
519  * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
520  * can be flagged as imprecise in the FSR. This causes a real headache
521  * since some of the machine state is lost. In this case, tf->tf_pc
522  * may not actually point to the offending instruction. In fact, if
523  * we've taken a double abort fault, it generally points somewhere near
524  * the top of "data_abort_entry" in exception.S.
525  *
526  * In all other cases, these data aborts are considered fatal.
527  */
528 static int
529 dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
530     struct ksig *ksig)
531 {
532         struct pcb *pcb = td->td_pcb;
533
534 #ifdef __XSCALE__
535         if ((fsr & FAULT_IMPRECISE) != 0 &&
536             (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
537                 /*
538                  * Oops, an imprecise, double abort fault. We've lost the
539                  * r14_abt/spsr_abt values corresponding to the original
540                  * abort, and the spsr saved in the trapframe indicates
541                  * ABT mode.
542                  */
543                 tf->tf_spsr &= ~PSR_MODE;
544
545                 /*
546                  * We use a simple heuristic to determine if the double abort
547                  * happened as a result of a kernel or user mode access.
548                  * If the current trapframe is at the top of the kernel stack,
549                  * the fault _must_ have come from user mode.
550                  */
551                 if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) {
552                         /*
553                          * Kernel mode. We're either about to die a
554                          * spectacular death, or pcb_onfault will come
555                          * to our rescue. Either way, the current value
556                          * of tf->tf_pc is irrelevant.
557                          */
558                         tf->tf_spsr |= PSR_SVC32_MODE;
559                         if (pcb->pcb_onfault == NULL)
560                                 printf("\nKernel mode double abort!\n");
561                 } else {
562                         /*
563                          * User mode. We've lost the program counter at the
564                          * time of the fault (not that it was accurate anyway;
565                          * it's not called an imprecise fault for nothing).
566                          * About all we can do is copy r14_usr to tf_pc and
567                          * hope for the best. The process is about to get a
568                          * SIGBUS, so it's probably history anyway.
569                          */
570                         tf->tf_spsr |= PSR_USR32_MODE;
571                         tf->tf_pc = tf->tf_usr_lr;
572                 }
573         }
574
575         /* FAR is invalid for imprecise exceptions */
576         if ((fsr & FAULT_IMPRECISE) != 0)
577                 far = 0;
578 #endif /* __XSCALE__ */
579
580         if (pcb->pcb_onfault) {
581                 tf->tf_r0 = EFAULT;
582                 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
583                 return (0);
584         }
585
586         /*
587          * At this point, if the fault happened in kernel mode, we're toast
588          */
589         if (!TRAP_USERMODE(tf))
590                 dab_fatal(tf, fsr, far, td, ksig);
591
592         /* Deliver a bus error signal to the process */
593         ksig->signb = SIGBUS;
594         ksig->code = 0;
595         td->td_frame = tf;
596
597         return (1);
598 }
599
600 /*
601  * void prefetch_abort_handler(struct trapframe *tf)
602  *
603  * Abort handler called when instruction execution occurs at
604  * a non existent or restricted (access permissions) memory page.
605  * If the address is invalid and we were in SVC mode then panic as
606  * the kernel should never prefetch abort.
607  * If the address is invalid and the page is mapped then the user process
608  * does no have read permission so send it a signal.
609  * Otherwise fault the page in and try again.
610  */
611 static void
612 prefetch_abort_handler(struct trapframe *tf)
613 {
614         struct thread *td;
615         struct proc * p;
616         struct vm_map *map;
617         vm_offset_t fault_pc, va;
618         int error = 0;
619         struct ksig ksig;
620
621
622 #if 0
623         /* Update vmmeter statistics */
624         uvmexp.traps++;
625 #endif
626 #if 0
627         printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
628             (void*)tf->tf_usr_lr);
629 #endif
630
631         td = curthread;
632         p = td->td_proc;
633         PCPU_INC(cnt.v_trap);
634
635         if (TRAP_USERMODE(tf)) {
636                 td->td_frame = tf;
637                 if (td->td_cowgen != td->td_proc->p_cowgen)
638                         thread_cow_update(td);
639         }
640         fault_pc = tf->tf_pc;
641         if (td->td_md.md_spinlock_count == 0) {
642                 if (__predict_true(tf->tf_spsr & PSR_I) == 0)
643                         enable_interrupts(PSR_I);
644                 if (__predict_true(tf->tf_spsr & PSR_F) == 0)
645                         enable_interrupts(PSR_F);
646         }
647
648         /* Prefetch aborts cannot happen in kernel mode */
649         if (__predict_false(!TRAP_USERMODE(tf)))
650                 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig);
651         td->td_pticks = 0;
652
653
654         /* Ok validate the address, can only execute in USER space */
655         if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
656             (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
657                 ksig.signb = SIGSEGV;
658                 ksig.code = 0;
659                 goto do_trapsignal;
660         }
661
662         map = &td->td_proc->p_vmspace->vm_map;
663         va = trunc_page(fault_pc);
664
665         /*
666          * See if the pmap can handle this fault on its own...
667          */
668 #ifdef DEBUG
669         last_fault_code = -1;
670 #endif
671         if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
672                 goto out;
673
674         error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
675             VM_FAULT_NORMAL);
676         if (__predict_true(error == 0))
677                 goto out;
678
679         if (error == ENOMEM) {
680                 printf("VM: pid %d (%s), uid %d killed: "
681                     "out of swap\n", td->td_proc->p_pid, td->td_name,
682                     (td->td_proc->p_ucred) ?
683                      td->td_proc->p_ucred->cr_uid : -1);
684                 ksig.signb = SIGKILL;
685         } else {
686                 ksig.signb = SIGSEGV;
687         }
688         ksig.code = 0;
689
690 do_trapsignal:
691         call_trapsignal(td, ksig.signb, ksig.code);
692
693 out:
694         userret(td, tf);
695
696 }
697
698 extern int badaddr_read_1(const uint8_t *, uint8_t *);
699 extern int badaddr_read_2(const uint16_t *, uint16_t *);
700 extern int badaddr_read_4(const uint32_t *, uint32_t *);
701 /*
702  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
703  * If the read succeeds, the value is written to 'rptr' and zero is returned.
704  * Else, return EFAULT.
705  */
706 int
707 badaddr_read(void *addr, size_t size, void *rptr)
708 {
709         union {
710                 uint8_t v1;
711                 uint16_t v2;
712                 uint32_t v4;
713         } u;
714         int rv;
715
716         cpu_drain_writebuf();
717
718         /* Read from the test address. */
719         switch (size) {
720         case sizeof(uint8_t):
721                 rv = badaddr_read_1(addr, &u.v1);
722                 if (rv == 0 && rptr)
723                         *(uint8_t *) rptr = u.v1;
724                 break;
725
726         case sizeof(uint16_t):
727                 rv = badaddr_read_2(addr, &u.v2);
728                 if (rv == 0 && rptr)
729                         *(uint16_t *) rptr = u.v2;
730                 break;
731
732         case sizeof(uint32_t):
733                 rv = badaddr_read_4(addr, &u.v4);
734                 if (rv == 0 && rptr)
735                         *(uint32_t *) rptr = u.v4;
736                 break;
737
738         default:
739                 panic("badaddr: invalid size (%lu)", (u_long) size);
740         }
741
742         /* Return EFAULT if the address was invalid, else zero */
743         return (rv);
744 }