2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
38 * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93
42 * Traditional sbrk/grow interface to VM
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
50 #include <sys/mutex.h>
52 #include <sys/racct.h>
53 #include <sys/resourcevar.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
57 #include <sys/systm.h>
58 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
59 #include <machine/md_var.h>
63 #include <vm/vm_param.h>
65 #include <vm/vm_map.h>
67 #ifndef _SYS_SYSPROTO_H_
73 sys_break(struct thread *td, struct break_args *uap)
75 #if !defined(__aarch64__) && !defined(__riscv)
79 addr = (uintptr_t)uap->nsize;
80 error = kern_break(td, &addr);
82 td->td_retval[0] = addr;
84 #else /* defined(__aarch64__) || defined(__riscv) */
86 #endif /* defined(__aarch64__) || defined(__riscv) */
90 kern_break(struct thread *td, uintptr_t *addr)
92 struct vmspace *vm = td->td_proc->p_vmspace;
93 vm_map_t map = &vm->vm_map;
94 vm_offset_t new, old, base;
95 rlim_t datalim, lmemlim, vmemlim;
99 datalim = lim_cur(td, RLIMIT_DATA);
100 lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
101 vmemlim = lim_cur(td, RLIMIT_VMEM);
103 new = round_page(*addr);
106 base = round_page((vm_offset_t) vm->vm_daddr);
107 old = base + ctob(vm->vm_dsize);
110 * Check the resource limit, but allow a process to reduce
111 * its usage, even if it remains over the limit.
113 if (new - base > datalim && new > old) {
117 if (new > vm_map_max(map)) {
121 } else if (new < base) {
123 * Simply return the current break address without
124 * modifying any state. This is an ad-hoc interface
125 * used by libc to determine the initial break address,
126 * avoiding a dependency on magic features in the system
134 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
135 if (ptoa(pmap_wired_count(map->pmap)) +
136 (new - old) > lmemlim) {
141 if (map->size + (new - old) > vmemlim) {
147 PROC_LOCK(td->td_proc);
148 error = racct_set(td->td_proc, RACCT_DATA, new - base);
150 PROC_UNLOCK(td->td_proc);
154 error = racct_set(td->td_proc, RACCT_VMEM,
155 map->size + (new - old));
157 racct_set_force(td->td_proc, RACCT_DATA,
159 PROC_UNLOCK(td->td_proc);
163 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
164 error = racct_set(td->td_proc, RACCT_MEMLOCK,
165 ptoa(pmap_wired_count(map->pmap)) +
168 racct_set_force(td->td_proc, RACCT_DATA,
170 racct_set_force(td->td_proc, RACCT_VMEM,
172 PROC_UNLOCK(td->td_proc);
177 PROC_UNLOCK(td->td_proc);
181 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
182 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
183 prot |= VM_PROT_EXECUTE;
185 rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL,
187 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
188 rv = vm_map_wire_locked(map, old, new,
189 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
190 if (rv != KERN_SUCCESS)
191 (void)vm_map_delete(map, old, new);
193 if (rv != KERN_SUCCESS) {
196 PROC_LOCK(td->td_proc);
197 racct_set_force(td->td_proc,
198 RACCT_DATA, old - base);
199 racct_set_force(td->td_proc,
200 RACCT_VMEM, map->size);
201 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
202 racct_set_force(td->td_proc,
204 ptoa(pmap_wired_count(map->pmap)));
206 PROC_UNLOCK(td->td_proc);
212 vm->vm_dsize += btoc(new - old);
213 } else if (new < old) {
214 rv = vm_map_delete(map, new, old);
215 if (rv != KERN_SUCCESS) {
219 vm->vm_dsize -= btoc(old - new);
222 PROC_LOCK(td->td_proc);
223 racct_set_force(td->td_proc, RACCT_DATA, new - base);
224 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
225 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
226 racct_set_force(td->td_proc, RACCT_MEMLOCK,
227 ptoa(pmap_wired_count(map->pmap)));
229 PROC_UNLOCK(td->td_proc);
242 #ifdef COMPAT_FREEBSD11
244 freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)