uvm_km.c revision 1.61 1 /* $NetBSD: uvm_km.c,v 1.61 2003/05/08 18:13:28 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
6 *
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor,
23 * Washington University, the University of California, Berkeley and
24 * its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
42 * from: Id: uvm_km.c,v 1.1.2.14 1998/02/06 05:19:27 chs Exp
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
53 *
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 *
58 * Carnegie Mellon requests users of this software to return to
59 *
60 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
64 *
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
67 */
68
69 /*
70 * uvm_km.c: handle kernel memory allocation and management
71 */
72
73 /*
74 * overview of kernel memory management:
75 *
76 * the kernel virtual address space is mapped by "kernel_map." kernel_map
77 * starts at virtual_avail and goes to virtual_end. note that virtual_avail
78 * is equal to vm_map_min(kernel_map).
79 *
80 * the kernel_map has several "submaps." submaps can only appear in
81 * the kernel_map (user processes can't use them). submaps "take over"
82 * the management of a sub-range of the kernel's address space. submaps
83 * are typically allocated at boot time and are never released. kernel
84 * virtual address space that is mapped by a submap is locked by the
85 * submap's lock -- not the kernel_map's lock.
86 *
87 * thus, the useful feature of submaps is that they allow us to break
88 * up the locking and protection of the kernel address space into smaller
89 * chunks.
90 *
91 * the vm system has several standard kernel submaps, including:
92 * kmem_map => contains only wired kernel memory for the kernel
93 * malloc. *** access to kmem_map must be protected
94 * by splvm() because we are allowed to call malloc()
95 * at interrupt time ***
96 * mb_map => memory for large mbufs, *** protected by splvm ***
97 * pager_map => used to map "buf" structures into kernel space
98 * exec_map => used during exec to handle exec args
99 * etc...
100 *
101 * the kernel allocates its private memory out of special uvm_objects whose
102 * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
103 * are "special" and never die). all kernel objects should be thought of
104 * as large, fixed-sized, sparsely populated uvm_objects. each kernel
105 * object is equal to the size of managed kernel virtual address space (i.e.
106 * the value "virtual_end - virtual_avail").
107 *
108 * most kernel private memory lives in kernel_object. the only exception
109 * to this is for memory that belongs to submaps that must be protected
110 * by splvm(). pages in these submaps are not assigned to an object.
111 *
112 * note that just because a kernel object spans the entire kernel virutal
113 * address space doesn't mean that it has to be mapped into the entire space.
114 * large chunks of a kernel object's space go unused either because
115 * that area of kernel VM is unmapped, or there is some other type of
116 * object mapped into that range (e.g. a vnode). for submap's kernel
117 * objects, the only part of the object that can ever be populated is the
118 * offsets that are managed by the submap.
119 *
120 * note that the "offset" in a kernel object is always the kernel virtual
121 * address minus virtual_avail (aka vm_map_min(kernel_map)).
122 * example:
123 * suppose virtual_avail is 0xf8000000 and the kernel does a
124 * uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
125 * kernel map]. if uvm_km_alloc returns virtual address 0xf8235000,
126 * then that means that the page at offset 0x235000 in kernel_object is
127 * mapped at 0xf8235000.
128 *
129 * kernel object have one other special property: when the kernel virtual
130 * memory mapping them is unmapped, the backing memory in the object is
131 * freed right away. this is done with the uvm_km_pgremove() function.
132 * this has to be done because there is no backing store for kernel pages
133 * and no need to save them after they are no longer referenced.
134 */
135
136 #include <sys/cdefs.h>
137 __KERNEL_RCSID(0, "$NetBSD: uvm_km.c,v 1.61 2003/05/08 18:13:28 thorpej Exp $");
138
139 #include "opt_uvmhist.h"
140
141 #include <sys/param.h>
142 #include <sys/systm.h>
143 #include <sys/proc.h>
144
145 #include <uvm/uvm.h>
146
147 /*
148 * global data structures
149 */
150
151 vaddr_t virtual_avail; /* start of managed kernel virtual memory */
152 vaddr_t virtual_end; /* end of managed kernel virtual memory */
153
154 struct vm_map *kernel_map = NULL;
155
156 /*
157 * local data structues
158 */
159
160 static struct vm_map kernel_map_store;
161
162 /*
163 * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
164 * KVM already allocated for text, data, bss, and static data structures).
165 *
166 * => KVM is defined by virtual_avail/virtual_end.
167 * we assume that any regions that have already been allocated from
168 * the total kernel address space have already been accounted for in
169 * the values of virtual_avail and virtual_end.
170 */
171
172 void
173 uvm_km_init(void)
174 {
175
176 /*
177 * virtual_avail and virtual_end should already be page-aligned.
178 */
179
180 KASSERT((virtual_avail & PAGE_MASK) == 0);
181 KASSERT((virtual_end & PAGE_MASK) == 0);
182
183 /*
184 * next, init kernel memory objects.
185 */
186
187 /* kernel_object: for pageable anonymous kernel memory */
188 uao_init();
189 uvm.kernel_object = uao_create(virtual_end - virtual_avail,
190 UAO_FLAG_KERNOBJ);
191
192 /*
193 * init the map and reserve any space that might already
194 * have been allocated kernel space before installing.
195 */
196
197 uvm_map_setup(&kernel_map_store, virtual_avail, virtual_end,
198 VM_MAP_PAGEABLE);
199 kernel_map_store.pmap = pmap_kernel();
200
201 /*
202 * install!
203 */
204
205 kernel_map = &kernel_map_store;
206 }
207
208 /*
209 * uvm_km_suballoc: allocate a submap in the kernel map. once a submap
210 * is allocated all references to that area of VM must go through it. this
211 * allows the locking of VAs in kernel_map to be broken up into regions.
212 *
213 * => if `fixed' is true, *min specifies where the region described
214 * by the submap must start
215 * => if submap is non NULL we use that as the submap, otherwise we
216 * alloc a new map
217 */
218 struct vm_map *
219 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
220 struct vm_map *map;
221 vaddr_t *min, *max; /* IN/OUT, OUT */
222 vsize_t size;
223 int flags;
224 boolean_t fixed;
225 struct vm_map *submap;
226 {
227 int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
228
229 size = round_page(size); /* round up to pagesize */
230
231 /*
232 * first allocate a blank spot in the parent map
233 */
234
235 if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET, 0,
236 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
237 UVM_ADV_RANDOM, mapflags)) != 0) {
238 panic("uvm_km_suballoc: unable to allocate space in parent map");
239 }
240
241 /*
242 * set VM bounds (min is filled in by uvm_map)
243 */
244
245 *max = *min + size;
246
247 /*
248 * add references to pmap and create or init the submap
249 */
250
251 pmap_reference(vm_map_pmap(map));
252 if (submap == NULL) {
253 submap = uvm_map_create(vm_map_pmap(map), *min, *max, flags);
254 if (submap == NULL)
255 panic("uvm_km_suballoc: unable to create submap");
256 } else {
257 uvm_map_setup(submap, *min, *max, flags);
258 submap->pmap = vm_map_pmap(map);
259 }
260
261 /*
262 * now let uvm_map_submap plug in it...
263 */
264
265 if (uvm_map_submap(map, *min, *max, submap) != 0)
266 panic("uvm_km_suballoc: submap allocation failed");
267
268 return(submap);
269 }
270
271 /*
272 * uvm_km_pgremove: remove pages from a kernel uvm_object.
273 *
274 * => when you unmap a part of anonymous kernel memory you want to toss
275 * the pages right away. (this gets called from uvm_unmap_...).
276 */
277
278 void
279 uvm_km_pgremove(uobj, start, end)
280 struct uvm_object *uobj;
281 vaddr_t start, end;
282 {
283 struct vm_page *pg;
284 voff_t curoff, nextoff;
285 int swpgonlydelta = 0;
286 UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
287
288 KASSERT(uobj->pgops == &aobj_pager);
289 simple_lock(&uobj->vmobjlock);
290
291 for (curoff = start; curoff < end; curoff = nextoff) {
292 nextoff = curoff + PAGE_SIZE;
293 pg = uvm_pagelookup(uobj, curoff);
294 if (pg != NULL && pg->flags & PG_BUSY) {
295 pg->flags |= PG_WANTED;
296 UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
297 "km_pgrm", 0);
298 simple_lock(&uobj->vmobjlock);
299 nextoff = curoff;
300 continue;
301 }
302
303 /*
304 * free the swap slot, then the page.
305 */
306
307 if (pg == NULL &&
308 uao_find_swslot(uobj, curoff >> PAGE_SHIFT) != 0) {
309 swpgonlydelta++;
310 }
311 uao_dropswap(uobj, curoff >> PAGE_SHIFT);
312 if (pg != NULL) {
313 uvm_lock_pageq();
314 uvm_pagefree(pg);
315 uvm_unlock_pageq();
316 }
317 }
318 simple_unlock(&uobj->vmobjlock);
319
320 if (swpgonlydelta > 0) {
321 simple_lock(&uvm.swap_data_lock);
322 KASSERT(uvmexp.swpgonly >= swpgonlydelta);
323 uvmexp.swpgonly -= swpgonlydelta;
324 simple_unlock(&uvm.swap_data_lock);
325 }
326 }
327
328
329 /*
330 * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
331 * maps
332 *
333 * => when you unmap a part of anonymous kernel memory you want to toss
334 * the pages right away. (this is called from uvm_unmap_...).
335 * => none of the pages will ever be busy, and none of them will ever
336 * be on the active or inactive queues (because they have no object).
337 */
338
339 void
340 uvm_km_pgremove_intrsafe(start, end)
341 vaddr_t start, end;
342 {
343 struct vm_page *pg;
344 paddr_t pa;
345 UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
346
347 for (; start < end; start += PAGE_SIZE) {
348 if (!pmap_extract(pmap_kernel(), start, &pa)) {
349 continue;
350 }
351 pg = PHYS_TO_VM_PAGE(pa);
352 KASSERT(pg);
353 KASSERT(pg->uobject == NULL && pg->uanon == NULL);
354 uvm_pagefree(pg);
355 }
356 }
357
358
359 /*
360 * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
361 *
362 * => we map wired memory into the specified map using the obj passed in
363 * => NOTE: we can return NULL even if we can wait if there is not enough
364 * free VM space in the map... caller should be prepared to handle
365 * this case.
366 * => we return KVA of memory allocated
367 * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
368 * lock the map
369 */
370
371 vaddr_t
372 uvm_km_kmemalloc(map, obj, size, flags)
373 struct vm_map *map;
374 struct uvm_object *obj;
375 vsize_t size;
376 int flags;
377 {
378 vaddr_t kva, loopva;
379 vaddr_t offset;
380 vsize_t loopsize;
381 struct vm_page *pg;
382 UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
383
384 UVMHIST_LOG(maphist," (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
385 map, obj, size, flags);
386 KASSERT(vm_map_pmap(map) == pmap_kernel());
387
388 /*
389 * setup for call
390 */
391
392 size = round_page(size);
393 kva = vm_map_min(map); /* hint */
394
395 /*
396 * allocate some virtual space
397 */
398
399 if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
400 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
401 UVM_ADV_RANDOM,
402 (flags & (UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT))))
403 != 0)) {
404 UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
405 return(0);
406 }
407
408 /*
409 * if all we wanted was VA, return now
410 */
411
412 if (flags & UVM_KMF_VALLOC) {
413 UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
414 return(kva);
415 }
416
417 /*
418 * recover object offset from virtual address
419 */
420
421 offset = kva - vm_map_min(kernel_map);
422 UVMHIST_LOG(maphist, " kva=0x%x, offset=0x%x", kva, offset,0,0);
423
424 /*
425 * now allocate and map in the memory... note that we are the only ones
426 * whom should ever get a handle on this area of VM.
427 */
428
429 loopva = kva;
430 loopsize = size;
431 while (loopsize) {
432 if (obj) {
433 simple_lock(&obj->vmobjlock);
434 }
435 pg = uvm_pagealloc(obj, offset, NULL, UVM_PGA_USERESERVE);
436 if (__predict_true(pg != NULL)) {
437 pg->flags &= ~PG_BUSY; /* new page */
438 UVM_PAGE_OWN(pg, NULL);
439 }
440 if (obj) {
441 simple_unlock(&obj->vmobjlock);
442 }
443
444 /*
445 * out of memory?
446 */
447
448 if (__predict_false(pg == NULL)) {
449 if ((flags & UVM_KMF_NOWAIT) ||
450 ((flags & UVM_KMF_CANFAIL) &&
451 uvmexp.swpgonly == uvmexp.swpages)) {
452 /* free everything! */
453 uvm_unmap(map, kva, kva + size);
454 return (0);
455 } else {
456 uvm_wait("km_getwait2"); /* sleep here */
457 continue;
458 }
459 }
460
461 /*
462 * map it in
463 */
464
465 if (obj == NULL) {
466 pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
467 VM_PROT_READ | VM_PROT_WRITE);
468 } else {
469 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
470 UVM_PROT_ALL,
471 PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
472 }
473 loopva += PAGE_SIZE;
474 offset += PAGE_SIZE;
475 loopsize -= PAGE_SIZE;
476 }
477
478 pmap_update(pmap_kernel());
479
480 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
481 return(kva);
482 }
483
484 /*
485 * uvm_km_free: free an area of kernel memory
486 */
487
488 void
489 uvm_km_free(map, addr, size)
490 struct vm_map *map;
491 vaddr_t addr;
492 vsize_t size;
493 {
494 uvm_unmap(map, trunc_page(addr), round_page(addr+size));
495 }
496
497 /*
498 * uvm_km_free_wakeup: free an area of kernel memory and wake up
499 * anyone waiting for vm space.
500 *
501 * => XXX: "wanted" bit + unlock&wait on other end?
502 */
503
504 void
505 uvm_km_free_wakeup(map, addr, size)
506 struct vm_map *map;
507 vaddr_t addr;
508 vsize_t size;
509 {
510 struct vm_map_entry *dead_entries;
511
512 vm_map_lock(map);
513 uvm_unmap_remove(map, trunc_page(addr), round_page(addr + size),
514 &dead_entries);
515 wakeup(map);
516 vm_map_unlock(map);
517 if (dead_entries != NULL)
518 uvm_unmap_detach(dead_entries, 0);
519 }
520
521 /*
522 * uvm_km_alloc1: allocate wired down memory in the kernel map.
523 *
524 * => we can sleep if needed
525 */
526
527 vaddr_t
528 uvm_km_alloc1(map, size, zeroit)
529 struct vm_map *map;
530 vsize_t size;
531 boolean_t zeroit;
532 {
533 vaddr_t kva, loopva, offset;
534 struct vm_page *pg;
535 UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
536
537 UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
538 KASSERT(vm_map_pmap(map) == pmap_kernel());
539
540 size = round_page(size);
541 kva = vm_map_min(map); /* hint */
542
543 /*
544 * allocate some virtual space
545 */
546
547 if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
548 UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
549 UVM_INH_NONE, UVM_ADV_RANDOM,
550 0)) != 0)) {
551 UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
552 return(0);
553 }
554
555 /*
556 * recover object offset from virtual address
557 */
558
559 offset = kva - vm_map_min(kernel_map);
560 UVMHIST_LOG(maphist," kva=0x%x, offset=0x%x", kva, offset,0,0);
561
562 /*
563 * now allocate the memory.
564 */
565
566 loopva = kva;
567 while (size) {
568 simple_lock(&uvm.kernel_object->vmobjlock);
569 KASSERT(uvm_pagelookup(uvm.kernel_object, offset) == NULL);
570 pg = uvm_pagealloc(uvm.kernel_object, offset, NULL, 0);
571 if (pg) {
572 pg->flags &= ~PG_BUSY;
573 UVM_PAGE_OWN(pg, NULL);
574 }
575 simple_unlock(&uvm.kernel_object->vmobjlock);
576 if (pg == NULL) {
577 uvm_wait("km_alloc1w");
578 continue;
579 }
580 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
581 UVM_PROT_ALL, PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
582 loopva += PAGE_SIZE;
583 offset += PAGE_SIZE;
584 size -= PAGE_SIZE;
585 }
586 pmap_update(map->pmap);
587
588 /*
589 * zero on request (note that "size" is now zero due to the above loop
590 * so we need to subtract kva from loopva to reconstruct the size).
591 */
592
593 if (zeroit)
594 memset((caddr_t)kva, 0, loopva - kva);
595 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
596 return(kva);
597 }
598
599 /*
600 * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
601 *
602 * => memory is not allocated until fault time
603 */
604
605 vaddr_t
606 uvm_km_valloc(map, size)
607 struct vm_map *map;
608 vsize_t size;
609 {
610 return(uvm_km_valloc_align(map, size, 0));
611 }
612
613 vaddr_t
614 uvm_km_valloc_align(map, size, align)
615 struct vm_map *map;
616 vsize_t size;
617 vsize_t align;
618 {
619 vaddr_t kva;
620 UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
621
622 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
623 KASSERT(vm_map_pmap(map) == pmap_kernel());
624
625 size = round_page(size);
626 kva = vm_map_min(map); /* hint */
627
628 /*
629 * allocate some virtual space. will be demand filled by kernel_object.
630 */
631
632 if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
633 UVM_UNKNOWN_OFFSET, align, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
634 UVM_INH_NONE, UVM_ADV_RANDOM,
635 0)) != 0)) {
636 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
637 return(0);
638 }
639
640 UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
641 return(kva);
642 }
643
644 /*
645 * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
646 *
647 * => memory is not allocated until fault time
648 * => if no room in map, wait for space to free, unless requested size
649 * is larger than map (in which case we return 0)
650 */
651
652 vaddr_t
653 uvm_km_valloc_prefer_wait(map, size, prefer)
654 struct vm_map *map;
655 vsize_t size;
656 voff_t prefer;
657 {
658 vaddr_t kva;
659 UVMHIST_FUNC("uvm_km_valloc_prefer_wait"); UVMHIST_CALLED(maphist);
660
661 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
662 KASSERT(vm_map_pmap(map) == pmap_kernel());
663
664 size = round_page(size);
665 if (size > vm_map_max(map) - vm_map_min(map))
666 return(0);
667
668 for (;;) {
669 kva = vm_map_min(map); /* hint */
670
671 /*
672 * allocate some virtual space. will be demand filled
673 * by kernel_object.
674 */
675
676 if (__predict_true(uvm_map(map, &kva, size, uvm.kernel_object,
677 prefer, 0, UVM_MAPFLAG(UVM_PROT_ALL,
678 UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
679 == 0)) {
680 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
681 return(kva);
682 }
683
684 /*
685 * failed. sleep for a while (on map)
686 */
687
688 UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
689 tsleep((caddr_t)map, PVM, "vallocwait", 0);
690 }
691 /*NOTREACHED*/
692 }
693
694 vaddr_t
695 uvm_km_valloc_wait(map, size)
696 struct vm_map *map;
697 vsize_t size;
698 {
699 return uvm_km_valloc_prefer_wait(map, size, UVM_UNKNOWN_OFFSET);
700 }
701
702 /* Sanity; must specify both or none. */
703 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
704 (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
705 #error Must specify MAP and UNMAP together.
706 #endif
707
708 /*
709 * uvm_km_alloc_poolpage: allocate a page for the pool allocator
710 *
711 * => if the pmap specifies an alternate mapping method, we use it.
712 */
713
714 /* ARGSUSED */
715 vaddr_t
716 uvm_km_alloc_poolpage1(map, obj, waitok)
717 struct vm_map *map;
718 struct uvm_object *obj;
719 boolean_t waitok;
720 {
721 #if defined(PMAP_MAP_POOLPAGE)
722 struct vm_page *pg;
723 vaddr_t va;
724
725 again:
726 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
727 if (__predict_false(pg == NULL)) {
728 if (waitok) {
729 uvm_wait("plpg");
730 goto again;
731 } else
732 return (0);
733 }
734 va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
735 if (__predict_false(va == 0))
736 uvm_pagefree(pg);
737 return (va);
738 #else
739 vaddr_t va;
740 int s;
741
742 /*
743 * NOTE: We may be called with a map that doens't require splvm
744 * protection (e.g. kernel_map). However, it does not hurt to
745 * go to splvm in this case (since unprocted maps will never be
746 * accessed in interrupt context).
747 *
748 * XXX We may want to consider changing the interface to this
749 * XXX function.
750 */
751
752 s = splvm();
753 va = uvm_km_kmemalloc(map, obj, PAGE_SIZE,
754 waitok ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK);
755 splx(s);
756 return (va);
757 #endif /* PMAP_MAP_POOLPAGE */
758 }
759
760 /*
761 * uvm_km_free_poolpage: free a previously allocated pool page
762 *
763 * => if the pmap specifies an alternate unmapping method, we use it.
764 */
765
766 /* ARGSUSED */
767 void
768 uvm_km_free_poolpage1(map, addr)
769 struct vm_map *map;
770 vaddr_t addr;
771 {
772 #if defined(PMAP_UNMAP_POOLPAGE)
773 paddr_t pa;
774
775 pa = PMAP_UNMAP_POOLPAGE(addr);
776 uvm_pagefree(PHYS_TO_VM_PAGE(pa));
777 #else
778 int s;
779
780 /*
781 * NOTE: We may be called with a map that doens't require splvm
782 * protection (e.g. kernel_map). However, it does not hurt to
783 * go to splvm in this case (since unprocted maps will never be
784 * accessed in interrupt context).
785 *
786 * XXX We may want to consider changing the interface to this
787 * XXX function.
788 */
789
790 s = splvm();
791 uvm_km_free(map, addr, PAGE_SIZE);
792 splx(s);
793 #endif /* PMAP_UNMAP_POOLPAGE */
794 }
795