/*-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/*162* Now we can properly handle calls into vm_fault() from163* kernel page faults during initialization, typically to164* panic. Clear the nofaulting flag set for thread0 in the165* image, see kern/init_main.c166*/167curthread->td_pflags &= ~TDP_NOFAULTING;168169#ifdef INVARIANTS170vm_check_pagesizes();171#endif172}173174void175vm_ksubmap_init(struct kva_md_info *kmi)176{177caddr_t firstaddr, v;178vm_size_t size = 0;179long physmem_est;180vm_offset_t minaddr;181vm_offset_t maxaddr;182183TSENTER();184/*185* Allocate space for system data structures.186* The first available kernel virtual address is in "v".187* As pages of kernel virtual memory are allocated, "v" is incremented.188* As pages of memory are allocated and cleared,189* "firstaddr" is incremented.190*/191192/*193* Make two passes. The first pass calculates how much memory is194* needed and allocates it. The second pass assigns virtual195* addresses to the various data structures.196*/197firstaddr = NULL;198again:199v = firstaddr;200201/*202* Discount the physical memory larger than the size of kernel_map203* to avoid eating up all of KVA space.204*/205physmem_est = lmin(physmem, btoc(vm_map_max(kernel_map) -206vm_map_min(kernel_map)));207208v = kern_vfs_bio_buffer_alloc(v, physmem_est);209210/*211* End of first pass, size has been calculated so allocate memory212*/213if (firstaddr == NULL) {214size = (vm_size_t)v;215#ifdef VM_FREELIST_DMA32216/*217* Try to protect 32-bit DMAable memory from the largest218* early alloc of wired mem.219*/220firstaddr = kmem_alloc_attr(size, M_ZERO | M_NOWAIT,221(vm_paddr_t)1 << 32, ~(vm_paddr_t)0, VM_MEMATTR_DEFAULT);222if (firstaddr == NULL)223#endif224firstaddr = kmem_malloc(size, M_ZERO | M_WAITOK);225if (firstaddr == NULL)226panic("startup: no room for tables");227goto again;228}229230/*231* End of second pass, addresses have been assigned232*/233if ((vm_size_t)(v - firstaddr) != size)234panic("startup: table size inconsistency");235236/*237* Allocate the clean map to hold all of I/O virtual memory.238*/239size = (long)nbuf * BKVASIZE + (long)bio_transient_maxcnt * maxphys;240kmi->clean_sva = kva_alloc(size);241kmi->clean_eva = kmi->clean_sva + size;242243/*244* Allocate the buffer arena.245*246* Enable the quantum cache if we have more than 4 cpus. This247* avoids lock contention at the expense of some fragmentation.248*/249size = (long)nbuf * BKVASIZE;250kmi->buffer_sva = kmi->clean_sva;251kmi->buffer_eva = kmi->buffer_sva + size;252vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size,253PAGE_SIZE, (mp_ncpus > 4) ? BKVASIZE * 8 : 0, M_WAITOK);254255/*256* And optionally transient bio space.257*/258if (bio_transient_maxcnt != 0) {259size = (long)bio_transient_maxcnt * maxphys;260vmem_init(transient_arena, "transient arena",261kmi->buffer_eva, size, PAGE_SIZE, 0, M_WAITOK);262}263264/*265* Allocate the pageable submaps. We may cache an exec map entry per266* CPU, so we therefore need to reserve space for at least ncpu+1267* entries to avoid deadlock. The exec map is also used by some image268* activators, so we leave a fixed number of pages for their use.269*/270#ifdef __LP64__271exec_map_entries = 8 * mp_ncpus;272#else273exec_map_entries = 2 * mp_ncpus + 4;274#endif275exec_map_entry_size = round_page(PATH_MAX + ARG_MAX);276kmem_subinit(exec_map, kernel_map, &minaddr, &maxaddr,277exec_map_entries * exec_map_entry_size + 64 * PAGE_SIZE, false);278kmem_subinit(pipe_map, kernel_map, &minaddr, &maxaddr, maxpipekva,279false);280TSEXIT();281}282283284