1139724Simp/*-
2127282Salc * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3127282Salc * Copyright (c) 1982, 1986, 1991, 1993
4127282Salc *	The Regents of the University of California.  All rights reserved.
5127282Salc * (c) UNIX System Laboratories, Inc.
6127282Salc * All or some portions of this file are derived from material licensed
7127282Salc * to the University of California by American Telephone and Telegraph
8127282Salc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9127282Salc * the permission of UNIX System Laboratories, Inc.
10127282Salc *
11127282Salc * Redistribution and use in source and binary forms, with or without
12127282Salc * modification, are permitted provided that the following conditions
13127282Salc * are met:
14127282Salc * 1. Redistributions of source code must retain the above copyright
15127282Salc *    notice, this list of conditions and the following disclaimer.
16127282Salc * 2. Redistributions in binary form must reproduce the above copyright
17127282Salc *    notice, this list of conditions and the following disclaimer in the
18127282Salc *    documentation and/or other materials provided with the distribution.
19266312Sian * 3. Neither the name of the University nor the names of its contributors
20127282Salc *    may be used to endorse or promote products derived from this software
21127282Salc *    without specific prior written permission.
22127282Salc *
23127282Salc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24127282Salc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25127282Salc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26127282Salc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27127282Salc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28127282Salc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29127282Salc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30127282Salc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31127282Salc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32127282Salc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33127282Salc * SUCH DAMAGE.
34127282Salc *
35127282Salc *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36127282Salc */
37127282Salc
38127282Salc#include <sys/cdefs.h>
39127282Salc__FBSDID("$FreeBSD$");
40127282Salc
41127282Salc#include <sys/param.h>
42253367Sae#include <sys/systm.h>
43127282Salc#include <sys/kernel.h>
44127282Salc#include <sys/lock.h>
45127282Salc#include <sys/mutex.h>
46127282Salc#include <sys/proc.h>
47141848Salc#include <sys/sched.h>
48127282Salc#include <sys/sf_buf.h>
49127282Salc#include <sys/uio.h>
50127282Salc
51127282Salc#include <vm/vm.h>
52127282Salc#include <vm/vm_page.h>
53127282Salc
54127282Salc/*
55127282Salc * Implement uiomove(9) from physical memory using sf_bufs to reduce
56127282Salc * the creation and destruction of ephemeral mappings.
57127282Salc */
58127282Salcint
59127282Salcuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
60127282Salc{
61127282Salc	struct sf_buf *sf;
62127282Salc	struct thread *td = curthread;
63127282Salc	struct iovec *iov;
64127282Salc	void *cp;
65127282Salc	vm_offset_t page_offset;
66127282Salc	size_t cnt;
67127282Salc	int error = 0;
68127282Salc	int save = 0;
69127282Salc
70127282Salc	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
71127282Salc	    ("uiomove_fromphys: mode"));
72127282Salc	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
73127282Salc	    ("uiomove_fromphys proc"));
74130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
75130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
76127282Salc	while (n > 0 && uio->uio_resid) {
77127282Salc		iov = uio->uio_iov;
78127282Salc		cnt = iov->iov_len;
79127282Salc		if (cnt == 0) {
80127282Salc			uio->uio_iov++;
81127282Salc			uio->uio_iovcnt--;
82127282Salc			continue;
83127282Salc		}
84127282Salc		if (cnt > n)
85127282Salc			cnt = n;
86127282Salc		page_offset = offset & PAGE_MASK;
87127282Salc		cnt = min(cnt, PAGE_SIZE - page_offset);
88141848Salc		sched_pin();
89141848Salc		sf = sf_buf_alloc(ma[offset >> PAGE_SHIFT], SFB_CPUPRIVATE);
90127282Salc		cp = (char *)sf_buf_kva(sf) + page_offset;
91127282Salc		switch (uio->uio_segflg) {
92127282Salc		case UIO_USERSPACE:
93218195Smdf			maybe_yield();
94127282Salc			if (uio->uio_rw == UIO_READ)
95127282Salc				error = copyout(cp, iov->iov_base, cnt);
96127282Salc			else
97127282Salc				error = copyin(iov->iov_base, cp, cnt);
98134127Salc			if (error) {
99134127Salc				sf_buf_free(sf);
100141848Salc				sched_unpin();
101127282Salc				goto out;
102134127Salc			}
103127282Salc			break;
104127282Salc		case UIO_SYSSPACE:
105127282Salc			if (uio->uio_rw == UIO_READ)
106127282Salc				bcopy(cp, iov->iov_base, cnt);
107127282Salc			else
108127282Salc				bcopy(iov->iov_base, cp, cnt);
109127282Salc			break;
110127282Salc		case UIO_NOCOPY:
111127282Salc			break;
112127282Salc		}
113127282Salc		sf_buf_free(sf);
114141848Salc		sched_unpin();
115127282Salc		iov->iov_base = (char *)iov->iov_base + cnt;
116127282Salc		iov->iov_len -= cnt;
117127282Salc		uio->uio_resid -= cnt;
118127282Salc		uio->uio_offset += cnt;
119127282Salc		offset += cnt;
120127282Salc		n -= cnt;
121127282Salc	}
122127282Salcout:
123130028Stjr	if (save == 0)
124130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
125127282Salc	return (error);
126127282Salc}
127