uio_machdep.c revision 267654
1169689Skan/*-
2169689Skan * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3169689Skan * Copyright (c) 1982, 1986, 1991, 1993
4169689Skan *	The Regents of the University of California.  All rights reserved.
5169689Skan * (c) UNIX System Laboratories, Inc.
6169689Skan * All or some portions of this file are derived from material licensed
7169689Skan * to the University of California by American Telephone and Telegraph
8169689Skan * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9169689Skan * the permission of UNIX System Laboratories, Inc.
10169689Skan *
11169689Skan * Redistribution and use in source and binary forms, with or without
12169689Skan * modification, are permitted provided that the following conditions
13169689Skan * are met:
14169689Skan * 1. Redistributions of source code must retain the above copyright
15169689Skan *    notice, this list of conditions and the following disclaimer.
16169689Skan * 2. Redistributions in binary form must reproduce the above copyright
17169689Skan *    notice, this list of conditions and the following disclaimer in the
18169689Skan *    documentation and/or other materials provided with the distribution.
19169689Skan * 4. Neither the name of the University nor the names of its contributors
20169689Skan *    may be used to endorse or promote products derived from this software
21169689Skan *    without specific prior written permission.
22169689Skan *
23169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33169689Skan * SUCH DAMAGE.
34169689Skan *
35169689Skan *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36169689Skan */
37169689Skan
38169689Skan#include <sys/cdefs.h>
39169689Skan__FBSDID("$FreeBSD: releng/9.3/sys/i386/i386/uio_machdep.c 218195 2011-02-02 16:35:10Z mdf $");
40169689Skan
41169689Skan#include <sys/param.h>
42169689Skan#include <sys/kernel.h>
43169689Skan#include <sys/lock.h>
44169689Skan#include <sys/mutex.h>
45169689Skan#include <sys/proc.h>
46169689Skan#include <sys/sched.h>
47169689Skan#include <sys/sf_buf.h>
48169689Skan#include <sys/systm.h>
49169689Skan#include <sys/uio.h>
50169689Skan
51169689Skan#include <vm/vm.h>
52169689Skan#include <vm/vm_page.h>
53169689Skan
54169689Skan/*
55169689Skan * Implement uiomove(9) from physical memory using sf_bufs to reduce
56169689Skan * the creation and destruction of ephemeral mappings.
57169689Skan */
58169689Skanint
59169689Skanuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
60169689Skan{
61169689Skan	struct sf_buf *sf;
62169689Skan	struct thread *td = curthread;
63169689Skan	struct iovec *iov;
64169689Skan	void *cp;
65169689Skan	vm_offset_t page_offset;
66169689Skan	size_t cnt;
67169689Skan	int error = 0;
68169689Skan	int save = 0;
69169689Skan
70169689Skan	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
71169689Skan	    ("uiomove_fromphys: mode"));
72169689Skan	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
73169689Skan	    ("uiomove_fromphys proc"));
74169689Skan	save = td->td_pflags & TDP_DEADLKTREAT;
75169689Skan	td->td_pflags |= TDP_DEADLKTREAT;
76169689Skan	while (n > 0 && uio->uio_resid) {
77169689Skan		iov = uio->uio_iov;
78169689Skan		cnt = iov->iov_len;
79169689Skan		if (cnt == 0) {
80169689Skan			uio->uio_iov++;
81169689Skan			uio->uio_iovcnt--;
82169689Skan			continue;
83169689Skan		}
84169689Skan		if (cnt > n)
85169689Skan			cnt = n;
86169689Skan		page_offset = offset & PAGE_MASK;
87169689Skan		cnt = min(cnt, PAGE_SIZE - page_offset);
88169689Skan		sched_pin();
89169689Skan		sf = sf_buf_alloc(ma[offset >> PAGE_SHIFT], SFB_CPUPRIVATE);
90169689Skan		cp = (char *)sf_buf_kva(sf) + page_offset;
91169689Skan		switch (uio->uio_segflg) {
92169689Skan		case UIO_USERSPACE:
93169689Skan			maybe_yield();
94169689Skan			if (uio->uio_rw == UIO_READ)
95169689Skan				error = copyout(cp, iov->iov_base, cnt);
96169689Skan			else
97169689Skan				error = copyin(iov->iov_base, cp, cnt);
98169689Skan			if (error) {
99169689Skan				sf_buf_free(sf);
100169689Skan				sched_unpin();
101169689Skan				goto out;
102169689Skan			}
103169689Skan			break;
104169689Skan		case UIO_SYSSPACE:
105169689Skan			if (uio->uio_rw == UIO_READ)
106169689Skan				bcopy(cp, iov->iov_base, cnt);
107169689Skan			else
108169689Skan				bcopy(iov->iov_base, cp, cnt);
109169689Skan			break;
110169689Skan		case UIO_NOCOPY:
111169689Skan			break;
112169689Skan		}
113169689Skan		sf_buf_free(sf);
114169689Skan		sched_unpin();
115169689Skan		iov->iov_base = (char *)iov->iov_base + cnt;
116169689Skan		iov->iov_len -= cnt;
117169689Skan		uio->uio_resid -= cnt;
118169689Skan		uio->uio_offset += cnt;
119169689Skan		offset += cnt;
120169689Skan		n -= cnt;
121169689Skan	}
122169689Skanout:
123169689Skan	if (save == 0)
124169689Skan		td->td_pflags &= ~TDP_DEADLKTREAT;
125169689Skan	return (error);
126169689Skan}
127169689Skan