]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/trap.c
For data and instruction prefetch aborts, call the same handler in the C
[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
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 (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK,
354             NULL, "Kernel page fault") != 0)
355                 goto fatal_pagefault;
356
357         if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
358             user)) {
359                 goto out;
360         }
361
362         onfault = pcb->pcb_onfault;
363         pcb->pcb_onfault = NULL;
364         if (map != kernel_map) {
365                 PROC_LOCK(p);
366                 p->p_lock++;
367                 PROC_UNLOCK(p);
368         }
369         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
370         pcb->pcb_onfault = onfault;
371
372         if (map != kernel_map) {
373                 PROC_LOCK(p);
374                 p->p_lock--;
375                 PROC_UNLOCK(p);
376         }
377         if (__predict_true(error == 0))
378                 goto out;
379 fatal_pagefault:
380         if (user == 0) {
381                 if (pcb->pcb_onfault) {
382                         tf->tf_r0 = error;
383                         tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
384                         return;
385                 }
386
387                 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
388                     error);
389                 dab_fatal(tf, fsr, far, td, &ksig);
390         }
391
392
393         if (error == ENOMEM) {
394                 printf("VM: pid %d (%s), uid %d killed: "
395                     "out of swap\n", td->td_proc->p_pid, td->td_name,
396                     (td->td_proc->p_ucred) ?
397                      td->td_proc->p_ucred->cr_uid : -1);
398                 ksig.signb = SIGKILL;
399         } else {
400                 ksig.signb = SIGSEGV;
401         }
402         ksig.code = 0;
403 do_trapsignal:
404         call_trapsignal(td, ksig.signb, ksig.code);
405 out:
406         /* If returning to user mode, make sure to invoke userret() */
407         if (user)
408                 userret(td, tf);
409 }
410
411 /*
412  * dab_fatal() handles the following data aborts:
413  *
414  *  FAULT_WRTBUF_0 - Vector Exception
415  *  FAULT_WRTBUF_1 - Terminal Exception
416  *
417  * We should never see these on a properly functioning system.
418  *
419  * This function is also called by the other handlers if they
420  * detect a fatal problem.
421  *
422  * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
423  */
424 static int
425 dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
426     struct ksig *ksig)
427 {
428         const char *mode;
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_ucred != td->td_proc->p_ucred)
638                         cred_update_thread(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         if (map != kernel_map) {
675                 PROC_LOCK(p);
676                 p->p_lock++;
677                 PROC_UNLOCK(p);
678         }
679
680         error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
681             VM_FAULT_NORMAL);
682         if (map != kernel_map) {
683                 PROC_LOCK(p);
684                 p->p_lock--;
685                 PROC_UNLOCK(p);
686         }
687
688         if (__predict_true(error == 0))
689                 goto out;
690
691         if (error == ENOMEM) {
692                 printf("VM: pid %d (%s), uid %d killed: "
693                     "out of swap\n", td->td_proc->p_pid, td->td_name,
694                     (td->td_proc->p_ucred) ?
695                      td->td_proc->p_ucred->cr_uid : -1);
696                 ksig.signb = SIGKILL;
697         } else {
698                 ksig.signb = SIGSEGV;
699         }
700         ksig.code = 0;
701
702 do_trapsignal:
703         call_trapsignal(td, ksig.signb, ksig.code);
704
705 out:
706         userret(td, tf);
707
708 }
709
710 extern int badaddr_read_1(const uint8_t *, uint8_t *);
711 extern int badaddr_read_2(const uint16_t *, uint16_t *);
712 extern int badaddr_read_4(const uint32_t *, uint32_t *);
713 /*
714  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
715  * If the read succeeds, the value is written to 'rptr' and zero is returned.
716  * Else, return EFAULT.
717  */
718 int
719 badaddr_read(void *addr, size_t size, void *rptr)
720 {
721         union {
722                 uint8_t v1;
723                 uint16_t v2;
724                 uint32_t v4;
725         } u;
726         int rv;
727
728         cpu_drain_writebuf();
729
730         /* Read from the test address. */
731         switch (size) {
732         case sizeof(uint8_t):
733                 rv = badaddr_read_1(addr, &u.v1);
734                 if (rv == 0 && rptr)
735                         *(uint8_t *) rptr = u.v1;
736                 break;
737
738         case sizeof(uint16_t):
739                 rv = badaddr_read_2(addr, &u.v2);
740                 if (rv == 0 && rptr)
741                         *(uint16_t *) rptr = u.v2;
742                 break;
743
744         case sizeof(uint32_t):
745                 rv = badaddr_read_4(addr, &u.v4);
746                 if (rv == 0 && rptr)
747                         *(uint32_t *) rptr = u.v4;
748                 break;
749
750         default:
751                 panic("badaddr: invalid size (%lu)", (u_long) size);
752         }
753
754         /* Return EFAULT if the address was invalid, else zero */
755         return (rv);
756 }