X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/ff6e181ae92fc6f1e89841290f461d1f2f9badd9..13f56ec4e58bf8687e2a68032c093c0213dd519b:/bsd/kern/kern_subr.c diff --git a/bsd/kern/kern_subr.c b/bsd/kern/kern_subr.c index 0a40c0a19..cc05a7db7 100644 --- a/bsd/kern/kern_subr.c +++ b/bsd/kern/kern_subr.c @@ -1,14 +1,19 @@ /* - * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER @@ -18,7 +23,7 @@ * Please see the License for the specific language governing rights and * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ /* @@ -79,27 +84,52 @@ #if DEBUG #include -static int uio_t_count = 0; +static uint32_t uio_t_count = 0; #endif /* DEBUG */ +#define IS_VALID_UIO_SEGFLG(segflg) \ + ( (segflg) == UIO_USERSPACE || \ + (segflg) == UIO_SYSSPACE || \ + (segflg) == UIO_USERSPACE32 || \ + (segflg) == UIO_USERSPACE64 || \ + (segflg) == UIO_SYSSPACE32 || \ + (segflg) == UIO_USERISPACE || \ + (segflg) == UIO_PHYS_USERSPACE || \ + (segflg) == UIO_PHYS_SYSSPACE || \ + (segflg) == UIO_USERISPACE32 || \ + (segflg) == UIO_PHYS_USERSPACE32 || \ + (segflg) == UIO_USERISPACE64 || \ + (segflg) == UIO_PHYS_USERSPACE64 ) +/* + * Returns: 0 Success + * uiomove64:EFAULT + * + * Notes: The first argument should be a caddr_t, but const poisoning + * for typedef'ed types doesn't work in gcc. + */ int -uiomove(cp, n, uio) - register caddr_t cp; - register int n; - register uio_t uio; +uiomove(const char * cp, int n, uio_t uio) { - return uiomove64((addr64_t)((unsigned int)cp), n, uio); + return uiomove64((const addr64_t)(uintptr_t)cp, n, uio); } - // LP64todo - fix this! 'n' should be int64_t? +/* + * Returns: 0 Success + * EFAULT + * copyout:EFAULT + * copyin:EFAULT + * copywithin:EFAULT + * copypv:EFAULT + */ int -uiomove64(addr64_t cp, int n, register struct uio *uio) +uiomove64(const addr64_t c_cp, int n, struct uio *uio) { + addr64_t cp = c_cp; #if LP64KERN - register uint64_t acnt; + uint64_t acnt; #else - register u_int acnt; + u_int acnt; #endif int error = 0; @@ -115,10 +145,9 @@ uiomove64(addr64_t cp, int n, register struct uio *uio) #endif /* LP64_DEBUG */ while (n > 0 && uio_resid(uio)) { - acnt = uio_iov_len(uio); + uio_update(uio, 0); + acnt = uio_curriovlen(uio); if (acnt == 0) { - uio_next_iov(uio); - uio->uio_iovcnt--; continue; } if (n > 0 && acnt > (uint64_t)n) @@ -128,54 +157,30 @@ uiomove64(addr64_t cp, int n, register struct uio *uio) case UIO_USERSPACE64: case UIO_USERISPACE64: - // LP64 - 3rd argument in debug code is 64 bit, expected to be 32 bit - if (uio->uio_rw == UIO_READ) - { - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START, - (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 0,0); - - error = copyout( CAST_DOWN(caddr_t, cp), uio->uio_iovs.iov64p->iov_base, acnt ); - - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END, - (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 0,0); - } - else - { - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START, - (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 0,0); - - error = copyin(uio->uio_iovs.iov64p->iov_base, CAST_DOWN(caddr_t, cp), acnt); - - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END, - (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 0,0); - } - if (error) - return (error); - break; - case UIO_USERSPACE32: case UIO_USERISPACE32: case UIO_USERSPACE: case UIO_USERISPACE: + // LP64 - 3rd argument in debug code is 64 bit, expected to be 32 bit if (uio->uio_rw == UIO_READ) { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 0,0); + (int)cp, (uintptr_t)uio->uio_iovs.uiovp->iov_base, acnt, 0,0); - error = copyout( CAST_DOWN(caddr_t, cp), CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), acnt ); + error = copyout( CAST_DOWN(caddr_t, cp), uio->uio_iovs.uiovp->iov_base, acnt ); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 0,0); + (int)cp, (uintptr_t)uio->uio_iovs.uiovp->iov_base, acnt, 0,0); } else { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 0,0); + (uintptr_t)uio->uio_iovs.uiovp->iov_base, (int)cp, acnt, 0,0); - error = copyin(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), CAST_DOWN(caddr_t, cp), acnt); + error = copyin(uio->uio_iovs.uiovp->iov_base, CAST_DOWN(caddr_t, cp), acnt); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 0,0); + (uintptr_t)uio->uio_iovs.uiovp->iov_base, (int)cp, acnt, 0,0); } if (error) return (error); @@ -184,97 +189,68 @@ uiomove64(addr64_t cp, int n, register struct uio *uio) case UIO_SYSSPACE32: case UIO_SYSSPACE: if (uio->uio_rw == UIO_READ) - error = copywithin(CAST_DOWN(caddr_t, cp), (caddr_t)uio->uio_iovs.iov32p->iov_base, + error = copywithin(CAST_DOWN(caddr_t, cp), CAST_DOWN(caddr_t, uio->uio_iovs.kiovp->iov_base), acnt); else - error = copywithin((caddr_t)uio->uio_iovs.iov32p->iov_base, CAST_DOWN(caddr_t, cp), + error = copywithin(CAST_DOWN(caddr_t, uio->uio_iovs.kiovp->iov_base), CAST_DOWN(caddr_t, cp), acnt); break; case UIO_PHYS_USERSPACE64: - if (uio->uio_rw == UIO_READ) - { - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START, - (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 1,0); - - error = copypv((addr64_t)cp, uio->uio_iovs.iov64p->iov_base, acnt, cppvPsrc | cppvNoRefSrc); - if (error) /* Copy physical to virtual */ - error = EFAULT; - - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END, - (int)cp, (int)uio->uio_iovs.iov64p->iov_base, acnt, 1,0); - } - else - { - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START, - (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 1,0); - - error = copypv(uio->uio_iovs.iov64p->iov_base, (addr64_t)cp, acnt, cppvPsnk | cppvNoRefSrc | cppvNoModSnk); - if (error) /* Copy virtual to physical */ - error = EFAULT; - - KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END, - (int)uio->uio_iovs.iov64p->iov_base, (int)cp, acnt, 1,0); - } - if (error) - return (error); - break; - case UIO_PHYS_USERSPACE32: case UIO_PHYS_USERSPACE: if (uio->uio_rw == UIO_READ) { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 1,0); + (int)cp, (uintptr_t)uio->uio_iovs.uiovp->iov_base, acnt, 1,0); - error = copypv((addr64_t)cp, (addr64_t)uio->uio_iovs.iov32p->iov_base, acnt, cppvPsrc | cppvNoRefSrc); + error = copypv((addr64_t)cp, uio->uio_iovs.uiovp->iov_base, acnt, cppvPsrc | cppvNoRefSrc); if (error) /* Copy physical to virtual */ error = EFAULT; KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 1,0); + (int)cp, (uintptr_t)uio->uio_iovs.uiovp->iov_base, acnt, 1,0); } else { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 1,0); + (uintptr_t)uio->uio_iovs.uiovp->iov_base, (int)cp, acnt, 1,0); - error = copypv((addr64_t)uio->uio_iovs.iov32p->iov_base, (addr64_t)cp, acnt, cppvPsnk | cppvNoRefSrc | cppvNoModSnk); + error = copypv(uio->uio_iovs.uiovp->iov_base, (addr64_t)cp, acnt, cppvPsnk | cppvNoRefSrc | cppvNoModSnk); if (error) /* Copy virtual to physical */ error = EFAULT; KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 1,0); + (uintptr_t)uio->uio_iovs.uiovp->iov_base, (int)cp, acnt, 1,0); } if (error) return (error); break; - case UIO_PHYS_SYSSPACE32: case UIO_PHYS_SYSSPACE: if (uio->uio_rw == UIO_READ) { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_START, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 2,0); + (int)cp, (uintptr_t)uio->uio_iovs.kiovp->iov_base, acnt, 2,0); - error = copypv((addr64_t)cp, uio->uio_iovs.iov32p->iov_base, acnt, cppvKmap | cppvPsrc | cppvNoRefSrc); + error = copypv((addr64_t)cp, uio->uio_iovs.kiovp->iov_base, acnt, cppvKmap | cppvPsrc | cppvNoRefSrc); if (error) /* Copy physical to virtual */ error = EFAULT; KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYOUT)) | DBG_FUNC_END, - (int)cp, (int)uio->uio_iovs.iov32p->iov_base, acnt, 2,0); + (int)cp, (uintptr_t)uio->uio_iovs.kiovp->iov_base, acnt, 2,0); } else { KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_START, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 2,0); + (uintptr_t)uio->uio_iovs.kiovp->iov_base, (int)cp, acnt, 2,0); - error = copypv(uio->uio_iovs.iov32p->iov_base, (addr64_t)cp, acnt, cppvKmap | cppvPsnk | cppvNoRefSrc | cppvNoModSnk); + error = copypv(uio->uio_iovs.kiovp->iov_base, (addr64_t)cp, acnt, cppvKmap | cppvPsnk | cppvNoRefSrc | cppvNoModSnk); if (error) /* Copy virtual to physical */ error = EFAULT; KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, DBG_UIO_COPYIN)) | DBG_FUNC_END, - (int)uio->uio_iovs.iov32p->iov_base, (int)cp, acnt, 2,0); + (uintptr_t)uio->uio_iovs.kiovp->iov_base, (int)cp, acnt, 2,0); } if (error) return (error); @@ -283,15 +259,7 @@ uiomove64(addr64_t cp, int n, register struct uio *uio) default: break; } - uio_iov_base_add(uio, acnt); -#if LP64KERN - uio_iov_len_add(uio, -((int64_t)acnt)); - uio_setresid(uio, (uio_resid(uio) - ((int64_t)acnt))); -#else - uio_iov_len_add(uio, -((int)acnt)); - uio_setresid(uio, (uio_resid(uio) - ((int)acnt))); -#endif - uio->uio_offset += acnt; + uio_update(uio, acnt); cp += acnt; n -= acnt; } @@ -302,120 +270,45 @@ uiomove64(addr64_t cp, int n, register struct uio *uio) * Give next character to user as result of read. */ int -ureadc(c, uio) - register int c; - register struct uio *uio; +ureadc(int c, struct uio *uio) { if (uio_resid(uio) <= 0) panic("ureadc: non-positive resid"); -again: + uio_update(uio, 0); if (uio->uio_iovcnt == 0) panic("ureadc: non-positive iovcnt"); - if (uio_iov_len(uio) <= 0) { - uio->uio_iovcnt--; - uio_next_iov(uio); - goto again; - } + if (uio_curriovlen(uio) <= 0) + panic("ureadc: non-positive iovlen"); + switch (uio->uio_segflg) { case UIO_USERSPACE32: case UIO_USERSPACE: - if (subyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), c) < 0) - return (EFAULT); - break; - - case UIO_USERSPACE64: - if (subyte((user_addr_t)uio->uio_iovs.iov64p->iov_base, c) < 0) - return (EFAULT); - break; - - case UIO_SYSSPACE32: - case UIO_SYSSPACE: - *((caddr_t)uio->uio_iovs.iov32p->iov_base) = c; - break; - case UIO_USERISPACE32: case UIO_USERISPACE: - if (suibyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base), c) < 0) - return (EFAULT); - break; - - default: - break; - } - uio_iov_base_add(uio, 1); - uio_iov_len_add(uio, -1); - uio_setresid(uio, (uio_resid(uio) - 1)); - uio->uio_offset++; - return (0); -} - -#if defined(vax) || defined(ppc) -/* unused except by ct.c, other oddities XXX */ -/* - * Get next character written in by user from uio. - */ -int -uwritec(uio) - uio_t uio; -{ - register int c = 0; - - if (uio_resid(uio) <= 0) - return (-1); -again: - if (uio->uio_iovcnt <= 0) - panic("uwritec: non-positive iovcnt"); - - if (uio_iov_len(uio) == 0) { - uio_next_iov(uio); - if (--uio->uio_iovcnt == 0) - return (-1); - goto again; - } - switch (uio->uio_segflg) { - - case UIO_USERSPACE32: - case UIO_USERSPACE: - c = fubyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base)); - break; - case UIO_USERSPACE64: - c = fubyte((user_addr_t)uio->uio_iovs.iov64p->iov_base); + case UIO_USERISPACE64: + if (subyte((user_addr_t)uio->uio_iovs.uiovp->iov_base, c) < 0) + return (EFAULT); break; case UIO_SYSSPACE32: case UIO_SYSSPACE: - c = *((caddr_t)uio->uio_iovs.iov32p->iov_base) & 0377; - break; - - case UIO_USERISPACE32: - case UIO_USERISPACE: - c = fuibyte(CAST_USER_ADDR_T(uio->uio_iovs.iov32p->iov_base)); + *(CAST_DOWN(caddr_t, uio->uio_iovs.kiovp->iov_base)) = c; break; default: - c = 0; /* avoid uninitialized variable warning */ - panic("uwritec: bogus uio_segflg"); break; } - if (c < 0) - return (-1); - uio_iov_base_add(uio, 1); - uio_iov_len_add(uio, -1); - uio_setresid(uio, (uio_resid(uio) - 1)); - uio->uio_offset++; - return (c); + uio_update(uio, 1); + return (0); } -#endif /* vax || ppc */ /* * General routine to allocate a hash table. */ void * -hashinit(elements, type, hashmask) - int elements, type; - u_long *hashmask; +hashinit(int elements, int type, u_long *hashmask) { long hashsize; LIST_HEAD(generic, generic) *hashtbl; @@ -427,7 +320,7 @@ hashinit(elements, type, hashmask) continue; hashsize >>= 1; MALLOC(hashtbl, struct generic *, - (u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK|M_ZERO); + hashsize * sizeof(*hashtbl), type, M_WAITOK|M_ZERO); if (hashtbl != NULL) { for (i = 0; i < hashsize; i++) LIST_INIT(&hashtbl[i]); @@ -443,7 +336,7 @@ user_ssize_t uio_resid( uio_t a_uio ) { #if DEBUG if (a_uio == NULL) { - panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__); + printf("%s :%d - invalid uio_t\n", __FILE__, __LINE__); } /* if (IS_VALID_UIO_SEGFLG(a_uio->uio_segflg) == 0) { */ /* panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); */ @@ -455,14 +348,7 @@ user_ssize_t uio_resid( uio_t a_uio ) return( 0 ); } - if (UIO_IS_64_BIT_SPACE(a_uio)) { -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - return( (user_ssize_t)a_uio->uio_resid ); -#else - return( a_uio->uio_resid_64 ); -#endif - } - return( (user_ssize_t)a_uio->uio_resid ); + return( a_uio->uio_resid_64 ); } /* @@ -483,57 +369,10 @@ void uio_setresid( uio_t a_uio, user_ssize_t a_value ) return; } - if (UIO_IS_64_BIT_SPACE(a_uio)) { -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - a_uio->uio_resid = (int)a_value; -#else - a_uio->uio_resid_64 = a_value; -#endif - } - else { - a_uio->uio_resid = (int)a_value; - } + a_uio->uio_resid_64 = a_value; return; } -#if 0 // obsolete -/* - * uio_proc_t - return the proc_t for the given uio_t - * WARNING - This call is going away. Find another way to get the proc_t!! - */ -__private_extern__ proc_t uio_proc_t( uio_t a_uio ) -{ -#if LP64_DEBUG - if (a_uio == NULL) { - panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__); - } -#endif /* LP64_DEBUG */ - - /* return 0 if there are no active iovecs */ - if (a_uio == NULL) { - return( NULL ); - } - return( a_uio->uio_procp ); -} - -/* - * uio_setproc_t - set the residual IO value for the given uio_t - * WARNING - This call is going away. - */ -__private_extern__ void uio_setproc_t( uio_t a_uio, proc_t a_proc_t ) -{ - if (a_uio == NULL) { -#if LP64_DEBUG - panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__); -#endif /* LP64_DEBUG */ - return; - } - - a_uio->uio_procp = a_proc_t; - return; -} -#endif // obsolete - /* * uio_curriovbase - return the base address of the current iovec associated * with the given uio_t. May return 0. @@ -550,10 +389,10 @@ user_addr_t uio_curriovbase( uio_t a_uio ) return(0); } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { return(a_uio->uio_iovs.uiovp->iov_base); } - return((user_addr_t)((uintptr_t)a_uio->uio_iovs.kiovp->iov_base)); + return((user_addr_t)a_uio->uio_iovs.kiovp->iov_base); } @@ -573,7 +412,7 @@ user_size_t uio_curriovlen( uio_t a_uio ) return(0); } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { return(a_uio->uio_iovs.uiovp->iov_len); } return((user_size_t)a_uio->uio_iovs.kiovp->iov_len); @@ -595,7 +434,7 @@ __private_extern__ void uio_setcurriovlen( uio_t a_uio, user_size_t a_value ) return; } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { a_uio->uio_iovs.uiovp->iov_len = a_value; } else { @@ -735,10 +574,10 @@ uio_t uio_create( int a_iovcount, /* number of iovecs */ int a_iodirection ) /* read or write flag */ { void * my_buf_p; - int my_size; + size_t my_size; uio_t my_uio; - my_size = sizeof(struct uio) + (sizeof(struct user_iovec) * a_iovcount); + my_size = UIO_SIZEOF(a_iovcount); my_buf_p = kalloc(my_size); my_uio = uio_createwithbuffer( a_iovcount, a_offset, @@ -750,7 +589,7 @@ uio_t uio_create( int a_iovcount, /* number of iovecs */ /* leave a note that we allocated this uio_t */ my_uio->uio_flags |= UIO_FLAGS_WE_ALLOCED; #if DEBUG - hw_atomic_add(&uio_t_count, 1); + (void)hw_atomic_add(&uio_t_count, 1); #endif } @@ -771,12 +610,12 @@ __private_extern__ uio_t int a_spacetype, /* type of address space */ int a_iodirection, /* read or write flag */ void *a_buf_p, /* pointer to a uio_t buffer */ - int a_buffer_size ) /* size of uio_t buffer */ + size_t a_buffer_size ) /* size of uio_t buffer */ { uio_t my_uio = (uio_t) a_buf_p; - int my_size; + size_t my_size; - my_size = sizeof(struct uio) + (sizeof(struct user_iovec) * a_iovcount); + my_size = UIO_SIZEOF(a_iovcount); if (a_buffer_size < my_size) { #if DEBUG panic("%s :%d - a_buffer_size is too small\n", __FILE__, __LINE__); @@ -803,17 +642,23 @@ __private_extern__ uio_t bzero(my_uio, my_size); my_uio->uio_size = my_size; - /* we use uio_segflg to indicate if the uio_t is the new format or */ - /* old (pre LP64 support) legacy format */ + /* + * we use uio_segflg to indicate if the uio_t is the new format or + * old (pre LP64 support) legacy format + * This switch statement should canonicalize incoming space type + * to one of UIO_USERSPACE32/64, UIO_PHYS_USERSPACE32/64, or + * UIO_SYSSPACE/UIO_PHYS_SYSSPACE + */ switch (a_spacetype) { case UIO_USERSPACE: my_uio->uio_segflg = UIO_USERSPACE32; - case UIO_SYSSPACE: - my_uio->uio_segflg = UIO_SYSSPACE32; + break; + case UIO_SYSSPACE32: + my_uio->uio_segflg = UIO_SYSSPACE; + break; case UIO_PHYS_USERSPACE: my_uio->uio_segflg = UIO_PHYS_USERSPACE32; - case UIO_PHYS_SYSSPACE: - my_uio->uio_segflg = UIO_PHYS_SYSSPACE32; + break; default: my_uio->uio_segflg = a_spacetype; break; @@ -838,7 +683,7 @@ __private_extern__ uio_t /* * uio_spacetype - return the address space type for the given uio_t */ -int uio_spacetype( uio_t a_uio ) +__private_extern__ int uio_spacetype( uio_t a_uio ) { if (a_uio == NULL) { #if LP64_DEBUG @@ -857,7 +702,7 @@ int uio_spacetype( uio_t a_uio ) * which will increase as the IO is completed and is NOT embedded within the * uio, it is a seperate array of one or more iovecs. */ -struct user_iovec * uio_iovsaddr( uio_t a_uio ) +__private_extern__ struct user_iovec * uio_iovsaddr( uio_t a_uio ) { struct user_iovec * my_addr; @@ -865,12 +710,15 @@ struct user_iovec * uio_iovsaddr( uio_t a_uio ) return(NULL); } - if (a_uio->uio_segflg == UIO_USERSPACE || a_uio->uio_segflg == UIO_SYSSPACE) { + if (UIO_SEG_IS_USER_SPACE(a_uio->uio_segflg)) { /* we need this for compatibility mode. */ - my_addr = (struct user_iovec *) a_uio->uio_iovs.iovp; + my_addr = (struct user_iovec *) a_uio->uio_iovs.uiovp; } else { - my_addr = (struct user_iovec *) (((uint8_t *)a_uio) + sizeof(struct uio)); +#if DEBUG + panic("uio_iovsaddr called for UIO_SYSSPACE request"); +#endif + my_addr = 0; } return(my_addr); } @@ -912,7 +760,29 @@ void uio_reset( uio_t a_uio, my_max_iovs = a_uio->uio_max_iovs; bzero(a_uio, my_size); a_uio->uio_size = my_size; - a_uio->uio_segflg = a_spacetype; + + /* + * we use uio_segflg to indicate if the uio_t is the new format or + * old (pre LP64 support) legacy format + * This switch statement should canonicalize incoming space type + * to one of UIO_USERSPACE32/64, UIO_PHYS_USERSPACE32/64, or + * UIO_SYSSPACE/UIO_PHYS_SYSSPACE + */ + switch (a_spacetype) { + case UIO_USERSPACE: + a_uio->uio_segflg = UIO_USERSPACE32; + break; + case UIO_SYSSPACE32: + a_uio->uio_segflg = UIO_SYSSPACE; + break; + case UIO_PHYS_USERSPACE: + a_uio->uio_segflg = UIO_PHYS_USERSPACE32; + break; + default: + a_uio->uio_segflg = a_spacetype; + break; + } + if (my_max_iovs > 0) { a_uio->uio_iovs.uiovp = (struct user_iovec *) (((uint8_t *)a_uio) + sizeof(struct uio)); @@ -920,6 +790,7 @@ void uio_reset( uio_t a_uio, else { a_uio->uio_iovs.uiovp = NULL; } + a_uio->uio_max_iovs = my_max_iovs; a_uio->uio_offset = a_offset; a_uio->uio_rw = a_iodirection; @@ -942,9 +813,8 @@ void uio_free( uio_t a_uio ) if (a_uio != NULL && (a_uio->uio_flags & UIO_FLAGS_WE_ALLOCED) != 0) { #if DEBUG - if ((int)(hw_atomic_sub(&uio_t_count, 1)) < 0) { - panic("%s :%d - uio_t_count has gone negative\n", __FILE__, __LINE__); - } + if (hw_atomic_sub(&uio_t_count, 1) == UINT_MAX) + panic("%s :%d - uio_t_count underflow\n", __FILE__, __LINE__); #endif kfree(a_uio, a_uio->uio_size); } @@ -969,17 +839,13 @@ int uio_addiov( uio_t a_uio, user_addr_t a_baseaddr, user_size_t a_length ) return(-1); } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { for ( i = 0; i < a_uio->uio_max_iovs; i++ ) { if (a_uio->uio_iovs.uiovp[i].iov_len == 0 && a_uio->uio_iovs.uiovp[i].iov_base == 0) { a_uio->uio_iovs.uiovp[i].iov_len = a_length; a_uio->uio_iovs.uiovp[i].iov_base = a_baseaddr; a_uio->uio_iovcnt++; -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - a_uio->uio_resid += a_length; -#else a_uio->uio_resid_64 += a_length; -#endif return( 0 ); } } @@ -987,10 +853,10 @@ int uio_addiov( uio_t a_uio, user_addr_t a_baseaddr, user_size_t a_length ) else { for ( i = 0; i < a_uio->uio_max_iovs; i++ ) { if (a_uio->uio_iovs.kiovp[i].iov_len == 0 && a_uio->uio_iovs.kiovp[i].iov_base == 0) { - a_uio->uio_iovs.kiovp[i].iov_len = (u_int32_t)a_length; - a_uio->uio_iovs.kiovp[i].iov_base = (u_int32_t)((uintptr_t)a_baseaddr); + a_uio->uio_iovs.kiovp[i].iov_len = (u_int64_t)a_length; + a_uio->uio_iovs.kiovp[i].iov_base = (u_int64_t)a_baseaddr; a_uio->uio_iovcnt++; - a_uio->uio_resid += a_length; + a_uio->uio_resid_64 += a_length; return( 0 ); } } @@ -1021,7 +887,7 @@ int uio_getiov( uio_t a_uio, return(-1); } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { if (a_baseaddr_p != NULL) { *a_baseaddr_p = a_uio->uio_iovs.uiovp[a_index].iov_base; } @@ -1056,33 +922,40 @@ __private_extern__ void uio_calculateresid( uio_t a_uio ) return; } - a_uio->uio_iovcnt = 0; - if (UIO_IS_64_BIT_SPACE(a_uio)) { -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - a_uio->uio_resid = 0; -#else + a_uio->uio_iovcnt = a_uio->uio_max_iovs; + if (UIO_IS_USER_SPACE(a_uio)) { a_uio->uio_resid_64 = 0; -#endif for ( i = 0; i < a_uio->uio_max_iovs; i++ ) { if (a_uio->uio_iovs.uiovp[i].iov_len != 0 && a_uio->uio_iovs.uiovp[i].iov_base != 0) { - a_uio->uio_iovcnt++; -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - a_uio->uio_resid += a_uio->uio_iovs.uiovp[i].iov_len; -#else a_uio->uio_resid_64 += a_uio->uio_iovs.uiovp[i].iov_len; -#endif + } + } + + /* position to first non zero length iovec (4235922) */ + while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.uiovp->iov_len == 0) { + a_uio->uio_iovcnt--; + if (a_uio->uio_iovcnt > 0) { + a_uio->uio_iovs.uiovp++; } } } else { - a_uio->uio_resid = 0; + a_uio->uio_resid_64 = 0; for ( i = 0; i < a_uio->uio_max_iovs; i++ ) { if (a_uio->uio_iovs.kiovp[i].iov_len != 0 && a_uio->uio_iovs.kiovp[i].iov_base != 0) { - a_uio->uio_iovcnt++; - a_uio->uio_resid += a_uio->uio_iovs.kiovp[i].iov_len; + a_uio->uio_resid_64 += a_uio->uio_iovs.kiovp[i].iov_len; + } + } + + /* position to first non zero length iovec (4235922) */ + while (a_uio->uio_iovcnt > 0 && a_uio->uio_iovs.kiovp->iov_len == 0) { + a_uio->uio_iovcnt--; + if (a_uio->uio_iovcnt > 0) { + a_uio->uio_iovs.kiovp++; } } } + return; } @@ -1110,7 +983,7 @@ void uio_update( uio_t a_uio, user_size_t a_count ) return; } - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { /* * if a_count == 0, then we are asking to skip over * any empty iovs @@ -1121,34 +994,20 @@ void uio_update( uio_t a_uio, user_size_t a_count ) a_uio->uio_iovs.uiovp->iov_len = 0; } else { - a_uio->uio_iovs.uiovp->iov_base += a_count; + a_uio->uio_iovs.uiovp->iov_base += a_count; a_uio->uio_iovs.uiovp->iov_len -= a_count; } -#if 1 // LP64todo - remove this temp workaround once we go live with uio KPI - if (a_uio->uio_resid < 0) { - a_uio->uio_resid = 0; - } - if (a_count > (user_size_t)a_uio->uio_resid) { - a_uio->uio_offset += a_uio->uio_resid; - a_uio->uio_resid = 0; - } - else { - a_uio->uio_offset += a_count; - a_uio->uio_resid -= a_count; - } -#else if (a_uio->uio_resid_64 < 0) { - a_uio->uio_resid_64 = 0; + a_uio->uio_resid_64 = 0; } if (a_count > (user_size_t)a_uio->uio_resid_64) { - a_uio->uio_offset += a_uio->uio_resid_64; + a_uio->uio_offset += a_uio->uio_resid_64; a_uio->uio_resid_64 = 0; } else { - a_uio->uio_offset += a_count; + a_uio->uio_offset += a_count; a_uio->uio_resid_64 -= a_count; } -#endif // LP64todo } /* * advance to next iovec if current one is totally consumed @@ -1174,16 +1033,16 @@ void uio_update( uio_t a_uio, user_size_t a_count ) a_uio->uio_iovs.kiovp->iov_base += a_count; a_uio->uio_iovs.kiovp->iov_len -= a_count; } - if (a_uio->uio_resid < 0) { - a_uio->uio_resid = 0; + if (a_uio->uio_resid_64 < 0) { + a_uio->uio_resid_64 = 0; } - if (a_count > (user_size_t)a_uio->uio_resid) { - a_uio->uio_offset += a_uio->uio_resid; - a_uio->uio_resid = 0; + if (a_count > (user_size_t)a_uio->uio_resid_64) { + a_uio->uio_offset += a_uio->uio_resid_64; + a_uio->uio_resid_64 = 0; } else { - a_uio->uio_offset += a_count; - a_uio->uio_resid -= a_count; + a_uio->uio_offset += a_count; + a_uio->uio_resid_64 -= a_count; } } /* @@ -1199,6 +1058,46 @@ void uio_update( uio_t a_uio, user_size_t a_count ) return; } +/* + * uio_pushback - undo uncommitted I/O by subtracting from the + * current base address and offset, and incrementing the residiual + * IO. If the UIO was previously exhausted, this call will panic. + * New code should not use this functionality. + */ +__private_extern__ void uio_pushback( uio_t a_uio, user_size_t a_count ) +{ +#if LP64_DEBUG + if (a_uio == NULL) { + panic("%s :%d - invalid uio_t\n", __FILE__, __LINE__); + } + if (UIO_IS_32_BIT_SPACE(a_uio) && a_count > 0xFFFFFFFFull) { + panic("%s :%d - invalid count value \n", __FILE__, __LINE__); + } +#endif /* LP64_DEBUG */ + + if (a_uio == NULL || a_count == 0) { + return; + } + + if (a_uio->uio_iovcnt < 1) { + panic("Invalid uio for pushback"); + } + + if (UIO_IS_USER_SPACE(a_uio)) { + a_uio->uio_iovs.uiovp->iov_base -= a_count; + a_uio->uio_iovs.uiovp->iov_len += a_count; + } + else { + a_uio->uio_iovs.kiovp->iov_base -= a_count; + a_uio->uio_iovs.kiovp->iov_len += a_count; + } + + a_uio->uio_offset -= a_count; + a_uio->uio_resid_64 += a_count; + + return; +} + /* * uio_duplicate - allocate a new uio and make a copy of the given uio_t. @@ -1227,7 +1126,7 @@ uio_t uio_duplicate( uio_t a_uio ) /* advance to first nonzero iovec */ if (my_uio->uio_iovcnt > 0) { for ( i = 0; i < my_uio->uio_max_iovs; i++ ) { - if (UIO_IS_64_BIT_SPACE(a_uio)) { + if (UIO_IS_USER_SPACE(a_uio)) { if (my_uio->uio_iovs.uiovp->iov_len != 0) { break; } @@ -1243,6 +1142,34 @@ uio_t uio_duplicate( uio_t a_uio ) } } + my_uio->uio_flags = UIO_FLAGS_WE_ALLOCED | UIO_FLAGS_INITED; + return(my_uio); } +int copyin_user_iovec_array(user_addr_t uaddr, int spacetype, int count, struct user_iovec *dst) +{ + size_t size_of_iovec = ( spacetype == UIO_USERSPACE64 ? sizeof(struct user64_iovec) : sizeof(struct user32_iovec)); + int error; + int i; + + // copyin to the front of "dst", without regard for putting records in the right places + error = copyin(uaddr, dst, count * size_of_iovec); + if (error) + return (error); + + // now, unpack the entries in reverse order, so we don't overwrite anything + for (i = count - 1; i >= 0; i--) { + if (spacetype == UIO_USERSPACE64) { + struct user64_iovec iovec = ((struct user64_iovec *)dst)[i]; + dst[i].iov_base = iovec.iov_base; + dst[i].iov_len = iovec.iov_len; + } else { + struct user32_iovec iovec = ((struct user32_iovec *)dst)[i]; + dst[i].iov_base = iovec.iov_base; + dst[i].iov_len = iovec.iov_len; + } + } + + return (0); +}