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