]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/trap-v4.c
Merge release 1.14 of bsnmp.
[FreeBSD/FreeBSD.git] / sys / arm / arm / trap-v4.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 #include <sys/vmmeter.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 #include <vm/vm_param.h>
98
99 #include <machine/cpu.h>
100 #include <machine/frame.h>
101 #include <machine/machdep.h>
102 #include <machine/pcb.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 #define ReadWord(a)     (*((volatile unsigned int *)(a)))
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         {dab_buserr,    "External Linefetch Abort (P)"},
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, signo, ucode;
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 = cp15_dfar_get();
193         fsr = cp15_dfsr_get();
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         VM_CNT_INC(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_cowgen != td->td_proc->p_cowgen)
215                         thread_cow_update(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                         signo = ksig.signb;
234                         ucode = ksig.code;
235                         goto do_trapsignal;
236                 }
237                 goto out;
238         }
239
240         /*
241          * At this point, we're dealing with one of the following data aborts:
242          *
243          *  FAULT_TRANS_S  - Translation -- Section
244          *  FAULT_TRANS_P  - Translation -- Page
245          *  FAULT_DOMAIN_S - Domain -- Section
246          *  FAULT_DOMAIN_P - Domain -- Page
247          *  FAULT_PERM_S   - Permission -- Section
248          *  FAULT_PERM_P   - Permission -- Page
249          *
250          * These are the main virtual memory-related faults signalled by
251          * the MMU.
252          */
253
254         /*
255          * Make sure the Program Counter is sane. We could fall foul of
256          * someone executing Thumb code, in which case the PC might not
257          * be word-aligned. This would cause a kernel alignment fault
258          * further down if we have to decode the current instruction.
259          * XXX: It would be nice to be able to support Thumb at some point.
260          */
261         if (__predict_false((tf->tf_pc & 3) != 0)) {
262                 if (user) {
263                         /*
264                          * Give the user an illegal instruction signal.
265                          */
266                         /* Deliver a SIGILL to the process */
267                         signo = SIGILL;
268                         ucode = 0;
269                         goto do_trapsignal;
270                 }
271
272                 /*
273                  * The kernel never executes Thumb code.
274                  */
275                 printf("\ndata_abort_fault: Misaligned Kernel-mode "
276                     "Program Counter\n");
277                 dab_fatal(tf, fsr, far, td, &ksig);
278         }
279
280         va = trunc_page((vm_offset_t)far);
281
282         /*
283          * It is only a kernel address space fault iff:
284          *      1. user == 0  and
285          *      2. pcb_onfault not set or
286          *      3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
287          */
288         if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
289             (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
290             __predict_true((pcb->pcb_onfault == NULL ||
291              (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
292                 map = kernel_map;
293
294                 /* Was the fault due to the FPE/IPKDB ? */
295                 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
296
297                         /*
298                          * Force exit via userret()
299                          * This is necessary as the FPE is an extension to
300                          * userland that actually runs in a priveledged mode
301                          * but uses USR mode permissions for its accesses.
302                          */
303                         user = 1;
304                         signo = SIGSEGV;
305                         ucode = 0;
306                         goto do_trapsignal;
307                 }
308         } else {
309                 map = &td->td_proc->p_vmspace->vm_map;
310         }
311
312         /*
313          * We need to know whether the page should be mapped as R or R/W.
314          * On armv4, the fault status register does not indicate whether
315          * the access was a read or write.  We know that a permission fault
316          * can only be the result of a write to a read-only location, so we
317          * can deal with those quickly.  Otherwise we need to disassemble
318          * the faulting instruction to determine if it was a write.
319          */
320         if (IS_PERMISSION_FAULT(fsr))
321                 ftype = VM_PROT_WRITE;
322         else {
323                 u_int insn = ReadWord(tf->tf_pc);
324
325                 if (((insn & 0x0c100000) == 0x04000000) ||      /* STR/STRB */
326                     ((insn & 0x0e1000b0) == 0x000000b0) ||      /* STRH/STRD */
327                     ((insn & 0x0a100000) == 0x08000000)) {      /* STM/CDT */
328                         ftype = VM_PROT_WRITE;
329                 } else {
330                         if ((insn & 0x0fb00ff0) == 0x01000090)  /* SWP */
331                                 ftype = VM_PROT_READ | VM_PROT_WRITE;
332                         else
333                                 ftype = VM_PROT_READ;
334                 }
335         }
336
337         /*
338          * See if the fault is as a result of ref/mod emulation,
339          * or domain mismatch.
340          */
341 #ifdef DEBUG
342         last_fault_code = fsr;
343 #endif
344         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK,
345             NULL, "Kernel page fault") != 0)
346                 goto fatal_pagefault;
347
348         if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
349             user)) {
350                 goto out;
351         }
352
353         onfault = pcb->pcb_onfault;
354         pcb->pcb_onfault = NULL;
355         error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &signo, &ucode);
356         pcb->pcb_onfault = onfault;
357         if (__predict_true(error == KERN_SUCCESS))
358                 goto out;
359 fatal_pagefault:
360         if (user == 0) {
361                 if (pcb->pcb_onfault) {
362                         tf->tf_r0 = error;
363                         tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
364                         return;
365                 }
366
367                 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
368                     error);
369                 dab_fatal(tf, fsr, far, td, &ksig);
370         }
371
372
373 do_trapsignal:
374         call_trapsignal(td, signo, ucode);
375 out:
376         /* If returning to user mode, make sure to invoke userret() */
377         if (user)
378                 userret(td, tf);
379 }
380
381 /*
382  * dab_fatal() handles the following data aborts:
383  *
384  *  FAULT_WRTBUF_0 - Vector Exception
385  *  FAULT_WRTBUF_1 - Terminal Exception
386  *
387  * We should never see these on a properly functioning system.
388  *
389  * This function is also called by the other handlers if they
390  * detect a fatal problem.
391  *
392  * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
393  */
394 static int
395 dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
396     struct ksig *ksig)
397 {
398         const char *mode;
399 #ifdef KDB
400         bool handled;
401 #endif
402
403 #ifdef KDB
404         if (kdb_active) {
405                 kdb_reenter();
406                 return (0);
407         }
408 #endif
409 #ifdef KDTRACE_HOOKS
410         if (!TRAP_USERMODE(tf)) {
411                 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK))
412                         return (0);
413         }
414 #endif
415
416         mode = TRAP_USERMODE(tf) ? "user" : "kernel";
417
418         disable_interrupts(PSR_I|PSR_F);
419         if (td != NULL) {
420                 printf("Fatal %s mode data abort: '%s'\n", mode,
421                     data_aborts[fsr & FAULT_TYPE_MASK].desc);
422                 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
423                 if ((fsr & FAULT_IMPRECISE) == 0)
424                         printf("%08x, ", far);
425                 else
426                         printf("Invalid,  ");
427                 printf("spsr=%08x\n", tf->tf_spsr);
428         } else {
429                 printf("Fatal %s mode prefetch abort at 0x%08x\n",
430                     mode, tf->tf_pc);
431                 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
432         }
433
434         printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
435             tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
436         printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
437             tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
438         printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
439             tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
440         printf("r12=%08x, ", tf->tf_r12);
441
442         if (TRAP_USERMODE(tf))
443                 printf("usp=%08x, ulr=%08x",
444                     tf->tf_usr_sp, tf->tf_usr_lr);
445         else
446                 printf("ssp=%08x, slr=%08x",
447                     tf->tf_svc_sp, tf->tf_svc_lr);
448         printf(", pc =%08x\n\n", tf->tf_pc);
449
450 #ifdef KDB
451         if (debugger_on_trap) {
452                 kdb_why = KDB_WHY_TRAP;
453                 handled = kdb_trap(fsr, 0, tf);
454                 kdb_why = KDB_WHY_UNSET;
455                 if (handled)
456                         return (0);
457         }
458 #endif
459         panic("Fatal abort");
460         /*NOTREACHED*/
461 }
462
463 /*
464  * dab_align() handles the following data aborts:
465  *
466  *  FAULT_ALIGN_0 - Alignment fault
467  *  FAULT_ALIGN_1 - Alignment fault
468  *
469  * These faults are fatal if they happen in kernel mode. Otherwise, we
470  * deliver a bus error to the process.
471  */
472 static int
473 dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
474     struct ksig *ksig)
475 {
476
477         /* Alignment faults are always fatal if they occur in kernel mode */
478         if (!TRAP_USERMODE(tf)) {
479                 if (!td || !td->td_pcb->pcb_onfault)
480                         dab_fatal(tf, fsr, far, td, ksig);
481                 tf->tf_r0 = EFAULT;
482                 tf->tf_pc = (int)td->td_pcb->pcb_onfault;
483                 return (0);
484         }
485
486         /* pcb_onfault *must* be NULL at this point */
487
488         /* Deliver a bus error signal to the process */
489         ksig->code = 0;
490         ksig->signb = SIGBUS;
491         td->td_frame = tf;
492
493         return (1);
494 }
495
496 /*
497  * dab_buserr() handles the following data aborts:
498  *
499  *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
500  *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
501  *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
502  *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
503  *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
504  *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
505  *
506  * If pcb_onfault is set, flag the fault and return to the handler.
507  * If the fault occurred in user mode, give the process a SIGBUS.
508  *
509  * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
510  * can be flagged as imprecise in the FSR. This causes a real headache
511  * since some of the machine state is lost. In this case, tf->tf_pc
512  * may not actually point to the offending instruction. In fact, if
513  * we've taken a double abort fault, it generally points somewhere near
514  * the top of "data_abort_entry" in exception.S.
515  *
516  * In all other cases, these data aborts are considered fatal.
517  */
518 static int
519 dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
520     struct ksig *ksig)
521 {
522         struct pcb *pcb = td->td_pcb;
523
524 #ifdef __XSCALE__
525         if ((fsr & FAULT_IMPRECISE) != 0 &&
526             (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
527                 /*
528                  * Oops, an imprecise, double abort fault. We've lost the
529                  * r14_abt/spsr_abt values corresponding to the original
530                  * abort, and the spsr saved in the trapframe indicates
531                  * ABT mode.
532                  */
533                 tf->tf_spsr &= ~PSR_MODE;
534
535                 /*
536                  * We use a simple heuristic to determine if the double abort
537                  * happened as a result of a kernel or user mode access.
538                  * If the current trapframe is at the top of the kernel stack,
539                  * the fault _must_ have come from user mode.
540                  */
541                 if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) {
542                         /*
543                          * Kernel mode. We're either about to die a
544                          * spectacular death, or pcb_onfault will come
545                          * to our rescue. Either way, the current value
546                          * of tf->tf_pc is irrelevant.
547                          */
548                         tf->tf_spsr |= PSR_SVC32_MODE;
549                         if (pcb->pcb_onfault == NULL)
550                                 printf("\nKernel mode double abort!\n");
551                 } else {
552                         /*
553                          * User mode. We've lost the program counter at the
554                          * time of the fault (not that it was accurate anyway;
555                          * it's not called an imprecise fault for nothing).
556                          * About all we can do is copy r14_usr to tf_pc and
557                          * hope for the best. The process is about to get a
558                          * SIGBUS, so it's probably history anyway.
559                          */
560                         tf->tf_spsr |= PSR_USR32_MODE;
561                         tf->tf_pc = tf->tf_usr_lr;
562                 }
563         }
564
565         /* FAR is invalid for imprecise exceptions */
566         if ((fsr & FAULT_IMPRECISE) != 0)
567                 far = 0;
568 #endif /* __XSCALE__ */
569
570         if (pcb->pcb_onfault) {
571                 tf->tf_r0 = EFAULT;
572                 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
573                 return (0);
574         }
575
576         /*
577          * At this point, if the fault happened in kernel mode, we're toast
578          */
579         if (!TRAP_USERMODE(tf))
580                 dab_fatal(tf, fsr, far, td, ksig);
581
582         /* Deliver a bus error signal to the process */
583         ksig->signb = SIGBUS;
584         ksig->code = 0;
585         td->td_frame = tf;
586
587         return (1);
588 }
589
590 /*
591  * void prefetch_abort_handler(struct trapframe *tf)
592  *
593  * Abort handler called when instruction execution occurs at
594  * a non existent or restricted (access permissions) memory page.
595  * If the address is invalid and we were in SVC mode then panic as
596  * the kernel should never prefetch abort.
597  * If the address is invalid and the page is mapped then the user process
598  * does no have read permission so send it a signal.
599  * Otherwise fault the page in and try again.
600  */
601 static void
602 prefetch_abort_handler(struct trapframe *tf)
603 {
604         struct thread *td;
605         struct proc * p;
606         struct vm_map *map;
607         vm_offset_t fault_pc, va;
608         int error = 0, signo, ucode;
609         struct ksig ksig;
610
611 #if 0
612         /* Update vmmeter statistics */
613         uvmexp.traps++;
614 #endif
615 #if 0
616         printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
617             (void*)tf->tf_usr_lr);
618 #endif
619
620         td = curthread;
621         p = td->td_proc;
622         VM_CNT_INC(v_trap);
623
624         if (TRAP_USERMODE(tf)) {
625                 td->td_frame = tf;
626                 if (td->td_cowgen != td->td_proc->p_cowgen)
627                         thread_cow_update(td);
628         }
629         fault_pc = tf->tf_pc;
630         if (td->td_md.md_spinlock_count == 0) {
631                 if (__predict_true(tf->tf_spsr & PSR_I) == 0)
632                         enable_interrupts(PSR_I);
633                 if (__predict_true(tf->tf_spsr & PSR_F) == 0)
634                         enable_interrupts(PSR_F);
635         }
636
637         /* Prefetch aborts cannot happen in kernel mode */
638         if (__predict_false(!TRAP_USERMODE(tf)))
639                 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig);
640         td->td_pticks = 0;
641
642
643         /* Ok validate the address, can only execute in USER space */
644         if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
645             (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
646                 signo = SIGSEGV;
647                 ucode = 0;
648                 goto do_trapsignal;
649         }
650
651         map = &td->td_proc->p_vmspace->vm_map;
652         va = trunc_page(fault_pc);
653
654         /*
655          * See if the pmap can handle this fault on its own...
656          */
657 #ifdef DEBUG
658         last_fault_code = -1;
659 #endif
660         if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
661                 goto out;
662
663         error = vm_fault_trap(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
664             VM_FAULT_NORMAL, &signo, &ucode);
665         if (__predict_true(error == KERN_SUCCESS))
666                 goto out;
667
668 do_trapsignal:
669         call_trapsignal(td, signo, ucode);
670
671 out:
672         userret(td, tf);
673
674 }
675
676 extern int badaddr_read_1(const uint8_t *, uint8_t *);
677 extern int badaddr_read_2(const uint16_t *, uint16_t *);
678 extern int badaddr_read_4(const uint32_t *, uint32_t *);
679 /*
680  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
681  * If the read succeeds, the value is written to 'rptr' and zero is returned.
682  * Else, return EFAULT.
683  */
684 int
685 badaddr_read(void *addr, size_t size, void *rptr)
686 {
687         union {
688                 uint8_t v1;
689                 uint16_t v2;
690                 uint32_t v4;
691         } u;
692         int rv;
693
694         cpu_drain_writebuf();
695
696         /* Read from the test address. */
697         switch (size) {
698         case sizeof(uint8_t):
699                 rv = badaddr_read_1(addr, &u.v1);
700                 if (rv == 0 && rptr)
701                         *(uint8_t *) rptr = u.v1;
702                 break;
703
704         case sizeof(uint16_t):
705                 rv = badaddr_read_2(addr, &u.v2);
706                 if (rv == 0 && rptr)
707                         *(uint16_t *) rptr = u.v2;
708                 break;
709
710         case sizeof(uint32_t):
711                 rv = badaddr_read_4(addr, &u.v4);
712                 if (rv == 0 && rptr)
713                         *(uint32_t *) rptr = u.v4;
714                 break;
715
716         default:
717                 panic("badaddr: invalid size (%lu)", (u_long) size);
718         }
719
720         /* Return EFAULT if the address was invalid, else zero */
721         return (rv);
722 }