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