uvm_bio.c revision 1.43 1 1.42 yamt /* $NetBSD: uvm_bio.c,v 1.43 2006/01/31 14:11:25 yamt Exp $ */
2 1.2 chs
3 1.13 chs /*
4 1.2 chs * Copyright (c) 1998 Chuck Silvers.
5 1.2 chs * All rights reserved.
6 1.2 chs *
7 1.2 chs * Redistribution and use in source and binary forms, with or without
8 1.2 chs * modification, are permitted provided that the following conditions
9 1.2 chs * are met:
10 1.2 chs * 1. Redistributions of source code must retain the above copyright
11 1.2 chs * notice, this list of conditions and the following disclaimer.
12 1.2 chs * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 chs * notice, this list of conditions and the following disclaimer in the
14 1.2 chs * documentation and/or other materials provided with the distribution.
15 1.2 chs * 3. The name of the author may not be used to endorse or promote products
16 1.2 chs * derived from this software without specific prior written permission.
17 1.2 chs *
18 1.2 chs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.2 chs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.2 chs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.2 chs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.2 chs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.2 chs * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.2 chs * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.2 chs * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.2 chs * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.2 chs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.2 chs * SUCH DAMAGE.
29 1.2 chs *
30 1.2 chs */
31 1.2 chs
32 1.2 chs /*
33 1.33 chs * uvm_bio.c: buffered i/o object mapping cache
34 1.2 chs */
35 1.2 chs
36 1.21 lukem #include <sys/cdefs.h>
37 1.42 yamt __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.43 2006/01/31 14:11:25 yamt Exp $");
38 1.21 lukem
39 1.21 lukem #include "opt_uvmhist.h"
40 1.2 chs
41 1.2 chs #include <sys/param.h>
42 1.2 chs #include <sys/systm.h>
43 1.2 chs #include <sys/malloc.h>
44 1.2 chs #include <sys/kernel.h>
45 1.2 chs
46 1.2 chs #include <uvm/uvm.h>
47 1.2 chs
48 1.2 chs /*
49 1.2 chs * global data structures
50 1.2 chs */
51 1.2 chs
52 1.2 chs /*
53 1.2 chs * local functions
54 1.2 chs */
55 1.2 chs
56 1.39 thorpej static int ubc_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
57 1.39 thorpej int, int, vm_fault_t, vm_prot_t, int);
58 1.39 thorpej static struct ubc_map *ubc_find_mapping(struct uvm_object *, voff_t);
59 1.2 chs
60 1.2 chs /*
61 1.2 chs * local data structues
62 1.2 chs */
63 1.2 chs
64 1.18 chs #define UBC_HASH(uobj, offset) \
65 1.18 chs (((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
66 1.2 chs ubc_object.hashmask)
67 1.2 chs
68 1.18 chs #define UBC_QUEUE(offset) \
69 1.18 chs (&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) & \
70 1.18 chs (UBC_NQUEUES - 1)])
71 1.18 chs
72 1.18 chs #define UBC_UMAP_ADDR(u) \
73 1.18 chs (vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
74 1.18 chs
75 1.18 chs
76 1.18 chs #define UMAP_PAGES_LOCKED 0x0001
77 1.18 chs #define UMAP_MAPPING_CACHED 0x0002
78 1.2 chs
79 1.2 chs struct ubc_map
80 1.2 chs {
81 1.2 chs struct uvm_object * uobj; /* mapped object */
82 1.2 chs voff_t offset; /* offset into uobj */
83 1.33 chs voff_t writeoff; /* write offset */
84 1.33 chs vsize_t writelen; /* write len */
85 1.18 chs int refcount; /* refcount on mapping */
86 1.18 chs int flags; /* extra state */
87 1.42 yamt int advice;
88 1.2 chs
89 1.2 chs LIST_ENTRY(ubc_map) hash; /* hash table */
90 1.2 chs TAILQ_ENTRY(ubc_map) inactive; /* inactive queue */
91 1.2 chs };
92 1.2 chs
93 1.2 chs static struct ubc_object
94 1.2 chs {
95 1.2 chs struct uvm_object uobj; /* glue for uvm_map() */
96 1.2 chs char *kva; /* where ubc_object is mapped */
97 1.2 chs struct ubc_map *umap; /* array of ubc_map's */
98 1.2 chs
99 1.2 chs LIST_HEAD(, ubc_map) *hash; /* hashtable for cached ubc_map's */
100 1.2 chs u_long hashmask; /* mask for hashtable */
101 1.2 chs
102 1.2 chs TAILQ_HEAD(ubc_inactive_head, ubc_map) *inactive;
103 1.2 chs /* inactive queues for ubc_map's */
104 1.2 chs
105 1.2 chs } ubc_object;
106 1.2 chs
107 1.2 chs struct uvm_pagerops ubc_pager =
108 1.2 chs {
109 1.2 chs NULL, /* init */
110 1.2 chs NULL, /* reference */
111 1.2 chs NULL, /* detach */
112 1.2 chs ubc_fault, /* fault */
113 1.2 chs /* ... rest are NULL */
114 1.2 chs };
115 1.2 chs
116 1.2 chs int ubc_nwins = UBC_NWINS;
117 1.11 chs int ubc_winshift = UBC_WINSHIFT;
118 1.11 chs int ubc_winsize;
119 1.27 thorpej #if defined(PMAP_PREFER)
120 1.2 chs int ubc_nqueues;
121 1.2 chs #define UBC_NQUEUES ubc_nqueues
122 1.2 chs #else
123 1.2 chs #define UBC_NQUEUES 1
124 1.2 chs #endif
125 1.2 chs
126 1.2 chs /*
127 1.2 chs * ubc_init
128 1.2 chs *
129 1.2 chs * init pager private data structures.
130 1.2 chs */
131 1.2 chs
132 1.2 chs void
133 1.2 chs ubc_init(void)
134 1.2 chs {
135 1.2 chs struct ubc_map *umap;
136 1.2 chs vaddr_t va;
137 1.2 chs int i;
138 1.15 simonb
139 1.15 simonb /*
140 1.15 simonb * Make sure ubc_winshift is sane.
141 1.15 simonb */
142 1.15 simonb if (ubc_winshift < PAGE_SHIFT)
143 1.15 simonb ubc_winshift = PAGE_SHIFT;
144 1.2 chs
145 1.2 chs /*
146 1.2 chs * init ubc_object.
147 1.2 chs * alloc and init ubc_map's.
148 1.2 chs * init inactive queues.
149 1.2 chs * alloc and init hashtable.
150 1.2 chs * map in ubc_object.
151 1.2 chs */
152 1.2 chs
153 1.38 yamt UVM_OBJ_INIT(&ubc_object.uobj, &ubc_pager, UVM_OBJ_KERN);
154 1.2 chs
155 1.2 chs ubc_object.umap = malloc(ubc_nwins * sizeof(struct ubc_map),
156 1.2 chs M_TEMP, M_NOWAIT);
157 1.7 enami if (ubc_object.umap == NULL)
158 1.7 enami panic("ubc_init: failed to allocate ubc_map");
159 1.16 thorpej memset(ubc_object.umap, 0, ubc_nwins * sizeof(struct ubc_map));
160 1.2 chs
161 1.18 chs if (ubc_winshift < PAGE_SHIFT) {
162 1.18 chs ubc_winshift = PAGE_SHIFT;
163 1.18 chs }
164 1.2 chs va = (vaddr_t)1L;
165 1.2 chs #ifdef PMAP_PREFER
166 1.36 atatat PMAP_PREFER(0, &va, 0, 0); /* kernel is never topdown */
167 1.11 chs ubc_nqueues = va >> ubc_winshift;
168 1.11 chs if (ubc_nqueues == 0) {
169 1.11 chs ubc_nqueues = 1;
170 1.2 chs }
171 1.2 chs #endif
172 1.11 chs ubc_winsize = 1 << ubc_winshift;
173 1.2 chs ubc_object.inactive = malloc(UBC_NQUEUES *
174 1.18 chs sizeof(struct ubc_inactive_head), M_TEMP, M_NOWAIT);
175 1.7 enami if (ubc_object.inactive == NULL)
176 1.7 enami panic("ubc_init: failed to allocate inactive queue heads");
177 1.2 chs for (i = 0; i < UBC_NQUEUES; i++) {
178 1.2 chs TAILQ_INIT(&ubc_object.inactive[i]);
179 1.2 chs }
180 1.2 chs for (i = 0; i < ubc_nwins; i++) {
181 1.2 chs umap = &ubc_object.umap[i];
182 1.2 chs TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
183 1.2 chs umap, inactive);
184 1.2 chs }
185 1.2 chs
186 1.2 chs ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, M_TEMP, M_NOWAIT,
187 1.2 chs &ubc_object.hashmask);
188 1.2 chs for (i = 0; i <= ubc_object.hashmask; i++) {
189 1.2 chs LIST_INIT(&ubc_object.hash[i]);
190 1.2 chs }
191 1.2 chs
192 1.2 chs if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
193 1.11 chs ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
194 1.2 chs UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
195 1.9 chs UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
196 1.26 provos panic("ubc_init: failed to map ubc_object");
197 1.2 chs }
198 1.2 chs UVMHIST_INIT(ubchist, 300);
199 1.2 chs }
200 1.2 chs
201 1.2 chs /*
202 1.2 chs * ubc_fault: fault routine for ubc mapping
203 1.2 chs */
204 1.18 chs
205 1.39 thorpej static int
206 1.39 thorpej ubc_fault(struct uvm_faultinfo *ufi, vaddr_t ign1, struct vm_page **ign2,
207 1.39 thorpej int ign3, int ign4, vm_fault_t fault_type, vm_prot_t access_type,
208 1.39 thorpej int flags)
209 1.2 chs {
210 1.2 chs struct uvm_object *uobj;
211 1.2 chs struct ubc_map *umap;
212 1.2 chs vaddr_t va, eva, ubc_offset, slot_offset;
213 1.18 chs int i, error, npages;
214 1.18 chs struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT], *pg;
215 1.23 chs vm_prot_t prot;
216 1.2 chs UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
217 1.2 chs
218 1.2 chs /*
219 1.2 chs * no need to try with PGO_LOCKED...
220 1.2 chs * we don't need to have the map locked since we know that
221 1.2 chs * no one will mess with it until our reference is released.
222 1.2 chs */
223 1.18 chs
224 1.2 chs if (flags & PGO_LOCKED) {
225 1.2 chs uvmfault_unlockall(ufi, NULL, &ubc_object.uobj, NULL);
226 1.2 chs flags &= ~PGO_LOCKED;
227 1.2 chs }
228 1.2 chs
229 1.2 chs va = ufi->orig_rvaddr;
230 1.2 chs ubc_offset = va - (vaddr_t)ubc_object.kva;
231 1.11 chs umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
232 1.2 chs KASSERT(umap->refcount != 0);
233 1.18 chs slot_offset = ubc_offset & (ubc_winsize - 1);
234 1.2 chs
235 1.34 chs /*
236 1.34 chs * some platforms cannot write to individual bytes atomically, so
237 1.34 chs * software has to do read/modify/write of larger quantities instead.
238 1.34 chs * this means that the access_type for "write" operations
239 1.34 chs * can be VM_PROT_READ, which confuses us mightily.
240 1.37 perry *
241 1.34 chs * deal with this by resetting access_type based on the info
242 1.34 chs * that ubc_alloc() stores for us.
243 1.34 chs */
244 1.34 chs
245 1.34 chs access_type = umap->writelen ? VM_PROT_WRITE : VM_PROT_READ;
246 1.34 chs UVMHIST_LOG(ubchist, "va 0x%lx ubc_offset 0x%lx access_type %d",
247 1.34 chs va, ubc_offset, access_type, 0);
248 1.34 chs
249 1.33 chs #ifdef DIAGNOSTIC
250 1.33 chs if ((access_type & VM_PROT_WRITE) != 0) {
251 1.33 chs if (slot_offset < trunc_page(umap->writeoff) ||
252 1.33 chs umap->writeoff + umap->writelen <= slot_offset) {
253 1.33 chs panic("ubc_fault: out of range write");
254 1.33 chs }
255 1.33 chs }
256 1.33 chs #endif
257 1.33 chs
258 1.2 chs /* no umap locking needed since we have a ref on the umap */
259 1.2 chs uobj = umap->uobj;
260 1.2 chs
261 1.33 chs if ((access_type & VM_PROT_WRITE) == 0) {
262 1.33 chs npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
263 1.33 chs } else {
264 1.33 chs npages = (round_page(umap->offset + umap->writeoff +
265 1.33 chs umap->writelen) - (umap->offset + slot_offset))
266 1.33 chs >> PAGE_SHIFT;
267 1.33 chs flags |= PGO_PASTEOF;
268 1.33 chs }
269 1.2 chs
270 1.2 chs again:
271 1.2 chs memset(pgs, 0, sizeof (pgs));
272 1.2 chs simple_lock(&uobj->vmobjlock);
273 1.2 chs
274 1.33 chs UVMHIST_LOG(ubchist, "slot_offset 0x%x writeoff 0x%x writelen 0x%x ",
275 1.33 chs slot_offset, umap->writeoff, umap->writelen, 0);
276 1.33 chs UVMHIST_LOG(ubchist, "getpages uobj %p offset 0x%x npages %d",
277 1.18 chs uobj, umap->offset + slot_offset, npages, 0);
278 1.2 chs
279 1.33 chs error = (*uobj->pgops->pgo_get)(uobj, umap->offset + slot_offset, pgs,
280 1.42 yamt &npages, 0, access_type, umap->advice, flags | PGO_NOBLOCKALLOC |
281 1.41 yamt PGO_NOTIMESTAMP);
282 1.24 simonb UVMHIST_LOG(ubchist, "getpages error %d npages %d", error, npages, 0,
283 1.24 simonb 0);
284 1.2 chs
285 1.5 chs if (error == EAGAIN) {
286 1.2 chs tsleep(&lbolt, PVM, "ubc_fault", 0);
287 1.2 chs goto again;
288 1.2 chs }
289 1.5 chs if (error) {
290 1.10 chs return error;
291 1.5 chs }
292 1.2 chs
293 1.2 chs va = ufi->orig_rvaddr;
294 1.2 chs eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
295 1.2 chs
296 1.28 yamt UVMHIST_LOG(ubchist, "va 0x%lx eva 0x%lx", va, eva, 0, 0);
297 1.28 yamt for (i = 0; va < eva; i++, va += PAGE_SIZE) {
298 1.40 yamt boolean_t rdonly;
299 1.40 yamt vm_prot_t mask;
300 1.34 chs
301 1.28 yamt /*
302 1.28 yamt * for virtually-indexed, virtually-tagged caches we should
303 1.28 yamt * avoid creating writable mappings when we don't absolutely
304 1.28 yamt * need them, since the "compatible alias" trick doesn't work
305 1.28 yamt * on such caches. otherwise, we can always map the pages
306 1.28 yamt * writable.
307 1.28 yamt */
308 1.23 chs
309 1.23 chs #ifdef PMAP_CACHE_VIVT
310 1.28 yamt prot = VM_PROT_READ | access_type;
311 1.23 chs #else
312 1.28 yamt prot = VM_PROT_READ | VM_PROT_WRITE;
313 1.23 chs #endif
314 1.24 simonb UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i], 0, 0);
315 1.2 chs pg = pgs[i];
316 1.2 chs
317 1.2 chs if (pg == NULL || pg == PGO_DONTCARE) {
318 1.2 chs continue;
319 1.2 chs }
320 1.43 yamt
321 1.43 yamt uobj = pg->uobject;
322 1.43 yamt simple_lock(&uobj->vmobjlock);
323 1.2 chs if (pg->flags & PG_WANTED) {
324 1.2 chs wakeup(pg);
325 1.2 chs }
326 1.2 chs KASSERT((pg->flags & PG_FAKE) == 0);
327 1.2 chs if (pg->flags & PG_RELEASED) {
328 1.43 yamt uvm_lock_pageq();
329 1.18 chs uvm_pagefree(pg);
330 1.43 yamt uvm_unlock_pageq();
331 1.43 yamt simple_unlock(&uobj->vmobjlock);
332 1.2 chs continue;
333 1.2 chs }
334 1.28 yamt if (pg->loan_count != 0) {
335 1.34 chs
336 1.28 yamt /*
337 1.28 yamt * avoid unneeded loan break if possible.
338 1.28 yamt */
339 1.34 chs
340 1.28 yamt if ((access_type & VM_PROT_WRITE) == 0)
341 1.28 yamt prot &= ~VM_PROT_WRITE;
342 1.28 yamt
343 1.28 yamt if (prot & VM_PROT_WRITE) {
344 1.28 yamt pg = uvm_loanbreak(pg);
345 1.28 yamt if (pg == NULL)
346 1.28 yamt continue; /* will re-fault */
347 1.28 yamt }
348 1.28 yamt }
349 1.40 yamt
350 1.40 yamt /*
351 1.40 yamt * note that a page whose backing store is partially allocated
352 1.40 yamt * is marked as PG_RDONLY.
353 1.40 yamt */
354 1.40 yamt
355 1.40 yamt rdonly = (access_type & VM_PROT_WRITE) == 0 &&
356 1.40 yamt (pg->flags & PG_RDONLY) != 0;
357 1.40 yamt KASSERT((pg->flags & PG_RDONLY) == 0 ||
358 1.40 yamt (access_type & VM_PROT_WRITE) == 0 ||
359 1.40 yamt pg->offset < umap->writeoff ||
360 1.40 yamt pg->offset + PAGE_SIZE > umap->writeoff + umap->writelen);
361 1.40 yamt mask = rdonly ? ~VM_PROT_WRITE : VM_PROT_ALL;
362 1.18 chs pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
363 1.40 yamt prot & mask, access_type & mask);
364 1.43 yamt uvm_lock_pageq();
365 1.2 chs uvm_pageactivate(pg);
366 1.43 yamt uvm_unlock_pageq();
367 1.2 chs pg->flags &= ~(PG_BUSY);
368 1.2 chs UVM_PAGE_OWN(pg, NULL);
369 1.43 yamt simple_unlock(&uobj->vmobjlock);
370 1.2 chs }
371 1.17 chris pmap_update(ufi->orig_map->pmap);
372 1.8 chs return 0;
373 1.2 chs }
374 1.2 chs
375 1.2 chs /*
376 1.2 chs * local functions
377 1.2 chs */
378 1.2 chs
379 1.39 thorpej static struct ubc_map *
380 1.39 thorpej ubc_find_mapping(struct uvm_object *uobj, voff_t offset)
381 1.2 chs {
382 1.2 chs struct ubc_map *umap;
383 1.2 chs
384 1.2 chs LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
385 1.2 chs if (umap->uobj == uobj && umap->offset == offset) {
386 1.2 chs return umap;
387 1.2 chs }
388 1.2 chs }
389 1.2 chs return NULL;
390 1.2 chs }
391 1.2 chs
392 1.2 chs
393 1.2 chs /*
394 1.2 chs * ubc interface functions
395 1.2 chs */
396 1.2 chs
397 1.2 chs /*
398 1.18 chs * ubc_alloc: allocate a file mapping window
399 1.2 chs */
400 1.18 chs
401 1.2 chs void *
402 1.42 yamt ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
403 1.42 yamt int flags)
404 1.2 chs {
405 1.6 chs vaddr_t slot_offset, va;
406 1.2 chs struct ubc_map *umap;
407 1.6 chs voff_t umap_offset;
408 1.18 chs int error;
409 1.2 chs UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
410 1.2 chs
411 1.33 chs UVMHIST_LOG(ubchist, "uobj %p offset 0x%lx len 0x%lx",
412 1.33 chs uobj, offset, *lenp, 0);
413 1.2 chs
414 1.34 chs KASSERT(*lenp > 0);
415 1.6 chs umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
416 1.4 enami slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
417 1.18 chs *lenp = MIN(*lenp, ubc_winsize - slot_offset);
418 1.2 chs
419 1.2 chs /*
420 1.33 chs * the object is always locked here, so we don't need to add a ref.
421 1.2 chs */
422 1.2 chs
423 1.2 chs again:
424 1.2 chs simple_lock(&ubc_object.uobj.vmobjlock);
425 1.2 chs umap = ubc_find_mapping(uobj, umap_offset);
426 1.2 chs if (umap == NULL) {
427 1.2 chs umap = TAILQ_FIRST(UBC_QUEUE(offset));
428 1.2 chs if (umap == NULL) {
429 1.2 chs simple_unlock(&ubc_object.uobj.vmobjlock);
430 1.2 chs tsleep(&lbolt, PVM, "ubc_alloc", 0);
431 1.2 chs goto again;
432 1.2 chs }
433 1.2 chs
434 1.2 chs /*
435 1.18 chs * remove from old hash (if any), add to new hash.
436 1.2 chs */
437 1.2 chs
438 1.2 chs if (umap->uobj != NULL) {
439 1.2 chs LIST_REMOVE(umap, hash);
440 1.2 chs }
441 1.2 chs umap->uobj = uobj;
442 1.2 chs umap->offset = umap_offset;
443 1.2 chs LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
444 1.18 chs umap, hash);
445 1.18 chs va = UBC_UMAP_ADDR(umap);
446 1.18 chs if (umap->flags & UMAP_MAPPING_CACHED) {
447 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
448 1.18 chs pmap_remove(pmap_kernel(), va, va + ubc_winsize);
449 1.18 chs pmap_update(pmap_kernel());
450 1.18 chs }
451 1.18 chs } else {
452 1.18 chs va = UBC_UMAP_ADDR(umap);
453 1.2 chs }
454 1.2 chs
455 1.2 chs if (umap->refcount == 0) {
456 1.2 chs TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
457 1.2 chs }
458 1.2 chs
459 1.2 chs #ifdef DIAGNOSTIC
460 1.18 chs if ((flags & UBC_WRITE) && (umap->writeoff || umap->writelen)) {
461 1.33 chs panic("ubc_alloc: concurrent writes uobj %p", uobj);
462 1.2 chs }
463 1.2 chs #endif
464 1.2 chs if (flags & UBC_WRITE) {
465 1.2 chs umap->writeoff = slot_offset;
466 1.2 chs umap->writelen = *lenp;
467 1.2 chs }
468 1.2 chs
469 1.2 chs umap->refcount++;
470 1.42 yamt umap->advice = advice;
471 1.2 chs simple_unlock(&ubc_object.uobj.vmobjlock);
472 1.18 chs UVMHIST_LOG(ubchist, "umap %p refs %d va %p flags 0x%x",
473 1.18 chs umap, umap->refcount, va, flags);
474 1.18 chs
475 1.18 chs if (flags & UBC_FAULTBUSY) {
476 1.18 chs int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
477 1.18 chs struct vm_page *pgs[npages];
478 1.40 yamt int gpflags =
479 1.41 yamt PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF|PGO_NOBLOCKALLOC|
480 1.41 yamt PGO_NOTIMESTAMP;
481 1.18 chs int i;
482 1.30 dbj KDASSERT(flags & UBC_WRITE);
483 1.2 chs
484 1.18 chs if (umap->flags & UMAP_MAPPING_CACHED) {
485 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
486 1.18 chs pmap_remove(pmap_kernel(), va, va + ubc_winsize);
487 1.18 chs }
488 1.22 enami memset(pgs, 0, sizeof(pgs));
489 1.18 chs simple_lock(&uobj->vmobjlock);
490 1.33 chs error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
491 1.42 yamt &npages, 0, VM_PROT_READ | VM_PROT_WRITE, advice, gpflags);
492 1.24 simonb UVMHIST_LOG(ubchist, "faultbusy getpages %d", error, 0, 0, 0);
493 1.18 chs if (error) {
494 1.18 chs goto out;
495 1.18 chs }
496 1.18 chs for (i = 0; i < npages; i++) {
497 1.18 chs pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
498 1.18 chs VM_PAGE_TO_PHYS(pgs[i]),
499 1.18 chs VM_PROT_READ | VM_PROT_WRITE);
500 1.18 chs }
501 1.18 chs pmap_update(pmap_kernel());
502 1.18 chs umap->flags |= UMAP_PAGES_LOCKED;
503 1.18 chs }
504 1.18 chs
505 1.18 chs out:
506 1.18 chs return (void *)(va + slot_offset);
507 1.2 chs }
508 1.2 chs
509 1.18 chs /*
510 1.18 chs * ubc_release: free a file mapping window.
511 1.18 chs */
512 1.2 chs
513 1.2 chs void
514 1.39 thorpej ubc_release(void *va, int flags)
515 1.2 chs {
516 1.2 chs struct ubc_map *umap;
517 1.2 chs struct uvm_object *uobj;
518 1.18 chs vaddr_t umapva;
519 1.18 chs boolean_t unmapped;
520 1.2 chs UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
521 1.2 chs
522 1.24 simonb UVMHIST_LOG(ubchist, "va %p", va, 0, 0, 0);
523 1.11 chs umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
524 1.18 chs umapva = UBC_UMAP_ADDR(umap);
525 1.2 chs uobj = umap->uobj;
526 1.2 chs KASSERT(uobj != NULL);
527 1.2 chs
528 1.18 chs if (umap->flags & UMAP_PAGES_LOCKED) {
529 1.18 chs int slot_offset = umap->writeoff;
530 1.18 chs int endoff = umap->writeoff + umap->writelen;
531 1.18 chs int zerolen = round_page(endoff) - endoff;
532 1.18 chs int npages = (int)(round_page(umap->writeoff + umap->writelen)
533 1.18 chs - trunc_page(umap->writeoff)) >> PAGE_SHIFT;
534 1.18 chs struct vm_page *pgs[npages];
535 1.18 chs paddr_t pa;
536 1.18 chs int i;
537 1.18 chs boolean_t rv;
538 1.18 chs
539 1.18 chs if (zerolen) {
540 1.18 chs memset((char *)umapva + endoff, 0, zerolen);
541 1.18 chs }
542 1.18 chs umap->flags &= ~UMAP_PAGES_LOCKED;
543 1.18 chs uvm_lock_pageq();
544 1.18 chs for (i = 0; i < npages; i++) {
545 1.18 chs rv = pmap_extract(pmap_kernel(),
546 1.18 chs umapva + slot_offset + (i << PAGE_SHIFT), &pa);
547 1.18 chs KASSERT(rv);
548 1.18 chs pgs[i] = PHYS_TO_VM_PAGE(pa);
549 1.18 chs pgs[i]->flags &= ~(PG_FAKE|PG_CLEAN);
550 1.28 yamt KASSERT(pgs[i]->loan_count == 0);
551 1.18 chs uvm_pageactivate(pgs[i]);
552 1.18 chs }
553 1.18 chs uvm_unlock_pageq();
554 1.18 chs pmap_kremove(umapva, ubc_winsize);
555 1.18 chs pmap_update(pmap_kernel());
556 1.32 yamt simple_lock(&uobj->vmobjlock);
557 1.18 chs uvm_page_unbusy(pgs, npages);
558 1.32 yamt simple_unlock(&uobj->vmobjlock);
559 1.18 chs unmapped = TRUE;
560 1.18 chs } else {
561 1.18 chs unmapped = FALSE;
562 1.18 chs }
563 1.18 chs
564 1.18 chs simple_lock(&ubc_object.uobj.vmobjlock);
565 1.2 chs umap->writeoff = 0;
566 1.2 chs umap->writelen = 0;
567 1.2 chs umap->refcount--;
568 1.2 chs if (umap->refcount == 0) {
569 1.33 chs if (flags & UBC_UNMAP) {
570 1.2 chs
571 1.2 chs /*
572 1.33 chs * Invalidate any cached mappings if requested.
573 1.33 chs * This is typically used to avoid leaving
574 1.33 chs * incompatible cache aliases around indefinitely.
575 1.2 chs */
576 1.2 chs
577 1.18 chs pmap_remove(pmap_kernel(), umapva,
578 1.18 chs umapva + ubc_winsize);
579 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
580 1.17 chris pmap_update(pmap_kernel());
581 1.2 chs LIST_REMOVE(umap, hash);
582 1.2 chs umap->uobj = NULL;
583 1.2 chs TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
584 1.2 chs inactive);
585 1.2 chs } else {
586 1.18 chs if (!unmapped) {
587 1.18 chs umap->flags |= UMAP_MAPPING_CACHED;
588 1.18 chs }
589 1.2 chs TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
590 1.2 chs inactive);
591 1.2 chs }
592 1.2 chs }
593 1.24 simonb UVMHIST_LOG(ubchist, "umap %p refs %d", umap, umap->refcount, 0, 0);
594 1.2 chs simple_unlock(&ubc_object.uobj.vmobjlock);
595 1.2 chs }
596 1.2 chs
597 1.2 chs
598 1.29 yamt #if 0 /* notused */
599 1.2 chs /*
600 1.2 chs * removing a range of mappings from the ubc mapping cache.
601 1.2 chs */
602 1.2 chs
603 1.2 chs void
604 1.39 thorpej ubc_flush(struct uvm_object *uobj, voff_t start, voff_t end)
605 1.2 chs {
606 1.2 chs struct ubc_map *umap;
607 1.2 chs vaddr_t va;
608 1.2 chs UVMHIST_FUNC("ubc_flush"); UVMHIST_CALLED(ubchist);
609 1.2 chs
610 1.2 chs UVMHIST_LOG(ubchist, "uobj %p start 0x%lx end 0x%lx",
611 1.24 simonb uobj, start, end, 0);
612 1.2 chs
613 1.2 chs simple_lock(&ubc_object.uobj.vmobjlock);
614 1.2 chs for (umap = ubc_object.umap;
615 1.2 chs umap < &ubc_object.umap[ubc_nwins];
616 1.2 chs umap++) {
617 1.2 chs
618 1.18 chs if (umap->uobj != uobj || umap->offset < start ||
619 1.2 chs (umap->offset >= end && end != 0) ||
620 1.2 chs umap->refcount > 0) {
621 1.2 chs continue;
622 1.2 chs }
623 1.2 chs
624 1.2 chs /*
625 1.2 chs * remove from hash,
626 1.2 chs * move to head of inactive queue.
627 1.2 chs */
628 1.2 chs
629 1.2 chs va = (vaddr_t)(ubc_object.kva +
630 1.18 chs ((umap - ubc_object.umap) << ubc_winshift));
631 1.4 enami pmap_remove(pmap_kernel(), va, va + ubc_winsize);
632 1.2 chs
633 1.2 chs LIST_REMOVE(umap, hash);
634 1.2 chs umap->uobj = NULL;
635 1.2 chs TAILQ_REMOVE(UBC_QUEUE(umap->offset), umap, inactive);
636 1.2 chs TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap, inactive);
637 1.2 chs }
638 1.18 chs pmap_update(pmap_kernel());
639 1.2 chs simple_unlock(&ubc_object.uobj.vmobjlock);
640 1.2 chs }
641 1.29 yamt #endif /* notused */
642