Home | History | Annotate | Line # | Download | only in arm32
fault.c revision 1.36
      1 /*	$NetBSD: fault.c,v 1.36 2003/10/13 21:13:30 scw Exp $	*/
      2 
      3 /*
      4  * Copyright 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 /*
     38  * Copyright (c) 1994-1997 Mark Brinicombe.
     39  * Copyright (c) 1994 Brini.
     40  * All rights reserved.
     41  *
     42  * This code is derived from software written for Brini by Mark Brinicombe
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Brini.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * RiscBSD kernel project
     72  *
     73  * fault.c
     74  *
     75  * Fault handlers
     76  *
     77  * Created      : 28/11/94
     78  */
     79 
     80 #include "opt_ddb.h"
     81 #include "opt_kgdb.h"
     82 #include "opt_pmap_debug.h"
     83 
     84 #include <sys/types.h>
     85 __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.36 2003/10/13 21:13:30 scw Exp $");
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/proc.h>
     90 #include <sys/savar.h>
     91 #include <sys/user.h>
     92 #include <sys/kernel.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #include <arm/cpuconf.h>
     97 
     98 #include <machine/frame.h>
     99 #include <arm/arm32/katelib.h>
    100 #include <machine/cpu.h>
    101 #include <machine/intr.h>
    102 #if defined(DDB) || defined(KGDB)
    103 #include <machine/db_machdep.h>
    104 #ifdef KGDB
    105 #include <sys/kgdb.h>
    106 #endif
    107 #if !defined(DDB)
    108 #define kdb_trap	kgdb_trap
    109 #endif
    110 #endif
    111 
    112 #include <arch/arm/arm/disassem.h>
    113 #include <arm/arm32/machdep.h>
    114 
    115 extern char fusubailout[];
    116 
    117 #ifdef DEBUG
    118 int last_fault_code;	/* For the benefit of pmap_fault_fixup() */
    119 #endif
    120 
    121 static void report_abort __P((const char *, u_int, u_int, u_int));
    122 
    123 /* Abort code */
    124 
    125 /* Define text descriptions of the different aborts */
    126 
    127 static const char *aborts[16] = {
    128 	"Write buffer fault",
    129 	"Alignment fault",
    130 	"Write buffer fault",
    131 	"Alignment fault",
    132 	"Bus error (LF section)",
    133 	"Translation fault (section)",
    134 	"Bus error (page)",
    135 	"Translation fault (page)",
    136 	"Bus error (section)",
    137 	"Domain error (section)",
    138 	"Bus error (page)",
    139 	"Domain error (page)",
    140 	"Bus error trans (L1)",
    141 	"Permission error (section)",
    142 	"Bus error trans (L2)",
    143 	"Permission error (page)"
    144 };
    145 
    146 static void
    147 report_abort(prefix, fault_status, fault_address, fault_pc)
    148 	const char *prefix;
    149 	u_int fault_status;
    150 	u_int fault_address;
    151 	u_int fault_pc;
    152 {
    153 #ifndef DEBUG
    154 	if (prefix == NULL) {
    155 #endif
    156 		if (prefix)
    157 			printf("%s ", prefix);
    158 		printf("Data abort: '%s' status=%03x address=%08x PC=%08x\n",
    159 		    aborts[fault_status & FAULT_TYPE_MASK],
    160 		    fault_status & 0xfff, fault_address, fault_pc);
    161 #ifndef DEBUG
    162 	}
    163 #endif
    164 }
    165 
    166 static __volatile int data_abort_expected;
    167 static __volatile int data_abort_received;
    168 
    169 int
    170 badaddr_read(void *addr, size_t size, void *rptr)
    171 {
    172 	u_long rcpt;
    173 	int rv;
    174 
    175 	/* Tell the Data Abort handler that we're expecting one. */
    176 	data_abort_received = 0;
    177 	data_abort_expected = 1;
    178 
    179 	cpu_drain_writebuf();
    180 
    181 	/* Read from the test address. */
    182 	switch (size) {
    183 	case sizeof(uint8_t):
    184 		__asm __volatile("ldrb %0, [%1]"
    185 			: "=r" (rcpt)
    186 			: "r" (addr));
    187 		break;
    188 
    189 	case sizeof(uint16_t):
    190 		__asm __volatile("ldrh %0, [%1]"
    191 			: "=r" (rcpt)
    192 			: "r" (addr));
    193 		break;
    194 
    195 	case sizeof(uint32_t):
    196 		__asm __volatile("ldr %0, [%1]"
    197 			: "=r" (rcpt)
    198 			: "r" (addr));
    199 		break;
    200 
    201 	default:
    202 		data_abort_expected = 0;
    203 		panic("badaddr: invalid size (%lu)", (u_long) size);
    204 	}
    205 
    206 	/* Disallow further Data Aborts. */
    207 	data_abort_expected = 0;
    208 
    209 	rv = data_abort_received;
    210 	data_abort_received = 0;
    211 
    212 	/* Copy the data back if no fault occurred. */
    213 	if (rptr != NULL && rv == 0) {
    214 		switch (size) {
    215 		case sizeof(uint8_t):
    216 			*(uint8_t *) rptr = rcpt;
    217 			break;
    218 
    219 		case sizeof(uint16_t):
    220 			*(uint16_t *) rptr = rcpt;
    221 			break;
    222 
    223 		case sizeof(uint32_t):
    224 			*(uint32_t *) rptr = rcpt;
    225 			break;
    226 		}
    227 	}
    228 
    229 	/* Return true if the address was invalid. */
    230 	return (rv);
    231 }
    232 
    233 /*
    234  * void data_abort_handler(trapframe_t *frame)
    235  *
    236  * Abort handler called when read/write occurs at an address of
    237  * a non existent or restricted (access permissions) memory page.
    238  * We first need to identify the type of page fault.
    239  */
    240 
    241 #define TRAP_CODE ((fault_status & 0x0f) | (fault_address & 0xfffffff0))
    242 
    243 /* Determine if we can recover from a fault */
    244 #define	IS_FATAL_FAULT(x)					\
    245 	(((1 << (x)) &						\
    246 	  ((1 << FAULT_WRTBUF_0) | (1 << FAULT_WRTBUF_1) |	\
    247 	   (1 << FAULT_BUSERR_0) | (1 << FAULT_BUSERR_1) |	\
    248 	   (1 << FAULT_BUSERR_2) | (1 << FAULT_BUSERR_3) |	\
    249 	   (1 << FAULT_BUSTRNL1) | (1 << FAULT_BUSTRNL2) |	\
    250 	   (1 << FAULT_ALIGN_0)  | (1 << FAULT_ALIGN_1))) != 0)
    251 
    252 void
    253 data_abort_handler(frame)
    254 	trapframe_t *frame;
    255 {
    256 	struct lwp *l;
    257 	struct proc *p;
    258 	struct pcb *pcb;
    259 	u_int fault_address;
    260 	u_int fault_status;
    261 	u_int fault_pc;
    262 	u_int fault_instruction;
    263 	int fault_code, fatal_fault;
    264 	int user;
    265 	int error;
    266 	int rv;
    267 	void *onfault;
    268 	vaddr_t va;
    269 	struct vmspace *vm;
    270 	struct vm_map *map;
    271 	vm_prot_t ftype;
    272 	extern struct vm_map *kernel_map;
    273 	ksiginfo_t ksi;
    274 
    275 	/*
    276 	 * If we were expecting a Data Abort, signal that we got
    277 	 * one, adjust the PC to skip the faulting insn, and
    278 	 * return.
    279 	 */
    280 	if (data_abort_expected) {
    281 		data_abort_received = 1;
    282 		frame->tf_pc += INSN_SIZE;
    283 		return;
    284 	}
    285 
    286 	/*
    287 	 * Must get fault address and status from the CPU before
    288 	 * re-enabling interrupts.  (Interrupt handlers may take
    289 	 * R/M emulation faults.)
    290 	 */
    291 	fault_address = cpu_faultaddress();
    292 	fault_status = cpu_faultstatus();
    293 	fault_pc = frame->tf_pc;
    294 
    295 	/*
    296 	 * Enable IRQ's (disabled by CPU on abort) if trapframe
    297 	 * shows they were enabled.
    298 	 */
    299 	if (!(frame->tf_spsr & I32_bit))
    300 		enable_interrupts(I32_bit);
    301 
    302 #ifdef DEBUG
    303 	if ((GetCPSR() & PSR_MODE) != PSR_SVC32_MODE)
    304 		panic("data_abort_handler: not in SVC32 mode");
    305 #endif
    306 
    307 	/* Update vmmeter statistics */
    308 	uvmexp.traps++;
    309 
    310 	/* Extract the fault code from the fault status */
    311 	fault_code = fault_status & FAULT_TYPE_MASK;
    312 	fatal_fault = IS_FATAL_FAULT(fault_code);
    313 
    314 	/* Get the current lwp structure or lwp0 if there is none */
    315 	l = curlwp == NULL ? &lwp0 : curlwp;
    316 	p = l->l_proc;
    317 
    318 	/*
    319 	 * can't use curpcb, as it might be NULL; and we have p in
    320 	 * a register anyway
    321 	 */
    322 	pcb = &l->l_addr->u_pcb;
    323 
    324 	/* fusubailout is used by [fs]uswintr to avoid page faulting */
    325 	if (pcb->pcb_onfault &&
    326 	    (fatal_fault || pcb->pcb_onfault == fusubailout)) {
    327 
    328 		frame->tf_r0 = EFAULT;
    329 copyfault:
    330 #ifdef DEBUG
    331 		printf("Using pcb_onfault=%p addr=%08x st=%08x l=%p\n",
    332 		    pcb->pcb_onfault, fault_address, fault_status, l);
    333 #endif
    334 		frame->tf_pc = (u_int)pcb->pcb_onfault;
    335 		if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
    336 			panic("Yikes pcb_onfault=%p during USR mode fault",
    337 			    pcb->pcb_onfault);
    338 		return;
    339 	}
    340 
    341 	/* More debug stuff */
    342 
    343 	fault_instruction = ReadWord(fault_pc);
    344 
    345 #ifdef PMAP_DEBUG
    346 	if (pmap_debug_level >= 0) {
    347 		report_abort(NULL, fault_status, fault_address, fault_pc);
    348 		printf("Instruction @V%08x = %08x\n",
    349 		    fault_pc, fault_instruction);
    350 	}
    351 #endif
    352 
    353 	/* Call the cpu specific abort fixup routine */
    354 	error = cpu_dataabt_fixup(frame);
    355 	if (error == ABORT_FIXUP_RETURN)
    356 		return;
    357 	if (error == ABORT_FIXUP_FAILED) {
    358 		printf("pc = 0x%08x, opcode 0x%08x, insn = ", fault_pc, *((u_int *)fault_pc));
    359 		disassemble(fault_pc);
    360 		printf("data abort handler: fixup failed for this instruction\n");
    361 	}
    362 
    363 #ifdef PMAP_DEBUG
    364 	if (pmap_debug_level >= 0)
    365 		printf("fault in process %p\n", p);
    366 #endif
    367 
    368 #ifdef DEBUG
    369 	/* Is this needed ? (XXXSCW: yes. can happen during boot ...) */
    370 	if (!cold && pcb != curpcb) {
    371 		printf("data_abort: Alert ! pcb(%p) != curpcb(%p)\n",
    372 		    pcb, curpcb);
    373 		printf("data_abort: Alert ! proc(%p), curlwp(%p)\n",
    374 		    p, curlwp);
    375 	}
    376 #endif	/* DEBUG */
    377 
    378 	/* Were we in user mode when the abort occurred ? */
    379 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
    380 		/*
    381 		 * Note that the fault was from USR mode.
    382 		 */
    383 		user = 1;
    384 		l->l_addr->u_pcb.pcb_tf = frame;
    385 	} else
    386 		user = 0;
    387 
    388 	/* check if this was a failed fixup */
    389 	if (error == ABORT_FIXUP_FAILED) {
    390 		if (user) {
    391 			KSI_INIT_TRAP(&ksi);
    392 			ksi.ksi_signo = SIGSEGV;
    393 			ksi.ksi_code = 0;
    394 			ksi.ksi_addr = (u_int32_t *)fault_address;
    395 			ksi.ksi_trap = TRAP_CODE;
    396 			ksi.ksi_errno = error;
    397 			goto trapsignal;
    398 		};
    399 		panic("Data abort fixup failed in kernel - we're dead");
    400 	};
    401 
    402 	/* Now act on the fault type */
    403 	if (fatal_fault) {
    404 		/*
    405 		 * None of these faults should happen on a perfectly
    406 		 * functioning system. They indicate either some gross
    407 		 * problem with the kernel, or a hardware problem.
    408 		 * In either case, stop.
    409 		 */
    410 		report_abort(NULL, fault_status, fault_address, fault_pc);
    411 
    412 we_re_toast:
    413 		/*
    414 		 * We're are dead, try and provide some debug
    415 		 * information before dying.
    416 		 */
    417 #if defined(DDB) || defined(KGDB)
    418 		printf("Unhandled trap (frame = %p)\n", frame);
    419 		report_abort(NULL, fault_status, fault_address, fault_pc);
    420 		kdb_trap(T_FAULT, frame);
    421 		return;
    422 #else
    423 		panic("Unhandled trap (frame = %p)", frame);
    424 #endif	/* DDB || KGDB */
    425 	}
    426 
    427 	/*
    428 	 * At this point, we're dealing with one of the following faults:
    429 	 *
    430 	 *  FAULT_TRANS_P	Page Translation Fault
    431 	 *  FAULT_PERM_P	Page Permission Fault
    432 	 *  FAULT_TRANS_S	Section Translation Fault
    433 	 *  FAULT_PERM_S	Section Permission Fault
    434 	 *  FAULT_DOMAIN_P	Page Domain Error Fault
    435 	 *  FAULT_DOMAIN_S	Section Domain Error Fault
    436 	 *
    437 	 * Page/section translation/permission fault -- need to fault in
    438 	 * the page.
    439 	 *
    440 	 * Page/section domain fault -- need to see if the L1 entry can
    441 	 * be fixed up.
    442 	 */
    443 	vm = p->p_vmspace;
    444 	va = trunc_page((vaddr_t)fault_address);
    445 
    446 #ifdef PMAP_DEBUG
    447 	if (pmap_debug_level >= 0)
    448 		printf("page fault: addr=V%08lx ", va);
    449 #endif
    450 
    451 	/*
    452 	 * It is only a kernel address space fault iff:
    453 	 *	1. user == 0  and
    454 	 *	2. pcb_onfault not set or
    455 	 *	3. pcb_onfault set but supervisor space fault
    456 	 * The last can occur during an exec() copyin where the
    457 	 * argument space is lazy-allocated.
    458 	 */
    459 	if (!user &&
    460 	    (va >= VM_MIN_KERNEL_ADDRESS || va < VM_MIN_ADDRESS)) {
    461 		/* Was the fault due to the FPE/IPKDB ? */
    462 		if ((frame->tf_spsr & PSR_MODE) == PSR_UND32_MODE) {
    463 			report_abort("UND32", fault_status,
    464 			    fault_address, fault_pc);
    465 			KSI_INIT_TRAP(&ksi);
    466 			ksi.ksi_signo = SIGSEGV;
    467 			ksi.ksi_code = fault_status;
    468 			ksi.ksi_addr = (u_int32_t *)fault_address;
    469 			ksi.ksi_trap = TRAP_CODE;
    470 			KERNEL_PROC_LOCK(p);
    471 			trapsignal(l, &ksi);
    472 			KERNEL_PROC_UNLOCK(p);
    473 
    474 			/*
    475 			 * Force exit via userret()
    476 			 * This is necessary as the FPE is an extension
    477 			 * to userland that actually runs in a
    478 			 * priveledged mode but uses USR mode
    479 			 * permissions for its accesses.
    480 			 */
    481 			userret(l);
    482 			return;
    483 		}
    484 		map = kernel_map;
    485 	} else {
    486 		map = &vm->vm_map;
    487 		if (l->l_flag & L_SA) {
    488 			KDASSERT(p != NULL && p->p_sa != NULL);
    489 			p->p_sa->sa_vp_faultaddr = (vaddr_t)fault_address;
    490 			l->l_flag |= L_SA_PAGEFAULT;
    491 		}
    492 	}
    493 
    494 #ifdef PMAP_DEBUG
    495 	if (pmap_debug_level >= 0)
    496 		printf("vmmap=%p ", map);
    497 #endif
    498 
    499 	if (map == NULL)
    500 		printf("No map for fault address va = 0x%08lx", va);
    501 
    502 	/*
    503 	 * We need to know whether the page should be mapped
    504 	 * as R or R/W. The MMU does not give us the info as
    505 	 * to whether the fault was caused by a read or a write.
    506 	 * This means we need to disassemble the instruction
    507 	 * responsible and determine if it was a read or write
    508 	 * instruction.
    509 	 */
    510 	/* STR instruction ? */
    511 	if ((fault_instruction & 0x0c100000) == 0x04000000)
    512 		ftype = VM_PROT_WRITE;
    513 	/* STM or CDT instruction ? */
    514 	else if ((fault_instruction & 0x0a100000) == 0x08000000)
    515 		ftype = VM_PROT_WRITE;
    516 #ifdef __XSCALE__
    517 	/* STRH, STRD instruction ? */
    518 	else if ((fault_instruction & 0x0e1000b0) == 0x000000b0)
    519 		ftype = VM_PROT_WRITE;
    520 #else
    521 	/* STRH, STRSH or STRSB instruction ? */
    522 	else if ((fault_instruction & 0x0e100090) == 0x00000090)
    523 		ftype = VM_PROT_WRITE;
    524 #endif
    525 	/* SWP instruction ? */
    526 	else if ((fault_instruction & 0x0fb00ff0) == 0x01000090)
    527 		ftype = VM_PROT_READ | VM_PROT_WRITE;
    528 	else
    529 		ftype = VM_PROT_READ;
    530 
    531 #ifdef PMAP_DEBUG
    532 	if (pmap_debug_level >= 0)
    533 		printf("fault protection = %d\n", ftype);
    534 #endif
    535 
    536 	if (pmap_fault_fixup(map->pmap, va, ftype, user))
    537 		goto out;
    538 
    539 	if (current_intr_depth > 0) {
    540 #if defined(DDB) || defined(KGDB)
    541 		printf("Non-emulated page fault with intr_depth > 0\n");
    542 		report_abort(NULL, fault_status, fault_address, fault_pc);
    543 		kdb_trap(T_FAULT, frame);
    544 		return;
    545 #else
    546 		panic("Fault with intr_depth > 0");
    547 #endif	/* DDB */
    548 	}
    549 
    550 	onfault = pcb->pcb_onfault;
    551 	pcb->pcb_onfault = NULL;
    552 	rv = uvm_fault(map, va, 0, ftype);
    553 	pcb->pcb_onfault = onfault;
    554 	if (map != kernel_map)
    555 		l->l_flag &= ~L_SA_PAGEFAULT;
    556 	if (rv == 0) {
    557 		if (user != 0) /* Record any stack growth... */
    558 			uvm_grow(p, trunc_page(va));
    559 		goto out;
    560 	}
    561 	if (user == 0) {
    562 		if (pcb->pcb_onfault) {
    563 			frame->tf_r0 = rv;
    564 			goto copyfault;
    565 		}
    566 		printf("[u]vm_fault(%p, %lx, %x, 0) -> %x\n", map, va, ftype,
    567 		    rv);
    568 		goto we_re_toast;
    569 	}
    570 
    571 	report_abort("", fault_status, fault_address, fault_pc);
    572 
    573 	KSI_INIT_TRAP(&ksi);
    574 	ksi.ksi_signo = SIGSEGV;
    575 	ksi.ksi_code = 0;
    576 	ksi.ksi_addr = (u_int32_t *)fault_address;
    577 	ksi.ksi_trap = TRAP_CODE;
    578 	ksi.ksi_errno = rv;
    579 
    580 	if (rv == ENOMEM) {
    581 		printf("UVM: pid %d (%s), uid %d killed: "
    582 		    "out of swap\n", p->p_pid, p->p_comm,
    583 		    (p->p_cred && p->p_ucred) ?  p->p_ucred->cr_uid : -1);
    584 	}
    585 
    586 trapsignal:
    587 	KERNEL_PROC_LOCK(p);
    588 #if 0
    589 	/* maybe one day we'll do emulations */
    590 	(*p->p_emul->e_trapsignal)(l, &ksi);
    591 #else
    592 	trapsignal(l, &ksi);
    593 #endif
    594 	KERNEL_PROC_UNLOCK(p);
    595 
    596 out:
    597 	/* Call userret() if it was a USR mode fault */
    598 	if (user)
    599 		userret(l);
    600 }
    601 
    602 
    603 /*
    604  * void prefetch_abort_handler(trapframe_t *frame)
    605  *
    606  * Abort handler called when instruction execution occurs at
    607  * a non existent or restricted (access permissions) memory page.
    608  * If the address is invalid and we were in SVC mode then panic as
    609  * the kernel should never prefetch abort.
    610  * If the address is invalid and the page is mapped then the user process
    611  * does no have read permission so send it a signal.
    612  * Otherwise fault the page in and try again.
    613  */
    614 
    615 void
    616 prefetch_abort_handler(frame)
    617 	trapframe_t *frame;
    618 {
    619 	struct lwp *l;
    620 	struct proc *p;
    621 	struct vm_map *map;
    622 	vaddr_t fault_pc, va;
    623 	int error;
    624 	ksiginfo_t ksi;
    625 
    626 	/*
    627 	 * Enable IRQ's (disabled by the abort) This always comes
    628 	 * from user mode so we know interrupts were not disabled.
    629 	 * But we check anyway.
    630 	 */
    631 	if (!(frame->tf_spsr & I32_bit))
    632 		enable_interrupts(I32_bit);
    633 
    634 #ifdef DEBUG
    635 	if ((GetCPSR() & PSR_MODE) != PSR_SVC32_MODE)
    636 		panic("prefetch_abort_handler: not in SVC32 mode");
    637 #endif
    638 
    639 	/* Update vmmeter statistics */
    640 	uvmexp.traps++;
    641 
    642 	/* Call the cpu specific abort fixup routine */
    643 	error = cpu_prefetchabt_fixup(frame);
    644 	if (error == ABORT_FIXUP_RETURN)
    645 		return;
    646 	if (error == ABORT_FIXUP_FAILED)
    647 		panic("prefetch abort fixup failed");
    648 
    649 	/* Get the current proc structure or proc0 if there is none */
    650 	if ((l = curlwp) == NULL) {
    651 		l = &lwp0;
    652 #ifdef DEBUG
    653 		printf("Prefetch abort with curlwp == 0\n");
    654 #endif
    655 	}
    656 	p = l->l_proc;
    657 
    658 #ifdef PMAP_DEBUG
    659 	if (pmap_debug_level >= 0)
    660 		printf("prefetch fault in process %p %s\n", p, p->p_comm);
    661 #endif
    662 
    663 	/* Get fault address */
    664 	fault_pc = frame->tf_pc;
    665 	va = trunc_page(fault_pc);
    666 
    667 	/* Was the prefectch abort from USR32 mode ? */
    668 	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
    669 		l->l_addr->u_pcb.pcb_tf = frame;
    670 	} else {
    671 		/*
    672 		 * All the kernel code pages are loaded at boot time
    673 		 * and do not get paged
    674 		 */
    675 	        panic("Prefetch abort in non-USR mode (frame=%p PC=0x%08lx)",
    676 	            frame, fault_pc);
    677 	}
    678 
    679 	map = &p->p_vmspace->vm_map;
    680 
    681 #ifdef PMAP_DEBUG
    682 	if (pmap_debug_level >= 0)
    683 		printf("prefetch_abort: PC = %08lx\n", fault_pc);
    684 #endif
    685 	/* Ok validate the address, can only execute in USER space */
    686 	if (fault_pc < VM_MIN_ADDRESS || fault_pc >= VM_MAXUSER_ADDRESS) {
    687 #ifdef DEBUG
    688 		printf("prefetch: pc (%08lx) not in user process space\n",
    689 		    fault_pc);
    690 #endif
    691 		KSI_INIT_TRAP(&ksi);
    692 		ksi.ksi_signo = SIGSEGV;
    693 		ksi.ksi_code = SEGV_ACCERR;
    694 		ksi.ksi_addr = (u_int32_t *)fault_pc;
    695 		ksi.ksi_trap = fault_pc;
    696 
    697 		goto prefetch_trapsignal;
    698 	}
    699 
    700 	/*
    701 	 * See if the pmap can handle this fault on its own...
    702 	 */
    703 	if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
    704 		goto prefetch_out;
    705 
    706 	if (current_intr_depth > 0) {
    707 #ifdef DDB
    708 		printf("Non-emulated prefetch abort with intr_depth > 0\n");
    709 		kdb_trap(T_FAULT, frame);
    710 		return;
    711 #else
    712 		panic("Prefetch Abort with intr_depth > 0");
    713 #endif
    714 	}
    715 
    716 	error = uvm_fault(map, va, 0, VM_PROT_READ);
    717 	if (error == 0)
    718 		goto prefetch_out;
    719 
    720 	KSI_INIT_TRAP(&ksi);
    721 	ksi.ksi_signo = SIGSEGV;
    722 	ksi.ksi_code = 0;
    723 	ksi.ksi_errno = error;
    724 	ksi.ksi_addr = (u_int32_t *)fault_pc;
    725 	ksi.ksi_trap = fault_pc;
    726 
    727 	if (error == ENOMEM) {
    728 		printf("UVM: pid %d (%s), uid %d killed: "
    729 		    "out of swap\n", p->p_pid, p->p_comm,
    730 		    (p->p_cred && p->p_ucred) ?  p->p_ucred->cr_uid : -1);
    731 	}
    732 prefetch_trapsignal:
    733 	KERNEL_PROC_LOCK(p);
    734 #if 0
    735 	/* maybe one day we'll do emulations */
    736 	(*p->p_emul->e_trapsignal)(l, &ksi);
    737 #else
    738 	trapsignal(l, &ksi);
    739 #endif
    740 	KERNEL_PROC_UNLOCK(p);
    741 prefetch_out:
    742 	userret(l);
    743 }
    744