4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
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 $
44 * Traditional sbrk/grow interface to VM
46 #include <sys/param.h>
47 #include <sys/sysmsg.h>
49 #include <sys/resourcevar.h>
52 #include <vm/vm_param.h>
55 #include <vm/vm_map.h>
57 #include <sys/thread.h>
60 * sys_sbrk backs the C library sbrk call
62 * void *sbrk(intptr_t incr)
67 sys_sbrk(struct sysmsg
*sysmsg
, const struct sbrk_args
*uap
)
69 struct proc
*p
= curproc
;
70 struct vmspace
*vm
= p
->p_vmspace
;
80 lwkt_gettoken(&vm
->vm_map
.token
);
83 * Cannot assume that last data page for binary is mapped R/W.
85 base
= round_page((vm_offset_t
)vm
->vm_daddr
) + vm
->vm_dsize
;
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
100 * Userland requests current base
103 sysmsg
->sysmsg_resultp
= (void *)base
;
108 * Calculate approximate area (vm_map_find() may change this).
109 * Check for overflow, address space, and rlimit caps.
111 base_end
= base
+ incr
;
112 if (base_end
>= VM_MAX_USER_ADDRESS
) {
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
) {
124 * Same-page optimization (protected by token)
126 if ((base
& PAGE_MASK
) != 0 &&
127 ((base
^ (base_end
- 1)) & ~(vm_offset_t
)PAGE_MASK
) == 0) {
128 sysmsg
->sysmsg_resultp
= (void *)base
;
129 vm
->vm_dsize
+= incr
;
134 * Formally map more space
136 nbase
= round_page(base
);
137 rv
= vm_map_find(&vm
->vm_map
, NULL
, NULL
,
138 0, &nbase
, round_page(incr
),
140 VM_MAPTYPE_NORMAL
, VM_SUBSYS_BRK
,
141 VM_PROT_ALL
, VM_PROT_ALL
, 0);
142 if (rv
!= KERN_SUCCESS
) {
146 base_end
= nbase
+ round_page(incr
);
147 sysmsg
->sysmsg_resultp
= (void *)nbase
;
150 if (vm
->vm_map
.flags
& MAP_WIREFUTURE
)
151 vm_map_kernel_wiring(&vm
->vm_map
, base
, base_end
, 0);
154 * Adjust dsize upwards only
156 incr
= nbase
+ incr
- round_page((vm_offset_t
)vm
->vm_daddr
);
157 if (vm
->vm_dsize
< incr
)
161 lwkt_reltoken(&vm
->vm_map
.token
);
167 * sys_obreak is used by the sbrk emulation code in libc when sbrk()
170 * obreak_args(char *nsize)
175 sys_obreak(struct sysmsg
*sysmsg
, const struct obreak_args
*uap
)
177 struct proc
*p
= curproc
;
178 struct vmspace
*vm
= p
->p_vmspace
;
179 vm_offset_t
new, old
, base
;
185 lwkt_gettoken(&vm
->vm_map
.token
);
187 base
= round_page((vm_offset_t
)vm
->vm_daddr
);
188 new = round_page((vm_offset_t
)uap
->nsize
);
189 old
= base
+ round_page(vm
->vm_dsize
);
193 * We check resource limits here, but alow processes to
194 * reduce their usage, even if they remain over the limit.
197 (new - base
) > (vm_offset_t
) p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
) {
201 if (new >= VM_MAX_USER_ADDRESS
) {
205 } else if (new < base
) {
207 * This is simply an invalid value. If someone wants to
208 * do fancy address space manipulations, mmap and munmap
209 * can do most of what the user would want.
219 if (vm
->vm_map
.size
+ diff
> p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
223 rv
= vm_map_find(&vm
->vm_map
, NULL
, NULL
,
226 VM_MAPTYPE_NORMAL
, VM_SUBSYS_BRK
,
227 VM_PROT_ALL
, VM_PROT_ALL
, 0);
228 if (rv
!= KERN_SUCCESS
) {
234 if (vm
->vm_map
.flags
& MAP_WIREFUTURE
)
235 vm_map_kernel_wiring(&vm
->vm_map
, old
, new, 0);
237 vm
->vm_dsize
+= diff
;
238 } else if (new < old
) {
241 rv
= vm_map_remove(&vm
->vm_map
, new, old
);
242 if (rv
!= KERN_SUCCESS
) {
246 vm
->vm_dsize
-= old
- new;
250 lwkt_reltoken(&vm
->vm_map
.token
);