uvm_bio.c revision 1.67 1 1.67 pooka /* $NetBSD: uvm_bio.c,v 1.67 2009/08/04 23:31:57 pooka 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.67 pooka __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.67 2009/08/04 23:31:57 pooka Exp $");
38 1.21 lukem
39 1.21 lukem #include "opt_uvmhist.h"
40 1.50 yamt #include "opt_ubc.h"
41 1.2 chs
42 1.2 chs #include <sys/param.h>
43 1.2 chs #include <sys/systm.h>
44 1.65 ad #include <sys/kmem.h>
45 1.2 chs #include <sys/kernel.h>
46 1.60 ad #include <sys/proc.h>
47 1.67 pooka #include <sys/vnode.h>
48 1.2 chs
49 1.2 chs #include <uvm/uvm.h>
50 1.2 chs
51 1.2 chs /*
52 1.2 chs * global data structures
53 1.2 chs */
54 1.2 chs
55 1.2 chs /*
56 1.2 chs * local functions
57 1.2 chs */
58 1.2 chs
59 1.39 thorpej static int ubc_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
60 1.44 drochner int, int, vm_prot_t, int);
61 1.39 thorpej static struct ubc_map *ubc_find_mapping(struct uvm_object *, voff_t);
62 1.2 chs
63 1.2 chs /*
64 1.2 chs * local data structues
65 1.2 chs */
66 1.2 chs
67 1.18 chs #define UBC_HASH(uobj, offset) \
68 1.18 chs (((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
69 1.2 chs ubc_object.hashmask)
70 1.2 chs
71 1.18 chs #define UBC_QUEUE(offset) \
72 1.18 chs (&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) & \
73 1.18 chs (UBC_NQUEUES - 1)])
74 1.18 chs
75 1.18 chs #define UBC_UMAP_ADDR(u) \
76 1.18 chs (vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
77 1.18 chs
78 1.18 chs
79 1.18 chs #define UMAP_PAGES_LOCKED 0x0001
80 1.18 chs #define UMAP_MAPPING_CACHED 0x0002
81 1.2 chs
82 1.2 chs struct ubc_map
83 1.2 chs {
84 1.2 chs struct uvm_object * uobj; /* mapped object */
85 1.2 chs voff_t offset; /* offset into uobj */
86 1.33 chs voff_t writeoff; /* write offset */
87 1.33 chs vsize_t writelen; /* write len */
88 1.18 chs int refcount; /* refcount on mapping */
89 1.18 chs int flags; /* extra state */
90 1.42 yamt int advice;
91 1.2 chs
92 1.2 chs LIST_ENTRY(ubc_map) hash; /* hash table */
93 1.2 chs TAILQ_ENTRY(ubc_map) inactive; /* inactive queue */
94 1.2 chs };
95 1.2 chs
96 1.2 chs static struct ubc_object
97 1.2 chs {
98 1.2 chs struct uvm_object uobj; /* glue for uvm_map() */
99 1.2 chs char *kva; /* where ubc_object is mapped */
100 1.2 chs struct ubc_map *umap; /* array of ubc_map's */
101 1.2 chs
102 1.2 chs LIST_HEAD(, ubc_map) *hash; /* hashtable for cached ubc_map's */
103 1.2 chs u_long hashmask; /* mask for hashtable */
104 1.2 chs
105 1.2 chs TAILQ_HEAD(ubc_inactive_head, ubc_map) *inactive;
106 1.2 chs /* inactive queues for ubc_map's */
107 1.2 chs
108 1.2 chs } ubc_object;
109 1.2 chs
110 1.63 yamt const struct uvm_pagerops ubc_pager = {
111 1.48 christos .pgo_fault = ubc_fault,
112 1.2 chs /* ... rest are NULL */
113 1.2 chs };
114 1.2 chs
115 1.2 chs int ubc_nwins = UBC_NWINS;
116 1.11 chs int ubc_winshift = UBC_WINSHIFT;
117 1.11 chs int ubc_winsize;
118 1.27 thorpej #if defined(PMAP_PREFER)
119 1.2 chs int ubc_nqueues;
120 1.2 chs #define UBC_NQUEUES ubc_nqueues
121 1.2 chs #else
122 1.2 chs #define UBC_NQUEUES 1
123 1.2 chs #endif
124 1.2 chs
125 1.50 yamt #if defined(UBC_STATS)
126 1.50 yamt
127 1.50 yamt #define UBC_EVCNT_DEFINE(name) \
128 1.50 yamt struct evcnt ubc_evcnt_##name = \
129 1.50 yamt EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "ubc", #name); \
130 1.50 yamt EVCNT_ATTACH_STATIC(ubc_evcnt_##name);
131 1.50 yamt #define UBC_EVCNT_INCR(name) ubc_evcnt_##name.ev_count++
132 1.50 yamt
133 1.50 yamt #else /* defined(UBC_STATS) */
134 1.50 yamt
135 1.50 yamt #define UBC_EVCNT_DEFINE(name) /* nothing */
136 1.50 yamt #define UBC_EVCNT_INCR(name) /* nothing */
137 1.50 yamt
138 1.50 yamt #endif /* defined(UBC_STATS) */
139 1.50 yamt
140 1.50 yamt UBC_EVCNT_DEFINE(wincachehit)
141 1.50 yamt UBC_EVCNT_DEFINE(wincachemiss)
142 1.57 yamt UBC_EVCNT_DEFINE(faultbusy)
143 1.50 yamt
144 1.2 chs /*
145 1.2 chs * ubc_init
146 1.2 chs *
147 1.2 chs * init pager private data structures.
148 1.2 chs */
149 1.2 chs
150 1.2 chs void
151 1.2 chs ubc_init(void)
152 1.2 chs {
153 1.2 chs struct ubc_map *umap;
154 1.2 chs vaddr_t va;
155 1.2 chs int i;
156 1.15 simonb
157 1.15 simonb /*
158 1.15 simonb * Make sure ubc_winshift is sane.
159 1.15 simonb */
160 1.15 simonb if (ubc_winshift < PAGE_SHIFT)
161 1.15 simonb ubc_winshift = PAGE_SHIFT;
162 1.2 chs
163 1.2 chs /*
164 1.2 chs * init ubc_object.
165 1.2 chs * alloc and init ubc_map's.
166 1.2 chs * init inactive queues.
167 1.2 chs * alloc and init hashtable.
168 1.2 chs * map in ubc_object.
169 1.2 chs */
170 1.2 chs
171 1.38 yamt UVM_OBJ_INIT(&ubc_object.uobj, &ubc_pager, UVM_OBJ_KERN);
172 1.2 chs
173 1.65 ad ubc_object.umap = kmem_zalloc(ubc_nwins * sizeof(struct ubc_map),
174 1.65 ad KM_SLEEP);
175 1.7 enami if (ubc_object.umap == NULL)
176 1.7 enami panic("ubc_init: failed to allocate ubc_map");
177 1.2 chs
178 1.18 chs if (ubc_winshift < PAGE_SHIFT) {
179 1.18 chs ubc_winshift = PAGE_SHIFT;
180 1.18 chs }
181 1.2 chs va = (vaddr_t)1L;
182 1.2 chs #ifdef PMAP_PREFER
183 1.36 atatat PMAP_PREFER(0, &va, 0, 0); /* kernel is never topdown */
184 1.11 chs ubc_nqueues = va >> ubc_winshift;
185 1.11 chs if (ubc_nqueues == 0) {
186 1.11 chs ubc_nqueues = 1;
187 1.2 chs }
188 1.2 chs #endif
189 1.11 chs ubc_winsize = 1 << ubc_winshift;
190 1.65 ad ubc_object.inactive = kmem_alloc(UBC_NQUEUES *
191 1.65 ad sizeof(struct ubc_inactive_head), KM_SLEEP);
192 1.7 enami if (ubc_object.inactive == NULL)
193 1.7 enami panic("ubc_init: failed to allocate inactive queue heads");
194 1.2 chs for (i = 0; i < UBC_NQUEUES; i++) {
195 1.2 chs TAILQ_INIT(&ubc_object.inactive[i]);
196 1.2 chs }
197 1.2 chs for (i = 0; i < ubc_nwins; i++) {
198 1.2 chs umap = &ubc_object.umap[i];
199 1.2 chs TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
200 1.2 chs umap, inactive);
201 1.2 chs }
202 1.2 chs
203 1.65 ad ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, true,
204 1.65 ad &ubc_object.hashmask);
205 1.2 chs for (i = 0; i <= ubc_object.hashmask; i++) {
206 1.2 chs LIST_INIT(&ubc_object.hash[i]);
207 1.2 chs }
208 1.2 chs
209 1.2 chs if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
210 1.11 chs ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
211 1.2 chs UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
212 1.9 chs UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
213 1.26 provos panic("ubc_init: failed to map ubc_object");
214 1.2 chs }
215 1.2 chs UVMHIST_INIT(ubchist, 300);
216 1.2 chs }
217 1.2 chs
218 1.2 chs /*
219 1.2 chs * ubc_fault: fault routine for ubc mapping
220 1.2 chs */
221 1.18 chs
222 1.39 thorpej static int
223 1.54 yamt ubc_fault(struct uvm_faultinfo *ufi, vaddr_t ign1, struct vm_page **ign2,
224 1.54 yamt int ign3, int ign4, vm_prot_t access_type, int flags)
225 1.2 chs {
226 1.2 chs struct uvm_object *uobj;
227 1.2 chs struct ubc_map *umap;
228 1.2 chs vaddr_t va, eva, ubc_offset, slot_offset;
229 1.18 chs int i, error, npages;
230 1.18 chs struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT], *pg;
231 1.23 chs vm_prot_t prot;
232 1.2 chs UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
233 1.2 chs
234 1.2 chs /*
235 1.2 chs * no need to try with PGO_LOCKED...
236 1.2 chs * we don't need to have the map locked since we know that
237 1.2 chs * no one will mess with it until our reference is released.
238 1.2 chs */
239 1.18 chs
240 1.2 chs if (flags & PGO_LOCKED) {
241 1.2 chs uvmfault_unlockall(ufi, NULL, &ubc_object.uobj, NULL);
242 1.2 chs flags &= ~PGO_LOCKED;
243 1.2 chs }
244 1.2 chs
245 1.2 chs va = ufi->orig_rvaddr;
246 1.2 chs ubc_offset = va - (vaddr_t)ubc_object.kva;
247 1.11 chs umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
248 1.2 chs KASSERT(umap->refcount != 0);
249 1.53 yamt KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
250 1.18 chs slot_offset = ubc_offset & (ubc_winsize - 1);
251 1.2 chs
252 1.34 chs /*
253 1.34 chs * some platforms cannot write to individual bytes atomically, so
254 1.34 chs * software has to do read/modify/write of larger quantities instead.
255 1.34 chs * this means that the access_type for "write" operations
256 1.34 chs * can be VM_PROT_READ, which confuses us mightily.
257 1.37 perry *
258 1.34 chs * deal with this by resetting access_type based on the info
259 1.34 chs * that ubc_alloc() stores for us.
260 1.34 chs */
261 1.34 chs
262 1.34 chs access_type = umap->writelen ? VM_PROT_WRITE : VM_PROT_READ;
263 1.34 chs UVMHIST_LOG(ubchist, "va 0x%lx ubc_offset 0x%lx access_type %d",
264 1.34 chs va, ubc_offset, access_type, 0);
265 1.34 chs
266 1.33 chs #ifdef DIAGNOSTIC
267 1.33 chs if ((access_type & VM_PROT_WRITE) != 0) {
268 1.33 chs if (slot_offset < trunc_page(umap->writeoff) ||
269 1.33 chs umap->writeoff + umap->writelen <= slot_offset) {
270 1.33 chs panic("ubc_fault: out of range write");
271 1.33 chs }
272 1.33 chs }
273 1.33 chs #endif
274 1.33 chs
275 1.2 chs /* no umap locking needed since we have a ref on the umap */
276 1.2 chs uobj = umap->uobj;
277 1.2 chs
278 1.33 chs if ((access_type & VM_PROT_WRITE) == 0) {
279 1.33 chs npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
280 1.33 chs } else {
281 1.33 chs npages = (round_page(umap->offset + umap->writeoff +
282 1.33 chs umap->writelen) - (umap->offset + slot_offset))
283 1.33 chs >> PAGE_SHIFT;
284 1.33 chs flags |= PGO_PASTEOF;
285 1.33 chs }
286 1.2 chs
287 1.2 chs again:
288 1.2 chs memset(pgs, 0, sizeof (pgs));
289 1.64 ad mutex_enter(&uobj->vmobjlock);
290 1.2 chs
291 1.33 chs UVMHIST_LOG(ubchist, "slot_offset 0x%x writeoff 0x%x writelen 0x%x ",
292 1.33 chs slot_offset, umap->writeoff, umap->writelen, 0);
293 1.33 chs UVMHIST_LOG(ubchist, "getpages uobj %p offset 0x%x npages %d",
294 1.18 chs uobj, umap->offset + slot_offset, npages, 0);
295 1.2 chs
296 1.33 chs error = (*uobj->pgops->pgo_get)(uobj, umap->offset + slot_offset, pgs,
297 1.42 yamt &npages, 0, access_type, umap->advice, flags | PGO_NOBLOCKALLOC |
298 1.41 yamt PGO_NOTIMESTAMP);
299 1.24 simonb UVMHIST_LOG(ubchist, "getpages error %d npages %d", error, npages, 0,
300 1.24 simonb 0);
301 1.2 chs
302 1.5 chs if (error == EAGAIN) {
303 1.60 ad kpause("ubc_fault", false, hz, NULL);
304 1.2 chs goto again;
305 1.2 chs }
306 1.5 chs if (error) {
307 1.10 chs return error;
308 1.5 chs }
309 1.2 chs
310 1.2 chs va = ufi->orig_rvaddr;
311 1.2 chs eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
312 1.2 chs
313 1.28 yamt UVMHIST_LOG(ubchist, "va 0x%lx eva 0x%lx", va, eva, 0, 0);
314 1.28 yamt for (i = 0; va < eva; i++, va += PAGE_SIZE) {
315 1.55 thorpej bool rdonly;
316 1.40 yamt vm_prot_t mask;
317 1.34 chs
318 1.28 yamt /*
319 1.28 yamt * for virtually-indexed, virtually-tagged caches we should
320 1.28 yamt * avoid creating writable mappings when we don't absolutely
321 1.28 yamt * need them, since the "compatible alias" trick doesn't work
322 1.28 yamt * on such caches. otherwise, we can always map the pages
323 1.28 yamt * writable.
324 1.28 yamt */
325 1.23 chs
326 1.23 chs #ifdef PMAP_CACHE_VIVT
327 1.28 yamt prot = VM_PROT_READ | access_type;
328 1.23 chs #else
329 1.28 yamt prot = VM_PROT_READ | VM_PROT_WRITE;
330 1.23 chs #endif
331 1.24 simonb UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i], 0, 0);
332 1.2 chs pg = pgs[i];
333 1.2 chs
334 1.2 chs if (pg == NULL || pg == PGO_DONTCARE) {
335 1.2 chs continue;
336 1.2 chs }
337 1.43 yamt
338 1.43 yamt uobj = pg->uobject;
339 1.64 ad mutex_enter(&uobj->vmobjlock);
340 1.2 chs if (pg->flags & PG_WANTED) {
341 1.2 chs wakeup(pg);
342 1.2 chs }
343 1.2 chs KASSERT((pg->flags & PG_FAKE) == 0);
344 1.2 chs if (pg->flags & PG_RELEASED) {
345 1.64 ad mutex_enter(&uvm_pageqlock);
346 1.18 chs uvm_pagefree(pg);
347 1.64 ad mutex_exit(&uvm_pageqlock);
348 1.64 ad mutex_exit(&uobj->vmobjlock);
349 1.2 chs continue;
350 1.2 chs }
351 1.28 yamt if (pg->loan_count != 0) {
352 1.34 chs
353 1.28 yamt /*
354 1.28 yamt * avoid unneeded loan break if possible.
355 1.28 yamt */
356 1.34 chs
357 1.28 yamt if ((access_type & VM_PROT_WRITE) == 0)
358 1.28 yamt prot &= ~VM_PROT_WRITE;
359 1.28 yamt
360 1.28 yamt if (prot & VM_PROT_WRITE) {
361 1.47 yamt struct vm_page *newpg;
362 1.47 yamt
363 1.47 yamt newpg = uvm_loanbreak(pg);
364 1.47 yamt if (newpg == NULL) {
365 1.47 yamt uvm_page_unbusy(&pg, 1);
366 1.64 ad mutex_exit(&uobj->vmobjlock);
367 1.47 yamt uvm_wait("ubc_loanbrk");
368 1.28 yamt continue; /* will re-fault */
369 1.47 yamt }
370 1.47 yamt pg = newpg;
371 1.28 yamt }
372 1.28 yamt }
373 1.40 yamt
374 1.40 yamt /*
375 1.40 yamt * note that a page whose backing store is partially allocated
376 1.40 yamt * is marked as PG_RDONLY.
377 1.40 yamt */
378 1.40 yamt
379 1.49 yamt rdonly = ((access_type & VM_PROT_WRITE) == 0 &&
380 1.49 yamt (pg->flags & PG_RDONLY) != 0) ||
381 1.49 yamt UVM_OBJ_NEEDS_WRITEFAULT(uobj);
382 1.40 yamt KASSERT((pg->flags & PG_RDONLY) == 0 ||
383 1.40 yamt (access_type & VM_PROT_WRITE) == 0 ||
384 1.40 yamt pg->offset < umap->writeoff ||
385 1.40 yamt pg->offset + PAGE_SIZE > umap->writeoff + umap->writelen);
386 1.40 yamt mask = rdonly ? ~VM_PROT_WRITE : VM_PROT_ALL;
387 1.46 yamt error = pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
388 1.46 yamt prot & mask, PMAP_CANFAIL | (access_type & mask));
389 1.64 ad mutex_enter(&uvm_pageqlock);
390 1.2 chs uvm_pageactivate(pg);
391 1.64 ad mutex_exit(&uvm_pageqlock);
392 1.45 yamt pg->flags &= ~(PG_BUSY|PG_WANTED);
393 1.2 chs UVM_PAGE_OWN(pg, NULL);
394 1.64 ad mutex_exit(&uobj->vmobjlock);
395 1.46 yamt if (error) {
396 1.46 yamt UVMHIST_LOG(ubchist, "pmap_enter fail %d",
397 1.46 yamt error, 0, 0, 0);
398 1.46 yamt uvm_wait("ubc_pmfail");
399 1.46 yamt /* will refault */
400 1.46 yamt }
401 1.2 chs }
402 1.17 chris pmap_update(ufi->orig_map->pmap);
403 1.8 chs return 0;
404 1.2 chs }
405 1.2 chs
406 1.2 chs /*
407 1.2 chs * local functions
408 1.2 chs */
409 1.2 chs
410 1.39 thorpej static struct ubc_map *
411 1.39 thorpej ubc_find_mapping(struct uvm_object *uobj, voff_t offset)
412 1.2 chs {
413 1.2 chs struct ubc_map *umap;
414 1.2 chs
415 1.2 chs LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
416 1.2 chs if (umap->uobj == uobj && umap->offset == offset) {
417 1.2 chs return umap;
418 1.2 chs }
419 1.2 chs }
420 1.2 chs return NULL;
421 1.2 chs }
422 1.2 chs
423 1.2 chs
424 1.2 chs /*
425 1.2 chs * ubc interface functions
426 1.2 chs */
427 1.2 chs
428 1.2 chs /*
429 1.18 chs * ubc_alloc: allocate a file mapping window
430 1.2 chs */
431 1.18 chs
432 1.2 chs void *
433 1.42 yamt ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
434 1.42 yamt int flags)
435 1.2 chs {
436 1.6 chs vaddr_t slot_offset, va;
437 1.2 chs struct ubc_map *umap;
438 1.6 chs voff_t umap_offset;
439 1.18 chs int error;
440 1.2 chs UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
441 1.2 chs
442 1.33 chs UVMHIST_LOG(ubchist, "uobj %p offset 0x%lx len 0x%lx",
443 1.33 chs uobj, offset, *lenp, 0);
444 1.2 chs
445 1.34 chs KASSERT(*lenp > 0);
446 1.6 chs umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
447 1.4 enami slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
448 1.18 chs *lenp = MIN(*lenp, ubc_winsize - slot_offset);
449 1.2 chs
450 1.2 chs /*
451 1.33 chs * the object is always locked here, so we don't need to add a ref.
452 1.2 chs */
453 1.2 chs
454 1.2 chs again:
455 1.64 ad mutex_enter(&ubc_object.uobj.vmobjlock);
456 1.2 chs umap = ubc_find_mapping(uobj, umap_offset);
457 1.2 chs if (umap == NULL) {
458 1.50 yamt UBC_EVCNT_INCR(wincachemiss);
459 1.2 chs umap = TAILQ_FIRST(UBC_QUEUE(offset));
460 1.2 chs if (umap == NULL) {
461 1.64 ad mutex_exit(&ubc_object.uobj.vmobjlock);
462 1.60 ad kpause("ubc_alloc", false, hz, NULL);
463 1.2 chs goto again;
464 1.2 chs }
465 1.2 chs
466 1.2 chs /*
467 1.18 chs * remove from old hash (if any), add to new hash.
468 1.2 chs */
469 1.2 chs
470 1.2 chs if (umap->uobj != NULL) {
471 1.2 chs LIST_REMOVE(umap, hash);
472 1.2 chs }
473 1.2 chs umap->uobj = uobj;
474 1.2 chs umap->offset = umap_offset;
475 1.2 chs LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
476 1.18 chs umap, hash);
477 1.18 chs va = UBC_UMAP_ADDR(umap);
478 1.18 chs if (umap->flags & UMAP_MAPPING_CACHED) {
479 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
480 1.18 chs pmap_remove(pmap_kernel(), va, va + ubc_winsize);
481 1.18 chs pmap_update(pmap_kernel());
482 1.18 chs }
483 1.18 chs } else {
484 1.50 yamt UBC_EVCNT_INCR(wincachehit);
485 1.18 chs va = UBC_UMAP_ADDR(umap);
486 1.2 chs }
487 1.2 chs
488 1.2 chs if (umap->refcount == 0) {
489 1.2 chs TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
490 1.2 chs }
491 1.2 chs
492 1.2 chs #ifdef DIAGNOSTIC
493 1.18 chs if ((flags & UBC_WRITE) && (umap->writeoff || umap->writelen)) {
494 1.33 chs panic("ubc_alloc: concurrent writes uobj %p", uobj);
495 1.2 chs }
496 1.2 chs #endif
497 1.2 chs if (flags & UBC_WRITE) {
498 1.2 chs umap->writeoff = slot_offset;
499 1.2 chs umap->writelen = *lenp;
500 1.2 chs }
501 1.2 chs
502 1.2 chs umap->refcount++;
503 1.42 yamt umap->advice = advice;
504 1.64 ad mutex_exit(&ubc_object.uobj.vmobjlock);
505 1.18 chs UVMHIST_LOG(ubchist, "umap %p refs %d va %p flags 0x%x",
506 1.18 chs umap, umap->refcount, va, flags);
507 1.18 chs
508 1.18 chs if (flags & UBC_FAULTBUSY) {
509 1.18 chs int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
510 1.18 chs struct vm_page *pgs[npages];
511 1.40 yamt int gpflags =
512 1.41 yamt PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF|PGO_NOBLOCKALLOC|
513 1.41 yamt PGO_NOTIMESTAMP;
514 1.18 chs int i;
515 1.30 dbj KDASSERT(flags & UBC_WRITE);
516 1.57 yamt KASSERT(umap->refcount == 1);
517 1.2 chs
518 1.57 yamt UBC_EVCNT_INCR(faultbusy);
519 1.18 chs if (umap->flags & UMAP_MAPPING_CACHED) {
520 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
521 1.18 chs pmap_remove(pmap_kernel(), va, va + ubc_winsize);
522 1.18 chs }
523 1.59 yamt again_faultbusy:
524 1.22 enami memset(pgs, 0, sizeof(pgs));
525 1.64 ad mutex_enter(&uobj->vmobjlock);
526 1.33 chs error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
527 1.42 yamt &npages, 0, VM_PROT_READ | VM_PROT_WRITE, advice, gpflags);
528 1.24 simonb UVMHIST_LOG(ubchist, "faultbusy getpages %d", error, 0, 0, 0);
529 1.18 chs if (error) {
530 1.18 chs goto out;
531 1.18 chs }
532 1.18 chs for (i = 0; i < npages; i++) {
533 1.59 yamt struct vm_page *pg = pgs[i];
534 1.59 yamt
535 1.59 yamt KASSERT(pg->uobject == uobj);
536 1.59 yamt if (pg->loan_count != 0) {
537 1.64 ad mutex_enter(&uobj->vmobjlock);
538 1.59 yamt if (pg->loan_count != 0) {
539 1.59 yamt pg = uvm_loanbreak(pg);
540 1.59 yamt }
541 1.64 ad mutex_exit(&uobj->vmobjlock);
542 1.59 yamt if (pg == NULL) {
543 1.59 yamt pmap_kremove(va, ubc_winsize);
544 1.59 yamt pmap_update(pmap_kernel());
545 1.64 ad mutex_enter(&uobj->vmobjlock);
546 1.59 yamt uvm_page_unbusy(pgs, npages);
547 1.64 ad mutex_exit(&uobj->vmobjlock);
548 1.59 yamt uvm_wait("ubc_alloc");
549 1.59 yamt goto again_faultbusy;
550 1.59 yamt }
551 1.59 yamt pgs[i] = pg;
552 1.59 yamt }
553 1.18 chs pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
554 1.59 yamt VM_PAGE_TO_PHYS(pg), VM_PROT_READ | VM_PROT_WRITE);
555 1.18 chs }
556 1.18 chs pmap_update(pmap_kernel());
557 1.18 chs umap->flags |= UMAP_PAGES_LOCKED;
558 1.57 yamt } else {
559 1.57 yamt KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
560 1.18 chs }
561 1.18 chs
562 1.18 chs out:
563 1.18 chs return (void *)(va + slot_offset);
564 1.2 chs }
565 1.2 chs
566 1.18 chs /*
567 1.18 chs * ubc_release: free a file mapping window.
568 1.18 chs */
569 1.2 chs
570 1.2 chs void
571 1.39 thorpej ubc_release(void *va, int flags)
572 1.2 chs {
573 1.2 chs struct ubc_map *umap;
574 1.2 chs struct uvm_object *uobj;
575 1.18 chs vaddr_t umapva;
576 1.55 thorpej bool unmapped;
577 1.2 chs UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
578 1.2 chs
579 1.24 simonb UVMHIST_LOG(ubchist, "va %p", va, 0, 0, 0);
580 1.11 chs umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
581 1.18 chs umapva = UBC_UMAP_ADDR(umap);
582 1.2 chs uobj = umap->uobj;
583 1.2 chs KASSERT(uobj != NULL);
584 1.2 chs
585 1.18 chs if (umap->flags & UMAP_PAGES_LOCKED) {
586 1.18 chs int slot_offset = umap->writeoff;
587 1.18 chs int endoff = umap->writeoff + umap->writelen;
588 1.18 chs int zerolen = round_page(endoff) - endoff;
589 1.18 chs int npages = (int)(round_page(umap->writeoff + umap->writelen)
590 1.18 chs - trunc_page(umap->writeoff)) >> PAGE_SHIFT;
591 1.18 chs struct vm_page *pgs[npages];
592 1.18 chs paddr_t pa;
593 1.18 chs int i;
594 1.55 thorpej bool rv;
595 1.18 chs
596 1.57 yamt KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
597 1.18 chs if (zerolen) {
598 1.18 chs memset((char *)umapva + endoff, 0, zerolen);
599 1.18 chs }
600 1.18 chs umap->flags &= ~UMAP_PAGES_LOCKED;
601 1.64 ad mutex_enter(&uvm_pageqlock);
602 1.18 chs for (i = 0; i < npages; i++) {
603 1.18 chs rv = pmap_extract(pmap_kernel(),
604 1.18 chs umapva + slot_offset + (i << PAGE_SHIFT), &pa);
605 1.18 chs KASSERT(rv);
606 1.18 chs pgs[i] = PHYS_TO_VM_PAGE(pa);
607 1.18 chs pgs[i]->flags &= ~(PG_FAKE|PG_CLEAN);
608 1.28 yamt KASSERT(pgs[i]->loan_count == 0);
609 1.18 chs uvm_pageactivate(pgs[i]);
610 1.18 chs }
611 1.64 ad mutex_exit(&uvm_pageqlock);
612 1.18 chs pmap_kremove(umapva, ubc_winsize);
613 1.18 chs pmap_update(pmap_kernel());
614 1.64 ad mutex_enter(&uobj->vmobjlock);
615 1.18 chs uvm_page_unbusy(pgs, npages);
616 1.64 ad mutex_exit(&uobj->vmobjlock);
617 1.56 thorpej unmapped = true;
618 1.18 chs } else {
619 1.56 thorpej unmapped = false;
620 1.18 chs }
621 1.18 chs
622 1.64 ad mutex_enter(&ubc_object.uobj.vmobjlock);
623 1.2 chs umap->writeoff = 0;
624 1.2 chs umap->writelen = 0;
625 1.2 chs umap->refcount--;
626 1.2 chs if (umap->refcount == 0) {
627 1.33 chs if (flags & UBC_UNMAP) {
628 1.2 chs
629 1.2 chs /*
630 1.33 chs * Invalidate any cached mappings if requested.
631 1.33 chs * This is typically used to avoid leaving
632 1.33 chs * incompatible cache aliases around indefinitely.
633 1.2 chs */
634 1.2 chs
635 1.18 chs pmap_remove(pmap_kernel(), umapva,
636 1.18 chs umapva + ubc_winsize);
637 1.18 chs umap->flags &= ~UMAP_MAPPING_CACHED;
638 1.17 chris pmap_update(pmap_kernel());
639 1.2 chs LIST_REMOVE(umap, hash);
640 1.2 chs umap->uobj = NULL;
641 1.2 chs TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
642 1.2 chs inactive);
643 1.2 chs } else {
644 1.18 chs if (!unmapped) {
645 1.18 chs umap->flags |= UMAP_MAPPING_CACHED;
646 1.18 chs }
647 1.2 chs TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
648 1.2 chs inactive);
649 1.2 chs }
650 1.2 chs }
651 1.24 simonb UVMHIST_LOG(ubchist, "umap %p refs %d", umap, umap->refcount, 0, 0);
652 1.64 ad mutex_exit(&ubc_object.uobj.vmobjlock);
653 1.2 chs }
654 1.2 chs
655 1.58 yamt /*
656 1.58 yamt * ubc_uiomove: move data to/from an object.
657 1.58 yamt */
658 1.58 yamt
659 1.58 yamt int
660 1.62 yamt ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
661 1.62 yamt int flags)
662 1.58 yamt {
663 1.58 yamt voff_t off;
664 1.58 yamt const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
665 1.58 yamt int error;
666 1.58 yamt
667 1.58 yamt KASSERT(todo <= uio->uio_resid);
668 1.58 yamt KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
669 1.58 yamt ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
670 1.58 yamt
671 1.58 yamt off = uio->uio_offset;
672 1.58 yamt error = 0;
673 1.58 yamt while (todo > 0) {
674 1.58 yamt vsize_t bytelen = todo;
675 1.58 yamt void *win;
676 1.58 yamt
677 1.62 yamt win = ubc_alloc(uobj, off, &bytelen, advice, flags);
678 1.58 yamt if (error == 0) {
679 1.58 yamt error = uiomove(win, bytelen, uio);
680 1.58 yamt }
681 1.58 yamt if (error != 0 && overwrite) {
682 1.58 yamt /*
683 1.58 yamt * if we haven't initialized the pages yet,
684 1.58 yamt * do it now. it's safe to use memset here
685 1.58 yamt * because we just mapped the pages above.
686 1.58 yamt */
687 1.64 ad printf("%s: error=%d\n", __func__, error);
688 1.58 yamt memset(win, 0, bytelen);
689 1.58 yamt }
690 1.58 yamt ubc_release(win, flags);
691 1.58 yamt off += bytelen;
692 1.58 yamt todo -= bytelen;
693 1.58 yamt if (error != 0 && (flags & UBC_PARTIALOK) != 0) {
694 1.58 yamt break;
695 1.58 yamt }
696 1.58 yamt }
697 1.58 yamt
698 1.58 yamt return error;
699 1.58 yamt }
700 1.67 pooka
701 1.67 pooka
702 1.67 pooka /*
703 1.67 pooka * uvm_vnp_zerorange: set a range of bytes in a file to zero.
704 1.67 pooka */
705 1.67 pooka
706 1.67 pooka void
707 1.67 pooka uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
708 1.67 pooka {
709 1.67 pooka void *win;
710 1.67 pooka int flags;
711 1.67 pooka
712 1.67 pooka /*
713 1.67 pooka * XXXUBC invent kzero() and use it
714 1.67 pooka */
715 1.67 pooka
716 1.67 pooka while (len) {
717 1.67 pooka vsize_t bytelen = len;
718 1.67 pooka
719 1.67 pooka win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
720 1.67 pooka UBC_WRITE);
721 1.67 pooka memset(win, 0, bytelen);
722 1.67 pooka flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
723 1.67 pooka ubc_release(win, flags);
724 1.67 pooka
725 1.67 pooka off += bytelen;
726 1.67 pooka len -= bytelen;
727 1.67 pooka }
728 1.67 pooka }
729