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