| @@ -1,117 +1,120 @@ | | | @@ -1,117 +1,120 @@ |
1 | /* $NetBSD: copy.c,v 1.4 2011/08/25 19:07:45 reinoud Exp $ */ | | 1 | /* $NetBSD: copy.c,v 1.5 2011/08/27 17:57:14 reinoud Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> | | 4 | * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
15 | * | | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 16 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __KERNEL_RCSID(0, "$NetBSD: copy.c,v 1.4 2011/08/25 19:07:45 reinoud Exp $"); | | 30 | __KERNEL_RCSID(0, "$NetBSD: copy.c,v 1.5 2011/08/27 17:57:14 reinoud Exp $"); |
31 | | | 31 | |
32 | #include <sys/types.h> | | 32 | #include <sys/types.h> |
33 | #include <sys/systm.h> | | 33 | #include <sys/systm.h> |
34 | #include <sys/param.h> // tmp | | 34 | |
35 | #include <uvm/uvm.h> // tmp | | 35 | /* XXX until strnlen(3) has been added to the kernel, we *could* panic on it */ |
36 | #include <uvm/uvm_pmap.h> // tmp | | 36 | #define strnlen(str, maxlen) min(strlen((str)), maxlen) |
37 | | | 37 | |
38 | int | | 38 | int |
39 | copyin(const void *uaddr, void *kaddr, size_t len) | | 39 | copyin(const void *uaddr, void *kaddr, size_t len) |
40 | { | | 40 | { |
41 | aprint_debug("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len); | | 41 | aprint_debug("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len); |
42 | memcpy(kaddr, uaddr, len); | | 42 | memcpy(kaddr, uaddr, len); |
43 | return 0; | | 43 | return 0; |
44 | } | | 44 | } |
45 | | | 45 | |
46 | int | | 46 | int |
47 | copyout(const void *kaddr, void *uaddr, size_t len) | | 47 | copyout(const void *kaddr, void *uaddr, size_t len) |
48 | { | | 48 | { |
49 | aprint_debug("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len); | | 49 | aprint_debug("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len); |
50 | memcpy(uaddr, kaddr, len); | | 50 | memcpy(uaddr, kaddr, len); |
51 | return 0; | | 51 | return 0; |
52 | } | | 52 | } |
53 | | | 53 | |
54 | int | | 54 | int |
55 | copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) | | 55 | copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) |
56 | { | | 56 | { |
| | | 57 | len = min(strnlen(uaddr, len), len) + 1; |
57 | strncpy(kaddr, uaddr, len); | | 58 | strncpy(kaddr, uaddr, len); |
58 | if (done) | | 59 | if (done) |
59 | *done = min(strlen(uaddr), len); | | 60 | *done = len; |
60 | return 0; | | 61 | return 0; |
61 | } | | 62 | } |
62 | | | 63 | |
63 | int | | 64 | int |
64 | copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) | | 65 | copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) |
65 | { | | 66 | { |
| | | 67 | len = min(strnlen(kaddr, len), len) + 1; |
66 | strncpy(uaddr, kaddr, len); | | 68 | strncpy(uaddr, kaddr, len); |
67 | if (done) | | 69 | if (done) |
68 | *done = min(strlen(kaddr), len); | | 70 | *done = len; |
69 | return 0; | | 71 | return 0; |
70 | } | | 72 | } |
71 | | | 73 | |
72 | int | | 74 | int |
73 | copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) | | 75 | copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) |
74 | { | | 76 | { |
| | | 77 | len = min(strnlen(kfaddr, len), len) + 1; |
75 | strncpy(kdaddr, kfaddr, len); | | 78 | strncpy(kdaddr, kfaddr, len); |
76 | if (done) | | 79 | if (done) |
77 | *done = min(strlen(kfaddr), len); | | 80 | *done = len; |
78 | return 0; | | 81 | return 0; |
79 | } | | 82 | } |
80 | | | 83 | |
81 | int | | 84 | int |
82 | kcopy(const void *src, void *dst, size_t len) | | 85 | kcopy(const void *src, void *dst, size_t len) |
83 | { | | 86 | { |
84 | memcpy(dst, src, len); | | 87 | memcpy(dst, src, len); |
85 | #ifdef DEBUG | | 88 | #ifdef DEBUG |
86 | if (memcmp(dst, src, len) != 0) | | 89 | if (memcmp(dst, src, len) != 0) |
87 | panic("kcopy not finished correctly\n"); | | 90 | panic("kcopy not finished correctly\n"); |
88 | #endif | | 91 | #endif |
89 | return 0; | | 92 | return 0; |
90 | } | | 93 | } |
91 | | | 94 | |
92 | int | | 95 | int |
93 | fuswintr(const void *base) | | 96 | fuswintr(const void *base) |
94 | { | | 97 | { |
95 | return *(const short *)base; | | 98 | return *(const short *)base; |
96 | } | | 99 | } |
97 | | | 100 | |
98 | int | | 101 | int |
99 | suswintr(void *base, short c) | | 102 | suswintr(void *base, short c) |
100 | { | | 103 | { |
101 | *(short *)base = c; | | 104 | *(short *)base = c; |
102 | return 0; | | 105 | return 0; |
103 | } | | 106 | } |
104 | | | 107 | |
105 | int | | 108 | int |
106 | subyte(void *base, int c) | | 109 | subyte(void *base, int c) |
107 | { | | 110 | { |
108 | *(char *)base = c; | | 111 | *(char *)base = c; |
109 | return 0; | | 112 | return 0; |
110 | } | | 113 | } |
111 | | | 114 | |
112 | int | | 115 | int |
113 | suword(void *base, long c) | | 116 | suword(void *base, long c) |
114 | { | | 117 | { |
115 | *(long *)base = c; | | 118 | *(long *)base = c; |
116 | return 0; | | 119 | return 0; |
117 | } | | 120 | } |