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