1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/errno.h>
31*0Sstevel@tonic-gate #include <sys/proc.h>
32*0Sstevel@tonic-gate #include <sys/procfs.h>
33*0Sstevel@tonic-gate #include <sys/sysmacros.h>
34*0Sstevel@tonic-gate #include <sys/uio.h>
35*0Sstevel@tonic-gate #include <sys/cmn_err.h>
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #if defined(__sparc)
38*0Sstevel@tonic-gate #include <sys/stack.h>
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #define STACK_BUF_SIZE 64 /* power of 2 */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate int
prusrio(proc_t * p,enum uio_rw rw,struct uio * uiop,int old)44*0Sstevel@tonic-gate prusrio(proc_t *p, enum uio_rw rw, struct uio *uiop, int old)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate /* longlong-aligned short buffer */
47*0Sstevel@tonic-gate longlong_t buffer[STACK_BUF_SIZE / sizeof (longlong_t)];
48*0Sstevel@tonic-gate int error = 0;
49*0Sstevel@tonic-gate void *bp;
50*0Sstevel@tonic-gate int allocated;
51*0Sstevel@tonic-gate ssize_t total = uiop->uio_resid;
52*0Sstevel@tonic-gate uintptr_t addr;
53*0Sstevel@tonic-gate size_t len;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /* for short reads/writes, use the on-stack buffer */
56*0Sstevel@tonic-gate if (uiop->uio_resid <= STACK_BUF_SIZE) {
57*0Sstevel@tonic-gate bp = buffer;
58*0Sstevel@tonic-gate allocated = 0;
59*0Sstevel@tonic-gate } else {
60*0Sstevel@tonic-gate bp = kmem_alloc(PAGESIZE, KM_SLEEP);
61*0Sstevel@tonic-gate allocated = 1;
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate #if defined(__sparc)
65*0Sstevel@tonic-gate if (p == curproc)
66*0Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL);
67*0Sstevel@tonic-gate #endif
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate switch (rw) {
70*0Sstevel@tonic-gate case UIO_READ:
71*0Sstevel@tonic-gate while (uiop->uio_resid != 0) {
72*0Sstevel@tonic-gate addr = uiop->uio_offset;
73*0Sstevel@tonic-gate len = MIN(uiop->uio_resid,
74*0Sstevel@tonic-gate PAGESIZE - (addr & PAGEOFFSET));
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate if ((error = uread(p, bp, len, addr)) != 0 ||
77*0Sstevel@tonic-gate (error = uiomove(bp, len, UIO_READ, uiop)) != 0)
78*0Sstevel@tonic-gate break;
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate * ENXIO indicates that a page didn't exist. If the I/O was
83*0Sstevel@tonic-gate * truncated, return success; otherwise convert the error into
84*0Sstevel@tonic-gate * EIO. When obeying new /proc semantics, we don't return an
85*0Sstevel@tonic-gate * error for a read that begins at an invalid address.
86*0Sstevel@tonic-gate */
87*0Sstevel@tonic-gate if (error == ENXIO) {
88*0Sstevel@tonic-gate if (total != uiop->uio_resid || !old)
89*0Sstevel@tonic-gate error = 0;
90*0Sstevel@tonic-gate else
91*0Sstevel@tonic-gate error = EIO;
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate break;
94*0Sstevel@tonic-gate case UIO_WRITE:
95*0Sstevel@tonic-gate while (uiop->uio_resid != 0) {
96*0Sstevel@tonic-gate addr = uiop->uio_offset;
97*0Sstevel@tonic-gate len = MIN(uiop->uio_resid,
98*0Sstevel@tonic-gate PAGESIZE - (addr & PAGEOFFSET));
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate if ((error = uiomove(bp, len, UIO_WRITE, uiop)) != 0)
101*0Sstevel@tonic-gate break;
102*0Sstevel@tonic-gate if ((error = uwrite(p, bp, len, addr)) != 0) {
103*0Sstevel@tonic-gate uiop->uio_resid += len;
104*0Sstevel@tonic-gate uiop->uio_loffset -= len;
105*0Sstevel@tonic-gate break;
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate * ENXIO indicates that a page didn't exist. If the I/O was
111*0Sstevel@tonic-gate * truncated, return success; otherwise convert the error
112*0Sstevel@tonic-gate * into EIO.
113*0Sstevel@tonic-gate */
114*0Sstevel@tonic-gate if (error == ENXIO) {
115*0Sstevel@tonic-gate if (total != uiop->uio_resid)
116*0Sstevel@tonic-gate error = 0;
117*0Sstevel@tonic-gate else
118*0Sstevel@tonic-gate error = EIO;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate break;
121*0Sstevel@tonic-gate default:
122*0Sstevel@tonic-gate panic("prusrio: rw=%d neither UIO_READ not UIO_WRITE", rw);
123*0Sstevel@tonic-gate /*NOTREACHED*/
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if (allocated)
127*0Sstevel@tonic-gate kmem_free(bp, PAGESIZE);
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate return (error);
130*0Sstevel@tonic-gate }
131