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