trap.c revision 160764
1/*- 2 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 3 * Copyright (C) 1995, 1996 TooLs GmbH. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by TooLs GmbH. 17 * 4. The name of TooLs GmbH may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $NetBSD: trap.c,v 1.58 2002/03/04 04:07:35 dbj Exp $ 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: head/sys/powerpc/aim/trap.c 160764 2006-07-27 19:50:16Z jhb $"); 36 37#include "opt_ktrace.h" 38 39#include <sys/param.h> 40#include <sys/kdb.h> 41#include <sys/proc.h> 42#include <sys/ktr.h> 43#include <sys/lock.h> 44#include <sys/mutex.h> 45#include <sys/pioctl.h> 46#include <sys/ptrace.h> 47#include <sys/reboot.h> 48#include <sys/syscall.h> 49#include <sys/sysent.h> 50#include <sys/systm.h> 51#include <sys/uio.h> 52#include <sys/signalvar.h> 53#ifdef KTRACE 54#include <sys/ktrace.h> 55#endif 56#include <sys/vmmeter.h> 57 58#include <vm/vm.h> 59#include <vm/pmap.h> 60#include <vm/vm_extern.h> 61#include <vm/vm_param.h> 62#include <vm/vm_kern.h> 63#include <vm/vm_map.h> 64#include <vm/vm_page.h> 65 66#include <machine/cpu.h> 67#include <machine/db_machdep.h> 68#include <machine/fpu.h> 69#include <machine/frame.h> 70#include <machine/pcb.h> 71#include <machine/pmap.h> 72#include <machine/psl.h> 73#include <machine/trap.h> 74#include <machine/spr.h> 75#include <machine/sr.h> 76 77void trap(struct trapframe *); 78 79static void trap_fatal(struct trapframe *frame); 80static void printtrap(u_int vector, struct trapframe *frame, int isfatal, 81 int user); 82static int trap_pfault(struct trapframe *frame, int user); 83static int fix_unaligned(struct thread *td, struct trapframe *frame); 84static int handle_onfault(struct trapframe *frame); 85static void syscall(struct trapframe *frame); 86 87static __inline void setusr(u_int); 88 89int setfault(faultbuf); /* defined in locore.S */ 90 91/* Why are these not defined in a header? */ 92int badaddr(void *, size_t); 93int badaddr_read(void *, size_t, int *); 94 95extern char *syscallnames[]; 96 97struct powerpc_exception { 98 u_int vector; 99 char *name; 100}; 101 102static struct powerpc_exception powerpc_exceptions[] = { 103 { 0x0100, "system reset" }, 104 { 0x0200, "machine check" }, 105 { 0x0300, "data storage interrupt" }, 106 { 0x0400, "instruction storage interrupt" }, 107 { 0x0500, "external interrupt" }, 108 { 0x0600, "alignment" }, 109 { 0x0700, "program" }, 110 { 0x0800, "floating-point unavailable" }, 111 { 0x0900, "decrementer" }, 112 { 0x0c00, "system call" }, 113 { 0x0d00, "trace" }, 114 { 0x0e00, "floating-point assist" }, 115 { 0x0f00, "performance monitoring" }, 116 { 0x0f20, "altivec unavailable" }, 117 { 0x1000, "instruction tlb miss" }, 118 { 0x1100, "data load tlb miss" }, 119 { 0x1200, "data store tlb miss" }, 120 { 0x1300, "instruction breakpoint" }, 121 { 0x1400, "system management" }, 122 { 0x1600, "altivec assist" }, 123 { 0x1700, "thermal management" }, 124 { 0x2000, "run mode/trace" }, 125 { 0x3000, NULL } 126}; 127 128static const char * 129trapname(u_int vector) 130{ 131 struct powerpc_exception *pe; 132 133 for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) { 134 if (pe->vector == vector) 135 return (pe->name); 136 } 137 138 return ("unknown"); 139} 140 141void 142trap(struct trapframe *frame) 143{ 144 struct thread *td; 145 struct proc *p; 146 int sig, type, user; 147 u_int ucode; 148 ksiginfo_t ksi; 149 150 PCPU_LAZY_INC(cnt.v_trap); 151 152 td = PCPU_GET(curthread); 153 p = td->td_proc; 154 155 type = ucode = frame->exc; 156 sig = 0; 157 user = frame->srr1 & PSL_PR; 158 159 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm, 160 trapname(type), user ? "user" : "kernel"); 161 162 if (user) { 163 td->td_pticks = 0; 164 td->td_frame = frame; 165 if (td->td_ucred != p->p_ucred) 166 cred_update_thread(td); 167 168 /* User Mode Traps */ 169 switch (type) { 170 case EXC_RUNMODETRC: 171 case EXC_TRC: 172 frame->srr1 &= ~PSL_SE; 173 sig = SIGTRAP; 174 break; 175 176 case EXC_DSI: 177 case EXC_ISI: 178 sig = trap_pfault(frame, 1); 179 break; 180 181 case EXC_SC: 182 syscall(frame); 183 break; 184 185 case EXC_FPU: 186 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU, 187 ("FPU already enabled for thread")); 188 enable_fpu(td); 189 break; 190 191#ifdef ALTIVEC 192 case EXC_VEC: 193 if ((vecthread = PCPU_GET(vecthread)) != NULL) { 194 KASSERT(vecthread != td, 195 ("altivec already enabled")); 196 save_vec(vecthread); 197 } 198 PCPU_SET(vecthread, td); 199 td->td_pcb->pcb_veccpu = PCPU_GET(cpuid); 200 enable_vec(td); 201 frame->srr1 |= PSL_VEC; 202 break; 203#else 204 case EXC_VEC: 205 case EXC_VECAST: 206 sig = SIGILL; 207 break; 208#endif /* ALTIVEC */ 209 210 case EXC_ALI: 211 if (fix_unaligned(td, frame) != 0) 212 sig = SIGBUS; 213 else 214 frame->srr0 += 4; 215 break; 216 217 case EXC_PGM: 218 /* XXX temporarily */ 219 /* XXX: Magic Number? */ 220 if (frame->srr1 & 0x0002000) 221 sig = SIGTRAP; 222 else 223 sig = SIGILL; 224 break; 225 226 default: 227 trap_fatal(frame); 228 } 229 } else { 230 /* Kernel Mode Traps */ 231 232 KASSERT(cold || td->td_ucred != NULL, 233 ("kernel trap doesn't have ucred")); 234 switch (type) { 235 case EXC_DSI: 236 if (trap_pfault(frame, 0) == 0) 237 return; 238 break; 239 case EXC_MCHK: 240 if (handle_onfault(frame)) 241 return; 242 break; 243 default: 244 break; 245 } 246 trap_fatal(frame); 247 } 248 249#ifdef ALTIVEC 250 if (td != PCPU_GET(vecthread) || 251 td->td_pcb->pcb_veccpu != PCPU_GET(cpuid)) 252 frame->srr1 &= ~PSL_VEC; 253#endif /* ALTIVEC */ 254 255 if (sig != 0) { 256 if (p->p_sysent->sv_transtrap != NULL) 257 sig = (p->p_sysent->sv_transtrap)(sig, type); 258 ksiginfo_init_trap(&ksi); 259 ksi.ksi_signo = sig; 260 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */ 261 /* ksi.ksi_addr = ? */ 262 ksi.ksi_trapno = type; 263 trapsignal(td, &ksi); 264 } 265 266 userret(td, frame); 267 mtx_assert(&Giant, MA_NOTOWNED); 268} 269 270static void 271trap_fatal(struct trapframe *frame) 272{ 273 274 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR)); 275#ifdef KDB 276 if ((debugger_on_panic || kdb_active) && 277 kdb_trap(frame->exc, 0, frame)) 278 return; 279#endif 280 panic("%s trap", trapname(frame->exc)); 281} 282 283static void 284printtrap(u_int vector, struct trapframe *frame, int isfatal, int user) 285{ 286 287 printf("\n"); 288 printf("%s %s trap:\n", isfatal ? "fatal" : "handled", 289 user ? "user" : "kernel"); 290 printf("\n"); 291 printf(" exception = 0x%x (%s)\n", vector >> 8, 292 trapname(vector)); 293 switch (vector) { 294 case EXC_DSI: 295 printf(" virtual address = 0x%x\n", frame->dar); 296 break; 297 case EXC_ISI: 298 printf(" virtual address = 0x%x\n", frame->srr0); 299 break; 300 } 301 printf(" srr0 = 0x%x\n", frame->srr0); 302 printf(" srr1 = 0x%x\n", frame->srr1); 303 printf(" curthread = %p\n", curthread); 304 if (curthread != NULL) 305 printf(" pid = %d, comm = %s\n", 306 curthread->td_proc->p_pid, curthread->td_proc->p_comm); 307 printf("\n"); 308} 309 310/* 311 * Handles a fatal fault when we have onfault state to recover. Returns 312 * non-zero if there was onfault recovery state available. 313 */ 314static int 315handle_onfault(struct trapframe *frame) 316{ 317 struct thread *td; 318 faultbuf *fb; 319 320 td = curthread; 321 fb = td->td_pcb->pcb_onfault; 322 if (fb != NULL) { 323 frame->srr0 = (*fb)[0]; 324 frame->fixreg[1] = (*fb)[1]; 325 frame->fixreg[2] = (*fb)[2]; 326 frame->fixreg[3] = 1; 327 frame->cr = (*fb)[3]; 328 bcopy(&(*fb)[4], &frame->fixreg[13], 329 19 * sizeof(register_t)); 330 return (1); 331 } 332 return (0); 333} 334 335void 336syscall(struct trapframe *frame) 337{ 338 caddr_t params; 339 struct sysent *callp; 340 struct thread *td; 341 struct proc *p; 342 int error, n; 343 size_t narg; 344 register_t args[10]; 345 u_int code; 346 347 td = PCPU_GET(curthread); 348 p = td->td_proc; 349 350 PCPU_LAZY_INC(cnt.v_syscall); 351 352 if (p->p_flag & P_SA) 353 thread_user_enter(td); 354 355 code = frame->fixreg[0]; 356 params = (caddr_t)(frame->fixreg + FIRSTARG); 357 n = NARGREG; 358 359 if (p->p_sysent->sv_prepsyscall) { 360 /* 361 * The prep code is MP aware. 362 */ 363 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms); 364 } else if (code == SYS_syscall) { 365 /* 366 * code is first argument, 367 * followed by actual args. 368 */ 369 code = *(u_int *) params; 370 params += sizeof(register_t); 371 n -= 1; 372 } else if (code == SYS___syscall) { 373 /* 374 * Like syscall, but code is a quad, 375 * so as to maintain quad alignment 376 * for the rest of the args. 377 */ 378 params += sizeof(register_t); 379 code = *(u_int *) params; 380 params += sizeof(register_t); 381 n -= 2; 382 } 383 384 if (p->p_sysent->sv_mask) 385 code &= p->p_sysent->sv_mask; 386 387 if (code >= p->p_sysent->sv_size) 388 callp = &p->p_sysent->sv_table[0]; 389 else 390 callp = &p->p_sysent->sv_table[code]; 391 392 narg = callp->sy_narg & SYF_ARGMASK; 393 394 if (narg > n) { 395 bcopy(params, args, n * sizeof(register_t)); 396 error = copyin(MOREARGS(frame->fixreg[1]), args + n, 397 (narg - n) * sizeof(register_t)); 398 params = (caddr_t)args; 399 } else 400 error = 0; 401 402 CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", p->p_comm, 403 syscallnames[code], 404 frame->fixreg[FIRSTARG], 405 frame->fixreg[FIRSTARG+1], 406 frame->fixreg[FIRSTARG+2]); 407 408#ifdef KTRACE 409 if (KTRPOINT(td, KTR_SYSCALL)) 410 ktrsyscall(code, narg, (register_t *)params); 411#endif 412 /* 413 * Try to run the syscall without Giant if the syscall is MP safe. 414 */ 415 if ((callp->sy_narg & SYF_MPSAFE) == 0) 416 mtx_lock(&Giant); 417 418 if (error == 0) { 419 td->td_retval[0] = 0; 420 td->td_retval[1] = frame->fixreg[FIRSTARG + 1]; 421 422 STOPEVENT(p, S_SCE, narg); 423 424 PTRACESTOP_SC(p, td, S_PT_SCE); 425 426 error = (*callp->sy_call)(td, params); 427 428 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", p->p_comm, 429 syscallnames[code], td->td_retval[0]); 430 } 431 switch (error) { 432 case 0: 433 if ((frame->fixreg[0] == SYS___syscall) && 434 (code != SYS_lseek)) { 435 /* 436 * 64-bit return, 32-bit syscall. Fixup byte order 437 */ 438 frame->fixreg[FIRSTARG] = 0; 439 frame->fixreg[FIRSTARG + 1] = td->td_retval[0]; 440 } else { 441 frame->fixreg[FIRSTARG] = td->td_retval[0]; 442 frame->fixreg[FIRSTARG + 1] = td->td_retval[1]; 443 } 444 /* XXX: Magic number */ 445 frame->cr &= ~0x10000000; 446 break; 447 case ERESTART: 448 /* 449 * Set user's pc back to redo the system call. 450 */ 451 frame->srr0 -= 4; 452 break; 453 case EJUSTRETURN: 454 /* nothing to do */ 455 break; 456 default: 457 if (p->p_sysent->sv_errsize) { 458 if (error >= p->p_sysent->sv_errsize) 459 error = -1; /* XXX */ 460 else 461 error = p->p_sysent->sv_errtbl[error]; 462 } 463 frame->fixreg[FIRSTARG] = error; 464 /* XXX: Magic number: Carry Flag Equivalent? */ 465 frame->cr |= 0x10000000; 466 break; 467 } 468 469 470 if ((callp->sy_narg & SYF_MPSAFE) == 0) 471 mtx_unlock(&Giant); 472 473#ifdef KTRACE 474 if (KTRPOINT(td, KTR_SYSRET)) 475 ktrsysret(code, error, td->td_retval[0]); 476#endif 477 478 /* 479 * Does the comment in the i386 code about errno apply here? 480 */ 481 STOPEVENT(p, S_SCX, code); 482 483 PTRACESTOP_SC(p, td, S_PT_SCX); 484 485 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning", 486 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"); 487 mtx_assert(&sched_lock, MA_NOTOWNED); 488 mtx_assert(&Giant, MA_NOTOWNED); 489} 490 491static int 492trap_pfault(struct trapframe *frame, int user) 493{ 494 vm_offset_t eva, va; 495 struct thread *td; 496 struct proc *p; 497 vm_map_t map; 498 vm_prot_t ftype; 499 int rv; 500 u_int user_sr; 501 502 td = curthread; 503 p = td->td_proc; 504 if (frame->exc == EXC_ISI) { 505 eva = frame->srr0; 506 ftype = VM_PROT_READ | VM_PROT_EXECUTE; 507 } else { 508 eva = frame->dar; 509 if (frame->dsisr & DSISR_STORE) 510 ftype = VM_PROT_WRITE; 511 else 512 ftype = VM_PROT_READ; 513 } 514 515 if (user) { 516 map = &p->p_vmspace->vm_map; 517 } else { 518 if ((eva >> ADDR_SR_SHFT) == USER_SR) { 519 if (p->p_vmspace == NULL) 520 return (SIGSEGV); 521 522 __asm ("mfsr %0, %1" 523 : "=r"(user_sr) 524 : "K"(USER_SR)); 525 eva &= ADDR_PIDX | ADDR_POFF; 526 eva |= user_sr << ADDR_SR_SHFT; 527 map = &p->p_vmspace->vm_map; 528 } else { 529 map = kernel_map; 530 } 531 } 532 va = trunc_page(eva); 533 534 if (map != kernel_map) { 535 /* 536 * Keep swapout from messing with us during this 537 * critical time. 538 */ 539 PROC_LOCK(p); 540 ++p->p_lock; 541 PROC_UNLOCK(p); 542 543 /* Fault in the user page: */ 544 rv = vm_fault(map, va, ftype, 545 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY 546 : VM_FAULT_NORMAL); 547 548 PROC_LOCK(p); 549 --p->p_lock; 550 PROC_UNLOCK(p); 551 } else { 552 /* 553 * Don't have to worry about process locking or stacks in the 554 * kernel. 555 */ 556 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 557 } 558 559 if (rv == KERN_SUCCESS) 560 return (0); 561 562 if (!user && handle_onfault(frame)) 563 return (0); 564 565 return (SIGSEGV); 566} 567 568static __inline void 569setusr(u_int content) 570{ 571 __asm __volatile ("isync; mtsr %0,%1; isync" 572 :: "n"(USER_SR), "r"(content)); 573} 574 575int 576badaddr(void *addr, size_t size) 577{ 578 return (badaddr_read(addr, size, NULL)); 579} 580 581int 582badaddr_read(void *addr, size_t size, int *rptr) 583{ 584 struct thread *td; 585 faultbuf env; 586 int x; 587 588 /* Get rid of any stale machine checks that have been waiting. */ 589 __asm __volatile ("sync; isync"); 590 591 td = PCPU_GET(curthread); 592 593 if (setfault(env)) { 594 td->td_pcb->pcb_onfault = 0; 595 __asm __volatile ("sync"); 596 return 1; 597 } 598 599 __asm __volatile ("sync"); 600 601 switch (size) { 602 case 1: 603 x = *(volatile int8_t *)addr; 604 break; 605 case 2: 606 x = *(volatile int16_t *)addr; 607 break; 608 case 4: 609 x = *(volatile int32_t *)addr; 610 break; 611 default: 612 panic("badaddr: invalid size (%d)", size); 613 } 614 615 /* Make sure we took the machine check, if we caused one. */ 616 __asm __volatile ("sync; isync"); 617 618 td->td_pcb->pcb_onfault = 0; 619 __asm __volatile ("sync"); /* To be sure. */ 620 621 /* Use the value to avoid reorder. */ 622 if (rptr) 623 *rptr = x; 624 625 return (0); 626} 627 628/* 629 * For now, this only deals with the particular unaligned access case 630 * that gcc tends to generate. Eventually it should handle all of the 631 * possibilities that can happen on a 32-bit PowerPC in big-endian mode. 632 */ 633 634static int 635fix_unaligned(struct thread *td, struct trapframe *frame) 636{ 637 struct thread *fputhread; 638 int indicator, reg; 639 double *fpr; 640 641 indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr); 642 643 switch (indicator) { 644 case EXC_ALI_LFD: 645 case EXC_ALI_STFD: 646 reg = EXC_ALI_RST(frame->dsisr); 647 fpr = &td->td_pcb->pcb_fpu.fpr[reg]; 648 fputhread = PCPU_GET(fputhread); 649 650 /* Juggle the FPU to ensure that we've initialized 651 * the FPRs, and that their current state is in 652 * the PCB. 653 */ 654 if (fputhread != td) { 655 if (fputhread) 656 save_fpu(fputhread); 657 enable_fpu(td); 658 } 659 save_fpu(td); 660 661 if (indicator == EXC_ALI_LFD) { 662 if (copyin((void *)frame->dar, fpr, 663 sizeof(double)) != 0) 664 return -1; 665 enable_fpu(td); 666 } else { 667 if (copyout(fpr, (void *)frame->dar, 668 sizeof(double)) != 0) 669 return -1; 670 } 671 return 0; 672 break; 673 } 674 675 return -1; 676} 677