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