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