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