uvm_km.c revision 1.51 1 /* $NetBSD: uvm_km.c,v 1.51 2001/09/10 21:19:42 chris 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 #include "opt_uvmhist.h"
70
71 /*
72 * uvm_km.c: handle kernel memory allocation and management
73 */
74
75 /*
76 * overview of kernel memory management:
77 *
78 * the kernel virtual address space is mapped by "kernel_map." kernel_map
79 * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
80 * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
81 *
82 * the kernel_map has several "submaps." submaps can only appear in
83 * the kernel_map (user processes can't use them). submaps "take over"
84 * the management of a sub-range of the kernel's address space. submaps
85 * are typically allocated at boot time and are never released. kernel
86 * virtual address space that is mapped by a submap is locked by the
87 * submap's lock -- not the kernel_map's lock.
88 *
89 * thus, the useful feature of submaps is that they allow us to break
90 * up the locking and protection of the kernel address space into smaller
91 * chunks.
92 *
93 * the vm system has several standard kernel submaps, including:
94 * kmem_map => contains only wired kernel memory for the kernel
95 * malloc. *** access to kmem_map must be protected
96 * by splvm() because we are allowed to call malloc()
97 * at interrupt time ***
98 * mb_map => memory for large mbufs, *** protected by splvm ***
99 * pager_map => used to map "buf" structures into kernel space
100 * exec_map => used during exec to handle exec args
101 * etc...
102 *
103 * the kernel allocates its private memory out of special uvm_objects whose
104 * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
105 * are "special" and never die). all kernel objects should be thought of
106 * as large, fixed-sized, sparsely populated uvm_objects. each kernel
107 * object is equal to the size of kernel virtual address space (i.e. the
108 * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
109 *
110 * most kernel private memory lives in kernel_object. the only exception
111 * to this is for memory that belongs to submaps that must be protected
112 * by splvm(). each of these submaps has their own private kernel
113 * object (e.g. kmem_object, mb_object).
114 *
115 * note that just because a kernel object spans the entire kernel virutal
116 * address space doesn't mean that it has to be mapped into the entire space.
117 * large chunks of a kernel object's space go unused either because
118 * that area of kernel VM is unmapped, or there is some other type of
119 * object mapped into that range (e.g. a vnode). for submap's kernel
120 * objects, the only part of the object that can ever be populated is the
121 * offsets that are managed by the submap.
122 *
123 * note that the "offset" in a kernel object is always the kernel virtual
124 * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
125 * example:
126 * suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
127 * uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
128 * kernel map]. if uvm_km_alloc returns virtual address 0xf8235000,
129 * then that means that the page at offset 0x235000 in kernel_object is
130 * mapped at 0xf8235000.
131 *
132 * note that the offsets in kmem_object and mb_object also follow this
133 * rule. this means that the offsets for kmem_object must fall in the
134 * range of [vm_map_min(kmem_object) - vm_map_min(kernel_map)] to
135 * [vm_map_max(kmem_object) - vm_map_min(kernel_map)], so the offsets
136 * in those objects will typically not start at zero.
137 *
138 * kernel object have one other special property: when the kernel virtual
139 * memory mapping them is unmapped, the backing memory in the object is
140 * freed right away. this is done with the uvm_km_pgremove() function.
141 * this has to be done because there is no backing store for kernel pages
142 * and no need to save them after they are no longer referenced.
143 */
144
145 #include <sys/param.h>
146 #include <sys/systm.h>
147 #include <sys/proc.h>
148
149 #include <uvm/uvm.h>
150
151 /*
152 * global data structures
153 */
154
155 struct vm_map *kernel_map = NULL;
156
157 /*
158 * local data structues
159 */
160
161 static struct vm_map kernel_map_store;
162 static struct uvm_object kmem_object_store;
163 static struct uvm_object mb_object_store;
164
165 /*
166 * All pager operations here are NULL, but the object must have
167 * a pager ops vector associated with it; various places assume
168 * it to be so.
169 */
170 static struct uvm_pagerops km_pager;
171
172 /*
173 * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
174 * KVM already allocated for text, data, bss, and static data structures).
175 *
176 * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
177 * we assume that [min -> start] has already been allocated and that
178 * "end" is the end.
179 */
180
181 void
182 uvm_km_init(start, end)
183 vaddr_t start, end;
184 {
185 vaddr_t base = VM_MIN_KERNEL_ADDRESS;
186
187 /*
188 * next, init kernel memory objects.
189 */
190
191 /* kernel_object: for pageable anonymous kernel memory */
192 uao_init();
193 uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
194 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
195
196 /*
197 * kmem_object: for use by the kernel malloc(). Memory is always
198 * wired, and this object (and the kmem_map) can be accessed at
199 * interrupt time.
200 */
201 simple_lock_init(&kmem_object_store.vmobjlock);
202 kmem_object_store.pgops = &km_pager;
203 TAILQ_INIT(&kmem_object_store.memq);
204 kmem_object_store.uo_npages = 0;
205 /* we are special. we never die */
206 kmem_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
207 uvmexp.kmem_object = &kmem_object_store;
208
209 /*
210 * mb_object: for mbuf cluster pages on platforms which use the
211 * mb_map. Memory is always wired, and this object (and the mb_map)
212 * can be accessed at interrupt time.
213 */
214 simple_lock_init(&mb_object_store.vmobjlock);
215 mb_object_store.pgops = &km_pager;
216 TAILQ_INIT(&mb_object_store.memq);
217 mb_object_store.uo_npages = 0;
218 /* we are special. we never die */
219 mb_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
220 uvmexp.mb_object = &mb_object_store;
221
222 /*
223 * init the map and reserve allready allocated kernel space
224 * before installing.
225 */
226
227 uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
228 kernel_map_store.pmap = pmap_kernel();
229 if (uvm_map(&kernel_map_store, &base, start - base, NULL,
230 UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
231 UVM_INH_NONE, UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != 0)
232 panic("uvm_km_init: could not reserve space for kernel");
233
234 /*
235 * install!
236 */
237
238 kernel_map = &kernel_map_store;
239 }
240
241 /*
242 * uvm_km_suballoc: allocate a submap in the kernel map. once a submap
243 * is allocated all references to that area of VM must go through it. this
244 * allows the locking of VAs in kernel_map to be broken up into regions.
245 *
246 * => if `fixed' is true, *min specifies where the region described
247 * by the submap must start
248 * => if submap is non NULL we use that as the submap, otherwise we
249 * alloc a new map
250 */
251 struct vm_map *
252 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
253 struct vm_map *map;
254 vaddr_t *min, *max; /* OUT, OUT */
255 vsize_t size;
256 int flags;
257 boolean_t fixed;
258 struct vm_map *submap;
259 {
260 int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
261
262 size = round_page(size); /* round up to pagesize */
263
264 /*
265 * first allocate a blank spot in the parent map
266 */
267
268 if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET, 0,
269 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
270 UVM_ADV_RANDOM, mapflags)) != 0) {
271 panic("uvm_km_suballoc: unable to allocate space in parent map");
272 }
273
274 /*
275 * set VM bounds (min is filled in by uvm_map)
276 */
277
278 *max = *min + size;
279
280 /*
281 * add references to pmap and create or init the submap
282 */
283
284 pmap_reference(vm_map_pmap(map));
285 if (submap == NULL) {
286 submap = uvm_map_create(vm_map_pmap(map), *min, *max, flags);
287 if (submap == NULL)
288 panic("uvm_km_suballoc: unable to create submap");
289 } else {
290 uvm_map_setup(submap, *min, *max, flags);
291 submap->pmap = vm_map_pmap(map);
292 }
293
294 /*
295 * now let uvm_map_submap plug in it...
296 */
297
298 if (uvm_map_submap(map, *min, *max, submap) != 0)
299 panic("uvm_km_suballoc: submap allocation failed");
300
301 return(submap);
302 }
303
304 /*
305 * uvm_km_pgremove: remove pages from a kernel uvm_object.
306 *
307 * => when you unmap a part of anonymous kernel memory you want to toss
308 * the pages right away. (this gets called from uvm_unmap_...).
309 */
310
311 #define UKM_HASH_PENALTY 4 /* a guess */
312
313 void
314 uvm_km_pgremove(uobj, start, end)
315 struct uvm_object *uobj;
316 vaddr_t start, end;
317 {
318 boolean_t by_list;
319 struct vm_page *pp, *ppnext;
320 vaddr_t curoff;
321 UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
322
323 KASSERT(uobj->pgops == &aobj_pager);
324 simple_lock(&uobj->vmobjlock);
325
326 /* choose cheapest traversal */
327 by_list = (uobj->uo_npages <=
328 ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
329
330 if (by_list)
331 goto loop_by_list;
332
333 /* by hash */
334
335 for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
336 pp = uvm_pagelookup(uobj, curoff);
337 if (pp == NULL)
338 continue;
339
340 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,
341 pp->flags & PG_BUSY, 0, 0);
342
343 /* now do the actual work */
344 if (pp->flags & PG_BUSY) {
345 /* owner must check for this when done */
346 pp->flags |= PG_RELEASED;
347 } else {
348 /* free the swap slot... */
349 uao_dropswap(uobj, curoff >> PAGE_SHIFT);
350
351 /*
352 * ...and free the page; note it may be on the
353 * active or inactive queues.
354 */
355 uvm_lock_pageq();
356 uvm_pagefree(pp);
357 uvm_unlock_pageq();
358 }
359 }
360 simple_unlock(&uobj->vmobjlock);
361 return;
362
363 loop_by_list:
364
365 for (pp = TAILQ_FIRST(&uobj->memq); pp != NULL; pp = ppnext) {
366 ppnext = TAILQ_NEXT(pp, listq);
367 if (pp->offset < start || pp->offset >= end) {
368 continue;
369 }
370
371 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,
372 pp->flags & PG_BUSY, 0, 0);
373
374 if (pp->flags & PG_BUSY) {
375 /* owner must check for this when done */
376 pp->flags |= PG_RELEASED;
377 } else {
378 /* free the swap slot... */
379 uao_dropswap(uobj, pp->offset >> PAGE_SHIFT);
380
381 /*
382 * ...and free the page; note it may be on the
383 * active or inactive queues.
384 */
385 uvm_lock_pageq();
386 uvm_pagefree(pp);
387 uvm_unlock_pageq();
388 }
389 }
390 simple_unlock(&uobj->vmobjlock);
391 }
392
393
394 /*
395 * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
396 * objects
397 *
398 * => when you unmap a part of anonymous kernel memory you want to toss
399 * the pages right away. (this gets called from uvm_unmap_...).
400 * => none of the pages will ever be busy, and none of them will ever
401 * be on the active or inactive queues (because these objects are
402 * never allowed to "page").
403 */
404
405 void
406 uvm_km_pgremove_intrsafe(uobj, start, end)
407 struct uvm_object *uobj;
408 vaddr_t start, end;
409 {
410 boolean_t by_list;
411 struct vm_page *pp, *ppnext;
412 vaddr_t curoff;
413 UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
414
415 KASSERT(UVM_OBJ_IS_INTRSAFE_OBJECT(uobj));
416 simple_lock(&uobj->vmobjlock); /* lock object */
417
418 /* choose cheapest traversal */
419 by_list = (uobj->uo_npages <=
420 ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
421
422 if (by_list)
423 goto loop_by_list;
424
425 /* by hash */
426
427 for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
428 pp = uvm_pagelookup(uobj, curoff);
429 if (pp == NULL) {
430 continue;
431 }
432
433 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,
434 pp->flags & PG_BUSY, 0, 0);
435 KASSERT((pp->flags & PG_BUSY) == 0);
436 KASSERT((pp->pqflags & PQ_ACTIVE) == 0);
437 KASSERT((pp->pqflags & PQ_INACTIVE) == 0);
438 uvm_pagefree(pp);
439 }
440 simple_unlock(&uobj->vmobjlock);
441 return;
442
443 loop_by_list:
444
445 for (pp = TAILQ_FIRST(&uobj->memq); pp != NULL; pp = ppnext) {
446 ppnext = TAILQ_NEXT(pp, listq);
447 if (pp->offset < start || pp->offset >= end) {
448 continue;
449 }
450
451 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,
452 pp->flags & PG_BUSY, 0, 0);
453 KASSERT((pp->flags & PG_BUSY) == 0);
454 KASSERT((pp->pqflags & PQ_ACTIVE) == 0);
455 KASSERT((pp->pqflags & PQ_INACTIVE) == 0);
456 uvm_pagefree(pp);
457 }
458 simple_unlock(&uobj->vmobjlock);
459 }
460
461
462 /*
463 * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
464 *
465 * => we map wired memory into the specified map using the obj passed in
466 * => NOTE: we can return NULL even if we can wait if there is not enough
467 * free VM space in the map... caller should be prepared to handle
468 * this case.
469 * => we return KVA of memory allocated
470 * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
471 * lock the map
472 */
473
474 vaddr_t
475 uvm_km_kmemalloc(map, obj, size, flags)
476 struct vm_map *map;
477 struct uvm_object *obj;
478 vsize_t size;
479 int flags;
480 {
481 vaddr_t kva, loopva;
482 vaddr_t offset;
483 vsize_t loopsize;
484 struct vm_page *pg;
485 UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
486
487 UVMHIST_LOG(maphist," (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
488 map, obj, size, flags);
489 KASSERT(vm_map_pmap(map) == pmap_kernel());
490
491 /*
492 * setup for call
493 */
494
495 size = round_page(size);
496 kva = vm_map_min(map); /* hint */
497
498 /*
499 * allocate some virtual space
500 */
501
502 if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
503 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
504 UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
505 != 0)) {
506 UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
507 return(0);
508 }
509
510 /*
511 * if all we wanted was VA, return now
512 */
513
514 if (flags & UVM_KMF_VALLOC) {
515 UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
516 return(kva);
517 }
518
519 /*
520 * recover object offset from virtual address
521 */
522
523 offset = kva - vm_map_min(kernel_map);
524 UVMHIST_LOG(maphist, " kva=0x%x, offset=0x%x", kva, offset,0,0);
525
526 /*
527 * now allocate and map in the memory... note that we are the only ones
528 * whom should ever get a handle on this area of VM.
529 */
530
531 loopva = kva;
532 loopsize = size;
533 while (loopsize) {
534 simple_lock(&obj->vmobjlock);
535 pg = uvm_pagealloc(obj, offset, NULL, 0);
536 if (__predict_true(pg != NULL)) {
537 pg->flags &= ~PG_BUSY; /* new page */
538 UVM_PAGE_OWN(pg, NULL);
539 }
540 simple_unlock(&obj->vmobjlock);
541
542 /*
543 * out of memory?
544 */
545
546 if (__predict_false(pg == NULL)) {
547 if (flags & UVM_KMF_NOWAIT) {
548 /* free everything! */
549 uvm_unmap(map, kva, kva + size);
550 return(0);
551 } else {
552 uvm_wait("km_getwait2"); /* sleep here */
553 continue;
554 }
555 }
556
557 /*
558 * map it in: note that we call pmap_enter with the map and
559 * object unlocked in case we are kmem_map/kmem_object
560 * (because if pmap_enter wants to allocate out of kmem_object
561 * it will need to lock it itself!)
562 */
563
564 if (UVM_OBJ_IS_INTRSAFE_OBJECT(obj)) {
565 pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
566 VM_PROT_ALL);
567 } else {
568 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
569 UVM_PROT_ALL,
570 PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
571 }
572 loopva += PAGE_SIZE;
573 offset += PAGE_SIZE;
574 loopsize -= PAGE_SIZE;
575 }
576
577 pmap_update(pmap_kernel());
578
579 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
580 return(kva);
581 }
582
583 /*
584 * uvm_km_free: free an area of kernel memory
585 */
586
587 void
588 uvm_km_free(map, addr, size)
589 struct vm_map *map;
590 vaddr_t addr;
591 vsize_t size;
592 {
593 uvm_unmap(map, trunc_page(addr), round_page(addr+size));
594 }
595
596 /*
597 * uvm_km_free_wakeup: free an area of kernel memory and wake up
598 * anyone waiting for vm space.
599 *
600 * => XXX: "wanted" bit + unlock&wait on other end?
601 */
602
603 void
604 uvm_km_free_wakeup(map, addr, size)
605 struct vm_map *map;
606 vaddr_t addr;
607 vsize_t size;
608 {
609 struct vm_map_entry *dead_entries;
610
611 vm_map_lock(map);
612 uvm_unmap_remove(map, trunc_page(addr), round_page(addr + size),
613 &dead_entries);
614 wakeup(map);
615 vm_map_unlock(map);
616 if (dead_entries != NULL)
617 uvm_unmap_detach(dead_entries, 0);
618 }
619
620 /*
621 * uvm_km_alloc1: allocate wired down memory in the kernel map.
622 *
623 * => we can sleep if needed
624 */
625
626 vaddr_t
627 uvm_km_alloc1(map, size, zeroit)
628 struct vm_map *map;
629 vsize_t size;
630 boolean_t zeroit;
631 {
632 vaddr_t kva, loopva, offset;
633 struct vm_page *pg;
634 UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
635
636 UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
637 KASSERT(vm_map_pmap(map) == pmap_kernel());
638
639 size = round_page(size);
640 kva = vm_map_min(map); /* hint */
641
642 /*
643 * allocate some virtual space
644 */
645
646 if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
647 UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
648 UVM_INH_NONE, UVM_ADV_RANDOM,
649 0)) != 0)) {
650 UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
651 return(0);
652 }
653
654 /*
655 * recover object offset from virtual address
656 */
657
658 offset = kva - vm_map_min(kernel_map);
659 UVMHIST_LOG(maphist," kva=0x%x, offset=0x%x", kva, offset,0,0);
660
661 /*
662 * now allocate the memory. we must be careful about released pages.
663 */
664
665 loopva = kva;
666 while (size) {
667 simple_lock(&uvm.kernel_object->vmobjlock);
668 pg = uvm_pagelookup(uvm.kernel_object, offset);
669
670 /*
671 * if we found a page in an unallocated region, it must be
672 * released
673 */
674 if (pg) {
675 if ((pg->flags & PG_RELEASED) == 0)
676 panic("uvm_km_alloc1: non-released page");
677 pg->flags |= PG_WANTED;
678 UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,
679 FALSE, "km_alloc", 0);
680 continue; /* retry */
681 }
682
683 /* allocate ram */
684 pg = uvm_pagealloc(uvm.kernel_object, offset, NULL, 0);
685 if (pg) {
686 pg->flags &= ~PG_BUSY; /* new page */
687 UVM_PAGE_OWN(pg, NULL);
688 }
689 simple_unlock(&uvm.kernel_object->vmobjlock);
690 if (__predict_false(pg == NULL)) {
691 uvm_wait("km_alloc1w"); /* wait for memory */
692 continue;
693 }
694
695 /*
696 * map it in; note we're never called with an intrsafe
697 * object, so we always use regular old pmap_enter().
698 */
699 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
700 UVM_PROT_ALL, PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
701
702 loopva += PAGE_SIZE;
703 offset += PAGE_SIZE;
704 size -= PAGE_SIZE;
705 }
706
707 pmap_update(map->pmap);
708
709 /*
710 * zero on request (note that "size" is now zero due to the above loop
711 * so we need to subtract kva from loopva to reconstruct the size).
712 */
713
714 if (zeroit)
715 memset((caddr_t)kva, 0, loopva - kva);
716
717 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
718 return(kva);
719 }
720
721 /*
722 * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
723 *
724 * => memory is not allocated until fault time
725 */
726
727 vaddr_t
728 uvm_km_valloc(map, size)
729 struct vm_map *map;
730 vsize_t size;
731 {
732 return(uvm_km_valloc_align(map, size, 0));
733 }
734
735 vaddr_t
736 uvm_km_valloc_align(map, size, align)
737 struct vm_map *map;
738 vsize_t size;
739 vsize_t align;
740 {
741 vaddr_t kva;
742 UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
743
744 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
745 KASSERT(vm_map_pmap(map) == pmap_kernel());
746
747 size = round_page(size);
748 kva = vm_map_min(map); /* hint */
749
750 /*
751 * allocate some virtual space. will be demand filled by kernel_object.
752 */
753
754 if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
755 UVM_UNKNOWN_OFFSET, align, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
756 UVM_INH_NONE, UVM_ADV_RANDOM,
757 0)) != 0)) {
758 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
759 return(0);
760 }
761
762 UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
763 return(kva);
764 }
765
766 /*
767 * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
768 *
769 * => memory is not allocated until fault time
770 * => if no room in map, wait for space to free, unless requested size
771 * is larger than map (in which case we return 0)
772 */
773
774 vaddr_t
775 uvm_km_valloc_prefer_wait(map, size, prefer)
776 struct vm_map *map;
777 vsize_t size;
778 voff_t prefer;
779 {
780 vaddr_t kva;
781 UVMHIST_FUNC("uvm_km_valloc_prefer_wait"); UVMHIST_CALLED(maphist);
782
783 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
784 KASSERT(vm_map_pmap(map) == pmap_kernel());
785
786 size = round_page(size);
787 if (size > vm_map_max(map) - vm_map_min(map))
788 return(0);
789
790 while (1) {
791 kva = vm_map_min(map); /* hint */
792
793 /*
794 * allocate some virtual space. will be demand filled
795 * by kernel_object.
796 */
797
798 if (__predict_true(uvm_map(map, &kva, size, uvm.kernel_object,
799 prefer, 0, UVM_MAPFLAG(UVM_PROT_ALL,
800 UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
801 == 0)) {
802 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
803 return(kva);
804 }
805
806 /*
807 * failed. sleep for a while (on map)
808 */
809
810 UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
811 tsleep((caddr_t)map, PVM, "vallocwait", 0);
812 }
813 /*NOTREACHED*/
814 }
815
816 vaddr_t
817 uvm_km_valloc_wait(map, size)
818 struct vm_map *map;
819 vsize_t size;
820 {
821 return uvm_km_valloc_prefer_wait(map, size, UVM_UNKNOWN_OFFSET);
822 }
823
824 /* Sanity; must specify both or none. */
825 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
826 (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
827 #error Must specify MAP and UNMAP together.
828 #endif
829
830 /*
831 * uvm_km_alloc_poolpage: allocate a page for the pool allocator
832 *
833 * => if the pmap specifies an alternate mapping method, we use it.
834 */
835
836 /* ARGSUSED */
837 vaddr_t
838 uvm_km_alloc_poolpage1(map, obj, waitok)
839 struct vm_map *map;
840 struct uvm_object *obj;
841 boolean_t waitok;
842 {
843 #if defined(PMAP_MAP_POOLPAGE)
844 struct vm_page *pg;
845 vaddr_t va;
846
847 again:
848 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
849 if (__predict_false(pg == NULL)) {
850 if (waitok) {
851 uvm_wait("plpg");
852 goto again;
853 } else
854 return (0);
855 }
856 va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
857 if (__predict_false(va == 0))
858 uvm_pagefree(pg);
859 return (va);
860 #else
861 vaddr_t va;
862 int s;
863
864 /*
865 * NOTE: We may be called with a map that doens't require splvm
866 * protection (e.g. kernel_map). However, it does not hurt to
867 * go to splvm in this case (since unprocted maps will never be
868 * accessed in interrupt context).
869 *
870 * XXX We may want to consider changing the interface to this
871 * XXX function.
872 */
873
874 s = splvm();
875 va = uvm_km_kmemalloc(map, obj, PAGE_SIZE, waitok ? 0 : UVM_KMF_NOWAIT);
876 splx(s);
877 return (va);
878 #endif /* PMAP_MAP_POOLPAGE */
879 }
880
881 /*
882 * uvm_km_free_poolpage: free a previously allocated pool page
883 *
884 * => if the pmap specifies an alternate unmapping method, we use it.
885 */
886
887 /* ARGSUSED */
888 void
889 uvm_km_free_poolpage1(map, addr)
890 struct vm_map *map;
891 vaddr_t addr;
892 {
893 #if defined(PMAP_UNMAP_POOLPAGE)
894 paddr_t pa;
895
896 pa = PMAP_UNMAP_POOLPAGE(addr);
897 uvm_pagefree(PHYS_TO_VM_PAGE(pa));
898 #else
899 int s;
900
901 /*
902 * NOTE: We may be called with a map that doens't require splvm
903 * protection (e.g. kernel_map). However, it does not hurt to
904 * go to splvm in this case (since unprocted maps will never be
905 * accessed in interrupt context).
906 *
907 * XXX We may want to consider changing the interface to this
908 * XXX function.
909 */
910
911 s = splvm();
912 uvm_km_free(map, addr, PAGE_SIZE);
913 splx(s);
914 #endif /* PMAP_UNMAP_POOLPAGE */
915 }
916