1178172Simp/*-
2178172Simp * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
3178172Simp * Copyright (c) 1982, 1986, 1991, 1993
4178172Simp *	The Regents of the University of California.  All rights reserved.
5178172Simp * (c) UNIX System Laboratories, Inc.
6178172Simp * All or some portions of this file are derived from material licensed
7178172Simp * to the University of California by American Telephone and Telegraph
8178172Simp * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9178172Simp * the permission of UNIX System Laboratories, Inc.
10178172Simp *
11178172Simp * Redistribution and use in source and binary forms, with or without
12178172Simp * modification, are permitted provided that the following conditions
13178172Simp * are met:
14178172Simp * 1. Redistributions of source code must retain the above copyright
15178172Simp *    notice, this list of conditions and the following disclaimer.
16178172Simp * 2. Redistributions in binary form must reproduce the above copyright
17178172Simp *    notice, this list of conditions and the following disclaimer in the
18178172Simp *    documentation and/or other materials provided with the distribution.
19266312Sian * 3. Neither the name of the University nor the names of its contributors
20178172Simp *    may be used to endorse or promote products derived from this software
21178172Simp *    without specific prior written permission.
22178172Simp *
23178172Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24178172Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25178172Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26178172Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27178172Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33178172Simp * SUCH DAMAGE.
34178172Simp *
35206714Sjmallett *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
36178172Simp */
37178172Simp
38178172Simp#include <sys/cdefs.h>
39178172Simp__FBSDID("$FreeBSD$");
40178172Simp
41178172Simp#include <sys/param.h>
42253367Sae#include <sys/systm.h>
43178172Simp#include <sys/kernel.h>
44178172Simp#include <sys/lock.h>
45178172Simp#include <sys/mutex.h>
46178172Simp#include <sys/proc.h>
47178172Simp#include <sys/sf_buf.h>
48178172Simp#include <sys/uio.h>
49178172Simp
50178172Simp#include <vm/vm.h>
51178172Simp#include <vm/vm_page.h>
52206714Sjmallett#include <vm/vm_param.h>
53178172Simp
54208581Sgonzo#include <machine/cache.h>
55208581Sgonzo
56178172Simp/*
57206714Sjmallett * Implement uiomove(9) from physical memory using a combination
58206714Sjmallett * of the direct mapping and sf_bufs to reduce the creation and
59206714Sjmallett * destruction of ephemeral mappings.
60178172Simp */
61178172Simpint
62178172Simpuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
63178172Simp{
64178172Simp	struct sf_buf *sf;
65178172Simp	struct thread *td = curthread;
66178172Simp	struct iovec *iov;
67178172Simp	void *cp;
68178172Simp	vm_offset_t page_offset;
69206714Sjmallett	vm_paddr_t pa;
70206714Sjmallett	vm_page_t m;
71178172Simp	size_t cnt;
72178172Simp	int error = 0;
73178172Simp	int save = 0;
74178172Simp
75178172Simp	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
76178172Simp	    ("uiomove_fromphys: mode"));
77178172Simp	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
78178172Simp	    ("uiomove_fromphys proc"));
79178172Simp	save = td->td_pflags & TDP_DEADLKTREAT;
80178172Simp	td->td_pflags |= TDP_DEADLKTREAT;
81178172Simp	while (n > 0 && uio->uio_resid) {
82178172Simp		iov = uio->uio_iov;
83178172Simp		cnt = iov->iov_len;
84178172Simp		if (cnt == 0) {
85178172Simp			uio->uio_iov++;
86178172Simp			uio->uio_iovcnt--;
87178172Simp			continue;
88178172Simp		}
89178172Simp		if (cnt > n)
90178172Simp			cnt = n;
91178172Simp		page_offset = offset & PAGE_MASK;
92206714Sjmallett		cnt = ulmin(cnt, PAGE_SIZE - page_offset);
93206714Sjmallett		m = ma[offset >> PAGE_SHIFT];
94206714Sjmallett		pa = VM_PAGE_TO_PHYS(m);
95217944Sjchandra		if (MIPS_DIRECT_MAPPABLE(pa)) {
96206714Sjmallett			sf = NULL;
97217944Sjchandra			cp = (char *)MIPS_PHYS_TO_DIRECT(pa) + page_offset;
98208581Sgonzo			/*
99208581Sgonzo			 * flush all mappings to this page, KSEG0 address first
100208581Sgonzo			 * in order to get it overwritten by correct data
101208581Sgonzo			 */
102208581Sgonzo			mips_dcache_wbinv_range((vm_offset_t)cp, cnt);
103208581Sgonzo			pmap_flush_pvcache(m);
104206714Sjmallett		} else {
105206714Sjmallett			sf = sf_buf_alloc(m, 0);
106206714Sjmallett			cp = (char *)sf_buf_kva(sf) + page_offset;
107206714Sjmallett		}
108178172Simp		switch (uio->uio_segflg) {
109178172Simp		case UIO_USERSPACE:
110218195Smdf			maybe_yield();
111178172Simp			if (uio->uio_rw == UIO_READ)
112178172Simp				error = copyout(cp, iov->iov_base, cnt);
113178172Simp			else
114178172Simp				error = copyin(iov->iov_base, cp, cnt);
115178172Simp			if (error) {
116206714Sjmallett				if (sf != NULL)
117206714Sjmallett					sf_buf_free(sf);
118178172Simp				goto out;
119178172Simp			}
120178172Simp			break;
121178172Simp		case UIO_SYSSPACE:
122178172Simp			if (uio->uio_rw == UIO_READ)
123178172Simp				bcopy(cp, iov->iov_base, cnt);
124178172Simp			else
125178172Simp				bcopy(iov->iov_base, cp, cnt);
126178172Simp			break;
127178172Simp		case UIO_NOCOPY:
128178172Simp			break;
129178172Simp		}
130206714Sjmallett		if (sf != NULL)
131206714Sjmallett			sf_buf_free(sf);
132208581Sgonzo		else
133208581Sgonzo			mips_dcache_wbinv_range((vm_offset_t)cp, cnt);
134178172Simp		iov->iov_base = (char *)iov->iov_base + cnt;
135178172Simp		iov->iov_len -= cnt;
136178172Simp		uio->uio_resid -= cnt;
137178172Simp		uio->uio_offset += cnt;
138178172Simp		offset += cnt;
139178172Simp		n -= cnt;
140178172Simp	}
141178172Simpout:
142178172Simp	if (save == 0)
143178172Simp		td->td_pflags &= ~TDP_DEADLKTREAT;
144178172Simp	return (error);
145178172Simp}
146