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