xref: /netbsd-src/sys/arch/usermode/usermode/copy.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* $NetBSD: copy.c,v 1.7 2012/01/14 17:42:52 reinoud 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.7 2012/01/14 17:42:52 reinoud Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <machine/thunk.h>
35 
36 /* XXX until strnlen(3) has been added to the kernel, we *could* panic on it */
37 #define strnlen(str, maxlen) min(strlen((str)), maxlen)
38 
39 int
40 copyin(const void *uaddr, void *kaddr, size_t len)
41 {
42 //	thunk_printf("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len);
43 	memcpy(kaddr, uaddr, len);
44 	return 0;
45 }
46 
47 int
48 copyout(const void *kaddr, void *uaddr, size_t len)
49 {
50 //	thunk_printf("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len);
51 	memcpy(uaddr, kaddr, len);
52 	return 0;
53 }
54 
55 int
56 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
57 {
58 	len = min(strnlen(uaddr, len), len) + 1;
59 	strncpy(kaddr, uaddr, len);
60 	if (done)
61 		*done = len;
62 	return 0;
63 }
64 
65 int
66 copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
67 {
68 	len = min(strnlen(kaddr, len), len) + 1;
69 	strncpy(uaddr, kaddr, len);
70 	if (done)
71 		*done = len;
72 	return 0;
73 }
74 
75 int
76 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
77 {
78 	len = min(strnlen(kfaddr, len), len) + 1;
79 	strncpy(kdaddr, kfaddr, len);
80 	if (done)
81 		*done = len;
82 	return 0;
83 }
84 
85 int
86 kcopy(const void *src, void *dst, size_t len)
87 {
88 	memcpy(dst, src, len);
89 #ifdef DEBUG
90 	if (memcmp(dst, src, len) != 0)
91 		panic("kcopy not finished correctly\n");
92 #endif
93 	return 0;
94 }
95 
96 int
97 fuswintr(const void *base)
98 {
99 	return *(const short *)base;
100 }
101 
102 int
103 suswintr(void *base, short c)
104 {
105 	*(short *)base = c;
106 	return 0;
107 }
108 
109 int
110 subyte(void *base, int c)
111 {
112 	*(char *)base = c;
113 	return 0;
114 }
115 
116 int
117 suword(void *base, long c)
118 {
119 	*(long *)base = c;
120 	return 0;
121 }
122