Home | History | Annotate | Line # | Download | only in booke
trap.c revision 1.5.2.2
      1  1.5.2.2  rmind /*	$NetBSD: trap.c,v 1.5.2.2 2011/03/05 20:51:34 rmind Exp $	*/
      2  1.5.2.2  rmind /*-
      3  1.5.2.2  rmind  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.5.2.2  rmind  * All rights reserved.
      5  1.5.2.2  rmind  *
      6  1.5.2.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
      7  1.5.2.2  rmind  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.5.2.2  rmind  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.5.2.2  rmind  *
     10  1.5.2.2  rmind  * This material is based upon work supported by the Defense Advanced Research
     11  1.5.2.2  rmind  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.5.2.2  rmind  * Contract No. N66001-09-C-2073.
     13  1.5.2.2  rmind  * Approved for Public Release, Distribution Unlimited
     14  1.5.2.2  rmind  *
     15  1.5.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     16  1.5.2.2  rmind  * modification, are permitted provided that the following conditions
     17  1.5.2.2  rmind  * are met:
     18  1.5.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     19  1.5.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     20  1.5.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.5.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     22  1.5.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     23  1.5.2.2  rmind  *
     24  1.5.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.5.2.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.5.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.5.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.5.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.5.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.5.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.5.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.5.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.5.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.5.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     35  1.5.2.2  rmind  */
     36  1.5.2.2  rmind 
     37  1.5.2.2  rmind #include "opt_ddb.h"
     38  1.5.2.2  rmind #include "opt_sa.h"
     39  1.5.2.2  rmind 
     40  1.5.2.2  rmind #include <sys/cdefs.h>
     41  1.5.2.2  rmind 
     42  1.5.2.2  rmind __KERNEL_RCSID(1, "$NetBSD: trap.c,v 1.5.2.2 2011/03/05 20:51:34 rmind Exp $");
     43  1.5.2.2  rmind 
     44  1.5.2.2  rmind #include <sys/param.h>
     45  1.5.2.2  rmind #include <sys/systm.h>
     46  1.5.2.2  rmind #include <sys/siginfo.h>
     47  1.5.2.2  rmind #include <sys/lwp.h>
     48  1.5.2.2  rmind #include <sys/proc.h>
     49  1.5.2.2  rmind #include <sys/cpu.h>
     50  1.5.2.2  rmind #ifdef KERN_SA
     51  1.5.2.2  rmind #include <sys/savar.h>
     52  1.5.2.2  rmind #endif
     53  1.5.2.2  rmind #include <sys/kauth.h>
     54  1.5.2.2  rmind #include <sys/kmem.h>
     55  1.5.2.2  rmind #include <sys/ras.h>
     56  1.5.2.2  rmind 
     57  1.5.2.2  rmind #include <uvm/uvm_extern.h>
     58  1.5.2.2  rmind 
     59  1.5.2.2  rmind #include <powerpc/pcb.h>
     60  1.5.2.2  rmind #include <powerpc/userret.h>
     61  1.5.2.2  rmind #include <powerpc/psl.h>
     62  1.5.2.2  rmind #include <powerpc/instr.h>
     63  1.5.2.2  rmind #include <powerpc/altivec.h>		/* use same interface for SPE */
     64  1.5.2.2  rmind 
     65  1.5.2.2  rmind #include <powerpc/spr.h>
     66  1.5.2.2  rmind #include <powerpc/booke/spr.h>
     67  1.5.2.2  rmind #include <powerpc/booke/cpuvar.h>
     68  1.5.2.2  rmind 
     69  1.5.2.2  rmind #include <powerpc/db_machdep.h>
     70  1.5.2.2  rmind #include <ddb/db_interface.h>
     71  1.5.2.2  rmind 
     72  1.5.2.2  rmind #include <powerpc/trap.h>
     73  1.5.2.2  rmind #include <powerpc/booke/trap.h>
     74  1.5.2.2  rmind #include <powerpc/booke/pte.h>
     75  1.5.2.2  rmind 
     76  1.5.2.2  rmind void trap(enum ppc_booke_exceptions, struct trapframe *);
     77  1.5.2.2  rmind static void dump_trapframe(const struct trapframe *);
     78  1.5.2.2  rmind 
     79  1.5.2.2  rmind static const char trap_names[][8] = {
     80  1.5.2.2  rmind 	[T_CRITIAL_INPUT] = "CRIT",
     81  1.5.2.2  rmind 	[T_EXTERNAL_INPUT] = "EXT",
     82  1.5.2.2  rmind 	[T_DECREMENTER] = "DECR",
     83  1.5.2.2  rmind 	[T_FIXED_INTERVAL] = "FIT",
     84  1.5.2.2  rmind 	[T_WATCHDOG] = "WDOG",
     85  1.5.2.2  rmind 	[T_SYSTEM_CALL] = "SC",
     86  1.5.2.2  rmind 	[T_MACHINE_CHECK] = "MCHK",
     87  1.5.2.2  rmind 	[T_DSI] = "DSI",
     88  1.5.2.2  rmind 	[T_ISI] = "ISI",
     89  1.5.2.2  rmind 	[T_ALIGNMENT] = "ALN",
     90  1.5.2.2  rmind 	[T_PROGRAM] = "PGM",
     91  1.5.2.2  rmind 	[T_FP_UNAVAILABLE] = "FP",
     92  1.5.2.2  rmind 	[T_AP_UNAVAILABLE] = "AP",
     93  1.5.2.2  rmind 	[T_DATA_TLB_ERROR] = "DTLB",
     94  1.5.2.2  rmind 	[T_INSTRUCTION_TLB_ERROR] = "ITLB",
     95  1.5.2.2  rmind 	[T_DEBUG] = "DEBUG",
     96  1.5.2.2  rmind 	[T_SPE_UNAVAILABLE] = "SPE",
     97  1.5.2.2  rmind 	[T_EMBEDDED_FP_DATA] = "FPDATA",
     98  1.5.2.2  rmind 	[T_EMBEDDED_FP_ROUND] = "FPROUND",
     99  1.5.2.2  rmind 	[T_EMBEDDED_PERF_MONITOR] = "PERFMON",
    100  1.5.2.2  rmind 	[T_AST] = "AST",
    101  1.5.2.2  rmind };
    102  1.5.2.2  rmind 
    103  1.5.2.2  rmind static inline bool
    104  1.5.2.2  rmind usertrap_p(struct trapframe *tf)
    105  1.5.2.2  rmind {
    106  1.5.2.2  rmind 	return (tf->tf_srr1 & PSL_PR) != 0;
    107  1.5.2.2  rmind }
    108  1.5.2.2  rmind 
    109  1.5.2.2  rmind static int
    110  1.5.2.2  rmind mchk_exception(struct trapframe *tf, ksiginfo_t *ksi)
    111  1.5.2.2  rmind {
    112  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    113  1.5.2.2  rmind 	const vaddr_t faultva = tf->tf_mcar;
    114  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    115  1.5.2.2  rmind 	int rv = EFAULT;
    116  1.5.2.2  rmind 
    117  1.5.2.2  rmind 	if (usertrap)
    118  1.5.2.2  rmind 		ci->ci_ev_umchk.ev_count++;
    119  1.5.2.2  rmind 
    120  1.5.2.2  rmind 	if (rv != 0 && usertrap) {
    121  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    122  1.5.2.2  rmind 		ksi->ksi_signo = SIGSEGV;
    123  1.5.2.2  rmind 		ksi->ksi_trap = EXC_DSI;
    124  1.5.2.2  rmind 		ksi->ksi_code = SEGV_ACCERR;
    125  1.5.2.2  rmind 		ksi->ksi_addr = (void *)faultva;
    126  1.5.2.2  rmind 	}
    127  1.5.2.2  rmind 
    128  1.5.2.2  rmind 	return rv;
    129  1.5.2.2  rmind }
    130  1.5.2.2  rmind 
    131  1.5.2.2  rmind static inline vm_prot_t
    132  1.5.2.2  rmind get_faulttype(const struct trapframe * const tf)
    133  1.5.2.2  rmind {
    134  1.5.2.2  rmind 	return VM_PROT_READ | (tf->tf_esr & ESR_ST ? VM_PROT_WRITE : 0);
    135  1.5.2.2  rmind }
    136  1.5.2.2  rmind 
    137  1.5.2.2  rmind static inline struct vm_map *
    138  1.5.2.2  rmind get_faultmap(const struct trapframe * const tf, register_t psl_mask)
    139  1.5.2.2  rmind {
    140  1.5.2.2  rmind 	return (tf->tf_srr1 & psl_mask)
    141  1.5.2.2  rmind 	    ? &curlwp->l_proc->p_vmspace->vm_map
    142  1.5.2.2  rmind 	    : kernel_map;
    143  1.5.2.2  rmind }
    144  1.5.2.2  rmind 
    145  1.5.2.2  rmind /*
    146  1.5.2.2  rmind  * We could use pmap_pte_lookip but this slightly faster since we already
    147  1.5.2.2  rmind  * the segtab pointers in cpu_info.
    148  1.5.2.2  rmind  */
    149  1.5.2.2  rmind static inline pt_entry_t *
    150  1.5.2.2  rmind trap_pte_lookup(struct trapframe *tf, vaddr_t va, register_t psl_mask)
    151  1.5.2.2  rmind {
    152  1.5.2.2  rmind 	struct pmap_segtab ** const stps = &curcpu()->ci_pmap_kern_segtab;
    153  1.5.2.2  rmind 	struct pmap_segtab * const stp = stps[(tf->tf_srr1 / psl_mask) & 1];
    154  1.5.2.2  rmind 	if (__predict_false(stp == NULL))
    155  1.5.2.2  rmind 		return NULL;
    156  1.5.2.2  rmind 	pt_entry_t *ptep = stp->seg_tab[va >> SEGSHIFT];
    157  1.5.2.2  rmind 	if (__predict_false(ptep == NULL))
    158  1.5.2.2  rmind 		return NULL;
    159  1.5.2.2  rmind 	return ptep + ((va & SEGOFSET) >> PAGE_SHIFT);
    160  1.5.2.2  rmind }
    161  1.5.2.2  rmind 
    162  1.5.2.2  rmind static int
    163  1.5.2.2  rmind pagefault(struct vm_map *map, vaddr_t va, vm_prot_t ftype, bool usertrap)
    164  1.5.2.2  rmind {
    165  1.5.2.2  rmind 	struct lwp * const l = curlwp;
    166  1.5.2.2  rmind 	int rv;
    167  1.5.2.2  rmind 
    168  1.5.2.2  rmind //	printf("%s(%p,%#lx,%u,%u)\n", __func__, map, va, ftype, usertrap);
    169  1.5.2.2  rmind 
    170  1.5.2.2  rmind 	if (usertrap) {
    171  1.5.2.2  rmind #ifdef KERN_SA
    172  1.5.2.2  rmind 		if (l->l_flag & LW_SA) {
    173  1.5.2.2  rmind 			l->l_savp->savp_faultaddr = va;
    174  1.5.2.2  rmind 			l->l_pflag |= LP_SA_PAGEFAULT;
    175  1.5.2.2  rmind 		}
    176  1.5.2.2  rmind #endif
    177  1.5.2.2  rmind 		rv = uvm_fault(map, trunc_page(va), ftype);
    178  1.5.2.2  rmind 		if (rv == 0)
    179  1.5.2.2  rmind 			uvm_grow(l->l_proc, trunc_page(va));
    180  1.5.2.2  rmind #ifdef KERN_SA
    181  1.5.2.2  rmind 		l->l_pflag &= ~LP_SA_PAGEFAULT;
    182  1.5.2.2  rmind #endif
    183  1.5.2.2  rmind 	} else {
    184  1.5.2.2  rmind 		if (cpu_intr_p())
    185  1.5.2.2  rmind 			return EFAULT;
    186  1.5.2.2  rmind 
    187  1.5.2.2  rmind 		struct pcb * const pcb = lwp_getpcb(l);
    188  1.5.2.2  rmind 		struct faultbuf * const fb = pcb->pcb_onfault;
    189  1.5.2.2  rmind 		pcb->pcb_onfault = NULL;
    190  1.5.2.2  rmind 		rv = uvm_fault(map, trunc_page(va), ftype);
    191  1.5.2.2  rmind 		pcb->pcb_onfault = fb;
    192  1.5.2.2  rmind 		if (map != kernel_map) {
    193  1.5.2.2  rmind 			if (rv == 0)
    194  1.5.2.2  rmind 				uvm_grow(l->l_proc, trunc_page(va));
    195  1.5.2.2  rmind #ifdef KERN_SA
    196  1.5.2.2  rmind 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    197  1.5.2.2  rmind #endif
    198  1.5.2.2  rmind 		}
    199  1.5.2.2  rmind 		if (rv == EACCES)
    200  1.5.2.2  rmind 			rv = EFAULT;
    201  1.5.2.2  rmind 	}
    202  1.5.2.2  rmind 	return rv;
    203  1.5.2.2  rmind }
    204  1.5.2.2  rmind 
    205  1.5.2.2  rmind static int
    206  1.5.2.2  rmind dsi_exception(struct trapframe *tf, ksiginfo_t *ksi)
    207  1.5.2.2  rmind {
    208  1.5.2.2  rmind 	const vaddr_t faultva = tf->tf_dear;
    209  1.5.2.2  rmind 	const vm_prot_t ftype = get_faulttype(tf);
    210  1.5.2.2  rmind 	struct vm_map * const faultmap = get_faultmap(tf, PSL_DS);
    211  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    212  1.5.2.2  rmind 
    213  1.5.2.2  rmind 	kpreempt_disable();
    214  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    215  1.5.2.2  rmind 
    216  1.5.2.2  rmind 	if (usertrap)
    217  1.5.2.2  rmind 		ci->ci_ev_udsi.ev_count++;
    218  1.5.2.2  rmind 	else
    219  1.5.2.2  rmind 		ci->ci_ev_kdsi.ev_count++;
    220  1.5.2.2  rmind 
    221  1.5.2.2  rmind 	/*
    222  1.5.2.2  rmind 	 * If we had a TLB entry (which we must have had to get this exception),
    223  1.5.2.2  rmind 	 * we certainly have a PTE.
    224  1.5.2.2  rmind 	 */
    225  1.5.2.2  rmind 	pt_entry_t * const ptep = trap_pte_lookup(tf, trunc_page(faultva),
    226  1.5.2.2  rmind 	    PSL_DS);
    227  1.5.2.2  rmind 	KASSERT(ptep != NULL);
    228  1.5.2.2  rmind 	pt_entry_t pte = *ptep;
    229  1.5.2.2  rmind 
    230  1.5.2.2  rmind 	if ((ftype & VM_PROT_WRITE)
    231  1.5.2.2  rmind 	    && ((pte & (PTE_xW|PTE_UNMODIFIED)) == (PTE_xW|PTE_UNMODIFIED))) {
    232  1.5.2.2  rmind 		const paddr_t pa = pte_to_paddr(pte);
    233  1.5.2.2  rmind 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
    234  1.5.2.2  rmind 		KASSERT(pg);
    235  1.5.2.2  rmind 
    236  1.5.2.2  rmind 		if (!VM_PAGE_MD_MODIFIED_P(pg)) {
    237  1.5.2.2  rmind 			pmap_page_set_attributes(pg, VM_PAGE_MD_MODIFIED);
    238  1.5.2.2  rmind 		}
    239  1.5.2.2  rmind 		pte &= ~PTE_UNMODIFIED;
    240  1.5.2.2  rmind 		*ptep = pte;
    241  1.5.2.2  rmind 		pmap_tlb_update_addr(faultmap->pmap, trunc_page(faultva),
    242  1.5.2.2  rmind 		    pte, 0);
    243  1.5.2.2  rmind 		kpreempt_enable();
    244  1.5.2.2  rmind 		return 0;
    245  1.5.2.2  rmind 	}
    246  1.5.2.2  rmind 	kpreempt_enable();
    247  1.5.2.2  rmind 
    248  1.5.2.2  rmind 	int rv = pagefault(faultmap, faultva, ftype, usertrap);
    249  1.5.2.2  rmind 
    250  1.5.2.2  rmind 	/*
    251  1.5.2.2  rmind 	 * We can't get a MAPERR here since that's a different exception.
    252  1.5.2.2  rmind 	 */
    253  1.5.2.2  rmind 	if (__predict_false(rv != 0 && usertrap)) {
    254  1.5.2.2  rmind 		ci->ci_ev_udsi_fatal.ev_count++;
    255  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    256  1.5.2.2  rmind 		ksi->ksi_signo = SIGSEGV;
    257  1.5.2.2  rmind 		ksi->ksi_trap = EXC_DSI;
    258  1.5.2.2  rmind 		ksi->ksi_code = SEGV_ACCERR;
    259  1.5.2.2  rmind 		ksi->ksi_addr = (void *)faultva;
    260  1.5.2.2  rmind 	}
    261  1.5.2.2  rmind 	return rv;
    262  1.5.2.2  rmind }
    263  1.5.2.2  rmind 
    264  1.5.2.2  rmind static int
    265  1.5.2.2  rmind isi_exception(struct trapframe *tf, ksiginfo_t *ksi)
    266  1.5.2.2  rmind {
    267  1.5.2.2  rmind 	const vaddr_t faultva = trunc_page(tf->tf_srr0);
    268  1.5.2.2  rmind 	struct vm_map * const faultmap = get_faultmap(tf, PSL_IS);
    269  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    270  1.5.2.2  rmind 
    271  1.5.2.2  rmind 	kpreempt_disable();
    272  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    273  1.5.2.2  rmind 
    274  1.5.2.2  rmind 	if (usertrap)
    275  1.5.2.2  rmind 		ci->ci_ev_isi.ev_count++;
    276  1.5.2.2  rmind 	else
    277  1.5.2.2  rmind 		ci->ci_ev_kisi.ev_count++;
    278  1.5.2.2  rmind 
    279  1.5.2.2  rmind 	/*
    280  1.5.2.2  rmind 	 * If we had a TLB entry (which we must have had to get this exception),
    281  1.5.2.2  rmind 	 * we certainly have a PTE.
    282  1.5.2.2  rmind 	 */
    283  1.5.2.2  rmind 	pt_entry_t * const ptep = trap_pte_lookup(tf, trunc_page(faultva),
    284  1.5.2.2  rmind 	    PSL_IS);
    285  1.5.2.2  rmind 	if (ptep == NULL)
    286  1.5.2.2  rmind 		dump_trapframe(tf);
    287  1.5.2.2  rmind 	KASSERT(ptep != NULL);
    288  1.5.2.2  rmind 	pt_entry_t pte = *ptep;
    289  1.5.2.2  rmind 
    290  1.5.2.2  rmind 	if ((pte & PTE_UNSYNCED) == PTE_UNSYNCED) {
    291  1.5.2.2  rmind 		const paddr_t pa = pte_to_paddr(pte);
    292  1.5.2.2  rmind 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
    293  1.5.2.2  rmind 		KASSERT(pg);
    294  1.5.2.2  rmind 
    295  1.5.2.2  rmind 		if (!VM_PAGE_MD_EXECPAGE_P(pg)) {
    296  1.5.2.2  rmind 			ci->ci_softc->cpu_ev_exec_trap_sync.ev_count++;
    297  1.5.2.2  rmind 			dcache_wb_page(pa);
    298  1.5.2.2  rmind 			icache_inv_page(pa);
    299  1.5.2.2  rmind 			pmap_page_set_attributes(pg, VM_PAGE_MD_EXECPAGE);
    300  1.5.2.2  rmind 		}
    301  1.5.2.2  rmind 		pte &= ~PTE_UNSYNCED;
    302  1.5.2.2  rmind 		pte |= PTE_xX;
    303  1.5.2.2  rmind 		*ptep = pte;
    304  1.5.2.2  rmind 		pmap_tlb_update_addr(faultmap->pmap, trunc_page(faultva),
    305  1.5.2.2  rmind 		    pte, 0);
    306  1.5.2.2  rmind 		kpreempt_enable();
    307  1.5.2.2  rmind 		return false;
    308  1.5.2.2  rmind 	}
    309  1.5.2.2  rmind 	kpreempt_enable();
    310  1.5.2.2  rmind 
    311  1.5.2.2  rmind 	int rv = pagefault(faultmap, faultva, VM_PROT_READ|VM_PROT_EXECUTE,
    312  1.5.2.2  rmind 	    usertrap);
    313  1.5.2.2  rmind 
    314  1.5.2.2  rmind 	if (__predict_false(rv != 0 && usertrap)) {
    315  1.5.2.2  rmind 		/*
    316  1.5.2.2  rmind 		 * We can't get a MAPERR here since
    317  1.5.2.2  rmind 		 * that's a different exception.
    318  1.5.2.2  rmind 		 */
    319  1.5.2.2  rmind 		ci->ci_ev_isi_fatal.ev_count++;
    320  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    321  1.5.2.2  rmind 		ksi->ksi_signo = SIGSEGV;
    322  1.5.2.2  rmind 		ksi->ksi_trap = EXC_ISI;
    323  1.5.2.2  rmind 		ksi->ksi_code = SEGV_ACCERR;
    324  1.5.2.2  rmind 		ksi->ksi_addr = (void *)tf->tf_srr0; /* not truncated */
    325  1.5.2.2  rmind 	}
    326  1.5.2.2  rmind 	return rv;
    327  1.5.2.2  rmind }
    328  1.5.2.2  rmind 
    329  1.5.2.2  rmind static int
    330  1.5.2.2  rmind dtlb_exception(struct trapframe *tf, ksiginfo_t *ksi)
    331  1.5.2.2  rmind {
    332  1.5.2.2  rmind 	const vaddr_t faultva = tf->tf_dear;
    333  1.5.2.2  rmind 	const vm_prot_t ftype = get_faulttype(tf);
    334  1.5.2.2  rmind 	struct vm_map * const faultmap = get_faultmap(tf, PSL_DS);
    335  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    336  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    337  1.5.2.2  rmind 
    338  1.5.2.2  rmind #if 0
    339  1.5.2.2  rmind 	/*
    340  1.5.2.2  rmind 	 * This is what pte_load in trap_subr.S does for us.
    341  1.5.2.2  rmind 	 */
    342  1.5.2.2  rmind 	const pt_entry_t * const ptep =
    343  1.5.2.2  rmind 	    trap_pte_lookup(tf, trunc_page(faultva), PSL_DS);
    344  1.5.2.2  rmind 	if (ptep != NULL && !usertrap && pte_valid_p(*ptep)) {
    345  1.5.2.2  rmind 		tlb_update_addr(trunc_page(faultva), KERNEL_PID, *ptep, true);
    346  1.5.2.2  rmind 		ci->ci_ev_tlbmiss_soft.ev_count++;
    347  1.5.2.2  rmind 		return 0;
    348  1.5.2.2  rmind 	}
    349  1.5.2.2  rmind #endif
    350  1.5.2.2  rmind 
    351  1.5.2.2  rmind 	ci->ci_ev_dtlbmiss_hard.ev_count++;
    352  1.5.2.2  rmind 
    353  1.5.2.2  rmind //	printf("pagefault(%p,%#lx,%u,%u)", faultmap, faultva, ftype, usertrap);
    354  1.5.2.2  rmind 	int rv = pagefault(faultmap, faultva, ftype, usertrap);
    355  1.5.2.2  rmind //	printf(": %d\n", rv);
    356  1.5.2.2  rmind 
    357  1.5.2.2  rmind 	if (__predict_false(rv != 0 && usertrap)) {
    358  1.5.2.2  rmind 		ci->ci_ev_udsi_fatal.ev_count++;
    359  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    360  1.5.2.2  rmind 		ksi->ksi_signo = SIGSEGV;
    361  1.5.2.2  rmind 		ksi->ksi_trap = EXC_DSI;
    362  1.5.2.2  rmind 		ksi->ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
    363  1.5.2.2  rmind 		ksi->ksi_addr = (void *)faultva;
    364  1.5.2.2  rmind 	}
    365  1.5.2.2  rmind 	return rv;
    366  1.5.2.2  rmind }
    367  1.5.2.2  rmind 
    368  1.5.2.2  rmind static int
    369  1.5.2.2  rmind itlb_exception(struct trapframe *tf, ksiginfo_t *ksi)
    370  1.5.2.2  rmind {
    371  1.5.2.2  rmind 	struct vm_map * const faultmap = get_faultmap(tf, PSL_IS);
    372  1.5.2.2  rmind 	const vaddr_t faultva = tf->tf_srr0;
    373  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    374  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    375  1.5.2.2  rmind 
    376  1.5.2.2  rmind 	ci->ci_ev_itlbmiss_hard.ev_count++;
    377  1.5.2.2  rmind 
    378  1.5.2.2  rmind 	int rv = pagefault(faultmap, faultva, VM_PROT_READ|VM_PROT_EXECUTE,
    379  1.5.2.2  rmind 	    usertrap);
    380  1.5.2.2  rmind 
    381  1.5.2.2  rmind 	if (__predict_false(rv != 0 && usertrap)) {
    382  1.5.2.2  rmind 		ci->ci_ev_isi_fatal.ev_count++;
    383  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    384  1.5.2.2  rmind 		ksi->ksi_signo = SIGSEGV;
    385  1.5.2.2  rmind 		ksi->ksi_trap = EXC_ISI;
    386  1.5.2.2  rmind 		ksi->ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
    387  1.5.2.2  rmind 		ksi->ksi_addr = (void *)tf->tf_srr0;
    388  1.5.2.2  rmind 	}
    389  1.5.2.2  rmind 	return rv;
    390  1.5.2.2  rmind }
    391  1.5.2.2  rmind 
    392  1.5.2.2  rmind static int
    393  1.5.2.2  rmind spe_exception(struct trapframe *tf, ksiginfo_t *ksi)
    394  1.5.2.2  rmind {
    395  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    396  1.5.2.2  rmind 
    397  1.5.2.2  rmind 	if (!usertrap_p(tf))
    398  1.5.2.2  rmind 		return EPERM;
    399  1.5.2.2  rmind 
    400  1.5.2.2  rmind 	ci->ci_ev_vec.ev_count++;
    401  1.5.2.2  rmind 
    402  1.5.2.2  rmind #ifdef PPC_HAVE_SPE
    403  1.5.2.2  rmind 	vec_enable();
    404  1.5.2.2  rmind 	return 0;
    405  1.5.2.2  rmind #else
    406  1.5.2.2  rmind 	KSI_INIT_TRAP(ksi);
    407  1.5.2.2  rmind 	ksi->ksi_signo = SIGILL;
    408  1.5.2.2  rmind 	ksi->ksi_trap = EXC_PGM;
    409  1.5.2.2  rmind 	ksi->ksi_code = ILL_ILLOPC;
    410  1.5.2.2  rmind 	ksi->ksi_addr = (void *)tf->tf_srr0;
    411  1.5.2.2  rmind 	return EPERM;
    412  1.5.2.2  rmind #endif
    413  1.5.2.2  rmind }
    414  1.5.2.2  rmind 
    415  1.5.2.2  rmind static bool
    416  1.5.2.2  rmind emulate_opcode(struct trapframe *tf, ksiginfo_t *ksi)
    417  1.5.2.2  rmind {
    418  1.5.2.2  rmind 	uint32_t opcode;
    419  1.5.2.2  rmind         if (copyin((void *)tf->tf_srr0, &opcode, sizeof(opcode)) != 0)
    420  1.5.2.2  rmind 		return false;
    421  1.5.2.2  rmind 
    422  1.5.2.2  rmind 	if (opcode == OPC_LWSYNC)
    423  1.5.2.2  rmind 		return true;
    424  1.5.2.2  rmind 
    425  1.5.2.2  rmind 	if (OPC_MFSPR_P(opcode, SPR_PVR)) {
    426  1.5.2.2  rmind 		__asm ("mfpvr %0" : "=r"(tf->tf_fixreg[OPC_MFSPR_REG(opcode)]));
    427  1.5.2.2  rmind 		return true;
    428  1.5.2.2  rmind 	}
    429  1.5.2.2  rmind 
    430  1.5.2.2  rmind 	/*
    431  1.5.2.2  rmind 	 * If we bothered to emulate FP, we would try to do so here.
    432  1.5.2.2  rmind 	 */
    433  1.5.2.2  rmind 	return false;
    434  1.5.2.2  rmind }
    435  1.5.2.2  rmind 
    436  1.5.2.2  rmind static int
    437  1.5.2.2  rmind pgm_exception(struct trapframe *tf, ksiginfo_t *ksi)
    438  1.5.2.2  rmind {
    439  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    440  1.5.2.2  rmind 	int rv = EPERM;
    441  1.5.2.2  rmind 
    442  1.5.2.2  rmind 	if (!usertrap_p(tf))
    443  1.5.2.2  rmind 		return rv;
    444  1.5.2.2  rmind 
    445  1.5.2.2  rmind 	ci->ci_ev_pgm.ev_count++;
    446  1.5.2.2  rmind 
    447  1.5.2.2  rmind 	if (tf->tf_esr & ESR_PTR) {
    448  1.5.2.2  rmind 		struct proc *p = curlwp->l_proc;
    449  1.5.2.2  rmind 		if (p->p_raslist != NULL
    450  1.5.2.2  rmind 		    && ras_lookup(p, (void *)tf->tf_srr0) != (void *) -1) {
    451  1.5.2.2  rmind 			tf->tf_srr0 += 4;
    452  1.5.2.2  rmind 			return 0;
    453  1.5.2.2  rmind 		}
    454  1.5.2.2  rmind 	}
    455  1.5.2.2  rmind 
    456  1.5.2.2  rmind 	if (tf->tf_esr & (ESR_PIL|ESR_PPR)) {
    457  1.5.2.2  rmind 		if (emulate_opcode(tf, ksi)) {
    458  1.5.2.2  rmind 			tf->tf_srr0 += 4;
    459  1.5.2.2  rmind 			return 0;
    460  1.5.2.2  rmind 		}
    461  1.5.2.2  rmind 	}
    462  1.5.2.2  rmind 
    463  1.5.2.2  rmind 	KSI_INIT_TRAP(ksi);
    464  1.5.2.2  rmind 	ksi->ksi_signo = SIGILL;
    465  1.5.2.2  rmind 	ksi->ksi_trap = EXC_PGM;
    466  1.5.2.2  rmind 	if (tf->tf_esr & ESR_PIL) {
    467  1.5.2.2  rmind 		ksi->ksi_code = ILL_ILLOPC;
    468  1.5.2.2  rmind 	} else if (tf->tf_esr & ESR_PPR) {
    469  1.5.2.2  rmind 		ksi->ksi_code = ILL_PRVOPC;
    470  1.5.2.2  rmind 	} else if (tf->tf_esr & ESR_PTR) {
    471  1.5.2.2  rmind 		ksi->ksi_signo = SIGTRAP;
    472  1.5.2.2  rmind 		ksi->ksi_code = TRAP_BRKPT;
    473  1.5.2.2  rmind 	} else {
    474  1.5.2.2  rmind 		ksi->ksi_code = 0;
    475  1.5.2.2  rmind 	}
    476  1.5.2.2  rmind 	ksi->ksi_addr = (void *)tf->tf_srr0;
    477  1.5.2.2  rmind 	return rv;
    478  1.5.2.2  rmind }
    479  1.5.2.2  rmind 
    480  1.5.2.2  rmind static int
    481  1.5.2.2  rmind debug_exception(struct trapframe *tf, ksiginfo_t *ksi)
    482  1.5.2.2  rmind {
    483  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    484  1.5.2.2  rmind 	int rv = EPERM;
    485  1.5.2.2  rmind 
    486  1.5.2.2  rmind 	if (!usertrap_p(tf))
    487  1.5.2.2  rmind 		return rv;
    488  1.5.2.2  rmind 
    489  1.5.2.2  rmind 	ci->ci_ev_debug.ev_count++;
    490  1.5.2.2  rmind 
    491  1.5.2.2  rmind 	/*
    492  1.5.2.2  rmind 	 * Ack the interrupt.
    493  1.5.2.2  rmind 	 */
    494  1.5.2.2  rmind 	mtspr(SPR_DBSR, tf->tf_esr);
    495  1.5.2.2  rmind 	KASSERT(tf->tf_esr & (DBSR_IAC1|DBSR_IAC2));
    496  1.5.2.2  rmind 	KASSERT((tf->tf_srr1 & PSL_SE) == 0);
    497  1.5.2.2  rmind 
    498  1.5.2.2  rmind 	/*
    499  1.5.2.2  rmind 	 * Disable debug events
    500  1.5.2.2  rmind 	 */
    501  1.5.2.2  rmind 	mtspr(SPR_DBCR1, 0);
    502  1.5.2.2  rmind 	mtspr(SPR_DBCR0, 0);
    503  1.5.2.2  rmind 
    504  1.5.2.2  rmind 	/*
    505  1.5.2.2  rmind 	 * Tell the debugger ...
    506  1.5.2.2  rmind 	 */
    507  1.5.2.2  rmind 	KSI_INIT_TRAP(ksi);
    508  1.5.2.2  rmind 	ksi->ksi_signo = SIGTRAP;
    509  1.5.2.2  rmind 	ksi->ksi_trap = EXC_TRC;
    510  1.5.2.2  rmind 	ksi->ksi_addr = (void *)tf->tf_srr0;
    511  1.5.2.2  rmind 	ksi->ksi_code = TRAP_TRACE;
    512  1.5.2.2  rmind 	return rv;
    513  1.5.2.2  rmind }
    514  1.5.2.2  rmind 
    515  1.5.2.2  rmind static int
    516  1.5.2.2  rmind ali_exception(struct trapframe *tf, ksiginfo_t *ksi)
    517  1.5.2.2  rmind {
    518  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    519  1.5.2.2  rmind 	int rv = EFAULT;
    520  1.5.2.2  rmind 
    521  1.5.2.2  rmind 	ci->ci_ev_ali.ev_count++;
    522  1.5.2.2  rmind 
    523  1.5.2.2  rmind 	if (rv != 0 && usertrap_p(tf)) {
    524  1.5.2.2  rmind 		ci->ci_ev_ali_fatal.ev_count++;
    525  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    526  1.5.2.2  rmind 		ksi->ksi_signo = SIGILL;
    527  1.5.2.2  rmind 		ksi->ksi_trap = EXC_PGM;
    528  1.5.2.2  rmind 		if (tf->tf_esr & ESR_PIL)
    529  1.5.2.2  rmind 			ksi->ksi_code = ILL_ILLOPC;
    530  1.5.2.2  rmind 		else if (tf->tf_esr & ESR_PPR)
    531  1.5.2.2  rmind 			ksi->ksi_code = ILL_PRVOPC;
    532  1.5.2.2  rmind 		else if (tf->tf_esr & ESR_PTR)
    533  1.5.2.2  rmind 			ksi->ksi_code = ILL_ILLTRP;
    534  1.5.2.2  rmind 		else
    535  1.5.2.2  rmind 			ksi->ksi_code = 0;
    536  1.5.2.2  rmind 		ksi->ksi_addr = (void *)tf->tf_srr0;
    537  1.5.2.2  rmind 	}
    538  1.5.2.2  rmind 	return rv;
    539  1.5.2.2  rmind }
    540  1.5.2.2  rmind 
    541  1.5.2.2  rmind static int
    542  1.5.2.2  rmind embedded_fp_data_exception(struct trapframe *tf, ksiginfo_t *ksi)
    543  1.5.2.2  rmind {
    544  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    545  1.5.2.2  rmind 	int rv = EFAULT;
    546  1.5.2.2  rmind 
    547  1.5.2.2  rmind 	ci->ci_ev_fpu.ev_count++;
    548  1.5.2.2  rmind 
    549  1.5.2.2  rmind 	if (rv != 0 && usertrap_p(tf)) {
    550  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    551  1.5.2.2  rmind #ifdef PPC_HAVE_SPE
    552  1.5.2.2  rmind 		ksi->ksi_signo = SIGFPE;
    553  1.5.2.2  rmind 		ksi->ksi_trap = tf->tf_exc;
    554  1.5.2.2  rmind 		ksi->ksi_code = vec_siginfo_code(tf);
    555  1.5.2.2  rmind #else
    556  1.5.2.2  rmind 		ksi->ksi_signo = SIGILL;
    557  1.5.2.2  rmind 		ksi->ksi_trap = EXC_PGM;
    558  1.5.2.2  rmind 		ksi->ksi_code = ILL_ILLOPC;
    559  1.5.2.2  rmind #endif
    560  1.5.2.2  rmind 		ksi->ksi_addr = (void *)tf->tf_srr0;
    561  1.5.2.2  rmind 	}
    562  1.5.2.2  rmind 	return rv;
    563  1.5.2.2  rmind }
    564  1.5.2.2  rmind 
    565  1.5.2.2  rmind static int
    566  1.5.2.2  rmind embedded_fp_round_exception(struct trapframe *tf, ksiginfo_t *ksi)
    567  1.5.2.2  rmind {
    568  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    569  1.5.2.2  rmind 	int rv = EDOM;
    570  1.5.2.2  rmind 
    571  1.5.2.2  rmind 	ci->ci_ev_fpu.ev_count++;
    572  1.5.2.2  rmind 
    573  1.5.2.2  rmind 	if (rv != 0 && usertrap_p(tf)) {
    574  1.5.2.2  rmind 		KSI_INIT_TRAP(ksi);
    575  1.5.2.2  rmind #ifdef PPC_HAVE_SPE
    576  1.5.2.2  rmind 		ksi->ksi_signo = SIGFPE;
    577  1.5.2.2  rmind 		ksi->ksi_trap = tf->tf_exc;
    578  1.5.2.2  rmind 		ksi->ksi_code = vec_siginfo_code(tf);
    579  1.5.2.2  rmind #else
    580  1.5.2.2  rmind 		ksi->ksi_signo = SIGILL;
    581  1.5.2.2  rmind 		ksi->ksi_trap = EXC_PGM;
    582  1.5.2.2  rmind 		ksi->ksi_code = ILL_ILLOPC;
    583  1.5.2.2  rmind #endif
    584  1.5.2.2  rmind 		ksi->ksi_addr = (void *)tf->tf_srr0;
    585  1.5.2.2  rmind 	}
    586  1.5.2.2  rmind 	return rv;
    587  1.5.2.2  rmind }
    588  1.5.2.2  rmind 
    589  1.5.2.2  rmind static void
    590  1.5.2.2  rmind dump_trapframe(const struct trapframe *tf)
    591  1.5.2.2  rmind {
    592  1.5.2.2  rmind 	printf("trapframe %p (exc=%x srr0/1=%#lx/%#lx esr/dear=%#x/%#lx)\n",
    593  1.5.2.2  rmind 	    tf, tf->tf_exc, tf->tf_srr0, tf->tf_srr1, tf->tf_esr, tf->tf_dear);
    594  1.5.2.2  rmind 	printf("lr =%08lx ctr=%08lx cr =%08x xer=%08x\n",
    595  1.5.2.2  rmind 	    tf->tf_lr, tf->tf_ctr, tf->tf_cr, tf->tf_xer);
    596  1.5.2.2  rmind 	for (u_int r = 0; r < 32; r += 4) {
    597  1.5.2.2  rmind 		printf("r%02u=%08lx r%02u=%08lx r%02u=%08lx r%02u=%08lx\n",
    598  1.5.2.2  rmind 		    r+0, tf->tf_fixreg[r+0], r+1, tf->tf_fixreg[r+1],
    599  1.5.2.2  rmind 		    r+2, tf->tf_fixreg[r+2], r+3, tf->tf_fixreg[r+3]);
    600  1.5.2.2  rmind 	}
    601  1.5.2.2  rmind }
    602  1.5.2.2  rmind static bool
    603  1.5.2.2  rmind ddb_exception(struct trapframe *tf)
    604  1.5.2.2  rmind {
    605  1.5.2.2  rmind #if 0
    606  1.5.2.2  rmind 	const register_t ddb_trapfunc = (uintptr_t) cpu_Debugger;
    607  1.5.2.2  rmind 	if ((tf->tf_esr & ESR_PTR) == 0)
    608  1.5.2.2  rmind 		return false;
    609  1.5.2.2  rmind 	if (ddb_trapfunc <= tf->tf_srr0 && tf->tf_srr0 <= ddb_trapfunc+16) {
    610  1.5.2.2  rmind 		register_t srr0 = tf->tf_srr0;
    611  1.5.2.2  rmind 		if (kdb_trap(tf->tf_exc, tf)) {
    612  1.5.2.2  rmind 			if (srr0 == tf->tf_srr0)
    613  1.5.2.2  rmind 				tf->tf_srr0 += 4;
    614  1.5.2.2  rmind 			return true;
    615  1.5.2.2  rmind 		}
    616  1.5.2.2  rmind 	}
    617  1.5.2.2  rmind 	return false;
    618  1.5.2.2  rmind #else
    619  1.5.2.2  rmind #if 0
    620  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    621  1.5.2.2  rmind 	struct cpu_softc * const cpu = ci->ci_softc;
    622  1.5.2.2  rmind 	printf("CPL stack:");
    623  1.5.2.2  rmind 	if (ci->ci_idepth >= 0) {
    624  1.5.2.2  rmind 		for (u_int i = 0; i <= ci->ci_idepth; i++) {
    625  1.5.2.2  rmind 			printf(" [%u]=%u", i, cpu->cpu_pcpls[i]);
    626  1.5.2.2  rmind 		}
    627  1.5.2.2  rmind 	}
    628  1.5.2.2  rmind 	printf(" %u\n", ci->ci_cpl);
    629  1.5.2.2  rmind 	dump_trapframe(tf);
    630  1.5.2.2  rmind #endif
    631  1.5.2.2  rmind 	if (kdb_trap(tf->tf_exc, tf)) {
    632  1.5.2.2  rmind 		tf->tf_srr0 += 4;
    633  1.5.2.2  rmind 		return true;
    634  1.5.2.2  rmind 	}
    635  1.5.2.2  rmind 	return false;
    636  1.5.2.2  rmind #endif
    637  1.5.2.2  rmind }
    638  1.5.2.2  rmind 
    639  1.5.2.2  rmind static bool
    640  1.5.2.2  rmind onfaulted(struct trapframe *tf, register_t rv)
    641  1.5.2.2  rmind {
    642  1.5.2.2  rmind 	struct lwp * const l = curlwp;
    643  1.5.2.2  rmind 	struct pcb * const pcb = lwp_getpcb(l);
    644  1.5.2.2  rmind 	struct faultbuf * const fb = pcb->pcb_onfault;
    645  1.5.2.2  rmind 	if (fb == NULL)
    646  1.5.2.2  rmind 		return false;
    647  1.5.2.2  rmind 	tf->tf_srr0 = fb->fb_pc;
    648  1.5.2.2  rmind 	tf->tf_srr1 = fb->fb_msr;
    649  1.5.2.2  rmind 	tf->tf_cr = fb->fb_cr;
    650  1.5.2.2  rmind 	tf->tf_fixreg[1] = fb->fb_sp;
    651  1.5.2.2  rmind 	tf->tf_fixreg[2] = fb->fb_r2;
    652  1.5.2.2  rmind 	tf->tf_fixreg[3] = rv;
    653  1.5.2.2  rmind 	pcb->pcb_onfault = NULL;
    654  1.5.2.2  rmind 	return true;
    655  1.5.2.2  rmind }
    656  1.5.2.2  rmind 
    657  1.5.2.2  rmind void
    658  1.5.2.2  rmind trap(enum ppc_booke_exceptions trap_code, struct trapframe *tf)
    659  1.5.2.2  rmind {
    660  1.5.2.2  rmind 	const bool usertrap = usertrap_p(tf);
    661  1.5.2.2  rmind 	struct cpu_info * const ci = curcpu();
    662  1.5.2.2  rmind 	struct lwp * const l = curlwp;
    663  1.5.2.2  rmind 	struct proc * const p = l->l_proc;
    664  1.5.2.2  rmind 	ksiginfo_t ksi;
    665  1.5.2.2  rmind 	int rv = EACCES;
    666  1.5.2.2  rmind 
    667  1.5.2.2  rmind 	ci->ci_ev_traps.ev_count++;
    668  1.5.2.2  rmind 	ci->ci_data.cpu_ntrap++;
    669  1.5.2.2  rmind 
    670  1.5.2.2  rmind 	KASSERTMSG(!usertrap || tf == trapframe(l),
    671  1.5.2.2  rmind 	    ("trap: tf=%p is invalid: trapframe(%p)=%p", tf, l, trapframe(l)));
    672  1.5.2.2  rmind 
    673  1.5.2.2  rmind #if 0
    674  1.5.2.2  rmind 	if (trap_code != T_PROGRAM || usertrap)
    675  1.5.2.2  rmind 		printf("trap(enter): %s (tf=%p, esr/dear=%#x/%#lx, srr0/1=%#lx/%#lx, lr=%#lx)\n",
    676  1.5.2.2  rmind 		    trap_names[trap_code], tf, tf->tf_esr, tf->tf_dear,
    677  1.5.2.2  rmind 		    tf->tf_srr0, tf->tf_srr1, tf->tf_lr);
    678  1.5.2.2  rmind #endif
    679  1.5.2.2  rmind #if 0
    680  1.5.2.2  rmind 	if ((register_t)tf >= (register_t)l->l_addr + USPACE
    681  1.5.2.2  rmind 	    || (register_t)tf < (register_t)l->l_addr + PAGE_SIZE) {
    682  1.5.2.2  rmind 		printf("%s(entry): pid %d.%d (%s): invalid tf addr %p\n",
    683  1.5.2.2  rmind 		    __func__, p->p_pid, l->l_lid, p->p_comm, tf);
    684  1.5.2.2  rmind 		dump_trapframe(tf);
    685  1.5.2.2  rmind 		Debugger();
    686  1.5.2.2  rmind 	}
    687  1.5.2.2  rmind #endif
    688  1.5.2.2  rmind #if 0
    689  1.5.2.2  rmind 	if ((mfmsr() & PSL_CE) == 0) {
    690  1.5.2.2  rmind 		printf("%s(entry): pid %d.%d (%s): %s: PSL_CE (%#lx) not set\n",
    691  1.5.2.2  rmind 		    __func__, p->p_pid, l->l_lid, p->p_comm,
    692  1.5.2.2  rmind 		    trap_names[trap_code], mfmsr());
    693  1.5.2.2  rmind 		dump_trapframe(tf);
    694  1.5.2.2  rmind 	}
    695  1.5.2.2  rmind #endif
    696  1.5.2.2  rmind 
    697  1.5.2.2  rmind 	if (usertrap && (tf->tf_fixreg[1] & 0x80000000)) {
    698  1.5.2.2  rmind 		printf("%s(entry): pid %d.%d (%s): %s invalid sp %#lx (sprg1=%#lx)\n",
    699  1.5.2.2  rmind 		    __func__, p->p_pid, l->l_lid, p->p_comm,
    700  1.5.2.2  rmind 		    trap_names[trap_code], tf->tf_fixreg[1], mfspr(SPR_SPRG1));
    701  1.5.2.2  rmind 		dump_trapframe(tf);
    702  1.5.2.2  rmind 		Debugger();
    703  1.5.2.2  rmind 	}
    704  1.5.2.2  rmind 
    705  1.5.2.2  rmind 	if (usertrap && (tf->tf_srr1 & (PSL_DS|PSL_IS)) != (PSL_DS|PSL_IS)) {
    706  1.5.2.2  rmind 		printf("%s(entry): pid %d.%d (%s): %s invalid PSL %#lx\n",
    707  1.5.2.2  rmind 		    __func__, p->p_pid, l->l_lid, p->p_comm,
    708  1.5.2.2  rmind 		    trap_names[trap_code], tf->tf_srr1);
    709  1.5.2.2  rmind 		dump_trapframe(tf);
    710  1.5.2.2  rmind 		Debugger();
    711  1.5.2.2  rmind 	}
    712  1.5.2.2  rmind 
    713  1.5.2.2  rmind 	switch (trap_code) {
    714  1.5.2.2  rmind 	case T_CRITIAL_INPUT:
    715  1.5.2.2  rmind 	case T_EXTERNAL_INPUT:
    716  1.5.2.2  rmind 	case T_DECREMENTER:
    717  1.5.2.2  rmind 	case T_FIXED_INTERVAL:
    718  1.5.2.2  rmind 	case T_WATCHDOG:
    719  1.5.2.2  rmind 	case T_SYSTEM_CALL:
    720  1.5.2.2  rmind 	default:
    721  1.5.2.2  rmind 		panic("trap: unexcepted trap code %d! (tf=%p, srr0/1=%#lx/%#lx)",
    722  1.5.2.2  rmind 		    trap_code, tf, tf->tf_srr0, tf->tf_srr1);
    723  1.5.2.2  rmind 	case T_MACHINE_CHECK:
    724  1.5.2.2  rmind 		rv = mchk_exception(tf, &ksi);
    725  1.5.2.2  rmind 		break;
    726  1.5.2.2  rmind 	case T_DSI:
    727  1.5.2.2  rmind 		rv = dsi_exception(tf, &ksi);
    728  1.5.2.2  rmind 		break;
    729  1.5.2.2  rmind 	case T_ISI:
    730  1.5.2.2  rmind 		rv = isi_exception(tf, &ksi);
    731  1.5.2.2  rmind 		break;
    732  1.5.2.2  rmind 	case T_ALIGNMENT:
    733  1.5.2.2  rmind 		rv = ali_exception(tf, &ksi);
    734  1.5.2.2  rmind 		break;
    735  1.5.2.2  rmind 	case T_SPE_UNAVAILABLE:
    736  1.5.2.2  rmind 		rv = spe_exception(tf, &ksi);
    737  1.5.2.2  rmind 		break;
    738  1.5.2.2  rmind 	case T_PROGRAM:
    739  1.5.2.2  rmind #ifdef DDB
    740  1.5.2.2  rmind 		if (!usertrap && ddb_exception(tf))
    741  1.5.2.2  rmind 			return;
    742  1.5.2.2  rmind #endif
    743  1.5.2.2  rmind 		rv = pgm_exception(tf, &ksi);
    744  1.5.2.2  rmind 		break;
    745  1.5.2.2  rmind 	case T_FP_UNAVAILABLE:
    746  1.5.2.2  rmind 	case T_AP_UNAVAILABLE:
    747  1.5.2.2  rmind 		panic("trap: unexcepted trap code %d! (tf=%p, srr0/1=%#lx/%#lx)",
    748  1.5.2.2  rmind 		    trap_code, tf, tf->tf_srr0, tf->tf_srr1);
    749  1.5.2.2  rmind 	case T_DATA_TLB_ERROR:
    750  1.5.2.2  rmind 		rv = dtlb_exception(tf, &ksi);
    751  1.5.2.2  rmind 		break;
    752  1.5.2.2  rmind 	case T_INSTRUCTION_TLB_ERROR:
    753  1.5.2.2  rmind 		rv = itlb_exception(tf, &ksi);
    754  1.5.2.2  rmind 		break;
    755  1.5.2.2  rmind 	case T_DEBUG:
    756  1.5.2.2  rmind #ifdef DDB
    757  1.5.2.2  rmind 		if (!usertrap && ddb_exception(tf))
    758  1.5.2.2  rmind 			return;
    759  1.5.2.2  rmind #endif
    760  1.5.2.2  rmind 		rv = debug_exception(tf, &ksi);
    761  1.5.2.2  rmind 		break;
    762  1.5.2.2  rmind 	case T_EMBEDDED_FP_DATA:
    763  1.5.2.2  rmind 		rv = embedded_fp_data_exception(tf, &ksi);
    764  1.5.2.2  rmind 		break;
    765  1.5.2.2  rmind 	case T_EMBEDDED_FP_ROUND:
    766  1.5.2.2  rmind 		rv = embedded_fp_round_exception(tf, &ksi);
    767  1.5.2.2  rmind 		break;
    768  1.5.2.2  rmind 	case T_EMBEDDED_PERF_MONITOR:
    769  1.5.2.2  rmind 		//db_stack_trace_print(tf->tf_fixreg[1], true, 40, "", printf);
    770  1.5.2.2  rmind 		dump_trapframe(tf);
    771  1.5.2.2  rmind 		rv = EPERM;
    772  1.5.2.2  rmind 		break;
    773  1.5.2.2  rmind 	case T_AST:
    774  1.5.2.2  rmind 		KASSERT(usertrap);
    775  1.5.2.2  rmind 		ci->ci_astpending = 0;		/* we are about to do it */
    776  1.5.2.2  rmind 		ci->ci_data.cpu_nsoft++;
    777  1.5.2.2  rmind 		if (l->l_pflag & LP_OWEUPC) {
    778  1.5.2.2  rmind 			l->l_pflag &= ~LP_OWEUPC;
    779  1.5.2.2  rmind 			ADDUPROF(l);
    780  1.5.2.2  rmind 		}
    781  1.5.2.2  rmind 		/* Check whether we are being preempted. */
    782  1.5.2.2  rmind 		if (ci->ci_want_resched)
    783  1.5.2.2  rmind 			preempt();
    784  1.5.2.2  rmind 		if (tf->tf_fixreg[1] & 0x80000000) {
    785  1.5.2.2  rmind 			printf("%s(ast-exit): pid %d.%d (%s): invalid sp %#lx\n",
    786  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    787  1.5.2.2  rmind 			    tf->tf_fixreg[1]);
    788  1.5.2.2  rmind 			dump_trapframe(tf);
    789  1.5.2.2  rmind 			Debugger();
    790  1.5.2.2  rmind 		}
    791  1.5.2.2  rmind 		if ((tf->tf_srr1 & (PSL_DS|PSL_IS)) != (PSL_DS|PSL_IS)) {
    792  1.5.2.2  rmind 			printf("%s(entry): pid %d.%d (%s): %s invalid PSL %#lx\n",
    793  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    794  1.5.2.2  rmind 			    trap_names[trap_code], tf->tf_srr1);
    795  1.5.2.2  rmind 			dump_trapframe(tf);
    796  1.5.2.2  rmind 			Debugger();
    797  1.5.2.2  rmind 		}
    798  1.5.2.2  rmind #if 0
    799  1.5.2.2  rmind 		if ((mfmsr() & PSL_CE) == 0) {
    800  1.5.2.2  rmind 			printf("%s(exit): pid %d.%d (%s): %s: PSL_CE (%#lx) not set\n",
    801  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    802  1.5.2.2  rmind 			    trap_names[trap_code], mfmsr());
    803  1.5.2.2  rmind 			dump_trapframe(tf);
    804  1.5.2.2  rmind 		}
    805  1.5.2.2  rmind #endif
    806  1.5.2.2  rmind 		userret(l, tf);
    807  1.5.2.2  rmind 		return;
    808  1.5.2.2  rmind 	}
    809  1.5.2.2  rmind 	if (!usertrap) {
    810  1.5.2.2  rmind 		if (rv != 0) {
    811  1.5.2.2  rmind 			if (!onfaulted(tf, rv)) {
    812  1.5.2.2  rmind 				db_stack_trace_print(tf->tf_fixreg[1], true, 40, "", printf);
    813  1.5.2.2  rmind 				dump_trapframe(tf);
    814  1.5.2.2  rmind 				panic("%s: pid %d.%d (%s): %s exception in kernel mode"
    815  1.5.2.2  rmind 				    " (tf=%p, dear=%#lx, esr=%#x,"
    816  1.5.2.2  rmind 				    " srr0/1=%#lx/%#lx)",
    817  1.5.2.2  rmind 				    __func__, p->p_pid, l->l_lid, p->p_comm,
    818  1.5.2.2  rmind 				    trap_names[trap_code], tf, tf->tf_dear,
    819  1.5.2.2  rmind 				    tf->tf_esr, tf->tf_srr0, tf->tf_srr1);
    820  1.5.2.2  rmind 			}
    821  1.5.2.2  rmind 		}
    822  1.5.2.2  rmind #if 0
    823  1.5.2.2  rmind 		if (tf->tf_fixreg[1] >= (register_t)l->l_addr + USPACE
    824  1.5.2.2  rmind 		    || tf->tf_fixreg[1] < (register_t)l->l_addr + PAGE_SIZE) {
    825  1.5.2.2  rmind 			printf("%s(exit): pid %d.%d (%s): invalid kern sp %#lx\n",
    826  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    827  1.5.2.2  rmind 			    tf->tf_fixreg[1]);
    828  1.5.2.2  rmind 			dump_trapframe(tf);
    829  1.5.2.2  rmind 			Debugger();
    830  1.5.2.2  rmind 		}
    831  1.5.2.2  rmind #endif
    832  1.5.2.2  rmind #if 0
    833  1.5.2.2  rmind 		if ((mfmsr() & PSL_CE) == 0) {
    834  1.5.2.2  rmind 			printf("%s(exit): pid %d.%d (%s): %s: PSL_CE (%#lx) not set\n",
    835  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    836  1.5.2.2  rmind 			    trap_names[trap_code], mfmsr());
    837  1.5.2.2  rmind 			mtmsr(mfmsr()|PSL_CE);
    838  1.5.2.2  rmind 			dump_trapframe(tf);
    839  1.5.2.2  rmind 		}
    840  1.5.2.2  rmind #endif
    841  1.5.2.2  rmind 	} else {
    842  1.5.2.2  rmind 		if (rv == ENOMEM) {
    843  1.5.2.2  rmind 			printf("UVM: pid %d.%d (%s), uid %d killed: "
    844  1.5.2.2  rmind 			    "out of swap\n",
    845  1.5.2.2  rmind 			    p->p_pid, l->l_lid, p->p_comm,
    846  1.5.2.2  rmind 			    l->l_cred ?  kauth_cred_geteuid(l->l_cred) : -1);
    847  1.5.2.2  rmind 			ksi.ksi_signo = SIGKILL;
    848  1.5.2.2  rmind 		}
    849  1.5.2.2  rmind 		if (rv != 0) {
    850  1.5.2.2  rmind 			if (cpu_printfataltraps) {
    851  1.5.2.2  rmind 				printf("%s: pid %d.%d (%s):"
    852  1.5.2.2  rmind 				    " %s exception in user mode\n",
    853  1.5.2.2  rmind 				    __func__, p->p_pid, l->l_lid, p->p_comm,
    854  1.5.2.2  rmind 				    trap_names[trap_code]);
    855  1.5.2.2  rmind 				if (cpu_printfataltraps > 1)
    856  1.5.2.2  rmind 					dump_trapframe(tf);
    857  1.5.2.2  rmind 			}
    858  1.5.2.2  rmind 			(*p->p_emul->e_trapsignal)(l, &ksi);
    859  1.5.2.2  rmind 		}
    860  1.5.2.2  rmind #ifdef DEBUG
    861  1.5.2.2  rmind 		if ((tf->tf_srr1 & (PSL_DS|PSL_IS)) != (PSL_DS|PSL_IS)) {
    862  1.5.2.2  rmind 			printf("%s(exit): pid %d.%d (%s): %s invalid PSL %#lx\n",
    863  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    864  1.5.2.2  rmind 			    trap_names[trap_code], tf->tf_srr1);
    865  1.5.2.2  rmind 			dump_trapframe(tf);
    866  1.5.2.2  rmind 			Debugger();
    867  1.5.2.2  rmind 		}
    868  1.5.2.2  rmind #endif
    869  1.5.2.2  rmind #if 0
    870  1.5.2.2  rmind 		if ((mfmsr() & PSL_CE) == 0) {
    871  1.5.2.2  rmind 			printf("%s(exit): pid %d.%d (%s): %s: PSL_CE (%#lx) not set\n",
    872  1.5.2.2  rmind 			    __func__, p->p_pid, l->l_lid, p->p_comm,
    873  1.5.2.2  rmind 			    trap_names[trap_code], mfmsr());
    874  1.5.2.2  rmind 			dump_trapframe(tf);
    875  1.5.2.2  rmind 		}
    876  1.5.2.2  rmind #endif
    877  1.5.2.2  rmind 		userret(l, tf);
    878  1.5.2.2  rmind 	}
    879  1.5.2.2  rmind }
    880  1.5.2.2  rmind 
    881  1.5.2.2  rmind void
    882  1.5.2.2  rmind upcallret(struct lwp *l)
    883  1.5.2.2  rmind {
    884  1.5.2.2  rmind 
    885  1.5.2.2  rmind 	mi_userret(l);		/* Invoke MI userret code */
    886  1.5.2.2  rmind }
    887  1.5.2.2  rmind 
    888  1.5.2.2  rmind /*
    889  1.5.2.2  rmind  * Start a new LWP
    890  1.5.2.2  rmind  */
    891  1.5.2.2  rmind void
    892  1.5.2.2  rmind startlwp(void *arg)
    893  1.5.2.2  rmind {
    894  1.5.2.2  rmind 	ucontext_t * const uc = arg;
    895  1.5.2.2  rmind 	struct lwp * const l = curlwp;
    896  1.5.2.2  rmind 
    897  1.5.2.2  rmind 	int error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
    898  1.5.2.2  rmind 	KASSERT(error == 0);
    899  1.5.2.2  rmind 	(void)error;
    900  1.5.2.2  rmind         kmem_free(uc, sizeof(ucontext_t));
    901  1.5.2.2  rmind 	upcallret(l);
    902  1.5.2.2  rmind }
    903