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