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