xref: /netbsd-src/sys/uvm/uvm_unix.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: uvm_unix.c,v 1.45 2014/09/05 05:36:49 matt 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. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
39  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
40  * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
41  */
42 
43 /*
44  * uvm_unix.c: traditional sbrk/grow interface to vm.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: uvm_unix.c,v 1.45 2014/09/05 05:36:49 matt Exp $");
49 
50 #include "opt_pax.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 
57 #include <sys/mount.h>
58 #include <sys/syscallargs.h>
59 
60 #ifdef PAX_MPROTECT
61 #include <sys/pax.h>
62 #endif /* PAX_MPROTECT */
63 
64 #include <uvm/uvm.h>
65 
66 /*
67  * sys_obreak: set break
68  */
69 
70 int
71 sys_obreak(struct lwp *l, const struct sys_obreak_args *uap, register_t *retval)
72 {
73 	/* {
74 		syscallarg(char *) nsize;
75 	} */
76 	struct proc *p = l->l_proc;
77 	struct vmspace *vm = p->p_vmspace;
78 	vaddr_t nbreak, obreak;
79 	int error;
80 
81 	mutex_enter(&p->p_auxlock);
82 	obreak = (vaddr_t)vm->vm_daddr;
83 	nbreak = round_page((vaddr_t)SCARG(uap, nsize));
84 	if (nbreak == 0
85 	    || ((nbreak - obreak) > p->p_rlimit[RLIMIT_DATA].rlim_cur
86 		&& nbreak > obreak)) {
87 		mutex_exit(&p->p_auxlock);
88 		return (ENOMEM);
89 	}
90 
91 	obreak = round_page(obreak + ptoa(vm->vm_dsize));
92 
93 	if (nbreak == obreak) {
94 		mutex_exit(&p->p_auxlock);
95 		return (0);
96 	}
97 
98 	/*
99 	 * grow or shrink?
100 	 */
101 
102 	if (nbreak > obreak) {
103 		vm_prot_t prot = UVM_PROT_READ | UVM_PROT_WRITE;
104 		vm_prot_t maxprot = UVM_PROT_ALL;
105 
106 #ifdef PAX_MPROTECT
107 		pax_mprotect(l, &prot, &maxprot);
108 #endif /* PAX_MPROTECT */
109 
110 		error = uvm_map(&vm->vm_map, &obreak, nbreak - obreak, NULL,
111 		    UVM_UNKNOWN_OFFSET, 0,
112 		    UVM_MAPFLAG(prot, maxprot,
113 				UVM_INH_COPY,
114 				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
115 				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
116 		if (error) {
117 #ifdef DEBUG
118 			uprintf("sbrk: grow %#"PRIxVADDR" failed, error = %d\n",
119 			    nbreak - obreak, error);
120 #endif
121 			mutex_exit(&p->p_auxlock);
122 			return (error);
123 		}
124 		vm->vm_dsize += atop(nbreak - obreak);
125 	} else {
126 		uvm_deallocate(&vm->vm_map, nbreak, obreak - nbreak);
127 		vm->vm_dsize -= atop(obreak - nbreak);
128 	}
129 	mutex_exit(&p->p_auxlock);
130 
131 	return (0);
132 }
133 
134 /*
135  * uvm_grow: enlarge the "stack segment" to include sp.
136  */
137 
138 int
139 uvm_grow(struct proc *p, vaddr_t sp)
140 {
141 	struct vmspace *vm = p->p_vmspace;
142 	vsize_t nss;
143 
144 	/*
145 	 * For user defined stacks (from sendsig).
146 	 */
147 #ifdef __MACHINE_STACK_GROWS_UP
148 	if (sp < (vaddr_t)vm->vm_minsaddr)
149 #else
150 	if (sp < (vaddr_t)vm->vm_maxsaddr)
151 #endif
152 		return (0);
153 
154 	/*
155 	 * For common case of already allocated (from trap).
156 	 */
157 #ifdef __MACHINE_STACK_GROWS_UP
158 	if (sp < USRSTACK + ctob(vm->vm_ssize))
159 #else
160 	if (sp >= USRSTACK - ctob(vm->vm_ssize))
161 #endif
162 		return (1);
163 
164 	/*
165 	 * Really need to check vs limit and increment stack size if ok.
166 	 */
167 #ifdef __MACHINE_STACK_GROWS_UP
168 	nss = btoc(sp - USRSTACK);
169 #else
170 	nss = btoc(USRSTACK - sp);
171 #endif
172 	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
173 		return (0);
174 	vm->vm_ssize = nss;
175 	return (1);
176 }
177 
178 /*
179  * sys_oadvise: old advice system call
180  */
181 
182 /* ARGSUSED */
183 int
184 sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
185 {
186 #if 0
187 	/* {
188 		syscallarg(int) anom;
189 	} */
190 #endif
191 
192 	return (EINVAL);
193 }
194