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