uio_machdep.c revision 130028
1127236Salc/*
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.
19127236Salc * 4. 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: head/sys/amd64/amd64/uio_machdep.c 130028 2004-06-03 10:22:47Z tjr $");
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;
64127236Salc	vm_offset_t page_offset;
65127236Salc	size_t cnt;
66127236Salc	int error = 0;
67127236Salc	int save = 0;
68127236Salc
69127236Salc	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
70127236Salc	    ("uiomove_fromphys: mode"));
71127236Salc	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
72127236Salc	    ("uiomove_fromphys proc"));
73130028Stjr	save = td->td_pflags & TDP_DEADLKTREAT;
74130028Stjr	td->td_pflags |= TDP_DEADLKTREAT;
75127236Salc	while (n > 0 && uio->uio_resid) {
76127236Salc		iov = uio->uio_iov;
77127236Salc		cnt = iov->iov_len;
78127236Salc		if (cnt == 0) {
79127236Salc			uio->uio_iov++;
80127236Salc			uio->uio_iovcnt--;
81127236Salc			continue;
82127236Salc		}
83127236Salc		if (cnt > n)
84127236Salc			cnt = n;
85127236Salc		page_offset = offset & PAGE_MASK;
86127236Salc		cnt = min(cnt, PAGE_SIZE - page_offset);
87127236Salc		cp = (char *)PHYS_TO_DMAP(ma[offset >> PAGE_SHIFT]->phys_addr) +
88127236Salc		    page_offset;
89127236Salc		switch (uio->uio_segflg) {
90127236Salc		case UIO_USERSPACE:
91127236Salc			if (ticks - PCPU_GET(switchticks) >= hogticks)
92127236Salc				uio_yield();
93127236Salc			if (uio->uio_rw == UIO_READ)
94127236Salc				error = copyout(cp, iov->iov_base, cnt);
95127236Salc			else
96127236Salc				error = copyin(iov->iov_base, cp, cnt);
97127236Salc			if (error)
98127236Salc				goto out;
99127236Salc			break;
100127236Salc		case UIO_SYSSPACE:
101127236Salc			if (uio->uio_rw == UIO_READ)
102127236Salc				bcopy(cp, iov->iov_base, cnt);
103127236Salc			else
104127236Salc				bcopy(iov->iov_base, cp, cnt);
105127236Salc			break;
106127236Salc		case UIO_NOCOPY:
107127236Salc			break;
108127236Salc		}
109127236Salc		iov->iov_base = (char *)iov->iov_base + cnt;
110127236Salc		iov->iov_len -= cnt;
111127236Salc		uio->uio_resid -= cnt;
112127236Salc		uio->uio_offset += cnt;
113127236Salc		offset += cnt;
114127236Salc		n -= cnt;
115127236Salc	}
116127236Salcout:
117130028Stjr	if (save == 0)
118130023Stjr		td->td_pflags &= ~TDP_DEADLKTREAT;
119127236Salc	return (error);
120127236Salc}
121