rumpcopy.c revision 1.7
1/*	$NetBSD: rumpcopy.c,v 1.7 2010/10/29 15:27:50 pooka Exp $	*/
2
3/*
4 * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: rumpcopy.c,v 1.7 2010/10/29 15:27:50 pooka Exp $");
30
31#include <sys/param.h>
32#include <sys/lwp.h>
33#include <sys/systm.h>
34
35#include <rump/rump.h>
36
37#include "rump_private.h"
38
39int
40copyin(const void *uaddr, void *kaddr, size_t len)
41{
42
43	if (__predict_false(uaddr == NULL && len)) {
44		return EFAULT;
45	}
46
47	if (curproc->p_vmspace == &vmspace0) {
48		memcpy(kaddr, uaddr, len);
49	} else {
50		rumpuser_sp_copyin(uaddr, kaddr, len);
51	}
52	return 0;
53}
54
55int
56copyout(const void *kaddr, void *uaddr, size_t len)
57{
58
59	if (__predict_false(uaddr == NULL && len)) {
60		return EFAULT;
61	}
62
63	if (curproc->p_vmspace == &vmspace0) {
64		memcpy(uaddr, kaddr, len);
65	} else {
66		rumpuser_sp_copyout(kaddr, uaddr, len);
67	}
68	return 0;
69}
70
71int
72subyte(void *uaddr, int byte)
73{
74
75	if (curproc->p_vmspace == &vmspace0)
76		*(char *)uaddr = byte;
77	else
78		rumpuser_sp_copyout(&byte, uaddr, 1);
79	return 0;
80}
81
82int
83copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
84{
85
86	strlcpy(kdaddr, kfaddr, len);
87	if (done)
88		*done = strlen(kdaddr)+1; /* includes termination */
89	return 0;
90}
91
92int
93copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
94{
95
96	if (curproc->p_vmspace == &vmspace0)
97		strlcpy(kaddr, uaddr, len);
98	else
99		rumpuser_sp_copyin(uaddr, kaddr, len);
100	if (done)
101		*done = strlen(kaddr)+1; /* includes termination */
102	return 0;
103}
104
105int
106copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
107{
108
109	if (curproc->p_vmspace == &vmspace0)
110		strlcpy(uaddr, kaddr, len);
111	else
112		rumpuser_sp_copyout(kaddr, uaddr, len);
113	if (done)
114		*done = strlen(uaddr)+1; /* includes termination */
115	return 0;
116}
117
118int
119kcopy(const void *src, void *dst, size_t len)
120{
121
122	memcpy(dst, src, len);
123	return 0;
124}
125