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