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