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