1139825Simp/*- 2127297Salc * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu> 3127297Salc * Copyright (c) 1982, 1986, 1991, 1993 4127297Salc * The Regents of the University of California. All rights reserved. 5127297Salc * (c) UNIX System Laboratories, Inc. 6127297Salc * All or some portions of this file are derived from material licensed 7127297Salc * to the University of California by American Telephone and Telegraph 8127297Salc * Co. or Unix System Laboratories, Inc. and are reproduced herein with 9127297Salc * the permission of UNIX System Laboratories, Inc. 10127297Salc * 11127297Salc * Redistribution and use in source and binary forms, with or without 12127297Salc * modification, are permitted provided that the following conditions 13127297Salc * are met: 14127297Salc * 1. Redistributions of source code must retain the above copyright 15127297Salc * notice, this list of conditions and the following disclaimer. 16127297Salc * 2. Redistributions in binary form must reproduce the above copyright 17127297Salc * notice, this list of conditions and the following disclaimer in the 18127297Salc * documentation and/or other materials provided with the distribution. 19127297Salc * 4. Neither the name of the University nor the names of its contributors 20127297Salc * may be used to endorse or promote products derived from this software 21127297Salc * without specific prior written permission. 22127297Salc * 23127297Salc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24127297Salc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25127297Salc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26127297Salc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27127297Salc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28127297Salc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29127297Salc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30127297Salc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31127297Salc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32127297Salc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33127297Salc * SUCH DAMAGE. 34127297Salc * 35127297Salc * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 36127297Salc */ 37127297Salc 38127297Salc#include <sys/cdefs.h> 39127297Salc__FBSDID("$FreeBSD$"); 40127297Salc 41127297Salc#include <sys/param.h> 42127297Salc#include <sys/kernel.h> 43127297Salc#include <sys/lock.h> 44127297Salc#include <sys/mutex.h> 45127297Salc#include <sys/proc.h> 46127297Salc#include <sys/sf_buf.h> 47127297Salc#include <sys/systm.h> 48127297Salc#include <sys/uio.h> 49127297Salc 50127297Salc#include <vm/vm.h> 51127297Salc#include <vm/vm_page.h> 52127297Salc#include <vm/vm_param.h> 53127297Salc 54211049Smarius#include <machine/cache.h> 55127297Salc#include <machine/tlb.h> 56127297Salc 57127297Salc/* 58127297Salc * Implement uiomove(9) from physical memory using a combination 59127297Salc * of the direct mapping and sf_bufs to reduce the creation and 60211049Smarius * destruction of ephemeral mappings. 61127297Salc */ 62127297Salcint 63127297Salcuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio) 64127297Salc{ 65127297Salc struct sf_buf *sf; 66127297Salc struct thread *td = curthread; 67127297Salc struct iovec *iov; 68127297Salc void *cp; 69127297Salc vm_offset_t page_offset; 70127297Salc vm_paddr_t pa; 71127297Salc vm_page_t m; 72127297Salc size_t cnt; 73127297Salc int error = 0; 74127297Salc int save = 0; 75127297Salc 76127297Salc KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 77127297Salc ("uiomove_fromphys: mode")); 78127297Salc KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 79127297Salc ("uiomove_fromphys proc")); 80130028Stjr save = td->td_pflags & TDP_DEADLKTREAT; 81130028Stjr td->td_pflags |= TDP_DEADLKTREAT; 82127297Salc while (n > 0 && uio->uio_resid) { 83127297Salc iov = uio->uio_iov; 84127297Salc cnt = iov->iov_len; 85127297Salc if (cnt == 0) { 86127297Salc uio->uio_iov++; 87127297Salc uio->uio_iovcnt--; 88127297Salc continue; 89127297Salc } 90127297Salc if (cnt > n) 91127297Salc cnt = n; 92127297Salc page_offset = offset & PAGE_MASK; 93133774Smarius cnt = ulmin(cnt, PAGE_SIZE - page_offset); 94127297Salc m = ma[offset >> PAGE_SHIFT]; 95127297Salc pa = VM_PAGE_TO_PHYS(m); 96211049Smarius if (dcache_color_ignore == 0 && 97211049Smarius m->md.color != DCACHE_COLOR(pa)) { 98127788Salc sf = sf_buf_alloc(m, 0); 99127297Salc cp = (char *)sf_buf_kva(sf) + page_offset; 100127297Salc } else { 101127297Salc sf = NULL; 102127297Salc cp = (char *)TLB_PHYS_TO_DIRECT(pa) + page_offset; 103127297Salc } 104127297Salc switch (uio->uio_segflg) { 105127297Salc case UIO_USERSPACE: 106218195Smdf maybe_yield(); 107127297Salc if (uio->uio_rw == UIO_READ) 108127297Salc error = copyout(cp, iov->iov_base, cnt); 109127297Salc else 110127297Salc error = copyin(iov->iov_base, cp, cnt); 111134127Salc if (error) { 112134127Salc if (sf != NULL) 113134127Salc sf_buf_free(sf); 114127297Salc goto out; 115134127Salc } 116127297Salc break; 117127297Salc case UIO_SYSSPACE: 118127297Salc if (uio->uio_rw == UIO_READ) 119127297Salc bcopy(cp, iov->iov_base, cnt); 120127297Salc else 121127297Salc bcopy(iov->iov_base, cp, cnt); 122127297Salc break; 123127297Salc case UIO_NOCOPY: 124127297Salc break; 125127297Salc } 126127297Salc if (sf != NULL) 127127297Salc sf_buf_free(sf); 128127297Salc iov->iov_base = (char *)iov->iov_base + cnt; 129127297Salc iov->iov_len -= cnt; 130127297Salc uio->uio_resid -= cnt; 131127297Salc uio->uio_offset += cnt; 132127297Salc offset += cnt; 133127297Salc n -= cnt; 134127297Salc } 135127297Salcout: 136130028Stjr if (save == 0) 137130023Stjr td->td_pflags &= ~TDP_DEADLKTREAT; 138127297Salc return (error); 139127297Salc} 140