Home | History | Annotate | Line # | Download | only in kern
subr_copy.c revision 1.1
      1  1.1  pooka /*	$NetBSD: subr_copy.c,v 1.1 2009/11/04 16:54:00 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pooka  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  pooka  * NASA Ames Research Center.
     10  1.1  pooka  *
     11  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     12  1.1  pooka  * modification, are permitted provided that the following conditions
     13  1.1  pooka  * are met:
     14  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     15  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     16  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     19  1.1  pooka  *
     20  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  pooka  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  pooka  */
     32  1.1  pooka 
     33  1.1  pooka /*
     34  1.1  pooka  * Copyright (c) 1982, 1986, 1991, 1993
     35  1.1  pooka  *	The Regents of the University of California.  All rights reserved.
     36  1.1  pooka  * (c) UNIX System Laboratories, Inc.
     37  1.1  pooka  * All or some portions of this file are derived from material licensed
     38  1.1  pooka  * to the University of California by American Telephone and Telegraph
     39  1.1  pooka  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  1.1  pooka  * the permission of UNIX System Laboratories, Inc.
     41  1.1  pooka  *
     42  1.1  pooka  * Copyright (c) 1992, 1993
     43  1.1  pooka  *	The Regents of the University of California.  All rights reserved.
     44  1.1  pooka  *
     45  1.1  pooka  * This software was developed by the Computer Systems Engineering group
     46  1.1  pooka  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     47  1.1  pooka  * contributed to Berkeley.
     48  1.1  pooka  *
     49  1.1  pooka  * All advertising materials mentioning features or use of this software
     50  1.1  pooka  * must display the following acknowledgement:
     51  1.1  pooka  *	This product includes software developed by the University of
     52  1.1  pooka  *	California, Lawrence Berkeley Laboratory.
     53  1.1  pooka  *
     54  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     55  1.1  pooka  * modification, are permitted provided that the following conditions
     56  1.1  pooka  * are met:
     57  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     58  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     59  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     60  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     61  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     62  1.1  pooka  * 3. Neither the name of the University nor the names of its contributors
     63  1.1  pooka  *    may be used to endorse or promote products derived from this software
     64  1.1  pooka  *    without specific prior written permission.
     65  1.1  pooka  *
     66  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     67  1.1  pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     68  1.1  pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     69  1.1  pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     70  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     71  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     72  1.1  pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     73  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     74  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     75  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     76  1.1  pooka  * SUCH DAMAGE.
     77  1.1  pooka  *
     78  1.1  pooka  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     79  1.1  pooka  */
     80  1.1  pooka 
     81  1.1  pooka #include <sys/cdefs.h>
     82  1.1  pooka __KERNEL_RCSID(0, "$NetBSD: subr_copy.c,v 1.1 2009/11/04 16:54:00 pooka Exp $");
     83  1.1  pooka 
     84  1.1  pooka #include <sys/param.h>
     85  1.1  pooka #include <sys/fcntl.h>
     86  1.1  pooka #include <sys/proc.h>
     87  1.1  pooka #include <sys/systm.h>
     88  1.1  pooka 
     89  1.1  pooka #include <uvm/uvm_extern.h>
     90  1.1  pooka 
     91  1.1  pooka void
     92  1.1  pooka uio_setup_sysspace(struct uio *uio)
     93  1.1  pooka {
     94  1.1  pooka 
     95  1.1  pooka 	uio->uio_vmspace = vmspace_kernel();
     96  1.1  pooka }
     97  1.1  pooka 
     98  1.1  pooka int
     99  1.1  pooka uiomove(void *buf, size_t n, struct uio *uio)
    100  1.1  pooka {
    101  1.1  pooka 	struct vmspace *vm = uio->uio_vmspace;
    102  1.1  pooka 	struct iovec *iov;
    103  1.1  pooka 	size_t cnt;
    104  1.1  pooka 	int error = 0;
    105  1.1  pooka 	char *cp = buf;
    106  1.1  pooka 
    107  1.1  pooka 	ASSERT_SLEEPABLE();
    108  1.1  pooka 
    109  1.1  pooka #ifdef DIAGNOSTIC
    110  1.1  pooka 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    111  1.1  pooka 		panic("uiomove: mode");
    112  1.1  pooka #endif
    113  1.1  pooka 	while (n > 0 && uio->uio_resid) {
    114  1.1  pooka 		iov = uio->uio_iov;
    115  1.1  pooka 		cnt = iov->iov_len;
    116  1.1  pooka 		if (cnt == 0) {
    117  1.1  pooka 			KASSERT(uio->uio_iovcnt > 0);
    118  1.1  pooka 			uio->uio_iov++;
    119  1.1  pooka 			uio->uio_iovcnt--;
    120  1.1  pooka 			continue;
    121  1.1  pooka 		}
    122  1.1  pooka 		if (cnt > n)
    123  1.1  pooka 			cnt = n;
    124  1.1  pooka 		if (!VMSPACE_IS_KERNEL_P(vm)) {
    125  1.1  pooka 			if (curcpu()->ci_schedstate.spc_flags &
    126  1.1  pooka 			    SPCF_SHOULDYIELD)
    127  1.1  pooka 				preempt();
    128  1.1  pooka 		}
    129  1.1  pooka 
    130  1.1  pooka 		if (uio->uio_rw == UIO_READ) {
    131  1.1  pooka 			error = copyout_vmspace(vm, cp, iov->iov_base,
    132  1.1  pooka 			    cnt);
    133  1.1  pooka 		} else {
    134  1.1  pooka 			error = copyin_vmspace(vm, iov->iov_base, cp,
    135  1.1  pooka 			    cnt);
    136  1.1  pooka 		}
    137  1.1  pooka 		if (error) {
    138  1.1  pooka 			break;
    139  1.1  pooka 		}
    140  1.1  pooka 		iov->iov_base = (char *)iov->iov_base + cnt;
    141  1.1  pooka 		iov->iov_len -= cnt;
    142  1.1  pooka 		uio->uio_resid -= cnt;
    143  1.1  pooka 		uio->uio_offset += cnt;
    144  1.1  pooka 		cp += cnt;
    145  1.1  pooka 		KDASSERT(cnt <= n);
    146  1.1  pooka 		n -= cnt;
    147  1.1  pooka 	}
    148  1.1  pooka 
    149  1.1  pooka 	return (error);
    150  1.1  pooka }
    151  1.1  pooka 
    152  1.1  pooka /*
    153  1.1  pooka  * Wrapper for uiomove() that validates the arguments against a known-good
    154  1.1  pooka  * kernel buffer.
    155  1.1  pooka  */
    156  1.1  pooka int
    157  1.1  pooka uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
    158  1.1  pooka {
    159  1.1  pooka 	size_t offset;
    160  1.1  pooka 
    161  1.1  pooka 	if (uio->uio_offset < 0 || /* uio->uio_resid < 0 || */
    162  1.1  pooka 	    (offset = uio->uio_offset) != uio->uio_offset)
    163  1.1  pooka 		return (EINVAL);
    164  1.1  pooka 	if (offset >= buflen)
    165  1.1  pooka 		return (0);
    166  1.1  pooka 	return (uiomove((char *)buf + offset, buflen - offset, uio));
    167  1.1  pooka }
    168  1.1  pooka 
    169  1.1  pooka /*
    170  1.1  pooka  * Give next character to user as result of read.
    171  1.1  pooka  */
    172  1.1  pooka int
    173  1.1  pooka ureadc(int c, struct uio *uio)
    174  1.1  pooka {
    175  1.1  pooka 	struct iovec *iov;
    176  1.1  pooka 
    177  1.1  pooka 	if (uio->uio_resid <= 0)
    178  1.1  pooka 		panic("ureadc: non-positive resid");
    179  1.1  pooka again:
    180  1.1  pooka 	if (uio->uio_iovcnt <= 0)
    181  1.1  pooka 		panic("ureadc: non-positive iovcnt");
    182  1.1  pooka 	iov = uio->uio_iov;
    183  1.1  pooka 	if (iov->iov_len <= 0) {
    184  1.1  pooka 		uio->uio_iovcnt--;
    185  1.1  pooka 		uio->uio_iov++;
    186  1.1  pooka 		goto again;
    187  1.1  pooka 	}
    188  1.1  pooka 	if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
    189  1.1  pooka 		if (subyte(iov->iov_base, c) < 0)
    190  1.1  pooka 			return (EFAULT);
    191  1.1  pooka 	} else {
    192  1.1  pooka 		*(char *)iov->iov_base = c;
    193  1.1  pooka 	}
    194  1.1  pooka 	iov->iov_base = (char *)iov->iov_base + 1;
    195  1.1  pooka 	iov->iov_len--;
    196  1.1  pooka 	uio->uio_resid--;
    197  1.1  pooka 	uio->uio_offset++;
    198  1.1  pooka 	return (0);
    199  1.1  pooka }
    200  1.1  pooka 
    201  1.1  pooka /*
    202  1.1  pooka  * Like copyin(), but operates on an arbitrary vmspace.
    203  1.1  pooka  */
    204  1.1  pooka int
    205  1.1  pooka copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
    206  1.1  pooka {
    207  1.1  pooka 	struct iovec iov;
    208  1.1  pooka 	struct uio uio;
    209  1.1  pooka 	int error;
    210  1.1  pooka 
    211  1.1  pooka 	if (len == 0)
    212  1.1  pooka 		return (0);
    213  1.1  pooka 
    214  1.1  pooka 	if (VMSPACE_IS_KERNEL_P(vm)) {
    215  1.1  pooka 		return kcopy(uaddr, kaddr, len);
    216  1.1  pooka 	}
    217  1.1  pooka 	if (__predict_true(vm == curproc->p_vmspace)) {
    218  1.1  pooka 		return copyin(uaddr, kaddr, len);
    219  1.1  pooka 	}
    220  1.1  pooka 
    221  1.1  pooka 	iov.iov_base = kaddr;
    222  1.1  pooka 	iov.iov_len = len;
    223  1.1  pooka 	uio.uio_iov = &iov;
    224  1.1  pooka 	uio.uio_iovcnt = 1;
    225  1.1  pooka 	uio.uio_offset = (off_t)(uintptr_t)uaddr;
    226  1.1  pooka 	uio.uio_resid = len;
    227  1.1  pooka 	uio.uio_rw = UIO_READ;
    228  1.1  pooka 	UIO_SETUP_SYSSPACE(&uio);
    229  1.1  pooka 	error = uvm_io(&vm->vm_map, &uio);
    230  1.1  pooka 
    231  1.1  pooka 	return (error);
    232  1.1  pooka }
    233  1.1  pooka 
    234  1.1  pooka /*
    235  1.1  pooka  * Like copyout(), but operates on an arbitrary vmspace.
    236  1.1  pooka  */
    237  1.1  pooka int
    238  1.1  pooka copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
    239  1.1  pooka {
    240  1.1  pooka 	struct iovec iov;
    241  1.1  pooka 	struct uio uio;
    242  1.1  pooka 	int error;
    243  1.1  pooka 
    244  1.1  pooka 	if (len == 0)
    245  1.1  pooka 		return (0);
    246  1.1  pooka 
    247  1.1  pooka 	if (VMSPACE_IS_KERNEL_P(vm)) {
    248  1.1  pooka 		return kcopy(kaddr, uaddr, len);
    249  1.1  pooka 	}
    250  1.1  pooka 	if (__predict_true(vm == curproc->p_vmspace)) {
    251  1.1  pooka 		return copyout(kaddr, uaddr, len);
    252  1.1  pooka 	}
    253  1.1  pooka 
    254  1.1  pooka 	iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
    255  1.1  pooka 	iov.iov_len = len;
    256  1.1  pooka 	uio.uio_iov = &iov;
    257  1.1  pooka 	uio.uio_iovcnt = 1;
    258  1.1  pooka 	uio.uio_offset = (off_t)(uintptr_t)uaddr;
    259  1.1  pooka 	uio.uio_resid = len;
    260  1.1  pooka 	uio.uio_rw = UIO_WRITE;
    261  1.1  pooka 	UIO_SETUP_SYSSPACE(&uio);
    262  1.1  pooka 	error = uvm_io(&vm->vm_map, &uio);
    263  1.1  pooka 
    264  1.1  pooka 	return (error);
    265  1.1  pooka }
    266  1.1  pooka 
    267  1.1  pooka /*
    268  1.1  pooka  * Like copyin(), but operates on an arbitrary process.
    269  1.1  pooka  */
    270  1.1  pooka int
    271  1.1  pooka copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
    272  1.1  pooka {
    273  1.1  pooka 	struct vmspace *vm;
    274  1.1  pooka 	int error;
    275  1.1  pooka 
    276  1.1  pooka 	error = proc_vmspace_getref(p, &vm);
    277  1.1  pooka 	if (error) {
    278  1.1  pooka 		return error;
    279  1.1  pooka 	}
    280  1.1  pooka 	error = copyin_vmspace(vm, uaddr, kaddr, len);
    281  1.1  pooka 	uvmspace_free(vm);
    282  1.1  pooka 
    283  1.1  pooka 	return error;
    284  1.1  pooka }
    285  1.1  pooka 
    286  1.1  pooka /*
    287  1.1  pooka  * Like copyout(), but operates on an arbitrary process.
    288  1.1  pooka  */
    289  1.1  pooka int
    290  1.1  pooka copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
    291  1.1  pooka {
    292  1.1  pooka 	struct vmspace *vm;
    293  1.1  pooka 	int error;
    294  1.1  pooka 
    295  1.1  pooka 	error = proc_vmspace_getref(p, &vm);
    296  1.1  pooka 	if (error) {
    297  1.1  pooka 		return error;
    298  1.1  pooka 	}
    299  1.1  pooka 	error = copyout_vmspace(vm, kaddr, uaddr, len);
    300  1.1  pooka 	uvmspace_free(vm);
    301  1.1  pooka 
    302  1.1  pooka 	return error;
    303  1.1  pooka }
    304  1.1  pooka 
    305  1.1  pooka /*
    306  1.1  pooka  * Like copyin(), except it operates on kernel addresses when the FKIOCTL
    307  1.1  pooka  * flag is passed in `ioctlflags' from the ioctl call.
    308  1.1  pooka  */
    309  1.1  pooka int
    310  1.1  pooka ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len)
    311  1.1  pooka {
    312  1.1  pooka 	if (ioctlflags & FKIOCTL)
    313  1.1  pooka 		return kcopy(src, dst, len);
    314  1.1  pooka 	return copyin(src, dst, len);
    315  1.1  pooka }
    316  1.1  pooka 
    317  1.1  pooka /*
    318  1.1  pooka  * Like copyout(), except it operates on kernel addresses when the FKIOCTL
    319  1.1  pooka  * flag is passed in `ioctlflags' from the ioctl call.
    320  1.1  pooka  */
    321  1.1  pooka int
    322  1.1  pooka ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len)
    323  1.1  pooka {
    324  1.1  pooka 	if (ioctlflags & FKIOCTL)
    325  1.1  pooka 		return kcopy(src, dst, len);
    326  1.1  pooka 	return copyout(src, dst, len);
    327  1.1  pooka }
    328