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