Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.44
      1  1.44   garbled /*	$NetBSD: trap.c,v 1.44 2007/10/17 19:56:39 garbled 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.14     lukem 
     69  1.14     lukem #include <sys/cdefs.h>
     70  1.44   garbled __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.44 2007/10/17 19:56:39 garbled Exp $");
     71   1.1    simonb 
     72   1.1    simonb #include "opt_altivec.h"
     73   1.1    simonb #include "opt_ddb.h"
     74  1.44   garbled #include "opt_kgdb.h"
     75   1.1    simonb 
     76   1.1    simonb #include <sys/param.h>
     77   1.1    simonb #include <sys/proc.h>
     78   1.1    simonb #include <sys/reboot.h>
     79   1.1    simonb #include <sys/syscall.h>
     80   1.1    simonb #include <sys/systm.h>
     81   1.1    simonb #include <sys/user.h>
     82  1.10   thorpej #include <sys/pool.h>
     83  1.20        cl #include <sys/userret.h>
     84  1.34      yamt #include <sys/kauth.h>
     85   1.1    simonb 
     86  1.44   garbled #if defined(KGDB)
     87  1.44   garbled #include <sys/kgdb.h>
     88  1.44   garbled #endif
     89  1.44   garbled 
     90   1.1    simonb #include <uvm/uvm_extern.h>
     91   1.1    simonb 
     92   1.1    simonb #include <dev/cons.h>
     93   1.1    simonb 
     94   1.1    simonb #include <machine/cpu.h>
     95   1.1    simonb #include <machine/db_machdep.h>
     96   1.1    simonb #include <machine/fpu.h>
     97   1.1    simonb #include <machine/frame.h>
     98   1.1    simonb #include <machine/pcb.h>
     99   1.1    simonb #include <machine/psl.h>
    100   1.1    simonb #include <machine/trap.h>
    101   1.1    simonb 
    102   1.1    simonb #include <powerpc/spr.h>
    103   1.1    simonb #include <powerpc/ibm4xx/pmap.h>
    104   1.1    simonb #include <powerpc/ibm4xx/tlb.h>
    105   1.1    simonb #include <powerpc/fpu/fpu_extern.h>
    106   1.1    simonb 
    107   1.1    simonb /* These definitions should probably be somewhere else			XXX */
    108   1.1    simonb #define	FIRSTARG	3		/* first argument is in reg 3 */
    109   1.1    simonb #define	NARGREG		8		/* 8 args are in registers */
    110  1.40  christos #define	MOREARGS(sp)	((void *)((int)(sp) + 8)) /* more args go here */
    111   1.1    simonb 
    112  1.10   thorpej static int fix_unaligned __P((struct lwp *l, struct trapframe *frame));
    113   1.1    simonb 
    114   1.1    simonb void trap __P((struct trapframe *));	/* Called from locore / trap_subr */
    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.10   thorpej 	struct lwp *l = curlwp;
    132  1.10   thorpej 	struct proc *p = l ? l->l_proc : NULL;
    133   1.1    simonb 	int type = frame->exc;
    134   1.1    simonb 	int ftype, rv;
    135  1.18       eeh 	ksiginfo_t ksi;
    136   1.1    simonb 
    137  1.10   thorpej 	KASSERT(l == 0 || (l->l_stat == LSONPROC));
    138   1.1    simonb 
    139  1.35        ad 	if (frame->srr1 & PSL_PR) {
    140  1.35        ad 		LWP_CACHE_CREDS(l, p);
    141   1.1    simonb 		type |= EXC_USER;
    142  1.35        ad 	}
    143   1.1    simonb 
    144   1.1    simonb 	ftype = VM_PROT_READ;
    145   1.1    simonb 
    146  1.13    simonb 	DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
    147  1.13    simonb 	    type, frame->srr0, frame, &frame));
    148   1.1    simonb 
    149   1.1    simonb 	switch (type) {
    150   1.1    simonb 	case EXC_DEBUG|EXC_USER:
    151  1.13    simonb 		{
    152  1.13    simonb 			int srr2, srr3;
    153  1.13    simonb 
    154  1.28     perry 			__asm volatile("mfspr %0,0x3f0" :
    155  1.13    simonb 			    "=r" (rv), "=r" (srr2), "=r" (srr3) :);
    156  1.13    simonb 			printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
    157  1.13    simonb 			    srr3);
    158  1.13    simonb 			/* XXX fall through or break here?! */
    159  1.13    simonb 		}
    160   1.1    simonb 		/*
    161   1.1    simonb 		 * DEBUG intr -- probably single-step.
    162   1.1    simonb 		 */
    163   1.1    simonb 	case EXC_TRC|EXC_USER:
    164  1.17      matt 		frame->srr1 &= ~PSL_SE;
    165  1.19   thorpej 		KSI_INIT_TRAP(&ksi);
    166  1.17      matt 		ksi.ksi_signo = SIGTRAP;
    167  1.17      matt 		ksi.ksi_trap = EXC_TRC;
    168  1.17      matt 		ksi.ksi_addr = (void *)frame->srr0;
    169  1.39        ad 		KERNEL_LOCK(1, l);
    170  1.17      matt 		trapsignal(l, &ksi);
    171  1.39        ad 		KERNEL_UNLOCK_LAST(l);
    172   1.1    simonb 		break;
    173   1.7    simonb 
    174  1.13    simonb 	/*
    175  1.13    simonb 	 * If we could not find and install appropriate TLB entry, fall through.
    176  1.13    simonb 	 */
    177   1.7    simonb 
    178   1.1    simonb 	case EXC_DSI:
    179   1.1    simonb 		/* FALLTHROUGH */
    180   1.1    simonb 	case EXC_DTMISS:
    181   1.1    simonb 		{
    182   1.1    simonb 			struct vm_map *map;
    183   1.1    simonb 			vaddr_t va;
    184  1.11      matt 			struct faultbuf *fb = NULL;
    185   1.1    simonb 
    186  1.39        ad 			KERNEL_LOCK(1, NULL);
    187  1.11      matt 			va = frame->dar;
    188  1.11      matt 			if (frame->tf_xtra[TF_PID] == KERNEL_PID) {
    189   1.1    simonb 				map = kernel_map;
    190   1.1    simonb 			} else {
    191   1.1    simonb 				map = &p->p_vmspace->vm_map;
    192   1.1    simonb 			}
    193   1.1    simonb 
    194  1.11      matt 			if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
    195   1.3       chs 				ftype = VM_PROT_WRITE;
    196   1.1    simonb 
    197  1.13    simonb 			DBPRINTF(TDB_ALL,
    198  1.13    simonb 			    ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
    199  1.13    simonb 			    frame->srr0,
    200  1.13    simonb 			    (ftype & VM_PROT_WRITE) ? "write" : "read",
    201  1.13    simonb 			    (void *)va, frame->tf_xtra[TF_ESR]));
    202  1.32  drochner 			rv = uvm_fault(map, trunc_page(va), ftype);
    203  1.39        ad 			KERNEL_UNLOCK_ONE(NULL);
    204   1.1    simonb 			if (rv == 0)
    205   1.1    simonb 				goto done;
    206  1.10   thorpej 			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
    207  1.11      matt 				frame->tf_xtra[TF_PID] = KERNEL_PID;
    208  1.11      matt 				frame->srr0 = fb->fb_pc;
    209   1.1    simonb 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    210  1.11      matt 				frame->fixreg[1] = fb->fb_sp;
    211  1.11      matt 				frame->fixreg[2] = fb->fb_r2;
    212   1.1    simonb 				frame->fixreg[3] = 1; /* Return TRUE */
    213  1.11      matt 				frame->cr = fb->fb_cr;
    214  1.11      matt 				memcpy(&frame->fixreg[13], fb->fb_fixreg,
    215  1.11      matt 				    sizeof(fb->fb_fixreg));
    216   1.1    simonb 				goto done;
    217   1.1    simonb 			}
    218   1.1    simonb 		}
    219   1.1    simonb 		goto brain_damage;
    220   1.7    simonb 
    221   1.1    simonb 	case EXC_DSI|EXC_USER:
    222   1.1    simonb 		/* FALLTHROUGH */
    223   1.1    simonb 	case EXC_DTMISS|EXC_USER:
    224  1.39        ad 		KERNEL_LOCK(1, l);
    225   1.7    simonb 
    226  1.11      matt 		if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
    227   1.3       chs 			ftype = VM_PROT_WRITE;
    228   1.1    simonb 
    229  1.13    simonb 		DBPRINTF(TDB_ALL,
    230  1.13    simonb 		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
    231  1.13    simonb 		    frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
    232  1.13    simonb 		    frame->dar, frame->tf_xtra[TF_ESR]));
    233  1.13    simonb 		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
    234  1.13    simonb 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->dar),
    235  1.32  drochner 		    ftype);
    236   1.1    simonb 		if (rv == 0) {
    237  1.39        ad 			KERNEL_UNLOCK_LAST(l);
    238  1.13    simonb 			break;
    239   1.1    simonb 		}
    240  1.19   thorpej 		KSI_INIT_TRAP(&ksi);
    241  1.17      matt 		ksi.ksi_signo = SIGSEGV;
    242  1.17      matt 		ksi.ksi_trap = EXC_DSI;
    243  1.17      matt 		ksi.ksi_addr = (void *)frame->dar;
    244   1.1    simonb 		if (rv == ENOMEM) {
    245  1.10   thorpej 			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
    246  1.13    simonb 			    "out of swap\n",
    247  1.13    simonb 			    p->p_pid, p->p_comm, l->l_lid,
    248  1.36        ad 			    l->l_cred ?
    249  1.36        ad 			    kauth_cred_geteuid(l->l_cred) : -1);
    250  1.17      matt 			ksi.ksi_signo = SIGKILL;
    251   1.1    simonb 		}
    252  1.17      matt 		trapsignal(l, &ksi);
    253  1.39        ad 		KERNEL_UNLOCK_LAST(l);
    254   1.1    simonb 		break;
    255  1.15       chs 
    256   1.1    simonb 	case EXC_ITMISS|EXC_USER:
    257   1.1    simonb 	case EXC_ISI|EXC_USER:
    258  1.39        ad 		KERNEL_LOCK(1, l);
    259  1.15       chs 		ftype = VM_PROT_EXECUTE;
    260  1.13    simonb 		DBPRINTF(TDB_ALL,
    261  1.15       chs 		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
    262  1.13    simonb 		    frame->srr0, frame));
    263  1.13    simonb 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0),
    264  1.32  drochner 		    ftype);
    265   1.1    simonb 		if (rv == 0) {
    266  1.39        ad 			KERNEL_UNLOCK_LAST(l);
    267  1.13    simonb 			break;
    268   1.1    simonb 		}
    269  1.19   thorpej 		KSI_INIT_TRAP(&ksi);
    270  1.17      matt 		ksi.ksi_signo = SIGSEGV;
    271  1.17      matt 		ksi.ksi_trap = EXC_ISI;
    272  1.17      matt 		ksi.ksi_addr = (void *)frame->srr0;
    273  1.21  drochner 		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
    274  1.17      matt 		trapsignal(l, &ksi);
    275  1.39        ad 		KERNEL_UNLOCK_LAST(l);
    276   1.1    simonb 		break;
    277   1.1    simonb 
    278   1.1    simonb 	case EXC_AST|EXC_USER:
    279  1.11      matt 		curcpu()->ci_astpending = 0;	/* we are about to do it */
    280   1.1    simonb 		uvmexp.softs++;
    281  1.39        ad 		if (l->l_pflag & LP_OWEUPC) {
    282  1.39        ad 			l->l_pflag &= ~LP_OWEUPC;
    283  1.39        ad 			ADDUPROF(l);
    284   1.1    simonb 		}
    285   1.1    simonb 		/* Check whether we are being preempted. */
    286  1.42       rjs 		if (curcpu()->ci_want_resched)
    287  1.38        ad 			preempt();
    288   1.1    simonb 		break;
    289   1.1    simonb 
    290   1.1    simonb 
    291   1.1    simonb 	case EXC_ALI|EXC_USER:
    292  1.39        ad 		KERNEL_LOCK(1, l);
    293  1.17      matt 		if (fix_unaligned(l, frame) != 0) {
    294  1.19   thorpej 			KSI_INIT_TRAP(&ksi);
    295  1.17      matt 			ksi.ksi_signo = SIGBUS;
    296  1.17      matt 			ksi.ksi_trap = EXC_ALI;
    297  1.17      matt 			ksi.ksi_addr = (void *)frame->dar;
    298  1.17      matt 			trapsignal(l, &ksi);
    299  1.17      matt 		} else
    300   1.1    simonb 			frame->srr0 += 4;
    301  1.39        ad 		KERNEL_UNLOCK_LAST(l);
    302   1.1    simonb 		break;
    303   1.1    simonb 
    304   1.1    simonb 	case EXC_PGM|EXC_USER:
    305   1.7    simonb 		/*
    306   1.7    simonb 		 * Illegal insn:
    307   1.1    simonb 		 *
    308   1.7    simonb 		 * let's try to see if it's FPU and can be emulated.
    309   1.1    simonb 		 */
    310  1.24    simonb 		uvmexp.traps++;
    311  1.10   thorpej 		if (!(l->l_addr->u_pcb.pcb_flags & PCB_FPU)) {
    312  1.10   thorpej 			memset(&l->l_addr->u_pcb.pcb_fpu, 0,
    313  1.10   thorpej 				sizeof l->l_addr->u_pcb.pcb_fpu);
    314  1.10   thorpej 			l->l_addr->u_pcb.pcb_flags |= PCB_FPU;
    315   1.1    simonb 		}
    316   1.1    simonb 
    317   1.7    simonb 		if ((rv = fpu_emulate(frame,
    318  1.10   thorpej 			(struct fpreg *)&l->l_addr->u_pcb.pcb_fpu))) {
    319  1.19   thorpej 			KSI_INIT_TRAP(&ksi);
    320  1.17      matt 			ksi.ksi_signo = rv;
    321  1.17      matt 			ksi.ksi_trap = EXC_PGM;
    322  1.17      matt 			ksi.ksi_addr = (void *)frame->srr0;
    323  1.39        ad 			KERNEL_LOCK(1, l);
    324  1.17      matt 			trapsignal(l, &ksi);
    325  1.39        ad 			KERNEL_UNLOCK_LAST(l);
    326   1.1    simonb 		}
    327   1.1    simonb 		break;
    328   1.1    simonb 
    329   1.1    simonb 	case EXC_MCHK:
    330   1.1    simonb 		{
    331  1.11      matt 			struct faultbuf *fb;
    332   1.1    simonb 
    333  1.10   thorpej 			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
    334  1.11      matt 				frame->tf_xtra[TF_PID] = KERNEL_PID;
    335  1.11      matt 				frame->srr0 = fb->fb_pc;
    336   1.1    simonb 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    337  1.11      matt 				frame->fixreg[1] = fb->fb_sp;
    338  1.11      matt 				frame->fixreg[2] = fb->fb_r2;
    339   1.1    simonb 				frame->fixreg[3] = 1; /* Return TRUE */
    340  1.11      matt 				frame->cr = fb->fb_cr;
    341  1.11      matt 				memcpy(&frame->fixreg[13], fb->fb_fixreg,
    342  1.11      matt 				    sizeof(fb->fb_fixreg));
    343   1.1    simonb 				goto done;
    344   1.1    simonb 			}
    345   1.1    simonb 		}
    346   1.1    simonb 		goto brain_damage;
    347   1.1    simonb 	default:
    348  1.13    simonb  brain_damage:
    349  1.11      matt 		printf("trap type 0x%x at 0x%lx\n", type, frame->srr0);
    350  1.44   garbled #if defined(DDB) || defined(KGDB)
    351   1.1    simonb 		if (kdb_trap(type, frame))
    352   1.1    simonb 			goto done;
    353   1.1    simonb #endif
    354   1.1    simonb #ifdef TRAP_PANICWAIT
    355   1.1    simonb 		printf("Press a key to panic.\n");
    356   1.1    simonb 		cngetc();
    357   1.1    simonb #endif
    358   1.1    simonb 		panic("trap");
    359   1.1    simonb 	}
    360   1.1    simonb 
    361  1.20        cl 	/* Invoke MI userret code */
    362  1.20        cl 	mi_userret(l);
    363  1.10   thorpej 
    364  1.10   thorpej 	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
    365  1.13    simonb  done:
    366   1.9   thorpej 	return;
    367   1.1    simonb }
    368   1.1    simonb 
    369   1.1    simonb int
    370   1.1    simonb ctx_setup(int ctx, int srr1)
    371   1.1    simonb {
    372   1.1    simonb 	volatile struct pmap *pm;
    373   1.1    simonb 
    374   1.1    simonb 	/* Update PID if we're returning to user mode. */
    375   1.1    simonb 	if (srr1 & PSL_PR) {
    376   1.1    simonb 		pm = curproc->p_vmspace->vm_map.pmap;
    377   1.1    simonb 		if (!pm->pm_ctx) {
    378  1.26       scw 			ctx_alloc(__UNVOLATILE(pm));
    379   1.1    simonb 		}
    380   1.1    simonb 		ctx = pm->pm_ctx;
    381   1.1    simonb 		if (srr1 & PSL_SE) {
    382   1.1    simonb 			int dbreg, mask = 0x48000000;
    383   1.1    simonb 				/*
    384   1.1    simonb 				 * Set the Internal Debug and
    385   1.1    simonb 				 * Instruction Completion bits of
    386   1.1    simonb 				 * the DBCR0 register.
    387   1.1    simonb 				 *
    388   1.1    simonb 				 * XXX this is also used by jtag debuggers...
    389   1.1    simonb 				 */
    390  1.28     perry 			__asm volatile("mfspr %0,0x3f2;"
    391  1.13    simonb 			    "or %0,%0,%1;"
    392  1.13    simonb 			    "mtspr 0x3f2,%0;" :
    393  1.13    simonb 			    "=&r" (dbreg) : "r" (mask));
    394   1.1    simonb 		}
    395   1.1    simonb 	}
    396   1.1    simonb 	else if (!ctx) {
    397   1.1    simonb 		ctx = KERNEL_PID;
    398   1.1    simonb 	}
    399   1.1    simonb 	return (ctx);
    400   1.1    simonb }
    401   1.1    simonb 
    402   1.1    simonb /*
    403   1.1    simonb  * Used by copyin()/copyout()
    404   1.1    simonb  */
    405   1.1    simonb extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
    406   1.1    simonb extern void vunmaprange __P((vaddr_t, vsize_t));
    407  1.13    simonb static int bigcopyin __P((const void *, void *, size_t ));
    408   1.1    simonb static int bigcopyout __P((const void *, void *, size_t ));
    409   1.1    simonb 
    410   1.1    simonb int
    411   1.1    simonb copyin(const void *udaddr, void *kaddr, size_t len)
    412   1.1    simonb {
    413   1.1    simonb 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    414   1.1    simonb 	int msr, pid, tmp, ctx;
    415  1.11      matt 	struct faultbuf env;
    416   1.1    simonb 
    417   1.1    simonb 	/* For bigger buffers use the faster copy */
    418  1.25    simonb 	if (len > 256)
    419  1.25    simonb 		return (bigcopyin(udaddr, kaddr, len));
    420   1.1    simonb 
    421  1.11      matt 	if (setfault(&env)) {
    422   1.1    simonb 		curpcb->pcb_onfault = 0;
    423   1.1    simonb 		return EFAULT;
    424   1.1    simonb 	}
    425   1.1    simonb 
    426   1.1    simonb 	if (!(ctx = pm->pm_ctx)) {
    427   1.1    simonb 		/* No context -- assign it one */
    428   1.1    simonb 		ctx_alloc(pm);
    429   1.1    simonb 		ctx = pm->pm_ctx;
    430   1.1    simonb 	}
    431   1.1    simonb 
    432  1.29     perry 	__asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    433   1.1    simonb 		"mfmsr %0;"			/* Save MSR */
    434   1.1    simonb 		"li %1,0x20; "
    435   1.1    simonb 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    436   1.1    simonb 		"mfpid %1;"			/* Save old PID */
    437   1.1    simonb 		"sync; isync;"
    438   1.1    simonb 
    439   1.1    simonb 		"1: bdz 2f;"			/* while len */
    440   1.1    simonb 		"mtpid %3; sync;"		/* Load user ctx */
    441   1.1    simonb 		"lbz %2,0(%4); addi %4,%4,1;"	/* Load byte */
    442   1.1    simonb 		"sync; isync;"
    443   1.1    simonb 		"mtpid %1;sync;"
    444  1.13    simonb 		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */
    445   1.1    simonb 		"sync; isync;"
    446   1.1    simonb 		"b 1b;"				/* repeat */
    447   1.1    simonb 
    448   1.1    simonb 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    449   1.1    simonb 		"sync; isync;"
    450   1.1    simonb 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    451  1.23       scw 		: "r" (ctx), "b" (udaddr), "b" (kaddr), "b" (len));
    452   1.1    simonb 
    453   1.1    simonb 	curpcb->pcb_onfault = 0;
    454   1.1    simonb 	return 0;
    455   1.1    simonb }
    456   1.1    simonb 
    457   1.1    simonb static int
    458   1.1    simonb bigcopyin(const void *udaddr, void *kaddr, size_t len)
    459   1.1    simonb {
    460   1.1    simonb 	const char *up;
    461   1.1    simonb 	char *kp = kaddr;
    462  1.10   thorpej 	struct lwp *l = curlwp;
    463  1.10   thorpej 	struct proc *p;
    464   1.1    simonb 	int error;
    465   1.1    simonb 
    466  1.10   thorpej 	if (!l) {
    467   1.1    simonb 		return EFAULT;
    468   1.1    simonb 	}
    469   1.1    simonb 
    470  1.10   thorpej 	p = l->l_proc;
    471  1.10   thorpej 
    472   1.1    simonb 	/*
    473   1.7    simonb 	 * Stolen from physio():
    474   1.1    simonb 	 */
    475  1.43        ad 	uvm_lwp_hold(l);
    476  1.37       chs 	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
    477   1.1    simonb 	if (error) {
    478  1.43        ad 		uvm_lwp_rele(l);
    479   1.1    simonb 		return EFAULT;
    480   1.1    simonb 	}
    481   1.1    simonb 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    482   1.1    simonb 
    483   1.2       wiz 	memcpy(kp, up, len);
    484   1.1    simonb 	vunmaprange((vaddr_t)up, len);
    485  1.37       chs 	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
    486  1.43        ad 	uvm_lwp_rele(l);
    487   1.1    simonb 
    488   1.7    simonb 	return 0;
    489   1.1    simonb }
    490   1.1    simonb 
    491   1.1    simonb int
    492   1.1    simonb copyout(const void *kaddr, void *udaddr, size_t len)
    493   1.1    simonb {
    494   1.1    simonb 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    495   1.1    simonb 	int msr, pid, tmp, ctx;
    496  1.11      matt 	struct faultbuf env;
    497   1.1    simonb 
    498   1.1    simonb 	/* For big copies use more efficient routine */
    499  1.25    simonb 	if (len > 256)
    500  1.25    simonb 		return (bigcopyout(kaddr, udaddr, len));
    501   1.1    simonb 
    502  1.11      matt 	if (setfault(&env)) {
    503   1.1    simonb 		curpcb->pcb_onfault = 0;
    504   1.1    simonb 		return EFAULT;
    505   1.1    simonb 	}
    506   1.1    simonb 
    507   1.1    simonb 	if (!(ctx = pm->pm_ctx)) {
    508   1.1    simonb 		/* No context -- assign it one */
    509   1.1    simonb 		ctx_alloc(pm);
    510   1.1    simonb 		ctx = pm->pm_ctx;
    511   1.1    simonb 	}
    512   1.1    simonb 
    513  1.29     perry 	__asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    514   1.1    simonb 		"mfmsr %0;"			/* Save MSR */
    515   1.1    simonb 		"li %1,0x20; "
    516   1.1    simonb 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    517   1.1    simonb 		"mfpid %1;"			/* Save old PID */
    518   1.1    simonb 		"sync; isync;"
    519   1.1    simonb 
    520   1.1    simonb 		"1: bdz 2f;"			/* while len */
    521   1.1    simonb 		"mtpid %1;sync;"
    522   1.1    simonb 		"lbz %2,0(%5); addi %5,%5,1;"	/* Load kernel byte */
    523   1.1    simonb 		"sync; isync;"
    524   1.1    simonb 		"mtpid %3; sync;"		/* Load user ctx */
    525  1.13    simonb 		"stb %2,0(%4);  dcbf 0,%4; addi %4,%4,1;" /* Store user byte */
    526   1.1    simonb 		"sync; isync;"
    527   1.1    simonb 		"b 1b;"				/* repeat */
    528   1.1    simonb 
    529   1.1    simonb 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    530   1.1    simonb 		"sync; isync;"
    531   1.1    simonb 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    532  1.23       scw 		: "r" (ctx), "b" (udaddr), "b" (kaddr), "b" (len));
    533   1.1    simonb 
    534   1.1    simonb 	curpcb->pcb_onfault = 0;
    535   1.1    simonb 	return 0;
    536   1.1    simonb }
    537   1.1    simonb 
    538   1.1    simonb static int
    539   1.1    simonb bigcopyout(const void *kaddr, void *udaddr, size_t len)
    540   1.1    simonb {
    541   1.1    simonb 	char *up;
    542  1.26       scw 	const char *kp = (const char *)kaddr;
    543  1.10   thorpej 	struct lwp *l = curlwp;
    544  1.10   thorpej 	struct proc *p;
    545   1.1    simonb 	int error;
    546   1.1    simonb 
    547  1.10   thorpej 	if (!l) {
    548   1.1    simonb 		return EFAULT;
    549   1.1    simonb 	}
    550   1.1    simonb 
    551  1.10   thorpej 	p = l->l_proc;
    552  1.10   thorpej 
    553   1.1    simonb 	/*
    554   1.7    simonb 	 * Stolen from physio():
    555   1.1    simonb 	 */
    556  1.43        ad 	uvm_lwp_hold(l);
    557  1.37       chs 	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
    558   1.1    simonb 	if (error) {
    559  1.43        ad 		uvm_lwp_rele(l);
    560   1.1    simonb 		return EFAULT;
    561   1.1    simonb 	}
    562   1.7    simonb 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    563  1.13    simonb 	    VM_PROT_READ | VM_PROT_WRITE);
    564   1.1    simonb 
    565   1.2       wiz 	memcpy(up, kp, len);
    566   1.1    simonb 	vunmaprange((vaddr_t)up, len);
    567  1.37       chs 	uvm_vsunlock(p->p_vmspace, udaddr, len);
    568  1.43        ad 	uvm_lwp_rele(l);
    569   1.1    simonb 
    570   1.7    simonb 	return 0;
    571   1.1    simonb }
    572   1.1    simonb 
    573   1.1    simonb /*
    574   1.1    simonb  * kcopy(const void *src, void *dst, size_t len);
    575   1.1    simonb  *
    576   1.1    simonb  * Copy len bytes from src to dst, aborting if we encounter a fatal
    577   1.1    simonb  * page fault.
    578   1.1    simonb  *
    579   1.1    simonb  * kcopy() _must_ save and restore the old fault handler since it is
    580   1.1    simonb  * called by uiomove(), which may be in the path of servicing a non-fatal
    581   1.1    simonb  * page fault.
    582   1.1    simonb  */
    583   1.1    simonb int
    584   1.1    simonb kcopy(const void *src, void *dst, size_t len)
    585   1.1    simonb {
    586  1.11      matt 	struct faultbuf env, *oldfault;
    587   1.1    simonb 
    588   1.1    simonb 	oldfault = curpcb->pcb_onfault;
    589  1.11      matt 	if (setfault(&env)) {
    590   1.1    simonb 		curpcb->pcb_onfault = oldfault;
    591   1.1    simonb 		return EFAULT;
    592   1.1    simonb 	}
    593   1.1    simonb 
    594   1.2       wiz 	memcpy(dst, src, len);
    595   1.1    simonb 
    596   1.1    simonb 	curpcb->pcb_onfault = oldfault;
    597   1.1    simonb 	return 0;
    598   1.1    simonb }
    599   1.1    simonb 
    600   1.1    simonb int
    601   1.1    simonb badaddr(void *addr, size_t size)
    602   1.1    simonb {
    603   1.1    simonb 
    604   1.1    simonb 	return badaddr_read(addr, size, NULL);
    605   1.1    simonb }
    606   1.1    simonb 
    607   1.1    simonb int
    608   1.1    simonb badaddr_read(void *addr, size_t size, int *rptr)
    609   1.1    simonb {
    610  1.11      matt 	struct faultbuf env;
    611   1.1    simonb 	int x;
    612   1.1    simonb 
    613   1.1    simonb 	/* Get rid of any stale machine checks that have been waiting.  */
    614  1.28     perry 	__asm volatile ("sync; isync");
    615   1.1    simonb 
    616  1.11      matt 	if (setfault(&env)) {
    617   1.1    simonb 		curpcb->pcb_onfault = 0;
    618  1.28     perry 		__asm volatile ("sync");
    619   1.1    simonb 		return 1;
    620   1.1    simonb 	}
    621   1.1    simonb 
    622  1.28     perry 	__asm volatile ("sync");
    623   1.1    simonb 
    624   1.1    simonb 	switch (size) {
    625   1.1    simonb 	case 1:
    626   1.1    simonb 		x = *(volatile int8_t *)addr;
    627   1.1    simonb 		break;
    628   1.1    simonb 	case 2:
    629   1.1    simonb 		x = *(volatile int16_t *)addr;
    630   1.1    simonb 		break;
    631   1.1    simonb 	case 4:
    632   1.1    simonb 		x = *(volatile int32_t *)addr;
    633   1.1    simonb 		break;
    634   1.1    simonb 	default:
    635   1.1    simonb 		panic("badaddr: invalid size (%d)", size);
    636   1.1    simonb 	}
    637   1.1    simonb 
    638   1.1    simonb 	/* Make sure we took the machine check, if we caused one. */
    639  1.28     perry 	__asm volatile ("sync; isync");
    640   1.1    simonb 
    641   1.1    simonb 	curpcb->pcb_onfault = 0;
    642  1.28     perry 	__asm volatile ("sync");	/* To be sure. */
    643   1.1    simonb 
    644   1.1    simonb 	/* Use the value to avoid reorder. */
    645   1.1    simonb 	if (rptr)
    646   1.1    simonb 		*rptr = x;
    647   1.1    simonb 
    648   1.1    simonb 	return 0;
    649   1.1    simonb }
    650   1.1    simonb 
    651   1.1    simonb /*
    652   1.1    simonb  * For now, this only deals with the particular unaligned access case
    653   1.1    simonb  * that gcc tends to generate.  Eventually it should handle all of the
    654   1.1    simonb  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    655   1.1    simonb  */
    656   1.1    simonb 
    657   1.1    simonb static int
    658  1.10   thorpej fix_unaligned(struct lwp *l, struct trapframe *frame)
    659   1.1    simonb {
    660   1.1    simonb 
    661   1.1    simonb 	return -1;
    662  1.10   thorpej }
    663  1.10   thorpej 
    664  1.10   thorpej /*
    665  1.10   thorpej  * Start a new LWP
    666  1.10   thorpej  */
    667  1.10   thorpej void
    668  1.10   thorpej startlwp(arg)
    669  1.10   thorpej 	void *arg;
    670  1.10   thorpej {
    671  1.10   thorpej 	int err;
    672  1.10   thorpej 	ucontext_t *uc = arg;
    673  1.10   thorpej 	struct lwp *l = curlwp;
    674  1.10   thorpej 
    675  1.10   thorpej 	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
    676  1.10   thorpej #if DIAGNOSTIC
    677  1.10   thorpej 	if (err) {
    678  1.10   thorpej 		printf("Error %d from cpu_setmcontext.", err);
    679  1.10   thorpej 	}
    680  1.10   thorpej #endif
    681  1.10   thorpej 	pool_put(&lwp_uc_pool, uc);
    682  1.10   thorpej 
    683  1.20        cl 	/* Invoke MI userret code */
    684  1.20        cl 	mi_userret(l);
    685  1.10   thorpej 
    686  1.10   thorpej 	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
    687   1.1    simonb }
    688