1139735Simp/*-
2129198Scognet * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3129198Scognet * Copyright (c) 1982, 1986, 1991, 1993
4129198Scognet *	The Regents of the University of California.  All rights reserved.
5129198Scognet * (c) UNIX System Laboratories, Inc.
6129198Scognet * All or some portions of this file are derived from material licensed
7129198Scognet * to the University of California by American Telephone and Telegraph
8129198Scognet * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9129198Scognet * the permission of UNIX System Laboratories, Inc.
10129198Scognet *
11129198Scognet * Redistribution and use in source and binary forms, with or without
12129198Scognet * modification, are permitted provided that the following conditions
13129198Scognet * are met:
14129198Scognet * 1. Redistributions of source code must retain the above copyright
15129198Scognet *    notice, this list of conditions and the following disclaimer.
16129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
17129198Scognet *    notice, this list of conditions and the following disclaimer in the
18129198Scognet *    documentation and/or other materials provided with the distribution.
19263059Simp * 3. Neither the name of the University nor the names of its contributors
20129198Scognet *    may be used to endorse or promote products derived from this software
21129198Scognet *    without specific prior written permission.
22129198Scognet *
23129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33129198Scognet * SUCH DAMAGE.
34129198Scognet *
35129198Scognet *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36129198Scognet */
37129198Scognet
38129198Scognet#include <sys/cdefs.h>
39129198Scognet__FBSDID("$FreeBSD$");
40129198Scognet
41129198Scognet#include <sys/param.h>
42129198Scognet#include <sys/kernel.h>
43129198Scognet#include <sys/lock.h>
44129198Scognet#include <sys/mutex.h>
45129198Scognet#include <sys/proc.h>
46129198Scognet#include <sys/systm.h>
47129198Scognet#include <sys/uio.h>
48135658Scognet#include <sys/sf_buf.h>
49129198Scognet
50129198Scognet#include <vm/vm.h>
51129198Scognet#include <vm/vm_page.h>
52129198Scognet
53129198Scognet#include <machine/vmparam.h>
54129198Scognet
55129198Scognet/*
56135658Scognet * Implement uiomove(9) from physical memory using sf_bufs to
57129198Scognet * avoid the creation and destruction of ephemeral mappings.
58129198Scognet */
59129198Scognetint
60129198Scognetuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
61129198Scognet{
62129198Scognet	struct thread *td = curthread;
63129198Scognet	struct iovec *iov;
64129198Scognet	void *cp;
65129198Scognet	vm_offset_t page_offset;
66129198Scognet	size_t cnt;
67129198Scognet	int error = 0;
68129198Scognet	int save = 0;
69135658Scognet	struct sf_buf *sf;
70129198Scognet
71129198Scognet	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
72129198Scognet	    ("uiomove_fromphys: mode"));
73129198Scognet	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
74129198Scognet	    ("uiomove_fromphys proc"));
75130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
76130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
77129198Scognet	while (n > 0 && uio->uio_resid) {
78129198Scognet		iov = uio->uio_iov;
79129198Scognet		cnt = iov->iov_len;
80129198Scognet		if (cnt == 0) {
81129198Scognet			uio->uio_iov++;
82129198Scognet			uio->uio_iovcnt--;
83129198Scognet			continue;
84129198Scognet		}
85129198Scognet		if (cnt > n)
86129198Scognet			cnt = n;
87129198Scognet		page_offset = offset & PAGE_MASK;
88129198Scognet		cnt = min(cnt, PAGE_SIZE - page_offset);
89135658Scognet		sf = sf_buf_alloc(ma[offset >> PAGE_SHIFT], 0);
90135658Scognet		cp = (char*)sf_buf_kva(sf) + page_offset;
91129198Scognet		switch (uio->uio_segflg) {
92129198Scognet		case UIO_USERSPACE:
93218195Smdf			maybe_yield();
94129198Scognet			if (uio->uio_rw == UIO_READ)
95129198Scognet				error = copyout(cp, iov->iov_base, cnt);
96129198Scognet			else
97129198Scognet				error = copyin(iov->iov_base, cp, cnt);
98176886Scognet			if (error) {
99176886Scognet				sf_buf_free(sf);
100129198Scognet				goto out;
101176886Scognet			}
102129198Scognet			break;
103129198Scognet		case UIO_SYSSPACE:
104129198Scognet			if (uio->uio_rw == UIO_READ)
105129198Scognet				bcopy(cp, iov->iov_base, cnt);
106129198Scognet			else
107129198Scognet				bcopy(iov->iov_base, cp, cnt);
108129198Scognet			break;
109129198Scognet		case UIO_NOCOPY:
110129198Scognet			break;
111129198Scognet		}
112135658Scognet		sf_buf_free(sf);
113129198Scognet		iov->iov_base = (char *)iov->iov_base + cnt;
114129198Scognet		iov->iov_len -= cnt;
115129198Scognet		uio->uio_resid -= cnt;
116129198Scognet		uio->uio_offset += cnt;
117129198Scognet		offset += cnt;
118129198Scognet		n -= cnt;
119129198Scognet	}
120129198Scognetout:
121130028Stjr	if (save == 0)
122130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
123129198Scognet	return (error);
124129198Scognet}
125