1 /**************************************************************************
3 Copyright (c) 2007-2008, 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>
44 #include <sys/syslog.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_extern.h>
51 #include <ulp/tom/cxgb_vm.h>
54 * This routine takes a user's map, array of pages, number of pages, and flags
55 * and then does the following:
56 * - validate that the user has access to those pages (flags indicates read
57 * 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
65 vm_fault_hold_user_pages(vm_map_t map, vm_offset_t addr, vm_page_t *mp,
66 int count, vm_prot_t prot)
73 pmap = vm_map_pmap(map);
77 * Check that virtual address range is legal
78 * This check is somewhat bogus as on some architectures kernel
79 * and user do not share VA - however, it appears that all FreeBSD
80 * architectures define it
82 end = addr + (count * PAGE_SIZE);
83 if (end > VM_MAXUSER_ADDRESS) {
84 log(LOG_WARNING, "bad address passed to vm_fault_hold_user_pages");
89 * First optimistically assume that all pages are resident
90 * (and R/W if for write) if so just mark pages as held (and
91 * dirty if for write) and return
93 vm_page_lock_queues();
94 for (pages = mp, faults = 0, va = addr; va < end;
95 va += PAGE_SIZE, pages++) {
97 * page queue mutex is recursable so this is OK
98 * it would be really nice if we had an unlocked
99 * version of this so we were only acquiring the
100 * pmap lock 1 time as opposed to potentially
101 * many dozens of times
103 *pages = m = pmap_extract_and_hold(pmap, va, prot);
109 * Preemptively mark dirty - the pages
110 * will never have the modified bit set if
111 * they are only changed via DMA
113 if (prot & VM_PROT_WRITE)
117 vm_page_unlock_queues();
123 * Pages either have insufficient permissions or are not present
124 * trigger a fault where neccessary
128 for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++) {
130 * Account for a very narrow race where the page may be
131 * taken away from us before it is held
133 while (*pages == NULL) {
134 rv = vm_fault(map, va, prot,
135 (prot & VM_PROT_WRITE) ? VM_FAULT_DIRTY : VM_FAULT_NORMAL);
138 *pages = pmap_extract_and_hold(pmap, va, prot);
144 "vm_fault bad return rv=%d va=0x%zx\n", rv, va);
145 vm_page_lock_queues();
146 for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++)
148 vm_page_unhold(*pages);
151 vm_page_unlock_queues();
156 vm_fault_unhold_pages(vm_page_t *mp, int count)
159 KASSERT(count >= 0, ("negative count %d", count));
160 vm_page_lock_queues();
165 vm_page_unlock_queues();