1139731Simp/*-
2127236Salc * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3127236Salc * Copyright (c) 1982, 1986, 1991, 1993
4127236Salc *	The Regents of the University of California.  All rights reserved.
5127236Salc * (c) UNIX System Laboratories, Inc.
6127236Salc * All or some portions of this file are derived from material licensed
7127236Salc * to the University of California by American Telephone and Telegraph
8127236Salc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9127236Salc * the permission of UNIX System Laboratories, Inc.
10127236Salc *
11127236Salc * Redistribution and use in source and binary forms, with or without
12127236Salc * modification, are permitted provided that the following conditions
13127236Salc * are met:
14127236Salc * 1. Redistributions of source code must retain the above copyright
15127236Salc *    notice, this list of conditions and the following disclaimer.
16127236Salc * 2. Redistributions in binary form must reproduce the above copyright
17127236Salc *    notice, this list of conditions and the following disclaimer in the
18127236Salc *    documentation and/or other materials provided with the distribution.
19263059Simp * 3. Neither the name of the University nor the names of its contributors
20127236Salc *    may be used to endorse or promote products derived from this software
21127236Salc *    without specific prior written permission.
22127236Salc *
23127236Salc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24127236Salc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25127236Salc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26127236Salc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27127236Salc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28127236Salc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29127236Salc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30127236Salc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31127236Salc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32127236Salc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33127236Salc * SUCH DAMAGE.
34127236Salc *
35127236Salc *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36127236Salc */
37127236Salc
38127236Salc#include <sys/cdefs.h>
39127236Salc__FBSDID("$FreeBSD$");
40127236Salc
41127236Salc#include <sys/param.h>
42127236Salc#include <sys/kernel.h>
43127236Salc#include <sys/lock.h>
44127236Salc#include <sys/mutex.h>
45127236Salc#include <sys/proc.h>
46127236Salc#include <sys/systm.h>
47127236Salc#include <sys/uio.h>
48127236Salc
49127236Salc#include <vm/vm.h>
50127236Salc#include <vm/vm_page.h>
51127236Salc
52127236Salc#include <machine/vmparam.h>
53127236Salc
54127236Salc/*
55127236Salc * Implement uiomove(9) from physical memory using the direct map to
56127241Salc * avoid the creation and destruction of ephemeral mappings.
57127236Salc */
58127236Salcint
59127236Salcuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
60127236Salc{
61127236Salc	struct thread *td = curthread;
62127236Salc	struct iovec *iov;
63127236Salc	void *cp;
64273582Sroyger	vm_offset_t page_offset, vaddr;
65127236Salc	size_t cnt;
66127236Salc	int error = 0;
67127236Salc	int save = 0;
68273582Sroyger	boolean_t mapped;
69127236Salc
70127236Salc	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
71127236Salc	    ("uiomove_fromphys: mode"));
72127236Salc	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
73127236Salc	    ("uiomove_fromphys proc"));
74130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
75130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
76273582Sroyger	mapped = FALSE;
77127236Salc	while (n > 0 && uio->uio_resid) {
78127236Salc		iov = uio->uio_iov;
79127236Salc		cnt = iov->iov_len;
80127236Salc		if (cnt == 0) {
81127236Salc			uio->uio_iov++;
82127236Salc			uio->uio_iovcnt--;
83127236Salc			continue;
84127236Salc		}
85127236Salc		if (cnt > n)
86127236Salc			cnt = n;
87127236Salc		page_offset = offset & PAGE_MASK;
88127236Salc		cnt = min(cnt, PAGE_SIZE - page_offset);
89273582Sroyger		if (uio->uio_segflg != UIO_NOCOPY) {
90273582Sroyger			mapped = pmap_map_io_transient(
91273582Sroyger			    &ma[offset >> PAGE_SHIFT], &vaddr, 1, TRUE);
92273582Sroyger			cp = (char *)vaddr + page_offset;
93273582Sroyger		}
94127236Salc		switch (uio->uio_segflg) {
95127236Salc		case UIO_USERSPACE:
96218195Smdf			maybe_yield();
97127236Salc			if (uio->uio_rw == UIO_READ)
98127236Salc				error = copyout(cp, iov->iov_base, cnt);
99127236Salc			else
100127236Salc				error = copyin(iov->iov_base, cp, cnt);
101127236Salc			if (error)
102127236Salc				goto out;
103127236Salc			break;
104127236Salc		case UIO_SYSSPACE:
105127236Salc			if (uio->uio_rw == UIO_READ)
106127236Salc				bcopy(cp, iov->iov_base, cnt);
107127236Salc			else
108127236Salc				bcopy(iov->iov_base, cp, cnt);
109127236Salc			break;
110127236Salc		case UIO_NOCOPY:
111127236Salc			break;
112127236Salc		}
113273582Sroyger		if (__predict_false(mapped)) {
114273582Sroyger			pmap_unmap_io_transient(&ma[offset >> PAGE_SHIFT],
115273582Sroyger			    &vaddr, 1, TRUE);
116273582Sroyger			mapped = FALSE;
117273582Sroyger		}
118127236Salc		iov->iov_base = (char *)iov->iov_base + cnt;
119127236Salc		iov->iov_len -= cnt;
120127236Salc		uio->uio_resid -= cnt;
121127236Salc		uio->uio_offset += cnt;
122127236Salc		offset += cnt;
123127236Salc		n -= cnt;
124127236Salc	}
125127236Salcout:
126273582Sroyger	if (__predict_false(mapped))
127273582Sroyger		pmap_unmap_io_transient(&ma[offset >> PAGE_SHIFT], &vaddr, 1,
128273582Sroyger		    TRUE);
129130028Stjr	if (save == 0)
130130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
131127236Salc	return (error);
132127236Salc}
133