]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/cxgb/ulp/tom/cxgb_vm.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / cxgb / ulp / tom / cxgb_vm.c
1 /**************************************************************************
2
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
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.
15
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.
27
28 ***************************************************************************/
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
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>
39 #include <sys/lock.h>
40 #include <sys/mbuf.h>
41 #include <sys/condvar.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44
45 #include <vm/vm.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_extern.h>
49 #include <vm/pmap.h>
50 #include <dev/cxgb/ulp/tom/cxgb_vm.h>
51
52 #define TRACE_ENTER printf("%s:%s entered", __FUNCTION__, __FILE__)
53 #define TRACE_EXIT printf("%s:%s:%d exited", __FUNCTION__, __FILE__, __LINE__)
54
55 /*
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
62  *  - hold all pages
63  *  - return number of pages in count
64  */
65 int
66 vm_fault_hold_user_pages(vm_offset_t addr, vm_page_t *mp, int count, int flags)
67 {
68         vm_offset_t end, va;
69         int faults, rv;
70
71         struct thread *td;
72         vm_map_t map;
73         pmap_t pmap;
74         vm_page_t m, *pages;
75         vm_prot_t prot;
76         
77
78         /*
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
83          */
84         end = addr + (count * PAGE_SIZE);
85         if (end > VM_MAXUSER_ADDRESS) {
86                 printf("bad address passed\n");
87                 return (EFAULT);
88         }
89
90         td = curthread;
91         map = &td->td_proc->p_vmspace->vm_map;
92         pmap = &td->td_proc->p_vmspace->vm_pmap;
93         pages = mp;
94
95         prot = VM_PROT_READ;
96         prot |= (flags & VM_HOLD_WRITEABLE) ? VM_PROT_WRITE : 0;
97 retry:
98
99         /*
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
102          */
103         vm_page_lock_queues();
104         for (pages = mp, faults = 0, va = addr; va < end; va += PAGE_SIZE, pages++) {
105                 /*
106                  * Assure that we only hold the page once
107                  */
108                 if (*pages == NULL) {
109                         /*
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
114                          */
115                         *pages = m = pmap_extract_and_hold(pmap, va, prot);
116                         if (m == NULL) {
117                                 faults++;
118                                 continue;
119                         }
120                         
121                         if (flags & VM_HOLD_WRITEABLE)
122                                 vm_page_dirty(m);
123                 }
124         }
125         vm_page_unlock_queues();
126         
127         if (faults == 0) {
128                 return (0);
129         }
130         
131         /*
132          * Pages either have insufficient permissions or are not present
133          * trigger a fault where neccessary
134          * 
135          */
136         for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++) {
137                 m = *pages;
138                 rv = 0;
139                 if (m)
140                         continue;
141                 if (flags & VM_HOLD_WRITEABLE) 
142                         rv = vm_fault(map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
143                 else    
144                         rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_NORMAL);
145                 if (rv) {
146                         printf("vm_fault bad return rv=%d va=0x%zx\n", rv, va);
147                         
148                         goto error;
149                 } 
150         }
151         
152         goto retry;
153
154 error:  
155         vm_page_lock_queues();
156         for (pages = mp, va = addr; va < end; va += PAGE_SIZE, pages++)
157                 if (*pages)
158                         vm_page_unhold(*pages);
159         vm_page_unlock_queues();
160         return (EFAULT);
161 }
162
163 void
164 vm_fault_unhold_pages(vm_page_t *mp, int count)
165 {
166
167         KASSERT(count >= 0, ("negative count %d", count));
168         vm_page_lock_queues();
169         while (count--) {
170                 vm_page_unhold(*mp);
171                 mp++;
172         }
173         vm_page_unlock_queues();
174 }