copyinstr.c revision 1.1.8.2 1 1.1.8.2 briggs /* $NetBSD: copyinstr.c,v 1.1.8.2 2001/11/05 19:46:15 briggs Exp $ */
2 1.1.8.2 briggs
3 1.1.8.2 briggs /*
4 1.1.8.2 briggs * Copyright 2001 Wasabi Systems, Inc.
5 1.1.8.2 briggs * All rights reserved.
6 1.1.8.2 briggs *
7 1.1.8.2 briggs * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 1.1.8.2 briggs *
9 1.1.8.2 briggs * Redistribution and use in source and binary forms, with or without
10 1.1.8.2 briggs * modification, are permitted provided that the following conditions
11 1.1.8.2 briggs * are met:
12 1.1.8.2 briggs * 1. Redistributions of source code must retain the above copyright
13 1.1.8.2 briggs * notice, this list of conditions and the following disclaimer.
14 1.1.8.2 briggs * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.8.2 briggs * notice, this list of conditions and the following disclaimer in the
16 1.1.8.2 briggs * documentation and/or other materials provided with the distribution.
17 1.1.8.2 briggs * 3. All advertising materials mentioning features or use of this software
18 1.1.8.2 briggs * must display the following acknowledgement:
19 1.1.8.2 briggs * This product includes software developed for the NetBSD Project by
20 1.1.8.2 briggs * Wasabi Systems, Inc.
21 1.1.8.2 briggs * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1.8.2 briggs * or promote products derived from this software without specific prior
23 1.1.8.2 briggs * written permission.
24 1.1.8.2 briggs *
25 1.1.8.2 briggs * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1.8.2 briggs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1.8.2 briggs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1.8.2 briggs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1.8.2 briggs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1.8.2 briggs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1.8.2 briggs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1.8.2 briggs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1.8.2 briggs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1.8.2 briggs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1.8.2 briggs * POSSIBILITY OF SUCH DAMAGE.
36 1.1.8.2 briggs */
37 1.1.8.2 briggs
38 1.1.8.2 briggs #include <sys/param.h>
39 1.1.8.2 briggs #include <sys/lwp.h>
40 1.1.8.2 briggs #include <uvm/uvm_extern.h>
41 1.1.8.2 briggs #include <machine/pcb.h>
42 1.1.8.2 briggs
43 1.1.8.2 briggs int setfault(faultbuf); /* defined in locore.S */
44 1.1.8.2 briggs
45 1.1.8.2 briggs int
46 1.1.8.2 briggs copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
47 1.1.8.2 briggs {
48 1.1.8.2 briggs struct pmap *pm = curproc->l_proc->p_vmspace->vm_map.pmap;
49 1.1.8.2 briggs int msr, pid, tmp, ctx;
50 1.1.8.2 briggs faultbuf env;
51 1.1.8.2 briggs
52 1.1.8.2 briggs if (setfault(env)) {
53 1.1.8.2 briggs curpcb->pcb_onfault = 0;
54 1.1.8.2 briggs /* XXXX -- len may be lost on a fault */
55 1.1.8.2 briggs if (done)
56 1.1.8.2 briggs *done = len;
57 1.1.8.2 briggs return EFAULT;
58 1.1.8.2 briggs }
59 1.1.8.2 briggs
60 1.1.8.2 briggs if (!(ctx = pm->pm_ctx)) {
61 1.1.8.2 briggs /* No context -- assign it one */
62 1.1.8.2 briggs ctx_alloc(pm);
63 1.1.8.2 briggs ctx = pm->pm_ctx;
64 1.1.8.2 briggs }
65 1.1.8.2 briggs
66 1.1.8.2 briggs if (len) {
67 1.1.8.2 briggs asm volatile("mtctr %3;" /* Set up counter */
68 1.1.8.2 briggs "mfmsr %0;" /* Save MSR */
69 1.1.8.2 briggs "li %1,0x20; "
70 1.1.8.2 briggs "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */
71 1.1.8.2 briggs "mfpid %1;" /* Save old PID */
72 1.1.8.2 briggs "sync; isync;"
73 1.1.8.2 briggs
74 1.1.8.2 briggs "li %3,0;" /* Clear len */
75 1.1.8.2 briggs
76 1.1.8.2 briggs "1: "
77 1.1.8.2 briggs "mtpid %4; sync;" /* Load user ctx */
78 1.1.8.2 briggs "lbz %2,0(%5); addi %5,%5,1;" /* Load byte */
79 1.1.8.2 briggs "sync; isync;"
80 1.1.8.2 briggs "mtpid %1;sync;"
81 1.1.8.2 briggs "stb %2,0(%6); dcbf 0,%6; addi %6,%6,1;" /* Store kernel byte */
82 1.1.8.2 briggs "sync; isync;"
83 1.1.8.2 briggs "addi %3,%3,1;" /* Inc len */
84 1.1.8.2 briggs "or. %2,%2,%2;"
85 1.1.8.2 briggs "bdnzf 2,1b;" /*
86 1.1.8.2 briggs * while(ctr-- && !zero)
87 1.1.8.2 briggs */
88 1.1.8.2 briggs
89 1.1.8.2 briggs "2: mtpid %1; mtmsr %0;" /* Restore PID, MSR */
90 1.1.8.2 briggs "sync; isync;"
91 1.1.8.2 briggs : "=&r" (msr), "=&r" (pid), "=&r" (tmp), "+r" (len)
92 1.1.8.2 briggs : "r" (ctx), "r" (udaddr), "r" (kaddr));
93 1.1.8.2 briggs }
94 1.1.8.2 briggs curpcb->pcb_onfault = 0;
95 1.1.8.2 briggs if (done)
96 1.1.8.2 briggs *done = len;
97 1.1.8.2 briggs return 0;
98 1.1.8.2 briggs }
99