trap.c revision 204197
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 204197 2010-02-22 14:17:23Z 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 ppc_instr_emulate(struct trapframe *frame); 86static int handle_onfault(struct trapframe *frame); 87static void syscall(struct trapframe *frame); 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_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)", td->td_name, 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 case EXC_VEC: 192 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC, 193 ("Altivec already enabled for thread")); 194 enable_vec(td); 195 break; 196 197 case EXC_VECAST: 198 printf("Vector assist exception!\n"); 199 sig = SIGILL; 200 break; 201 202 case EXC_ALI: 203 if (fix_unaligned(td, frame) != 0) 204 sig = SIGBUS; 205 else 206 frame->srr0 += 4; 207 break; 208 209 case EXC_PGM: 210 /* Identify the trap reason */ 211 if (frame->srr1 & EXC_PGM_TRAP) 212 sig = SIGTRAP; 213 else if (ppc_instr_emulate(frame) == 0) 214 frame->srr0 += 4; 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 if (sig != 0) { 243 if (p->p_sysent->sv_transtrap != NULL) 244 sig = (p->p_sysent->sv_transtrap)(sig, type); 245 ksiginfo_init_trap(&ksi); 246 ksi.ksi_signo = sig; 247 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */ 248 /* ksi.ksi_addr = ? */ 249 ksi.ksi_trapno = type; 250 trapsignal(td, &ksi); 251 } 252 253 userret(td, frame); 254 mtx_assert(&Giant, MA_NOTOWNED); 255} 256 257static void 258trap_fatal(struct trapframe *frame) 259{ 260 261 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR)); 262#ifdef KDB 263 if ((debugger_on_panic || kdb_active) && 264 kdb_trap(frame->exc, 0, frame)) 265 return; 266#endif 267 panic("%s trap", trapname(frame->exc)); 268} 269 270static void 271printtrap(u_int vector, struct trapframe *frame, int isfatal, int user) 272{ 273 274 printf("\n"); 275 printf("%s %s trap:\n", isfatal ? "fatal" : "handled", 276 user ? "user" : "kernel"); 277 printf("\n"); 278 printf(" exception = 0x%x (%s)\n", vector >> 8, 279 trapname(vector)); 280 switch (vector) { 281 case EXC_DSI: 282 printf(" virtual address = 0x%x\n", frame->cpu.aim.dar); 283 break; 284 case EXC_ISI: 285 printf(" virtual address = 0x%x\n", frame->srr0); 286 break; 287 } 288 printf(" srr0 = 0x%x\n", frame->srr0); 289 printf(" srr1 = 0x%x\n", frame->srr1); 290 printf(" lr = 0x%x\n", frame->lr); 291 printf(" curthread = %p\n", curthread); 292 if (curthread != NULL) 293 printf(" pid = %d, comm = %s\n", 294 curthread->td_proc->p_pid, curthread->td_name); 295 printf("\n"); 296} 297 298/* 299 * Handles a fatal fault when we have onfault state to recover. Returns 300 * non-zero if there was onfault recovery state available. 301 */ 302static int 303handle_onfault(struct trapframe *frame) 304{ 305 struct thread *td; 306 faultbuf *fb; 307 308 td = curthread; 309 fb = td->td_pcb->pcb_onfault; 310 if (fb != NULL) { 311 frame->srr0 = (*fb)[0]; 312 frame->fixreg[1] = (*fb)[1]; 313 frame->fixreg[2] = (*fb)[2]; 314 frame->fixreg[3] = 1; 315 frame->cr = (*fb)[3]; 316 bcopy(&(*fb)[4], &frame->fixreg[13], 317 19 * sizeof(register_t)); 318 return (1); 319 } 320 return (0); 321} 322 323void 324syscall(struct trapframe *frame) 325{ 326 caddr_t params; 327 struct sysent *callp; 328 struct thread *td; 329 struct proc *p; 330 int error, n; 331 size_t narg; 332 register_t args[10]; 333 u_int code; 334 335 td = PCPU_GET(curthread); 336 p = td->td_proc; 337 338 PCPU_INC(cnt.v_syscall); 339 340 code = frame->fixreg[0]; 341 params = (caddr_t)(frame->fixreg + FIRSTARG); 342 n = NARGREG; 343 344 if (p->p_sysent->sv_prepsyscall) { 345 /* 346 * The prep code is MP aware. 347 */ 348 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms); 349 } else if (code == SYS_syscall) { 350 /* 351 * code is first argument, 352 * followed by actual args. 353 */ 354 code = *(u_int *) params; 355 params += sizeof(register_t); 356 n -= 1; 357 } else if (code == SYS___syscall) { 358 /* 359 * Like syscall, but code is a quad, 360 * so as to maintain quad alignment 361 * for the rest of the args. 362 */ 363 params += sizeof(register_t); 364 code = *(u_int *) params; 365 params += sizeof(register_t); 366 n -= 2; 367 } 368 369 if (p->p_sysent->sv_mask) 370 code &= p->p_sysent->sv_mask; 371 372 if (code >= p->p_sysent->sv_size) 373 callp = &p->p_sysent->sv_table[0]; 374 else 375 callp = &p->p_sysent->sv_table[code]; 376 377 narg = callp->sy_narg; 378 379 if (narg > n) { 380 bcopy(params, args, n * sizeof(register_t)); 381 error = copyin(MOREARGS(frame->fixreg[1]), args + n, 382 (narg - n) * sizeof(register_t)); 383 params = (caddr_t)args; 384 } else 385 error = 0; 386 387 CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", td->td_name, 388 syscallnames[code], 389 frame->fixreg[FIRSTARG], 390 frame->fixreg[FIRSTARG+1], 391 frame->fixreg[FIRSTARG+2]); 392 393#ifdef KTRACE 394 if (KTRPOINT(td, KTR_SYSCALL)) 395 ktrsyscall(code, narg, (register_t *)params); 396#endif 397 398 td->td_syscalls++; 399 400 if (error == 0) { 401 td->td_retval[0] = 0; 402 td->td_retval[1] = frame->fixreg[FIRSTARG + 1]; 403 404 STOPEVENT(p, S_SCE, narg); 405 406 PTRACESTOP_SC(p, td, S_PT_SCE); 407 408 AUDIT_SYSCALL_ENTER(code, td); 409 error = (*callp->sy_call)(td, params); 410 AUDIT_SYSCALL_EXIT(error, td); 411 412 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", td->td_name, 413 syscallnames[code], td->td_retval[0]); 414 } 415 416 cpu_set_syscall_retval(td, error); 417 418 /* 419 * Check for misbehavior. 420 */ 421 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning", 422 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"); 423 KASSERT(td->td_critnest == 0, 424 ("System call %s returning in a critical section", 425 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???")); 426 KASSERT(td->td_locks == 0, 427 ("System call %s returning with %d locks held", 428 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???", 429 td->td_locks)); 430 431#ifdef KTRACE 432 if (KTRPOINT(td, KTR_SYSRET)) 433 ktrsysret(code, error, td->td_retval[0]); 434#endif 435 436 /* 437 * Does the comment in the i386 code about errno apply here? 438 */ 439 STOPEVENT(p, S_SCX, code); 440 441 PTRACESTOP_SC(p, td, S_PT_SCX); 442} 443 444static int 445trap_pfault(struct trapframe *frame, int user) 446{ 447 vm_offset_t eva, va; 448 struct thread *td; 449 struct proc *p; 450 vm_map_t map; 451 vm_prot_t ftype; 452 int rv; 453 u_int user_sr; 454 455 td = curthread; 456 p = td->td_proc; 457 if (frame->exc == EXC_ISI) { 458 eva = frame->srr0; 459 ftype = VM_PROT_READ | VM_PROT_EXECUTE; 460 } else { 461 eva = frame->cpu.aim.dar; 462 if (frame->cpu.aim.dsisr & DSISR_STORE) 463 ftype = VM_PROT_WRITE; 464 else 465 ftype = VM_PROT_READ; 466 } 467 468 if (user) { 469 map = &p->p_vmspace->vm_map; 470 } else { 471 if ((eva >> ADDR_SR_SHFT) == USER_SR) { 472 if (p->p_vmspace == NULL) 473 return (SIGSEGV); 474 475 __asm ("mfsr %0, %1" 476 : "=r"(user_sr) 477 : "K"(USER_SR)); 478 eva &= ADDR_PIDX | ADDR_POFF; 479 eva |= user_sr << ADDR_SR_SHFT; 480 map = &p->p_vmspace->vm_map; 481 } else { 482 map = kernel_map; 483 } 484 } 485 va = trunc_page(eva); 486 487 if (map != kernel_map) { 488 /* 489 * Keep swapout from messing with us during this 490 * critical time. 491 */ 492 PROC_LOCK(p); 493 ++p->p_lock; 494 PROC_UNLOCK(p); 495 496 /* Fault in the user page: */ 497 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 498 499 PROC_LOCK(p); 500 --p->p_lock; 501 PROC_UNLOCK(p); 502 } else { 503 /* 504 * Don't have to worry about process locking or stacks in the 505 * kernel. 506 */ 507 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 508 } 509 510 if (rv == KERN_SUCCESS) 511 return (0); 512 513 if (!user && handle_onfault(frame)) 514 return (0); 515 516 return (SIGSEGV); 517} 518 519int 520badaddr(void *addr, size_t size) 521{ 522 return (badaddr_read(addr, size, NULL)); 523} 524 525int 526badaddr_read(void *addr, size_t size, int *rptr) 527{ 528 struct thread *td; 529 faultbuf env; 530 int x; 531 532 /* Get rid of any stale machine checks that have been waiting. */ 533 __asm __volatile ("sync; isync"); 534 535 td = PCPU_GET(curthread); 536 537 if (setfault(env)) { 538 td->td_pcb->pcb_onfault = 0; 539 __asm __volatile ("sync"); 540 return 1; 541 } 542 543 __asm __volatile ("sync"); 544 545 switch (size) { 546 case 1: 547 x = *(volatile int8_t *)addr; 548 break; 549 case 2: 550 x = *(volatile int16_t *)addr; 551 break; 552 case 4: 553 x = *(volatile int32_t *)addr; 554 break; 555 default: 556 panic("badaddr: invalid size (%d)", size); 557 } 558 559 /* Make sure we took the machine check, if we caused one. */ 560 __asm __volatile ("sync; isync"); 561 562 td->td_pcb->pcb_onfault = 0; 563 __asm __volatile ("sync"); /* To be sure. */ 564 565 /* Use the value to avoid reorder. */ 566 if (rptr) 567 *rptr = x; 568 569 return (0); 570} 571 572/* 573 * For now, this only deals with the particular unaligned access case 574 * that gcc tends to generate. Eventually it should handle all of the 575 * possibilities that can happen on a 32-bit PowerPC in big-endian mode. 576 */ 577 578static int 579fix_unaligned(struct thread *td, struct trapframe *frame) 580{ 581 struct thread *fputhread; 582 int indicator, reg; 583 double *fpr; 584 585 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr); 586 587 switch (indicator) { 588 case EXC_ALI_LFD: 589 case EXC_ALI_STFD: 590 reg = EXC_ALI_RST(frame->cpu.aim.dsisr); 591 fpr = &td->td_pcb->pcb_fpu.fpr[reg]; 592 fputhread = PCPU_GET(fputhread); 593 594 /* Juggle the FPU to ensure that we've initialized 595 * the FPRs, and that their current state is in 596 * the PCB. 597 */ 598 if (fputhread != td) { 599 if (fputhread) 600 save_fpu(fputhread); 601 enable_fpu(td); 602 } 603 save_fpu(td); 604 605 if (indicator == EXC_ALI_LFD) { 606 if (copyin((void *)frame->cpu.aim.dar, fpr, 607 sizeof(double)) != 0) 608 return -1; 609 enable_fpu(td); 610 } else { 611 if (copyout(fpr, (void *)frame->cpu.aim.dar, 612 sizeof(double)) != 0) 613 return -1; 614 } 615 return 0; 616 break; 617 } 618 619 return -1; 620} 621 622static int 623ppc_instr_emulate(struct trapframe *frame) 624{ 625 uint32_t instr; 626 int reg; 627 628 instr = fuword32((void *)frame->srr0); 629 630 if ((instr & 0xfc1fffff) == 0x7c1f42a6) { /* mfpvr */ 631 reg = (instr & ~0xfc1fffff) >> 21; 632 frame->fixreg[reg] = mfpvr(); 633 return (0); 634 } 635 636 return (-1); 637} 638 639