xref: /freebsd/share/man/man9/uio.9 (revision 3e102307b7637eaab1e213740399e9ce50ef3c1b)
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$
27.\"
28.Dd January 19, 2012
29.Dt UIO 9
30.Os
31.Sh NAME
32.Nm uio ,
33.Nm uiomove ,
34.Nm uiomove_nofault
35.Nd device driver I/O routines
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/uio.h
39.Bd -literal
40struct uio {
41	struct	iovec *uio_iov;		/* scatter/gather list */
42	int	uio_iovcnt;		/* length of scatter/gather list */
43	off_t	uio_offset;		/* offset in target object */
44	ssize_t	uio_resid;		/* remaining bytes to copy */
45	enum	uio_seg uio_segflg;	/* address space */
46	enum	uio_rw uio_rw;		/* operation */
47	struct	thread *uio_td;		/* owner */
48};
49.Ed
50.Ft int
51.Fn uiomove "void *buf" "int howmuch" "struct uio *uiop"
52.Ft int
53.Fn uiomove_nofault "void *buf" "int howmuch" "struct uio *uiop"
54.Sh DESCRIPTION
55The functions
56.Fn uiomove
57and
58.Fn uiomove_nofault
59are used to transfer data between buffers and I/O vectors that might
60possibly cross the user/kernel space boundary.
61.Pp
62As a result of any
63.Xr read 2 ,
64.Xr write 2 ,
65.Xr readv 2 ,
66or
67.Xr writev 2
68system call that is being passed to a character-device driver, the
69appropriate driver
70.Va d_read
71or
72.Va d_write
73entry will be called with a pointer to a
74.Vt "struct uio"
75being passed.
76The transfer request is encoded in this structure.
77The driver itself should use
78.Fn uiomove
79or
80.Fn uiomove_nofault
81to get at the data in this structure.
82.Pp
83The fields in the
84.Vt uio
85structure are:
86.Bl -tag -width ".Va uio_iovcnt"
87.It Va uio_iov
88The array of I/O vectors to be processed.
89In the case of scatter/gather
90I/O, this will be more than one vector.
91.It Va uio_iovcnt
92The number of I/O vectors present.
93.It Va uio_offset
94The offset into the device.
95.It Va uio_resid
96The remaining number of bytes to process, updated after transfer.
97.It Va uio_segflg
98One of the following flags:
99.Bl -tag -width ".Dv UIO_USERSPACE"
100.It Dv UIO_USERSPACE
101The I/O vector points into a process's address space.
102.It Dv UIO_SYSSPACE
103The I/O vector points into the kernel address space.
104.It Dv UIO_NOCOPY
105Do not copy, already in object.
106.El
107.It Va uio_rw
108The direction of the desired transfer, either
109.Dv UIO_READ
110or
111.Dv UIO_WRITE .
112.It Va uio_td
113The pointer to a
114.Vt "struct thread"
115for the associated thread; used if
116.Va uio_segflg
117indicates that the transfer is to be made from/to a process's address
118space.
119.El
120.Pp
121The function
122.Fn uiomove_nofault
123requires that the buffer and I/O vectors be accessible without
124incurring a page fault.
125The source and destination addresses must be physically mapped for
126read and write access, respectively, and neither the source nor
127destination addresses may be pageable.
128Thus, the function
129.Fn uiomove_nofault
130can be called from contexts where acquiring virtual memory system
131locks or sleeping are prohibited.
132.Sh RETURN VALUES
133On success
134.Fn uiomove
135and
136.Fn uiomove_nofault
137will return 0; on error they will return an appropriate error code.
138.Sh EXAMPLES
139The idea is that the driver maintains a private buffer for its data,
140and processes the request in chunks of maximal the size of this
141buffer.
142Note that the buffer handling below is very simplified and
143will not work (the buffer pointer is not being advanced in case of a
144partial read), it is just here to demonstrate the
145.Nm
146handling.
147.Bd -literal
148/* MIN() can be found there: */
149#include <sys/param.h>
150
151#define BUFSIZE 512
152static char buffer[BUFSIZE];
153
154static int data_available;	/* amount of data that can be read */
155
156static int
157fooread(struct cdev *dev, struct uio *uio, int flag)
158{
159	int rv, amnt;
160
161	rv = 0;
162	while (uio->uio_resid > 0) {
163		if (data_available > 0) {
164			amnt = MIN(uio->uio_resid, data_available);
165			rv = uiomove(buffer, amnt, uio);
166			if (rv != 0)
167				break;
168			data_available -= amnt;
169		} else
170			tsleep(...);	/* wait for a better time */
171	}
172	if (rv != 0) {
173		/* do error cleanup here */
174	}
175	return (rv);
176}
177.Ed
178.Sh ERRORS
179.Fn uiomove
180and
181.Fn uiomove_nofault
182will fail and return the following error code if:
183.Bl -tag -width Er
184.It Bq Er EFAULT
185The invoked
186.Xr copyin 9
187or
188.Xr copyout 9
189returned
190.Er EFAULT
191.El
192.Pp
193In addition,
194.Fn uiomove_nofault
195will fail and return the following error code if:
196.Bl -tag -width Er
197.It Bq Er EFAULT
198A page fault occurs.
199.El
200.Sh SEE ALSO
201.Xr read 2 ,
202.Xr readv 2 ,
203.Xr write 2 ,
204.Xr writev 2 ,
205.Xr copyin 9 ,
206.Xr copyout 9 ,
207.Xr sleep 9
208.Sh HISTORY
209The
210.Nm
211mechanism appeared in some early version of
212.Ux .
213.Sh AUTHORS
214This manual page was written by
215.An J\(:org Wunsch .
216