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