1.\" 2.\" Copyright (c) 1997 Joerg Wunsch 3.\" 4.\" 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 DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.\" $FreeBSD: src/share/man/man9/uio.9,v 1.5.2.4 2001/12/17 11:30:19 ru Exp $ 27.\" 28.Dd January 16, 2015 29.Dt UIO 9 30.Os 31.Sh NAME 32.Nm uio , 33.Nm uiomove 34.Nd device driver I/O routines 35.Sh SYNOPSIS 36.In sys/types.h 37.In sys/uio.h 38.Bd -literal 39struct uio { 40 struct iovec *uio_iov; 41 int uio_iovcnt; 42 off_t uio_offset; 43 size_t uio_resid; 44 enum uio_seg uio_segflg; 45 enum uio_rw uio_rw; 46 struct thread *uio_td; 47}; 48.Ed 49.Ft int 50.Fn uiomove "caddr_t buf" "size_t howmuch" "struct uio *uiop" 51.Sh DESCRIPTION 52The function 53.Fn uiomove 54is used to handle transfer of data between buffers and I/O vectors 55that might possibly also cross the user/kernel space boundary. 56.Pp 57As a result of any 58.Xr read 2 , 59.Xr write 2 , 60.Xr readv 2 , 61or 62.Xr writev 2 63system call that is being passed to a character-device driver, the 64appropriate driver 65.Nm d_read 66or 67.Nm d_write 68entry will be called with a pointer to a 69.Vt "struct dev_read_args" 70or 71.Vt "struct dev_write_args" 72being passed, 73a member of which is a pointer to a 74.Vt "struct uio" . 75The transfer request is encoded in this structure. 76The driver itself should use 77.Fn uiomove 78to get at the data in this structure. 79.Pp 80The fields in the 81.Vt uio 82structure are: 83.Bl -tag -width ".Va uio_iovcnt" 84.It Va uio_iov 85The array of I/O vectors to be processed. 86In the case of scatter/gather 87I/O, this will be more than one vector. 88.It Va uio_iovcnt 89The number of I/O vectors present. 90.It Va uio_offset 91The offset into the device. 92.It Va uio_resid 93The number of bytes to process. 94.It Va uio_segflg 95One of the following flags: 96.Bl -tag -width ".Dv UIO_USERSPACE" 97.It Dv UIO_USERSPACE 98The I/O vector points into a process's address space. 99.It Dv UIO_SYSSPACE 100The I/O vector points into the kernel address space. 101.It Dv UIO_NOCOPY 102Don't copy, already in object. 103.El 104.It Va uio_rw 105The direction of the desired transfer, either 106.Dv UIO_READ , 107or 108.Dv UIO_WRITE . 109.It Va uio_td 110The pointer to a 111.Vt "struct thread" 112for the associated thread; used if 113.Va uio_segflg 114indicates that the transfer is to be made from/to a process's address 115space. 116.El 117.Sh RETURN VALUES 118.Fn uiomove 119can return 120.Er EFAULT 121from the invoked 122.Xr copyin 9 123or 124.Xr copyout 9 125in case the transfer was to/from a process's address space. 126.Sh EXAMPLES 127The idea is that the driver maintains a private buffer for its data, 128and processes the request in chunks of maximal the size of this 129buffer. 130Note that the buffer handling below is very simplified and 131won't work (the buffer pointer is not being advanced in case of a 132partial read), it's just here to demonstrate the 133.Nm 134handling. 135.Bd -literal 136/* MIN() can be found there: */ 137#include <sys/param.h> 138 139#define BUFSIZE 512 140static char buffer[BUFSIZE]; 141 142static int data_available; /* amount of data that can be read */ 143 144static int 145fooread(struct dev_read_args *ap) 146{ 147 cdev_t dev = ap->a_head.a_dev; 148 int rv, amnt; 149 150 while (ap->a_uio->uio_resid > 0) { 151 if (data_available > 0) { 152 amnt = MIN(ap->a_uio->uio_resid, data_available); 153 if ((rv = uiomove((caddr_t)buffer, amnt, ap->a_uio)) 154 != 0) 155 goto error; 156 data_available -= amnt; 157 } else { 158 tsleep(...); /* wait for a better time */ 159 } 160 } 161 return 0; 162error: 163 /* do error cleanup here */ 164 return rv; 165} 166.Ed 167.Sh SEE ALSO 168.Xr read 2 , 169.Xr readv 2 , 170.Xr write 2 , 171.Xr writev 2 , 172.Xr copyin 9 , 173.Xr copyout 9 , 174.Xr physio 9 , 175.Xr sleep 9 176.Sh HISTORY 177The 178.Nm 179mechanism appeared in some early version of 180.Ux . 181.Sh AUTHORS 182This man page was written by 183.An J\(:org Wunsch . 184