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