xref: /freebsd/sys/vm/vm_unix.c (revision 0957b409)
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  *	@(#)vm_unix.c	8.1 (Berkeley) 6/11/93
39  */
40 
41 /*
42  * Traditional sbrk/grow interface to VM
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.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>
60 #endif
61 
62 #include <vm/vm.h>
63 #include <vm/vm_param.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66 
67 #ifndef _SYS_SYSPROTO_H_
68 struct break_args {
69 	char *nsize;
70 };
71 #endif
72 int
73 sys_break(struct thread *td, struct break_args *uap)
74 {
75 #if !defined(__aarch64__) && !defined(__riscv)
76 	uintptr_t addr;
77 	int error;
78 
79 	addr = (uintptr_t)uap->nsize;
80 	error = kern_break(td, &addr);
81 	if (error == 0)
82 		td->td_retval[0] = addr;
83 	return (error);
84 #else /* defined(__aarch64__) || defined(__riscv) */
85 	return (ENOSYS);
86 #endif /* defined(__aarch64__) || defined(__riscv) */
87 }
88 
89 int
90 kern_break(struct thread *td, uintptr_t *addr)
91 {
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;
96 	int prot, rv;
97 	int error = 0;
98 	boolean_t do_map_wirefuture;
99 
100 	datalim = lim_cur(td, RLIMIT_DATA);
101 	lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
102 	vmemlim = lim_cur(td, RLIMIT_VMEM);
103 
104 	do_map_wirefuture = FALSE;
105 	new = round_page(*addr);
106 	vm_map_lock(map);
107 
108 	base = round_page((vm_offset_t) vm->vm_daddr);
109 	old = base + ctob(vm->vm_dsize);
110 	if (new > base) {
111 		/*
112 		 * Check the resource limit, but allow a process to reduce
113 		 * its usage, even if it remains over the limit.
114 		 */
115 		if (new - base > datalim && new > old) {
116 			error = ENOMEM;
117 			goto done;
118 		}
119 		if (new > vm_map_max(map)) {
120 			error = ENOMEM;
121 			goto done;
122 		}
123 	} else if (new < base) {
124 		/*
125 		 * Simply return the current break address without
126 		 * modifying any state.  This is an ad-hoc interface
127 		 * used by libc to determine the initial break address,
128 		 * avoiding a dependency on magic features in the system
129 		 * linker.
130 		 */
131 		new = old;
132 		goto done;
133 	}
134 
135 	if (new > old) {
136 		if (!old_mlock && map->flags & MAP_WIREFUTURE) {
137 			if (ptoa(pmap_wired_count(map->pmap)) +
138 			    (new - old) > lmemlim) {
139 				error = ENOMEM;
140 				goto done;
141 			}
142 		}
143 		if (map->size + (new - old) > vmemlim) {
144 			error = ENOMEM;
145 			goto done;
146 		}
147 #ifdef RACCT
148 		if (racct_enable) {
149 			PROC_LOCK(td->td_proc);
150 			error = racct_set(td->td_proc, RACCT_DATA, new - base);
151 			if (error != 0) {
152 				PROC_UNLOCK(td->td_proc);
153 				error = ENOMEM;
154 				goto done;
155 			}
156 			error = racct_set(td->td_proc, RACCT_VMEM,
157 			    map->size + (new - old));
158 			if (error != 0) {
159 				racct_set_force(td->td_proc, RACCT_DATA,
160 				    old - base);
161 				PROC_UNLOCK(td->td_proc);
162 				error = ENOMEM;
163 				goto done;
164 			}
165 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
166 				error = racct_set(td->td_proc, RACCT_MEMLOCK,
167 				    ptoa(pmap_wired_count(map->pmap)) +
168 				    (new - old));
169 				if (error != 0) {
170 					racct_set_force(td->td_proc, RACCT_DATA,
171 					    old - base);
172 					racct_set_force(td->td_proc, RACCT_VMEM,
173 					    map->size);
174 					PROC_UNLOCK(td->td_proc);
175 					error = ENOMEM;
176 					goto done;
177 				}
178 			}
179 			PROC_UNLOCK(td->td_proc);
180 		}
181 #endif
182 		prot = VM_PROT_RW;
183 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
184 		if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
185 			prot |= VM_PROT_EXECUTE;
186 #endif
187 		rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL, 0);
188 		if (rv != KERN_SUCCESS) {
189 #ifdef RACCT
190 			if (racct_enable) {
191 				PROC_LOCK(td->td_proc);
192 				racct_set_force(td->td_proc,
193 				    RACCT_DATA, old - base);
194 				racct_set_force(td->td_proc,
195 				    RACCT_VMEM, map->size);
196 				if (!old_mlock && map->flags & MAP_WIREFUTURE) {
197 					racct_set_force(td->td_proc,
198 					    RACCT_MEMLOCK,
199 					    ptoa(pmap_wired_count(map->pmap)));
200 				}
201 				PROC_UNLOCK(td->td_proc);
202 			}
203 #endif
204 			error = ENOMEM;
205 			goto done;
206 		}
207 		vm->vm_dsize += btoc(new - old);
208 		/*
209 		 * Handle the MAP_WIREFUTURE case for legacy applications,
210 		 * by marking the newly mapped range of pages as wired.
211 		 * We are not required to perform a corresponding
212 		 * vm_map_unwire() before vm_map_delete() below, as
213 		 * it will forcibly unwire the pages in the range.
214 		 *
215 		 * XXX If the pages cannot be wired, no error is returned.
216 		 */
217 		if ((map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
218 			do_map_wirefuture = TRUE;
219 	} else if (new < old) {
220 		rv = vm_map_delete(map, new, old);
221 		if (rv != KERN_SUCCESS) {
222 			error = ENOMEM;
223 			goto done;
224 		}
225 		vm->vm_dsize -= btoc(old - new);
226 #ifdef RACCT
227 		if (racct_enable) {
228 			PROC_LOCK(td->td_proc);
229 			racct_set_force(td->td_proc, RACCT_DATA, new - base);
230 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
231 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
232 				racct_set_force(td->td_proc, RACCT_MEMLOCK,
233 				    ptoa(pmap_wired_count(map->pmap)));
234 			}
235 			PROC_UNLOCK(td->td_proc);
236 		}
237 #endif
238 	}
239 done:
240 	vm_map_unlock(map);
241 
242 	if (do_map_wirefuture)
243 		(void) vm_map_wire(map, old, new,
244 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
245 
246 	if (error == 0)
247 		*addr = new;
248 
249 	return (error);
250 }
251 
252 #ifdef COMPAT_FREEBSD11
253 int
254 freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)
255 {
256 
257 	return (EINVAL);
258 }
259 #endif
260