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