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