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