.\" .\" Copyright (c) 1997 Joerg Wunsch .\" .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" $FreeBSD: src/share/man/man9/uio.9,v 1.5.2.4 2001/12/17 11:30:19 ru Exp $ .\" .Dd January 16, 2015 .Dt UIO 9 .Os .Sh NAME .Nm uio , .Nm uiomove .Nd device driver I/O routines .Sh SYNOPSIS .In sys/types.h .In sys/uio.h .Bd -literal struct uio { struct iovec *uio_iov; int uio_iovcnt; off_t uio_offset; size_t uio_resid; enum uio_seg uio_segflg; enum uio_rw uio_rw; struct thread *uio_td; }; .Ed .Ft int .Fn uiomove "caddr_t buf" "size_t howmuch" "struct uio *uiop" .Sh DESCRIPTION The function .Fn uiomove is used to handle transfer of data between buffers and I/O vectors that might possibly also cross the user/kernel space boundary. .Pp As a result of any .Xr read 2 , .Xr write 2 , .Xr readv 2 , or .Xr writev 2 system call that is being passed to a character-device driver, the appropriate driver .Nm d_read or .Nm d_write entry will be called with a pointer to a .Vt "struct dev_read_args" or .Vt "struct dev_write_args" being passed, a member of which is a pointer to a .Vt "struct uio" . The transfer request is encoded in this structure. The driver itself should use .Fn uiomove to get at the data in this structure. .Pp The fields in the .Vt uio structure are: .Bl -tag -width ".Va uio_iovcnt" .It Va uio_iov The array of I/O vectors to be processed. In the case of scatter/gather I/O, this will be more than one vector. .It Va uio_iovcnt The number of I/O vectors present. .It Va uio_offset The offset into the device. .It Va uio_resid The number of bytes to process. .It Va uio_segflg One of the following flags: .Bl -tag -width ".Dv UIO_USERSPACE" .It Dv UIO_USERSPACE The I/O vector points into a process's address space. .It Dv UIO_SYSSPACE The I/O vector points into the kernel address space. .It Dv UIO_NOCOPY Don't copy, already in object. .El .It Va uio_rw The direction of the desired transfer, either .Dv UIO_READ , or .Dv UIO_WRITE . .It Va uio_td The pointer to a .Vt "struct thread" for the associated thread; used if .Va uio_segflg indicates that the transfer is to be made from/to a process's address space. .El .Sh RETURN VALUES .Fn uiomove can return .Er EFAULT from the invoked .Xr copyin 9 or .Xr copyout 9 in case the transfer was to/from a process's address space. .Sh EXAMPLES The idea is that the driver maintains a private buffer for its data, and processes the request in chunks of maximal the size of this buffer. Note that the buffer handling below is very simplified and won't work (the buffer pointer is not being advanced in case of a partial read), it's just here to demonstrate the .Nm handling. .Bd -literal /* MIN() can be found there: */ #include #define BUFSIZE 512 static char buffer[BUFSIZE]; static int data_available; /* amount of data that can be read */ static int fooread(struct dev_read_args *ap) { cdev_t dev = ap->a_head.a_dev; int rv, amnt; while (ap->a_uio->uio_resid > 0) { if (data_available > 0) { amnt = MIN(ap->a_uio->uio_resid, data_available); if ((rv = uiomove((caddr_t)buffer, amnt, ap->a_uio)) != 0) goto error; data_available -= amnt; } else { tsleep(...); /* wait for a better time */ } } return 0; error: /* do error cleanup here */ return rv; } .Ed .Sh SEE ALSO .Xr read 2 , .Xr readv 2 , .Xr write 2 , .Xr writev 2 , .Xr copyin 9 , .Xr copyout 9 , .Xr physio 9 , .Xr sleep 9 .Sh HISTORY The .Nm mechanism appeared in some early version of .Ux . .Sh AUTHORS This man page was written by .An J\(:org Wunsch .