sys/vfs/msdosfs: Sync with FreeBSD (non functional diffs)
[dragonfly.git] / sys / vm / vm_unix.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1991, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
37  *
38  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
39  * $FreeBSD: src/sys/vm/vm_unix.c,v 1.24.2.2 2002/07/02 20:06:19 dillon Exp $
40  * $DragonFly: src/sys/vm/vm_unix.c,v 1.7 2006/11/07 17:51:24 dillon Exp $
41  */
42
43 /*
44  * Traditional sbrk/grow interface to VM
45  */
46 #include <sys/param.h>
47 #include <sys/sysproto.h>
48 #include <sys/proc.h>
49 #include <sys/resourcevar.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <sys/lock.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56
57 #include <sys/thread.h>
58
59 /*
60  * sys_sbrk backs the C library sbrk call
61  *
62  * void *sbrk(intptr_t incr)
63  *
64  * No requirements.
65  */
66 int
67 sys_sbrk(struct sbrk_args *uap)
68 {
69         struct proc *p = curproc;
70         struct vmspace *vm = p->p_vmspace;
71         vm_offset_t nbase;
72         vm_offset_t base;
73         vm_offset_t base_end;
74         vm_offset_t incr;
75         int rv;
76         int error;
77
78         error = 0;
79
80         lwkt_gettoken(&vm->vm_map.token);
81
82         /*
83          * Cannot assume that last data page for binary is mapped R/W.
84          */
85         base = round_page((vm_offset_t)vm->vm_daddr) + vm->vm_dsize;
86         incr = uap->incr;
87
88         /*
89          * Cannot allow space to be freed with sbrk() because it is not
90          * thread-safe for userland and because unrelated mmap()s can reside
91          * in the data space if the DATA rlimit is raised on the running
92          * program.
93          */
94         if (incr < 0) {
95                 error = EOPNOTSUPP;
96                 goto done;
97         }
98
99         /*
100          * Userland requests current base
101          */
102         if (incr == 0) {
103                 uap->sysmsg_resultp = (void *)base;
104                 goto done;
105         }
106
107         /*
108          * Calculate approximate area (vm_map_find() may change this).
109          * Check for overflow, address space, and rlimit caps.
110          */
111         base_end = base + incr;
112         if (base_end >= VM_MAX_USER_ADDRESS) {
113                 error = ENOMEM;
114                 goto done;
115         }
116         if (base_end < base ||
117             base_end - (vm_offset_t)vm->vm_daddr >
118             (vm_offset_t)p->p_rlimit[RLIMIT_DATA].rlim_cur) {
119                 error = ENOMEM;
120                 goto done;
121         }
122
123         /*
124          * Same-page optimization (protected by token)
125          */
126         if ((base & PAGE_MASK) != 0 &&
127             ((base ^ (base_end - 1)) & ~(vm_offset_t)PAGE_MASK) == 0) {
128                 uap->sysmsg_resultp = (void *)base;
129                 vm->vm_dsize += incr;
130                 goto done;
131         }
132
133         /*
134          * Formally map more space
135          */
136         nbase = round_page(base);
137         rv = vm_map_find(&vm->vm_map, NULL, NULL,
138                          0, &nbase, round_page(incr),
139                          PAGE_SIZE, FALSE,
140                          VM_MAPTYPE_NORMAL, VM_SUBSYS_BRK,
141                          VM_PROT_ALL, VM_PROT_ALL, 0);
142         if (rv != KERN_SUCCESS) {
143                 error = ENOMEM;
144                 goto done;
145         }
146         base_end = nbase + round_page(incr);
147         uap->sysmsg_resultp = (void *)nbase;
148         if (vm->vm_map.flags & MAP_WIREFUTURE)
149                 vm_map_wire(&vm->vm_map, base, base_end, FALSE);
150
151         /*
152          * Adjust dsize upwards only
153          */
154         incr = nbase + incr - round_page((vm_offset_t)vm->vm_daddr);
155         if (vm->vm_dsize < incr)
156                 vm->vm_dsize = incr;
157
158 done:
159         lwkt_reltoken(&vm->vm_map.token);
160
161         return (error);
162 }
163
164 /*
165  * sys_obreak is used by the sbrk emulation code in libc when sbrk()
166  * is not supported.
167  *
168  * obreak_args(char *nsize)
169  *
170  * No requirements.
171  */
172 int
173 sys_obreak(struct obreak_args *uap)
174 {
175         struct proc *p = curproc;
176         struct vmspace *vm = p->p_vmspace;
177         vm_offset_t new, old, base;
178         int rv;
179         int error;
180
181         error = 0;
182
183         lwkt_gettoken(&vm->vm_map.token);
184
185         base = round_page((vm_offset_t)vm->vm_daddr);
186         new = round_page((vm_offset_t)uap->nsize);
187         old = base + round_page(vm->vm_dsize);
188
189         if (new > base) {
190                 /*
191                  * We check resource limits here, but alow processes to
192                  * reduce their usage, even if they remain over the limit.
193                  */
194                 if (new > old &&
195                     (new - base) > (vm_offset_t) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
196                         error = ENOMEM;
197                         goto done;
198                 }
199                 if (new >= VM_MAX_USER_ADDRESS) {
200                         error = ENOMEM;
201                         goto done;
202                 }
203         } else if (new < base) {
204                 /*
205                  * This is simply an invalid value.  If someone wants to
206                  * do fancy address space manipulations, mmap and munmap
207                  * can do most of what the user would want.
208                  */
209                 error = EINVAL;
210                 goto done;
211         }
212
213         if (new > old) {
214                 vm_size_t diff;
215
216                 diff = new - old;
217                 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
218                         error = ENOMEM;
219                         goto done;
220                 }
221                 rv = vm_map_find(&vm->vm_map, NULL, NULL,
222                                  0, &old, diff,
223                                  PAGE_SIZE, FALSE,
224                                  VM_MAPTYPE_NORMAL, VM_SUBSYS_BRK,
225                                  VM_PROT_ALL, VM_PROT_ALL, 0);
226                 if (rv != KERN_SUCCESS) {
227                         error = ENOMEM;
228                         goto done;
229                 }
230                 if (vm->vm_map.flags & MAP_WIREFUTURE)
231                         vm_map_wire(&vm->vm_map, old, new, FALSE);
232
233                 vm->vm_dsize += diff;
234         } else if (new < old) {
235                 error = EOPNOTSUPP;
236 #if 0
237                 rv = vm_map_remove(&vm->vm_map, new, old);
238                 if (rv != KERN_SUCCESS) {
239                         error = ENOMEM;
240                         goto done;
241                 }
242                 vm->vm_dsize -= old - new;
243 #endif
244         }
245 done:
246         lwkt_reltoken(&vm->vm_map.token);
247
248         return (error);
249 }