uvm_unix.c revision 1.21 1 /* $NetBSD: uvm_unix.c,v 1.21 2001/05/06 04:32:09 ross 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/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 <uvm/uvm.h>
64
65 /*
66 * sys_obreak: set break
67 */
68
69 int
70 sys_obreak(p, v, retval)
71 struct proc *p;
72 void *v;
73 register_t *retval;
74 {
75 struct sys_obreak_args /* {
76 syscallarg(char *) nsize;
77 } */ *uap = v;
78 struct vmspace *vm = p->p_vmspace;
79 vaddr_t new, old;
80 int error;
81
82 old = (vaddr_t)vm->vm_daddr;
83 new = round_page((vaddr_t)SCARG(uap, nsize));
84 if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur && new > old)
85 return (ENOMEM);
86
87 old = round_page(old + ptoa(vm->vm_dsize));
88
89 if (new == old)
90 return (0);
91
92 /*
93 * grow or shrink?
94 */
95 if (new > old) {
96 error = uvm_map(&vm->vm_map, &old, new - old, NULL,
97 UVM_UNKNOWN_OFFSET, 0,
98 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
99 UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
100 UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
101 if (error) {
102 uprintf("sbrk: grow %ld failed, error = %d\n",
103 new - old, error);
104 return error;
105 }
106 vm->vm_dsize += atop(new - old);
107 } else {
108 uvm_deallocate(&vm->vm_map, new, old - new);
109 vm->vm_dsize -= atop(old - new);
110 }
111 return 0;
112 }
113
114 /*
115 * uvm_grow: enlarge the "stack segment" to include sp.
116 */
117
118 int
119 uvm_grow(p, sp)
120 struct proc *p;
121 vaddr_t sp;
122 {
123 struct vmspace *vm = p->p_vmspace;
124 int si;
125
126 /*
127 * For user defined stacks (from sendsig).
128 */
129 if (sp < (vaddr_t)vm->vm_maxsaddr)
130 return (0);
131
132 /*
133 * For common case of already allocated (from trap).
134 */
135 if (sp >= USRSTACK - ctob(vm->vm_ssize))
136 return (1);
137
138 /*
139 * Really need to check vs limit and increment stack size if ok.
140 */
141 si = btoc(USRSTACK-sp) - vm->vm_ssize;
142 if (vm->vm_ssize + si > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
143 return (0);
144 vm->vm_ssize += si;
145 return (1);
146 }
147
148 /*
149 * sys_oadvise: old advice system call
150 */
151
152 /* ARGSUSED */
153 int
154 sys_ovadvise(p, v, retval)
155 struct proc *p;
156 void *v;
157 register_t *retval;
158 {
159 #if 0
160 struct sys_ovadvise_args /* {
161 syscallarg(int) anom;
162 } */ *uap = v;
163 #endif
164
165 return (EINVAL);
166 }
167
168 /*
169 * uvm_coredump: dump core!
170 */
171
172 int
173 uvm_coredump(p, vp, cred, chdr)
174 struct proc *p;
175 struct vnode *vp;
176 struct ucred *cred;
177 struct core *chdr;
178 {
179 struct vmspace *vm = p->p_vmspace;
180 vm_map_t map = &vm->vm_map;
181 vm_map_entry_t entry;
182 vaddr_t start, end, maxstack;
183 struct coreseg cseg;
184 off_t offset;
185 int flag, error = 0;
186
187 offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
188 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
189
190 for (entry = map->header.next; entry != &map->header;
191 entry = entry->next) {
192
193 /* should never happen for a user process */
194 if (UVM_ET_ISSUBMAP(entry)) {
195 panic("uvm_coredump: user process with submap?");
196 }
197
198 if (!(entry->protection & VM_PROT_WRITE))
199 continue;
200
201 start = entry->start;
202 end = entry->end;
203
204 if (start >= VM_MAXUSER_ADDRESS)
205 continue;
206
207 if (end > VM_MAXUSER_ADDRESS)
208 end = VM_MAXUSER_ADDRESS;
209
210 if (start >= (vaddr_t)vm->vm_maxsaddr) {
211 if (end <= maxstack)
212 continue;
213 if (start < maxstack)
214 start = maxstack;
215 flag = CORE_STACK;
216 } else
217 flag = CORE_DATA;
218
219 /*
220 * Set up a new core file segment.
221 */
222 CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
223 cseg.c_addr = start;
224 cseg.c_size = end - start;
225
226 error = vn_rdwr(UIO_WRITE, vp,
227 (caddr_t)&cseg, chdr->c_seghdrsize,
228 offset, UIO_SYSSPACE,
229 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
230 if (error)
231 break;
232
233 offset += chdr->c_seghdrsize;
234 error = vn_rdwr(UIO_WRITE, vp,
235 (caddr_t)cseg.c_addr, (int)cseg.c_size,
236 offset, UIO_USERSPACE,
237 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
238 if (error)
239 break;
240
241 offset += cseg.c_size;
242 chdr->c_nseg++;
243 }
244
245 return (error);
246 }
247
248 #if COMPAT_NETBSD32
249 /*
250 * uvm_coredump32: dump 32-bit core!
251 */
252
253 int
254 uvm_coredump32(p, vp, cred, chdr)
255 struct proc *p;
256 struct vnode *vp;
257 struct ucred *cred;
258 struct core32 *chdr;
259 {
260 struct vmspace *vm = p->p_vmspace;
261 vm_map_t map = &vm->vm_map;
262 vm_map_entry_t entry;
263 vaddr_t start, end, maxstack;
264 struct coreseg32 cseg;
265 off_t offset;
266 int flag, error = 0;
267
268 offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
269 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
270
271 for (entry = map->header.next; entry != &map->header;
272 entry = entry->next) {
273
274 /* should never happen for a user process */
275 if (UVM_ET_ISSUBMAP(entry)) {
276 panic("uvm_coredump: user process with submap?");
277 }
278
279 if (!(entry->protection & VM_PROT_WRITE))
280 continue;
281
282 start = entry->start;
283 end = entry->end;
284
285 if (start >= VM_MAXUSER_ADDRESS)
286 continue;
287
288 if (end > VM_MAXUSER_ADDRESS)
289 end = VM_MAXUSER_ADDRESS;
290
291 if (start >= (vaddr_t)vm->vm_maxsaddr) {
292 if (end <= maxstack)
293 continue;
294 if (start < maxstack)
295 start = maxstack;
296 flag = CORE_STACK;
297 } else
298 flag = CORE_DATA;
299
300 /*
301 * Set up a new core file segment.
302 */
303 CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
304 cseg.c_addr = start;
305 cseg.c_size = end - start;
306
307 error = vn_rdwr(UIO_WRITE, vp,
308 (caddr_t)&cseg, chdr->c_seghdrsize,
309 offset, UIO_SYSSPACE,
310 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
311 if (error)
312 break;
313
314 offset += chdr->c_seghdrsize;
315 error = vn_rdwr(UIO_WRITE, vp,
316 (caddr_t)(u_long)cseg.c_addr, (int)cseg.c_size,
317 offset, UIO_USERSPACE,
318 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
319 if (error)
320 break;
321
322 offset += cseg.c_size;
323 chdr->c_nseg++;
324 }
325
326 return (error);
327 }
328
329 #endif
330