1 /* $NetBSD: trap.c,v 1.78 2020/02/21 15:15:48 rin 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 <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.78 2020/02/21 15:15:48 rin Exp $"); 71 72 #include "opt_altivec.h" 73 #include "opt_ddb.h" 74 #include "opt_kgdb.h" 75 76 #define __UFETCHSTORE_PRIVATE 77 78 #include <sys/param.h> 79 #include <sys/cpu.h> 80 #include <sys/kauth.h> 81 #include <sys/proc.h> 82 #include <sys/reboot.h> 83 #include <sys/syscall.h> 84 #include <sys/systm.h> 85 86 #if defined(KGDB) 87 #include <sys/kgdb.h> 88 #endif 89 90 #include <uvm/uvm_extern.h> 91 92 #include <dev/cons.h> 93 94 #include <machine/fpu.h> 95 #include <machine/frame.h> 96 #include <machine/pcb.h> 97 #include <machine/psl.h> 98 #include <machine/trap.h> 99 100 #include <powerpc/db_machdep.h> 101 #include <powerpc/spr.h> 102 #include <powerpc/userret.h> 103 104 #include <powerpc/ibm4xx/cpu.h> 105 #include <powerpc/ibm4xx/pmap.h> 106 #include <powerpc/ibm4xx/spr.h> 107 #include <powerpc/ibm4xx/tlb.h> 108 109 #include <powerpc/fpu/fpu_extern.h> 110 111 /* These definitions should probably be somewhere else XXX */ 112 #define FIRSTARG 3 /* first argument is in reg 3 */ 113 #define NARGREG 8 /* 8 args are in registers */ 114 #define MOREARGS(sp) ((void *)((int)(sp) + 8)) /* more args go here */ 115 116 static int fix_unaligned(struct lwp *l, struct trapframe *tf); 117 118 void trap(struct trapframe *); /* Called from locore / trap_subr */ 119 #if 0 120 /* Not currently used nor exposed externally in any header file */ 121 int badaddr(void *, size_t); 122 int badaddr_read(void *, size_t, int *); 123 #endif 124 int ctx_setup(int, int); 125 126 #ifdef DEBUG 127 #define TDB_ALL 0x1 128 int trapdebug = /* TDB_ALL */ 0; 129 #define DBPRINTF(x, y) if (trapdebug & (x)) printf y 130 #else 131 #define DBPRINTF(x, y) 132 #endif 133 134 void 135 trap(struct trapframe *tf) 136 { 137 struct lwp *l = curlwp; 138 struct proc *p = l->l_proc; 139 struct pcb *pcb; 140 int type = tf->tf_exc; 141 int ftype, rv; 142 ksiginfo_t ksi; 143 144 KASSERT(l->l_stat == LSONPROC); 145 146 if (tf->tf_srr1 & PSL_PR) { 147 LWP_CACHE_CREDS(l, p); 148 type |= EXC_USER; 149 } 150 151 ftype = VM_PROT_READ; 152 153 DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n", 154 type, tf->tf_srr0, tf, &tf)); 155 156 switch (type) { 157 case EXC_DEBUG|EXC_USER: 158 { 159 int srr2, srr3; 160 161 __asm volatile("mfspr %0,0x3f0" : 162 "=r" (rv), "=r" (srr2), "=r" (srr3) :); 163 printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, 164 srr3); 165 /* XXX fall through or break here?! */ 166 } 167 /* 168 * DEBUG intr -- probably single-step. 169 */ 170 case EXC_TRC|EXC_USER: 171 tf->tf_srr1 &= ~PSL_SE; 172 KSI_INIT_TRAP(&ksi); 173 ksi.ksi_signo = SIGTRAP; 174 ksi.ksi_trap = EXC_TRC; 175 ksi.ksi_addr = (void *)tf->tf_srr0; 176 trapsignal(l, &ksi); 177 break; 178 179 case EXC_DSI: 180 /* FALLTHROUGH */ 181 case EXC_DTMISS: 182 { 183 struct vm_map *map; 184 vaddr_t va; 185 struct faultbuf *fb; 186 187 pcb = lwp_getpcb(l); 188 fb = pcb->pcb_onfault; 189 190 if (curcpu()->ci_idepth >= 0) { 191 rv = EFAULT; 192 goto out; 193 } 194 195 va = tf->tf_dear; 196 if (tf->tf_pid == KERNEL_PID) { 197 map = kernel_map; 198 } else { 199 map = &p->p_vmspace->vm_map; 200 } 201 202 if (tf->tf_esr & (ESR_DST|ESR_DIZ)) 203 ftype = VM_PROT_WRITE; 204 205 DBPRINTF(TDB_ALL, 206 ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n", 207 tf->tf_srr0, 208 (ftype & VM_PROT_WRITE) ? "write" : "read", 209 (void *)va, tf->tf_esr)); 210 211 pcb->pcb_onfault = NULL; 212 rv = uvm_fault(map, trunc_page(va), ftype); 213 pcb->pcb_onfault = fb; 214 if (rv == 0) 215 return; 216 out: 217 if (fb != NULL) { 218 tf->tf_pid = KERNEL_PID; 219 tf->tf_srr0 = fb->fb_pc; 220 tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */ 221 tf->tf_cr = fb->fb_cr; 222 tf->tf_fixreg[1] = fb->fb_sp; 223 tf->tf_fixreg[2] = fb->fb_r2; 224 tf->tf_fixreg[3] = 1; /* Return TRUE */ 225 memcpy(&tf->tf_fixreg[13], fb->fb_fixreg, 226 sizeof(fb->fb_fixreg)); 227 return; 228 } 229 } 230 goto brain_damage; 231 232 case EXC_DSI|EXC_USER: 233 /* FALLTHROUGH */ 234 case EXC_DTMISS|EXC_USER: 235 if (tf->tf_esr & (ESR_DST|ESR_DIZ)) 236 ftype = VM_PROT_WRITE; 237 238 DBPRINTF(TDB_ALL, 239 ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n", 240 tf->tf_srr0, (ftype & VM_PROT_WRITE) ? "write" : "read", 241 tf->tf_dear, tf->tf_esr)); 242 KASSERT(l == curlwp && (l->l_stat == LSONPROC)); 243 // KASSERT(curpcb->pcb_onfault == NULL); 244 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear), 245 ftype); 246 if (rv == 0) { 247 break; 248 } 249 KSI_INIT_TRAP(&ksi); 250 ksi.ksi_signo = SIGSEGV; 251 ksi.ksi_trap = EXC_DSI; 252 ksi.ksi_addr = (void *)tf->tf_dear; 253 if (rv == ENOMEM) { 254 printf("UVM: pid %d (%s) lid %d, uid %d killed: " 255 "out of swap\n", 256 p->p_pid, p->p_comm, l->l_lid, 257 l->l_cred ? 258 kauth_cred_geteuid(l->l_cred) : -1); 259 ksi.ksi_signo = SIGKILL; 260 } 261 trapsignal(l, &ksi); 262 break; 263 264 case EXC_ITMISS|EXC_USER: 265 case EXC_ISI|EXC_USER: 266 ftype = VM_PROT_EXECUTE; 267 DBPRINTF(TDB_ALL, 268 ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n", 269 tf->tf_srr0, tf)); 270 // KASSERT(curpcb->pcb_onfault == NULL); 271 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0), 272 ftype); 273 if (rv == 0) { 274 break; 275 } 276 KSI_INIT_TRAP(&ksi); 277 ksi.ksi_signo = SIGSEGV; 278 ksi.ksi_trap = EXC_ISI; 279 ksi.ksi_addr = (void *)tf->tf_srr0; 280 ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR); 281 trapsignal(l, &ksi); 282 break; 283 284 case EXC_AST|EXC_USER: 285 cpu_ast(l, curcpu()); 286 break; 287 288 case EXC_ALI|EXC_USER: 289 if (fix_unaligned(l, tf) != 0) { 290 KSI_INIT_TRAP(&ksi); 291 ksi.ksi_signo = SIGBUS; 292 ksi.ksi_trap = EXC_ALI; 293 ksi.ksi_addr = (void *)tf->tf_dear; 294 trapsignal(l, &ksi); 295 } else 296 tf->tf_srr0 += 4; 297 break; 298 299 case EXC_PGM|EXC_USER: 300 /* 301 * Illegal insn: 302 * 303 * let's try to see if its FPU and can be emulated. 304 */ 305 curcpu()->ci_data.cpu_ntrap++; 306 pcb = lwp_getpcb(l); 307 308 if (__predict_false(!fpu_used_p(l))) { 309 memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu)); 310 fpu_mark_used(l); 311 } 312 313 if (fpu_emulate(tf, &pcb->pcb_fpu, &ksi)) { 314 if (ksi.ksi_signo == 0) /* was emulated */ 315 break; 316 } else { 317 ksi.ksi_signo = SIGILL; 318 ksi.ksi_code = ILL_ILLOPC; 319 ksi.ksi_trap = EXC_PGM; 320 ksi.ksi_addr = (void *)tf->tf_srr0; 321 } 322 323 trapsignal(l, &ksi); 324 break; 325 326 case EXC_MCHK: 327 { 328 struct faultbuf *fb; 329 330 pcb = lwp_getpcb(l); 331 if ((fb = pcb->pcb_onfault) != NULL) { 332 tf->tf_pid = KERNEL_PID; 333 tf->tf_srr0 = fb->fb_pc; 334 tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */ 335 tf->tf_fixreg[1] = fb->fb_sp; 336 tf->tf_fixreg[2] = fb->fb_r2; 337 tf->tf_fixreg[3] = 1; /* Return TRUE */ 338 tf->tf_cr = fb->fb_cr; 339 memcpy(&tf->tf_fixreg[13], fb->fb_fixreg, 340 sizeof(fb->fb_fixreg)); 341 return; 342 } 343 } 344 goto brain_damage; 345 346 default: 347 brain_damage: 348 printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0); 349 #if defined(DDB) || defined(KGDB) 350 if (kdb_trap(type, tf)) 351 return; 352 #endif 353 #ifdef TRAP_PANICWAIT 354 printf("Press a key to panic.\n"); 355 cngetc(); 356 #endif 357 panic("trap"); 358 } 359 360 /* Invoke powerpc userret code */ 361 userret(l, tf); 362 } 363 364 int 365 ctx_setup(int ctx, int srr1) 366 { 367 volatile struct pmap *pm; 368 369 /* Update PID if we're returning to user mode. */ 370 if (srr1 & PSL_PR) { 371 pm = curproc->p_vmspace->vm_map.pmap; 372 if (!pm->pm_ctx) { 373 ctx_alloc(__UNVOLATILE(pm)); 374 } 375 ctx = pm->pm_ctx; 376 if (srr1 & PSL_SE) { 377 int dbreg, mask = 0x48000000; 378 /* 379 * Set the Internal Debug and 380 * Instruction Completion bits of 381 * the DBCR0 register. 382 * 383 * XXX this is also used by jtag debuggers... 384 */ 385 __asm volatile("mfspr %0,0x3f2;" 386 "or %0,%0,%1;" 387 "mtspr 0x3f2,%0;" : 388 "=&r" (dbreg) : "r" (mask)); 389 } 390 } 391 else if (!ctx) { 392 ctx = KERNEL_PID; 393 } 394 return (ctx); 395 } 396 397 /* 398 * Used by copyin()/copyout() 399 */ 400 extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int); 401 extern void vunmaprange(vaddr_t, vsize_t); 402 static int bigcopyin(const void *, void *, size_t ); 403 static int bigcopyout(const void *, void *, size_t ); 404 405 int 406 copyin(const void *udaddr, void *kaddr, size_t len) 407 { 408 struct pmap *pm = curproc->p_vmspace->vm_map.pmap; 409 int rv, msr, pid, tmp, ctx, count = 0; 410 struct faultbuf env; 411 412 /* For bigger buffers use the faster copy */ 413 if (len > 1024) 414 return (bigcopyin(udaddr, kaddr, len)); 415 416 if ((rv = setfault(&env))) { 417 curpcb->pcb_onfault = NULL; 418 return rv; 419 } 420 421 if (!(ctx = pm->pm_ctx)) { 422 /* No context -- assign it one */ 423 ctx_alloc(pm); 424 ctx = pm->pm_ctx; 425 } 426 427 __asm volatile( 428 " mfmsr %[msr];" /* Save MSR */ 429 " li %[pid],0x20;" 430 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */ 431 " mfpid %[pid];" /* Save old PID */ 432 " sync; isync;" 433 434 " srwi. %[count],%[len],0x2;" /* How many words? */ 435 " beq- 2f;" /* No words. Go do bytes */ 436 " mtctr %[count];" 437 "1: mtpid %[ctx]; sync;" 438 #ifdef PPC_IBM403 439 " lswi %[tmp],%[udaddr],4;" /* Load user word */ 440 #else 441 " lwz %[tmp],0(%[udaddr]);" 442 #endif 443 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */ 444 " sync; isync;" 445 " mtpid %[pid]; sync;" 446 #ifdef PPC_IBM403 447 " stswi %[tmp],%[kaddr],4;" /* Store kernel word */ 448 #else 449 " stw %[tmp],0(%[kaddr]);" 450 #endif 451 " dcbst 0,%[kaddr];" /* flush cache */ 452 " addi %[kaddr],%[kaddr],0x4;" /* next udaddr word */ 453 " sync; isync;" 454 " bdnz 1b;" /* repeat */ 455 456 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */ 457 " addi %[count],%[count],0x1;" 458 " mtctr %[count];" 459 "3: bdz 10f;" /* while count */ 460 " mtpid %[ctx]; sync;" 461 " lbz %[tmp],0(%[udaddr]);" /* Load user byte */ 462 " addi %[udaddr],%[udaddr],0x1;" /* next udaddr byte */ 463 " sync; isync;" 464 " mtpid %[pid]; sync;" 465 " stb %[tmp],0(%[kaddr]);" /* Store kernel byte */ 466 " dcbst 0,%[kaddr];" /* flush cache */ 467 " addi %[kaddr],%[kaddr],0x1;" 468 " sync; isync;" 469 " b 3b;" 470 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" 471 /* Restore PID and MSR */ 472 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp) 473 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), 474 [len] "b" (len), [count] "b" (count)); 475 476 curpcb->pcb_onfault = NULL; 477 return 0; 478 } 479 480 static int 481 bigcopyin(const void *udaddr, void *kaddr, size_t len) 482 { 483 const char *up; 484 char *kp = kaddr; 485 struct lwp *l = curlwp; 486 struct proc *p; 487 struct faultbuf env; 488 int error; 489 490 p = l->l_proc; 491 492 /* 493 * Stolen from physio(): 494 */ 495 error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ); 496 if (error) { 497 return error; 498 } 499 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ); 500 501 if ((error = setfault(&env)) == 0) { 502 memcpy(kp, up, len); 503 } 504 505 curpcb->pcb_onfault = NULL; 506 vunmaprange((vaddr_t)up, len); 507 uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len); 508 509 return error; 510 } 511 512 int 513 copyout(const void *kaddr, void *udaddr, size_t len) 514 { 515 struct pmap *pm = curproc->p_vmspace->vm_map.pmap; 516 int rv, msr, pid, tmp, ctx, count = 0; 517 struct faultbuf env; 518 519 /* For big copies use more efficient routine */ 520 if (len > 1024) 521 return (bigcopyout(kaddr, udaddr, len)); 522 523 if ((rv = setfault(&env))) { 524 curpcb->pcb_onfault = NULL; 525 return rv; 526 } 527 528 if (!(ctx = pm->pm_ctx)) { 529 /* No context -- assign it one */ 530 ctx_alloc(pm); 531 ctx = pm->pm_ctx; 532 } 533 534 __asm volatile( 535 " mfmsr %[msr];" /* Save MSR */ 536 " li %[pid],0x20;" 537 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */ 538 " mfpid %[pid];" /* Save old PID */ 539 " sync; isync;" 540 541 " srwi. %[count],%[len],0x2;" /* How many words? */ 542 " beq- 2f;" /* No words. Go do bytes */ 543 " mtctr %[count];" 544 "1: mtpid %[pid]; sync;" 545 #ifdef PPC_IBM403 546 " lswi %[tmp],%[kaddr],4;" /* Load kernel word */ 547 #else 548 " lwz %[tmp],0(%[kaddr]);" 549 #endif 550 " addi %[kaddr],%[kaddr],0x4;" /* next kaddr word */ 551 " sync; isync;" 552 " mtpid %[ctx]; sync;" 553 #ifdef PPC_IBM403 554 " stswi %[tmp],%[udaddr],4;" /* Store user word */ 555 #else 556 " stw %[tmp],0(%[udaddr]);" 557 #endif 558 " dcbst 0,%[udaddr];" /* flush cache */ 559 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */ 560 " sync; isync;" 561 " bdnz 1b;" /* repeat */ 562 563 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */ 564 " addi %[count],%[count],0x1;" 565 " mtctr %[count];" 566 "3: bdz 10f;" /* while count */ 567 " mtpid %[pid]; sync;" 568 " lbz %[tmp],0(%[kaddr]);" /* Load kernel byte */ 569 " addi %[kaddr],%[kaddr],0x1;" /* next kaddr byte */ 570 " sync; isync;" 571 " mtpid %[ctx]; sync;" 572 " stb %[tmp],0(%[udaddr]);" /* Store user byte */ 573 " dcbst 0,%[udaddr];" /* flush cache */ 574 " addi %[udaddr],%[udaddr],0x1;" 575 " sync; isync;" 576 " b 3b;" 577 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" 578 /* Restore PID and MSR */ 579 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp) 580 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), 581 [len] "b" (len), [count] "b" (count)); 582 583 curpcb->pcb_onfault = NULL; 584 return 0; 585 } 586 587 static int 588 bigcopyout(const void *kaddr, void *udaddr, size_t len) 589 { 590 char *up; 591 const char *kp = (const char *)kaddr; 592 struct lwp *l = curlwp; 593 struct proc *p; 594 struct faultbuf env; 595 int error; 596 597 p = l->l_proc; 598 599 /* 600 * Stolen from physio(): 601 */ 602 error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE); 603 if (error) { 604 return error; 605 } 606 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, 607 VM_PROT_READ | VM_PROT_WRITE); 608 609 if ((error = setfault(&env)) == 0) { 610 memcpy(up, kp, len); 611 } 612 613 curpcb->pcb_onfault = NULL; 614 vunmaprange((vaddr_t)up, len); 615 uvm_vsunlock(p->p_vmspace, udaddr, len); 616 617 return error; 618 } 619 620 /* 621 * kcopy(const void *src, void *dst, size_t len); 622 * 623 * Copy len bytes from src to dst, aborting if we encounter a fatal 624 * page fault. 625 * 626 * kcopy() _must_ save and restore the old fault handler since it is 627 * called by uiomove(), which may be in the path of servicing a non-fatal 628 * page fault. 629 */ 630 int 631 kcopy(const void *src, void *dst, size_t len) 632 { 633 struct faultbuf env, *oldfault; 634 int rv; 635 636 oldfault = curpcb->pcb_onfault; 637 if ((rv = setfault(&env))) { 638 curpcb->pcb_onfault = oldfault; 639 return rv; 640 } 641 642 memcpy(dst, src, len); 643 644 curpcb->pcb_onfault = oldfault; 645 return 0; 646 } 647 648 #if 0 649 int 650 badaddr(void *addr, size_t size) 651 { 652 653 return badaddr_read(addr, size, NULL); 654 } 655 656 int 657 badaddr_read(void *addr, size_t size, int *rptr) 658 { 659 struct faultbuf env; 660 int x; 661 662 /* Get rid of any stale machine checks that have been waiting. */ 663 __asm volatile ("sync; isync"); 664 665 if (setfault(&env)) { 666 curpcb->pcb_onfault = NULL; 667 __asm volatile ("sync"); 668 return 1; 669 } 670 671 __asm volatile ("sync"); 672 673 switch (size) { 674 case 1: 675 x = *(volatile int8_t *)addr; 676 break; 677 case 2: 678 x = *(volatile int16_t *)addr; 679 break; 680 case 4: 681 x = *(volatile int32_t *)addr; 682 break; 683 default: 684 panic("badaddr: invalid size (%d)", size); 685 } 686 687 /* Make sure we took the machine check, if we caused one. */ 688 __asm volatile ("sync; isync"); 689 690 curpcb->pcb_onfault = NULL; 691 __asm volatile ("sync"); /* To be sure. */ 692 693 /* Use the value to avoid reorder. */ 694 if (rptr) 695 *rptr = x; 696 697 return 0; 698 } 699 #endif 700 701 /* 702 * For now, this only deals with the particular unaligned access case 703 * that gcc tends to generate. Eventually it should handle all of the 704 * possibilities that can happen on a 32-bit PowerPC in big-endian mode. 705 */ 706 707 static int 708 fix_unaligned(struct lwp *l, struct trapframe *tf) 709 { 710 711 return -1; 712 } 713 714 /* 715 * XXX Extremely lame implementations of _ufetch_* / _ustore_*. IBM 4xx 716 * experts should make versions that are good. 717 */ 718 719 #define UFETCH(sz) \ 720 int \ 721 _ufetch_ ## sz(const uint ## sz ## _t *uaddr, uint ## sz ## _t *valp) \ 722 { \ 723 return copyin(uaddr, valp, sizeof(*valp)); \ 724 } 725 726 UFETCH(8) 727 UFETCH(16) 728 UFETCH(32) 729 730 #define USTORE(sz) \ 731 int \ 732 _ustore_ ## sz(uint ## sz ## _t *uaddr, uint ## sz ## _t val) \ 733 { \ 734 return copyout(&val, uaddr, sizeof(val)); \ 735 } 736 737 USTORE(8) 738 USTORE(16) 739 USTORE(32) 740