|
root / base / usr / src / uts / common / fs / proc / prusrio.c
prusrio.c C 129 lines 3.1 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/procfs.h>
#include <sys/sysmacros.h>
#include <sys/uio.h>
#include <sys/cmn_err.h>

#if defined(__sparc)
#include <sys/stack.h>
#endif

#define	STACK_BUF_SIZE	64	/* power of 2 */

int
prusrio(proc_t *p, enum uio_rw rw, struct uio *uiop, int old)
{
	/* longlong-aligned short buffer */
	longlong_t buffer[STACK_BUF_SIZE / sizeof (longlong_t)];
	int error = 0;
	void *bp;
	int allocated;
	ssize_t total = uiop->uio_resid;
	uintptr_t addr;
	size_t len;

	/* for short reads/writes, use the on-stack buffer */
	if (uiop->uio_resid <= STACK_BUF_SIZE) {
		bp = buffer;
		allocated = 0;
	} else {
		bp = kmem_alloc(PAGESIZE, KM_SLEEP);
		allocated = 1;
	}

#if defined(__sparc)
	if (p == curproc)
		(void) flush_user_windows_to_stack(NULL);
#endif

	switch (rw) {
	case UIO_READ:
		while (uiop->uio_resid != 0) {
			addr = uiop->uio_offset;
			len = MIN(uiop->uio_resid,
			    PAGESIZE - (addr & PAGEOFFSET));

			if ((error = uread(p, bp, len, addr)) != 0 ||
			    (error = uiomove(bp, len, UIO_READ, uiop)) != 0)
				break;
		}

		/*
		 * ENXIO indicates that a page didn't exist. If the I/O was
		 * truncated, return success; otherwise convert the error into
		 * EIO. When obeying new /proc semantics, we don't return an
		 * error for a read that begins at an invalid address.
		 */
		if (error == ENXIO) {
			if (total != uiop->uio_resid || !old)
				error = 0;
			else
				error = EIO;
		}
		break;
	case UIO_WRITE:
		while (uiop->uio_resid != 0) {
			addr = uiop->uio_offset;
			len = MIN(uiop->uio_resid,
			    PAGESIZE - (addr & PAGEOFFSET));

			if ((error = uiomove(bp, len, UIO_WRITE, uiop)) != 0)
				break;
			if ((error = uwrite(p, bp, len, addr)) != 0) {
				uiop->uio_resid += len;
				uiop->uio_loffset -= len;
				break;
			}
		}

		/*
		 * ENXIO indicates that a page didn't exist. If the I/O was
		 * truncated, return success; otherwise convert the error
		 * into EIO.
		 */
		if (error == ENXIO) {
			if (total != uiop->uio_resid)
				error = 0;
			else
				error = EIO;
		}
		break;
	default:
		panic("prusrio: rw=%d neither UIO_READ not UIO_WRITE", rw);
		/*NOTREACHED*/
	}

	if (allocated)
		kmem_free(bp, PAGESIZE);

	return (error);
}