1 /*      $OpenBSD: criov.c,v 1.14 2003/08/14 15:18:05 jason Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Theo de Raadt
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  *
10  * 1. Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/proc.h>
31 #include <sys/errno.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/mbuf.h>
35 
36 #include <uvm/uvm_extern.h>
37 
38 #include <crypto/cryptodev.h>
39 
40 void
cuio_copydata(struct uio * uio,int off,int len,caddr_t cp)41 cuio_copydata(struct uio *uio, int off, int len, caddr_t cp)
42 {
43 	struct iovec *iov = uio->uio_iov;
44 	int iol = uio->uio_iovcnt;
45 	unsigned count;
46 
47 	if (off < 0)
48 		panic("cuio_copydata: off %d < 0", off);
49 	if (len < 0)
50 		panic("cuio_copydata: len %d < 0", len);
51 	while (off > 0) {
52 		if (iol == 0)
53 			panic("iov_copydata: empty in skip");
54 		if (off < iov->iov_len)
55 			break;
56 		off -= iov->iov_len;
57 		iol--;
58 		iov++;
59 	}
60 	while (len > 0) {
61 		if (iol == 0)
62 			panic("cuio_copydata: empty");
63 		count = min(iov->iov_len - off, len);
64 		bcopy(((caddr_t)iov->iov_base) + off, cp, count);
65 		len -= count;
66 		cp += count;
67 		off = 0;
68 		iol--;
69 		iov++;
70 	}
71 }
72 
73 void
cuio_copyback(struct uio * uio,int off,int len,const void * cp)74 cuio_copyback(struct uio *uio, int off, int len, const void *cp)
75 {
76 	struct iovec *iov = uio->uio_iov;
77 	int iol = uio->uio_iovcnt;
78 	unsigned count;
79 
80 	if (off < 0)
81 		panic("cuio_copyback: off %d < 0", off);
82 	if (len < 0)
83 		panic("cuio_copyback: len %d < 0", len);
84 	while (off > 0) {
85 		if (iol == 0)
86 			panic("cuio_copyback: empty in skip");
87 		if (off < iov->iov_len)
88 			break;
89 		off -= iov->iov_len;
90 		iol--;
91 		iov++;
92 	}
93 	while (len > 0) {
94 		if (iol == 0)
95 			panic("uio_copyback: empty");
96 		count = min(iov->iov_len - off, len);
97 		bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
98 		len -= count;
99 		cp += count;
100 		off = 0;
101 		iol--;
102 		iov++;
103 	}
104 }
105 
106 int
cuio_getptr(struct uio * uio,int loc,int * off)107 cuio_getptr(struct uio *uio, int loc, int *off)
108 {
109 	int ind, len;
110 
111 	ind = 0;
112 	while (loc >= 0 && ind < uio->uio_iovcnt) {
113 		len = uio->uio_iov[ind].iov_len;
114 		if (len > loc) {
115 			*off = loc;
116 			return (ind);
117 		}
118 		loc -= len;
119 		ind++;
120 	}
121 
122 	if (ind > 0 && loc == 0) {
123 		ind--;
124 		*off = uio->uio_iov[ind].iov_len;
125 		return (ind);
126 	}
127 
128 	return (-1);
129 }
130 
131 int
cuio_apply(struct uio * uio,int off,int len,int (* f)(caddr_t,caddr_t,unsigned int),caddr_t fstate)132 cuio_apply(struct uio *uio, int off, int len,
133     int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate)
134 {
135 	int rval, ind, uiolen;
136 	unsigned int count;
137 
138 	if (len < 0)
139 		panic("cuio_apply: len %d < 0", len);
140 	if (off < 0)
141 		panic("cuio_apply: off %d < 0", off);
142 
143 	ind = 0;
144 	while (off > 0) {
145 		if (ind >= uio->uio_iovcnt)
146 			panic("cui_apply: ind %d >= uio_iovcnt %d for off",
147 			    ind, uio->uio_iovcnt);
148 		uiolen = uio->uio_iov[ind].iov_len;
149 		if (off < uiolen)
150 			break;
151 		off -= uiolen;
152 		ind++;
153 	}
154 	while (len > 0) {
155 		if (ind >= uio->uio_iovcnt)
156 			panic("cui_apply: ind %d >= uio_iovcnt %d for len",
157 			    ind, uio->uio_iovcnt);
158 		count = min(uio->uio_iov[ind].iov_len - off, len);
159 
160 		rval = f(fstate, uio->uio_iov[ind].iov_base + off, count);
161 		if (rval)
162 			return (rval);
163 
164 		len -= count;
165 		off = 0;
166 		ind++;
167 	}
168 
169 	return (0);
170 }
171