trap-v4.c revision 314526
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#include <sys/cdefs.h>
82__FBSDID("$FreeBSD: stable/11/sys/arm/arm/trap-v4.c 314526 2017-03-01 23:35:40Z ian $");
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/proc.h>
87#include <sys/lock.h>
88#include <sys/mutex.h>
89#include <sys/signalvar.h>
90
91#include <vm/vm.h>
92#include <vm/pmap.h>
93#include <vm/vm_kern.h>
94#include <vm/vm_map.h>
95#include <vm/vm_extern.h>
96
97#include <machine/cpu.h>
98#include <machine/frame.h>
99#include <machine/machdep.h>
100#include <machine/pcb.h>
101#include <machine/vmparam.h>
102
103#ifdef KDB
104#include <sys/kdb.h>
105#endif
106
107#ifdef KDTRACE_HOOKS
108#include <sys/dtrace_bsd.h>
109#endif
110
111#define ReadWord(a)	(*((volatile unsigned int *)(a)))
112
113#ifdef DEBUG
114int last_fault_code;	/* For the benefit of pmap_fault_fixup() */
115#endif
116
117struct ksig {
118	int signb;
119	u_long code;
120};
121struct data_abort {
122	int (*func)(struct trapframe *, u_int, u_int, struct thread *,
123	    struct ksig *);
124	const char *desc;
125};
126
127static int dab_fatal(struct trapframe *, u_int, u_int, struct thread *,
128    struct ksig *);
129static int dab_align(struct trapframe *, u_int, u_int, struct thread *,
130    struct ksig *);
131static int dab_buserr(struct trapframe *, u_int, u_int, struct thread *,
132    struct ksig *);
133static void prefetch_abort_handler(struct trapframe *);
134
135static const struct data_abort data_aborts[] = {
136	{dab_fatal,	"Vector Exception"},
137	{dab_align,	"Alignment Fault 1"},
138	{dab_fatal,	"Terminal Exception"},
139	{dab_align,	"Alignment Fault 3"},
140	{dab_buserr,	"External Linefetch Abort (S)"},
141	{NULL,		"Translation Fault (S)"},
142	{dab_buserr,	"External Linefetch Abort (P)"},
143	{NULL,		"Translation Fault (P)"},
144	{dab_buserr,	"External Non-Linefetch Abort (S)"},
145	{NULL,		"Domain Fault (S)"},
146	{dab_buserr,	"External Non-Linefetch Abort (P)"},
147	{NULL,		"Domain Fault (P)"},
148	{dab_buserr,	"External Translation Abort (L1)"},
149	{NULL,		"Permission Fault (S)"},
150	{dab_buserr,	"External Translation Abort (L2)"},
151	{NULL,		"Permission Fault (P)"}
152};
153
154/* Determine if a fault came from user mode */
155#define	TRAP_USERMODE(tf)	((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
156
157/* Determine if 'x' is a permission fault */
158#define	IS_PERMISSION_FAULT(x)					\
159	(((1 << ((x) & FAULT_TYPE_MASK)) &			\
160	  ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
161
162static __inline void
163call_trapsignal(struct thread *td, int sig, u_long code)
164{
165	ksiginfo_t ksi;
166
167	ksiginfo_init_trap(&ksi);
168	ksi.ksi_signo = sig;
169	ksi.ksi_code = (int)code;
170	trapsignal(td, &ksi);
171}
172
173void
174abort_handler(struct trapframe *tf, int type)
175{
176	struct vm_map *map;
177	struct pcb *pcb;
178	struct thread *td;
179	u_int user, far, fsr;
180	vm_prot_t ftype;
181	void *onfault;
182	vm_offset_t va;
183	int error = 0;
184	struct ksig ksig;
185	struct proc *p;
186
187	if (type == 1)
188		return (prefetch_abort_handler(tf));
189
190	/* Grab FAR/FSR before enabling interrupts */
191	far = cpu_faultaddress();
192	fsr = cpu_faultstatus();
193#if 0
194	printf("data abort: fault address=%p (from pc=%p lr=%p)\n",
195	       (void*)far, (void*)tf->tf_pc, (void*)tf->tf_svc_lr);
196#endif
197
198	/* Update vmmeter statistics */
199#if 0
200	vmexp.traps++;
201#endif
202
203	td = curthread;
204	p = td->td_proc;
205
206	PCPU_INC(cnt.v_trap);
207	/* Data abort came from user mode? */
208	user = TRAP_USERMODE(tf);
209
210	if (user) {
211		td->td_pticks = 0;
212		td->td_frame = tf;
213		if (td->td_cowgen != td->td_proc->p_cowgen)
214			thread_cow_update(td);
215
216	}
217	/* Grab the current pcb */
218	pcb = td->td_pcb;
219	/* Re-enable interrupts if they were enabled previously */
220	if (td->td_md.md_spinlock_count == 0) {
221		if (__predict_true(tf->tf_spsr & PSR_I) == 0)
222			enable_interrupts(PSR_I);
223		if (__predict_true(tf->tf_spsr & PSR_F) == 0)
224			enable_interrupts(PSR_F);
225	}
226
227
228	/* Invoke the appropriate handler, if necessary */
229	if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
230		if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
231		    td, &ksig)) {
232			goto do_trapsignal;
233		}
234		goto out;
235	}
236
237	/*
238	 * At this point, we're dealing with one of the following data aborts:
239	 *
240	 *  FAULT_TRANS_S  - Translation -- Section
241	 *  FAULT_TRANS_P  - Translation -- Page
242	 *  FAULT_DOMAIN_S - Domain -- Section
243	 *  FAULT_DOMAIN_P - Domain -- Page
244	 *  FAULT_PERM_S   - Permission -- Section
245	 *  FAULT_PERM_P   - Permission -- Page
246	 *
247	 * These are the main virtual memory-related faults signalled by
248	 * the MMU.
249	 */
250
251	/*
252	 * Make sure the Program Counter is sane. We could fall foul of
253	 * someone executing Thumb code, in which case the PC might not
254	 * be word-aligned. This would cause a kernel alignment fault
255	 * further down if we have to decode the current instruction.
256	 * XXX: It would be nice to be able to support Thumb at some point.
257	 */
258	if (__predict_false((tf->tf_pc & 3) != 0)) {
259		if (user) {
260			/*
261			 * Give the user an illegal instruction signal.
262			 */
263			/* Deliver a SIGILL to the process */
264			ksig.signb = SIGILL;
265			ksig.code = 0;
266			goto do_trapsignal;
267		}
268
269		/*
270		 * The kernel never executes Thumb code.
271		 */
272		printf("\ndata_abort_fault: Misaligned Kernel-mode "
273		    "Program Counter\n");
274		dab_fatal(tf, fsr, far, td, &ksig);
275	}
276
277	va = trunc_page((vm_offset_t)far);
278
279	/*
280	 * It is only a kernel address space fault iff:
281	 *	1. user == 0  and
282	 *	2. pcb_onfault not set or
283	 *	3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
284	 */
285	if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
286	    (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
287	    __predict_true((pcb->pcb_onfault == NULL ||
288	     (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
289		map = kernel_map;
290
291		/* Was the fault due to the FPE/IPKDB ? */
292		if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
293
294			/*
295			 * Force exit via userret()
296			 * This is necessary as the FPE is an extension to
297			 * userland that actually runs in a priveledged mode
298			 * but uses USR mode permissions for its accesses.
299			 */
300			user = 1;
301			ksig.signb = SIGSEGV;
302			ksig.code = 0;
303			goto do_trapsignal;
304		}
305	} else {
306		map = &td->td_proc->p_vmspace->vm_map;
307	}
308
309	/*
310	 * We need to know whether the page should be mapped as R or R/W.
311	 * On armv4, the fault status register does not indicate whether
312	 * the access was a read or write.  We know that a permission fault
313	 * can only be the result of a write to a read-only location, so we
314	 * can deal with those quickly.  Otherwise we need to disassemble
315	 * the faulting instruction to determine if it was a write.
316	 */
317	if (IS_PERMISSION_FAULT(fsr))
318		ftype = VM_PROT_WRITE;
319	else {
320		u_int insn = ReadWord(tf->tf_pc);
321
322		if (((insn & 0x0c100000) == 0x04000000) ||	/* STR/STRB */
323		    ((insn & 0x0e1000b0) == 0x000000b0) ||	/* STRH/STRD */
324		    ((insn & 0x0a100000) == 0x08000000)) {	/* STM/CDT */
325			ftype = VM_PROT_WRITE;
326		} else {
327			if ((insn & 0x0fb00ff0) == 0x01000090)	/* SWP */
328				ftype = VM_PROT_READ | VM_PROT_WRITE;
329			else
330				ftype = VM_PROT_READ;
331		}
332	}
333
334	/*
335	 * See if the fault is as a result of ref/mod emulation,
336	 * or domain mismatch.
337	 */
338#ifdef DEBUG
339	last_fault_code = fsr;
340#endif
341	if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK,
342	    NULL, "Kernel page fault") != 0)
343		goto fatal_pagefault;
344
345	if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
346	    user)) {
347		goto out;
348	}
349
350	onfault = pcb->pcb_onfault;
351	pcb->pcb_onfault = NULL;
352	error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
353	pcb->pcb_onfault = onfault;
354	if (__predict_true(error == 0))
355		goto out;
356fatal_pagefault:
357	if (user == 0) {
358		if (pcb->pcb_onfault) {
359			tf->tf_r0 = error;
360			tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
361			return;
362		}
363
364		printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
365		    error);
366		dab_fatal(tf, fsr, far, td, &ksig);
367	}
368
369
370	if (error == ENOMEM) {
371		printf("VM: pid %d (%s), uid %d killed: "
372		    "out of swap\n", td->td_proc->p_pid, td->td_name,
373		    (td->td_proc->p_ucred) ?
374		     td->td_proc->p_ucred->cr_uid : -1);
375		ksig.signb = SIGKILL;
376	} else {
377		ksig.signb = SIGSEGV;
378	}
379	ksig.code = 0;
380do_trapsignal:
381	call_trapsignal(td, ksig.signb, ksig.code);
382out:
383	/* If returning to user mode, make sure to invoke userret() */
384	if (user)
385		userret(td, tf);
386}
387
388/*
389 * dab_fatal() handles the following data aborts:
390 *
391 *  FAULT_WRTBUF_0 - Vector Exception
392 *  FAULT_WRTBUF_1 - Terminal Exception
393 *
394 * We should never see these on a properly functioning system.
395 *
396 * This function is also called by the other handlers if they
397 * detect a fatal problem.
398 *
399 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
400 */
401static int
402dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
403    struct ksig *ksig)
404{
405	const char *mode;
406
407#ifdef KDTRACE_HOOKS
408	if (!TRAP_USERMODE(tf))	{
409		if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK))
410			return (0);
411	}
412#endif
413
414	mode = TRAP_USERMODE(tf) ? "user" : "kernel";
415
416	disable_interrupts(PSR_I|PSR_F);
417	if (td != NULL) {
418		printf("Fatal %s mode data abort: '%s'\n", mode,
419		    data_aborts[fsr & FAULT_TYPE_MASK].desc);
420		printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
421		if ((fsr & FAULT_IMPRECISE) == 0)
422			printf("%08x, ", far);
423		else
424			printf("Invalid,  ");
425		printf("spsr=%08x\n", tf->tf_spsr);
426	} else {
427		printf("Fatal %s mode prefetch abort at 0x%08x\n",
428		    mode, tf->tf_pc);
429		printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
430	}
431
432	printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
433	    tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
434	printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
435	    tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
436	printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
437	    tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
438	printf("r12=%08x, ", tf->tf_r12);
439
440	if (TRAP_USERMODE(tf))
441		printf("usp=%08x, ulr=%08x",
442		    tf->tf_usr_sp, tf->tf_usr_lr);
443	else
444		printf("ssp=%08x, slr=%08x",
445		    tf->tf_svc_sp, tf->tf_svc_lr);
446	printf(", pc =%08x\n\n", tf->tf_pc);
447
448#ifdef KDB
449	if (debugger_on_panic || kdb_active)
450		if (kdb_trap(fsr, 0, tf))
451			return (0);
452#endif
453	panic("Fatal abort");
454	/*NOTREACHED*/
455}
456
457/*
458 * dab_align() handles the following data aborts:
459 *
460 *  FAULT_ALIGN_0 - Alignment fault
461 *  FAULT_ALIGN_1 - Alignment fault
462 *
463 * These faults are fatal if they happen in kernel mode. Otherwise, we
464 * deliver a bus error to the process.
465 */
466static int
467dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
468    struct ksig *ksig)
469{
470
471	/* Alignment faults are always fatal if they occur in kernel mode */
472	if (!TRAP_USERMODE(tf)) {
473		if (!td || !td->td_pcb->pcb_onfault)
474			dab_fatal(tf, fsr, far, td, ksig);
475		tf->tf_r0 = EFAULT;
476		tf->tf_pc = (int)td->td_pcb->pcb_onfault;
477		return (0);
478	}
479
480	/* pcb_onfault *must* be NULL at this point */
481
482	/* Deliver a bus error signal to the process */
483	ksig->code = 0;
484	ksig->signb = SIGBUS;
485	td->td_frame = tf;
486
487	return (1);
488}
489
490/*
491 * dab_buserr() handles the following data aborts:
492 *
493 *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
494 *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
495 *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
496 *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
497 *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
498 *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
499 *
500 * If pcb_onfault is set, flag the fault and return to the handler.
501 * If the fault occurred in user mode, give the process a SIGBUS.
502 *
503 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
504 * can be flagged as imprecise in the FSR. This causes a real headache
505 * since some of the machine state is lost. In this case, tf->tf_pc
506 * may not actually point to the offending instruction. In fact, if
507 * we've taken a double abort fault, it generally points somewhere near
508 * the top of "data_abort_entry" in exception.S.
509 *
510 * In all other cases, these data aborts are considered fatal.
511 */
512static int
513dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
514    struct ksig *ksig)
515{
516	struct pcb *pcb = td->td_pcb;
517
518#ifdef __XSCALE__
519	if ((fsr & FAULT_IMPRECISE) != 0 &&
520	    (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
521		/*
522		 * Oops, an imprecise, double abort fault. We've lost the
523		 * r14_abt/spsr_abt values corresponding to the original
524		 * abort, and the spsr saved in the trapframe indicates
525		 * ABT mode.
526		 */
527		tf->tf_spsr &= ~PSR_MODE;
528
529		/*
530		 * We use a simple heuristic to determine if the double abort
531		 * happened as a result of a kernel or user mode access.
532		 * If the current trapframe is at the top of the kernel stack,
533		 * the fault _must_ have come from user mode.
534		 */
535		if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) {
536			/*
537			 * Kernel mode. We're either about to die a
538			 * spectacular death, or pcb_onfault will come
539			 * to our rescue. Either way, the current value
540			 * of tf->tf_pc is irrelevant.
541			 */
542			tf->tf_spsr |= PSR_SVC32_MODE;
543			if (pcb->pcb_onfault == NULL)
544				printf("\nKernel mode double abort!\n");
545		} else {
546			/*
547			 * User mode. We've lost the program counter at the
548			 * time of the fault (not that it was accurate anyway;
549			 * it's not called an imprecise fault for nothing).
550			 * About all we can do is copy r14_usr to tf_pc and
551			 * hope for the best. The process is about to get a
552			 * SIGBUS, so it's probably history anyway.
553			 */
554			tf->tf_spsr |= PSR_USR32_MODE;
555			tf->tf_pc = tf->tf_usr_lr;
556		}
557	}
558
559	/* FAR is invalid for imprecise exceptions */
560	if ((fsr & FAULT_IMPRECISE) != 0)
561		far = 0;
562#endif /* __XSCALE__ */
563
564	if (pcb->pcb_onfault) {
565		tf->tf_r0 = EFAULT;
566		tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
567		return (0);
568	}
569
570	/*
571	 * At this point, if the fault happened in kernel mode, we're toast
572	 */
573	if (!TRAP_USERMODE(tf))
574		dab_fatal(tf, fsr, far, td, ksig);
575
576	/* Deliver a bus error signal to the process */
577	ksig->signb = SIGBUS;
578	ksig->code = 0;
579	td->td_frame = tf;
580
581	return (1);
582}
583
584/*
585 * void prefetch_abort_handler(struct trapframe *tf)
586 *
587 * Abort handler called when instruction execution occurs at
588 * a non existent or restricted (access permissions) memory page.
589 * If the address is invalid and we were in SVC mode then panic as
590 * the kernel should never prefetch abort.
591 * If the address is invalid and the page is mapped then the user process
592 * does no have read permission so send it a signal.
593 * Otherwise fault the page in and try again.
594 */
595static void
596prefetch_abort_handler(struct trapframe *tf)
597{
598	struct thread *td;
599	struct proc * p;
600	struct vm_map *map;
601	vm_offset_t fault_pc, va;
602	int error = 0;
603	struct ksig ksig;
604
605
606#if 0
607	/* Update vmmeter statistics */
608	uvmexp.traps++;
609#endif
610#if 0
611	printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
612	    (void*)tf->tf_usr_lr);
613#endif
614
615 	td = curthread;
616	p = td->td_proc;
617	PCPU_INC(cnt.v_trap);
618
619	if (TRAP_USERMODE(tf)) {
620		td->td_frame = tf;
621		if (td->td_cowgen != td->td_proc->p_cowgen)
622			thread_cow_update(td);
623	}
624	fault_pc = tf->tf_pc;
625	if (td->td_md.md_spinlock_count == 0) {
626		if (__predict_true(tf->tf_spsr & PSR_I) == 0)
627			enable_interrupts(PSR_I);
628		if (__predict_true(tf->tf_spsr & PSR_F) == 0)
629			enable_interrupts(PSR_F);
630	}
631
632	/* Prefetch aborts cannot happen in kernel mode */
633	if (__predict_false(!TRAP_USERMODE(tf)))
634		dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig);
635	td->td_pticks = 0;
636
637
638	/* Ok validate the address, can only execute in USER space */
639	if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
640	    (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
641		ksig.signb = SIGSEGV;
642		ksig.code = 0;
643		goto do_trapsignal;
644	}
645
646	map = &td->td_proc->p_vmspace->vm_map;
647	va = trunc_page(fault_pc);
648
649	/*
650	 * See if the pmap can handle this fault on its own...
651	 */
652#ifdef DEBUG
653	last_fault_code = -1;
654#endif
655	if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
656		goto out;
657
658	error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
659	    VM_FAULT_NORMAL);
660	if (__predict_true(error == 0))
661		goto out;
662
663	if (error == ENOMEM) {
664		printf("VM: pid %d (%s), uid %d killed: "
665		    "out of swap\n", td->td_proc->p_pid, td->td_name,
666		    (td->td_proc->p_ucred) ?
667		     td->td_proc->p_ucred->cr_uid : -1);
668		ksig.signb = SIGKILL;
669	} else {
670		ksig.signb = SIGSEGV;
671	}
672	ksig.code = 0;
673
674do_trapsignal:
675	call_trapsignal(td, ksig.signb, ksig.code);
676
677out:
678	userret(td, tf);
679
680}
681
682extern int badaddr_read_1(const uint8_t *, uint8_t *);
683extern int badaddr_read_2(const uint16_t *, uint16_t *);
684extern int badaddr_read_4(const uint32_t *, uint32_t *);
685/*
686 * Tentatively read an 8, 16, or 32-bit value from 'addr'.
687 * If the read succeeds, the value is written to 'rptr' and zero is returned.
688 * Else, return EFAULT.
689 */
690int
691badaddr_read(void *addr, size_t size, void *rptr)
692{
693	union {
694		uint8_t v1;
695		uint16_t v2;
696		uint32_t v4;
697	} u;
698	int rv;
699
700	cpu_drain_writebuf();
701
702	/* Read from the test address. */
703	switch (size) {
704	case sizeof(uint8_t):
705		rv = badaddr_read_1(addr, &u.v1);
706		if (rv == 0 && rptr)
707			*(uint8_t *) rptr = u.v1;
708		break;
709
710	case sizeof(uint16_t):
711		rv = badaddr_read_2(addr, &u.v2);
712		if (rv == 0 && rptr)
713			*(uint16_t *) rptr = u.v2;
714		break;
715
716	case sizeof(uint32_t):
717		rv = badaddr_read_4(addr, &u.v4);
718		if (rv == 0 && rptr)
719			*(uint32_t *) rptr = u.v4;
720		break;
721
722	default:
723		panic("badaddr: invalid size (%lu)", (u_long) size);
724	}
725
726	/* Return EFAULT if the address was invalid, else zero */
727	return (rv);
728}
729