Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.1
      1  1.1  simonb /*	$NetBSD: trap.c,v 1.1 2001/06/13 06:01:48 simonb Exp $	*/
      2  1.1  simonb 
      3  1.1  simonb /*
      4  1.1  simonb  * Copyright 2001 Wasabi Systems, Inc.
      5  1.1  simonb  * All rights reserved.
      6  1.1  simonb  *
      7  1.1  simonb  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8  1.1  simonb  *
      9  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     10  1.1  simonb  * modification, are permitted provided that the following conditions
     11  1.1  simonb  * are met:
     12  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     13  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     14  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     17  1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     18  1.1  simonb  *    must display the following acknowledgement:
     19  1.1  simonb  *      This product includes software developed for the NetBSD Project by
     20  1.1  simonb  *      Wasabi Systems, Inc.
     21  1.1  simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  simonb  *    or promote products derived from this software without specific prior
     23  1.1  simonb  *    written permission.
     24  1.1  simonb  *
     25  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  simonb  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  simonb  */
     37  1.1  simonb 
     38  1.1  simonb /*
     39  1.1  simonb  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     40  1.1  simonb  * Copyright (C) 1995, 1996 TooLs GmbH.
     41  1.1  simonb  * All rights reserved.
     42  1.1  simonb  *
     43  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     44  1.1  simonb  * modification, are permitted provided that the following conditions
     45  1.1  simonb  * are met:
     46  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     47  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     48  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     50  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     51  1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     52  1.1  simonb  *    must display the following acknowledgement:
     53  1.1  simonb  *	This product includes software developed by TooLs GmbH.
     54  1.1  simonb  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     55  1.1  simonb  *    derived from this software without specific prior written permission.
     56  1.1  simonb  *
     57  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     58  1.1  simonb  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  1.1  simonb  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  1.1  simonb  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     61  1.1  simonb  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     62  1.1  simonb  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     63  1.1  simonb  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     64  1.1  simonb  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     65  1.1  simonb  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     66  1.1  simonb  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  1.1  simonb  */
     68  1.1  simonb 
     69  1.1  simonb #include "opt_altivec.h"
     70  1.1  simonb #include "opt_ddb.h"
     71  1.1  simonb #include "opt_ktrace.h"
     72  1.1  simonb #include "opt_syscall_debug.h"
     73  1.1  simonb 
     74  1.1  simonb #include <sys/param.h>
     75  1.1  simonb #include <sys/proc.h>
     76  1.1  simonb #include <sys/reboot.h>
     77  1.1  simonb #include <sys/syscall.h>
     78  1.1  simonb #include <sys/systm.h>
     79  1.1  simonb #include <sys/user.h>
     80  1.1  simonb #include <sys/ktrace.h>
     81  1.1  simonb 
     82  1.1  simonb #include <uvm/uvm_extern.h>
     83  1.1  simonb 
     84  1.1  simonb #include <dev/cons.h>
     85  1.1  simonb 
     86  1.1  simonb #include <machine/cpu.h>
     87  1.1  simonb #include <machine/db_machdep.h>
     88  1.1  simonb #include <machine/fpu.h>
     89  1.1  simonb #include <machine/frame.h>
     90  1.1  simonb #include <machine/pcb.h>
     91  1.1  simonb #include <machine/psl.h>
     92  1.1  simonb #include <machine/trap.h>
     93  1.1  simonb 
     94  1.1  simonb #include <powerpc/spr.h>
     95  1.1  simonb #include <powerpc/ibm4xx/pmap.h>
     96  1.1  simonb #include <powerpc/ibm4xx/tlb.h>
     97  1.1  simonb #include <powerpc/fpu/fpu_extern.h>
     98  1.1  simonb 
     99  1.1  simonb /* These definitions should probably be somewhere else			XXX */
    100  1.1  simonb #define	FIRSTARG	3		/* first argument is in reg 3 */
    101  1.1  simonb #define	NARGREG		8		/* 8 args are in registers */
    102  1.1  simonb #define	MOREARGS(sp)	((caddr_t)((int)(sp) + 8)) /* more args go here */
    103  1.1  simonb 
    104  1.1  simonb #ifndef MULTIPROCESSOR
    105  1.1  simonb volatile int astpending;
    106  1.1  simonb volatile int want_resched;
    107  1.1  simonb #endif
    108  1.1  simonb 
    109  1.1  simonb void *syscall = NULL;	/* XXX dummy symbol for emul_netbsd */
    110  1.1  simonb 
    111  1.1  simonb static int fix_unaligned __P((struct proc *p, struct trapframe *frame));
    112  1.1  simonb 
    113  1.1  simonb void trap __P((struct trapframe *));	/* Called from locore / trap_subr */
    114  1.1  simonb int setfault __P((faultbuf));	/* defined in locore.S */
    115  1.1  simonb /* Why are these not defined in a header? */
    116  1.1  simonb int badaddr __P((void *, size_t));
    117  1.1  simonb int badaddr_read __P((void *, size_t, int *));
    118  1.1  simonb int ctx_setup __P((int, int));
    119  1.1  simonb 
    120  1.1  simonb #ifdef DEBUG
    121  1.1  simonb #define TDB_ALL	0x1
    122  1.1  simonb int trapdebug = /* TDB_ALL */ 0;
    123  1.1  simonb #define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
    124  1.1  simonb #else
    125  1.1  simonb #define DBPRINTF(x, y)
    126  1.1  simonb #endif
    127  1.1  simonb 
    128  1.1  simonb void
    129  1.1  simonb trap(struct trapframe *frame)
    130  1.1  simonb {
    131  1.1  simonb 	struct proc *p = curproc;
    132  1.1  simonb 	int type = frame->exc;
    133  1.1  simonb 	int ftype, rv;
    134  1.1  simonb 
    135  1.1  simonb 	KASSERT(p == 0 || (p->p_stat == SONPROC));
    136  1.1  simonb 
    137  1.1  simonb 	if (frame->srr1 & PSL_PR)
    138  1.1  simonb 		type |= EXC_USER;
    139  1.1  simonb 
    140  1.1  simonb 	ftype = VM_PROT_READ;
    141  1.1  simonb 
    142  1.1  simonb DBPRINTF(TDB_ALL, ("trap(%x) at %x from frame %p &frame %p\n",
    143  1.1  simonb 	type, frame->srr0, frame, &frame));
    144  1.1  simonb 
    145  1.1  simonb 	switch (type) {
    146  1.1  simonb 	case EXC_DEBUG|EXC_USER:
    147  1.1  simonb {
    148  1.1  simonb 	int srr2, srr3;
    149  1.1  simonb __asm __volatile("mfspr %0,0x3f0" : "=r" (rv), "=r" (srr2), "=r" (srr3) :);
    150  1.1  simonb printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, srr3);
    151  1.1  simonb }
    152  1.1  simonb 		/*
    153  1.1  simonb 		 * DEBUG intr -- probably single-step.
    154  1.1  simonb 		 */
    155  1.1  simonb 	case EXC_TRC|EXC_USER:
    156  1.1  simonb 		KERNEL_PROC_LOCK(p);
    157  1.1  simonb 		frame->srr1 &= ~PSL_SE;
    158  1.1  simonb 		trapsignal(p, SIGTRAP, EXC_TRC);
    159  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    160  1.1  simonb 		break;
    161  1.1  simonb 
    162  1.1  simonb 	  /* If we could not find and install appropriate TLB entry, fall through */
    163  1.1  simonb 
    164  1.1  simonb 	case EXC_DSI:
    165  1.1  simonb 		/* FALLTHROUGH */
    166  1.1  simonb 	case EXC_DTMISS:
    167  1.1  simonb 		{
    168  1.1  simonb 			struct vm_map *map;
    169  1.1  simonb 			vaddr_t va;
    170  1.1  simonb 			faultbuf *fb = NULL;
    171  1.1  simonb 
    172  1.1  simonb 			KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
    173  1.1  simonb 			va = frame->dear;
    174  1.1  simonb 			if (frame->pid == KERNEL_PID) {
    175  1.1  simonb 				map = kernel_map;
    176  1.1  simonb 			} else {
    177  1.1  simonb 				map = &p->p_vmspace->vm_map;
    178  1.1  simonb 			}
    179  1.1  simonb 
    180  1.1  simonb 			if (frame->esr & (ESR_DST|ESR_DIZ))
    181  1.1  simonb 				ftype = VM_PROT_READ | VM_PROT_WRITE;
    182  1.1  simonb 
    183  1.1  simonb DBPRINTF(TDB_ALL, ("trap(EXC_DSI) at %x %s fault on %p esr %x\n",
    184  1.1  simonb frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", (void *)va, frame->esr));
    185  1.1  simonb 			rv = uvm_fault(map, trunc_page(va), 0, ftype);
    186  1.1  simonb 			KERNEL_UNLOCK();
    187  1.1  simonb 			if (rv == 0)
    188  1.1  simonb 				goto done;
    189  1.1  simonb 			if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
    190  1.1  simonb 				frame->pid = KERNEL_PID;
    191  1.1  simonb 				frame->srr0 = (*fb)[0];
    192  1.1  simonb 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    193  1.1  simonb 				frame->fixreg[1] = (*fb)[1];
    194  1.1  simonb 				frame->fixreg[2] = (*fb)[2];
    195  1.1  simonb 				frame->fixreg[3] = 1; /* Return TRUE */
    196  1.1  simonb 				frame->cr = (*fb)[3];
    197  1.1  simonb 				bcopy(&(*fb)[4], &frame->fixreg[13],
    198  1.1  simonb 				      19 * sizeof(register_t));
    199  1.1  simonb 				goto done;
    200  1.1  simonb 			}
    201  1.1  simonb 		}
    202  1.1  simonb 		goto brain_damage;
    203  1.1  simonb 
    204  1.1  simonb 	case EXC_DSI|EXC_USER:
    205  1.1  simonb 		/* FALLTHROUGH */
    206  1.1  simonb 	case EXC_DTMISS|EXC_USER:
    207  1.1  simonb 		KERNEL_PROC_LOCK(p);
    208  1.1  simonb 
    209  1.1  simonb 		if (frame->esr & (ESR_DST|ESR_DIZ))
    210  1.1  simonb 			ftype = VM_PROT_READ | VM_PROT_WRITE;
    211  1.1  simonb 
    212  1.1  simonb DBPRINTF(TDB_ALL, ("trap(EXC_DSI|EXC_USER) at %x %s fault on %x %x\n",
    213  1.1  simonb frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->dear, frame->esr));
    214  1.1  simonb KASSERT(p == curproc && (p->p_stat == SONPROC));
    215  1.1  simonb 		rv = uvm_fault(&p->p_vmspace->vm_map,
    216  1.1  simonb 			       trunc_page(frame->dear), 0, ftype);
    217  1.1  simonb 		if (rv == 0) {
    218  1.1  simonb 		  KERNEL_PROC_UNLOCK(p);
    219  1.1  simonb 		  break;
    220  1.1  simonb 		}
    221  1.1  simonb 		if (rv == ENOMEM) {
    222  1.1  simonb 			printf("UVM: pid %d (%s), uid %d killed: "
    223  1.1  simonb 			       "out of swap\n",
    224  1.1  simonb 			       p->p_pid, p->p_comm,
    225  1.1  simonb 			       p->p_cred && p->p_ucred ?
    226  1.1  simonb 			       p->p_ucred->cr_uid : -1);
    227  1.1  simonb 			trapsignal(p, SIGKILL, EXC_DSI);
    228  1.1  simonb 		} else {
    229  1.1  simonb 			trapsignal(p, SIGSEGV, EXC_DSI);
    230  1.1  simonb 		}
    231  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    232  1.1  simonb 		break;
    233  1.1  simonb 	case EXC_ITMISS|EXC_USER:
    234  1.1  simonb 	case EXC_ISI|EXC_USER:
    235  1.1  simonb 		KERNEL_PROC_LOCK(p);
    236  1.1  simonb 		ftype = VM_PROT_READ | VM_PROT_EXECUTE;
    237  1.1  simonb DBPRINTF(TDB_ALL, ("trap(EXC_ISI|EXC_USER) at %x %s fault on %x tf %p\n",
    238  1.1  simonb frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->srr0, frame));
    239  1.1  simonb 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0), 0, ftype);
    240  1.1  simonb 		if (rv == 0) {
    241  1.1  simonb 		  KERNEL_PROC_UNLOCK(p);
    242  1.1  simonb 		  break;
    243  1.1  simonb 		}
    244  1.1  simonb 		trapsignal(p, SIGSEGV, EXC_ISI);
    245  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    246  1.1  simonb 		break;
    247  1.1  simonb 	case EXC_SC|EXC_USER:
    248  1.1  simonb 		{
    249  1.1  simonb 			const struct sysent *callp;
    250  1.1  simonb 			size_t argsize;
    251  1.1  simonb 			register_t code, error;
    252  1.1  simonb 			register_t *params, rval[2];
    253  1.1  simonb 			int n;
    254  1.1  simonb 			register_t args[10];
    255  1.1  simonb 
    256  1.1  simonb 			KERNEL_PROC_LOCK(p);
    257  1.1  simonb 
    258  1.1  simonb 			uvmexp.syscalls++;
    259  1.1  simonb 
    260  1.1  simonb 			code = frame->fixreg[0];
    261  1.1  simonb 			callp = p->p_emul->e_sysent;
    262  1.1  simonb 			params = frame->fixreg + FIRSTARG;
    263  1.1  simonb 			n = NARGREG;
    264  1.1  simonb 
    265  1.1  simonb 			switch (code) {
    266  1.1  simonb 			case SYS_syscall:
    267  1.1  simonb 				/*
    268  1.1  simonb 				 * code is first argument,
    269  1.1  simonb 				 * followed by actual args.
    270  1.1  simonb 				 */
    271  1.1  simonb 				code = *params++;
    272  1.1  simonb 				n -= 1;
    273  1.1  simonb 				break;
    274  1.1  simonb 			case SYS___syscall:
    275  1.1  simonb 				params++;
    276  1.1  simonb 				code = *params++;
    277  1.1  simonb 				n -= 2;
    278  1.1  simonb 				break;
    279  1.1  simonb 			default:
    280  1.1  simonb 				break;
    281  1.1  simonb 			}
    282  1.1  simonb 
    283  1.1  simonb 			code &= (SYS_NSYSENT - 1);
    284  1.1  simonb 			callp += code;
    285  1.1  simonb 			argsize = callp->sy_argsize;
    286  1.1  simonb 
    287  1.1  simonb 			if (argsize > n * sizeof(register_t)) {
    288  1.1  simonb 				memcpy(args, params, n * sizeof(register_t));
    289  1.1  simonb 				error = copyin(MOREARGS(frame->fixreg[1]),
    290  1.1  simonb 					       args + n,
    291  1.1  simonb 					       argsize - n * sizeof(register_t));
    292  1.1  simonb 				if (error)
    293  1.1  simonb 					goto syscall_bad;
    294  1.1  simonb 				params = args;
    295  1.1  simonb 			}
    296  1.1  simonb 
    297  1.1  simonb #ifdef	KTRACE
    298  1.1  simonb 			if (KTRPOINT(p, KTR_SYSCALL))
    299  1.1  simonb 				ktrsyscall(p, code, argsize, params);
    300  1.1  simonb #endif
    301  1.1  simonb #ifdef SYSCALL_DEBUG
    302  1.1  simonb 			if (trapdebug)
    303  1.1  simonb 				scdebug_call(p, code, args);
    304  1.1  simonb #endif
    305  1.1  simonb 			rval[0] = 0;
    306  1.1  simonb 			rval[1] = 0;
    307  1.1  simonb 
    308  1.1  simonb 			error = (*callp->sy_call)(p, params, rval);
    309  1.1  simonb #ifdef SYSCALL_DEBUG
    310  1.1  simonb 			if (trapdebug)
    311  1.1  simonb 				scdebug_ret(p, code, error, rval);
    312  1.1  simonb #endif
    313  1.1  simonb 			switch (error) {
    314  1.1  simonb 			case 0:
    315  1.1  simonb 				frame->fixreg[FIRSTARG] = rval[0];
    316  1.1  simonb 				frame->fixreg[FIRSTARG + 1] = rval[1];
    317  1.1  simonb 				frame->cr &= ~0x10000000;
    318  1.1  simonb 				break;
    319  1.1  simonb 			case ERESTART:
    320  1.1  simonb 				/*
    321  1.1  simonb 				 * Set user's pc back to redo the system call.
    322  1.1  simonb 				 */
    323  1.1  simonb 				frame->srr0 -= 4;
    324  1.1  simonb 				break;
    325  1.1  simonb 			case EJUSTRETURN:
    326  1.1  simonb 				/* nothing to do */
    327  1.1  simonb 				break;
    328  1.1  simonb 			default:
    329  1.1  simonb syscall_bad:
    330  1.1  simonb 				if (p->p_emul->e_errno)
    331  1.1  simonb 					error = p->p_emul->e_errno[error];
    332  1.1  simonb 				frame->fixreg[FIRSTARG] = error;
    333  1.1  simonb 				frame->cr |= 0x10000000;
    334  1.1  simonb 				break;
    335  1.1  simonb 			}
    336  1.1  simonb 
    337  1.1  simonb #ifdef	KTRACE
    338  1.1  simonb 			if (KTRPOINT(p, KTR_SYSRET))
    339  1.1  simonb 				ktrsysret(p, code, error, rval[0]);
    340  1.1  simonb #endif
    341  1.1  simonb 		}
    342  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    343  1.1  simonb 		break;
    344  1.1  simonb 
    345  1.1  simonb 	case EXC_AST|EXC_USER:
    346  1.1  simonb 		astpending = 0;		/* we are about to do it */
    347  1.1  simonb 		KERNEL_PROC_LOCK(p);
    348  1.1  simonb 		uvmexp.softs++;
    349  1.1  simonb 		if (p->p_flag & P_OWEUPC) {
    350  1.1  simonb 			p->p_flag &= ~P_OWEUPC;
    351  1.1  simonb 			ADDUPROF(p);
    352  1.1  simonb 		}
    353  1.1  simonb 		/* Check whether we are being preempted. */
    354  1.1  simonb 		if (want_resched)
    355  1.1  simonb 			preempt(NULL);
    356  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    357  1.1  simonb 		break;
    358  1.1  simonb 
    359  1.1  simonb 
    360  1.1  simonb 	case EXC_ALI|EXC_USER:
    361  1.1  simonb 		KERNEL_PROC_LOCK(p);
    362  1.1  simonb 		if (fix_unaligned(p, frame) != 0)
    363  1.1  simonb 			trapsignal(p, SIGBUS, EXC_ALI);
    364  1.1  simonb 		else
    365  1.1  simonb 			frame->srr0 += 4;
    366  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    367  1.1  simonb 		break;
    368  1.1  simonb 
    369  1.1  simonb 	case EXC_PGM|EXC_USER:
    370  1.1  simonb 		/*
    371  1.1  simonb 		 * Illegal insn:
    372  1.1  simonb 		 *
    373  1.1  simonb 		 * let's try to see if it's FPU and can be emulated.
    374  1.1  simonb 		 */
    375  1.1  simonb 		uvmexp.traps ++;
    376  1.1  simonb 		if (!(p->p_addr->u_pcb.pcb_flags & PCB_FPU)) {
    377  1.1  simonb 			bzero(&p->p_addr->u_pcb.pcb_fpu,
    378  1.1  simonb 				sizeof p->p_addr->u_pcb.pcb_fpu);
    379  1.1  simonb 			p->p_addr->u_pcb.pcb_flags |= PCB_FPU;
    380  1.1  simonb 		}
    381  1.1  simonb 
    382  1.1  simonb 		if ((rv = fpu_emulate(frame,
    383  1.1  simonb 			(struct fpreg *)&p->p_addr->u_pcb.pcb_fpu))) {
    384  1.1  simonb 			KERNEL_PROC_LOCK(p);
    385  1.1  simonb 			trapsignal(p, rv, EXC_PGM);
    386  1.1  simonb 			KERNEL_PROC_UNLOCK(p);
    387  1.1  simonb 		}
    388  1.1  simonb 		break;
    389  1.1  simonb 
    390  1.1  simonb 	case EXC_MCHK:
    391  1.1  simonb 		{
    392  1.1  simonb 			faultbuf *fb;
    393  1.1  simonb 
    394  1.1  simonb 			if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
    395  1.1  simonb 				frame->pid = KERNEL_PID;
    396  1.1  simonb 				frame->srr0 = (*fb)[0];
    397  1.1  simonb 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    398  1.1  simonb 				frame->fixreg[1] = (*fb)[1];
    399  1.1  simonb 				frame->fixreg[2] = (*fb)[2];
    400  1.1  simonb 				frame->fixreg[3] = 1; /* Return TRUE */
    401  1.1  simonb 				frame->cr = (*fb)[3];
    402  1.1  simonb 				bcopy(&(*fb)[4], &frame->fixreg[13],
    403  1.1  simonb 				      19 * sizeof(register_t));
    404  1.1  simonb 				goto done;
    405  1.1  simonb 			}
    406  1.1  simonb 		}
    407  1.1  simonb 		goto brain_damage;
    408  1.1  simonb 	default:
    409  1.1  simonb brain_damage:
    410  1.1  simonb 		printf("trap type 0x%x at 0x%x\n", type, frame->srr0);
    411  1.1  simonb #ifdef DDB
    412  1.1  simonb 		if (kdb_trap(type, frame))
    413  1.1  simonb 			goto done;
    414  1.1  simonb #endif
    415  1.1  simonb #ifdef TRAP_PANICWAIT
    416  1.1  simonb 		printf("Press a key to panic.\n");
    417  1.1  simonb 		cngetc();
    418  1.1  simonb #endif
    419  1.1  simonb 		panic("trap");
    420  1.1  simonb 	}
    421  1.1  simonb 
    422  1.1  simonb 	/* Take pending signals. */
    423  1.1  simonb 	{
    424  1.1  simonb 		int sig;
    425  1.1  simonb 
    426  1.1  simonb 		while ((sig = CURSIG(p)) != 0)
    427  1.1  simonb 			postsig(sig);
    428  1.1  simonb 	}
    429  1.1  simonb 
    430  1.1  simonb 	curcpu()->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
    431  1.1  simonb   done:
    432  1.1  simonb }
    433  1.1  simonb 
    434  1.1  simonb int
    435  1.1  simonb ctx_setup(int ctx, int srr1)
    436  1.1  simonb {
    437  1.1  simonb 	volatile struct pmap *pm;
    438  1.1  simonb 
    439  1.1  simonb 	/* Update PID if we're returning to user mode. */
    440  1.1  simonb 	if (srr1 & PSL_PR) {
    441  1.1  simonb 		pm = curproc->p_vmspace->vm_map.pmap;
    442  1.1  simonb 		if (!pm->pm_ctx) {
    443  1.1  simonb 			ctx_alloc((struct pmap *)pm);
    444  1.1  simonb 		}
    445  1.1  simonb 		ctx = pm->pm_ctx;
    446  1.1  simonb 		if (srr1 & PSL_SE) {
    447  1.1  simonb 			int dbreg, mask = 0x48000000;
    448  1.1  simonb 				/*
    449  1.1  simonb 				 * Set the Internal Debug and
    450  1.1  simonb 				 * Instruction Completion bits of
    451  1.1  simonb 				 * the DBCR0 register.
    452  1.1  simonb 				 *
    453  1.1  simonb 				 * XXX this is also used by jtag debuggers...
    454  1.1  simonb 				 */
    455  1.1  simonb 			__asm __volatile("mfspr %0,0x3f2;"
    456  1.1  simonb 				"or %0,%0,%1;"
    457  1.1  simonb 				"mtspr 0x3f2,%0;" :
    458  1.1  simonb 				"=&r" (dbreg) : "r" (mask));
    459  1.1  simonb 		}
    460  1.1  simonb 	}
    461  1.1  simonb 	else if (!ctx) {
    462  1.1  simonb 		ctx = KERNEL_PID;
    463  1.1  simonb 	}
    464  1.1  simonb 	return (ctx);
    465  1.1  simonb }
    466  1.1  simonb 
    467  1.1  simonb void
    468  1.1  simonb child_return(void *arg)
    469  1.1  simonb {
    470  1.1  simonb 	struct proc *p = arg;
    471  1.1  simonb 	struct trapframe *tf = trapframe(p);
    472  1.1  simonb 
    473  1.1  simonb 	KERNEL_PROC_UNLOCK(p);
    474  1.1  simonb 
    475  1.1  simonb 	tf->fixreg[FIRSTARG] = 0;
    476  1.1  simonb 	tf->fixreg[FIRSTARG + 1] = 1;
    477  1.1  simonb 	tf->cr &= ~0x10000000;
    478  1.1  simonb 	tf->srr1 &= ~(PSL_FP|PSL_VEC);	/* Disable FP & AltiVec, as we can't be them */
    479  1.1  simonb #ifdef	KTRACE
    480  1.1  simonb 	if (KTRPOINT(p, KTR_SYSRET)) {
    481  1.1  simonb 		KERNEL_PROC_LOCK(p);
    482  1.1  simonb 		ktrsysret(p, SYS_fork, 0, 0);
    483  1.1  simonb 		KERNEL_PROC_UNLOCK(p);
    484  1.1  simonb 	}
    485  1.1  simonb #endif
    486  1.1  simonb 	/* Profiling?							XXX */
    487  1.1  simonb 	curcpu()->ci_schedstate.spc_curpriority = p->p_priority;
    488  1.1  simonb }
    489  1.1  simonb 
    490  1.1  simonb /*
    491  1.1  simonb  * Used by copyin()/copyout()
    492  1.1  simonb  */
    493  1.1  simonb extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
    494  1.1  simonb extern void vunmaprange __P((vaddr_t, vsize_t));
    495  1.1  simonb static int bigcopyin __P((const void *,	void *,	size_t ));
    496  1.1  simonb static int bigcopyout __P((const void *, void *, size_t ));
    497  1.1  simonb 
    498  1.1  simonb int
    499  1.1  simonb copyin(const void *udaddr, void *kaddr, size_t len)
    500  1.1  simonb {
    501  1.1  simonb 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    502  1.1  simonb 	int msr, pid, tmp, ctx;
    503  1.1  simonb 	faultbuf env;
    504  1.1  simonb 
    505  1.1  simonb 	/* For bigger buffers use the faster copy */
    506  1.1  simonb 	if (len > 256) return (bigcopyin(udaddr, kaddr, len));
    507  1.1  simonb 
    508  1.1  simonb 	if (setfault(env)) {
    509  1.1  simonb 		curpcb->pcb_onfault = 0;
    510  1.1  simonb 		return EFAULT;
    511  1.1  simonb 	}
    512  1.1  simonb 
    513  1.1  simonb 	if (!(ctx = pm->pm_ctx)) {
    514  1.1  simonb 		/* No context -- assign it one */
    515  1.1  simonb 		ctx_alloc(pm);
    516  1.1  simonb 		ctx = pm->pm_ctx;
    517  1.1  simonb 	}
    518  1.1  simonb 
    519  1.1  simonb 	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    520  1.1  simonb 		"mfmsr %0;"			/* Save MSR */
    521  1.1  simonb 		"li %1,0x20; "
    522  1.1  simonb 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    523  1.1  simonb 		"mfpid %1;"			/* Save old PID */
    524  1.1  simonb 		"sync; isync;"
    525  1.1  simonb 
    526  1.1  simonb 		"1: bdz 2f;"			/* while len */
    527  1.1  simonb 		"mtpid %3; sync;"		/* Load user ctx */
    528  1.1  simonb 		"lbz %2,0(%4); addi %4,%4,1;"	/* Load byte */
    529  1.1  simonb 		"sync; isync;"
    530  1.1  simonb 		"mtpid %1;sync;"
    531  1.1  simonb 		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;"	/* Store kernel byte */
    532  1.1  simonb 		"sync; isync;"
    533  1.1  simonb 		"b 1b;"				/* repeat */
    534  1.1  simonb 
    535  1.1  simonb 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    536  1.1  simonb 		"sync; isync;"
    537  1.1  simonb 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    538  1.1  simonb 		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
    539  1.1  simonb 
    540  1.1  simonb 	curpcb->pcb_onfault = 0;
    541  1.1  simonb 	return 0;
    542  1.1  simonb }
    543  1.1  simonb 
    544  1.1  simonb static int
    545  1.1  simonb bigcopyin(const void *udaddr, void *kaddr, size_t len)
    546  1.1  simonb {
    547  1.1  simonb 	const char *up;
    548  1.1  simonb 	char *kp = kaddr;
    549  1.1  simonb 	struct proc *p = curproc;
    550  1.1  simonb 	int error;
    551  1.1  simonb 
    552  1.1  simonb 	if (!p) {
    553  1.1  simonb 		return EFAULT;
    554  1.1  simonb 	}
    555  1.1  simonb 
    556  1.1  simonb 	/*
    557  1.1  simonb 	 * Stolen from physio():
    558  1.1  simonb 	 */
    559  1.1  simonb 	PHOLD(p);
    560  1.1  simonb 	error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ);
    561  1.1  simonb 	if (error) {
    562  1.1  simonb 		PRELE(p);
    563  1.1  simonb 		return EFAULT;
    564  1.1  simonb 	}
    565  1.1  simonb 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    566  1.1  simonb 
    567  1.1  simonb 	bcopy(up, kp, len);
    568  1.1  simonb 	vunmaprange((vaddr_t)up, len);
    569  1.1  simonb 	uvm_vsunlock(p, (caddr_t)udaddr, len);
    570  1.1  simonb 	PRELE(p);
    571  1.1  simonb 
    572  1.1  simonb 	return 0;
    573  1.1  simonb }
    574  1.1  simonb 
    575  1.1  simonb int
    576  1.1  simonb copyout(const void *kaddr, void *udaddr, size_t len)
    577  1.1  simonb {
    578  1.1  simonb 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    579  1.1  simonb 	int msr, pid, tmp, ctx;
    580  1.1  simonb 	faultbuf env;
    581  1.1  simonb 
    582  1.1  simonb 	/* For big copies use more efficient routine */
    583  1.1  simonb 	if (len > 256) return (bigcopyout(kaddr, udaddr, len));
    584  1.1  simonb 
    585  1.1  simonb 	if (setfault(env)) {
    586  1.1  simonb 		curpcb->pcb_onfault = 0;
    587  1.1  simonb 		return EFAULT;
    588  1.1  simonb 	}
    589  1.1  simonb 
    590  1.1  simonb 	if (!(ctx = pm->pm_ctx)) {
    591  1.1  simonb 		/* No context -- assign it one */
    592  1.1  simonb 		ctx_alloc(pm);
    593  1.1  simonb 		ctx = pm->pm_ctx;
    594  1.1  simonb 	}
    595  1.1  simonb 
    596  1.1  simonb 	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    597  1.1  simonb 		"mfmsr %0;"			/* Save MSR */
    598  1.1  simonb 		"li %1,0x20; "
    599  1.1  simonb 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    600  1.1  simonb 		"mfpid %1;"			/* Save old PID */
    601  1.1  simonb 		"sync; isync;"
    602  1.1  simonb 
    603  1.1  simonb 		"1: bdz 2f;"			/* while len */
    604  1.1  simonb 		"mtpid %1;sync;"
    605  1.1  simonb 		"lbz %2,0(%5); addi %5,%5,1;"	/* Load kernel byte */
    606  1.1  simonb 		"sync; isync;"
    607  1.1  simonb 		"mtpid %3; sync;"		/* Load user ctx */
    608  1.1  simonb 		"stb %2,0(%4);  dcbf 0,%4; addi %4,%4,1;"	/* Store user byte */
    609  1.1  simonb 		"sync; isync;"
    610  1.1  simonb 		"b 1b;"				/* repeat */
    611  1.1  simonb 
    612  1.1  simonb 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    613  1.1  simonb 		"sync; isync;"
    614  1.1  simonb 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    615  1.1  simonb 		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
    616  1.1  simonb 
    617  1.1  simonb 	curpcb->pcb_onfault = 0;
    618  1.1  simonb 	return 0;
    619  1.1  simonb }
    620  1.1  simonb 
    621  1.1  simonb static int
    622  1.1  simonb bigcopyout(const void *kaddr, void *udaddr, size_t len)
    623  1.1  simonb {
    624  1.1  simonb 	char *up;
    625  1.1  simonb 	const char *kp = (char *)kaddr;
    626  1.1  simonb 	struct proc *p = curproc;
    627  1.1  simonb 	int error;
    628  1.1  simonb 
    629  1.1  simonb 	if (!p) {
    630  1.1  simonb 		return EFAULT;
    631  1.1  simonb 	}
    632  1.1  simonb 
    633  1.1  simonb 	/*
    634  1.1  simonb 	 * Stolen from physio():
    635  1.1  simonb 	 */
    636  1.1  simonb 	PHOLD(p);
    637  1.1  simonb 	error = uvm_vslock(p, udaddr, len, VM_PROT_READ|VM_PROT_WRITE);
    638  1.1  simonb 	if (error) {
    639  1.1  simonb 		PRELE(p);
    640  1.1  simonb 		return EFAULT;
    641  1.1  simonb 	}
    642  1.1  simonb 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    643  1.1  simonb 		VM_PROT_READ|VM_PROT_WRITE);
    644  1.1  simonb 
    645  1.1  simonb 	bcopy(kp, up, len);
    646  1.1  simonb 	vunmaprange((vaddr_t)up, len);
    647  1.1  simonb 	uvm_vsunlock(p, udaddr, len);
    648  1.1  simonb 	PRELE(p);
    649  1.1  simonb 
    650  1.1  simonb 	return 0;
    651  1.1  simonb }
    652  1.1  simonb 
    653  1.1  simonb /*
    654  1.1  simonb  * kcopy(const void *src, void *dst, size_t len);
    655  1.1  simonb  *
    656  1.1  simonb  * Copy len bytes from src to dst, aborting if we encounter a fatal
    657  1.1  simonb  * page fault.
    658  1.1  simonb  *
    659  1.1  simonb  * kcopy() _must_ save and restore the old fault handler since it is
    660  1.1  simonb  * called by uiomove(), which may be in the path of servicing a non-fatal
    661  1.1  simonb  * page fault.
    662  1.1  simonb  */
    663  1.1  simonb int
    664  1.1  simonb kcopy(const void *src, void *dst, size_t len)
    665  1.1  simonb {
    666  1.1  simonb 	faultbuf env, *oldfault;
    667  1.1  simonb 
    668  1.1  simonb 	oldfault = curpcb->pcb_onfault;
    669  1.1  simonb 	if (setfault(env)) {
    670  1.1  simonb 		curpcb->pcb_onfault = oldfault;
    671  1.1  simonb 		return EFAULT;
    672  1.1  simonb 	}
    673  1.1  simonb 
    674  1.1  simonb 	bcopy(src, dst, len);
    675  1.1  simonb 
    676  1.1  simonb 	curpcb->pcb_onfault = oldfault;
    677  1.1  simonb 	return 0;
    678  1.1  simonb }
    679  1.1  simonb 
    680  1.1  simonb int
    681  1.1  simonb badaddr(void *addr, size_t size)
    682  1.1  simonb {
    683  1.1  simonb 
    684  1.1  simonb 	return badaddr_read(addr, size, NULL);
    685  1.1  simonb }
    686  1.1  simonb 
    687  1.1  simonb int
    688  1.1  simonb badaddr_read(void *addr, size_t size, int *rptr)
    689  1.1  simonb {
    690  1.1  simonb 	faultbuf env;
    691  1.1  simonb 	int x;
    692  1.1  simonb 
    693  1.1  simonb 	/* Get rid of any stale machine checks that have been waiting.  */
    694  1.1  simonb 	__asm __volatile ("sync; isync");
    695  1.1  simonb 
    696  1.1  simonb 	if (setfault(env)) {
    697  1.1  simonb 		curpcb->pcb_onfault = 0;
    698  1.1  simonb 		__asm __volatile ("sync");
    699  1.1  simonb 		return 1;
    700  1.1  simonb 	}
    701  1.1  simonb 
    702  1.1  simonb 	__asm __volatile ("sync");
    703  1.1  simonb 
    704  1.1  simonb 	switch (size) {
    705  1.1  simonb 	case 1:
    706  1.1  simonb 		x = *(volatile int8_t *)addr;
    707  1.1  simonb 		break;
    708  1.1  simonb 	case 2:
    709  1.1  simonb 		x = *(volatile int16_t *)addr;
    710  1.1  simonb 		break;
    711  1.1  simonb 	case 4:
    712  1.1  simonb 		x = *(volatile int32_t *)addr;
    713  1.1  simonb 		break;
    714  1.1  simonb 	default:
    715  1.1  simonb 		panic("badaddr: invalid size (%d)", size);
    716  1.1  simonb 	}
    717  1.1  simonb 
    718  1.1  simonb 	/* Make sure we took the machine check, if we caused one. */
    719  1.1  simonb 	__asm __volatile ("sync; isync");
    720  1.1  simonb 
    721  1.1  simonb 	curpcb->pcb_onfault = 0;
    722  1.1  simonb 	__asm __volatile ("sync");	/* To be sure. */
    723  1.1  simonb 
    724  1.1  simonb 	/* Use the value to avoid reorder. */
    725  1.1  simonb 	if (rptr)
    726  1.1  simonb 		*rptr = x;
    727  1.1  simonb 
    728  1.1  simonb 	return 0;
    729  1.1  simonb }
    730  1.1  simonb 
    731  1.1  simonb /*
    732  1.1  simonb  * For now, this only deals with the particular unaligned access case
    733  1.1  simonb  * that gcc tends to generate.  Eventually it should handle all of the
    734  1.1  simonb  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    735  1.1  simonb  */
    736  1.1  simonb 
    737  1.1  simonb static int
    738  1.1  simonb fix_unaligned(struct proc *p, struct trapframe *frame)
    739  1.1  simonb {
    740  1.1  simonb 
    741  1.1  simonb 	return -1;
    742  1.1  simonb }
    743