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