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