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