1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 33 * 34 * 35 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Permission to use, copy, modify and distribute this software and 39 * its documentation is hereby granted, provided that both the copyright 40 * notice and this permission notice appear in all copies of the 41 * software, derivative works or modified versions, and any portions 42 * thereof, and that both notices appear in supporting documentation. 43 * 44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 46 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 47 * 48 * Carnegie Mellon requests users of this software to return to 49 * 50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 51 * School of Computer Science 52 * Carnegie Mellon University 53 * Pittsburgh PA 15213-3890 54 * 55 * any improvements or extensions that they make and grant Carnegie the 56 * rights to redistribute these changes. 57 * 58 * $FreeBSD: src/sys/vm/vm_glue.c,v 1.94.2.4 2003/01/13 22:51:17 dillon Exp $ 59 */ 60 61 #include "opt_vm.h" 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/proc.h> 66 #include <sys/resourcevar.h> 67 #include <sys/buf.h> 68 #include <sys/shm.h> 69 #include <sys/vmmeter.h> 70 #include <sys/sysctl.h> 71 72 #include <sys/kernel.h> 73 #include <sys/unistd.h> 74 75 #include <machine/limits.h> 76 #include <machine/vmm.h> 77 78 #include <vm/vm.h> 79 #include <vm/vm_param.h> 80 #include <sys/lock.h> 81 #include <vm/pmap.h> 82 #include <vm/vm_map.h> 83 #include <vm/vm_page.h> 84 #include <vm/vm_page2.h> 85 #include <vm/vm_pageout.h> 86 #include <vm/vm_kern.h> 87 #include <vm/vm_extern.h> 88 89 /* 90 * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 91 * 92 * Process 0 falls into this function, just loop on nothing. 93 */ 94 95 static void scheduler(void *); 96 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL); 97 98 #ifdef INVARIANTS 99 100 static int swap_debug = 0; 101 SYSCTL_INT(_vm, OID_AUTO, swap_debug, CTLFLAG_RW, &swap_debug, 0, ""); 102 103 #endif 104 105 /* 106 * No requirements. 107 */ 108 int 109 kernacc(c_caddr_t addr, int len, int rw) 110 { 111 boolean_t rv; 112 vm_offset_t saddr, eaddr; 113 vm_prot_t prot; 114 115 KASSERT((rw & (~VM_PROT_ALL)) == 0, 116 ("illegal ``rw'' argument to kernacc (%x)", rw)); 117 118 /* 119 * The globaldata space is not part of the kernel_map proper, 120 * check access separately. 121 */ 122 if (is_globaldata_space((vm_offset_t)addr, (vm_offset_t)(addr + len))) 123 return (TRUE); 124 125 /* 126 * Nominal kernel memory access - check access via kernel_map. 127 */ 128 if ((vm_offset_t)addr + len > vm_map_max(kernel_map) || 129 (vm_offset_t)addr + len < (vm_offset_t)addr) { 130 return (FALSE); 131 } 132 prot = rw; 133 saddr = trunc_page((vm_offset_t)addr); 134 eaddr = round_page((vm_offset_t)addr + len); 135 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot, FALSE); 136 137 return (rv == TRUE); 138 } 139 140 /* 141 * No requirements. 142 */ 143 int 144 useracc(c_caddr_t addr, int len, int rw) 145 { 146 boolean_t rv; 147 vm_prot_t prot; 148 vm_map_t map; 149 vm_offset_t wrap; 150 vm_offset_t gpa; 151 152 KASSERT((rw & (~VM_PROT_ALL)) == 0, 153 ("illegal ``rw'' argument to useracc (%x)", rw)); 154 prot = rw; 155 156 if (curthread->td_vmm) { 157 if (vmm_vm_get_gpa(curproc, (register_t *)&gpa, (register_t) addr)) 158 panic("%s: could not get GPA\n", __func__); 159 addr = (c_caddr_t) gpa; 160 } 161 162 /* 163 * XXX - check separately to disallow access to user area and user 164 * page tables - they are in the map. 165 */ 166 wrap = (vm_offset_t)addr + len; 167 if (wrap > VM_MAX_USER_ADDRESS || wrap < (vm_offset_t)addr) { 168 return (FALSE); 169 } 170 map = &curproc->p_vmspace->vm_map; 171 vm_map_lock_read(map); 172 173 rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr), 174 round_page(wrap), prot, TRUE); 175 vm_map_unlock_read(map); 176 177 return (rv == TRUE); 178 } 179 180 /* 181 * No requirements. 182 */ 183 void 184 vslock(caddr_t addr, u_int len) 185 { 186 if (len) { 187 /* wire the pages */ 188 vm_map_kernel_wiring(&curproc->p_vmspace->vm_map, 189 trunc_page((vm_offset_t)addr), 190 round_page((vm_offset_t)addr + len), 191 0); 192 } 193 } 194 195 /* 196 * No requirements. 197 */ 198 void 199 vsunlock(caddr_t addr, u_int len) 200 { 201 if (len) { 202 /* unwire the pages */ 203 vm_map_kernel_wiring(&curproc->p_vmspace->vm_map, 204 trunc_page((vm_offset_t)addr), 205 round_page((vm_offset_t)addr + len), 206 KM_PAGEABLE); 207 } 208 } 209 210 /* 211 * Implement fork's actions on an address space. Here we arrange for the 212 * address space to be copied or referenced, allocate a user struct (pcb 213 * and kernel stack), then call the machine-dependent layer to fill those 214 * in and make the new process ready to run. The new process is set up 215 * so that it returns directly to user mode to avoid stack copying and 216 * relocation problems. 217 * 218 * If p2 is NULL and RFPROC is 0 we are just divorcing parts of the process 219 * from itself. 220 * 221 * Otherwise if p2 is NULL the new vmspace is not to be associated with any 222 * process or thread (so things like /dev/upmap and /dev/lpmap are not 223 * retained). 224 * 225 * Otherwise if p2 is not NULL then process specific mappings will be forked. 226 * If lp2 is not NULL only the thread-specific mappings for lp2 are forked, 227 * otherwise no thread-specific mappings are forked. 228 * 229 * No requirements. 230 */ 231 void 232 vm_fork(struct proc *p1, struct proc *p2, struct lwp *lp2, int flags) 233 { 234 if ((flags & RFPROC) == 0) { 235 /* 236 * Divorce the memory, if it is shared, essentially 237 * this changes shared memory amongst threads, into 238 * COW locally. 239 */ 240 if ((flags & RFMEM) == 0) { 241 if (vmspace_getrefs(p1->p_vmspace) > 1) { 242 vmspace_unshare(p1); 243 } 244 } 245 cpu_fork(ONLY_LWP_IN_PROC(p1), NULL, flags); 246 return; 247 } 248 249 if (flags & RFMEM) { 250 vmspace_ref(p1->p_vmspace); 251 p2->p_vmspace = p1->p_vmspace; 252 } 253 254 while (vm_paging_severe()) { 255 vm_wait(0); 256 } 257 258 if ((flags & RFMEM) == 0) { 259 p2->p_vmspace = vmspace_fork(p1->p_vmspace, p2, lp2); 260 261 pmap_pinit2(vmspace_pmap(p2->p_vmspace)); 262 263 if (p1->p_vmspace->vm_shm) 264 shmfork(p1, p2); 265 } 266 267 pmap_init_proc(p2); 268 } 269 270 /* 271 * Set default limits for VM system. Call during proc0's initialization. 272 * 273 * Called from the low level boot code only. 274 */ 275 void 276 vm_init_limits(struct proc *p) 277 { 278 int rss_limit; 279 280 /* 281 * Set up the initial limits on process VM. Set the maximum resident 282 * set size to be half of (reasonably) available memory. Since this 283 * is a soft limit, it comes into effect only when the system is out 284 * of memory - half of main memory helps to favor smaller processes, 285 * and reduces thrashing of the object cache. 286 */ 287 p->p_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 288 p->p_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 289 p->p_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 290 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 291 /* limit the limit to no less than 2MB */ 292 rss_limit = max(vmstats.v_free_count, 512); 293 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 294 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 295 } 296 297 /* 298 * process 0 winds up here after all kernel initialization sysinits have 299 * run. 300 */ 301 static void 302 scheduler(void *dummy) 303 { 304 for (;;) 305 tsleep(&proc0, 0, "idle", 0); 306 } 307