xref: /openbsd-src/sys/uvm/uvm_unix.c (revision 1e8cdc2e593c4e9ec7f4bdac2fbf48a4ad29f5b8)
1 /*	$OpenBSD: uvm_unix.c,v 1.51 2014/11/16 12:31:01 deraadt Exp $	*/
2 /*	$NetBSD: uvm_unix.c,v 1.18 2000/09/13 15:00:25 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1991, 1993 The Regents of the University of California.
7  * Copyright (c) 1988 University of Utah.
8  *
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * the Systems Programming Group of the University of Utah Computer
13  * Science Department.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
40  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
41  * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
42  */
43 
44 /*
45  * uvm_unix.c: traditional sbrk/grow interface to vm.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <sys/core.h>
54 
55 #include <sys/mount.h>
56 #include <sys/syscallargs.h>
57 
58 #include <uvm/uvm.h>
59 
60 /*
61  * sys_obreak: set break
62  */
63 
64 int
65 sys_obreak(struct proc *p, void *v, register_t *retval)
66 {
67 	struct sys_obreak_args /* {
68 		syscallarg(char *) nsize;
69 	} */ *uap = v;
70 	struct vmspace *vm = p->p_vmspace;
71 	vaddr_t new, old, base;
72 	int error;
73 
74 	base = (vaddr_t)vm->vm_daddr;
75 	new = round_page((vaddr_t)SCARG(uap, nsize));
76 	if (new < base || (new - base) > p->p_rlimit[RLIMIT_DATA].rlim_cur)
77 		return (ENOMEM);
78 
79 	old = round_page(base + ptoa(vm->vm_dsize));
80 
81 	if (new == old)
82 		return (0);
83 
84 	/* grow or shrink? */
85 	if (new > old) {
86 		error = uvm_map(&vm->vm_map, &old, new - old, NULL,
87 		    UVM_UNKNOWN_OFFSET, 0,
88 		    UVM_MAPFLAG(PROT_READ | PROT_WRITE, PROT_MASK, UVM_INH_COPY,
89 		    POSIX_MADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
90 		    UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
91 		if (error) {
92 			uprintf("sbrk: grow %ld failed, error = %d\n",
93 			    new - old, error);
94 			return (ENOMEM);
95 		}
96 		vm->vm_dsize += atop(new - old);
97 	} else {
98 		uvm_deallocate(&vm->vm_map, new, old - new);
99 		vm->vm_dsize -= atop(old - new);
100 	}
101 
102 	return (0);
103 }
104 
105 /*
106  * uvm_grow: enlarge the "stack segment" to include sp.
107  */
108 void
109 uvm_grow(struct proc *p, vaddr_t sp)
110 {
111 	struct vmspace *vm = p->p_vmspace;
112 	int si;
113 
114 	/* For user defined stacks (from sendsig). */
115 	if (sp < (vaddr_t)vm->vm_maxsaddr)
116 		return;
117 
118 	/* For common case of already allocated (from trap). */
119 #ifdef MACHINE_STACK_GROWS_UP
120 	if (sp < USRSTACK + ptoa(vm->vm_ssize))
121 #else
122 	if (sp >= USRSTACK - ptoa(vm->vm_ssize))
123 #endif
124 		return;
125 
126 	/* Really need to check vs limit and increment stack size if ok. */
127 #ifdef MACHINE_STACK_GROWS_UP
128 	si = atop(sp - USRSTACK) - vm->vm_ssize + 1;
129 #else
130 	si = atop(USRSTACK - sp) - vm->vm_ssize;
131 #endif
132 	if (vm->vm_ssize + si <= atop(p->p_rlimit[RLIMIT_STACK].rlim_cur))
133 		vm->vm_ssize += si;
134 }
135 
136 #ifndef SMALL_KERNEL
137 
138 /*
139  * uvm_coredump: dump core!
140  */
141 
142 int
143 uvm_coredump(struct proc *p, struct vnode *vp, struct ucred *cred,
144     struct core *chdr)
145 {
146 	struct vmspace *vm = p->p_vmspace;
147 	vm_map_t map = &vm->vm_map;
148 	vm_map_entry_t entry, safe;
149 	vaddr_t start, end, top;
150 	struct coreseg cseg;
151 	off_t offset, coffset;
152 	int csize, chunk, flag, error = 0;
153 
154 	offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
155 
156 	RB_FOREACH_SAFE(entry, uvm_map_addr, &map->addr, safe) {
157 		/* should never happen for a user process */
158 		if (UVM_ET_ISSUBMAP(entry)) {
159 			panic("uvm_coredump: user process with submap?");
160 		}
161 
162 		if (!(entry->protection & PROT_WRITE) &&
163 		    entry->start != p->p_p->ps_sigcode)
164 			continue;
165 
166 		/* Don't dump mmaped devices. */
167 		if (entry->object.uvm_obj != NULL &&
168 		    UVM_OBJ_IS_DEVICE(entry->object.uvm_obj))
169 			continue;
170 
171 		start = entry->start;
172 		end = entry->end;
173 
174 		if (start >= VM_MAXUSER_ADDRESS)
175 			continue;
176 
177 		if (end > VM_MAXUSER_ADDRESS)
178 			end = VM_MAXUSER_ADDRESS;
179 
180 #ifdef MACHINE_STACK_GROWS_UP
181 		if (USRSTACK <= start && start < (USRSTACK + MAXSSIZ)) {
182 			top = round_page(USRSTACK + ptoa(vm->vm_ssize));
183 			if (end > top)
184 				end = top;
185 
186 			if (start >= end)
187 				continue;
188 #else
189 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
190 			top = trunc_page(USRSTACK - ptoa(vm->vm_ssize));
191 			if (start < top)
192 				start = top;
193 
194 			if (start >= end)
195 				continue;
196 #endif
197 			flag = CORE_STACK;
198 		} else
199 			flag = CORE_DATA;
200 
201 		/* Set up a new core file segment. */
202 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
203 		cseg.c_addr = start;
204 		cseg.c_size = end - start;
205 
206 		error = vn_rdwr(UIO_WRITE, vp,
207 		    (caddr_t)&cseg, chdr->c_seghdrsize,
208 		    offset, UIO_SYSSPACE, IO_UNIT, cred, NULL, p);
209 		/*
210 		 * We might get an EFAULT on objects mapped beyond
211 		 * EOF. Ignore the error.
212 		 */
213 		if (error && error != EFAULT)
214 			break;
215 
216 		offset += chdr->c_seghdrsize;
217 
218 		coffset = 0;
219 		csize = (int)cseg.c_size;
220 		do {
221 			if (p->p_siglist & sigmask(SIGKILL))
222 				return (EINTR);
223 
224 			/* Rest of the loop sleeps with lock held, so... */
225 			yield();
226 
227 			chunk = MIN(csize, MAXPHYS);
228 			error = vn_rdwr(UIO_WRITE, vp,
229 			    (caddr_t)(u_long)cseg.c_addr + coffset,
230 			    chunk, offset + coffset, UIO_USERSPACE,
231 			    IO_UNIT, cred, NULL, p);
232 			if (error)
233 				return (error);
234 
235 			coffset += chunk;
236 			csize -= chunk;
237 		} while (csize > 0);
238 		offset += cseg.c_size;
239 
240 		/* Discard the memory */
241 		uvm_unmap(map, cseg.c_addr, cseg.c_addr + cseg.c_size);
242 
243 		chdr->c_nseg++;
244 	}
245 
246 	return (error);
247 }
248 
249 int
250 uvm_coredump_walkmap(struct proc *p, void *iocookie,
251     int (*func)(struct proc *, void *, struct uvm_coredump_state *),
252     void *cookie)
253 {
254 	struct uvm_coredump_state state;
255 	struct vmspace *vm = p->p_vmspace;
256 	struct vm_map *map = &vm->vm_map;
257 	struct vm_map_entry *entry;
258 	vaddr_t top;
259 	int error;
260 
261 	RB_FOREACH(entry, uvm_map_addr, &map->addr) {
262 		state.cookie = cookie;
263 		state.prot = entry->protection;
264 		state.flags = 0;
265 
266 		/* should never happen for a user process */
267 		if (UVM_ET_ISSUBMAP(entry)) {
268 			panic("uvm_coredump: user process with submap?");
269 		}
270 
271 		if (!(entry->protection & PROT_WRITE) &&
272 		    entry->start != p->p_p->ps_sigcode)
273 			continue;
274 
275 		/* Don't dump mmaped devices. */
276 		if (entry->object.uvm_obj != NULL &&
277 		    UVM_OBJ_IS_DEVICE(entry->object.uvm_obj))
278 			continue;
279 
280 		state.start = entry->start;
281 		state.realend = entry->end;
282 		state.end = entry->end;
283 
284 		if (state.start >= VM_MAXUSER_ADDRESS)
285 			continue;
286 
287 		if (state.end > VM_MAXUSER_ADDRESS)
288 			state.end = VM_MAXUSER_ADDRESS;
289 
290 #ifdef MACHINE_STACK_GROWS_UP
291 		if (USRSTACK <= state.start &&
292 		    state.start < (USRSTACK + MAXSSIZ)) {
293 			top = round_page(USRSTACK + ptoa(vm->vm_ssize));
294 			if (state.end > top)
295 				state.end = top;
296 
297 			if (state.start >= state.end)
298 				continue;
299 #else
300 		if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
301 			top = trunc_page(USRSTACK - ptoa(vm->vm_ssize));
302 			if (state.start < top)
303 				state.start = top;
304 
305 			if (state.start >= state.end)
306 				continue;
307 #endif
308 			state.flags |= UVM_COREDUMP_STACK;
309 		}
310 
311 		error = (*func)(p, iocookie, &state);
312 		if (error)
313 			return (error);
314 	}
315 
316 	return (0);
317 }
318 
319 #endif	/* !SMALL_KERNEL */
320