146280ae7SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
433d13796SAlan Cox * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
533d13796SAlan Cox * Copyright (c) 1982, 1986, 1991, 1993
633d13796SAlan Cox * The Regents of the University of California. All rights reserved.
733d13796SAlan Cox * (c) UNIX System Laboratories, Inc.
833d13796SAlan Cox * All or some portions of this file are derived from material licensed
933d13796SAlan Cox * to the University of California by American Telephone and Telegraph
1033d13796SAlan Cox * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1133d13796SAlan Cox * the permission of UNIX System Laboratories, Inc.
1233d13796SAlan Cox *
1333d13796SAlan Cox * Redistribution and use in source and binary forms, with or without
1433d13796SAlan Cox * modification, are permitted provided that the following conditions
1533d13796SAlan Cox * are met:
1633d13796SAlan Cox * 1. Redistributions of source code must retain the above copyright
1733d13796SAlan Cox * notice, this list of conditions and the following disclaimer.
1833d13796SAlan Cox * 2. Redistributions in binary form must reproduce the above copyright
1933d13796SAlan Cox * notice, this list of conditions and the following disclaimer in the
2033d13796SAlan Cox * documentation and/or other materials provided with the distribution.
2122b8ff24SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2233d13796SAlan Cox * may be used to endorse or promote products derived from this software
2333d13796SAlan Cox * without specific prior written permission.
2433d13796SAlan Cox *
2533d13796SAlan Cox * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2633d13796SAlan Cox * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2733d13796SAlan Cox * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2833d13796SAlan Cox * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2933d13796SAlan Cox * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3033d13796SAlan Cox * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3133d13796SAlan Cox * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3233d13796SAlan Cox * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3333d13796SAlan Cox * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3433d13796SAlan Cox * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3533d13796SAlan Cox * SUCH DAMAGE.
3633d13796SAlan Cox */
3733d13796SAlan Cox
3833d13796SAlan Cox #include <sys/param.h>
3933d13796SAlan Cox #include <sys/kernel.h>
4033d13796SAlan Cox #include <sys/lock.h>
4133d13796SAlan Cox #include <sys/mutex.h>
4233d13796SAlan Cox #include <sys/proc.h>
4333d13796SAlan Cox #include <sys/systm.h>
4433d13796SAlan Cox #include <sys/uio.h>
4533d13796SAlan Cox
4633d13796SAlan Cox #include <vm/vm.h>
4733d13796SAlan Cox #include <vm/vm_page.h>
4833d13796SAlan Cox
4933d13796SAlan Cox #include <machine/vmparam.h>
5033d13796SAlan Cox
5133d13796SAlan Cox /*
5233d13796SAlan Cox * Implement uiomove(9) from physical memory using the direct map to
531af1ebb8SAlan Cox * avoid the creation and destruction of ephemeral mappings.
5433d13796SAlan Cox */
5533d13796SAlan Cox int
uiomove_fromphys(vm_page_t ma[],vm_offset_t offset,int n,struct uio * uio)5633d13796SAlan Cox uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio)
5733d13796SAlan Cox {
5833d13796SAlan Cox struct thread *td = curthread;
5933d13796SAlan Cox struct iovec *iov;
6033d13796SAlan Cox void *cp;
61927dc0e0SRoger Pau Monné vm_offset_t page_offset, vaddr;
6233d13796SAlan Cox size_t cnt;
6333d13796SAlan Cox int error = 0;
6433d13796SAlan Cox int save = 0;
654961faaaSJohn Baldwin bool mapped;
6633d13796SAlan Cox
6733d13796SAlan Cox KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
6833d13796SAlan Cox ("uiomove_fromphys: mode"));
6933d13796SAlan Cox KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
7033d13796SAlan Cox ("uiomove_fromphys proc"));
71*8fd0ec53SMark Johnston KASSERT(uio->uio_resid >= 0,
72*8fd0ec53SMark Johnston ("%s: uio %p resid underflow", __func__, uio));
73*8fd0ec53SMark Johnston
74fa2a4d05STim J. Robbins save = td->td_pflags & TDP_DEADLKTREAT;
75fa2a4d05STim J. Robbins td->td_pflags |= TDP_DEADLKTREAT;
764961faaaSJohn Baldwin mapped = false;
7733d13796SAlan Cox while (n > 0 && uio->uio_resid) {
78*8fd0ec53SMark Johnston KASSERT(uio->uio_iovcnt > 0,
79*8fd0ec53SMark Johnston ("%s: uio %p iovcnt underflow", __func__, uio));
80*8fd0ec53SMark Johnston
8133d13796SAlan Cox iov = uio->uio_iov;
8233d13796SAlan Cox cnt = iov->iov_len;
8333d13796SAlan Cox if (cnt == 0) {
8433d13796SAlan Cox uio->uio_iov++;
8533d13796SAlan Cox uio->uio_iovcnt--;
8633d13796SAlan Cox continue;
8733d13796SAlan Cox }
8833d13796SAlan Cox if (cnt > n)
8933d13796SAlan Cox cnt = n;
9033d13796SAlan Cox page_offset = offset & PAGE_MASK;
9133d13796SAlan Cox cnt = min(cnt, PAGE_SIZE - page_offset);
92927dc0e0SRoger Pau Monné if (uio->uio_segflg != UIO_NOCOPY) {
93927dc0e0SRoger Pau Monné mapped = pmap_map_io_transient(
944961faaaSJohn Baldwin &ma[offset >> PAGE_SHIFT], &vaddr, 1, true);
95927dc0e0SRoger Pau Monné cp = (char *)vaddr + page_offset;
96927dc0e0SRoger Pau Monné }
9733d13796SAlan Cox switch (uio->uio_segflg) {
9833d13796SAlan Cox case UIO_USERSPACE:
9908b163faSMatthew D Fleming maybe_yield();
10033d13796SAlan Cox if (uio->uio_rw == UIO_READ)
10133d13796SAlan Cox error = copyout(cp, iov->iov_base, cnt);
10233d13796SAlan Cox else
10333d13796SAlan Cox error = copyin(iov->iov_base, cp, cnt);
10433d13796SAlan Cox if (error)
10533d13796SAlan Cox goto out;
10633d13796SAlan Cox break;
10733d13796SAlan Cox case UIO_SYSSPACE:
10833d13796SAlan Cox if (uio->uio_rw == UIO_READ)
10933d13796SAlan Cox bcopy(cp, iov->iov_base, cnt);
11033d13796SAlan Cox else
11133d13796SAlan Cox bcopy(iov->iov_base, cp, cnt);
11233d13796SAlan Cox break;
11333d13796SAlan Cox case UIO_NOCOPY:
11433d13796SAlan Cox break;
11533d13796SAlan Cox }
116927dc0e0SRoger Pau Monné if (__predict_false(mapped)) {
117927dc0e0SRoger Pau Monné pmap_unmap_io_transient(&ma[offset >> PAGE_SHIFT],
1184961faaaSJohn Baldwin &vaddr, 1, true);
1194961faaaSJohn Baldwin mapped = false;
120927dc0e0SRoger Pau Monné }
12133d13796SAlan Cox iov->iov_base = (char *)iov->iov_base + cnt;
12233d13796SAlan Cox iov->iov_len -= cnt;
12333d13796SAlan Cox uio->uio_resid -= cnt;
12433d13796SAlan Cox uio->uio_offset += cnt;
12533d13796SAlan Cox offset += cnt;
12633d13796SAlan Cox n -= cnt;
12733d13796SAlan Cox }
12833d13796SAlan Cox out:
129927dc0e0SRoger Pau Monné if (__predict_false(mapped))
130927dc0e0SRoger Pau Monné pmap_unmap_io_transient(&ma[offset >> PAGE_SHIFT], &vaddr, 1,
1314961faaaSJohn Baldwin true);
132cc05397fSTim J. Robbins if (save == 0)
133fa2a4d05STim J. Robbins td->td_pflags &= ~TDP_DEADLKTREAT;
13433d13796SAlan Cox return (error);
13533d13796SAlan Cox }
136