1 /**************************************************************************
3 Copyright (c) 2007, Chelsio Inc.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Neither the name of the Chelsio Corporation nor the names of its
13 contributors may be used to endorse or promote products derived from
14 this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
28 ***************************************************************************/
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
41 #include <sys/condvar.h>
42 #include <sys/mutex.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_extern.h>
50 #include <dev/cxgb/ulp/tom/cxgb_vm.h>
52 #define TRACE_ENTER printf("%s:%s entered", __FUNCTION__, __FILE__)
53 #define TRACE_EXIT printf("%s:%s:%d exited", __FUNCTION__, __FILE__, __LINE__)
56 * This routine takes a user address range and does the following:
57 * - validate that the user has access to those pages (flags indicates read or write) - if not fail
58 * - validate that count is enough to hold range number of pages - if not fail
59 * - fault in any non-resident pages
60 * - if the user is doing a read force a write fault for any COWed pages
61 * - if the user is doing a read mark all pages as dirty
63 * - return number of pages in count
66 vm_fault_hold_user_pages(vm_offset_t addr, vm_page_t *mp, int count, int flags)
79 * Check that virtual address range is legal
80 * This check is somewhat bogus as on some architectures kernel
81 * and user do not share VA - however, it appears that all FreeBSD
82 * architectures define it
84 end = addr + (count * PAGE_SIZE);
85 if (end > VM_MAXUSER_ADDRESS) {
86 printf("bad address passed\n");
91 map = &td->td_proc->p_vmspace->vm_map;
92 pmap = &td->td_proc->p_vmspace->vm_pmap;
96 prot |= (flags & VM_HOLD_WRITEABLE) ? VM_PROT_WRITE : 0;
100 * First optimistically assume that all pages are resident (and R/W if for write)
101 * if so just mark pages as held (and dirty if for write) and return
103 vm_page_lock_queues();
104 for (pages = mp, faults = 0, va = addr; va < end; va += PAGE_SIZE, pages++) {
106 * Assure that we only hold the page once
108 if (*pages == NULL) {
110 * page queue mutex is recursable so this is OK
111 * it would be really nice if we had an unlocked version of this so
112 * we were only acquiring the pmap lock 1 time as opposed to potentially
113 * many dozens of times
115 *pages = m = pmap_extract_and_hold(pmap, va, prot);
121 if (flags & VM_HOLD_WRITEABLE)
125 vm_page_unlock_queues();
132 * Pages either have insufficient permissions or are not present
133 * trigger a fault where neccessary
136 for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++) {
141 if (flags & VM_HOLD_WRITEABLE)
142 rv = vm_fault(map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
144 rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_NORMAL);
146 printf("vm_fault bad return rv=%d va=0x%zx\n", rv, va);
155 vm_page_lock_queues();
156 for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++)
158 vm_page_unhold(*pages);
159 vm_page_unlock_queues();
164 vm_fault_unhold_pages(vm_page_t *mp, int count)
167 KASSERT(count >= 0, ("negative count %d", count));
168 vm_page_lock_queues();
173 vm_page_unlock_queues();