xref: /freebsd/sys/vm/vm_unix.c (revision 5c2bc3db201a4fe8d7911cf816bea104d5dc2138)
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/param.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/racct.h>
48 #include <sys/resourcevar.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysent.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
54 #include <machine/md_var.h>
55 #endif
56 
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61 
62 #ifndef _SYS_SYSPROTO_H_
63 struct break_args {
64 	char *nsize;
65 };
66 #endif
67 int
68 sys_break(struct thread *td, struct break_args *uap)
69 {
70 #if !defined(__aarch64__) && !defined(__riscv)
71 	uintptr_t addr;
72 	int error;
73 
74 	addr = (uintptr_t)uap->nsize;
75 	error = kern_break(td, &addr);
76 	if (error == 0)
77 		td->td_retval[0] = addr;
78 	return (error);
79 #else /* defined(__aarch64__) || defined(__riscv) */
80 	return (ENOSYS);
81 #endif /* defined(__aarch64__) || defined(__riscv) */
82 }
83 
84 int
85 kern_break(struct thread *td, uintptr_t *addr)
86 {
87 	struct vmspace *vm = td->td_proc->p_vmspace;
88 	vm_map_t map = &vm->vm_map;
89 	vm_offset_t new, old, base;
90 	rlim_t datalim, lmemlim, vmemlim;
91 	int prot, rv;
92 	int error = 0;
93 
94 	datalim = lim_cur(td, RLIMIT_DATA);
95 	lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
96 	vmemlim = lim_cur(td, RLIMIT_VMEM);
97 
98 	new = round_page(*addr);
99 	vm_map_lock(map);
100 
101 	base = round_page((vm_offset_t) vm->vm_daddr);
102 	old = base + ctob(vm->vm_dsize);
103 	if (new > base) {
104 		/*
105 		 * Check the resource limit, but allow a process to reduce
106 		 * its usage, even if it remains over the limit.
107 		 */
108 		if (new - base > datalim && new > old) {
109 			error = ENOMEM;
110 			goto done;
111 		}
112 		if (new > vm_map_max(map)) {
113 			error = ENOMEM;
114 			goto done;
115 		}
116 	} else if (new < base) {
117 		/*
118 		 * Simply return the current break address without
119 		 * modifying any state.  This is an ad-hoc interface
120 		 * used by libc to determine the initial break address,
121 		 * avoiding a dependency on magic features in the system
122 		 * linker.
123 		 */
124 		new = old;
125 		goto done;
126 	}
127 
128 	if (new > old) {
129 		if (!old_mlock && map->flags & MAP_WIREFUTURE) {
130 			if (ptoa(pmap_wired_count(map->pmap)) +
131 			    (new - old) > lmemlim) {
132 				error = ENOMEM;
133 				goto done;
134 			}
135 		}
136 		if (map->size + (new - old) > vmemlim) {
137 			error = ENOMEM;
138 			goto done;
139 		}
140 #ifdef RACCT
141 		if (racct_enable) {
142 			PROC_LOCK(td->td_proc);
143 			error = racct_set(td->td_proc, RACCT_DATA, new - base);
144 			if (error != 0) {
145 				PROC_UNLOCK(td->td_proc);
146 				error = ENOMEM;
147 				goto done;
148 			}
149 			error = racct_set(td->td_proc, RACCT_VMEM,
150 			    map->size + (new - old));
151 			if (error != 0) {
152 				racct_set_force(td->td_proc, RACCT_DATA,
153 				    old - base);
154 				PROC_UNLOCK(td->td_proc);
155 				error = ENOMEM;
156 				goto done;
157 			}
158 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
159 				error = racct_set(td->td_proc, RACCT_MEMLOCK,
160 				    ptoa(pmap_wired_count(map->pmap)) +
161 				    (new - old));
162 				if (error != 0) {
163 					racct_set_force(td->td_proc, RACCT_DATA,
164 					    old - base);
165 					racct_set_force(td->td_proc, RACCT_VMEM,
166 					    map->size);
167 					PROC_UNLOCK(td->td_proc);
168 					error = ENOMEM;
169 					goto done;
170 				}
171 			}
172 			PROC_UNLOCK(td->td_proc);
173 		}
174 #endif
175 		prot = VM_PROT_RW;
176 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
177 		if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
178 			prot |= VM_PROT_EXECUTE;
179 #endif
180 		rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL,
181 		    0);
182 		if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
183 			rv = vm_map_wire_locked(map, old, new,
184 			    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
185 			if (rv != KERN_SUCCESS)
186 				(void)vm_map_delete(map, old, new);
187 		}
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 	} else if (new < old) {
209 		rv = vm_map_delete(map, new, old);
210 		if (rv != KERN_SUCCESS) {
211 			error = ENOMEM;
212 			goto done;
213 		}
214 		vm->vm_dsize -= btoc(old - new);
215 #ifdef RACCT
216 		if (racct_enable) {
217 			PROC_LOCK(td->td_proc);
218 			racct_set_force(td->td_proc, RACCT_DATA, new - base);
219 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
220 			if (!old_mlock && map->flags & MAP_WIREFUTURE) {
221 				racct_set_force(td->td_proc, RACCT_MEMLOCK,
222 				    ptoa(pmap_wired_count(map->pmap)));
223 			}
224 			PROC_UNLOCK(td->td_proc);
225 		}
226 #endif
227 	}
228 done:
229 	vm_map_unlock(map);
230 
231 	if (error == 0)
232 		*addr = new;
233 
234 	return (error);
235 }
236 
237 #ifdef COMPAT_FREEBSD11
238 int
239 freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)
240 {
241 
242 	return (EINVAL);
243 }
244 #endif
245