160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_init.c 8.1 (Berkeley) 6/11/93 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Initialize the Virtual Memory subsystem. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67874651b1SDavid E. O'Brien #include <sys/cdefs.h> 68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 69874651b1SDavid E. O'Brien 70df8bae1dSRodney W. Grimes #include <sys/param.h> 7187ab1a10SMark Johnston #include <sys/domainset.h> 722b14f991SJulian Elischer #include <sys/kernel.h> 73fb919e4dSMark Murray #include <sys/lock.h> 74fb919e4dSMark Murray #include <sys/proc.h> 7589f6b863SAttilio Rao #include <sys/rwlock.h> 765df87b21SJeff Roberson #include <sys/malloc.h> 77f5fca0d8SKris Kennaway #include <sys/sysctl.h> 7805f0fdd2SPoul-Henning Kamp #include <sys/systm.h> 79cebde069SMike Silbersack #include <sys/selinfo.h> 8021fae961SJeff Roberson #include <sys/smp.h> 81cebde069SMike Silbersack #include <sys/pipe.h> 82219d632cSMatthew Dillon #include <sys/bio.h> 83219d632cSMatthew Dillon #include <sys/buf.h> 845f518366SJeff Roberson #include <sys/vmem.h> 857a469c8eSJeff Roberson #include <sys/vmmeter.h> 86df8bae1dSRodney W. Grimes 87df8bae1dSRodney W. Grimes #include <vm/vm.h> 88219d632cSMatthew Dillon #include <vm/vm_param.h> 89219d632cSMatthew Dillon #include <vm/vm_kern.h> 90efeaf95aSDavid Greenman #include <vm/vm_object.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 927a469c8eSJeff Roberson #include <vm/vm_phys.h> 93e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 94efeaf95aSDavid Greenman #include <vm/vm_map.h> 9524a1cce3SDavid Greenman #include <vm/vm_pager.h> 96efeaf95aSDavid Greenman #include <vm/vm_extern.h> 97df8bae1dSRodney W. Grimes 98a81c400eSJeff Roberson extern void uma_startup1(vm_offset_t); 997a469c8eSJeff Roberson 1000fca57b8SThomas Moestl long physmem; 1010fca57b8SThomas Moestl 102df8bae1dSRodney W. Grimes /* 1032b14f991SJulian Elischer * System initialization 1042b14f991SJulian Elischer */ 10511caded3SAlfred Perlstein static void vm_mem_init(void *); 106237fdd78SRobert Watson SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL); 1072b14f991SJulian Elischer 1082b14f991SJulian Elischer /* 109df8bae1dSRodney W. Grimes * vm_init initializes the virtual memory system. 110df8bae1dSRodney W. Grimes * This is done only by the first cpu up. 111df8bae1dSRodney W. Grimes */ 1122b14f991SJulian Elischer static void 1131aed6d48SMark Johnston vm_mem_init(void *dummy) 114df8bae1dSRodney W. Grimes { 1155df87b21SJeff Roberson 116df8bae1dSRodney W. Grimes /* 11787ab1a10SMark Johnston * Initialize static domainsets, used by various allocators. 11887ab1a10SMark Johnston */ 11987ab1a10SMark Johnston domainset_init(); 12087ab1a10SMark Johnston 12187ab1a10SMark Johnston /* 122920239efSMark Johnston * Initialize resident memory structures. From here on, all physical 1230d94caffSDavid Greenman * memory is accounted for, and we use only virtual addresses. 124df8bae1dSRodney W. Grimes */ 12526f9a767SRodney W. Grimes vm_set_page_size(); 126889eb0fcSAlan Cox virtual_avail = vm_page_startup(virtual_avail); 127e7841165SDag-Erling Smørgrav 128920239efSMark Johnston /* 129920239efSMark Johnston * Set an initial domain policy for thread0 so that allocations 130920239efSMark Johnston * can work. 131920239efSMark Johnston */ 132920239efSMark Johnston domainset_zero(); 133920239efSMark Johnston 134a81c400eSJeff Roberson /* Bootstrap the kernel memory allocator. */ 135a81c400eSJeff Roberson uma_startup1(virtual_avail); 136a81c400eSJeff Roberson 137df8bae1dSRodney W. Grimes /* 138df8bae1dSRodney W. Grimes * Initialize other VM packages 139df8bae1dSRodney W. Grimes */ 1405f518366SJeff Roberson vmem_startup(); 141a316d390SJohn Dyson vm_object_init(); 142df8bae1dSRodney W. Grimes vm_map_startup(); 143df8bae1dSRodney W. Grimes kmem_init(virtual_avail, virtual_end); 1445df87b21SJeff Roberson 1455df87b21SJeff Roberson kmem_init_zero_region(); 146bdb93eb2SAlan Cox pmap_init(); 147df8bae1dSRodney W. Grimes vm_pager_init(); 148df8bae1dSRodney W. Grimes } 149219d632cSMatthew Dillon 150219d632cSMatthew Dillon void 151219d632cSMatthew Dillon vm_ksubmap_init(struct kva_md_info *kmi) 152219d632cSMatthew Dillon { 153f49fd63aSJohn Baldwin caddr_t firstaddr, v; 154219d632cSMatthew Dillon vm_size_t size = 0; 155447b3772SPeter Wemm long physmem_est; 156219d632cSMatthew Dillon vm_offset_t minaddr; 157219d632cSMatthew Dillon vm_offset_t maxaddr; 158219d632cSMatthew Dillon 159*45cc8519SColin Percival TSENTER(); 160219d632cSMatthew Dillon /* 161219d632cSMatthew Dillon * Allocate space for system data structures. 162219d632cSMatthew Dillon * The first available kernel virtual address is in "v". 163219d632cSMatthew Dillon * As pages of kernel virtual memory are allocated, "v" is incremented. 164219d632cSMatthew Dillon * As pages of memory are allocated and cleared, 165219d632cSMatthew Dillon * "firstaddr" is incremented. 166219d632cSMatthew Dillon */ 167219d632cSMatthew Dillon 168219d632cSMatthew Dillon /* 169219d632cSMatthew Dillon * Make two passes. The first pass calculates how much memory is 170219d632cSMatthew Dillon * needed and allocates it. The second pass assigns virtual 171219d632cSMatthew Dillon * addresses to the various data structures. 172219d632cSMatthew Dillon */ 173f49fd63aSJohn Baldwin firstaddr = NULL; 174219d632cSMatthew Dillon again: 175f49fd63aSJohn Baldwin v = firstaddr; 176219d632cSMatthew Dillon 177219d632cSMatthew Dillon /* 178219d632cSMatthew Dillon * Discount the physical memory larger than the size of kernel_map 179219d632cSMatthew Dillon * to avoid eating up all of KVA space. 180219d632cSMatthew Dillon */ 181f0165b1cSKonstantin Belousov physmem_est = lmin(physmem, btoc(vm_map_max(kernel_map) - 182f0165b1cSKonstantin Belousov vm_map_min(kernel_map))); 183219d632cSMatthew Dillon 184219d632cSMatthew Dillon v = kern_vfs_bio_buffer_alloc(v, physmem_est); 185219d632cSMatthew Dillon 186219d632cSMatthew Dillon /* 187219d632cSMatthew Dillon * End of first pass, size has been calculated so allocate memory 188219d632cSMatthew Dillon */ 189f49fd63aSJohn Baldwin if (firstaddr == NULL) { 190857961d9SRobert Drehmel size = (vm_size_t)v; 191edb2994aSAndrew Gallatin #ifdef VM_FREELIST_DMA32 192edb2994aSAndrew Gallatin /* 193edb2994aSAndrew Gallatin * Try to protect 32-bit DMAable memory from the largest 194edb2994aSAndrew Gallatin * early alloc of wired mem. 195edb2994aSAndrew Gallatin */ 196db7c2a48SAlan Cox firstaddr = kmem_alloc_attr(size, M_ZERO | M_NOWAIT, 197db7c2a48SAlan Cox (vm_paddr_t)1 << 32, ~(vm_paddr_t)0, VM_MEMATTR_DEFAULT); 198f49fd63aSJohn Baldwin if (firstaddr == NULL) 199edb2994aSAndrew Gallatin #endif 20083a90bffSAlan Cox firstaddr = kmem_malloc(size, M_ZERO | M_WAITOK); 201f49fd63aSJohn Baldwin if (firstaddr == NULL) 202219d632cSMatthew Dillon panic("startup: no room for tables"); 203219d632cSMatthew Dillon goto again; 204219d632cSMatthew Dillon } 205219d632cSMatthew Dillon 206219d632cSMatthew Dillon /* 207219d632cSMatthew Dillon * End of second pass, addresses have been assigned 208219d632cSMatthew Dillon */ 209f49fd63aSJohn Baldwin if ((vm_size_t)(v - firstaddr) != size) 210219d632cSMatthew Dillon panic("startup: table size inconsistency"); 211219d632cSMatthew Dillon 2125df87b21SJeff Roberson /* 21346b0292aSGleb Smirnoff * Allocate the clean map to hold all of I/O virtual memory. 2145df87b21SJeff Roberson */ 215cd853791SKonstantin Belousov size = (long)nbuf * BKVASIZE + (long)bio_transient_maxcnt * maxphys; 216f49fd63aSJohn Baldwin kmi->clean_sva = kva_alloc(size); 217f49fd63aSJohn Baldwin kmi->clean_eva = kmi->clean_sva + size; 2185f518366SJeff Roberson 2195df87b21SJeff Roberson /* 2205df87b21SJeff Roberson * Allocate the buffer arena. 22121fae961SJeff Roberson * 22221fae961SJeff Roberson * Enable the quantum cache if we have more than 4 cpus. This 22321fae961SJeff Roberson * avoids lock contention at the expense of some fragmentation. 2245df87b21SJeff Roberson */ 2255f518366SJeff Roberson size = (long)nbuf * BKVASIZE; 226f49fd63aSJohn Baldwin kmi->buffer_sva = kmi->clean_sva; 2275f518366SJeff Roberson kmi->buffer_eva = kmi->buffer_sva + size; 2285f518366SJeff Roberson vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size, 229cfbb5f8cSEric van Gyzen PAGE_SIZE, (mp_ncpus > 4) ? BKVASIZE * 8 : 0, M_WAITOK); 2305f518366SJeff Roberson 2315df87b21SJeff Roberson /* 2325df87b21SJeff Roberson * And optionally transient bio space. 2335df87b21SJeff Roberson */ 2347db07e1cSKonstantin Belousov if (bio_transient_maxcnt != 0) { 235cd853791SKonstantin Belousov size = (long)bio_transient_maxcnt * maxphys; 2365f518366SJeff Roberson vmem_init(transient_arena, "transient arena", 237f49fd63aSJohn Baldwin kmi->buffer_eva, size, PAGE_SIZE, 0, M_WAITOK); 2387db07e1cSKonstantin Belousov } 2395df87b21SJeff Roberson 2405df87b21SJeff Roberson /* 241ec492b13SMark Johnston * Allocate the pageable submaps. We may cache an exec map entry per 242ec492b13SMark Johnston * CPU, so we therefore need to reserve space for at least ncpu+1 243ec492b13SMark Johnston * entries to avoid deadlock. The exec map is also used by some image 244ec492b13SMark Johnston * activators, so we leave a fixed number of pages for their use. 2455df87b21SJeff Roberson */ 246ec492b13SMark Johnston #ifdef __LP64__ 247ec492b13SMark Johnston exec_map_entries = 8 * mp_ncpus; 248ec492b13SMark Johnston #else 2498d65cba2SMark Johnston exec_map_entries = 2 * mp_ncpus + 4; 250ec492b13SMark Johnston #endif 251ec492b13SMark Johnston exec_map_entry_size = round_page(PATH_MAX + ARG_MAX); 2527dd979dfSMark Johnston kmem_subinit(exec_map, kernel_map, &minaddr, &maxaddr, 2537dd979dfSMark Johnston exec_map_entries * exec_map_entry_size + 64 * PAGE_SIZE, false); 2547dd979dfSMark Johnston kmem_subinit(pipe_map, kernel_map, &minaddr, &maxaddr, maxpipekva, 2557dd979dfSMark Johnston false); 256*45cc8519SColin Percival TSEXIT(); 257219d632cSMatthew Dillon } 258