]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_subr.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_subr.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
91447636 71#include <sys/proc_internal.h>
1c79356b
A
72#include <sys/malloc.h>
73#include <sys/queue.h>
55e303ae 74#include <vm/pmap.h>
91447636
A
75#include <sys/uio_internal.h>
76#include <kern/kalloc.h>
1c79356b
A
77
78#include <kdebug.h>
79
80#include <sys/kdebug.h>
81#define DBG_UIO_COPYOUT 16
82#define DBG_UIO_COPYIN 17
83
91447636
A
84#if DEBUG
85#include <kern/simple_lock.h>
86
87static int uio_t_count = 0;
88#endif /* DEBUG */
89
de355530 90
1c79356b
A
91int
92uiomove(cp, n, uio)
93 register caddr_t cp;
94 register int n;
91447636 95 register uio_t uio;
55e303ae
A
96{
97 return uiomove64((addr64_t)((unsigned int)cp), n, uio);
98}
99
91447636 100 // LP64todo - fix this! 'n' should be int64_t?
55e303ae 101int
91447636 102uiomove64(addr64_t cp, int n, register struct uio *uio)
1c79356b 103{
91447636
A
104#if LP64KERN
105 register uint64_t acnt;
106#else
107 register u_int acnt;
108#endif
1c79356b
A
109 int error = 0;
110
111#if DIAGNOSTIC
112 if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
113 panic("uiomove: mode");
1c79356b
A
114#endif
115
91447636
A
116#if LP64_DEBUG
117 if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) {
118 panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__);
119 }
120#endif /* LP64_DEBUG */
121
122 while (n > 0 && uio_resid(uio)) {
123 acnt = uio_iov_len(uio);
124 if (acnt == 0) {
125 uio_next_iov(uio);
1c79356b
A
126 uio->uio_iovcnt--;
127 continue;
128 }
91447636
A
129 if (n > 0 && acnt > (uint64_t)n)
130 acnt = n;
131
1c79356b
A
132 switch (uio->uio_segflg) {
133
91447636
A
134 case UIO_USERSPACE64:
135 case UIO_USERISPACE64:
136 // LP64 - 3rd argument in debug code is 64 bit, expected to be 32 bit
137 if (uio->uio_rw == UIO_READ)
138 {
139 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START,
140 (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 0,0);
141
142 error = copyout( CAST_DOWN(caddr_t, cp), uio->uio_iovs.iov64p->iov_base, acnt );
143
144 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END,
145 (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 0,0);
146 }
147 else
148 {
149 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START,
150 (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 0,0);
151
152 error = copyin(uio->uio_iovs.iov64p->iov_base, CAST_DOWN(caddr_t, cp), acnt);
153
154 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END,
155 (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 0,0);
156 }
157 if (error)
158 return (error);
159 break;
160
161 case UIO_USERSPACE32:
162 case UIO_USERISPACE32:
1c79356b
A
163 case UIO_USERSPACE:
164 case UIO_USERISPACE:
165 if (uio->uio_rw == UIO_READ)
166 {
167 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START,
91447636 168 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 0,0);
1c79356b 169
91447636 170 error = copyout( CAST_DOWN(caddr_t, cp), CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), acnt );
1c79356b
A
171
172 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END,
91447636 173 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 0,0);
1c79356b
A
174 }
175 else
176 {
177 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START,
91447636 178 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 0,0);
1c79356b 179
91447636 180 error = copyin(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), CAST_DOWN(caddr_t, cp), acnt);
1c79356b
A
181
182 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END,
91447636 183 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 0,0);
1c79356b
A
184 }
185 if (error)
186 return (error);
187 break;
188
91447636 189 case UIO_SYSSPACE32:
1c79356b
A
190 case UIO_SYSSPACE:
191 if (uio->uio_rw == UIO_READ)
91447636
A
192 error = copywithin(CAST_DOWN(caddr_t, cp), (caddr_t)uio->uio_iovs.iov32p->iov_base,
193 acnt);
1c79356b 194 else
91447636
A
195 error = copywithin((caddr_t)uio->uio_iovs.iov32p->iov_base, CAST_DOWN(caddr_t, cp),
196 acnt);
1c79356b 197 break;
9bccf70c 198
91447636
A
199 case UIO_PHYS_USERSPACE64:
200 if (uio->uio_rw == UIO_READ)
201 {
202 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START,
203 (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 1,0);
204
205 error = copypv((addr64_t)cp, uio->uio_iovs.iov64p->iov_base, acnt, cppvPsrc | cppvNoRefSrc);
206 if (error) /* Copy physical to virtual */
207 error = EFAULT;
208
209 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END,
210 (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 1,0);
211 }
212 else
213 {
214 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START,
215 (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 1,0);
216
217 error = copypv(uio->uio_iovs.iov64p->iov_base, (addr64_t)cp, acnt, cppvPsnk | cppvNoRefSrc | cppvNoModSnk);
218 if (error) /* Copy virtual to physical */
219 error = EFAULT;
220
221 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END,
222 (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 1,0);
223 }
224 if (error)
225 return (error);
226 break;
227
228 case UIO_PHYS_USERSPACE32:
1c79356b
A
229 case UIO_PHYS_USERSPACE:
230 if (uio->uio_rw == UIO_READ)
231 {
232 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START,
91447636 233 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 1,0);
55e303ae 234
91447636
A
235 error = copypv((addr64_t)cp, (addr64_t)uio->uio_iovs.iov32p->iov_base, acnt, cppvPsrc | cppvNoRefSrc);
236 if (error) /* Copy physical to virtual */
55e303ae
A
237 error = EFAULT;
238
239 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END,
91447636 240 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 1,0);
55e303ae
A
241 }
242 else
243 {
244 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START,
91447636 245 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 1,0);
1c79356b 246
91447636
A
247 error = copypv((addr64_t)uio->uio_iovs.iov32p->iov_base, (addr64_t)cp, acnt, cppvPsnk | cppvNoRefSrc | cppvNoModSnk);
248 if (error) /* Copy virtual to physical */
55e303ae
A
249 error = EFAULT;
250
251 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END,
91447636 252 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 1,0);
55e303ae
A
253 }
254 if (error)
255 return (error);
256 break;
257
91447636 258 case UIO_PHYS_SYSSPACE32:
55e303ae
A
259 case UIO_PHYS_SYSSPACE:
260 if (uio->uio_rw == UIO_READ)
261 {
262 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START,
91447636 263 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 2,0);
de355530 264
91447636
A
265 error = copypv((addr64_t)cp, uio->uio_iovs.iov32p->iov_base, acnt, cppvKmap | cppvPsrc | cppvNoRefSrc);
266 if (error) /* Copy physical to virtual */
55e303ae 267 error = EFAULT;
1c79356b
A
268
269 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END,
91447636 270 (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 2,0);
1c79356b
A
271 }
272 else
273 {
274 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START,
91447636 275 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 2,0);
1c79356b 276
91447636
A
277 error = copypv(uio->uio_iovs.iov32p->iov_base, (addr64_t)cp, acnt, cppvKmap | cppvPsnk | cppvNoRefSrc | cppvNoModSnk);
278 if (error) /* Copy virtual to physical */
55e303ae 279 error = EFAULT;
1c79356b
A
280
281 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END,
91447636 282 (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 2,0);
1c79356b
A
283 }
284 if (error)
285 return (error);
286 break;
91447636
A
287
288 default:
289 break;
1c79356b 290 }
91447636
A
291 uio_iov_base_add(uio, acnt);
292#if LP64KERN
293 uio_iov_len_add(uio, -((int64_t)acnt));
294 uio_setresid(uio, (uio_resid(uio) - ((int64_t)acnt)));
295#else
296 uio_iov_len_add(uio, -((int)acnt));
297 uio_setresid(uio, (uio_resid(uio) - ((int)acnt)));
298#endif
299 uio->uio_offset += acnt;
300 cp += acnt;
301 n -= acnt;
1c79356b
A
302 }
303 return (error);
304}
305
306/*
307 * Give next character to user as result of read.
308 */
309int
310ureadc(c, uio)
311 register int c;
312 register struct uio *uio;
313{
91447636 314 if (uio_resid(uio) <= 0)
1c79356b
A
315 panic("ureadc: non-positive resid");
316again:
317 if (uio->uio_iovcnt == 0)
318 panic("ureadc: non-positive iovcnt");
91447636 319 if (uio_iov_len(uio) <= 0) {
1c79356b 320 uio->uio_iovcnt--;
91447636 321 uio_next_iov(uio);
1c79356b
A
322 goto again;
323 }
324 switch (uio->uio_segflg) {
325
91447636 326 case UIO_USERSPACE32:
1c79356b 327 case UIO_USERSPACE:
91447636 328 if (subyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), c) < 0)
1c79356b
A
329 return (EFAULT);
330 break;
331
91447636
A
332 case UIO_USERSPACE64:
333 if (subyte((user_addr_t)uio->uio_iovs.iov64p->iov_base, c) < 0)
334 return (EFAULT);
335 break;
336
337 case UIO_SYSSPACE32:
1c79356b 338 case UIO_SYSSPACE:
91447636 339 *((caddr_t)uio->uio_iovs.iov32p->iov_base) = c;
1c79356b
A
340 break;
341
91447636 342 case UIO_USERISPACE32:
1c79356b 343 case UIO_USERISPACE:
91447636 344 if (suibyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), c) < 0)
1c79356b
A
345 return (EFAULT);
346 break;
91447636
A
347
348 default:
349 break;
1c79356b 350 }
91447636
A
351 uio_iov_base_add(uio, 1);
352 uio_iov_len_add(uio, -1);
353 uio_setresid(uio, (uio_resid(uio) - 1));
1c79356b
A
354 uio->uio_offset++;
355 return (0);
356}
357
358#if defined(vax) || defined(ppc)
359/* unused except by ct.c, other oddities XXX */
360/*
361 * Get next character written in by user from uio.
362 */
91447636 363int
1c79356b 364uwritec(uio)
91447636 365 uio_t uio;
1c79356b 366{
91447636 367 register int c = 0;
1c79356b 368
91447636 369 if (uio_resid(uio) <= 0)
1c79356b
A
370 return (-1);
371again:
372 if (uio->uio_iovcnt <= 0)
373 panic("uwritec: non-positive iovcnt");
91447636
A
374
375 if (uio_iov_len(uio) == 0) {
376 uio_next_iov(uio);
1c79356b
A
377 if (--uio->uio_iovcnt == 0)
378 return (-1);
379 goto again;
380 }
381 switch (uio->uio_segflg) {
382
91447636 383 case UIO_USERSPACE32:
1c79356b 384 case UIO_USERSPACE:
91447636 385 c = fubyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base));
1c79356b
A
386 break;
387
91447636
A
388 case UIO_USERSPACE64:
389 c = fubyte((user_addr_t)uio->uio_iovs.iov64p->iov_base);
390 break;
391
392 case UIO_SYSSPACE32:
1c79356b 393 case UIO_SYSSPACE:
91447636 394 c = *((caddr_t)uio->uio_iovs.iov32p->iov_base) & 0377;
1c79356b
A
395 break;
396
91447636 397 case UIO_USERISPACE32:
1c79356b 398 case UIO_USERISPACE:
91447636 399 c = fuibyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base));
1c79356b
A
400 break;
401
402 default:
403 c = 0; /* avoid uninitialized variable warning */
404 panic("uwritec: bogus uio_segflg");
405 break;
406 }
407 if (c < 0)
408 return (-1);
91447636
A
409 uio_iov_base_add(uio, 1);
410 uio_iov_len_add(uio, -1);
411 uio_setresid(uio, (uio_resid(uio) - 1));
1c79356b
A
412 uio->uio_offset++;
413 return (c);
414}
415#endif /* vax || ppc */
416
417/*
418 * General routine to allocate a hash table.
419 */
420void *
421hashinit(elements, type, hashmask)
422 int elements, type;
423 u_long *hashmask;
424{
425 long hashsize;
426 LIST_HEAD(generic, generic) *hashtbl;
427 int i;
428
429 if (elements <= 0)
430 panic("hashinit: bad cnt");
431 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
432 continue;
433 hashsize >>= 1;
434 MALLOC(hashtbl, struct generic *,
91447636
A
435 (u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK|M_ZERO);
436 if (hashtbl != NULL) {
437 for (i = 0; i < hashsize; i++)
438 LIST_INIT(&hashtbl[i]);
439 *hashmask = hashsize - 1;
440 }
1c79356b
A
441 return (hashtbl);
442}
91447636
A
443
444/*
445 * uio_resid - return the residual IO value for the given uio_t
446 */
447user_ssize_t uio_resid( uio_t a_uio )
448{
449#if DEBUG
450 if (a_uio == NULL) {
451 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
452 }
453/* if (IS_VALID_UIO_SEGFLG(a_uio->uio_segflg) == 0) { */
454/* panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); */
455/* } */
456#endif /* DEBUG */
457
458 /* return 0 if there are no active iovecs */
459 if (a_uio == NULL) {
460 return( 0 );
461 }
462
463 if (UIO_IS_64_BIT_SPACE(a_uio)) {
464#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
465 return( (user_ssize_t)a_uio->uio_resid );
466#else
467 return( a_uio->uio_resid_64 );
468#endif
469 }
470 return( (user_ssize_t)a_uio->uio_resid );
471}
472
473/*
474 * uio_setresid - set the residual IO value for the given uio_t
475 */
476void uio_setresid( uio_t a_uio, user_ssize_t a_value )
477{
478#if DEBUG
479 if (a_uio == NULL) {
480 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
481 }
482/* if (IS_VALID_UIO_SEGFLG(a_uio->uio_segflg) == 0) { */
483/* panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); */
484/* } */
485#endif /* DEBUG */
486
487 if (a_uio == NULL) {
488 return;
489 }
490
491 if (UIO_IS_64_BIT_SPACE(a_uio)) {
492#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
493 a_uio->uio_resid = (int)a_value;
494#else
495 a_uio->uio_resid_64 = a_value;
496#endif
497 }
498 else {
499 a_uio->uio_resid = (int)a_value;
500 }
501 return;
502}
503
504#if 0 // obsolete
505/*
506 * uio_proc_t - return the proc_t for the given uio_t
507 * WARNING - This call is going away. Find another way to get the proc_t!!
508 */
509__private_extern__ proc_t uio_proc_t( uio_t a_uio )
510{
511#if LP64_DEBUG
512 if (a_uio == NULL) {
513 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
514 }
515#endif /* LP64_DEBUG */
516
517 /* return 0 if there are no active iovecs */
518 if (a_uio == NULL) {
519 return( NULL );
520 }
521 return( a_uio->uio_procp );
522}
523
524/*
525 * uio_setproc_t - set the residual IO value for the given uio_t
526 * WARNING - This call is going away.
527 */
528__private_extern__ void uio_setproc_t( uio_t a_uio, proc_t a_proc_t )
529{
530 if (a_uio == NULL) {
531#if LP64_DEBUG
532 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
533#endif /* LP64_DEBUG */
534 return;
535 }
536
537 a_uio->uio_procp = a_proc_t;
538 return;
539}
540#endif // obsolete
541
542/*
543 * uio_curriovbase - return the base address of the current iovec associated
544 * with the given uio_t. May return 0.
545 */
546user_addr_t uio_curriovbase( uio_t a_uio )
547{
548#if LP64_DEBUG
549 if (a_uio == NULL) {
550 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
551 }
552#endif /* LP64_DEBUG */
553
554 if (a_uio == NULL || a_uio->uio_iovcnt < 1) {
555 return(0);
556 }
557
558 if (UIO_IS_64_BIT_SPACE(a_uio)) {
559 return(a_uio->uio_iovs.uiovp->iov_base);
560 }
561 return((user_addr_t)((uintptr_t)a_uio->uio_iovs.kiovp->iov_base));
562
563}
564
565/*
566 * uio_curriovlen - return the length value of the current iovec associated
567 * with the given uio_t.
568 */
569user_size_t uio_curriovlen( uio_t a_uio )
570{
571#if LP64_DEBUG
572 if (a_uio == NULL) {
573 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
574 }
575#endif /* LP64_DEBUG */
576
577 if (a_uio == NULL || a_uio->uio_iovcnt < 1) {
578 return(0);
579 }
580
581 if (UIO_IS_64_BIT_SPACE(a_uio)) {
582 return(a_uio->uio_iovs.uiovp->iov_len);
583 }
584 return((user_size_t)a_uio->uio_iovs.kiovp->iov_len);
585}
586
587/*
588 * uio_setcurriovlen - set the length value of the current iovec associated
589 * with the given uio_t.
590 */
591__private_extern__ void uio_setcurriovlen( uio_t a_uio, user_size_t a_value )
592{
593#if LP64_DEBUG
594 if (a_uio == NULL) {
595 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
596 }
597#endif /* LP64_DEBUG */
598
599 if (a_uio == NULL) {
600 return;
601 }
602
603 if (UIO_IS_64_BIT_SPACE(a_uio)) {
604 a_uio->uio_iovs.uiovp->iov_len = a_value;
605 }
606 else {
607#if LP64_DEBUG
608 if (a_value > 0xFFFFFFFFull) {
609 panic("%s :%d - invalid a_value\n", __FILE__, __LINE__);
610 }
611#endif /* LP64_DEBUG */
612 a_uio->uio_iovs.kiovp->iov_len = (size_t)a_value;
613 }
614 return;
615}
616
617/*
618 * uio_iovcnt - return count of active iovecs for the given uio_t
619 */
620int uio_iovcnt( uio_t a_uio )
621{
622#if LP64_DEBUG
623 if (a_uio == NULL) {
624 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
625 }
626#endif /* LP64_DEBUG */
627
628 if (a_uio == NULL) {
629 return(0);
630 }
631
632 return( a_uio->uio_iovcnt );
633}
634
635/*
636 * uio_offset - return the current offset value for the given uio_t
637 */
638off_t uio_offset( uio_t a_uio )
639{
640#if LP64_DEBUG
641 if (a_uio == NULL) {
642 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
643 }
644#endif /* LP64_DEBUG */
645
646 if (a_uio == NULL) {
647 return(0);
648 }
649 return( a_uio->uio_offset );
650}
651
652/*
653 * uio_setoffset - set the current offset value for the given uio_t
654 */
655void uio_setoffset( uio_t a_uio, off_t a_offset )
656{
657#if LP64_DEBUG
658 if (a_uio == NULL) {
659 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
660 }
661#endif /* LP64_DEBUG */
662
663 if (a_uio == NULL) {
664 return;
665 }
666 a_uio->uio_offset = a_offset;
667 return;
668}
669
670/*
671 * uio_rw - return the read / write flag for the given uio_t
672 */
673int uio_rw( uio_t a_uio )
674{
675#if LP64_DEBUG
676 if (a_uio == NULL) {
677 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
678 }
679#endif /* LP64_DEBUG */
680
681 if (a_uio == NULL) {
682 return(-1);
683 }
684 return( a_uio->uio_rw );
685}
686
687/*
688 * uio_setrw - set the read / write flag for the given uio_t
689 */
690void uio_setrw( uio_t a_uio, int a_value )
691{
692 if (a_uio == NULL) {
693#if LP64_DEBUG
694 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
695#endif /* LP64_DEBUG */
696 return;
697 }
698
699#if LP64_DEBUG
700 if (!(a_value == UIO_READ || a_value == UIO_WRITE)) {
701 panic("%s :%d - invalid a_value\n", __FILE__, __LINE__);
702 }
703#endif /* LP64_DEBUG */
704
705 if (a_value == UIO_READ || a_value == UIO_WRITE) {
706 a_uio->uio_rw = a_value;
707 }
708 return;
709}
710
711/*
712 * uio_isuserspace - return non zero value if the address space
713 * flag is for a user address space (could be 32 or 64 bit).
714 */
715int uio_isuserspace( uio_t a_uio )
716{
717 if (a_uio == NULL) {
718#if LP64_DEBUG
719 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
720#endif /* LP64_DEBUG */
721 return(0);
722 }
723
724 if (UIO_SEG_IS_USER_SPACE(a_uio->uio_segflg)) {
725 return( 1 );
726 }
727 return( 0 );
728}
729
730
731/*
732 * uio_create - create an uio_t.
733 * Space is allocated to hold up to a_iovcount number of iovecs. The uio_t
734 * is not fully initialized until all iovecs are added using uio_addiov calls.
735 * a_iovcount is the maximum number of iovecs you may add.
736 */
737uio_t uio_create( int a_iovcount, /* number of iovecs */
738 off_t a_offset, /* current offset */
739 int a_spacetype, /* type of address space */
740 int a_iodirection ) /* read or write flag */
741{
742 void * my_buf_p;
743 int my_size;
744 uio_t my_uio;
745
746 my_size = sizeof(struct uio) + (sizeof(struct user_iovec) * a_iovcount);
747 my_buf_p = kalloc(my_size);
748 my_uio = uio_createwithbuffer( a_iovcount,
749 a_offset,
750 a_spacetype,
751 a_iodirection,
752 my_buf_p,
753 my_size );
754 if (my_uio != 0) {
755 /* leave a note that we allocated this uio_t */
756 my_uio->uio_flags |= UIO_FLAGS_WE_ALLOCED;
757#if DEBUG
758 hw_atomic_add(&uio_t_count, 1);
759#endif
760 }
761
762 return( my_uio );
763}
764
765
766/*
767 * uio_createwithbuffer - create an uio_t.
768 * Create a uio_t using the given buffer. The uio_t
769 * is not fully initialized until all iovecs are added using uio_addiov calls.
770 * a_iovcount is the maximum number of iovecs you may add.
771 * This call may fail if the given buffer is not large enough.
772 */
773__private_extern__ uio_t
774 uio_createwithbuffer( int a_iovcount, /* number of iovecs */
775 off_t a_offset, /* current offset */
776 int a_spacetype, /* type of address space */
777 int a_iodirection, /* read or write flag */
778 void *a_buf_p, /* pointer to a uio_t buffer */
779 int a_buffer_size ) /* size of uio_t buffer */
780{
781 uio_t my_uio = (uio_t) a_buf_p;
782 int my_size;
783
784 my_size = sizeof(struct uio) + (sizeof(struct user_iovec) * a_iovcount);
785 if (a_buffer_size < my_size) {
786#if DEBUG
787 panic("%s :%d - a_buffer_size is too small\n", __FILE__, __LINE__);
788#endif /* DEBUG */
789 return( NULL );
790 }
791 my_size = a_buffer_size;
792
793#if DEBUG
794 if (my_uio == 0) {
795 panic("%s :%d - could not allocate uio_t\n", __FILE__, __LINE__);
796 }
797 if (!IS_VALID_UIO_SEGFLG(a_spacetype)) {
798 panic("%s :%d - invalid address space type\n", __FILE__, __LINE__);
799 }
800 if (!(a_iodirection == UIO_READ || a_iodirection == UIO_WRITE)) {
801 panic("%s :%d - invalid IO direction flag\n", __FILE__, __LINE__);
802 }
803 if (a_iovcount > UIO_MAXIOV) {
804 panic("%s :%d - invalid a_iovcount\n", __FILE__, __LINE__);
805 }
806#endif /* DEBUG */
807
808 bzero(my_uio, my_size);
809 my_uio->uio_size = my_size;
810
811 /* we use uio_segflg to indicate if the uio_t is the new format or */
812 /* old (pre LP64 support) legacy format */
813 switch (a_spacetype) {
814 case UIO_USERSPACE:
815 my_uio->uio_segflg = UIO_USERSPACE32;
816 case UIO_SYSSPACE:
817 my_uio->uio_segflg = UIO_SYSSPACE32;
818 case UIO_PHYS_USERSPACE:
819 my_uio->uio_segflg = UIO_PHYS_USERSPACE32;
820 case UIO_PHYS_SYSSPACE:
821 my_uio->uio_segflg = UIO_PHYS_SYSSPACE32;
822 default:
823 my_uio->uio_segflg = a_spacetype;
824 break;
825 }
826
827 if (a_iovcount > 0) {
828 my_uio->uio_iovs.uiovp = (struct user_iovec *)
829 (((uint8_t *)my_uio) + sizeof(struct uio));
830 }
831 else {
832 my_uio->uio_iovs.uiovp = NULL;
833 }
834
835 my_uio->uio_max_iovs = a_iovcount;
836 my_uio->uio_offset = a_offset;
837 my_uio->uio_rw = a_iodirection;
838 my_uio->uio_flags = UIO_FLAGS_INITED;
839
840 return( my_uio );
841}
842
843/*
844 * uio_spacetype - return the address space type for the given uio_t
845 */
846int uio_spacetype( uio_t a_uio )
847{
848 if (a_uio == NULL) {
849#if LP64_DEBUG
850 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
851#endif /* LP64_DEBUG */
852 return(-1);
853 }
854
855 return( a_uio->uio_segflg );
856}
857
858/*
859 * uio_iovsaddr - get the address of the iovec array for the given uio_t.
860 * This returns the location of the iovecs within the uio.
861 * NOTE - for compatibility mode we just return the current value in uio_iovs
862 * which will increase as the IO is completed and is NOT embedded within the
863 * uio, it is a seperate array of one or more iovecs.
864 */
865struct user_iovec * uio_iovsaddr( uio_t a_uio )
866{
867 struct user_iovec * my_addr;
868
869 if (a_uio == NULL) {
870 return(NULL);
871 }
872
873 if (a_uio->uio_segflg == UIO_USERSPACE || a_uio->uio_segflg == UIO_SYSSPACE) {
874 /* we need this for compatibility mode. */
875 my_addr = (struct user_iovec *) a_uio->uio_iovs.iovp;
876 }
877 else {
878 my_addr = (struct user_iovec *) (((uint8_t *)a_uio) + sizeof(struct uio));
879 }
880 return(my_addr);
881}
882
883/*
884 * uio_reset - reset an uio_t.
885 * Reset the given uio_t to initial values. The uio_t is not fully initialized
886 * until all iovecs are added using uio_addiov calls.
887 * The a_iovcount value passed in the uio_create is the maximum number of
888 * iovecs you may add.
889 */
890void uio_reset( uio_t a_uio,
891 off_t a_offset, /* current offset */
892 int a_spacetype, /* type of address space */
893 int a_iodirection ) /* read or write flag */
894{
895 vm_size_t my_size;
896 int my_max_iovs;
897 u_int32_t my_old_flags;
898
899#if LP64_DEBUG
900 if (a_uio == NULL) {
901 panic("%s :%d - could not allocate uio_t\n", __FILE__, __LINE__);
902 }
903 if (!IS_VALID_UIO_SEGFLG(a_spacetype)) {
904 panic("%s :%d - invalid address space type\n", __FILE__, __LINE__);
905 }
906 if (!(a_iodirection == UIO_READ || a_iodirection == UIO_WRITE)) {
907 panic("%s :%d - invalid IO direction flag\n", __FILE__, __LINE__);
908 }
909#endif /* LP64_DEBUG */
910
911 if (a_uio == NULL) {
912 return;
913 }
914
915 my_size = a_uio->uio_size;
916 my_old_flags = a_uio->uio_flags;
917 my_max_iovs = a_uio->uio_max_iovs;
918 bzero(a_uio, my_size);
919 a_uio->uio_size = my_size;
920 a_uio->uio_segflg = a_spacetype;
921 if (my_max_iovs > 0) {
922 a_uio->uio_iovs.uiovp = (struct user_iovec *)
923 (((uint8_t *)a_uio) + sizeof(struct uio));
924 }
925 else {
926 a_uio->uio_iovs.uiovp = NULL;
927 }
928 a_uio->uio_max_iovs = my_max_iovs;
929 a_uio->uio_offset = a_offset;
930 a_uio->uio_rw = a_iodirection;
931 a_uio->uio_flags = my_old_flags;
932
933 return;
934}
935
936/*
937 * uio_free - free a uio_t allocated via uio_init. this also frees all
938 * associated iovecs.
939 */
940void uio_free( uio_t a_uio )
941{
942#if DEBUG
943 if (a_uio == NULL) {
944 panic("%s :%d - passing NULL uio_t\n", __FILE__, __LINE__);
945 }
946#endif /* LP64_DEBUG */
947
948 if (a_uio != NULL && (a_uio->uio_flags & UIO_FLAGS_WE_ALLOCED) != 0) {
949#if DEBUG
950 if ((int)(hw_atomic_sub(&uio_t_count, 1)) < 0) {
951 panic("%s :%d - uio_t_count has gone negative\n", __FILE__, __LINE__);
952 }
953#endif
954 kfree(a_uio, a_uio->uio_size);
955 }
956
957
958}
959
960/*
961 * uio_addiov - add an iovec to the given uio_t. You may call this up to
962 * the a_iovcount number that was passed to uio_create. This call will
963 * increment the residual IO count as iovecs are added to the uio_t.
964 * returns 0 if add was successful else non zero.
965 */
966int uio_addiov( uio_t a_uio, user_addr_t a_baseaddr, user_size_t a_length )
967{
968 int i;
969
970 if (a_uio == NULL) {
971#if DEBUG
972 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
973#endif /* LP64_DEBUG */
974 return(-1);
975 }
976
977 if (UIO_IS_64_BIT_SPACE(a_uio)) {
978 for ( i = 0; i < a_uio->uio_max_iovs; i++ ) {
979 if (a_uio->uio_iovs.uiovp[i].iov_len == 0 && a_uio->uio_iovs.uiovp[i].iov_base == 0) {
980 a_uio->uio_iovs.uiovp[i].iov_len = a_length;
981 a_uio->uio_iovs.uiovp[i].iov_base = a_baseaddr;
982 a_uio->uio_iovcnt++;
983#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
984 a_uio->uio_resid += a_length;
985#else
986 a_uio->uio_resid_64 += a_length;
987#endif
988 return( 0 );
989 }
990 }
991 }
992 else {
993 for ( i = 0; i < a_uio->uio_max_iovs; i++ ) {
994 if (a_uio->uio_iovs.kiovp[i].iov_len == 0 && a_uio->uio_iovs.kiovp[i].iov_base == 0) {
995 a_uio->uio_iovs.kiovp[i].iov_len = (u_int32_t)a_length;
996 a_uio->uio_iovs.kiovp[i].iov_base = (u_int32_t)((uintptr_t)a_baseaddr);
997 a_uio->uio_iovcnt++;
998 a_uio->uio_resid += a_length;
999 return( 0 );
1000 }
1001 }
1002 }
1003
1004 return( -1 );
1005}
1006
1007/*
1008 * uio_getiov - get iovec data associated with the given uio_t. Use
1009 * a_index to iterate over each iovec (0 to (uio_iovcnt(uio_t) - 1)).
1010 * a_baseaddr_p and a_length_p may be NULL.
1011 * returns -1 when a_index is >= uio_t.uio_iovcnt or invalid uio_t.
1012 * returns 0 when data is returned.
1013 */
1014int uio_getiov( uio_t a_uio,
1015 int a_index,
1016 user_addr_t * a_baseaddr_p,
1017 user_size_t * a_length_p )
1018{
1019 if (a_uio == NULL) {
1020#if DEBUG
1021 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
1022#endif /* DEBUG */
1023 return(-1);
1024 }
1025 if ( a_index < 0 || a_index >= a_uio->uio_iovcnt) {
1026 return(-1);
1027 }
1028
1029 if (UIO_IS_64_BIT_SPACE(a_uio)) {
1030 if (a_baseaddr_p != NULL) {
1031 *a_baseaddr_p = a_uio->uio_iovs.uiovp[a_index].iov_base;
1032 }
1033 if (a_length_p != NULL) {
1034 *a_length_p = a_uio->uio_iovs.uiovp[a_index].iov_len;
1035 }
1036 }
1037 else {
1038 if (a_baseaddr_p != NULL) {
1039 *a_baseaddr_p = a_uio->uio_iovs.kiovp[a_index].iov_base;
1040 }
1041 if (a_length_p != NULL) {
1042 *a_length_p = a_uio->uio_iovs.kiovp[a_index].iov_len;
1043 }
1044 }
1045
1046 return( 0 );
1047}
1048
1049/*
1050 * uio_calculateresid - runs through all iovecs associated with this
1051 * uio_t and calculates (and sets) the residual IO count.
1052 */
1053__private_extern__ void uio_calculateresid( uio_t a_uio )
1054{
1055 int i;
1056
1057 if (a_uio == NULL) {
1058#if LP64_DEBUG
1059 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
1060#endif /* LP64_DEBUG */
1061 return;
1062 }
1063
743b1565 1064 a_uio->uio_iovcnt = a_uio->uio_max_iovs;
91447636
A
1065 if (UIO_IS_64_BIT_SPACE(a_uio)) {
1066#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
1067 a_uio->uio_resid = 0;
1068#else
1069 a_uio->uio_resid_64 = 0;
1070#endif
1071 for ( i = 0; i < a_uio->uio_max_iovs; i++ ) {
1072 if (a_uio->uio_iovs.uiovp[i].iov_len != 0 && a_uio->uio_iovs.uiovp[i].iov_base != 0) {
91447636
A
1073#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
1074 a_uio->uio_resid += a_uio->uio_iovs.uiovp[i].iov_len;
1075#else
1076 a_uio->uio_resid_64 += a_uio->uio_iovs.uiovp[i].iov_len;
1077#endif
1078 }
1079 }
743b1565
A
1080
1081 /* position to first non zero length iovec (4235922) */
1082 while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.uiovp->iov_len == 0) {
1083 a_uio->uio_iovcnt--;
1084 if (a_uio->uio_iovcnt > 0) {
1085 a_uio->uio_iovs.uiovp++;
1086 }
1087 }
91447636
A
1088 }
1089 else {
1090 a_uio->uio_resid = 0;
1091 for ( i = 0; i < a_uio->uio_max_iovs; i++ ) {
1092 if (a_uio->uio_iovs.kiovp[i].iov_len != 0 && a_uio->uio_iovs.kiovp[i].iov_base != 0) {
91447636
A
1093 a_uio->uio_resid += a_uio->uio_iovs.kiovp[i].iov_len;
1094 }
1095 }
743b1565
A
1096
1097 /* position to first non zero length iovec (4235922) */
1098 while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.kiovp->iov_len == 0) {
1099 a_uio->uio_iovcnt--;
1100 if (a_uio->uio_iovcnt > 0) {
1101 a_uio->uio_iovs.kiovp++;
1102 }
1103 }
91447636 1104 }
743b1565 1105
91447636
A
1106 return;
1107}
1108
1109/*
1110 * uio_update - update the given uio_t for a_count of completed IO.
1111 * This call decrements the current iovec length and residual IO value
1112 * and increments the current iovec base address and offset value.
1113 * If the current iovec length is 0 then advance to the next
1114 * iovec (if any).
cc9f6e38
A
1115 * If the a_count passed in is 0, than only do the advancement
1116 * over any 0 length iovec's.
91447636
A
1117 */
1118void uio_update( uio_t a_uio, user_size_t a_count )
1119{
1120#if LP64_DEBUG
1121 if (a_uio == NULL) {
1122 panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__);
1123 }
1124 if (UIO_IS_32_BIT_SPACE(a_uio) && a_count > 0xFFFFFFFFull) {
1125 panic("%s :%d - invalid count value \n", __FILE__, __LINE__);
1126 }
1127#endif /* LP64_DEBUG */
1128
1129 if (a_uio == NULL || a_uio->uio_iovcnt < 1) {
1130 return;
1131 }
1132
1133 if (UIO_IS_64_BIT_SPACE(a_uio)) {
cc9f6e38
A
1134 /*
1135 * if a_count == 0, then we are asking to skip over
1136 * any empty iovs
1137 */
1138 if (a_count) {
1139 if (a_count > a_uio->uio_iovs.uiovp->iov_len) {
1140 a_uio->uio_iovs.uiovp->iov_base += a_uio->uio_iovs.uiovp->iov_len;
1141 a_uio->uio_iovs.uiovp->iov_len = 0;
1142 }
1143 else {
1144 a_uio->uio_iovs.uiovp->iov_base += a_count;
1145 a_uio->uio_iovs.uiovp->iov_len -= a_count;
1146 }
91447636 1147#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI
cc9f6e38
A
1148 if (a_uio->uio_resid < 0) {
1149 a_uio->uio_resid = 0;
1150 }
1151 if (a_count > (user_size_t)a_uio->uio_resid) {
1152 a_uio->uio_offset += a_uio->uio_resid;
1153 a_uio->uio_resid = 0;
1154 }
1155 else {
1156 a_uio->uio_offset += a_count;
1157 a_uio->uio_resid -= a_count;
1158 }
91447636 1159#else
cc9f6e38
A
1160 if (a_uio->uio_resid_64 < 0) {
1161 a_uio->uio_resid_64 = 0;
1162 }
1163 if (a_count > (user_size_t)a_uio->uio_resid_64) {
1164 a_uio->uio_offset += a_uio->uio_resid_64;
1165 a_uio->uio_resid_64 = 0;
1166 }
1167 else {
1168 a_uio->uio_offset += a_count;
1169 a_uio->uio_resid_64 -= a_count;
1170 }
91447636 1171#endif // LP64todo
cc9f6e38
A
1172 }
1173 /*
1174 * advance to next iovec if current one is totally consumed
1175 */
1176 while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.uiovp->iov_len == 0) {
91447636
A
1177 a_uio->uio_iovcnt--;
1178 if (a_uio->uio_iovcnt > 0) {
1179 a_uio->uio_iovs.uiovp++;
1180 }
1181 }
1182 }
1183 else {
cc9f6e38
A
1184 /*
1185 * if a_count == 0, then we are asking to skip over
1186 * any empty iovs
1187 */
1188 if (a_count) {
1189 if (a_count > a_uio->uio_iovs.kiovp->iov_len) {
1190 a_uio->uio_iovs.kiovp->iov_base += a_uio->uio_iovs.kiovp->iov_len;
1191 a_uio->uio_iovs.kiovp->iov_len = 0;
1192 }
1193 else {
1194 a_uio->uio_iovs.kiovp->iov_base += a_count;
1195 a_uio->uio_iovs.kiovp->iov_len -= a_count;
1196 }
1197 if (a_uio->uio_resid < 0) {
1198 a_uio->uio_resid = 0;
1199 }
1200 if (a_count > (user_size_t)a_uio->uio_resid) {
1201 a_uio->uio_offset += a_uio->uio_resid;
1202 a_uio->uio_resid = 0;
1203 }
1204 else {
1205 a_uio->uio_offset += a_count;
1206 a_uio->uio_resid -= a_count;
1207 }
91447636 1208 }
cc9f6e38
A
1209 /*
1210 * advance to next iovec if current one is totally consumed
1211 */
1212 while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.kiovp->iov_len == 0) {
91447636
A
1213 a_uio->uio_iovcnt--;
1214 if (a_uio->uio_iovcnt > 0) {
1215 a_uio->uio_iovs.kiovp++;
1216 }
1217 }
1218 }
1219 return;
1220}
1221
1222
1223/*
1224 * uio_duplicate - allocate a new uio and make a copy of the given uio_t.
1225 * may return NULL.
1226 */
1227uio_t uio_duplicate( uio_t a_uio )
1228{
1229 uio_t my_uio;
1230 int i;
1231
1232 if (a_uio == NULL) {
1233 return(NULL);
1234 }
1235
1236 my_uio = (uio_t) kalloc(a_uio->uio_size);
1237 if (my_uio == 0) {
1238 panic("%s :%d - allocation failed\n", __FILE__, __LINE__);
1239 }
1240
1241 bcopy((void *)a_uio, (void *)my_uio, a_uio->uio_size);
1242 /* need to set our iovec pointer to point to first active iovec */
1243 if (my_uio->uio_max_iovs > 0) {
1244 my_uio->uio_iovs.uiovp = (struct user_iovec *)
1245 (((uint8_t *)my_uio) + sizeof(struct uio));
1246
1247 /* advance to first nonzero iovec */
1248 if (my_uio->uio_iovcnt > 0) {
1249 for ( i = 0; i < my_uio->uio_max_iovs; i++ ) {
1250 if (UIO_IS_64_BIT_SPACE(a_uio)) {
1251 if (my_uio->uio_iovs.uiovp->iov_len != 0) {
1252 break;
1253 }
1254 my_uio->uio_iovs.uiovp++;
1255 }
1256 else {
1257 if (my_uio->uio_iovs.kiovp->iov_len != 0) {
1258 break;
1259 }
1260 my_uio->uio_iovs.kiovp++;
1261 }
1262 }
1263 }
1264 }
1265
1266 return(my_uio);
1267}
1268