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