1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$ 37 */ 38 39 /* 40 * Traditional sbrk/grow interface to VM 41 */ 42 43 #include <sys/cdefs.h> 44 #include <sys/param.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/racct.h> 49 #include <sys/resourcevar.h> 50 #include <sys/syscallsubr.h> 51 #include <sys/sysent.h> 52 #include <sys/sysproto.h> 53 #include <sys/systm.h> 54 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */ 55 #include <machine/md_var.h> 56 #endif 57 58 #include <vm/vm.h> 59 #include <vm/vm_param.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_map.h> 62 63 #ifndef _SYS_SYSPROTO_H_ 64 struct break_args { 65 char *nsize; 66 }; 67 #endif 68 int 69 sys_break(struct thread *td, struct break_args *uap) 70 { 71 #if !defined(__aarch64__) && !defined(__riscv) 72 uintptr_t addr; 73 int error; 74 75 addr = (uintptr_t)uap->nsize; 76 error = kern_break(td, &addr); 77 if (error == 0) 78 td->td_retval[0] = addr; 79 return (error); 80 #else /* defined(__aarch64__) || defined(__riscv) */ 81 return (ENOSYS); 82 #endif /* defined(__aarch64__) || defined(__riscv) */ 83 } 84 85 int 86 kern_break(struct thread *td, uintptr_t *addr) 87 { 88 struct vmspace *vm = td->td_proc->p_vmspace; 89 vm_map_t map = &vm->vm_map; 90 vm_offset_t new, old, base; 91 rlim_t datalim, lmemlim, vmemlim; 92 int prot, rv; 93 int error = 0; 94 95 datalim = lim_cur(td, RLIMIT_DATA); 96 lmemlim = lim_cur(td, RLIMIT_MEMLOCK); 97 vmemlim = lim_cur(td, RLIMIT_VMEM); 98 99 new = round_page(*addr); 100 vm_map_lock(map); 101 102 base = round_page((vm_offset_t) vm->vm_daddr); 103 old = base + ctob(vm->vm_dsize); 104 if (new > base) { 105 /* 106 * Check the resource limit, but allow a process to reduce 107 * its usage, even if it remains over the limit. 108 */ 109 if (new - base > datalim && new > old) { 110 error = ENOMEM; 111 goto done; 112 } 113 if (new > vm_map_max(map)) { 114 error = ENOMEM; 115 goto done; 116 } 117 } else if (new < base) { 118 /* 119 * Simply return the current break address without 120 * modifying any state. This is an ad-hoc interface 121 * used by libc to determine the initial break address, 122 * avoiding a dependency on magic features in the system 123 * linker. 124 */ 125 new = old; 126 goto done; 127 } 128 129 if (new > old) { 130 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 131 if (ptoa(pmap_wired_count(map->pmap)) + 132 (new - old) > lmemlim) { 133 error = ENOMEM; 134 goto done; 135 } 136 } 137 if (map->size + (new - old) > vmemlim) { 138 error = ENOMEM; 139 goto done; 140 } 141 #ifdef RACCT 142 if (racct_enable) { 143 PROC_LOCK(td->td_proc); 144 error = racct_set(td->td_proc, RACCT_DATA, new - base); 145 if (error != 0) { 146 PROC_UNLOCK(td->td_proc); 147 error = ENOMEM; 148 goto done; 149 } 150 error = racct_set(td->td_proc, RACCT_VMEM, 151 map->size + (new - old)); 152 if (error != 0) { 153 racct_set_force(td->td_proc, RACCT_DATA, 154 old - base); 155 PROC_UNLOCK(td->td_proc); 156 error = ENOMEM; 157 goto done; 158 } 159 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 160 error = racct_set(td->td_proc, RACCT_MEMLOCK, 161 ptoa(pmap_wired_count(map->pmap)) + 162 (new - old)); 163 if (error != 0) { 164 racct_set_force(td->td_proc, RACCT_DATA, 165 old - base); 166 racct_set_force(td->td_proc, RACCT_VMEM, 167 map->size); 168 PROC_UNLOCK(td->td_proc); 169 error = ENOMEM; 170 goto done; 171 } 172 } 173 PROC_UNLOCK(td->td_proc); 174 } 175 #endif 176 prot = VM_PROT_RW; 177 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__) 178 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32)) 179 prot |= VM_PROT_EXECUTE; 180 #endif 181 rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 182 0); 183 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) { 184 rv = vm_map_wire_locked(map, old, new, 185 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 186 if (rv != KERN_SUCCESS) 187 (void)vm_map_delete(map, old, new); 188 } 189 if (rv != KERN_SUCCESS) { 190 #ifdef RACCT 191 if (racct_enable) { 192 PROC_LOCK(td->td_proc); 193 racct_set_force(td->td_proc, 194 RACCT_DATA, old - base); 195 racct_set_force(td->td_proc, 196 RACCT_VMEM, map->size); 197 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 198 racct_set_force(td->td_proc, 199 RACCT_MEMLOCK, 200 ptoa(pmap_wired_count(map->pmap))); 201 } 202 PROC_UNLOCK(td->td_proc); 203 } 204 #endif 205 error = ENOMEM; 206 goto done; 207 } 208 vm->vm_dsize += btoc(new - old); 209 } else if (new < old) { 210 rv = vm_map_delete(map, new, old); 211 if (rv != KERN_SUCCESS) { 212 error = ENOMEM; 213 goto done; 214 } 215 vm->vm_dsize -= btoc(old - new); 216 #ifdef RACCT 217 if (racct_enable) { 218 PROC_LOCK(td->td_proc); 219 racct_set_force(td->td_proc, RACCT_DATA, new - base); 220 racct_set_force(td->td_proc, RACCT_VMEM, map->size); 221 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 222 racct_set_force(td->td_proc, RACCT_MEMLOCK, 223 ptoa(pmap_wired_count(map->pmap))); 224 } 225 PROC_UNLOCK(td->td_proc); 226 } 227 #endif 228 } 229 done: 230 vm_map_unlock(map); 231 232 if (error == 0) 233 *addr = new; 234 235 return (error); 236 } 237 238 #ifdef COMPAT_FREEBSD11 239 int 240 freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap) 241 { 242 243 return (EINVAL); 244 } 245 #endif 246