Home | History | Annotate | Line # | Download | only in uvm
      1 /*	$NetBSD: uvm_init.c,v 1.60 2025/11/05 00:39:46 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * from: Id: uvm_init.c,v 1.1.2.3 1998/02/06 05:15:27 chs Exp
     28  */
     29 
     30 /*
     31  * uvm_init.c: init the vm system.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uvm_init.c,v 1.60 2025/11/05 00:39:46 thorpej Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/debug.h>
     40 #include <sys/file.h>
     41 #include <sys/filedesc.h>
     42 #include <sys/resourcevar.h>
     43 #include <sys/kmem.h>
     44 #include <sys/mman.h>
     45 #include <sys/vnode.h>
     46 #include <sys/rndsource.h>
     47 
     48 #include <uvm/uvm.h>
     49 #include <uvm/uvm_pdpolicy.h>
     50 #include <uvm/uvm_physseg.h>
     51 #include <uvm/uvm_readahead.h>
     52 #include <uvm/uvm_rndsource.h>
     53 
     54 /*
     55  * struct uvm: we store most global vars in this structure to make them
     56  * easier to spot...
     57  */
     58 
     59 struct uvm uvm;		/* decl */
     60 struct uvmexp uvmexp;	/* decl */
     61 struct uvm_object *uvm_kernel_object;
     62 
     63 #if defined(__uvmexp_pagesize)
     64 const int * const uvmexp_pagesize = &uvmexp.pagesize;
     65 const int * const uvmexp_pagemask = &uvmexp.pagemask;
     66 const int * const uvmexp_pageshift = &uvmexp.pageshift;
     67 #endif
     68 
     69 kmutex_t uvm_kentry_lock __cacheline_aligned;
     70 
     71 struct krndsource uvm_fault_rndsource;
     72 
     73 /*
     74  * uvm_md_init: Init dependant on the MD boot context.
     75  *		called from MD code.
     76  */
     77 
     78 void
     79 uvm_md_init(void)
     80 {
     81 	static bool uvm_md_initialized;
     82 
     83 	if (! uvm_md_initialized) {
     84 		uvm_setpagesize(); /* initialize PAGE_SIZE-dependent vars */
     85 		uvm_physseg_init();
     86 		uvm_md_initialized = true;
     87 	}
     88 }
     89 
     90 /*
     91  * uvm_init: init the VM system.   called from kern/init_main.c.
     92  */
     93 
     94 void
     95 uvm_init(void)
     96 {
     97 	vaddr_t kvm_start, kvm_end;
     98 
     99 	/*
    100 	 * Ensure that the hardware set the page size, zero the UVM structure.
    101 	 */
    102 
    103 	if (uvmexp.pagesize == 0) {
    104 		panic("uvm_init: page size not set");
    105 	}
    106 
    107 	memset(&uvm, 0, sizeof(uvm));
    108 	averunnable.fscale = FSCALE;
    109 
    110 	/*
    111 	 * Init the page sub-system.  This includes allocating the vm_page
    112 	 * structures, and setting up all the page queues (and locks).
    113 	 * Available memory will be put in the "free" queue, kvm_start and
    114 	 * kvm_end will be set to the area of kernel virtual memory which
    115 	 * is available for general use.
    116 	 */
    117 
    118 	uvm_page_init(&kvm_start, &kvm_end);
    119 	uvm_pglistalloc_init();
    120 
    121 	/*
    122 	 * Init the map sub-system.
    123 	 */
    124 
    125 	uvm_map_init();
    126 
    127 	/*
    128 	 * Setup the kernel's virtual memory data structures.  This includes
    129 	 * setting up the kernel_map/kernel_object.
    130 	 * Bootstrap all kernel memory allocators.
    131 	 */
    132 
    133 	uao_init();
    134 	uvm_km_bootstrap(kvm_start, kvm_end);
    135 
    136 	/*
    137 	 * Setup uvm_map caches and init the amap.
    138 	 */
    139 
    140 	uvm_map_init_caches();
    141 	uvm_amap_init();
    142 
    143 	/*
    144 	 * Init the pmap module.  The pmap module is free to allocate
    145 	 * memory for its private use (e.g. pvlists).
    146 	 */
    147 
    148 	pmap_init();
    149 
    150 	/*
    151 	 * Make kernel memory allocators ready for use.
    152 	 * After this call the pool/kmem memory allocators can be used.
    153 	 */
    154 
    155 	uvm_km_init();
    156 #ifdef __HAVE_PMAP_PV_TRACK
    157 	pmap_pv_init();
    158 #endif
    159 
    160 #ifdef DEBUG
    161 	debug_init();
    162 #endif
    163 
    164 	/*
    165 	 * Init all pagers and the pager_map.
    166 	 */
    167 
    168 	uvm_pager_init();
    169 
    170 	/*
    171 	 * Initialize the uvm_loan() facility.
    172 	 */
    173 
    174 	uvm_loan_init();
    175 
    176 	/*
    177 	 * Enable paging of kernel objects.
    178 	 * This second pass of initializing kernel_object requires rw_obj,
    179 	 * so initialize that first.
    180 	 */
    181 
    182 	uao_create(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
    183 	    UAO_FLAG_KERNSWAP);
    184 
    185 	uvmpdpol_reinit();
    186 
    187 	/*
    188 	 * Init anonymous memory systems.
    189 	 */
    190 
    191 	uvm_anon_init();
    192 
    193 	uvm_uarea_init();
    194 
    195 	/*
    196 	 * Init readahead mechanism.
    197 	 */
    198 
    199 	uvm_ra_init();
    200 
    201 	/*
    202 	 * Initialize random source for page fault events.
    203 	 */
    204 
    205 	rnd_attach_source(&uvm_fault_rndsource, "uvmfault", RND_TYPE_VM,
    206 	    RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE|
    207 	    RND_FLAG_ESTIMATE_VALUE);
    208 }
    209