Home | History | Annotate | Line # | Download | only in rumpkern
rumpcopy.c revision 1.18.6.2
      1 /*	$NetBSD: rumpcopy.c,v 1.18.6.2 2015/06/06 14:40:29 skrll 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.18.6.2 2015/06/06 14:40:29 skrll Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/lwp.h>
     33 #include <sys/systm.h>
     34 #include <sys/uio.h>
     35 
     36 #include <rump/rumpuser.h>
     37 
     38 #include "rump_private.h"
     39 
     40 int
     41 copyin(const void *uaddr, void *kaddr, size_t len)
     42 {
     43 	int error = 0;
     44 
     45 	if (__predict_false(uaddr == NULL && len)) {
     46 		return EFAULT;
     47 	}
     48 
     49 	if (RUMP_LOCALPROC_P(curproc)) {
     50 		memcpy(kaddr, uaddr, len);
     51 	} else if (len) {
     52 		error = rump_sysproxy_copyin(RUMP_SPVM2CTL(curproc->p_vmspace),
     53 		    uaddr, kaddr, len);
     54 	}
     55 
     56 	return error;
     57 }
     58 
     59 int
     60 copyout(const void *kaddr, void *uaddr, size_t len)
     61 {
     62 	int error = 0;
     63 
     64 	if (__predict_false(uaddr == NULL && len)) {
     65 		return EFAULT;
     66 	}
     67 
     68 	if (RUMP_LOCALPROC_P(curproc)) {
     69 		memcpy(uaddr, kaddr, len);
     70 	} else if (len) {
     71 		error = rump_sysproxy_copyout(RUMP_SPVM2CTL(curproc->p_vmspace),
     72 		    kaddr, uaddr, len);
     73 	}
     74 	return error;
     75 }
     76 
     77 int
     78 subyte(void *uaddr, int byte)
     79 {
     80 	int error = 0;
     81 
     82 	if (RUMP_LOCALPROC_P(curproc))
     83 		*(char *)uaddr = byte;
     84 	else
     85 		error = rump_sysproxy_copyout(RUMP_SPVM2CTL(curproc->p_vmspace),
     86 		    &byte, uaddr, 1);
     87 
     88 	return error;
     89 }
     90 
     91 int
     92 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
     93 {
     94 	uint8_t *to = kdaddr;
     95 	const uint8_t *from = kfaddr;
     96 	size_t actlen = 0;
     97 
     98 	while (len-- > 0 && (*to++ = *from++) != 0)
     99 		actlen++;
    100 
    101 	if (len+1 == 0 && *(to-1) != 0)
    102 		return ENAMETOOLONG;
    103 
    104 	if (done)
    105 		*done = actlen+1; /* + '\0' */
    106 	return 0;
    107 }
    108 
    109 int
    110 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    111 {
    112 	uint8_t *to;
    113 	int rv;
    114 
    115 	if (len == 0)
    116 		return 0;
    117 
    118 	if (__predict_false(uaddr == NULL)) {
    119 		return EFAULT;
    120 	}
    121 
    122 	if (RUMP_LOCALPROC_P(curproc))
    123 		return copystr(uaddr, kaddr, len, done);
    124 
    125 	if ((rv = rump_sysproxy_copyinstr(RUMP_SPVM2CTL(curproc->p_vmspace),
    126 	    uaddr, kaddr, &len)) != 0)
    127 		return rv;
    128 
    129 	/* figure out if we got a terminated string or not */
    130 	to = (uint8_t *)kaddr + (len-1);
    131 	while (to >= (uint8_t *)kaddr) {
    132 		if (*to == 0)
    133 			goto found;
    134 		to--;
    135 	}
    136 	return ENAMETOOLONG;
    137 
    138  found:
    139 	if (done)
    140 		*done = strlen(kaddr)+1; /* includes termination */
    141 
    142 	return 0;
    143 }
    144 
    145 int
    146 copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
    147 {
    148 	size_t slen;
    149 	int error;
    150 
    151 	if (__predict_false(uaddr == NULL && len)) {
    152 		return EFAULT;
    153 	}
    154 
    155 	if (RUMP_LOCALPROC_P(curproc))
    156 		return copystr(kaddr, uaddr, len, done);
    157 
    158 	slen = strlen(kaddr)+1;
    159 	if (slen > len)
    160 		return ENAMETOOLONG;
    161 
    162 	error = rump_sysproxy_copyoutstr(RUMP_SPVM2CTL(curproc->p_vmspace),
    163 	    kaddr, uaddr, &slen);
    164 	if (done)
    165 		*done = slen;
    166 
    167 	return error;
    168 }
    169 
    170 int
    171 kcopy(const void *src, void *dst, size_t len)
    172 {
    173 
    174 	memcpy(dst, src, len);
    175 	return 0;
    176 }
    177 
    178 /*
    179  * Low-level I/O routine.  This is used only when "all else fails",
    180  * i.e. the current thread does not have an appropriate vm context.
    181  */
    182 int
    183 uvm_io(struct vm_map *vm, struct uio *uio)
    184 {
    185 	int error = 0;
    186 
    187 	/* loop over iovecs one-by-one and copyout */
    188 	for (; uio->uio_resid && uio->uio_iovcnt;
    189 	    uio->uio_iovcnt--, uio->uio_iov++) {
    190 		struct iovec *iov = uio->uio_iov;
    191 		size_t curlen = MIN(uio->uio_resid, iov->iov_len);
    192 
    193 		if (__predict_false(curlen == 0))
    194 			continue;
    195 
    196 		if (uio->uio_rw == UIO_READ) {
    197 			error = rump_sysproxy_copyin(RUMP_SPVM2CTL(vm),
    198 			    (void *)(vaddr_t)uio->uio_offset, iov->iov_base,
    199 			    curlen);
    200 		} else {
    201 			error = rump_sysproxy_copyout(RUMP_SPVM2CTL(vm),
    202 			    iov->iov_base, (void *)(vaddr_t)uio->uio_offset,
    203 			    curlen);
    204 		}
    205 		if (error)
    206 			break;
    207 
    208 		iov->iov_base = (uint8_t *)iov->iov_base + curlen;
    209 		iov->iov_len -= curlen;
    210 
    211 		uio->uio_resid -= curlen;
    212 		uio->uio_offset += curlen;
    213 	}
    214 
    215 	return error;
    216 }
    217 
    218 /*
    219  * Copy one byte from userspace to kernel.
    220  */
    221 int
    222 fubyte(const void *base)
    223 {
    224 	unsigned char val;
    225 	int error;
    226 
    227 	error = copyin(base, &val, sizeof(char));
    228 	if (error != 0)
    229 		return -1;
    230 
    231 	return (int)val;
    232 }
    233