trap.c revision 99900
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 99900 2002-07-13 04:36:50Z mini $"; 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 save_fpu(fputhread); 195 } 196 PCPU_SET(fputhread, td); 197 td->td_pcb->pcb_fpcpu = PCPU_GET(cpuid); 198 enable_fpu(td); 199 frame->srr1 |= PSL_FP; 200 break; 201 202#ifdef ALTIVEC 203 case EXC_VEC: 204 if ((vecthread = PCPU_GET(vecthread)) != NULL) { 205 KASSERT(vecthread != td, 206 ("altivec already enabled")); 207 save_vec(vecthread); 208 } 209 PCPU_SET(vecthread, td); 210 td->td_pcb->pcb_veccpu = PCPU_GET(cpuid); 211 enable_vec(td); 212 frame->srr1 |= PSL_VEC; 213 break; 214#endif /* ALTIVEC */ 215 216 case EXC_ALI: 217 if (fix_unaligned(td, frame) != 0) 218 sig = SIGBUS; 219 else 220 frame->srr0 += 4; 221 break; 222 223 case EXC_PGM: 224 /* XXX temporarily */ 225 /* XXX: Magic Number? */ 226 if (frame->srr1 & 0x0002000) 227 sig = SIGTRAP; 228 else 229 sig = SIGILL; 230 break; 231 232 default: 233 trap_fatal(frame); 234 } 235 } else { 236 /* Kernel Mode Traps */ 237 238 KASSERT(cold || td->td_ucred != NULL, 239 ("kernel trap doesn't have ucred")); 240 switch (type) { 241 case EXC_DSI: 242 if (trap_pfault(frame, 0) == 0) 243 return; 244 break; 245 case EXC_MCHK: 246 if (handle_onfault(frame)) 247 return; 248 break; 249 default: 250 trap_fatal(frame); 251 } 252 } 253 254 if (td != PCPU_GET(fputhread) || 255 td->td_pcb->pcb_fpcpu != PCPU_GET(cpuid)) 256 frame->srr1 &= ~PSL_FP; 257 258#ifdef ALTIVEC 259 if (td != PCPU_GET(vecthread) || 260 td->td_pcb->pcb_veccpu != PCPU_GET(cpuid)) 261 frame->srr1 &= ~PSL_VEC; 262#endif /* ALTIVEC */ 263 264 if (sig != 0) { 265 if (p->p_sysent->sv_transtrap != NULL) 266 sig = (p->p_sysent->sv_transtrap)(sig, type); 267 trapsignal(p, sig, ucode); 268 } 269 270 userret(td, frame, sticks); 271 mtx_assert(&Giant, MA_NOTOWNED); 272#ifdef DIAGNOSTIC 273 cred_free_thread(td); 274#endif /* DIAGNOSTIC */ 275} 276 277static void 278trap_fatal(struct trapframe *frame) 279{ 280 281 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR)); 282#ifdef DDB 283 if ((debugger_on_panic || db_active) && kdb_trap(frame->exc, 0, frame)) 284 return; 285#endif 286 panic("%s trap", trapname(frame->exc)); 287} 288 289static void 290printtrap(u_int vector, struct trapframe *frame, int isfatal, int user) 291{ 292 293 printf("\n"); 294 printf("%s %s trap:\n", isfatal ? "fatal" : "handled", 295 user ? "user" : "kernel"); 296 printf("\n"); 297 printf(" exception = 0x%x (%s)\n", vector >> 8, 298 trapname(vector)); 299 switch (vector) { 300 case EXC_DSI: 301 printf(" virtual address = 0x%x\n", frame->dar); 302 break; 303 case EXC_ISI: 304 printf(" virtual address = 0x%x\n", frame->srr0); 305 break; 306 } 307 printf(" srr0 = 0x%x\n", frame->srr0); 308 printf(" srr1 = 0x%x\n", frame->srr1); 309 printf(" curthread = %p\n", curthread); 310 if (curthread != NULL) 311 printf(" pid = %d, comm = %s\n", 312 curthread->td_proc->p_pid, curthread->td_proc->p_comm); 313 printf("\n"); 314} 315 316/* 317 * Handles a fatal fault when we have onfault state to recover. Returns 318 * non-zero if there was onfault recovery state available. 319 */ 320static int 321handle_onfault(struct trapframe *frame) 322{ 323 struct thread *td; 324 faultbuf *fb; 325 326 td = curthread; 327 fb = td->td_pcb->pcb_onfault; 328 if (fb != NULL) { 329 frame->srr0 = (*fb)[0]; 330 frame->fixreg[1] = (*fb)[1]; 331 frame->fixreg[2] = (*fb)[2]; 332 frame->cr = (*fb)[3]; 333 bcopy(&(*fb)[4], &frame->fixreg[13], 334 19 * sizeof(register_t)); 335 return (1); 336 } 337 return (0); 338} 339 340void 341syscall(struct trapframe *frame) 342{ 343 caddr_t params; 344 struct sysent *callp; 345 struct thread *td; 346 struct proc *p; 347 int error, n; 348 size_t narg; 349 register_t args[10]; 350 u_int code; 351 352 td = PCPU_GET(curthread); 353 p = td->td_proc; 354 355 atomic_add_int(&cnt.v_syscall, 1); 356 357 code = frame->fixreg[0]; 358 params = (caddr_t)(frame->fixreg + FIRSTARG); 359 n = NARGREG; 360 361 if (p->p_sysent->sv_prepsyscall) { 362 /* 363 * The prep code is MP aware. 364 */ 365 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms); 366 } else if (code == SYS_syscall) { 367 /* 368 * code is first argument, 369 * followed by actual args. 370 */ 371 code = *params++; 372 n -= 1; 373 } else if (code == SYS___syscall) { 374 /* 375 * Like syscall, but code is a quad, 376 * so as to maintain quad alignment 377 * for the rest of the args. 378 */ 379 params++; 380 code = *params++; 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 * sizeof(register_t)) { 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#ifdef KTRACE 403 if (KTRPOINT(td, KTR_SYSCALL)) 404 ktrsyscall(code, narg, params); 405#endif 406 /* 407 * Try to run the syscall without Giant if the syscall is MP safe. 408 */ 409 if ((callp->sy_narg & SYF_MPSAFE) == 0) 410 mtx_lock(&Giant); 411 412 if (error == 0) { 413 td->td_retval[0] = 0; 414 td->td_retval[1] = frame->fixreg[FIRSTARG + 1]; 415 416 STOPEVENT(p, S_SCE, narg); 417 418 error = (*callp->sy_call)(td, params); 419 } 420 switch (error) { 421 case 0: 422 frame->fixreg[FIRSTARG] = td->td_retval[0]; 423 frame->fixreg[FIRSTARG + 1] = td->td_retval[1]; 424 /* XXX: Magic number */ 425 frame->cr &= ~0x10000000; 426 break; 427 case ERESTART: 428 /* 429 * Set user's pc back to redo the system call. 430 */ 431 frame->srr0 -= 4; 432 break; 433 case EJUSTRETURN: 434 /* nothing to do */ 435 break; 436 default: 437 if (p->p_sysent->sv_errsize) { 438 if (error >= p->p_sysent->sv_errsize) 439 error = -1; /* XXX */ 440 else 441 error = p->p_sysent->sv_errtbl[error]; 442 } 443 frame->fixreg[FIRSTARG] = error; 444 /* XXX: Magic number: Carry Flag Equivalent? */ 445 frame->cr |= 0x10000000; 446 break; 447 } 448 449 450 if ((callp->sy_narg & SYF_MPSAFE) == 0) 451 mtx_unlock(&Giant); 452 453#ifdef KTRACE 454 if (KTRPOINT(td, KTR_SYSRET)) 455 ktrsysret(code, error, td->td_retval[0]); 456#endif 457 458 /* 459 * Does the comment in the i386 code about errno apply here? 460 */ 461 STOPEVENT(p, S_SCX, code); 462 463#ifdef WITNESS 464 if (witness_list(td)) { 465 panic("system call %s returning with mutex(s) held\n", 466 syscallnames[code]); 467 } 468#endif 469 mtx_assert(&sched_lock, MA_NOTOWNED); 470 mtx_assert(&Giant, MA_NOTOWNED); 471} 472 473static int 474trap_pfault(struct trapframe *frame, int user) 475{ 476 vm_offset_t eva, va; 477 struct thread *td; 478 struct proc *p; 479 vm_map_t map; 480 vm_prot_t ftype; 481 int rv; 482 u_int user_sr; 483 484 td = curthread; 485 p = td->td_proc; 486 if (frame->exc == EXC_ISI) { 487 eva = frame->srr0; 488 ftype = VM_PROT_READ | VM_PROT_EXECUTE; 489 } else { 490 eva = frame->dar; 491 if (frame->dsisr & DSISR_STORE) 492 ftype = VM_PROT_READ | VM_PROT_WRITE; 493 else 494 ftype = VM_PROT_READ; 495 } 496 497 if (user) { 498 map = &p->p_vmspace->vm_map; 499 } else { 500 if ((eva >> ADDR_SR_SHFT) == USER_SR) { 501 if (p->p_vmspace == NULL) 502 return (SIGSEGV); 503 504 __asm ("mfsr %0, %1" 505 : "=r"(user_sr) 506 : "K"(USER_SR)); 507 eva &= ADDR_PIDX | ADDR_POFF; 508 eva |= user_sr << ADDR_SR_SHFT; 509 map = &p->p_vmspace->vm_map; 510 } else { 511 map = kernel_map; 512 } 513 } 514 va = trunc_page(eva); 515 516 mtx_lock(&Giant); 517 if (map != kernel_map) { 518 /* 519 * Keep swapout from messing with us during this 520 * critical time. 521 */ 522 PROC_LOCK(p); 523 ++p->p_lock; 524 PROC_UNLOCK(p); 525 526 /* Fault in the user page: */ 527 rv = vm_fault(map, va, ftype, 528 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY 529 : VM_FAULT_NORMAL); 530 531 PROC_LOCK(p); 532 --p->p_lock; 533 PROC_UNLOCK(p); 534 } else { 535 /* 536 * Don't have to worry about process locking or stacks in the 537 * kernel. 538 */ 539 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 540 } 541 mtx_unlock(&Giant); 542 543 if (rv == KERN_SUCCESS) 544 return (0); 545 546 if (!user && handle_onfault(frame)) 547 return (0); 548 549 return (SIGSEGV); 550} 551 552static __inline void 553setusr(u_int content) 554{ 555 __asm __volatile ("isync; mtsr %0,%1; isync" 556 :: "n"(USER_SR), "r"(content)); 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->dsisr); 626 627 switch (indicator) { 628 case EXC_ALI_LFD: 629 case EXC_ALI_STFD: 630 reg = EXC_ALI_RST(frame->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->dar, fpr, 647 sizeof(double)) != 0) 648 return -1; 649 enable_fpu(td); 650 } else { 651 if (copyout(fpr, (void *)frame->dar, 652 sizeof(double)) != 0) 653 return -1; 654 } 655 return 0; 656 break; 657 } 658 659 return -1; 660} 661