xref: /illumos-gate/usr/src/uts/common/fs/proc/prusrio.c (revision 2d6eb4a5)
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 #include <sys/types.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
36*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	STACK_BUF_SIZE	64	/* power of 2 */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate int
prusrio(proc_t * p,enum uio_rw rw,struct uio * uiop,int old)42*7c478bd9Sstevel@tonic-gate prusrio(proc_t *p, enum uio_rw rw, struct uio *uiop, int old)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	/* longlong-aligned short buffer */
45*7c478bd9Sstevel@tonic-gate 	longlong_t buffer[STACK_BUF_SIZE / sizeof (longlong_t)];
46*7c478bd9Sstevel@tonic-gate 	int error = 0;
47*7c478bd9Sstevel@tonic-gate 	void *bp;
48*7c478bd9Sstevel@tonic-gate 	int allocated;
49*7c478bd9Sstevel@tonic-gate 	ssize_t total = uiop->uio_resid;
50*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
51*7c478bd9Sstevel@tonic-gate 	size_t len;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	/* for short reads/writes, use the on-stack buffer */
54*7c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid <= STACK_BUF_SIZE) {
55*7c478bd9Sstevel@tonic-gate 		bp = buffer;
56*7c478bd9Sstevel@tonic-gate 		allocated = 0;
57*7c478bd9Sstevel@tonic-gate 	} else {
58*7c478bd9Sstevel@tonic-gate 		bp = kmem_alloc(PAGESIZE, KM_SLEEP);
59*7c478bd9Sstevel@tonic-gate 		allocated = 1;
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
63*7c478bd9Sstevel@tonic-gate 	if (p == curproc)
64*7c478bd9Sstevel@tonic-gate 		(void) flush_user_windows_to_stack(NULL);
65*7c478bd9Sstevel@tonic-gate #endif
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	switch (rw) {
68*7c478bd9Sstevel@tonic-gate 	case UIO_READ:
69*7c478bd9Sstevel@tonic-gate 		while (uiop->uio_resid != 0) {
70*7c478bd9Sstevel@tonic-gate 			addr = uiop->uio_offset;
71*7c478bd9Sstevel@tonic-gate 			len = MIN(uiop->uio_resid,
72*7c478bd9Sstevel@tonic-gate 			    PAGESIZE - (addr & PAGEOFFSET));
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 			if ((error = uread(p, bp, len, addr)) != 0 ||
75*7c478bd9Sstevel@tonic-gate 			    (error = uiomove(bp, len, UIO_READ, uiop)) != 0)
76*7c478bd9Sstevel@tonic-gate 				break;
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 		/*
80*7c478bd9Sstevel@tonic-gate 		 * ENXIO indicates that a page didn't exist. If the I/O was
81*7c478bd9Sstevel@tonic-gate 		 * truncated, return success; otherwise convert the error into
82*7c478bd9Sstevel@tonic-gate 		 * EIO. When obeying new /proc semantics, we don't return an
83*7c478bd9Sstevel@tonic-gate 		 * error for a read that begins at an invalid address.
84*7c478bd9Sstevel@tonic-gate 		 */
85*7c478bd9Sstevel@tonic-gate 		if (error == ENXIO) {
86*7c478bd9Sstevel@tonic-gate 			if (total != uiop->uio_resid || !old)
87*7c478bd9Sstevel@tonic-gate 				error = 0;
88*7c478bd9Sstevel@tonic-gate 			else
89*7c478bd9Sstevel@tonic-gate 				error = EIO;
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 		break;
92*7c478bd9Sstevel@tonic-gate 	case UIO_WRITE:
93*7c478bd9Sstevel@tonic-gate 		while (uiop->uio_resid != 0) {
94*7c478bd9Sstevel@tonic-gate 			addr = uiop->uio_offset;
95*7c478bd9Sstevel@tonic-gate 			len = MIN(uiop->uio_resid,
96*7c478bd9Sstevel@tonic-gate 			    PAGESIZE - (addr & PAGEOFFSET));
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 			if ((error = uiomove(bp, len, UIO_WRITE, uiop)) != 0)
99*7c478bd9Sstevel@tonic-gate 				break;
100*7c478bd9Sstevel@tonic-gate 			if ((error = uwrite(p, bp, len, addr)) != 0) {
101*7c478bd9Sstevel@tonic-gate 				uiop->uio_resid += len;
102*7c478bd9Sstevel@tonic-gate 				uiop->uio_loffset -= len;
103*7c478bd9Sstevel@tonic-gate 				break;
104*7c478bd9Sstevel@tonic-gate 			}
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		/*
108*7c478bd9Sstevel@tonic-gate 		 * ENXIO indicates that a page didn't exist. If the I/O was
109*7c478bd9Sstevel@tonic-gate 		 * truncated, return success; otherwise convert the error
110*7c478bd9Sstevel@tonic-gate 		 * into EIO.
111*7c478bd9Sstevel@tonic-gate 		 */
112*7c478bd9Sstevel@tonic-gate 		if (error == ENXIO) {
113*7c478bd9Sstevel@tonic-gate 			if (total != uiop->uio_resid)
114*7c478bd9Sstevel@tonic-gate 				error = 0;
115*7c478bd9Sstevel@tonic-gate 			else
116*7c478bd9Sstevel@tonic-gate 				error = EIO;
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		break;
119*7c478bd9Sstevel@tonic-gate 	default:
120*7c478bd9Sstevel@tonic-gate 		panic("prusrio: rw=%d neither UIO_READ not UIO_WRITE", rw);
121*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (allocated)
125*7c478bd9Sstevel@tonic-gate 		kmem_free(bp, PAGESIZE);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	return (error);
128*7c478bd9Sstevel@tonic-gate }
129