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