/*-1* SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)2*3* Copyright (c) 1991, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* The Mach Operating System project at Carnegie-Mellon University.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33*34* Copyright (c) 1987, 1990 Carnegie-Mellon University.35* All rights reserved.36*37* Authors: Avadis Tevanian, Jr., Michael Wayne Young38*39* Permission to use, copy, modify and distribute this software and40* its documentation is hereby granted, provided that both the copyright41* notice and this permission notice appear in all copies of the42* software, derivative works or modified versions, and any portions43* thereof, and that both notices appear in supporting documentation.44*45* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"46* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND47* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.48*49* Carnegie Mellon requests users of this software to return to50*51* Software Distribution Coordinator or [email protected]52* School of Computer Science53* Carnegie Mellon University54* Pittsburgh PA 15213-389055*56* any improvements or extensions that they make and grant Carnegie the57* rights to redistribute these changes.58*/5960/*61* Initialize the Virtual Memory subsystem.62*/6364#include <sys/param.h>65#include <sys/domainset.h>66#include <sys/kernel.h>67#include <sys/lock.h>68#include <sys/proc.h>69#include <sys/rwlock.h>70#include <sys/malloc.h>71#include <sys/sysctl.h>72#include <sys/systm.h>73#include <sys/selinfo.h>74#include <sys/smp.h>75#include <sys/pipe.h>76#include <sys/bio.h>77#include <sys/buf.h>78#include <sys/vmem.h>79#include <sys/vmmeter.h>8081#include <vm/vm.h>82#include <vm/vm_param.h>83#include <vm/vm_kern.h>84#include <vm/vm_object.h>85#include <vm/vm_page.h>86#include <vm/vm_phys.h>87#include <vm/vm_pagequeue.h>88#include <vm/vm_map.h>89#include <vm/vm_pager.h>90#include <vm/vm_extern.h>9192extern void uma_startup1(vm_offset_t);9394long physmem;9596/*97* System initialization98*/99static void vm_mem_init(void *);100SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL);101102#ifdef INVARIANTS103/*104* Ensure that pmap_init() correctly initialized pagesizes[].105*/106static void107vm_check_pagesizes(void)108{109int i;110111KASSERT(pagesizes[0] == PAGE_SIZE, ("pagesizes[0] != PAGE_SIZE"));112for (i = 1; i < MAXPAGESIZES; i++) {113KASSERT((pagesizes[i - 1] != 0 &&114pagesizes[i - 1] < pagesizes[i]) || pagesizes[i] == 0,115("pagesizes[%d ... %d] are misconfigured", i - 1, i));116}117}118#endif119120/*121* vm_mem_init() initializes the virtual memory system.122* This is done only by the first cpu up.123*/124static void125vm_mem_init(void *dummy)126{127128/*129* Initialize static domainsets, used by various allocators.130*/131domainset_init();132133/*134* Initialize resident memory structures. From here on, all physical135* memory is accounted for, and we use only virtual addresses.136*/137vm_set_page_size();138virtual_avail = vm_page_startup(virtual_avail);139140/*141* Set an initial domain policy for thread0 so that allocations142* can work.143*/144domainset_zero();145146/* Bootstrap the kernel memory allocator. */147uma_startup1(virtual_avail);148149/*150* Initialize other VM packages151*/152vmem_startup();153vm_object_init();154vm_map_startup();155kmem_init(virtual_avail, virtual_end);156157kmem_init_zero_region();158pmap_init();159vm_pager_init();160161#ifdef INVARIANTS162vm_check_pagesizes();163#endif164}165166void167vm_ksubmap_init(struct kva_md_info *kmi)168{169caddr_t firstaddr, v;170vm_size_t size = 0;171long physmem_est;172vm_offset_t minaddr;173vm_offset_t maxaddr;174175TSENTER();176/*177* Allocate space for system data structures.178* The first available kernel virtual address is in "v".179* As pages of kernel virtual memory are allocated, "v" is incremented.180* As pages of memory are allocated and cleared,181* "firstaddr" is incremented.182*/183184/*185* Make two passes. The first pass calculates how much memory is186* needed and allocates it. The second pass assigns virtual187* addresses to the various data structures.188*/189firstaddr = NULL;190again:191v = firstaddr;192193/*194* Discount the physical memory larger than the size of kernel_map195* to avoid eating up all of KVA space.196*/197physmem_est = lmin(physmem, btoc(vm_map_max(kernel_map) -198vm_map_min(kernel_map)));199200v = kern_vfs_bio_buffer_alloc(v, physmem_est);201202/*203* End of first pass, size has been calculated so allocate memory204*/205if (firstaddr == NULL) {206size = (vm_size_t)v;207#ifdef VM_FREELIST_DMA32208/*209* Try to protect 32-bit DMAable memory from the largest210* early alloc of wired mem.211*/212firstaddr = kmem_alloc_attr(size, M_ZERO | M_NOWAIT,213(vm_paddr_t)1 << 32, ~(vm_paddr_t)0, VM_MEMATTR_DEFAULT);214if (firstaddr == NULL)215#endif216firstaddr = kmem_malloc(size, M_ZERO | M_WAITOK);217if (firstaddr == NULL)218panic("startup: no room for tables");219goto again;220}221222/*223* End of second pass, addresses have been assigned224*/225if ((vm_size_t)(v - firstaddr) != size)226panic("startup: table size inconsistency");227228/*229* Allocate the clean map to hold all of I/O virtual memory.230*/231size = (long)nbuf * BKVASIZE + (long)bio_transient_maxcnt * maxphys;232kmi->clean_sva = kva_alloc(size);233kmi->clean_eva = kmi->clean_sva + size;234235/*236* Allocate the buffer arena.237*238* Enable the quantum cache if we have more than 4 cpus. This239* avoids lock contention at the expense of some fragmentation.240*/241size = (long)nbuf * BKVASIZE;242kmi->buffer_sva = kmi->clean_sva;243kmi->buffer_eva = kmi->buffer_sva + size;244vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size,245PAGE_SIZE, (mp_ncpus > 4) ? BKVASIZE * 8 : 0, M_WAITOK);246247/*248* And optionally transient bio space.249*/250if (bio_transient_maxcnt != 0) {251size = (long)bio_transient_maxcnt * maxphys;252vmem_init(transient_arena, "transient arena",253kmi->buffer_eva, size, PAGE_SIZE, 0, M_WAITOK);254}255256/*257* Allocate the pageable submaps. We may cache an exec map entry per258* CPU, so we therefore need to reserve space for at least ncpu+1259* entries to avoid deadlock. The exec map is also used by some image260* activators, so we leave a fixed number of pages for their use.261*/262#ifdef __LP64__263exec_map_entries = 8 * mp_ncpus;264#else265exec_map_entries = 2 * mp_ncpus + 4;266#endif267exec_map_entry_size = round_page(PATH_MAX + ARG_MAX);268kmem_subinit(exec_map, kernel_map, &minaddr, &maxaddr,269exec_map_entries * exec_map_entry_size + 64 * PAGE_SIZE, false);270kmem_subinit(pipe_map, kernel_map, &minaddr, &maxaddr, maxpipekva,271false);272TSEXIT();273}274275276