2 * memory management system call shims and definitions
4 * Copyright (c) 2013-15 Stacey D. Son
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 1982, 1986, 1993
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
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.
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
49 #ifndef BSD_USER_BSD_MEM_H
50 #define BSD_USER_BSD_MEM_H
52 #include <sys/types.h>
59 #include "exec/page-protection.h"
61 extern struct bsd_shm_regions bsd_shm_regions
[];
62 extern abi_ulong target_brk
;
63 extern abi_ulong initial_target_brk
;
66 static inline abi_long
do_bsd_mmap(void *cpu_env
, abi_long arg1
, abi_long arg2
,
67 abi_long arg3
, abi_long arg4
, abi_long arg5
, abi_long arg6
, abi_long arg7
,
70 if (regpairs_aligned(cpu_env
) != 0) {
74 return get_errno(target_mmap(arg1
, arg2
, arg3
,
75 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
76 arg5
, target_arg64(arg6
, arg7
)));
80 static inline abi_long
do_bsd_munmap(abi_long arg1
, abi_long arg2
)
82 return get_errno(target_munmap(arg1
, arg2
));
86 static inline abi_long
do_bsd_mprotect(abi_long arg1
, abi_long arg2
,
89 return get_errno(target_mprotect(arg1
, arg2
, arg3
));
93 static inline abi_long
do_bsd_msync(abi_long addr
, abi_long len
, abi_long flags
)
95 if (!guest_range_valid_untagged(addr
, len
)) {
96 /* It seems odd, but POSIX wants this to be ENOMEM */
97 return -TARGET_ENOMEM
;
100 return get_errno(msync(g2h_untagged(addr
), len
, flags
));
104 static inline abi_long
do_bsd_mlock(abi_long arg1
, abi_long arg2
)
106 if (!guest_range_valid_untagged(arg1
, arg2
)) {
107 return -TARGET_EINVAL
;
109 return get_errno(mlock(g2h_untagged(arg1
), arg2
));
113 static inline abi_long
do_bsd_munlock(abi_long arg1
, abi_long arg2
)
115 if (!guest_range_valid_untagged(arg1
, arg2
)) {
116 return -TARGET_EINVAL
;
118 return get_errno(munlock(g2h_untagged(arg1
), arg2
));
122 static inline abi_long
do_bsd_mlockall(abi_long arg1
)
124 return get_errno(mlockall(arg1
));
128 static inline abi_long
do_bsd_munlockall(void)
130 return get_errno(munlockall());
134 static inline abi_long
do_bsd_madvise(abi_long arg1
, abi_long arg2
,
139 abi_long start
= arg1
;
140 abi_long len_in
= arg2
;
141 abi_long advice
= arg3
;
143 if (start
& ~TARGET_PAGE_MASK
) {
144 return -TARGET_EINVAL
;
149 len
= TARGET_PAGE_ALIGN(len_in
);
150 if (len
== 0 || !guest_range_valid_untagged(start
, len
)) {
151 return -TARGET_EINVAL
;
155 * Most advice values are hints, so ignoring and returning success is ok.
157 * However, some advice values such as MADV_DONTNEED, are not hints and
158 * need to be emulated.
160 * A straight passthrough for those may not be safe because qemu sometimes
161 * turns private file-backed mappings into anonymous mappings.
162 * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
163 * same semantics for the host as for the guest.
165 * MADV_DONTNEED is passed through, if possible.
166 * If passthrough isn't possible, we nevertheless (wrongly!) return
167 * success, which is broken but some userspace programs fail to work
168 * otherwise. Completely implementing such emulation is quite complicated
174 if (page_check_range(start
, len
, PAGE_PASSTHROUGH
)) {
175 ret
= get_errno(madvise(g2h_untagged(start
), len
, advice
));
177 page_reset_target_data(start
, start
+ len
- 1);
187 static inline abi_long
do_bsd_minherit(abi_long addr
, abi_long len
,
190 return get_errno(minherit(g2h_untagged(addr
), len
, inherit
));
194 static inline abi_long
do_bsd_mincore(abi_ulong target_addr
, abi_ulong len
,
195 abi_ulong target_vec
)
199 abi_ulong vec_len
= DIV_ROUND_UP(len
, TARGET_PAGE_SIZE
);
201 if (!guest_range_valid_untagged(target_addr
, len
)
202 || !page_check_range(target_addr
, len
, PAGE_VALID
)) {
203 return -TARGET_EFAULT
;
206 p
= lock_user(VERIFY_WRITE
, target_vec
, vec_len
, 0);
208 return -TARGET_EFAULT
;
210 ret
= get_errno(mincore(g2h_untagged(target_addr
), len
, p
));
211 unlock_user(p
, target_vec
, vec_len
);
216 /* do_brk() must return target values and target errnos. */
217 static inline abi_long
do_obreak(abi_ulong brk_val
)
219 abi_long mapped_addr
;
223 /* brk pointers are always untagged */
225 /* do not allow to shrink below initial brk value */
226 if (brk_val
< initial_target_brk
) {
230 new_brk
= TARGET_PAGE_ALIGN(brk_val
);
231 old_brk
= TARGET_PAGE_ALIGN(target_brk
);
233 /* new and old target_brk might be on the same page */
234 if (new_brk
== old_brk
) {
235 target_brk
= brk_val
;
239 /* Release heap if necessary */
240 if (new_brk
< old_brk
) {
241 target_munmap(new_brk
, old_brk
- new_brk
);
243 target_brk
= brk_val
;
247 mapped_addr
= target_mmap(old_brk
, new_brk
- old_brk
,
248 PROT_READ
| PROT_WRITE
,
249 MAP_FIXED
| MAP_EXCL
| MAP_ANON
| MAP_PRIVATE
,
252 if (mapped_addr
== old_brk
) {
253 target_brk
= brk_val
;
257 /* For everything else, return the previous break. */
262 static inline abi_long
do_bsd_shm_open(abi_ulong arg1
, abi_long arg2
,
268 if (arg1
== (uintptr_t)SHM_ANON
) {
271 p
= lock_user_string(arg1
);
273 return -TARGET_EFAULT
;
276 ret
= get_errno(shm_open(p
, target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
280 unlock_user(p
, arg1
, 0);
287 static inline abi_long
do_bsd_shm_unlink(abi_ulong arg1
)
292 p
= lock_user_string(arg1
);
294 return -TARGET_EFAULT
;
296 ret
= get_errno(shm_unlink(p
)); /* XXX path(p)? */
297 unlock_user(p
, arg1
, 0);
303 static inline abi_long
do_bsd_shmget(abi_long arg1
, abi_ulong arg2
,
306 return get_errno(shmget(arg1
, arg2
, arg3
));
310 static inline abi_long
do_bsd_shmctl(abi_long shmid
, abi_long cmd
,
313 struct shmid_ds dsarg
;
314 abi_long ret
= -TARGET_EINVAL
;
320 if (target_to_host_shmid_ds(&dsarg
, buff
)) {
321 return -TARGET_EFAULT
;
323 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
324 if (host_to_target_shmid_ds(buff
, &dsarg
)) {
325 return -TARGET_EFAULT
;
330 if (target_to_host_shmid_ds(&dsarg
, buff
)) {
331 return -TARGET_EFAULT
;
333 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
337 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
341 ret
= -TARGET_EINVAL
;
349 static inline abi_long
do_bsd_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
353 struct shmid_ds shm_info
;
355 /* Find out the length of the shared memory segment. */
356 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
358 /* Can't get the length */
362 if (!guest_range_valid_untagged(shmaddr
, shm_info
.shm_segsz
)) {
363 return -TARGET_EINVAL
;
366 WITH_MMAP_LOCK_GUARD() {
370 host_raddr
= shmat(shmid
, (void *)g2h_untagged(shmaddr
), shmflg
);
372 abi_ulong mmap_start
;
374 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
376 if (mmap_start
== -1) {
377 return -TARGET_ENOMEM
;
379 host_raddr
= shmat(shmid
, g2h_untagged(mmap_start
),
383 if (host_raddr
== (void *)-1) {
384 return get_errno(-1);
386 raddr
= h2g(host_raddr
);
388 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
- 1,
389 PAGE_VALID
| PAGE_RESET
| PAGE_READ
|
390 (shmflg
& SHM_RDONLY
? 0 : PAGE_WRITE
));
392 for (int i
= 0; i
< N_BSD_SHM_REGIONS
; i
++) {
393 if (bsd_shm_regions
[i
].start
== 0) {
394 bsd_shm_regions
[i
].start
= raddr
;
395 bsd_shm_regions
[i
].size
= shm_info
.shm_segsz
;
405 static inline abi_long
do_bsd_shmdt(abi_ulong shmaddr
)
409 WITH_MMAP_LOCK_GUARD() {
412 for (i
= 0; i
< N_BSD_SHM_REGIONS
; ++i
) {
413 if (bsd_shm_regions
[i
].start
== shmaddr
) {
418 if (i
== N_BSD_SHM_REGIONS
) {
419 return -TARGET_EINVAL
;
422 ret
= get_errno(shmdt(g2h_untagged(shmaddr
)));
424 abi_ulong size
= bsd_shm_regions
[i
].size
;
426 bsd_shm_regions
[i
].start
= 0;
427 page_set_flags(shmaddr
, shmaddr
+ size
- 1, 0);
428 mmap_reserve(shmaddr
, size
);
435 static inline abi_long
do_bsd_vadvise(void)
437 /* See sys_ovadvise() in vm_unix.c */
438 return -TARGET_EINVAL
;
441 static inline abi_long
do_bsd_sbrk(void)
443 /* see sys_sbrk() in vm_mmap.c */
444 return -TARGET_EOPNOTSUPP
;
447 static inline abi_long
do_bsd_sstk(void)
449 /* see sys_sstk() in vm_mmap.c */
450 return -TARGET_EOPNOTSUPP
;
453 #endif /* BSD_USER_BSD_MEM_H */