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