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