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