#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
int
prusrio(proc_t *p, enum uio_rw rw, struct uio *uiop, int old)
{
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;
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;
}
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;
}
}
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);
}
if (allocated)
kmem_free(bp, PAGESIZE);
return (error);
}