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.
19129198Scognet * 3. All advertising materials mentioning features or use of this software
20129198Scognet *    must display the following acknowledgement:
21129198Scognet *	This product includes software developed by the University of
22129198Scognet *	California, Berkeley and its contributors.
23129198Scognet * 4. Neither the name of the University nor the names of its contributors
24129198Scognet *    may be used to endorse or promote products derived from this software
25129198Scognet *    without specific prior written permission.
26129198Scognet *
27129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37129198Scognet * SUCH DAMAGE.
38129198Scognet *
39129198Scognet *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
40129198Scognet */
41129198Scognet
42129198Scognet#include <sys/cdefs.h>
43129198Scognet__FBSDID("$FreeBSD$");
44129198Scognet
45129198Scognet#include <sys/param.h>
46129198Scognet#include <sys/kernel.h>
47129198Scognet#include <sys/lock.h>
48129198Scognet#include <sys/mutex.h>
49129198Scognet#include <sys/proc.h>
50129198Scognet#include <sys/systm.h>
51129198Scognet#include <sys/uio.h>
52135658Scognet#include <sys/sf_buf.h>
53129198Scognet
54129198Scognet#include <vm/vm.h>
55129198Scognet#include <vm/vm_page.h>
56129198Scognet
57129198Scognet#include <machine/vmparam.h>
58129198Scognet
59129198Scognet/*
60135658Scognet * Implement uiomove(9) from physical memory using sf_bufs to
61129198Scognet * avoid the creation and destruction of ephemeral mappings.
62129198Scognet */
63129198Scognetint
64129198Scognetuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
65129198Scognet{
66129198Scognet	struct thread *td = curthread;
67129198Scognet	struct iovec *iov;
68129198Scognet	void *cp;
69129198Scognet	vm_offset_t page_offset;
70129198Scognet	size_t cnt;
71129198Scognet	int error = 0;
72129198Scognet	int save = 0;
73135658Scognet	struct sf_buf *sf;
74129198Scognet
75129198Scognet	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
76129198Scognet	    ("uiomove_fromphys: mode"));
77129198Scognet	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
78129198Scognet	    ("uiomove_fromphys proc"));
79130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
80130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
81129198Scognet	while (n > 0 && uio->uio_resid) {
82129198Scognet		iov = uio->uio_iov;
83129198Scognet		cnt = iov->iov_len;
84129198Scognet		if (cnt == 0) {
85129198Scognet			uio->uio_iov++;
86129198Scognet			uio->uio_iovcnt--;
87129198Scognet			continue;
88129198Scognet		}
89129198Scognet		if (cnt > n)
90129198Scognet			cnt = n;
91129198Scognet		page_offset = offset & PAGE_MASK;
92129198Scognet		cnt = min(cnt, PAGE_SIZE - page_offset);
93135658Scognet		sf = sf_buf_alloc(ma[offset >> PAGE_SHIFT], 0);
94135658Scognet		cp = (char*)sf_buf_kva(sf) + page_offset;
95129198Scognet		switch (uio->uio_segflg) {
96129198Scognet		case UIO_USERSPACE:
97218195Smdf			maybe_yield();
98129198Scognet			if (uio->uio_rw == UIO_READ)
99129198Scognet				error = copyout(cp, iov->iov_base, cnt);
100129198Scognet			else
101129198Scognet				error = copyin(iov->iov_base, cp, cnt);
102176886Scognet			if (error) {
103176886Scognet				sf_buf_free(sf);
104129198Scognet				goto out;
105176886Scognet			}
106129198Scognet			break;
107129198Scognet		case UIO_SYSSPACE:
108129198Scognet			if (uio->uio_rw == UIO_READ)
109129198Scognet				bcopy(cp, iov->iov_base, cnt);
110129198Scognet			else
111129198Scognet				bcopy(iov->iov_base, cp, cnt);
112129198Scognet			break;
113129198Scognet		case UIO_NOCOPY:
114129198Scognet			break;
115129198Scognet		}
116135658Scognet		sf_buf_free(sf);
117129198Scognet		iov->iov_base = (char *)iov->iov_base + cnt;
118129198Scognet		iov->iov_len -= cnt;
119129198Scognet		uio->uio_resid -= cnt;
120129198Scognet		uio->uio_offset += cnt;
121129198Scognet		offset += cnt;
122129198Scognet		n -= cnt;
123129198Scognet	}
124129198Scognetout:
125130028Stjr	if (save == 0)
126130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
127129198Scognet	return (error);
128129198Scognet}
129