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