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