xref: /netbsd/sys/arch/usermode/usermode/copy.c (revision d71e829b)
1 /* $NetBSD: copy.c,v 1.12 2020/06/30 16:20:02 maxv Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
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
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: copy.c,v 1.12 2020/06/30 16:20:02 maxv Exp $");
31 
32 #define	__UFETCHSTORE_PRIVATE
33 #define	__UCAS_PRIVATE
34 
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <machine/thunk.h>
38 
39 /* XXX until strnlen(3) has been added to the kernel, we *could* panic on it */
40 #define strnlen(str, maxlen) uimin(strlen((str)), maxlen)
41 
42 int
copyin(const void * uaddr,void * kaddr,size_t len)43 copyin(const void *uaddr, void *kaddr, size_t len)
44 {
45 //	thunk_printf("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len);
46 	memcpy(kaddr, uaddr, len);
47 	return 0;
48 }
49 
50 int
copyout(const void * kaddr,void * uaddr,size_t len)51 copyout(const void *kaddr, void *uaddr, size_t len)
52 {
53 //	thunk_printf("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len);
54 	memcpy(uaddr, kaddr, len);
55 	return 0;
56 }
57 
58 int
copyinstr(const void * uaddr,void * kaddr,size_t len,size_t * done)59 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
60 {
61 	len = uimin(strnlen(uaddr, len), len) + 1;
62 	strncpy(kaddr, uaddr, len);
63 	if (done)
64 		*done = len;
65 	return 0;
66 }
67 
68 int
copyoutstr(const void * kaddr,void * uaddr,size_t len,size_t * done)69 copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
70 {
71 	len = uimin(strnlen(kaddr, len), len) + 1;
72 	strncpy(uaddr, kaddr, len);
73 	if (done)
74 		*done = len;
75 	return 0;
76 }
77 
78 int
kcopy(const void * src,void * dst,size_t len)79 kcopy(const void *src, void *dst, size_t len)
80 {
81 	memcpy(dst, src, len);
82 #ifdef DEBUG
83 	if (memcmp(dst, src, len) != 0)
84 		panic("kcopy not finished correctly\n");
85 #endif
86 	return 0;
87 }
88 
89 int
_ufetch_8(const uint8_t * uaddr,uint8_t * valp)90 _ufetch_8(const uint8_t *uaddr, uint8_t *valp)
91 {
92 	*valp = *uaddr;
93 	return 0;
94 }
95 
96 int
_ufetch_16(const uint16_t * uaddr,uint16_t * valp)97 _ufetch_16(const uint16_t *uaddr, uint16_t *valp)
98 {
99 	*valp = *uaddr;
100 	return 0;
101 }
102 
103 int
_ufetch_32(const uint32_t * uaddr,uint32_t * valp)104 _ufetch_32(const uint32_t *uaddr, uint32_t *valp)
105 {
106 	*valp = *uaddr;
107 	return 0;
108 }
109 
110 #ifdef _LP64
111 int
_ufetch_64(const uint64_t * uaddr,uint64_t * valp)112 _ufetch_64(const uint64_t *uaddr, uint64_t *valp)
113 {
114 	*valp = *uaddr;
115 	return 0;
116 }
117 #endif /* _LP64 */
118 
119 int
_ustore_8(uint8_t * uaddr,uint8_t val)120 _ustore_8(uint8_t *uaddr, uint8_t val)
121 {
122 	*uaddr = val;
123 	return 0;
124 }
125 
126 int
_ustore_16(uint16_t * uaddr,uint16_t val)127 _ustore_16(uint16_t *uaddr, uint16_t val)
128 {
129 	*uaddr = val;
130 	return 0;
131 }
132 
133 int
_ustore_32(uint32_t * uaddr,uint32_t val)134 _ustore_32(uint32_t *uaddr, uint32_t val)
135 {
136 	*uaddr = val;
137 	return 0;
138 }
139 
140 #ifdef _LP64
141 int
_ustore_64(uint64_t * uaddr,uint64_t val)142 _ustore_64(uint64_t *uaddr, uint64_t val)
143 {
144 	*uaddr = val;
145 	return 0;
146 }
147 #endif /* _LP64 */
148