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