uio_machdep.c revision 130028
1127297Salc/* 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: head/sys/sparc64/sparc64/uio_machdep.c 130028 2004-06-03 10:22:47Z tjr $"); 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 54127297Salc#include <machine/tlb.h> 55127297Salc 56127297Salc/* 57127297Salc * Implement uiomove(9) from physical memory using a combination 58127297Salc * of the direct mapping and sf_bufs to reduce the creation and 59127297Salc * destruction of ephemeral mappings. 60127297Salc */ 61127297Salcint 62127297Salcuiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, struct uio *uio) 63127297Salc{ 64127297Salc struct sf_buf *sf; 65127297Salc struct thread *td = curthread; 66127297Salc struct iovec *iov; 67127297Salc void *cp; 68127297Salc vm_offset_t page_offset; 69127297Salc vm_paddr_t pa; 70127297Salc vm_page_t m; 71127297Salc size_t cnt; 72127297Salc int error = 0; 73127297Salc int save = 0; 74127297Salc 75127297Salc KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 76127297Salc ("uiomove_fromphys: mode")); 77127297Salc KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 78127297Salc ("uiomove_fromphys proc")); 79130028Stjr save = td->td_pflags & TDP_DEADLKTREAT; 80130028Stjr td->td_pflags |= TDP_DEADLKTREAT; 81127297Salc while (n > 0 && uio->uio_resid) { 82127297Salc iov = uio->uio_iov; 83127297Salc cnt = iov->iov_len; 84127297Salc if (cnt == 0) { 85127297Salc uio->uio_iov++; 86127297Salc uio->uio_iovcnt--; 87127297Salc continue; 88127297Salc } 89127297Salc if (cnt > n) 90127297Salc cnt = n; 91127297Salc page_offset = offset & PAGE_MASK; 92127297Salc cnt = min(cnt, PAGE_SIZE - page_offset); 93127297Salc m = ma[offset >> PAGE_SHIFT]; 94127297Salc pa = VM_PAGE_TO_PHYS(m); 95127297Salc if (m->md.color != DCACHE_COLOR(pa)) { 96127788Salc sf = sf_buf_alloc(m, 0); 97127297Salc cp = (char *)sf_buf_kva(sf) + page_offset; 98127297Salc } else { 99127297Salc sf = NULL; 100127297Salc cp = (char *)TLB_PHYS_TO_DIRECT(pa) + page_offset; 101127297Salc } 102127297Salc switch (uio->uio_segflg) { 103127297Salc case UIO_USERSPACE: 104127297Salc if (ticks - PCPU_GET(switchticks) >= hogticks) 105127297Salc uio_yield(); 106127297Salc if (uio->uio_rw == UIO_READ) 107127297Salc error = copyout(cp, iov->iov_base, cnt); 108127297Salc else 109127297Salc error = copyin(iov->iov_base, cp, cnt); 110127297Salc if (error) 111127297Salc goto out; 112127297Salc break; 113127297Salc case UIO_SYSSPACE: 114127297Salc if (uio->uio_rw == UIO_READ) 115127297Salc bcopy(cp, iov->iov_base, cnt); 116127297Salc else 117127297Salc bcopy(iov->iov_base, cp, cnt); 118127297Salc break; 119127297Salc case UIO_NOCOPY: 120127297Salc break; 121127297Salc } 122127297Salc if (sf != NULL) 123127297Salc sf_buf_free(sf); 124127297Salc iov->iov_base = (char *)iov->iov_base + cnt; 125127297Salc iov->iov_len -= cnt; 126127297Salc uio->uio_resid -= cnt; 127127297Salc uio->uio_offset += cnt; 128127297Salc offset += cnt; 129127297Salc n -= cnt; 130127297Salc } 131127297Salcout: 132130028Stjr if (save == 0) 133130023Stjr td->td_pflags &= ~TDP_DEADLKTREAT; 134127297Salc return (error); 135127297Salc} 136