uvm_unix.c revision 1.9 1 /* $NetBSD: uvm_unix.c,v 1.9 1999/12/04 23:14:40 fvdl 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
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 #include <sys/core.h>
58
59 #include <sys/mount.h>
60 #include <sys/syscallargs.h>
61
62 #include <vm/vm.h>
63 #include <uvm/uvm.h>
64
65
66 /*
67 * sys_obreak: set break
68 */
69
70 int
71 sys_obreak(p, v, retval)
72 struct proc *p;
73 void *v;
74 register_t *retval;
75 {
76 struct sys_obreak_args /* {
77 syscallarg(char *) nsize;
78 } */ *uap = v;
79 register struct vmspace *vm = p->p_vmspace;
80 vaddr_t new, old;
81 int rv;
82 register int diff;
83
84 old = (vaddr_t)vm->vm_daddr;
85 new = round_page(SCARG(uap, nsize));
86 if ((int)(new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur)
87 return(ENOMEM);
88
89 old = round_page(old + ctob(vm->vm_dsize));
90 diff = new - old;
91
92 /*
93 * grow or shrink?
94 */
95
96 if (diff > 0) {
97
98 rv = uvm_map(&vm->vm_map, &old, diff, NULL, UVM_UNKNOWN_OFFSET,
99 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
100 UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
101 UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
102
103 if (rv != KERN_SUCCESS) {
104 uprintf("sbrk: grow failed, return = %d\n", rv);
105 return(ENOMEM);
106 }
107 vm->vm_dsize += btoc(diff);
108
109 } else if (diff < 0) {
110
111 diff = -diff;
112 rv = uvm_deallocate(&vm->vm_map, new, diff);
113 if (rv != KERN_SUCCESS) {
114 uprintf("sbrk: shrink failed, return = %d\n", rv);
115 return(ENOMEM);
116 }
117 vm->vm_dsize -= btoc(diff);
118
119 }
120 return(0);
121 }
122
123 /*
124 * uvm_grow: enlarge the "stack segment" to include sp.
125 */
126
127 int
128 uvm_grow(p, sp)
129 struct proc *p;
130 vaddr_t sp;
131 {
132 register struct vmspace *vm = p->p_vmspace;
133 register int si;
134
135 /*
136 * For user defined stacks (from sendsig).
137 */
138 if (sp < (vaddr_t)vm->vm_maxsaddr)
139 return (0);
140
141 /*
142 * For common case of already allocated (from trap).
143 */
144 if (sp >= USRSTACK - ctob(vm->vm_ssize))
145 return (1);
146
147 /*
148 * Really need to check vs limit and increment stack size if ok.
149 */
150 si = btoc(USRSTACK-sp) - vm->vm_ssize;
151 if (vm->vm_ssize + si > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
152 return (0);
153 vm->vm_ssize += si;
154 return (1);
155 }
156
157 /*
158 * sys_oadvise: old advice system call
159 */
160
161 /* ARGSUSED */
162 int
163 sys_ovadvise(p, v, retval)
164 struct proc *p;
165 void *v;
166 register_t *retval;
167 {
168 #if 0
169 struct sys_ovadvise_args /* {
170 syscallarg(int) anom;
171 } */ *uap = v;
172 #endif
173
174 return (EINVAL);
175 }
176
177 /*
178 * uvm_coredump: dump core!
179 */
180
181 int
182 uvm_coredump(p, vp, cred, chdr)
183 struct proc *p;
184 struct vnode *vp;
185 struct ucred *cred;
186 struct core *chdr;
187 {
188 register struct vmspace *vm = p->p_vmspace;
189 register vm_map_t map = &vm->vm_map;
190 register vm_map_entry_t entry;
191 vaddr_t start, end;
192 struct coreseg cseg;
193 off_t offset;
194 int flag, error = 0;
195
196 offset = chdr->c_hdrsize + chdr->c_seghdrsize + chdr->c_cpusize;
197
198 for (entry = map->header.next; entry != &map->header;
199 entry = entry->next) {
200
201 /* should never happen for a user process */
202 if (UVM_ET_ISSUBMAP(entry)) {
203 panic("uvm_coredump: user process with submap?");
204 }
205
206 if (!(entry->protection & VM_PROT_WRITE))
207 continue;
208
209 start = entry->start;
210 end = entry->end;
211
212 if (start >= VM_MAXUSER_ADDRESS)
213 continue;
214
215 if (end > VM_MAXUSER_ADDRESS)
216 end = VM_MAXUSER_ADDRESS;
217
218 if (start >= (vaddr_t)vm->vm_maxsaddr) {
219 flag = CORE_STACK;
220 start = trunc_page(USRSTACK - ctob(vm->vm_ssize));
221 if (start >= end)
222 continue;
223 } else
224 flag = CORE_DATA;
225
226 /*
227 * Set up a new core file segment.
228 */
229 CORE_SETMAGIC(cseg, CORESEGMAGIC, CORE_GETMID(*chdr), flag);
230 cseg.c_addr = start;
231 cseg.c_size = end - start;
232
233 error = vn_rdwr(UIO_WRITE, vp,
234 (caddr_t)&cseg, chdr->c_seghdrsize,
235 offset, UIO_SYSSPACE,
236 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
237 if (error)
238 break;
239
240 offset += chdr->c_seghdrsize;
241 error = vn_rdwr(UIO_WRITE, vp,
242 (caddr_t)cseg.c_addr, (int)cseg.c_size,
243 offset, UIO_USERSPACE,
244 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
245 if (error)
246 break;
247
248 offset += cseg.c_size;
249 chdr->c_nseg++;
250 }
251
252 return (error);
253 }
254
255