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