1*d1579b2dSriastradh /* $NetBSD: criov.c,v 1.9 2018/09/03 16:29:37 riastradh Exp $ */
2cdfce9ceSjonathan /* $OpenBSD: criov.c,v 1.11 2002/06/10 19:36:43 espie Exp $ */
3cdfce9ceSjonathan
4cdfce9ceSjonathan /*
5cdfce9ceSjonathan * Copyright (c) 1999 Theo de Raadt
6cdfce9ceSjonathan *
7cdfce9ceSjonathan * Redistribution and use in source and binary forms, with or without
8cdfce9ceSjonathan * modification, are permitted provided that the following conditions
9cdfce9ceSjonathan * are met:
10cdfce9ceSjonathan *
11cdfce9ceSjonathan * 1. Redistributions of source code must retain the above copyright
12cdfce9ceSjonathan * notice, this list of conditions and the following disclaimer.
13cdfce9ceSjonathan * 2. Redistributions in binary form must reproduce the above copyright
14cdfce9ceSjonathan * notice, this list of conditions and the following disclaimer in the
15cdfce9ceSjonathan * documentation and/or other materials provided with the distribution.
16cdfce9ceSjonathan * 3. The name of the author may not be used to endorse or promote products
17cdfce9ceSjonathan * derived from this software without specific prior written permission.
18cdfce9ceSjonathan *
19cdfce9ceSjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20cdfce9ceSjonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21cdfce9ceSjonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22cdfce9ceSjonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23cdfce9ceSjonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24cdfce9ceSjonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25cdfce9ceSjonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26cdfce9ceSjonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27cdfce9ceSjonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28cdfce9ceSjonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29cdfce9ceSjonathan */
30cdfce9ceSjonathan
31cdfce9ceSjonathan #include <sys/cdefs.h>
32*d1579b2dSriastradh __KERNEL_RCSID(0, "$NetBSD: criov.c,v 1.9 2018/09/03 16:29:37 riastradh Exp $");
33cdfce9ceSjonathan
34cdfce9ceSjonathan #include <sys/param.h>
35cdfce9ceSjonathan #include <sys/systm.h>
36cdfce9ceSjonathan #include <sys/proc.h>
37cdfce9ceSjonathan #include <sys/errno.h>
38cdfce9ceSjonathan #include <sys/malloc.h>
39cdfce9ceSjonathan #include <sys/kernel.h>
40cdfce9ceSjonathan #include <sys/mbuf.h>
41cdfce9ceSjonathan
42cdfce9ceSjonathan #include <uvm/uvm_extern.h>
43cdfce9ceSjonathan
44cdfce9ceSjonathan #include <opencrypto/cryptodev.h>
45cdfce9ceSjonathan int cuio_getindx(struct uio *uio, int loc, int *off);
46cdfce9ceSjonathan
47cdfce9ceSjonathan
48cdfce9ceSjonathan void
cuio_copydata(struct uio * uio,int off,int len,void * cp)4982357f6dSdsl cuio_copydata(struct uio *uio, int off, int len, void *cp)
50cdfce9ceSjonathan {
51cdfce9ceSjonathan struct iovec *iov = uio->uio_iov;
52cdfce9ceSjonathan int iol = uio->uio_iovcnt;
53cdfce9ceSjonathan unsigned count;
54cdfce9ceSjonathan
55cdfce9ceSjonathan if (off < 0)
56cdfce9ceSjonathan panic("cuio_copydata: off %d < 0", off);
57cdfce9ceSjonathan if (len < 0)
58cdfce9ceSjonathan panic("cuio_copydata: len %d < 0", len);
59cdfce9ceSjonathan while (off > 0) {
60cdfce9ceSjonathan if (iol == 0)
61cdfce9ceSjonathan panic("iov_copydata: empty in skip");
62cdfce9ceSjonathan if (off < iov->iov_len)
63cdfce9ceSjonathan break;
64cdfce9ceSjonathan off -= iov->iov_len;
65cdfce9ceSjonathan iol--;
66cdfce9ceSjonathan iov++;
67cdfce9ceSjonathan }
68cdfce9ceSjonathan while (len > 0) {
69cdfce9ceSjonathan if (iol == 0)
70cdfce9ceSjonathan panic("cuio_copydata: empty");
71*d1579b2dSriastradh count = uimin(iov->iov_len - off, len);
7253524e44Schristos memcpy(cp, (char *)iov->iov_base + off, count);
73cdfce9ceSjonathan len -= count;
7453524e44Schristos cp = (char *)cp + count;
75cdfce9ceSjonathan off = 0;
76cdfce9ceSjonathan iol--;
77cdfce9ceSjonathan iov++;
78cdfce9ceSjonathan }
79cdfce9ceSjonathan }
80cdfce9ceSjonathan
81cdfce9ceSjonathan void
cuio_copyback(struct uio * uio,int off,int len,void * cp)8282357f6dSdsl cuio_copyback(struct uio *uio, int off, int len, void *cp)
83cdfce9ceSjonathan {
84cdfce9ceSjonathan struct iovec *iov = uio->uio_iov;
85cdfce9ceSjonathan int iol = uio->uio_iovcnt;
86cdfce9ceSjonathan unsigned count;
87cdfce9ceSjonathan
88cdfce9ceSjonathan if (off < 0)
89cdfce9ceSjonathan panic("cuio_copyback: off %d < 0", off);
90cdfce9ceSjonathan if (len < 0)
91cdfce9ceSjonathan panic("cuio_copyback: len %d < 0", len);
92cdfce9ceSjonathan while (off > 0) {
938b3e4906Sdrochner if (iol == 0) {
948b3e4906Sdrochner #ifdef DEBUG
958b3e4906Sdrochner printf("cuio_copyback: empty in skip\n");
968b3e4906Sdrochner #endif
978b3e4906Sdrochner return;
988b3e4906Sdrochner }
99cdfce9ceSjonathan if (off < iov->iov_len)
100cdfce9ceSjonathan break;
101cdfce9ceSjonathan off -= iov->iov_len;
102cdfce9ceSjonathan iol--;
103cdfce9ceSjonathan iov++;
104cdfce9ceSjonathan }
105cdfce9ceSjonathan while (len > 0) {
1068b3e4906Sdrochner if (iol == 0) {
1078b3e4906Sdrochner #ifdef DEBUG
1088b3e4906Sdrochner printf("uio_copyback: empty\n");
1098b3e4906Sdrochner #endif
1108b3e4906Sdrochner return;
1118b3e4906Sdrochner }
112*d1579b2dSriastradh count = uimin(iov->iov_len - off, len);
11353524e44Schristos memcpy((char *)iov->iov_base + off, cp, count);
114cdfce9ceSjonathan len -= count;
11553524e44Schristos cp = (char *)cp + count;
116cdfce9ceSjonathan off = 0;
117cdfce9ceSjonathan iol--;
118cdfce9ceSjonathan iov++;
119cdfce9ceSjonathan }
120cdfce9ceSjonathan }
121cdfce9ceSjonathan
122cdfce9ceSjonathan /*
123cdfce9ceSjonathan * Return a pointer to iov/offset of location in iovec list.
124cdfce9ceSjonathan */
125cdfce9ceSjonathan
126cdfce9ceSjonathan int
cuio_getptr(struct uio * uio,int loc,int * off)127cdfce9ceSjonathan cuio_getptr(struct uio *uio, int loc, int *off)
128cdfce9ceSjonathan {
129cdfce9ceSjonathan int ind, len;
130cdfce9ceSjonathan
131cdfce9ceSjonathan ind = 0;
132cdfce9ceSjonathan while (loc >= 0 && ind < uio->uio_iovcnt) {
133cdfce9ceSjonathan len = uio->uio_iov[ind].iov_len;
134cdfce9ceSjonathan if (len > loc) {
135cdfce9ceSjonathan *off = loc;
136cdfce9ceSjonathan return (ind);
137cdfce9ceSjonathan }
138cdfce9ceSjonathan loc -= len;
139cdfce9ceSjonathan ind++;
140cdfce9ceSjonathan }
141cdfce9ceSjonathan
142cdfce9ceSjonathan if (ind > 0 && loc == 0) {
143cdfce9ceSjonathan ind--;
144cdfce9ceSjonathan *off = uio->uio_iov[ind].iov_len;
145cdfce9ceSjonathan return (ind);
146cdfce9ceSjonathan }
147cdfce9ceSjonathan
148cdfce9ceSjonathan return (-1);
149cdfce9ceSjonathan }
150cdfce9ceSjonathan
151cdfce9ceSjonathan int
cuio_apply(struct uio * uio,int off,int len,int (* f)(void *,void *,unsigned int),void * fstate)152cdfce9ceSjonathan cuio_apply(struct uio *uio, int off, int len,
15353524e44Schristos int (*f)(void *, void *, unsigned int), void *fstate)
154cdfce9ceSjonathan {
155cdfce9ceSjonathan int rval, ind, uiolen;
156cdfce9ceSjonathan unsigned int count;
157cdfce9ceSjonathan
158cdfce9ceSjonathan if (len < 0)
159cdfce9ceSjonathan panic("%s: len %d < 0", __func__, len);
160cdfce9ceSjonathan if (off < 0)
161cdfce9ceSjonathan panic("%s: off %d < 0", __func__, off);
162cdfce9ceSjonathan
163cdfce9ceSjonathan ind = 0;
164cdfce9ceSjonathan while (off > 0) {
165cdfce9ceSjonathan if (ind >= uio->uio_iovcnt)
166cd8ee127Slha panic("cuio_apply: out of ivecs before data in uio");
167cdfce9ceSjonathan uiolen = uio->uio_iov[ind].iov_len;
168cdfce9ceSjonathan if (off < uiolen)
169cdfce9ceSjonathan break;
170cdfce9ceSjonathan off -= uiolen;
171cdfce9ceSjonathan ind++;
172cdfce9ceSjonathan }
173cdfce9ceSjonathan while (len > 0) {
174cdfce9ceSjonathan if (ind >= uio->uio_iovcnt)
175cd8ee127Slha panic("cuio_apply: out of ivecs when processing uio");
176*d1579b2dSriastradh count = uimin(uio->uio_iov[ind].iov_len - off, len);
177cdfce9ceSjonathan
178cdfce9ceSjonathan rval = f(fstate,
17953524e44Schristos ((char *)uio->uio_iov[ind].iov_base + off), count);
180cdfce9ceSjonathan if (rval)
181cdfce9ceSjonathan return (rval);
182cdfce9ceSjonathan
183cdfce9ceSjonathan len -= count;
184cdfce9ceSjonathan off = 0;
185cdfce9ceSjonathan ind++;
186cdfce9ceSjonathan }
187cdfce9ceSjonathan
188cdfce9ceSjonathan return (0);
189cdfce9ceSjonathan }
190