xref: /netbsd-src/sys/uvm/uvm_unix.c (revision 313c6c94c424eed90c7b7e494aa83308a0a5d0ce)
1 /*	$NetBSD: uvm_unix.c,v 1.41 2009/03/04 21:52:38 christos 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/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: uvm_unix.c,v 1.41 2009/03/04 21:52:38 christos Exp $");
54 
55 #include "opt_pax.h"
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <sys/resourcevar.h>
61 
62 #include <sys/mount.h>
63 #include <sys/syscallargs.h>
64 
65 #ifdef PAX_MPROTECT
66 #include <sys/pax.h>
67 #endif /* PAX_MPROTECT */
68 
69 #include <uvm/uvm.h>
70 
71 /*
72  * sys_obreak: set break
73  */
74 
75 int
76 sys_obreak(struct lwp *l, const struct sys_obreak_args *uap, register_t *retval)
77 {
78 	/* {
79 		syscallarg(char *) nsize;
80 	} */
81 	struct proc *p = l->l_proc;
82 	struct vmspace *vm = p->p_vmspace;
83 	vaddr_t new, old;
84 	int error;
85 
86 	mutex_enter(&p->p_auxlock);
87 	old = (vaddr_t)vm->vm_daddr;
88 	new = round_page((vaddr_t)SCARG(uap, nsize));
89 	if ((new - old) > p->p_rlimit[RLIMIT_DATA].rlim_cur && new > old) {
90 		mutex_exit(&p->p_auxlock);
91 		return (ENOMEM);
92 	}
93 
94 	old = round_page(old + ptoa(vm->vm_dsize));
95 
96 	if (new == old) {
97 		mutex_exit(&p->p_auxlock);
98 		return (0);
99 	}
100 
101 	/*
102 	 * grow or shrink?
103 	 */
104 
105 	if (new > old) {
106 		vm_prot_t prot = UVM_PROT_READ | UVM_PROT_WRITE;
107 		vm_prot_t maxprot = UVM_PROT_ALL;
108 
109 #ifdef PAX_MPROTECT
110 		pax_mprotect(l, &prot, &maxprot);
111 #endif /* PAX_MPROTECT */
112 
113 		error = uvm_map(&vm->vm_map, &old, new - old, NULL,
114 		    UVM_UNKNOWN_OFFSET, 0,
115 		    UVM_MAPFLAG(prot, maxprot,
116 				UVM_INH_COPY,
117 				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
118 				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
119 		if (error) {
120 #ifdef DEBUG
121 			uprintf("sbrk: grow %ld failed, error = %d\n",
122 			    new - old, error);
123 #endif
124 			mutex_exit(&p->p_auxlock);
125 			return (error);
126 		}
127 		vm->vm_dsize += atop(new - old);
128 	} else {
129 		uvm_deallocate(&vm->vm_map, new, old - new);
130 		vm->vm_dsize -= atop(old - new);
131 	}
132 	mutex_exit(&p->p_auxlock);
133 
134 	return (0);
135 }
136 
137 /*
138  * uvm_grow: enlarge the "stack segment" to include sp.
139  */
140 
141 int
142 uvm_grow(struct proc *p, vaddr_t sp)
143 {
144 	struct vmspace *vm = p->p_vmspace;
145 	vsize_t nss;
146 
147 	/*
148 	 * For user defined stacks (from sendsig).
149 	 */
150 #ifdef __MACHINE_STACK_GROWS_UP
151 	if (sp < (vaddr_t)vm->vm_minsaddr)
152 #else
153 	if (sp < (vaddr_t)vm->vm_maxsaddr)
154 #endif
155 		return (0);
156 
157 	/*
158 	 * For common case of already allocated (from trap).
159 	 */
160 #ifdef __MACHINE_STACK_GROWS_UP
161 	if (sp < USRSTACK + ctob(vm->vm_ssize))
162 #else
163 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
164 #endif
165 		return (1);
166 
167 	/*
168 	 * Really need to check vs limit and increment stack size if ok.
169 	 */
170 #ifdef __MACHINE_STACK_GROWS_UP
171 	nss = btoc(sp - USRSTACK);
172 #else
173 	nss = btoc(USRSTACK - sp);
174 #endif
175 	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
176 		return (0);
177 	vm->vm_ssize = nss;
178 	return (1);
179 }
180 
181 /*
182  * sys_oadvise: old advice system call
183  */
184 
185 /* ARGSUSED */
186 int
187 sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
188 {
189 #if 0
190 	/* {
191 		syscallarg(int) anom;
192 	} */
193 #endif
194 
195 	return (EINVAL);
196 }
197