uvm_km.c revision 1.7 1 /* $NetBSD: uvm_km.c,v 1.7 1998/02/24 15:58:09 chuck Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 * Copyright (c) 1997 Charles D. Cranor and Washington University.
9 * Copyright (c) 1991, 1993, The Regents of the University of California.
10 *
11 * All rights reserved.
12 *
13 * This code is derived from software contributed to Berkeley by
14 * The Mach Operating System project at Carnegie-Mellon University.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by Charles D. Cranor,
27 * Washington University, the University of California, Berkeley and
28 * its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 * @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
46 * from: Id: uvm_km.c,v 1.1.2.14 1998/02/06 05:19:27 chs Exp
47 *
48 *
49 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
50 * All rights reserved.
51 *
52 * Permission to use, copy, modify and distribute this software and
53 * its documentation is hereby granted, provided that both the copyright
54 * notice and this permission notice appear in all copies of the
55 * software, derivative works or modified versions, and any portions
56 * thereof, and that both notices appear in supporting documentation.
57 *
58 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
59 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
60 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 *
62 * Carnegie Mellon requests users of this software to return to
63 *
64 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
65 * School of Computer Science
66 * Carnegie Mellon University
67 * Pittsburgh PA 15213-3890
68 *
69 * any improvements or extensions that they make and grant Carnegie the
70 * rights to redistribute these changes.
71 */
72
73 #include "opt_uvmhist.h"
74 #include "opt_pmap_new.h"
75
76 /*
77 * uvm_km.c: handle kernel memory allocation and management
78 */
79
80 /*
81 * overview of kernel memory management:
82 *
83 * the kernel virtual address space is mapped by "kernel_map." kernel_map
84 * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
85 * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
86 *
87 * the kernel_map has several "submaps." submaps can only appear in
88 * the kernel_map (user processes can't use them). submaps "take over"
89 * the management of a sub-range of the kernel's address space. submaps
90 * are typically allocated at boot time and are never released. kernel
91 * virtual address space that is mapped by a submap is locked by the
92 * submap's lock -- not the kernel_map's lock.
93 *
94 * thus, the useful feature of submaps is that they allow us to break
95 * up the locking and protection of the kernel address space into smaller
96 * chunks.
97 *
98 * the vm system has several standard kernel submaps, including:
99 * kmem_map => contains only wired kernel memory for the kernel
100 * malloc. *** access to kmem_map must be protected
101 * by splimp() because we are allowed to call malloc()
102 * at interrupt time ***
103 * mb_map => memory for large mbufs, *** protected by splimp ***
104 * pager_map => used to map "buf" structures into kernel space
105 * exec_map => used during exec to handle exec args
106 * etc...
107 *
108 * the kernel allocates its private memory out of special uvm_objects whose
109 * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
110 * are "special" and never die). all kernel objects should be thought of
111 * as large, fixed-sized, sparsely populated uvm_objects. each kernel
112 * object is equal to the size of kernel virtual address space (i.e. the
113 * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
114 *
115 * most kernel private memory lives in kernel_object. the only exception
116 * to this is for memory that belongs to submaps that must be protected
117 * by splimp(). each of these submaps has their own private kernel
118 * object (e.g. kmem_object, mb_object).
119 *
120 * note that just because a kernel object spans the entire kernel virutal
121 * address space doesn't mean that it has to be mapped into the entire space.
122 * large chunks of a kernel object's space go unused either because
123 * that area of kernel VM is unmapped, or there is some other type of
124 * object mapped into that range (e.g. a vnode). for submap's kernel
125 * objects, the only part of the object that can ever be populated is the
126 * offsets that are managed by the submap.
127 *
128 * note that the "offset" in a kernel object is always the kernel virtual
129 * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
130 * example:
131 * suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
132 * uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
133 * kernel map]. if uvm_km_alloc returns virtual address 0xf8235000,
134 * then that means that the page at offset 0x235000 in kernel_object is
135 * mapped at 0xf8235000.
136 *
137 * note that the offsets in kmem_object and mb_object also follow this
138 * rule. this means that the offsets for kmem_object must fall in the
139 * range of [vm_map_min(kmem_object) - vm_map_min(kernel_map)] to
140 * [vm_map_max(kmem_object) - vm_map_min(kernel_map)], so the offsets
141 * in those objects will typically not start at zero.
142 *
143 * kernel object have one other special property: when the kernel virtual
144 * memory mapping them is unmapped, the backing memory in the object is
145 * freed right away. this is done with the uvm_km_pgremove() function.
146 * this has to be done because there is no backing store for kernel pages
147 * and no need to save them after they are no longer referenced.
148 */
149
150 #include <sys/param.h>
151 #include <sys/systm.h>
152 #include <sys/proc.h>
153
154 #include <vm/vm.h>
155 #include <vm/vm_page.h>
156 #include <vm/vm_kern.h>
157
158 #include <uvm/uvm.h>
159
160 /*
161 * global data structures
162 */
163
164 vm_map_t kernel_map = NULL;
165
166 /*
167 * local functions
168 */
169
170 static int uvm_km_get __P((struct uvm_object *, vm_offset_t,
171 vm_page_t *, int *, int, vm_prot_t, int, int));
172 /*
173 * local data structues
174 */
175
176 static struct vm_map kernel_map_store;
177 static struct uvm_object kmem_object_store;
178 static struct uvm_object mb_object_store;
179
180 static struct uvm_pagerops km_pager = {
181 NULL, /* init */
182 NULL, /* attach */
183 NULL, /* reference */
184 NULL, /* detach */
185 NULL, /* fault */
186 NULL, /* flush */
187 uvm_km_get, /* get */
188 /* ... rest are NULL */
189 };
190
191 /*
192 * uvm_km_get: pager get function for kernel objects
193 *
194 * => currently we do not support pageout to the swap area, so this
195 * pager is very simple. eventually we may want an anonymous
196 * object pager which will do paging.
197 * => XXXCDC: this pager should be phased out in favor of the aobj pager
198 */
199
200
201 static int uvm_km_get(uobj, offset, pps, npagesp, centeridx, access_type,
202 advice, flags)
203
204 struct uvm_object *uobj;
205 vm_offset_t offset;
206 struct vm_page **pps;
207 int *npagesp;
208 int centeridx, advice, flags;
209 vm_prot_t access_type;
210
211 {
212 vm_offset_t current_offset;
213 vm_page_t ptmp;
214 int lcv, gotpages, maxpages;
215 boolean_t done;
216 UVMHIST_FUNC("uvm_km_get"); UVMHIST_CALLED(maphist);
217
218 UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0);
219
220 /*
221 * get number of pages
222 */
223
224 maxpages = *npagesp;
225
226 /*
227 * step 1: handled the case where fault data structures are locked.
228 */
229
230 if (flags & PGO_LOCKED) {
231
232 /*
233 * step 1a: get pages that are already resident. only do this
234 * if the data structures are locked (i.e. the first time through).
235 */
236
237 done = TRUE; /* be optimistic */
238 gotpages = 0; /* # of pages we got so far */
239
240 for (lcv = 0, current_offset = offset ;
241 lcv < maxpages ; lcv++, current_offset += PAGE_SIZE) {
242
243 /* do we care about this page? if not, skip it */
244 if (pps[lcv] == PGO_DONTCARE)
245 continue;
246
247 /* lookup page */
248 ptmp = uvm_pagelookup(uobj, current_offset);
249
250 /* null? attempt to allocate the page */
251 if (ptmp == NULL) {
252 ptmp = uvm_pagealloc(uobj, current_offset, NULL);
253 if (ptmp) {
254 ptmp->flags &= ~(PG_BUSY|PG_FAKE); /* new page */
255 UVM_PAGE_OWN(ptmp, NULL);
256 ptmp->wire_count = 1; /* XXX: prevents pageout attempts */
257 uvm_pagezero(ptmp);
258 }
259 }
260
261 /* to be useful must get a non-busy, non-released page */
262 if (ptmp == NULL || (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
263 if (lcv == centeridx || (flags & PGO_ALLPAGES) != 0)
264 done = FALSE; /* need to do a wait or I/O! */
265 continue;
266 }
267
268 /* useful page: busy/lock it and plug it in our result array */
269 ptmp->flags |= PG_BUSY; /* caller must un-busy this page */
270 UVM_PAGE_OWN(ptmp, "uvm_km_get1");
271 pps[lcv] = ptmp;
272 gotpages++;
273
274 } /* "for" lcv loop */
275
276 /*
277 * step 1b: now we've either done everything needed or we to unlock
278 * and do some waiting or I/O.
279 */
280
281 UVMHIST_LOG(maphist, "<- done (done=%d)", done, 0,0,0);
282
283 *npagesp = gotpages;
284 if (done)
285 return(VM_PAGER_OK); /* bingo! */
286 else
287 return(VM_PAGER_UNLOCK); /* EEK! Need to unlock and I/O */
288 }
289
290 /*
291 * step 2: get non-resident or busy pages.
292 * object is locked. data structures are unlocked.
293 */
294
295 for (lcv = 0, current_offset = offset ;
296 lcv < maxpages ; lcv++, current_offset += PAGE_SIZE) {
297
298 /* skip over pages we've already gotten or don't want */
299 /* skip over pages we don't _have_ to get */
300 if (pps[lcv] != NULL ||
301 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
302 continue;
303
304 /*
305 * we have yet to locate the current page (pps[lcv]). we first
306 * look for a page that is already at the current offset. if we
307 * find a page, we check to see if it is busy or released. if that
308 * is the case, then we sleep on the page until it is no longer busy
309 * or released and repeat the lookup. if the page we found is
310 * neither busy nor released, then we busy it (so we own it) and
311 * plug it into pps[lcv]. this 'break's the following while loop
312 * and indicates we are ready to move on to the next page in the
313 * "lcv" loop above.
314 *
315 * if we exit the while loop with pps[lcv] still set to NULL, then
316 * it means that we allocated a new busy/fake/clean page ptmp in the
317 * object and we need to do I/O to fill in the data.
318 */
319
320 while (pps[lcv] == NULL) { /* top of "pps" while loop */
321
322 /* look for a current page */
323 ptmp = uvm_pagelookup(uobj, current_offset);
324
325 /* nope? allocate one now (if we can) */
326 if (ptmp == NULL) {
327
328 ptmp = uvm_pagealloc(uobj, current_offset, NULL); /* alloc */
329
330 /* out of RAM? */
331 if (ptmp == NULL) {
332 simple_unlock(&uobj->vmobjlock);
333 uvm_wait("kmgetwait1");
334 simple_lock(&uobj->vmobjlock);
335 continue; /* goto top of pps while loop */
336 }
337
338 /*
339 * got new page ready for I/O. break pps while loop. pps[lcv] is
340 * still NULL.
341 */
342 break;
343 }
344
345 /* page is there, see if we need to wait on it */
346 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
347 ptmp->flags |= PG_WANTED;
348 UVM_UNLOCK_AND_WAIT(ptmp,&uobj->vmobjlock,0,"uvn_get",0);
349 simple_lock(&uobj->vmobjlock);
350 continue; /* goto top of pps while loop */
351 }
352
353 /*
354 * if we get here then the page has become resident and unbusy
355 * between steps 1 and 2. we busy it now (so we own it) and set
356 * pps[lcv] (so that we exit the while loop).
357 */
358 ptmp->flags |= PG_BUSY; /* we own it, caller must un-busy */
359 UVM_PAGE_OWN(ptmp, "uvm_km_get2");
360 pps[lcv] = ptmp;
361 }
362
363 /*
364 * if we own the a valid page at the correct offset, pps[lcv] will
365 * point to it. nothing more to do except go to the next page.
366 */
367
368 if (pps[lcv])
369 continue; /* next lcv */
370
371 /*
372 * we have a "fake/busy/clean" page that we just allocated.
373 * do the needed "i/o" (in this case that means zero it).
374 */
375
376 uvm_pagezero(ptmp);
377 ptmp->flags &= ~(PG_FAKE);
378 ptmp->wire_count = 1; /* XXX: prevents pageout attempts */
379 pps[lcv] = ptmp;
380
381 } /* lcv loop */
382
383 /*
384 * finally, unlock object and return.
385 */
386
387 simple_unlock(&uobj->vmobjlock);
388 UVMHIST_LOG(maphist, "<- done (OK)",0,0,0,0);
389 return(VM_PAGER_OK);
390 }
391
392 /*
393 * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
394 * KVM already allocated for text, data, bss, and static data structures).
395 *
396 * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
397 * we assume that [min -> start] has already been allocated and that
398 * "end" is the end.
399 */
400
401 void uvm_km_init(start, end)
402
403 vm_offset_t start, end;
404
405 {
406 vm_offset_t base = VM_MIN_KERNEL_ADDRESS;
407
408 /*
409 * first, init kernel memory objects.
410 */
411
412 /* kernel_object: for pageable anonymous kernel memory */
413 uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
414 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
415
416 /* kmem_object: for malloc'd memory (always wired, protected by splimp) */
417 simple_lock_init(&kmem_object_store.vmobjlock);
418 kmem_object_store.pgops = &km_pager;
419 TAILQ_INIT(&kmem_object_store.memq);
420 kmem_object_store.uo_npages = 0;
421 kmem_object_store.uo_refs = UVM_OBJ_KERN;
422 /* we are special. we never die */
423 uvmexp.kmem_object = &kmem_object_store;
424
425 /* mb_object: for mbuf memory (always wired, protected by splimp) */
426 simple_lock_init(&mb_object_store.vmobjlock);
427 mb_object_store.pgops = &km_pager;
428 TAILQ_INIT(&mb_object_store.memq);
429 mb_object_store.uo_npages = 0;
430 mb_object_store.uo_refs = UVM_OBJ_KERN;
431 /* we are special. we never die */
432 uvmexp.mb_object = &mb_object_store;
433
434 /*
435 * init the map and reserve allready allocated kernel space
436 * before installing.
437 */
438
439 uvm_map_setup(&kernel_map_store, base, end, FALSE);
440 kernel_map_store.pmap = pmap_kernel();
441 if (uvm_map(&kernel_map_store, &base, start - base, NULL, UVM_UNKNOWN_OFFSET,
442 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
443 UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != KERN_SUCCESS)
444 panic("uvm_km_init: could not reserve space for kernel");
445
446 /*
447 * install!
448 */
449
450 kernel_map = &kernel_map_store;
451 }
452
453 /*
454 * uvm_km_suballoc: allocate a submap in the kernel map. once a submap
455 * is allocated all references to that area of VM must go through it. this
456 * allows the locking of VAs in kernel_map to be broken up into regions.
457 *
458 * => if `fixed' is true, *min specifies where the region described
459 * by the submap must start
460 * => if submap is non NULL we use that as the submap, otherwise we
461 * alloc a new map
462 */
463
464 struct vm_map *uvm_km_suballoc(map, min, max, size, pageable, fixed, submap)
465
466 struct vm_map *map;
467 vm_offset_t *min, *max; /* OUT, OUT */
468 vm_size_t size;
469 boolean_t pageable;
470 boolean_t fixed;
471 struct vm_map *submap;
472
473 {
474 int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
475
476 size = round_page(size); /* round up to pagesize */
477
478 /*
479 * first allocate a blank spot in the parent map
480 */
481
482 if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET,
483 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
484 UVM_ADV_RANDOM, mapflags)) != KERN_SUCCESS) {
485 panic("uvm_km_suballoc: unable to allocate space in parent map");
486 }
487
488 /*
489 * set VM bounds (min is filled in by uvm_map)
490 */
491
492 *max = *min + size;
493
494 /*
495 * add references to pmap and create or init the submap
496 */
497
498 pmap_reference(vm_map_pmap(map));
499 if (submap == NULL) {
500 submap = uvm_map_create(vm_map_pmap(map), *min, *max, pageable);
501 if (submap == NULL)
502 panic("uvm_km_suballoc: unable to create submap");
503 } else {
504 uvm_map_setup(submap, *min, *max, pageable);
505 submap->pmap = vm_map_pmap(map);
506 }
507
508 /*
509 * now let uvm_map_submap plug in it...
510 */
511
512 if (uvm_map_submap(map, *min, *max, submap) != KERN_SUCCESS)
513 panic("uvm_km_suballoc: submap allocation failed");
514
515 return(submap);
516 }
517
518 /*
519 * uvm_km_pgremove: remove pages from a kernel uvm_object.
520 *
521 * => when you unmap a part of anonymous kernel memory you want to toss
522 * the pages right away. (this gets called from uvm_unmap_...).
523 */
524
525 #define UKM_HASH_PENALTY 4 /* a guess */
526
527 void uvm_km_pgremove(uobj, start, end)
528
529 struct uvm_object *uobj;
530 vm_offset_t start, end;
531
532 {
533 boolean_t by_list, is_aobj;
534 struct vm_page *pp, *ppnext;
535 vm_offset_t curoff;
536 UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
537
538 simple_lock(&uobj->vmobjlock); /* lock object */
539
540 /* is uobj an aobj? */
541 is_aobj = uobj->pgops == &aobj_pager;
542
543 /* choose cheapest traversal */
544 by_list = (uobj->uo_npages <=
545 ((end - start) / PAGE_SIZE) * UKM_HASH_PENALTY);
546
547 if (by_list)
548 goto loop_by_list;
549
550 /* by hash */
551
552 for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
553 pp = uvm_pagelookup(uobj, curoff);
554 if (pp == NULL)
555 continue;
556
557 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,pp->flags & PG_BUSY,0,0);
558 /* now do the actual work */
559 if (pp->flags & PG_BUSY)
560 pp->flags |= PG_RELEASED; /* owner must check for this when done */
561 else {
562 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
563
564 /*
565 * if this kernel object is an aobj, free the swap slot.
566 */
567 if (is_aobj) {
568 int slot = uao_set_swslot(uobj, curoff / PAGE_SIZE, 0);
569
570 if (slot)
571 uvm_swap_free(slot, 1);
572 }
573
574 uvm_lock_pageq();
575 uvm_pagefree(pp);
576 uvm_unlock_pageq();
577 }
578 /* done */
579
580 }
581 simple_unlock(&uobj->vmobjlock);
582 return;
583
584 loop_by_list:
585
586 for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
587
588 ppnext = pp->listq.tqe_next;
589 if (pp->offset < start || pp->offset >= end) {
590 continue;
591 }
592
593 UVMHIST_LOG(maphist," page 0x%x, busy=%d", pp,pp->flags & PG_BUSY,0,0);
594 /* now do the actual work */
595 if (pp->flags & PG_BUSY)
596 pp->flags |= PG_RELEASED; /* owner must check for this when done */
597 else {
598 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
599
600 /*
601 * if this kernel object is an aobj, free the swap slot.
602 */
603 if (is_aobj) {
604 int slot = uao_set_swslot(uobj, pp->offset / PAGE_SIZE, 0);
605
606 if (slot)
607 uvm_swap_free(slot, 1);
608 }
609
610 uvm_lock_pageq();
611 uvm_pagefree(pp);
612 uvm_unlock_pageq();
613 }
614 /* done */
615
616 }
617 simple_unlock(&uobj->vmobjlock);
618 return;
619 }
620
621
622 /*
623 * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
624 *
625 * => we map wired memory into the specified map using the obj passed in
626 * => NOTE: we can return NULL even if we can wait if there is not enough
627 * free VM space in the map... caller should be prepared to handle
628 * this case.
629 * => we return KVA of memory allocated
630 * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
631 * lock the map
632 */
633
634 vm_offset_t uvm_km_kmemalloc(map, obj, size, flags)
635
636 vm_map_t map;
637 struct uvm_object *obj;
638 vm_size_t size;
639 int flags;
640
641 {
642 vm_offset_t kva, loopva;
643 vm_offset_t offset;
644 struct vm_page *pg;
645 UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
646
647
648 UVMHIST_LOG(maphist," (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
649 map, obj, size, flags);
650 #ifdef DIAGNOSTIC
651 /* sanity check */
652 if (vm_map_pmap(map) != pmap_kernel())
653 panic("uvm_km_kmemalloc: invalid map");
654 #endif
655
656 /*
657 * setup for call
658 */
659
660 size = round_page(size);
661 kva = vm_map_min(map); /* hint */
662
663 /*
664 * allocate some virtual space
665 */
666
667 if (uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
668 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
669 UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
670 != KERN_SUCCESS) {
671 UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
672 return(0);
673 }
674
675 /*
676 * if all we wanted was VA, return now
677 */
678
679 if (flags & UVM_KMF_VALLOC) {
680 UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
681 return(kva);
682 }
683 /*
684 * recover object offset from virtual address
685 */
686
687 offset = kva - vm_map_min(kernel_map);
688 UVMHIST_LOG(maphist, " kva=0x%x, offset=0x%x", kva, offset,0,0);
689
690 /*
691 * now allocate and map in the memory... note that we are the only ones
692 * whom should ever get a handle on this area of VM.
693 */
694
695 loopva = kva;
696 while (size) {
697 simple_lock(&obj->vmobjlock);
698 pg = uvm_pagealloc(obj, offset, NULL);
699 if (pg) {
700 pg->flags &= ~PG_BUSY; /* new page */
701 UVM_PAGE_OWN(pg, NULL);
702
703 pg->wire_count = 1;
704 uvmexp.wired++;
705 }
706 simple_unlock(&obj->vmobjlock);
707
708 /*
709 * out of memory?
710 */
711
712 if (pg == NULL) {
713 if (flags & UVM_KMF_NOWAIT) {
714 uvm_unmap(map, kva, kva + size, 0); /* free everything! */
715 return(0);
716 } else {
717 uvm_wait("km_getwait2"); /* sleep here */
718 continue;
719 }
720 }
721
722 /*
723 * map it in: note that we call pmap_enter with the map and object
724 * unlocked in case we are kmem_map/kmem_object (because if pmap_enter
725 * wants to allocate out of kmem_object it will need to lock it itself!)
726 */
727 #if defined(PMAP_NEW)
728 pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), VM_PROT_ALL);
729 #else
730 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg), UVM_PROT_ALL, TRUE);
731 #endif
732 loopva += PAGE_SIZE;
733 offset += PAGE_SIZE;
734 size -= PAGE_SIZE;
735 }
736
737 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
738 return(kva);
739 }
740
741 /*
742 * uvm_km_free: free an area of kernel memory
743 */
744
745 void uvm_km_free(map, addr, size)
746
747 vm_map_t map;
748 vm_offset_t addr;
749 vm_size_t size;
750
751 {
752 uvm_unmap(map, trunc_page(addr), round_page(addr+size), 1);
753 }
754
755 /*
756 * uvm_km_free_wakeup: free an area of kernel memory and wake up
757 * anyone waiting for vm space.
758 *
759 * => XXX: "wanted" bit + unlock&wait on other end?
760 */
761
762 void uvm_km_free_wakeup(map, addr, size)
763
764 vm_map_t map;
765 vm_offset_t addr;
766 vm_size_t size;
767
768 {
769 vm_map_entry_t dead_entries;
770
771 vm_map_lock(map);
772 (void)uvm_unmap_remove(map, trunc_page(addr), round_page(addr+size), 1,
773 &dead_entries);
774 thread_wakeup(map);
775 vm_map_unlock(map);
776
777 if (dead_entries != NULL)
778 uvm_unmap_detach(dead_entries, 0);
779 }
780
781 /*
782 * uvm_km_alloc1: allocate wired down memory in the kernel map.
783 *
784 * => we can sleep if needed
785 */
786
787 vm_offset_t uvm_km_alloc1(map, size, zeroit)
788
789 vm_map_t map;
790 vm_size_t size;
791 boolean_t zeroit;
792
793 {
794 vm_offset_t kva, loopva, offset;
795 struct vm_page *pg;
796 UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
797
798 UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
799
800 #ifdef DIAGNOSTIC
801 if (vm_map_pmap(map) != pmap_kernel())
802 panic("uvm_km_alloc1");
803 #endif
804
805 size = round_page(size);
806 kva = vm_map_min(map); /* hint */
807
808 /*
809 * allocate some virtual space
810 */
811
812 if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
813 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
814 UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
815 UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
816 return(0);
817 }
818
819 /*
820 * recover object offset from virtual address
821 */
822
823 offset = kva - vm_map_min(kernel_map);
824 UVMHIST_LOG(maphist," kva=0x%x, offset=0x%x", kva, offset,0,0);
825
826 /*
827 * now allocate the memory. we must be careful about released pages.
828 */
829
830 loopva = kva;
831 while (size) {
832 simple_lock(&uvm.kernel_object->vmobjlock);
833 pg = uvm_pagelookup(uvm.kernel_object, offset);
834
835 /* if we found a page in an unallocated region, it must be released */
836 if (pg) {
837 if ((pg->flags & PG_RELEASED) == 0)
838 panic("uvm_km_alloc1: non-released page");
839 pg->flags |= PG_WANTED;
840 UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,0,"km_alloc",0);
841 continue; /* retry */
842 }
843
844 /* allocate ram */
845 pg = uvm_pagealloc(uvm.kernel_object, offset, NULL);
846 if (pg) {
847 pg->flags &= ~PG_BUSY; /* new page */
848 UVM_PAGE_OWN(pg, NULL);
849 }
850 simple_unlock(&uvm.kernel_object->vmobjlock);
851 if (pg == NULL) {
852 uvm_wait("km_alloc1w"); /* wait for memory */
853 continue;
854 }
855
856 /* map it in */
857 #if defined(PMAP_NEW)
858 pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), UVM_PROT_ALL);
859 #else
860 pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg), UVM_PROT_ALL, TRUE);
861 #endif
862 loopva += PAGE_SIZE;
863 offset += PAGE_SIZE;
864 size -= PAGE_SIZE;
865 }
866
867 /*
868 * zero on request (note that "size" is now zero due to the above loop
869 * so we need to subtract kva from loopva to reconstruct the size).
870 */
871
872 if (zeroit)
873 bzero((caddr_t)kva, loopva - kva);
874
875 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
876 return(kva);
877 }
878
879 /*
880 * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
881 *
882 * => memory is not allocated until fault time
883 */
884
885 vm_offset_t uvm_km_valloc(map, size)
886
887 vm_map_t map;
888 vm_size_t size;
889
890 {
891 vm_offset_t kva;
892 UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
893
894 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
895
896 #ifdef DIAGNOSTIC
897 if (vm_map_pmap(map) != pmap_kernel())
898 panic("uvm_km_valloc");
899 #endif
900
901 size = round_page(size);
902 kva = vm_map_min(map); /* hint */
903
904 /*
905 * allocate some virtual space. will be demand filled by kernel_object.
906 */
907
908 if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
909 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
910 UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
911 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
912 return(0);
913 }
914
915 UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
916 return(kva);
917 }
918
919 /*
920 * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
921 *
922 * => memory is not allocated until fault time
923 * => if no room in map, wait for space to free, unless requested size
924 * is larger than map (in which case we return 0)
925 */
926
927 vm_offset_t uvm_km_valloc_wait(map, size)
928
929 vm_map_t map;
930 vm_size_t size;
931
932 {
933 vm_offset_t kva;
934 UVMHIST_FUNC("uvm_km_valloc_wait"); UVMHIST_CALLED(maphist);
935
936 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
937
938 #ifdef DIAGNOSTIC
939 if (vm_map_pmap(map) != pmap_kernel())
940 panic("uvm_km_valloc_wait");
941 #endif
942
943 size = round_page(size);
944 if (size > vm_map_max(map) - vm_map_min(map))
945 return(0);
946
947 while (1) {
948 kva = vm_map_min(map); /* hint */
949
950 /*
951 * allocate some virtual space. will be demand filled by kernel_object.
952 */
953
954 if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
955 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
956 UVM_ADV_RANDOM, 0)) == KERN_SUCCESS){
957 UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
958 return(kva);
959 }
960
961 /*
962 * failed. sleep for a while (on map)
963 */
964
965 UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
966 tsleep((caddr_t)map, PVM, "vallocwait", 0);
967 }
968 /*NOTREACHED*/
969 }
970