1 /* $NetBSD: trap.c,v 1.3 2002/02/14 07:08:13 chs Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 40 * Copyright (C) 1995, 1996 TooLs GmbH. 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by TooLs GmbH. 54 * 4. The name of TooLs GmbH may not be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 #include "opt_altivec.h" 70 #include "opt_ddb.h" 71 #include "opt_ktrace.h" 72 #include "opt_syscall_debug.h" 73 74 #include <sys/param.h> 75 #include <sys/proc.h> 76 #include <sys/reboot.h> 77 #include <sys/syscall.h> 78 #include <sys/systm.h> 79 #include <sys/user.h> 80 #include <sys/ktrace.h> 81 82 #include <uvm/uvm_extern.h> 83 84 #include <dev/cons.h> 85 86 #include <machine/cpu.h> 87 #include <machine/db_machdep.h> 88 #include <machine/fpu.h> 89 #include <machine/frame.h> 90 #include <machine/pcb.h> 91 #include <machine/psl.h> 92 #include <machine/trap.h> 93 94 #include <powerpc/spr.h> 95 #include <powerpc/ibm4xx/pmap.h> 96 #include <powerpc/ibm4xx/tlb.h> 97 #include <powerpc/fpu/fpu_extern.h> 98 99 /* These definitions should probably be somewhere else XXX */ 100 #define FIRSTARG 3 /* first argument is in reg 3 */ 101 #define NARGREG 8 /* 8 args are in registers */ 102 #define MOREARGS(sp) ((caddr_t)((int)(sp) + 8)) /* more args go here */ 103 104 #ifndef MULTIPROCESSOR 105 volatile int astpending; 106 volatile int want_resched; 107 #endif 108 109 void *syscall = NULL; /* XXX dummy symbol for emul_netbsd */ 110 111 static int fix_unaligned __P((struct proc *p, struct trapframe *frame)); 112 113 void trap __P((struct trapframe *)); /* Called from locore / trap_subr */ 114 int setfault __P((faultbuf)); /* defined in locore.S */ 115 /* Why are these not defined in a header? */ 116 int badaddr __P((void *, size_t)); 117 int badaddr_read __P((void *, size_t, int *)); 118 int ctx_setup __P((int, int)); 119 120 #ifdef DEBUG 121 #define TDB_ALL 0x1 122 int trapdebug = /* TDB_ALL */ 0; 123 #define DBPRINTF(x, y) if (trapdebug & (x)) printf y 124 #else 125 #define DBPRINTF(x, y) 126 #endif 127 128 void 129 trap(struct trapframe *frame) 130 { 131 struct proc *p = curproc; 132 int type = frame->exc; 133 int ftype, rv; 134 135 KASSERT(p == 0 || (p->p_stat == SONPROC)); 136 137 if (frame->srr1 & PSL_PR) 138 type |= EXC_USER; 139 140 ftype = VM_PROT_READ; 141 142 DBPRINTF(TDB_ALL, ("trap(%x) at %x from frame %p &frame %p\n", 143 type, frame->srr0, frame, &frame)); 144 145 switch (type) { 146 case EXC_DEBUG|EXC_USER: 147 { 148 int srr2, srr3; 149 __asm __volatile("mfspr %0,0x3f0" : "=r" (rv), "=r" (srr2), "=r" (srr3) :); 150 printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, srr3); 151 } 152 /* 153 * DEBUG intr -- probably single-step. 154 */ 155 case EXC_TRC|EXC_USER: 156 KERNEL_PROC_LOCK(p); 157 frame->srr1 &= ~PSL_SE; 158 trapsignal(p, SIGTRAP, EXC_TRC); 159 KERNEL_PROC_UNLOCK(p); 160 break; 161 162 /* If we could not find and install appropriate TLB entry, fall through */ 163 164 case EXC_DSI: 165 /* FALLTHROUGH */ 166 case EXC_DTMISS: 167 { 168 struct vm_map *map; 169 vaddr_t va; 170 faultbuf *fb = NULL; 171 172 KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE); 173 va = frame->dear; 174 if (frame->pid == KERNEL_PID) { 175 map = kernel_map; 176 } else { 177 map = &p->p_vmspace->vm_map; 178 } 179 180 if (frame->esr & (ESR_DST|ESR_DIZ)) 181 ftype = VM_PROT_WRITE; 182 183 DBPRINTF(TDB_ALL, ("trap(EXC_DSI) at %x %s fault on %p esr %x\n", 184 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", (void *)va, frame->esr)); 185 rv = uvm_fault(map, trunc_page(va), 0, ftype); 186 KERNEL_UNLOCK(); 187 if (rv == 0) 188 goto done; 189 if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) { 190 frame->pid = KERNEL_PID; 191 frame->srr0 = (*fb)[0]; 192 frame->srr1 |= PSL_IR; /* Re-enable IMMU */ 193 frame->fixreg[1] = (*fb)[1]; 194 frame->fixreg[2] = (*fb)[2]; 195 frame->fixreg[3] = 1; /* Return TRUE */ 196 frame->cr = (*fb)[3]; 197 memcpy(&frame->fixreg[13], &(*fb)[4], 198 19 * sizeof(register_t)); 199 goto done; 200 } 201 } 202 goto brain_damage; 203 204 case EXC_DSI|EXC_USER: 205 /* FALLTHROUGH */ 206 case EXC_DTMISS|EXC_USER: 207 KERNEL_PROC_LOCK(p); 208 209 if (frame->esr & (ESR_DST|ESR_DIZ)) 210 ftype = VM_PROT_WRITE; 211 212 DBPRINTF(TDB_ALL, ("trap(EXC_DSI|EXC_USER) at %x %s fault on %x %x\n", 213 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->dear, frame->esr)); 214 KASSERT(p == curproc && (p->p_stat == SONPROC)); 215 rv = uvm_fault(&p->p_vmspace->vm_map, 216 trunc_page(frame->dear), 0, ftype); 217 if (rv == 0) { 218 KERNEL_PROC_UNLOCK(p); 219 break; 220 } 221 if (rv == ENOMEM) { 222 printf("UVM: pid %d (%s), uid %d killed: " 223 "out of swap\n", 224 p->p_pid, p->p_comm, 225 p->p_cred && p->p_ucred ? 226 p->p_ucred->cr_uid : -1); 227 trapsignal(p, SIGKILL, EXC_DSI); 228 } else { 229 trapsignal(p, SIGSEGV, EXC_DSI); 230 } 231 KERNEL_PROC_UNLOCK(p); 232 break; 233 case EXC_ITMISS|EXC_USER: 234 case EXC_ISI|EXC_USER: 235 KERNEL_PROC_LOCK(p); 236 ftype = VM_PROT_READ | VM_PROT_EXECUTE; 237 DBPRINTF(TDB_ALL, ("trap(EXC_ISI|EXC_USER) at %x %s fault on %x tf %p\n", 238 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->srr0, frame)); 239 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0), 0, ftype); 240 if (rv == 0) { 241 KERNEL_PROC_UNLOCK(p); 242 break; 243 } 244 trapsignal(p, SIGSEGV, EXC_ISI); 245 KERNEL_PROC_UNLOCK(p); 246 break; 247 case EXC_SC|EXC_USER: 248 { 249 const struct sysent *callp; 250 size_t argsize; 251 register_t code, error; 252 register_t *params, rval[2]; 253 int n; 254 register_t args[10]; 255 256 KERNEL_PROC_LOCK(p); 257 258 uvmexp.syscalls++; 259 260 code = frame->fixreg[0]; 261 callp = p->p_emul->e_sysent; 262 params = frame->fixreg + FIRSTARG; 263 n = NARGREG; 264 265 switch (code) { 266 case SYS_syscall: 267 /* 268 * code is first argument, 269 * followed by actual args. 270 */ 271 code = *params++; 272 n -= 1; 273 break; 274 case SYS___syscall: 275 params++; 276 code = *params++; 277 n -= 2; 278 break; 279 default: 280 break; 281 } 282 283 code &= (SYS_NSYSENT - 1); 284 callp += code; 285 argsize = callp->sy_argsize; 286 287 if (argsize > n * sizeof(register_t)) { 288 memcpy(args, params, n * sizeof(register_t)); 289 error = copyin(MOREARGS(frame->fixreg[1]), 290 args + n, 291 argsize - n * sizeof(register_t)); 292 if (error) 293 goto syscall_bad; 294 params = args; 295 } 296 297 #ifdef KTRACE 298 if (KTRPOINT(p, KTR_SYSCALL)) 299 ktrsyscall(p, code, argsize, params); 300 #endif 301 #ifdef SYSCALL_DEBUG 302 if (trapdebug) 303 scdebug_call(p, code, args); 304 #endif 305 rval[0] = 0; 306 rval[1] = 0; 307 308 error = (*callp->sy_call)(p, params, rval); 309 #ifdef SYSCALL_DEBUG 310 if (trapdebug) 311 scdebug_ret(p, code, error, rval); 312 #endif 313 switch (error) { 314 case 0: 315 frame->fixreg[FIRSTARG] = rval[0]; 316 frame->fixreg[FIRSTARG + 1] = rval[1]; 317 frame->cr &= ~0x10000000; 318 break; 319 case ERESTART: 320 /* 321 * Set user's pc back to redo the system call. 322 */ 323 frame->srr0 -= 4; 324 break; 325 case EJUSTRETURN: 326 /* nothing to do */ 327 break; 328 default: 329 syscall_bad: 330 if (p->p_emul->e_errno) 331 error = p->p_emul->e_errno[error]; 332 frame->fixreg[FIRSTARG] = error; 333 frame->cr |= 0x10000000; 334 break; 335 } 336 337 #ifdef KTRACE 338 if (KTRPOINT(p, KTR_SYSRET)) 339 ktrsysret(p, code, error, rval[0]); 340 #endif 341 } 342 KERNEL_PROC_UNLOCK(p); 343 break; 344 345 case EXC_AST|EXC_USER: 346 astpending = 0; /* we are about to do it */ 347 KERNEL_PROC_LOCK(p); 348 uvmexp.softs++; 349 if (p->p_flag & P_OWEUPC) { 350 p->p_flag &= ~P_OWEUPC; 351 ADDUPROF(p); 352 } 353 /* Check whether we are being preempted. */ 354 if (want_resched) 355 preempt(NULL); 356 KERNEL_PROC_UNLOCK(p); 357 break; 358 359 360 case EXC_ALI|EXC_USER: 361 KERNEL_PROC_LOCK(p); 362 if (fix_unaligned(p, frame) != 0) 363 trapsignal(p, SIGBUS, EXC_ALI); 364 else 365 frame->srr0 += 4; 366 KERNEL_PROC_UNLOCK(p); 367 break; 368 369 case EXC_PGM|EXC_USER: 370 /* 371 * Illegal insn: 372 * 373 * let's try to see if it's FPU and can be emulated. 374 */ 375 uvmexp.traps ++; 376 if (!(p->p_addr->u_pcb.pcb_flags & PCB_FPU)) { 377 memset(&p->p_addr->u_pcb.pcb_fpu, 0, 378 sizeof p->p_addr->u_pcb.pcb_fpu); 379 p->p_addr->u_pcb.pcb_flags |= PCB_FPU; 380 } 381 382 if ((rv = fpu_emulate(frame, 383 (struct fpreg *)&p->p_addr->u_pcb.pcb_fpu))) { 384 KERNEL_PROC_LOCK(p); 385 trapsignal(p, rv, EXC_PGM); 386 KERNEL_PROC_UNLOCK(p); 387 } 388 break; 389 390 case EXC_MCHK: 391 { 392 faultbuf *fb; 393 394 if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) { 395 frame->pid = KERNEL_PID; 396 frame->srr0 = (*fb)[0]; 397 frame->srr1 |= PSL_IR; /* Re-enable IMMU */ 398 frame->fixreg[1] = (*fb)[1]; 399 frame->fixreg[2] = (*fb)[2]; 400 frame->fixreg[3] = 1; /* Return TRUE */ 401 frame->cr = (*fb)[3]; 402 memcpy(&frame->fixreg[13], &(*fb)[4], 403 19 * sizeof(register_t)); 404 goto done; 405 } 406 } 407 goto brain_damage; 408 default: 409 brain_damage: 410 printf("trap type 0x%x at 0x%x\n", type, frame->srr0); 411 #ifdef DDB 412 if (kdb_trap(type, frame)) 413 goto done; 414 #endif 415 #ifdef TRAP_PANICWAIT 416 printf("Press a key to panic.\n"); 417 cngetc(); 418 #endif 419 panic("trap"); 420 } 421 422 /* Take pending signals. */ 423 { 424 int sig; 425 426 while ((sig = CURSIG(p)) != 0) 427 postsig(sig); 428 } 429 430 curcpu()->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri; 431 done: 432 } 433 434 int 435 ctx_setup(int ctx, int srr1) 436 { 437 volatile struct pmap *pm; 438 439 /* Update PID if we're returning to user mode. */ 440 if (srr1 & PSL_PR) { 441 pm = curproc->p_vmspace->vm_map.pmap; 442 if (!pm->pm_ctx) { 443 ctx_alloc((struct pmap *)pm); 444 } 445 ctx = pm->pm_ctx; 446 if (srr1 & PSL_SE) { 447 int dbreg, mask = 0x48000000; 448 /* 449 * Set the Internal Debug and 450 * Instruction Completion bits of 451 * the DBCR0 register. 452 * 453 * XXX this is also used by jtag debuggers... 454 */ 455 __asm __volatile("mfspr %0,0x3f2;" 456 "or %0,%0,%1;" 457 "mtspr 0x3f2,%0;" : 458 "=&r" (dbreg) : "r" (mask)); 459 } 460 } 461 else if (!ctx) { 462 ctx = KERNEL_PID; 463 } 464 return (ctx); 465 } 466 467 void 468 child_return(void *arg) 469 { 470 struct proc *p = arg; 471 struct trapframe *tf = trapframe(p); 472 473 KERNEL_PROC_UNLOCK(p); 474 475 tf->fixreg[FIRSTARG] = 0; 476 tf->fixreg[FIRSTARG + 1] = 1; 477 tf->cr &= ~0x10000000; 478 tf->srr1 &= ~(PSL_FP|PSL_VEC); /* Disable FP & AltiVec, as we can't be them */ 479 #ifdef KTRACE 480 if (KTRPOINT(p, KTR_SYSRET)) { 481 KERNEL_PROC_LOCK(p); 482 ktrsysret(p, SYS_fork, 0, 0); 483 KERNEL_PROC_UNLOCK(p); 484 } 485 #endif 486 /* Profiling? XXX */ 487 curcpu()->ci_schedstate.spc_curpriority = p->p_priority; 488 } 489 490 /* 491 * Used by copyin()/copyout() 492 */ 493 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int)); 494 extern void vunmaprange __P((vaddr_t, vsize_t)); 495 static int bigcopyin __P((const void *, void *, size_t )); 496 static int bigcopyout __P((const void *, void *, size_t )); 497 498 int 499 copyin(const void *udaddr, void *kaddr, size_t len) 500 { 501 struct pmap *pm = curproc->p_vmspace->vm_map.pmap; 502 int msr, pid, tmp, ctx; 503 faultbuf env; 504 505 /* For bigger buffers use the faster copy */ 506 if (len > 256) return (bigcopyin(udaddr, kaddr, len)); 507 508 if (setfault(env)) { 509 curpcb->pcb_onfault = 0; 510 return EFAULT; 511 } 512 513 if (!(ctx = pm->pm_ctx)) { 514 /* No context -- assign it one */ 515 ctx_alloc(pm); 516 ctx = pm->pm_ctx; 517 } 518 519 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */ 520 "mfmsr %0;" /* Save MSR */ 521 "li %1,0x20; " 522 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */ 523 "mfpid %1;" /* Save old PID */ 524 "sync; isync;" 525 526 "1: bdz 2f;" /* while len */ 527 "mtpid %3; sync;" /* Load user ctx */ 528 "lbz %2,0(%4); addi %4,%4,1;" /* Load byte */ 529 "sync; isync;" 530 "mtpid %1;sync;" 531 "stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */ 532 "sync; isync;" 533 "b 1b;" /* repeat */ 534 535 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */ 536 "sync; isync;" 537 : "=&r" (msr), "=&r" (pid), "=&r" (tmp) 538 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len)); 539 540 curpcb->pcb_onfault = 0; 541 return 0; 542 } 543 544 static int 545 bigcopyin(const void *udaddr, void *kaddr, size_t len) 546 { 547 const char *up; 548 char *kp = kaddr; 549 struct proc *p = curproc; 550 int error; 551 552 if (!p) { 553 return EFAULT; 554 } 555 556 /* 557 * Stolen from physio(): 558 */ 559 PHOLD(p); 560 error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ); 561 if (error) { 562 PRELE(p); 563 return EFAULT; 564 } 565 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ); 566 567 memcpy(kp, up, len); 568 vunmaprange((vaddr_t)up, len); 569 uvm_vsunlock(p, (caddr_t)udaddr, len); 570 PRELE(p); 571 572 return 0; 573 } 574 575 int 576 copyout(const void *kaddr, void *udaddr, size_t len) 577 { 578 struct pmap *pm = curproc->p_vmspace->vm_map.pmap; 579 int msr, pid, tmp, ctx; 580 faultbuf env; 581 582 /* For big copies use more efficient routine */ 583 if (len > 256) return (bigcopyout(kaddr, udaddr, len)); 584 585 if (setfault(env)) { 586 curpcb->pcb_onfault = 0; 587 return EFAULT; 588 } 589 590 if (!(ctx = pm->pm_ctx)) { 591 /* No context -- assign it one */ 592 ctx_alloc(pm); 593 ctx = pm->pm_ctx; 594 } 595 596 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */ 597 "mfmsr %0;" /* Save MSR */ 598 "li %1,0x20; " 599 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */ 600 "mfpid %1;" /* Save old PID */ 601 "sync; isync;" 602 603 "1: bdz 2f;" /* while len */ 604 "mtpid %1;sync;" 605 "lbz %2,0(%5); addi %5,%5,1;" /* Load kernel byte */ 606 "sync; isync;" 607 "mtpid %3; sync;" /* Load user ctx */ 608 "stb %2,0(%4); dcbf 0,%4; addi %4,%4,1;" /* Store user byte */ 609 "sync; isync;" 610 "b 1b;" /* repeat */ 611 612 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */ 613 "sync; isync;" 614 : "=&r" (msr), "=&r" (pid), "=&r" (tmp) 615 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len)); 616 617 curpcb->pcb_onfault = 0; 618 return 0; 619 } 620 621 static int 622 bigcopyout(const void *kaddr, void *udaddr, size_t len) 623 { 624 char *up; 625 const char *kp = (char *)kaddr; 626 struct proc *p = curproc; 627 int error; 628 629 if (!p) { 630 return EFAULT; 631 } 632 633 /* 634 * Stolen from physio(): 635 */ 636 PHOLD(p); 637 error = uvm_vslock(p, udaddr, len, VM_PROT_WRITE); 638 if (error) { 639 PRELE(p); 640 return EFAULT; 641 } 642 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, 643 VM_PROT_READ|VM_PROT_WRITE); 644 645 memcpy(up, kp, len); 646 vunmaprange((vaddr_t)up, len); 647 uvm_vsunlock(p, udaddr, len); 648 PRELE(p); 649 650 return 0; 651 } 652 653 /* 654 * kcopy(const void *src, void *dst, size_t len); 655 * 656 * Copy len bytes from src to dst, aborting if we encounter a fatal 657 * page fault. 658 * 659 * kcopy() _must_ save and restore the old fault handler since it is 660 * called by uiomove(), which may be in the path of servicing a non-fatal 661 * page fault. 662 */ 663 int 664 kcopy(const void *src, void *dst, size_t len) 665 { 666 faultbuf env, *oldfault; 667 668 oldfault = curpcb->pcb_onfault; 669 if (setfault(env)) { 670 curpcb->pcb_onfault = oldfault; 671 return EFAULT; 672 } 673 674 memcpy(dst, src, len); 675 676 curpcb->pcb_onfault = oldfault; 677 return 0; 678 } 679 680 int 681 badaddr(void *addr, size_t size) 682 { 683 684 return badaddr_read(addr, size, NULL); 685 } 686 687 int 688 badaddr_read(void *addr, size_t size, int *rptr) 689 { 690 faultbuf env; 691 int x; 692 693 /* Get rid of any stale machine checks that have been waiting. */ 694 __asm __volatile ("sync; isync"); 695 696 if (setfault(env)) { 697 curpcb->pcb_onfault = 0; 698 __asm __volatile ("sync"); 699 return 1; 700 } 701 702 __asm __volatile ("sync"); 703 704 switch (size) { 705 case 1: 706 x = *(volatile int8_t *)addr; 707 break; 708 case 2: 709 x = *(volatile int16_t *)addr; 710 break; 711 case 4: 712 x = *(volatile int32_t *)addr; 713 break; 714 default: 715 panic("badaddr: invalid size (%d)", size); 716 } 717 718 /* Make sure we took the machine check, if we caused one. */ 719 __asm __volatile ("sync; isync"); 720 721 curpcb->pcb_onfault = 0; 722 __asm __volatile ("sync"); /* To be sure. */ 723 724 /* Use the value to avoid reorder. */ 725 if (rptr) 726 *rptr = x; 727 728 return 0; 729 } 730 731 /* 732 * For now, this only deals with the particular unaligned access case 733 * that gcc tends to generate. Eventually it should handle all of the 734 * possibilities that can happen on a 32-bit PowerPC in big-endian mode. 735 */ 736 737 static int 738 fix_unaligned(struct proc *p, struct trapframe *frame) 739 { 740 741 return -1; 742 } 743