xref: /openbsd-src/sys/uvm/uvm_unix.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: uvm_unix.c,v 1.13 2001/08/11 10:57:22 art Exp $	*/
2 /*	$NetBSD: uvm_unix.c,v 1.12 2000/03/30 12:31:50 augustss 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. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *      This product includes software developed by Charles D. Cranor,
26  *	Washington University, the University of California, Berkeley and
27  *	its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
45  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
46  * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
47  */
48 
49 /*
50  * uvm_unix.c: traditional sbrk/grow interface to vm.
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/resourcevar.h>
57 #include <sys/vnode.h>
58 #include <sys/core.h>
59 
60 #include <sys/mount.h>
61 #include <sys/syscallargs.h>
62 
63 #include <vm/vm.h>
64 #include <uvm/uvm.h>
65 
66 
67 /*
68  * sys_obreak: set break
69  */
70 
71 int
72 sys_obreak(p, v, retval)
73 	struct proc *p;
74 	void *v;
75 	register_t *retval;
76 {
77 	struct sys_obreak_args /* {
78 		syscallarg(char *) nsize;
79 	} */ *uap = v;
80 	struct vmspace *vm = p->p_vmspace;
81 	vaddr_t new, old;
82 	int rv;
83 	long diff;
84 
85 	old = (vaddr_t)vm->vm_daddr;
86 	new = round_page((vaddr_t)SCARG(uap, nsize));
87 	if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur)
88 		return(ENOMEM);
89 
90 	old = round_page(old + ptoa(vm->vm_dsize));
91 	diff = new - old;
92 
93 	if (diff == 0)
94 		return (0);
95 
96 	/*
97 	 * grow or shrink?
98 	 */
99 	if (diff > 0) {
100 		rv = uvm_map(&vm->vm_map, &old, diff, NULL, UVM_UNKNOWN_OFFSET,
101 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
102 		    UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
103 		    UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
104 		if (rv == KERN_SUCCESS) {
105 			vm->vm_dsize += atop(diff);
106 			return (0);
107 		}
108 	} else {
109 		rv = uvm_deallocate(&vm->vm_map, new, -diff);
110 		if (rv == KERN_SUCCESS) {
111 			vm->vm_dsize -= atop(-diff);
112 			return (0);
113 		}
114 	}
115 
116 	uprintf("sbrk: %s %ld failed, return = %d\n",
117 		diff > 0 ? "grow" : "shrink",
118 		(long)(diff > 0 ? diff : -diff), rv);
119 	return(ENOMEM);
120 }
121 
122 /*
123  * uvm_grow: enlarge the "stack segment" to include sp.
124  */
125 
126 int
127 uvm_grow(p, sp)
128 	struct proc *p;
129 	vaddr_t sp;
130 {
131 	struct vmspace *vm = p->p_vmspace;
132 	int si;
133 
134 	/*
135 	 * For user defined stacks (from sendsig).
136 	 */
137 #ifdef MACHINE_STACK_GROWS_UP
138 	if (sp > (vaddr_t)vm->vm_maxsaddr)
139 #else
140 	if (sp < (vaddr_t)vm->vm_maxsaddr)
141 #endif
142 		return (0);
143 
144 	/*
145 	 * For common case of already allocated (from trap).
146 	 */
147 #ifdef MACHINE_STACK_GROWS_UP
148 	if (sp < USRSTACK + ctob(vm->vm_ssize))
149 #else
150 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
151 #endif
152 		return (1);
153 
154 	/*
155 	 * Really need to check vs limit and increment stack size if ok.
156 	 */
157 #ifdef MACHINE_STACK_GROWS_UP
158 	si = btoc(sp - USRSTACK) - vm->vm_ssize;
159 #else
160 	si = btoc(USRSTACK-sp) - vm->vm_ssize;
161 #endif
162 	if (vm->vm_ssize + si > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
163 		return (0);
164 	vm->vm_ssize += si;
165 	return (1);
166 }
167 
168 /*
169  * sys_oadvise: old advice system call
170  */
171 
172 /* ARGSUSED */
173 int
174 sys_ovadvise(p, v, retval)
175 	struct proc *p;
176 	void *v;
177 	register_t *retval;
178 {
179 #if 0
180 	struct sys_ovadvise_args /* {
181 		syscallarg(int) anom;
182 	} */ *uap = v;
183 #endif
184 
185 	return (EINVAL);
186 }
187 
188 /*
189  * uvm_coredump: dump core!
190  */
191 
192 int
193 uvm_coredump(p, vp, cred, chdr)
194 	struct proc *p;
195 	struct vnode *vp;
196 	struct ucred *cred;
197 	struct core *chdr;
198 {
199 	struct vmspace *vm = p->p_vmspace;
200 	vm_map_t map = &vm->vm_map;
201 	vm_map_entry_t entry;
202 	vaddr_t start, end;
203 	struct coreseg cseg;
204 	off_t offset;
205 	int flag, error = 0;
206 
207 	offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
208 
209 	for (entry = map->header.next; entry != &map->header;
210 	    entry = entry->next) {
211 
212 		/* should never happen for a user process */
213 		if (UVM_ET_ISSUBMAP(entry)) {
214 			panic("uvm_coredump: user process with submap?");
215 		}
216 
217 		if (!(entry->protection & VM_PROT_WRITE))
218 			continue;
219 
220 		start = entry->start;
221 		end = entry->end;
222 
223 		if (start >= VM_MAXUSER_ADDRESS)
224 			continue;
225 
226 		if (end > VM_MAXUSER_ADDRESS)
227 			end = VM_MAXUSER_ADDRESS;
228 
229 #ifdef MACHINE_STACK_GROWS_UP
230 		if (start >= USRSTACK) {
231 			start = USRSTACK;
232 #else
233 		if (start >= (vaddr_t)vm->vm_maxsaddr) {
234 			start = trunc_page(USRSTACK - ctob(vm->vm_ssize));
235 #endif
236 			flag = CORE_STACK;
237 			if (start >= end)
238 				continue;
239 		} else
240 			flag = CORE_DATA;
241 
242 		/*
243 		 * Set up a new core file segment.
244 		 */
245 		CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
246 		cseg.c_addr = start;
247 		cseg.c_size = end - start;
248 
249 		error = vn_rdwr(UIO_WRITE, vp,
250 		    (caddr_t)&cseg, chdr->c_seghdrsize,
251 		    offset, UIO_SYSSPACE,
252 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
253 		if (error)
254 			break;
255 
256 		offset += chdr->c_seghdrsize;
257 		error = vn_rdwr(UIO_WRITE, vp,
258 		    (caddr_t)cseg.c_addr, (int)cseg.c_size,
259 		    offset, UIO_USERSPACE,
260 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
261 		if (error)
262 			break;
263 
264 		offset += cseg.c_size;
265 		chdr->c_nseg++;
266 	}
267 
268 	return (error);
269 }
270 
271