10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56707Sbrutus * Common Development and Distribution License (the "License").
66707Sbrutus * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9412SAleksandr.Guzovskiy@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate * The Regents of the University of California
320Sstevel@tonic-gate * All Rights Reserved
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate * contributors.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/uio.h>
440Sstevel@tonic-gate #include <sys/errno.h>
456707Sbrutus #include <sys/vmsystm.h>
466707Sbrutus #include <sys/cmn_err.h>
476707Sbrutus #include <vm/as.h>
486707Sbrutus #include <vm/page.h>
496707Sbrutus
506707Sbrutus #include <sys/dcopy.h>
516707Sbrutus
526707Sbrutus int64_t uioa_maxpoll = -1; /* <0 = noblock, 0 = block, >0 = block after */
536707Sbrutus #define UIO_DCOPY_CHANNEL 0
546707Sbrutus #define UIO_DCOPY_CMD 1
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * Move "n" bytes at byte address "p"; "rw" indicates the direction
580Sstevel@tonic-gate * of the move, and the I/O parameters are provided in "uio", which is
590Sstevel@tonic-gate * update to reflect the data which was moved. Returns 0 on success or
600Sstevel@tonic-gate * a non-zero errno on failure.
610Sstevel@tonic-gate */
620Sstevel@tonic-gate int
uiomove(void * p,size_t n,enum uio_rw rw,struct uio * uio)630Sstevel@tonic-gate uiomove(void *p, size_t n, enum uio_rw rw, struct uio *uio)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate struct iovec *iov;
660Sstevel@tonic-gate ulong_t cnt;
670Sstevel@tonic-gate int error;
680Sstevel@tonic-gate
690Sstevel@tonic-gate while (n && uio->uio_resid) {
700Sstevel@tonic-gate iov = uio->uio_iov;
710Sstevel@tonic-gate cnt = MIN(iov->iov_len, n);
720Sstevel@tonic-gate if (cnt == 0l) {
730Sstevel@tonic-gate uio->uio_iov++;
740Sstevel@tonic-gate uio->uio_iovcnt--;
750Sstevel@tonic-gate continue;
760Sstevel@tonic-gate }
770Sstevel@tonic-gate switch (uio->uio_segflg) {
780Sstevel@tonic-gate
790Sstevel@tonic-gate case UIO_USERSPACE:
800Sstevel@tonic-gate case UIO_USERISPACE:
810Sstevel@tonic-gate if (rw == UIO_READ) {
820Sstevel@tonic-gate error = xcopyout_nta(p, iov->iov_base, cnt,
830Sstevel@tonic-gate (uio->uio_extflg & UIO_COPY_CACHED));
840Sstevel@tonic-gate } else {
850Sstevel@tonic-gate error = xcopyin_nta(iov->iov_base, p, cnt,
860Sstevel@tonic-gate (uio->uio_extflg & UIO_COPY_CACHED));
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate if (error)
900Sstevel@tonic-gate return (error);
910Sstevel@tonic-gate break;
920Sstevel@tonic-gate
930Sstevel@tonic-gate case UIO_SYSSPACE:
940Sstevel@tonic-gate if (rw == UIO_READ)
950Sstevel@tonic-gate error = kcopy_nta(p, iov->iov_base, cnt,
960Sstevel@tonic-gate (uio->uio_extflg & UIO_COPY_CACHED));
970Sstevel@tonic-gate else
980Sstevel@tonic-gate error = kcopy_nta(iov->iov_base, p, cnt,
990Sstevel@tonic-gate (uio->uio_extflg & UIO_COPY_CACHED));
1000Sstevel@tonic-gate if (error)
1010Sstevel@tonic-gate return (error);
1020Sstevel@tonic-gate break;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate iov->iov_base += cnt;
1050Sstevel@tonic-gate iov->iov_len -= cnt;
1060Sstevel@tonic-gate uio->uio_resid -= cnt;
1070Sstevel@tonic-gate uio->uio_loffset += cnt;
1080Sstevel@tonic-gate p = (caddr_t)p + cnt;
1090Sstevel@tonic-gate n -= cnt;
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate return (0);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1158059SDonghai.Qiao@Sun.COM * Fault in the pages of the first n bytes specified by the uio structure.
1168059SDonghai.Qiao@Sun.COM * 1 byte in each page is touched and the uio struct is unmodified. Any
1178059SDonghai.Qiao@Sun.COM * error will terminate the process as this is only a best attempt to get
1188059SDonghai.Qiao@Sun.COM * the pages resident.
1198059SDonghai.Qiao@Sun.COM */
1208059SDonghai.Qiao@Sun.COM void
uio_prefaultpages(ssize_t n,struct uio * uio)1218059SDonghai.Qiao@Sun.COM uio_prefaultpages(ssize_t n, struct uio *uio)
1228059SDonghai.Qiao@Sun.COM {
1238059SDonghai.Qiao@Sun.COM struct iovec *iov;
1248059SDonghai.Qiao@Sun.COM ulong_t cnt, incr;
1258059SDonghai.Qiao@Sun.COM caddr_t p;
1268059SDonghai.Qiao@Sun.COM uint8_t tmp;
1278059SDonghai.Qiao@Sun.COM int iovcnt;
1288059SDonghai.Qiao@Sun.COM
1298059SDonghai.Qiao@Sun.COM iov = uio->uio_iov;
1308059SDonghai.Qiao@Sun.COM iovcnt = uio->uio_iovcnt;
1318059SDonghai.Qiao@Sun.COM
1328059SDonghai.Qiao@Sun.COM while ((n > 0) && (iovcnt > 0)) {
1338059SDonghai.Qiao@Sun.COM cnt = MIN(iov->iov_len, n);
1348059SDonghai.Qiao@Sun.COM if (cnt == 0) {
1358059SDonghai.Qiao@Sun.COM /* empty iov entry */
1368059SDonghai.Qiao@Sun.COM iov++;
1378059SDonghai.Qiao@Sun.COM iovcnt--;
1388059SDonghai.Qiao@Sun.COM continue;
1398059SDonghai.Qiao@Sun.COM }
1408059SDonghai.Qiao@Sun.COM n -= cnt;
1418059SDonghai.Qiao@Sun.COM /*
1428059SDonghai.Qiao@Sun.COM * touch each page in this segment.
1438059SDonghai.Qiao@Sun.COM */
1448059SDonghai.Qiao@Sun.COM p = iov->iov_base;
1458059SDonghai.Qiao@Sun.COM while (cnt) {
1468059SDonghai.Qiao@Sun.COM switch (uio->uio_segflg) {
1478059SDonghai.Qiao@Sun.COM case UIO_USERSPACE:
1488059SDonghai.Qiao@Sun.COM case UIO_USERISPACE:
1498059SDonghai.Qiao@Sun.COM if (fuword8(p, &tmp))
1508059SDonghai.Qiao@Sun.COM return;
1518059SDonghai.Qiao@Sun.COM break;
1528059SDonghai.Qiao@Sun.COM case UIO_SYSSPACE:
1538059SDonghai.Qiao@Sun.COM if (kcopy(p, &tmp, 1))
1548059SDonghai.Qiao@Sun.COM return;
1558059SDonghai.Qiao@Sun.COM break;
1568059SDonghai.Qiao@Sun.COM }
1578059SDonghai.Qiao@Sun.COM incr = MIN(cnt, PAGESIZE);
1588059SDonghai.Qiao@Sun.COM p += incr;
1598059SDonghai.Qiao@Sun.COM cnt -= incr;
1608059SDonghai.Qiao@Sun.COM }
1618059SDonghai.Qiao@Sun.COM /*
1628059SDonghai.Qiao@Sun.COM * touch the last byte in case it straddles a page.
1638059SDonghai.Qiao@Sun.COM */
1648059SDonghai.Qiao@Sun.COM p--;
1658059SDonghai.Qiao@Sun.COM switch (uio->uio_segflg) {
1668059SDonghai.Qiao@Sun.COM case UIO_USERSPACE:
1678059SDonghai.Qiao@Sun.COM case UIO_USERISPACE:
1688059SDonghai.Qiao@Sun.COM if (fuword8(p, &tmp))
1698059SDonghai.Qiao@Sun.COM return;
1708059SDonghai.Qiao@Sun.COM break;
1718059SDonghai.Qiao@Sun.COM case UIO_SYSSPACE:
1728059SDonghai.Qiao@Sun.COM if (kcopy(p, &tmp, 1))
1738059SDonghai.Qiao@Sun.COM return;
1748059SDonghai.Qiao@Sun.COM break;
1758059SDonghai.Qiao@Sun.COM }
1768059SDonghai.Qiao@Sun.COM iov++;
1778059SDonghai.Qiao@Sun.COM iovcnt--;
1788059SDonghai.Qiao@Sun.COM }
1798059SDonghai.Qiao@Sun.COM }
1808059SDonghai.Qiao@Sun.COM
1818059SDonghai.Qiao@Sun.COM /*
182*9412SAleksandr.Guzovskiy@Sun.COM * same as uiomove() but doesn't modify uio structure.
183*9412SAleksandr.Guzovskiy@Sun.COM * return in cbytes how many bytes were copied.
184*9412SAleksandr.Guzovskiy@Sun.COM */
185*9412SAleksandr.Guzovskiy@Sun.COM int
uiocopy(void * p,size_t n,enum uio_rw rw,struct uio * uio,size_t * cbytes)186*9412SAleksandr.Guzovskiy@Sun.COM uiocopy(void *p, size_t n, enum uio_rw rw, struct uio *uio, size_t *cbytes)
187*9412SAleksandr.Guzovskiy@Sun.COM {
188*9412SAleksandr.Guzovskiy@Sun.COM struct iovec *iov;
189*9412SAleksandr.Guzovskiy@Sun.COM ulong_t cnt;
190*9412SAleksandr.Guzovskiy@Sun.COM int error;
191*9412SAleksandr.Guzovskiy@Sun.COM int iovcnt;
192*9412SAleksandr.Guzovskiy@Sun.COM
193*9412SAleksandr.Guzovskiy@Sun.COM iovcnt = uio->uio_iovcnt;
194*9412SAleksandr.Guzovskiy@Sun.COM *cbytes = 0;
195*9412SAleksandr.Guzovskiy@Sun.COM
196*9412SAleksandr.Guzovskiy@Sun.COM for (iov = uio->uio_iov; n && iovcnt; iov++, iovcnt--) {
197*9412SAleksandr.Guzovskiy@Sun.COM cnt = MIN(iov->iov_len, n);
198*9412SAleksandr.Guzovskiy@Sun.COM if (cnt == 0)
199*9412SAleksandr.Guzovskiy@Sun.COM continue;
200*9412SAleksandr.Guzovskiy@Sun.COM
201*9412SAleksandr.Guzovskiy@Sun.COM switch (uio->uio_segflg) {
202*9412SAleksandr.Guzovskiy@Sun.COM
203*9412SAleksandr.Guzovskiy@Sun.COM case UIO_USERSPACE:
204*9412SAleksandr.Guzovskiy@Sun.COM case UIO_USERISPACE:
205*9412SAleksandr.Guzovskiy@Sun.COM if (rw == UIO_READ) {
206*9412SAleksandr.Guzovskiy@Sun.COM error = xcopyout_nta(p, iov->iov_base, cnt,
207*9412SAleksandr.Guzovskiy@Sun.COM (uio->uio_extflg & UIO_COPY_CACHED));
208*9412SAleksandr.Guzovskiy@Sun.COM } else {
209*9412SAleksandr.Guzovskiy@Sun.COM error = xcopyin_nta(iov->iov_base, p, cnt,
210*9412SAleksandr.Guzovskiy@Sun.COM (uio->uio_extflg & UIO_COPY_CACHED));
211*9412SAleksandr.Guzovskiy@Sun.COM }
212*9412SAleksandr.Guzovskiy@Sun.COM
213*9412SAleksandr.Guzovskiy@Sun.COM if (error)
214*9412SAleksandr.Guzovskiy@Sun.COM return (error);
215*9412SAleksandr.Guzovskiy@Sun.COM break;
216*9412SAleksandr.Guzovskiy@Sun.COM
217*9412SAleksandr.Guzovskiy@Sun.COM case UIO_SYSSPACE:
218*9412SAleksandr.Guzovskiy@Sun.COM if (rw == UIO_READ)
219*9412SAleksandr.Guzovskiy@Sun.COM error = kcopy_nta(p, iov->iov_base, cnt,
220*9412SAleksandr.Guzovskiy@Sun.COM (uio->uio_extflg & UIO_COPY_CACHED));
221*9412SAleksandr.Guzovskiy@Sun.COM else
222*9412SAleksandr.Guzovskiy@Sun.COM error = kcopy_nta(iov->iov_base, p, cnt,
223*9412SAleksandr.Guzovskiy@Sun.COM (uio->uio_extflg & UIO_COPY_CACHED));
224*9412SAleksandr.Guzovskiy@Sun.COM if (error)
225*9412SAleksandr.Guzovskiy@Sun.COM return (error);
226*9412SAleksandr.Guzovskiy@Sun.COM break;
227*9412SAleksandr.Guzovskiy@Sun.COM }
228*9412SAleksandr.Guzovskiy@Sun.COM p = (caddr_t)p + cnt;
229*9412SAleksandr.Guzovskiy@Sun.COM n -= cnt;
230*9412SAleksandr.Guzovskiy@Sun.COM *cbytes += cnt;
231*9412SAleksandr.Guzovskiy@Sun.COM }
232*9412SAleksandr.Guzovskiy@Sun.COM return (0);
233*9412SAleksandr.Guzovskiy@Sun.COM }
234*9412SAleksandr.Guzovskiy@Sun.COM
235*9412SAleksandr.Guzovskiy@Sun.COM /*
2360Sstevel@tonic-gate * transfer a character value into the address space
2370Sstevel@tonic-gate * delineated by a uio and update fields within the
2380Sstevel@tonic-gate * uio for next character. Return 0 for success, EFAULT
2390Sstevel@tonic-gate * for error.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate int
ureadc(int val,struct uio * uiop)2420Sstevel@tonic-gate ureadc(int val, struct uio *uiop)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate struct iovec *iovp;
2450Sstevel@tonic-gate unsigned char c;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * first determine if uio is valid. uiop should be
2490Sstevel@tonic-gate * non-NULL and the resid count > 0.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate if (!(uiop && uiop->uio_resid > 0))
2520Sstevel@tonic-gate return (EFAULT);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * scan through iovecs until one is found that is non-empty.
2560Sstevel@tonic-gate * Return EFAULT if none found.
2570Sstevel@tonic-gate */
2580Sstevel@tonic-gate while (uiop->uio_iovcnt > 0) {
2590Sstevel@tonic-gate iovp = uiop->uio_iov;
2600Sstevel@tonic-gate if (iovp->iov_len <= 0) {
2610Sstevel@tonic-gate uiop->uio_iovcnt--;
2620Sstevel@tonic-gate uiop->uio_iov++;
2630Sstevel@tonic-gate } else
2640Sstevel@tonic-gate break;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (uiop->uio_iovcnt <= 0)
2680Sstevel@tonic-gate return (EFAULT);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * Transfer character to uio space.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate c = (unsigned char) (val & 0xFF);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate switch (uiop->uio_segflg) {
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate case UIO_USERISPACE:
2790Sstevel@tonic-gate case UIO_USERSPACE:
2800Sstevel@tonic-gate if (copyout(&c, iovp->iov_base, sizeof (unsigned char)))
2810Sstevel@tonic-gate return (EFAULT);
2820Sstevel@tonic-gate break;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate case UIO_SYSSPACE: /* can do direct copy since kernel-kernel */
2850Sstevel@tonic-gate *iovp->iov_base = c;
2860Sstevel@tonic-gate break;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate default:
2890Sstevel@tonic-gate return (EFAULT); /* invalid segflg value */
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * bump up/down iovec and uio members to reflect transfer.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate iovp->iov_base++;
2960Sstevel@tonic-gate iovp->iov_len--;
2970Sstevel@tonic-gate uiop->uio_resid--;
2980Sstevel@tonic-gate uiop->uio_loffset++;
2990Sstevel@tonic-gate return (0); /* success */
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate * return a character value from the address space
3040Sstevel@tonic-gate * delineated by a uio and update fields within the
3050Sstevel@tonic-gate * uio for next character. Return the character for success,
3060Sstevel@tonic-gate * -1 for error.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate int
uwritec(struct uio * uiop)3090Sstevel@tonic-gate uwritec(struct uio *uiop)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate struct iovec *iovp;
3120Sstevel@tonic-gate unsigned char c;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * verify we were passed a valid uio structure.
3160Sstevel@tonic-gate * (1) non-NULL uiop, (2) positive resid count
3170Sstevel@tonic-gate * (3) there is an iovec with positive length
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate if (!(uiop && uiop->uio_resid > 0))
3210Sstevel@tonic-gate return (-1);
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate while (uiop->uio_iovcnt > 0) {
3240Sstevel@tonic-gate iovp = uiop->uio_iov;
3250Sstevel@tonic-gate if (iovp->iov_len <= 0) {
3260Sstevel@tonic-gate uiop->uio_iovcnt--;
3270Sstevel@tonic-gate uiop->uio_iov++;
3280Sstevel@tonic-gate } else
3290Sstevel@tonic-gate break;
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate if (uiop->uio_iovcnt <= 0)
3330Sstevel@tonic-gate return (-1);
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate * Get the character from the uio address space.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate switch (uiop->uio_segflg) {
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate case UIO_USERISPACE:
3410Sstevel@tonic-gate case UIO_USERSPACE:
3420Sstevel@tonic-gate if (copyin(iovp->iov_base, &c, sizeof (unsigned char)))
3430Sstevel@tonic-gate return (-1);
3440Sstevel@tonic-gate break;
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate case UIO_SYSSPACE:
3470Sstevel@tonic-gate c = *iovp->iov_base;
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate default:
3510Sstevel@tonic-gate return (-1); /* invalid segflg */
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate * Adjust fields of iovec and uio appropriately.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate iovp->iov_base++;
3580Sstevel@tonic-gate iovp->iov_len--;
3590Sstevel@tonic-gate uiop->uio_resid--;
3600Sstevel@tonic-gate uiop->uio_loffset++;
3610Sstevel@tonic-gate return ((int)c & 0xFF); /* success */
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Drop the next n chars out of *uiop.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate void
uioskip(uio_t * uiop,size_t n)3680Sstevel@tonic-gate uioskip(uio_t *uiop, size_t n)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate if (n > uiop->uio_resid)
3710Sstevel@tonic-gate return;
3720Sstevel@tonic-gate while (n != 0) {
3730Sstevel@tonic-gate register iovec_t *iovp = uiop->uio_iov;
3740Sstevel@tonic-gate register size_t niovb = MIN(iovp->iov_len, n);
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate if (niovb == 0) {
3770Sstevel@tonic-gate uiop->uio_iov++;
3780Sstevel@tonic-gate uiop->uio_iovcnt--;
3790Sstevel@tonic-gate continue;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate iovp->iov_base += niovb;
3820Sstevel@tonic-gate uiop->uio_loffset += niovb;
3830Sstevel@tonic-gate iovp->iov_len -= niovb;
3840Sstevel@tonic-gate uiop->uio_resid -= niovb;
3850Sstevel@tonic-gate n -= niovb;
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * Dup the suio into the duio and diovec of size diov_cnt. If diov
3910Sstevel@tonic-gate * is too small to dup suio then an error will be returned, else 0.
3920Sstevel@tonic-gate */
3930Sstevel@tonic-gate int
uiodup(uio_t * suio,uio_t * duio,iovec_t * diov,int diov_cnt)3940Sstevel@tonic-gate uiodup(uio_t *suio, uio_t *duio, iovec_t *diov, int diov_cnt)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate int ix;
3970Sstevel@tonic-gate iovec_t *siov = suio->uio_iov;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate *duio = *suio;
4000Sstevel@tonic-gate for (ix = 0; ix < suio->uio_iovcnt; ix++) {
4010Sstevel@tonic-gate diov[ix] = siov[ix];
4020Sstevel@tonic-gate if (ix >= diov_cnt)
4030Sstevel@tonic-gate return (1);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate duio->uio_iov = diov;
4060Sstevel@tonic-gate return (0);
4070Sstevel@tonic-gate }
4086707Sbrutus
4096707Sbrutus /*
4106707Sbrutus * Shadow state for checking if a platform has hardware asynchronous
4116707Sbrutus * copy capability and minimum copy size, e.g. Intel's I/OAT dma engine,
4126707Sbrutus *
4136707Sbrutus * Dcopy does a call-back to uioa_dcopy_enable() when a dma device calls
4146707Sbrutus * into dcopy to register and uioa_dcopy_disable() when the device calls
4156707Sbrutus * into dcopy to unregister.
4166707Sbrutus */
4176707Sbrutus uioasync_t uioasync = {B_FALSE, 1024};
4186707Sbrutus
4196707Sbrutus void
uioa_dcopy_enable()4206707Sbrutus uioa_dcopy_enable()
4216707Sbrutus {
4226707Sbrutus uioasync.enabled = B_TRUE;
4236707Sbrutus }
4246707Sbrutus
4256707Sbrutus void
uioa_dcopy_disable()4266707Sbrutus uioa_dcopy_disable()
4276707Sbrutus {
4286707Sbrutus uioasync.enabled = B_FALSE;
4296707Sbrutus }
4306707Sbrutus
4316707Sbrutus /*
4326707Sbrutus * Schedule an asynchronous move of "n" bytes at byte address "p",
4336707Sbrutus * "rw" indicates the direction of the move, I/O parameters and
4346707Sbrutus * async state are provided in "uioa" which is update to reflect
4356707Sbrutus * the data which is to be moved.
4366707Sbrutus *
4376707Sbrutus * Returns 0 on success or a non-zero errno on failure.
4386707Sbrutus *
4396707Sbrutus * Note, while the uioasync APIs are general purpose in design
4406707Sbrutus * the current implementation is Intel I/OAT specific.
4416707Sbrutus */
4426707Sbrutus int
uioamove(void * p,size_t n,enum uio_rw rw,uioa_t * uioa)4436707Sbrutus uioamove(void *p, size_t n, enum uio_rw rw, uioa_t *uioa)
4446707Sbrutus {
4456707Sbrutus int soff, doff;
4466707Sbrutus uint64_t pa;
4476707Sbrutus int cnt;
4486707Sbrutus iovec_t *iov;
4496707Sbrutus dcopy_handle_t channel;
4506707Sbrutus dcopy_cmd_t cmd;
4516707Sbrutus int ret = 0;
4526707Sbrutus int dcopy_flags;
4536707Sbrutus
4546707Sbrutus if (!(uioa->uioa_state & UIOA_ENABLED)) {
4556707Sbrutus /* The uioa_t isn't enabled */
4566707Sbrutus return (ENXIO);
4576707Sbrutus }
4586707Sbrutus
4596707Sbrutus if (uioa->uio_segflg != UIO_USERSPACE || rw != UIO_READ) {
4606707Sbrutus /* Only support to user-land from kernel */
4616707Sbrutus return (ENOTSUP);
4626707Sbrutus }
4636707Sbrutus
4646707Sbrutus
4656707Sbrutus channel = uioa->uioa_hwst[UIO_DCOPY_CHANNEL];
4666707Sbrutus cmd = uioa->uioa_hwst[UIO_DCOPY_CMD];
4676707Sbrutus dcopy_flags = DCOPY_NOSLEEP;
4686707Sbrutus
4696707Sbrutus /*
4706707Sbrutus * While source bytes and destination bytes.
4716707Sbrutus */
4726707Sbrutus while (n > 0 && uioa->uio_resid > 0) {
4736707Sbrutus iov = uioa->uio_iov;
4746707Sbrutus if (iov->iov_len == 0l) {
4756707Sbrutus uioa->uio_iov++;
4766707Sbrutus uioa->uio_iovcnt--;
4776707Sbrutus uioa->uioa_lcur++;
4786707Sbrutus uioa->uioa_lppp = uioa->uioa_lcur->uioa_ppp;
4796707Sbrutus continue;
4806707Sbrutus }
4816707Sbrutus /*
4826707Sbrutus * While source bytes schedule an async
4836707Sbrutus * dma for destination page by page.
4846707Sbrutus */
4856707Sbrutus while (n > 0) {
4866707Sbrutus /* Addr offset in page src/dst */
4876707Sbrutus soff = (uintptr_t)p & PAGEOFFSET;
4886707Sbrutus doff = (uintptr_t)iov->iov_base & PAGEOFFSET;
4896707Sbrutus /* Min copy count src and dst and page sized */
4906707Sbrutus cnt = MIN(n, iov->iov_len);
4916707Sbrutus cnt = MIN(cnt, PAGESIZE - soff);
4926707Sbrutus cnt = MIN(cnt, PAGESIZE - doff);
4936707Sbrutus /* XXX if next page(s) contiguous could use multipage */
4946707Sbrutus
4956707Sbrutus /*
4966707Sbrutus * if we have an old command, we want to link all
4976707Sbrutus * other commands to the next command we alloced so
4986707Sbrutus * we only need to track the last command but can
4996707Sbrutus * still free them all.
5006707Sbrutus */
5016707Sbrutus if (cmd != NULL) {
5026707Sbrutus dcopy_flags |= DCOPY_ALLOC_LINK;
5036707Sbrutus }
5046707Sbrutus ret = dcopy_cmd_alloc(channel, dcopy_flags, &cmd);
5056707Sbrutus if (ret != DCOPY_SUCCESS) {
5066707Sbrutus /* Error of some sort */
5076707Sbrutus return (EIO);
5086707Sbrutus }
5096707Sbrutus uioa->uioa_hwst[UIO_DCOPY_CMD] = cmd;
5106707Sbrutus
5116707Sbrutus ASSERT(cmd->dp_version == DCOPY_CMD_V0);
5126707Sbrutus if (uioa_maxpoll >= 0) {
5136707Sbrutus /* Blocking (>0 may be) used in uioafini() */
5146707Sbrutus cmd->dp_flags = DCOPY_CMD_INTR;
5156707Sbrutus } else {
5166707Sbrutus /* Non blocking uioafini() so no intr */
5176707Sbrutus cmd->dp_flags = DCOPY_CMD_NOFLAGS;
5186707Sbrutus }
5196707Sbrutus cmd->dp_cmd = DCOPY_CMD_COPY;
5206707Sbrutus pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, p));
5216707Sbrutus cmd->dp.copy.cc_source = pa + soff;
5226707Sbrutus if (uioa->uioa_lcur->uioa_pfncnt == 0) {
5236707Sbrutus /* Have a (page_t **) */
5246707Sbrutus pa = ptob((uint64_t)(
5256707Sbrutus *(page_t **)uioa->uioa_lppp)->p_pagenum);
5266707Sbrutus } else {
5276707Sbrutus /* Have a (pfn_t *) */
5286707Sbrutus pa = ptob((uint64_t)(
5296707Sbrutus *(pfn_t *)uioa->uioa_lppp));
5306707Sbrutus }
5316707Sbrutus cmd->dp.copy.cc_dest = pa + doff;
5326707Sbrutus cmd->dp.copy.cc_size = cnt;
5336707Sbrutus ret = dcopy_cmd_post(cmd);
5346707Sbrutus if (ret != DCOPY_SUCCESS) {
5356707Sbrutus /* Error of some sort */
5366707Sbrutus return (EIO);
5376707Sbrutus }
5386707Sbrutus ret = 0;
5396707Sbrutus
5406707Sbrutus /* If UIOA_POLL not set, set it */
5416707Sbrutus if (!(uioa->uioa_state & UIOA_POLL))
5426707Sbrutus uioa->uioa_state |= UIOA_POLL;
5436707Sbrutus
5446707Sbrutus /* Update iov, uio, and local pointers/counters */
5456707Sbrutus iov->iov_base += cnt;
5466707Sbrutus iov->iov_len -= cnt;
5476707Sbrutus uioa->uio_resid -= cnt;
5487660SEric.Yu@Sun.COM uioa->uioa_mbytes += cnt;
5496707Sbrutus uioa->uio_loffset += cnt;
5506707Sbrutus p = (caddr_t)p + cnt;
5516707Sbrutus n -= cnt;
5526707Sbrutus
5536707Sbrutus /* End of iovec? */
5546707Sbrutus if (iov->iov_len == 0) {
5556707Sbrutus /* Yup, next iovec */
5566707Sbrutus break;
5576707Sbrutus }
5586707Sbrutus
5596707Sbrutus /* Next dst addr page? */
5606707Sbrutus if (doff + cnt == PAGESIZE) {
5616707Sbrutus /* Yup, next page_t */
5626707Sbrutus uioa->uioa_lppp++;
5636707Sbrutus }
5646707Sbrutus }
5656707Sbrutus }
5666707Sbrutus
5676707Sbrutus return (ret);
5686707Sbrutus }
5696707Sbrutus
5706707Sbrutus /*
5716707Sbrutus * Initialize a uioa_t for a given uio_t for the current user context,
5726707Sbrutus * copy the common uio_t to the uioa_t, walk the shared iovec_t and
5736707Sbrutus * lock down the user-land page(s) containing iovec_t data, then mapin
5746707Sbrutus * user-land pages using segkpm.
5756707Sbrutus */
5766707Sbrutus int
uioainit(uio_t * uiop,uioa_t * uioap)5776707Sbrutus uioainit(uio_t *uiop, uioa_t *uioap)
5786707Sbrutus {
5796707Sbrutus caddr_t addr;
5806707Sbrutus page_t **pages;
5816707Sbrutus int off;
5826707Sbrutus int len;
5836707Sbrutus proc_t *procp = ttoproc(curthread);
5846707Sbrutus struct as *as = procp->p_as;
5856707Sbrutus iovec_t *iov = uiop->uio_iov;
5866707Sbrutus int32_t iovcnt = uiop->uio_iovcnt;
5876707Sbrutus uioa_page_t *locked = uioap->uioa_locked;
5886707Sbrutus dcopy_handle_t channel;
5896707Sbrutus int error;
5906707Sbrutus
5916707Sbrutus if (! (uioap->uioa_state & UIOA_ALLOC)) {
5926707Sbrutus /* Can only init() a freshly allocated uioa_t */
5936707Sbrutus return (EINVAL);
5946707Sbrutus }
5956707Sbrutus
5966707Sbrutus error = dcopy_alloc(DCOPY_NOSLEEP, &channel);
5976707Sbrutus if (error == DCOPY_NORESOURCES) {
5986707Sbrutus /* Turn off uioa */
5996707Sbrutus uioasync.enabled = B_FALSE;
6006707Sbrutus return (ENODEV);
6016707Sbrutus }
6026707Sbrutus if (error != DCOPY_SUCCESS) {
6036707Sbrutus /* Alloc failed */
6046707Sbrutus return (EIO);
6056707Sbrutus }
6066707Sbrutus
6076707Sbrutus uioap->uioa_hwst[UIO_DCOPY_CHANNEL] = channel;
6086707Sbrutus uioap->uioa_hwst[UIO_DCOPY_CMD] = NULL;
6096707Sbrutus
6106707Sbrutus /* Indicate uioa_t (will be) initialized */
6116707Sbrutus uioap->uioa_state = UIOA_INIT;
6126707Sbrutus
6137660SEric.Yu@Sun.COM uioap->uioa_mbytes = 0;
6147660SEric.Yu@Sun.COM
6156707Sbrutus /* uio_t/uioa_t uio_t common struct copy */
6166707Sbrutus *((uio_t *)uioap) = *uiop;
6176707Sbrutus
6186707Sbrutus /* initialize *uiop->uio_iov */
6196707Sbrutus if (iovcnt > UIOA_IOV_MAX) {
6206707Sbrutus /* Too big? */
6216707Sbrutus return (E2BIG);
6226707Sbrutus }
6236707Sbrutus uioap->uio_iov = iov;
6246707Sbrutus uioap->uio_iovcnt = iovcnt;
6256707Sbrutus
6266707Sbrutus /* Mark the uioap as such */
6276707Sbrutus uioap->uio_extflg |= UIO_ASYNC;
6286707Sbrutus
6296707Sbrutus /*
6306707Sbrutus * For each iovec_t, lock-down the page(s) backing the iovec_t
6316707Sbrutus * and save the page_t list for phys addr use in uioamove().
6326707Sbrutus */
6336707Sbrutus iov = uiop->uio_iov;
6346707Sbrutus iovcnt = uiop->uio_iovcnt;
6356707Sbrutus while (iovcnt > 0) {
6366707Sbrutus addr = iov->iov_base;
6376707Sbrutus off = (uintptr_t)addr & PAGEOFFSET;
6386707Sbrutus addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
6396707Sbrutus len = iov->iov_len + off;
6406707Sbrutus
6416707Sbrutus /* Lock down page(s) for the iov span */
6426707Sbrutus if ((error = as_pagelock(as, &pages,
6436707Sbrutus iov->iov_base, iov->iov_len, S_WRITE)) != 0) {
6446707Sbrutus /* Error */
6456707Sbrutus goto cleanup;
6466707Sbrutus }
6476707Sbrutus
6486707Sbrutus if (pages == NULL) {
6496707Sbrutus /*
6506707Sbrutus * Need page_t list, really only need
6516707Sbrutus * a pfn list so build one.
6526707Sbrutus */
6536707Sbrutus pfn_t *pfnp;
6546707Sbrutus int pcnt = len >> PAGESHIFT;
6556707Sbrutus
6566707Sbrutus if (off)
6576707Sbrutus pcnt++;
6586707Sbrutus if ((pfnp = kmem_alloc(pcnt * sizeof (pfnp),
6596707Sbrutus KM_NOSLEEP)) == NULL) {
6606707Sbrutus error = ENOMEM;
6616707Sbrutus goto cleanup;
6626707Sbrutus }
6636707Sbrutus locked->uioa_ppp = (void **)pfnp;
6646707Sbrutus locked->uioa_pfncnt = pcnt;
6656707Sbrutus AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
6666707Sbrutus while (pcnt-- > 0) {
6676707Sbrutus *pfnp++ = hat_getpfnum(as->a_hat, addr);
6686707Sbrutus addr += PAGESIZE;
6696707Sbrutus }
6706707Sbrutus AS_LOCK_EXIT(as, &as->a_lock);
6716707Sbrutus } else {
6726707Sbrutus /* Have a page_t list, save it */
6736707Sbrutus locked->uioa_ppp = (void **)pages;
6746707Sbrutus locked->uioa_pfncnt = 0;
6756707Sbrutus }
6766707Sbrutus /* Save for as_pageunlock() in uioafini() */
6776707Sbrutus locked->uioa_base = iov->iov_base;
6786707Sbrutus locked->uioa_len = iov->iov_len;
6796707Sbrutus locked++;
6806707Sbrutus
6816707Sbrutus /* Next iovec_t */
6826707Sbrutus iov++;
6836707Sbrutus iovcnt--;
6846707Sbrutus }
6856707Sbrutus /* Initialize curret pointer into uioa_locked[] and it's uioa_ppp */
6866707Sbrutus uioap->uioa_lcur = uioap->uioa_locked;
6876707Sbrutus uioap->uioa_lppp = uioap->uioa_lcur->uioa_ppp;
6886707Sbrutus return (0);
6896707Sbrutus
6906707Sbrutus cleanup:
6916707Sbrutus /* Unlock any previously locked page_t(s) */
6926707Sbrutus while (locked > uioap->uioa_locked) {
6936707Sbrutus locked--;
6946707Sbrutus as_pageunlock(as, (page_t **)locked->uioa_ppp,
6956707Sbrutus locked->uioa_base, locked->uioa_len, S_WRITE);
6966707Sbrutus }
6976707Sbrutus
6986707Sbrutus /* Last indicate uioa_t still in alloc state */
6996707Sbrutus uioap->uioa_state = UIOA_ALLOC;
7007660SEric.Yu@Sun.COM uioap->uioa_mbytes = 0;
7016707Sbrutus
7026707Sbrutus return (error);
7036707Sbrutus }
7046707Sbrutus
7056707Sbrutus /*
7066707Sbrutus * Finish processing of a uioa_t by cleanup any pending "uioap" actions.
7076707Sbrutus */
7086707Sbrutus int
uioafini(uio_t * uiop,uioa_t * uioap)7096707Sbrutus uioafini(uio_t *uiop, uioa_t *uioap)
7106707Sbrutus {
7116707Sbrutus int32_t iovcnt = uiop->uio_iovcnt;
7126707Sbrutus uioa_page_t *locked = uioap->uioa_locked;
7136707Sbrutus struct as *as = ttoproc(curthread)->p_as;
7146707Sbrutus dcopy_handle_t channel;
7156707Sbrutus dcopy_cmd_t cmd;
7166707Sbrutus int ret = 0;
7176707Sbrutus
7186707Sbrutus ASSERT(uioap->uio_extflg & UIO_ASYNC);
7196707Sbrutus
7206707Sbrutus if (!(uioap->uioa_state & (UIOA_ENABLED|UIOA_FINI))) {
7216707Sbrutus /* Must be an active uioa_t */
7226707Sbrutus return (EINVAL);
7236707Sbrutus }
7246707Sbrutus
7256707Sbrutus channel = uioap->uioa_hwst[UIO_DCOPY_CHANNEL];
7266707Sbrutus cmd = uioap->uioa_hwst[UIO_DCOPY_CMD];
7276707Sbrutus
7286707Sbrutus /* XXX - why do we get cmd == NULL sometimes? */
7296707Sbrutus if (cmd != NULL) {
7306707Sbrutus if (uioap->uioa_state & UIOA_POLL) {
7316707Sbrutus /* Wait for last dcopy() to finish */
7326707Sbrutus int64_t poll = 1;
7336707Sbrutus int poll_flag = DCOPY_POLL_NOFLAGS;
7346707Sbrutus
7356707Sbrutus do {
7366707Sbrutus if (uioa_maxpoll == 0 ||
7376707Sbrutus (uioa_maxpoll > 0 &&
7386707Sbrutus poll >= uioa_maxpoll)) {
7396707Sbrutus /* Always block or after maxpoll */
7406707Sbrutus poll_flag = DCOPY_POLL_BLOCK;
7416707Sbrutus } else {
7426707Sbrutus /* No block, poll */
7436707Sbrutus poll++;
7446707Sbrutus }
7456707Sbrutus ret = dcopy_cmd_poll(cmd, poll_flag);
7466707Sbrutus } while (ret == DCOPY_PENDING);
7476707Sbrutus
7486707Sbrutus if (ret == DCOPY_COMPLETED) {
7496707Sbrutus /* Poll/block succeeded */
7506707Sbrutus ret = 0;
7516707Sbrutus } else {
7526707Sbrutus /* Poll/block failed */
7536707Sbrutus ret = EIO;
7546707Sbrutus }
7556707Sbrutus }
7566707Sbrutus dcopy_cmd_free(&cmd);
7576707Sbrutus }
7586707Sbrutus
7596707Sbrutus dcopy_free(&channel);
7606707Sbrutus
7616707Sbrutus /* Unlock all page(s) iovec_t by iovec_t */
7626707Sbrutus while (iovcnt-- > 0) {
7636707Sbrutus page_t **pages;
7646707Sbrutus
7656707Sbrutus if (locked->uioa_pfncnt == 0) {
7666707Sbrutus /* A as_pagelock() returned (page_t **) */
7676707Sbrutus pages = (page_t **)locked->uioa_ppp;
7686707Sbrutus } else {
7696707Sbrutus /* Our pfn_t array */
7706707Sbrutus pages = NULL;
7716707Sbrutus kmem_free(locked->uioa_ppp, locked->uioa_pfncnt *
7726707Sbrutus sizeof (pfn_t *));
7736707Sbrutus }
7746707Sbrutus as_pageunlock(as, pages, locked->uioa_base, locked->uioa_len,
7756707Sbrutus S_WRITE);
7766707Sbrutus
7776707Sbrutus locked++;
7786707Sbrutus }
7796707Sbrutus /* uioa_t->uio_t common struct copy */
7806707Sbrutus *uiop = *((uio_t *)uioap);
7816707Sbrutus
7826707Sbrutus /*
7836707Sbrutus * Last, reset uioa state to alloc.
7846707Sbrutus *
7856707Sbrutus * Note, we only initialize the state here, all other members
7866707Sbrutus * will be initialized in a subsequent uioainit().
7876707Sbrutus */
7886707Sbrutus uioap->uioa_state = UIOA_ALLOC;
7897660SEric.Yu@Sun.COM uioap->uioa_mbytes = 0;
7906707Sbrutus
7916707Sbrutus uioap->uioa_hwst[UIO_DCOPY_CMD] = NULL;
7926707Sbrutus uioap->uioa_hwst[UIO_DCOPY_CHANNEL] = NULL;
7936707Sbrutus
7946707Sbrutus return (ret);
7956707Sbrutus }
796