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