1139790Simp/*-
2127241Salc * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3127241Salc * Copyright (c) 1982, 1986, 1991, 1993
4127241Salc *	The Regents of the University of California.  All rights reserved.
5127241Salc * (c) UNIX System Laboratories, Inc.
6127241Salc * All or some portions of this file are derived from material licensed
7127241Salc * to the University of California by American Telephone and Telegraph
8127241Salc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9127241Salc * the permission of UNIX System Laboratories, Inc.
10127241Salc *
11127241Salc * Redistribution and use in source and binary forms, with or without
12127241Salc * modification, are permitted provided that the following conditions
13127241Salc * are met:
14127241Salc * 1. Redistributions of source code must retain the above copyright
15127241Salc *    notice, this list of conditions and the following disclaimer.
16127241Salc * 2. Redistributions in binary form must reproduce the above copyright
17127241Salc *    notice, this list of conditions and the following disclaimer in the
18127241Salc *    documentation and/or other materials provided with the distribution.
19127241Salc * 4. Neither the name of the University nor the names of its contributors
20127241Salc *    may be used to endorse or promote products derived from this software
21127241Salc *    without specific prior written permission.
22127241Salc *
23127241Salc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24127241Salc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25127241Salc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26127241Salc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27127241Salc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28127241Salc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29127241Salc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30127241Salc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31127241Salc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32127241Salc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33127241Salc * SUCH DAMAGE.
34127241Salc *
35127241Salc *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36127241Salc */
37127241Salc
38127241Salc#include <sys/cdefs.h>
39127241Salc__FBSDID("$FreeBSD$");
40127241Salc
41127241Salc#include <sys/param.h>
42127241Salc#include <sys/kernel.h>
43127241Salc#include <sys/lock.h>
44127241Salc#include <sys/mutex.h>
45127241Salc#include <sys/proc.h>
46127241Salc#include <sys/systm.h>
47127241Salc#include <sys/uio.h>
48127241Salc
49127241Salc#include <vm/vm.h>
50127241Salc#include <vm/vm_page.h>
51127241Salc
52127241Salc#include <machine/vmparam.h>
53127241Salc
54127241Salc/*
55127241Salc * Implement uiomove(9) from physical memory using the direct map to
56127241Salc * avoid the creation and destruction of ephemeral mappings.
57127241Salc */
58127241Salcint
59127241Salcuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
60127241Salc{
61127241Salc	struct thread *td = curthread;
62127241Salc	struct iovec *iov;
63127241Salc	void *cp;
64127241Salc	vm_offset_t page_offset;
65127241Salc	size_t cnt;
66127241Salc	int error = 0;
67127241Salc	int save = 0;
68127241Salc
69127241Salc	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
70127241Salc	    ("uiomove_fromphys: mode"));
71127241Salc	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
72127241Salc	    ("uiomove_fromphys proc"));
73130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
74130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
75127241Salc	while (n > 0 && uio->uio_resid) {
76127241Salc		iov = uio->uio_iov;
77127241Salc		cnt = iov->iov_len;
78127241Salc		if (cnt == 0) {
79127241Salc			uio->uio_iov++;
80127241Salc			uio->uio_iovcnt--;
81127241Salc			continue;
82127241Salc		}
83127241Salc		if (cnt > n)
84127241Salc			cnt = n;
85127241Salc		page_offset = offset & PAGE_MASK;
86127241Salc		cnt = min(cnt, PAGE_SIZE - page_offset);
87127241Salc		cp = (char *)
88127241Salc		    IA64_PHYS_TO_RR7(ma[offset >> PAGE_SHIFT]->phys_addr) +
89127241Salc		    page_offset;
90127241Salc		switch (uio->uio_segflg) {
91127241Salc		case UIO_USERSPACE:
92218195Smdf			maybe_yield();
93127241Salc			if (uio->uio_rw == UIO_READ)
94127241Salc				error = copyout(cp, iov->iov_base, cnt);
95127241Salc			else
96127241Salc				error = copyin(iov->iov_base, cp, cnt);
97127241Salc			if (error)
98127241Salc				goto out;
99127241Salc			break;
100127241Salc		case UIO_SYSSPACE:
101127241Salc			if (uio->uio_rw == UIO_READ)
102127241Salc				bcopy(cp, iov->iov_base, cnt);
103127241Salc			else
104127241Salc				bcopy(iov->iov_base, cp, cnt);
105127241Salc			break;
106127241Salc		case UIO_NOCOPY:
107127241Salc			break;
108127241Salc		}
109127241Salc		iov->iov_base = (char *)iov->iov_base + cnt;
110127241Salc		iov->iov_len -= cnt;
111127241Salc		uio->uio_resid -= cnt;
112127241Salc		uio->uio_offset += cnt;
113127241Salc		offset += cnt;
114127241Salc		n -= cnt;
115127241Salc	}
116127241Salcout:
117130028Stjr	if (save == 0)
118130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
119127241Salc	return (error);
120127241Salc}
121