xref: /qemu/bsd-user/bsd-mem.h (revision 9bfba08a)
16765e988SStacey Son /*
26765e988SStacey Son  *  memory management system call shims and definitions
36765e988SStacey Son  *
46765e988SStacey Son  *  Copyright (c) 2013-15 Stacey D. Son
56765e988SStacey Son  *
66765e988SStacey Son  *  This program is free software; you can redistribute it and/or modify
76765e988SStacey Son  *  it under the terms of the GNU General Public License as published by
86765e988SStacey Son  *  the Free Software Foundation; either version 2 of the License, or
96765e988SStacey Son  *  (at your option) any later version.
106765e988SStacey Son  *
116765e988SStacey Son  *  This program is distributed in the hope that it will be useful,
126765e988SStacey Son  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
136765e988SStacey Son  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
146765e988SStacey Son  *  GNU General Public License for more details.
156765e988SStacey Son  *
166765e988SStacey Son  *  You should have received a copy of the GNU General Public License
176765e988SStacey Son  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
186765e988SStacey Son  */
196765e988SStacey Son 
206765e988SStacey Son /*
216765e988SStacey Son  * Copyright (c) 1982, 1986, 1993
226765e988SStacey Son  *      The Regents of the University of California.  All rights reserved.
236765e988SStacey Son  *
246765e988SStacey Son  * Redistribution and use in source and binary forms, with or without
256765e988SStacey Son  * modification, are permitted provided that the following conditions
266765e988SStacey Son  * are met:
276765e988SStacey Son  * 1. Redistributions of source code must retain the above copyright
286765e988SStacey Son  *    notice, this list of conditions and the following disclaimer.
296765e988SStacey Son  * 2. Redistributions in binary form must reproduce the above copyright
306765e988SStacey Son  *    notice, this list of conditions and the following disclaimer in the
316765e988SStacey Son  *    documentation and/or other materials provided with the distribution.
326765e988SStacey Son  * 4. Neither the name of the University nor the names of its contributors
336765e988SStacey Son  *    may be used to endorse or promote products derived from this software
346765e988SStacey Son  *    without specific prior written permission.
356765e988SStacey Son  *
366765e988SStacey Son  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
376765e988SStacey Son  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
386765e988SStacey Son  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
396765e988SStacey Son  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
406765e988SStacey Son  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
416765e988SStacey Son  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
426765e988SStacey Son  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
436765e988SStacey Son  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
446765e988SStacey Son  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
456765e988SStacey Son  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
466765e988SStacey Son  * SUCH DAMAGE.
476765e988SStacey Son  */
486765e988SStacey Son 
496765e988SStacey Son #ifndef BSD_USER_BSD_MEM_H
506765e988SStacey Son #define BSD_USER_BSD_MEM_H
516765e988SStacey Son 
526765e988SStacey Son #include <sys/types.h>
536765e988SStacey Son #include <sys/ipc.h>
546765e988SStacey Son #include <sys/mman.h>
556765e988SStacey Son #include <sys/shm.h>
566765e988SStacey Son #include <fcntl.h>
576765e988SStacey Son 
586765e988SStacey Son #include "qemu-bsd.h"
596765e988SStacey Son 
606765e988SStacey Son extern struct bsd_shm_regions bsd_shm_regions[];
616765e988SStacey Son extern abi_ulong target_brk;
626765e988SStacey Son extern abi_ulong initial_target_brk;
636765e988SStacey Son 
6487dcb4adSStacey Son /* mmap(2) */
do_bsd_mmap(void * cpu_env,abi_long arg1,abi_long arg2,abi_long arg3,abi_long arg4,abi_long arg5,abi_long arg6,abi_long arg7,abi_long arg8)6587dcb4adSStacey Son static inline abi_long do_bsd_mmap(void *cpu_env, abi_long arg1, abi_long arg2,
6687dcb4adSStacey Son     abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7,
6787dcb4adSStacey Son     abi_long arg8)
6887dcb4adSStacey Son {
6987dcb4adSStacey Son     if (regpairs_aligned(cpu_env) != 0) {
7087dcb4adSStacey Son         arg6 = arg7;
7187dcb4adSStacey Son         arg7 = arg8;
7287dcb4adSStacey Son     }
7387dcb4adSStacey Son     return get_errno(target_mmap(arg1, arg2, arg3,
7487dcb4adSStacey Son                                  target_to_host_bitmask(arg4, mmap_flags_tbl),
7587dcb4adSStacey Son                                  arg5, target_arg64(arg6, arg7)));
7687dcb4adSStacey Son }
7787dcb4adSStacey Son 
7887dcb4adSStacey Son /* munmap(2) */
do_bsd_munmap(abi_long arg1,abi_long arg2)7987dcb4adSStacey Son static inline abi_long do_bsd_munmap(abi_long arg1, abi_long arg2)
8087dcb4adSStacey Son {
8187dcb4adSStacey Son     return get_errno(target_munmap(arg1, arg2));
8287dcb4adSStacey Son }
8387dcb4adSStacey Son 
84ecbe2249SStacey Son /* mprotect(2) */
do_bsd_mprotect(abi_long arg1,abi_long arg2,abi_long arg3)85ecbe2249SStacey Son static inline abi_long do_bsd_mprotect(abi_long arg1, abi_long arg2,
86ecbe2249SStacey Son         abi_long arg3)
87ecbe2249SStacey Son {
88ecbe2249SStacey Son     return get_errno(target_mprotect(arg1, arg2, arg3));
89ecbe2249SStacey Son }
90ecbe2249SStacey Son 
91f28a1e4bSStacey Son /* msync(2) */
do_bsd_msync(abi_long addr,abi_long len,abi_long flags)92f28a1e4bSStacey Son static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long flags)
93f28a1e4bSStacey Son {
94f28a1e4bSStacey Son     if (!guest_range_valid_untagged(addr, len)) {
95f28a1e4bSStacey Son         /* It seems odd, but POSIX wants this to be ENOMEM */
96f28a1e4bSStacey Son         return -TARGET_ENOMEM;
97f28a1e4bSStacey Son     }
98f28a1e4bSStacey Son 
99f28a1e4bSStacey Son     return get_errno(msync(g2h_untagged(addr), len, flags));
100f28a1e4bSStacey Son }
101f28a1e4bSStacey Son 
1020a49ef02SStacey Son /* mlock(2) */
do_bsd_mlock(abi_long arg1,abi_long arg2)1030a49ef02SStacey Son static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
1040a49ef02SStacey Son {
1050a49ef02SStacey Son     if (!guest_range_valid_untagged(arg1, arg2)) {
1060a49ef02SStacey Son         return -TARGET_EINVAL;
1070a49ef02SStacey Son     }
1080a49ef02SStacey Son     return get_errno(mlock(g2h_untagged(arg1), arg2));
1090a49ef02SStacey Son }
1100a49ef02SStacey Son 
1110a49ef02SStacey Son /* munlock(2) */
do_bsd_munlock(abi_long arg1,abi_long arg2)1120a49ef02SStacey Son static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
1130a49ef02SStacey Son {
1140a49ef02SStacey Son     if (!guest_range_valid_untagged(arg1, arg2)) {
1150a49ef02SStacey Son         return -TARGET_EINVAL;
1160a49ef02SStacey Son     }
1170a49ef02SStacey Son     return get_errno(munlock(g2h_untagged(arg1), arg2));
1180a49ef02SStacey Son }
1190a49ef02SStacey Son 
1200a49ef02SStacey Son /* mlockall(2) */
do_bsd_mlockall(abi_long arg1)1210a49ef02SStacey Son static inline abi_long do_bsd_mlockall(abi_long arg1)
1220a49ef02SStacey Son {
1230a49ef02SStacey Son     return get_errno(mlockall(arg1));
1240a49ef02SStacey Son }
1250a49ef02SStacey Son 
1260a49ef02SStacey Son /* munlockall(2) */
do_bsd_munlockall(void)1270a49ef02SStacey Son static inline abi_long do_bsd_munlockall(void)
1280a49ef02SStacey Son {
1290a49ef02SStacey Son     return get_errno(munlockall());
1300a49ef02SStacey Son }
1310a49ef02SStacey Son 
1320c1ced42SKarim Taha /* madvise(2) */
do_bsd_madvise(abi_long arg1,abi_long arg2,abi_long arg3)1330c1ced42SKarim Taha static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
1340c1ced42SKarim Taha         abi_long arg3)
1350c1ced42SKarim Taha {
1360c1ced42SKarim Taha     abi_ulong len;
1370c1ced42SKarim Taha     int ret = 0;
1380c1ced42SKarim Taha     abi_long start = arg1;
1390c1ced42SKarim Taha     abi_long len_in = arg2;
1400c1ced42SKarim Taha     abi_long advice = arg3;
1410c1ced42SKarim Taha 
1420c1ced42SKarim Taha     if (start & ~TARGET_PAGE_MASK) {
1430c1ced42SKarim Taha         return -TARGET_EINVAL;
1440c1ced42SKarim Taha     }
1450c1ced42SKarim Taha     if (len_in == 0) {
1460c1ced42SKarim Taha         return 0;
1470c1ced42SKarim Taha     }
1480c1ced42SKarim Taha     len = TARGET_PAGE_ALIGN(len_in);
1490c1ced42SKarim Taha     if (len == 0 || !guest_range_valid_untagged(start, len)) {
1500c1ced42SKarim Taha         return -TARGET_EINVAL;
1510c1ced42SKarim Taha     }
1520c1ced42SKarim Taha 
1530c1ced42SKarim Taha     /*
1540c1ced42SKarim Taha      * Most advice values are hints, so ignoring and returning success is ok.
1550c1ced42SKarim Taha      *
1560c1ced42SKarim Taha      * However, some advice values such as MADV_DONTNEED, are not hints and
1570c1ced42SKarim Taha      * need to be emulated.
1580c1ced42SKarim Taha      *
1590c1ced42SKarim Taha      * A straight passthrough for those may not be safe because qemu sometimes
1600c1ced42SKarim Taha      * turns private file-backed mappings into anonymous mappings.
1610c1ced42SKarim Taha      * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
1620c1ced42SKarim Taha      * same semantics for the host as for the guest.
1630c1ced42SKarim Taha      *
1640c1ced42SKarim Taha      * MADV_DONTNEED is passed through, if possible.
1650c1ced42SKarim Taha      * If passthrough isn't possible, we nevertheless (wrongly!) return
1660c1ced42SKarim Taha      * success, which is broken but some userspace programs fail to work
1670c1ced42SKarim Taha      * otherwise. Completely implementing such emulation is quite complicated
1680c1ced42SKarim Taha      * though.
1690c1ced42SKarim Taha      */
1700c1ced42SKarim Taha     mmap_lock();
1710c1ced42SKarim Taha     switch (advice) {
1720c1ced42SKarim Taha     case MADV_DONTNEED:
1730c1ced42SKarim Taha         if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
1740c1ced42SKarim Taha             ret = get_errno(madvise(g2h_untagged(start), len, advice));
1750c1ced42SKarim Taha             if (ret == 0) {
1760c1ced42SKarim Taha                 page_reset_target_data(start, start + len - 1);
1770c1ced42SKarim Taha             }
1780c1ced42SKarim Taha         }
1790c1ced42SKarim Taha     }
1800c1ced42SKarim Taha     mmap_unlock();
1810c1ced42SKarim Taha 
1820c1ced42SKarim Taha     return ret;
1830c1ced42SKarim Taha }
1840c1ced42SKarim Taha 
1850a49ef02SStacey Son /* minherit(2) */
do_bsd_minherit(abi_long addr,abi_long len,abi_long inherit)1860a49ef02SStacey Son static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
1870a49ef02SStacey Son         abi_long inherit)
1880a49ef02SStacey Son {
1890a49ef02SStacey Son     return get_errno(minherit(g2h_untagged(addr), len, inherit));
1900a49ef02SStacey Son }
1910a49ef02SStacey Son 
19283b045adSStacey Son /* mincore(2) */
do_bsd_mincore(abi_ulong target_addr,abi_ulong len,abi_ulong target_vec)19383b045adSStacey Son static inline abi_long do_bsd_mincore(abi_ulong target_addr, abi_ulong len,
19483b045adSStacey Son         abi_ulong target_vec)
19583b045adSStacey Son {
19683b045adSStacey Son     abi_long ret;
19783b045adSStacey Son     void *p;
19883b045adSStacey Son     abi_ulong vec_len = DIV_ROUND_UP(len, TARGET_PAGE_SIZE);
19983b045adSStacey Son 
20083b045adSStacey Son     if (!guest_range_valid_untagged(target_addr, len)
20183b045adSStacey Son         || !page_check_range(target_addr, len, PAGE_VALID)) {
20283b045adSStacey Son         return -TARGET_EFAULT;
20383b045adSStacey Son     }
20483b045adSStacey Son 
20583b045adSStacey Son     p = lock_user(VERIFY_WRITE, target_vec, vec_len, 0);
20683b045adSStacey Son     if (p == NULL) {
20783b045adSStacey Son         return -TARGET_EFAULT;
20883b045adSStacey Son     }
20983b045adSStacey Son     ret = get_errno(mincore(g2h_untagged(target_addr), len, p));
21083b045adSStacey Son     unlock_user(p, target_vec, vec_len);
21183b045adSStacey Son 
21283b045adSStacey Son     return ret;
21383b045adSStacey Son }
21483b045adSStacey Son 
215a99d7403SStacey Son /* do_brk() must return target values and target errnos. */
do_obreak(abi_ulong brk_val)216a99d7403SStacey Son static inline abi_long do_obreak(abi_ulong brk_val)
217a99d7403SStacey Son {
218a99d7403SStacey Son     abi_long mapped_addr;
219a99d7403SStacey Son     abi_ulong new_brk;
220a99d7403SStacey Son     abi_ulong old_brk;
221a99d7403SStacey Son 
222a99d7403SStacey Son     /* brk pointers are always untagged */
223a99d7403SStacey Son 
224a99d7403SStacey Son     /* do not allow to shrink below initial brk value */
225a99d7403SStacey Son     if (brk_val < initial_target_brk) {
226a99d7403SStacey Son         return target_brk;
227a99d7403SStacey Son     }
228a99d7403SStacey Son 
229a99d7403SStacey Son     new_brk = TARGET_PAGE_ALIGN(brk_val);
230a99d7403SStacey Son     old_brk = TARGET_PAGE_ALIGN(target_brk);
231a99d7403SStacey Son 
232a99d7403SStacey Son     /* new and old target_brk might be on the same page */
233a99d7403SStacey Son     if (new_brk == old_brk) {
234a99d7403SStacey Son         target_brk = brk_val;
235a99d7403SStacey Son         return target_brk;
236a99d7403SStacey Son     }
237a99d7403SStacey Son 
2389bfba08aSMichael Tokarev     /* Release heap if necessary */
239a99d7403SStacey Son     if (new_brk < old_brk) {
240a99d7403SStacey Son         target_munmap(new_brk, old_brk - new_brk);
241a99d7403SStacey Son 
242a99d7403SStacey Son         target_brk = brk_val;
243a99d7403SStacey Son         return target_brk;
244a99d7403SStacey Son     }
245a99d7403SStacey Son 
246a99d7403SStacey Son     mapped_addr = target_mmap(old_brk, new_brk - old_brk,
247a99d7403SStacey Son                               PROT_READ | PROT_WRITE,
248a99d7403SStacey Son                               MAP_FIXED | MAP_EXCL | MAP_ANON | MAP_PRIVATE,
249a99d7403SStacey Son                               -1, 0);
250a99d7403SStacey Son 
251a99d7403SStacey Son     if (mapped_addr == old_brk) {
252a99d7403SStacey Son         target_brk = brk_val;
253a99d7403SStacey Son         return target_brk;
254a99d7403SStacey Son     }
255a99d7403SStacey Son 
256a99d7403SStacey Son     /* For everything else, return the previous break. */
257a99d7403SStacey Son     return target_brk;
258a99d7403SStacey Son }
259a99d7403SStacey Son 
2604f0be683SStacey Son /* shm_open(2) */
do_bsd_shm_open(abi_ulong arg1,abi_long arg2,abi_long arg3)2614f0be683SStacey Son static inline abi_long do_bsd_shm_open(abi_ulong arg1, abi_long arg2,
2624f0be683SStacey Son         abi_long arg3)
2634f0be683SStacey Son {
2644f0be683SStacey Son     int ret;
2654f0be683SStacey Son     void *p;
2664f0be683SStacey Son 
2674f0be683SStacey Son     if (arg1 == (uintptr_t)SHM_ANON) {
2684f0be683SStacey Son         p = SHM_ANON;
2694f0be683SStacey Son     } else {
2704f0be683SStacey Son         p = lock_user_string(arg1);
2714f0be683SStacey Son         if (p == NULL) {
2724f0be683SStacey Son             return -TARGET_EFAULT;
2734f0be683SStacey Son         }
2744f0be683SStacey Son     }
2754f0be683SStacey Son     ret = get_errno(shm_open(p, target_to_host_bitmask(arg2, fcntl_flags_tbl),
2764f0be683SStacey Son                              arg3));
2774f0be683SStacey Son 
2784f0be683SStacey Son     if (p != SHM_ANON) {
2794f0be683SStacey Son         unlock_user(p, arg1, 0);
2804f0be683SStacey Son     }
2814f0be683SStacey Son 
2824f0be683SStacey Son     return ret;
2834f0be683SStacey Son }
2844f0be683SStacey Son 
2859d14db15SStacey Son /* shm_unlink(2) */
do_bsd_shm_unlink(abi_ulong arg1)2869d14db15SStacey Son static inline abi_long do_bsd_shm_unlink(abi_ulong arg1)
2879d14db15SStacey Son {
2889d14db15SStacey Son     int ret;
2899d14db15SStacey Son     void *p;
2909d14db15SStacey Son 
2919d14db15SStacey Son     p = lock_user_string(arg1);
2929d14db15SStacey Son     if (p == NULL) {
2939d14db15SStacey Son         return -TARGET_EFAULT;
2949d14db15SStacey Son     }
2959d14db15SStacey Son     ret = get_errno(shm_unlink(p)); /* XXX path(p)? */
2969d14db15SStacey Son     unlock_user(p, arg1, 0);
2979d14db15SStacey Son 
2989d14db15SStacey Son     return ret;
2999d14db15SStacey Son }
3009d14db15SStacey Son 
3019d14db15SStacey Son /* shmget(2) */
do_bsd_shmget(abi_long arg1,abi_ulong arg2,abi_long arg3)3029d14db15SStacey Son static inline abi_long do_bsd_shmget(abi_long arg1, abi_ulong arg2,
3039d14db15SStacey Son         abi_long arg3)
3049d14db15SStacey Son {
3059d14db15SStacey Son     return get_errno(shmget(arg1, arg2, arg3));
3069d14db15SStacey Son }
3079d14db15SStacey Son 
308f9bbe3cfSStacey Son /* shmctl(2) */
do_bsd_shmctl(abi_long shmid,abi_long cmd,abi_ulong buff)309f9bbe3cfSStacey Son static inline abi_long do_bsd_shmctl(abi_long shmid, abi_long cmd,
310f9bbe3cfSStacey Son         abi_ulong buff)
311f9bbe3cfSStacey Son {
312f9bbe3cfSStacey Son     struct shmid_ds dsarg;
313f9bbe3cfSStacey Son     abi_long ret = -TARGET_EINVAL;
314f9bbe3cfSStacey Son 
315f9bbe3cfSStacey Son     cmd &= 0xff;
316f9bbe3cfSStacey Son 
317f9bbe3cfSStacey Son     switch (cmd) {
318f9bbe3cfSStacey Son     case IPC_STAT:
319f9bbe3cfSStacey Son         if (target_to_host_shmid_ds(&dsarg, buff)) {
320f9bbe3cfSStacey Son             return -TARGET_EFAULT;
321f9bbe3cfSStacey Son         }
322f9bbe3cfSStacey Son         ret = get_errno(shmctl(shmid, cmd, &dsarg));
323f9bbe3cfSStacey Son         if (host_to_target_shmid_ds(buff, &dsarg)) {
324f9bbe3cfSStacey Son             return -TARGET_EFAULT;
325f9bbe3cfSStacey Son         }
326f9bbe3cfSStacey Son         break;
327f9bbe3cfSStacey Son 
328f9bbe3cfSStacey Son     case IPC_SET:
329f9bbe3cfSStacey Son         if (target_to_host_shmid_ds(&dsarg, buff)) {
330f9bbe3cfSStacey Son             return -TARGET_EFAULT;
331f9bbe3cfSStacey Son         }
332f9bbe3cfSStacey Son         ret = get_errno(shmctl(shmid, cmd, &dsarg));
333f9bbe3cfSStacey Son         break;
334f9bbe3cfSStacey Son 
335f9bbe3cfSStacey Son     case IPC_RMID:
336f9bbe3cfSStacey Son         ret = get_errno(shmctl(shmid, cmd, NULL));
337f9bbe3cfSStacey Son         break;
338f9bbe3cfSStacey Son 
339f9bbe3cfSStacey Son     default:
340f9bbe3cfSStacey Son         ret = -TARGET_EINVAL;
341f9bbe3cfSStacey Son         break;
342f9bbe3cfSStacey Son     }
343f9bbe3cfSStacey Son 
344f9bbe3cfSStacey Son     return ret;
345f9bbe3cfSStacey Son }
346f9bbe3cfSStacey Son 
3474e00b7d8SStacey Son /* shmat(2) */
do_bsd_shmat(int shmid,abi_ulong shmaddr,int shmflg)3484e00b7d8SStacey Son static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3494e00b7d8SStacey Son {
3504e00b7d8SStacey Son     abi_ulong raddr;
3514e00b7d8SStacey Son     abi_long ret;
3524e00b7d8SStacey Son     struct shmid_ds shm_info;
3534e00b7d8SStacey Son 
3544e00b7d8SStacey Son     /* Find out the length of the shared memory segment. */
3554e00b7d8SStacey Son     ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3564e00b7d8SStacey Son     if (is_error(ret)) {
3574e00b7d8SStacey Son         /* Can't get the length */
3584e00b7d8SStacey Son         return ret;
3594e00b7d8SStacey Son     }
3604e00b7d8SStacey Son 
3614e00b7d8SStacey Son     if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
3624e00b7d8SStacey Son         return -TARGET_EINVAL;
3634e00b7d8SStacey Son     }
3644e00b7d8SStacey Son 
3654e00b7d8SStacey Son     WITH_MMAP_LOCK_GUARD() {
3664e00b7d8SStacey Son         void *host_raddr;
3674e00b7d8SStacey Son 
3684e00b7d8SStacey Son         if (shmaddr) {
3694e00b7d8SStacey Son             host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
3704e00b7d8SStacey Son         } else {
3714e00b7d8SStacey Son             abi_ulong mmap_start;
3724e00b7d8SStacey Son 
3734e00b7d8SStacey Son             mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3744e00b7d8SStacey Son 
3754e00b7d8SStacey Son             if (mmap_start == -1) {
3764e00b7d8SStacey Son                 return -TARGET_ENOMEM;
3774e00b7d8SStacey Son             }
3784e00b7d8SStacey Son             host_raddr = shmat(shmid, g2h_untagged(mmap_start),
3794e00b7d8SStacey Son                                shmflg | SHM_REMAP);
3804e00b7d8SStacey Son         }
3814e00b7d8SStacey Son 
3824e00b7d8SStacey Son         if (host_raddr == (void *)-1) {
3834e00b7d8SStacey Son             return get_errno(-1);
3844e00b7d8SStacey Son         }
3854e00b7d8SStacey Son         raddr = h2g(host_raddr);
3864e00b7d8SStacey Son 
3874e00b7d8SStacey Son         page_set_flags(raddr, raddr + shm_info.shm_segsz - 1,
3884e00b7d8SStacey Son                        PAGE_VALID | PAGE_RESET | PAGE_READ |
3894e00b7d8SStacey Son                        (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
3904e00b7d8SStacey Son 
3914e00b7d8SStacey Son         for (int i = 0; i < N_BSD_SHM_REGIONS; i++) {
3924e00b7d8SStacey Son             if (bsd_shm_regions[i].start == 0) {
3934e00b7d8SStacey Son                 bsd_shm_regions[i].start = raddr;
3944e00b7d8SStacey Son                 bsd_shm_regions[i].size = shm_info.shm_segsz;
3954e00b7d8SStacey Son                 break;
3964e00b7d8SStacey Son             }
3974e00b7d8SStacey Son         }
3984e00b7d8SStacey Son     }
3994e00b7d8SStacey Son 
4004e00b7d8SStacey Son     return raddr;
4014e00b7d8SStacey Son }
4024e00b7d8SStacey Son 
4034e00b7d8SStacey Son /* shmdt(2) */
do_bsd_shmdt(abi_ulong shmaddr)4044e00b7d8SStacey Son static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
4054e00b7d8SStacey Son {
4064e00b7d8SStacey Son     abi_long ret;
4074e00b7d8SStacey Son 
4084e00b7d8SStacey Son     WITH_MMAP_LOCK_GUARD() {
4094e00b7d8SStacey Son         int i;
4104e00b7d8SStacey Son 
4114e00b7d8SStacey Son         for (i = 0; i < N_BSD_SHM_REGIONS; ++i) {
4124e00b7d8SStacey Son             if (bsd_shm_regions[i].start == shmaddr) {
4134e00b7d8SStacey Son                 break;
4144e00b7d8SStacey Son             }
4154e00b7d8SStacey Son         }
4164e00b7d8SStacey Son 
4174e00b7d8SStacey Son         if (i == N_BSD_SHM_REGIONS) {
4184e00b7d8SStacey Son             return -TARGET_EINVAL;
4194e00b7d8SStacey Son         }
4204e00b7d8SStacey Son 
4214e00b7d8SStacey Son         ret = get_errno(shmdt(g2h_untagged(shmaddr)));
4224e00b7d8SStacey Son         if (ret == 0) {
4234e00b7d8SStacey Son             abi_ulong size = bsd_shm_regions[i].size;
4244e00b7d8SStacey Son 
4254e00b7d8SStacey Son             bsd_shm_regions[i].start = 0;
4264e00b7d8SStacey Son             page_set_flags(shmaddr, shmaddr + size - 1, 0);
4274e00b7d8SStacey Son             mmap_reserve(shmaddr, size);
4284e00b7d8SStacey Son         }
4294e00b7d8SStacey Son     }
4304e00b7d8SStacey Son 
4314e00b7d8SStacey Son     return ret;
4324e00b7d8SStacey Son }
4334e00b7d8SStacey Son 
do_bsd_vadvise(void)434dfa1d915SWarner Losh static inline abi_long do_bsd_vadvise(void)
435dfa1d915SWarner Losh {
436dfa1d915SWarner Losh     /* See sys_ovadvise() in vm_unix.c */
437dfa1d915SWarner Losh     return -TARGET_EINVAL;
438dfa1d915SWarner Losh }
439dfa1d915SWarner Losh 
do_bsd_sbrk(void)440dfa1d915SWarner Losh static inline abi_long do_bsd_sbrk(void)
441dfa1d915SWarner Losh {
442dfa1d915SWarner Losh     /* see sys_sbrk() in vm_mmap.c */
443dfa1d915SWarner Losh     return -TARGET_EOPNOTSUPP;
444dfa1d915SWarner Losh }
445dfa1d915SWarner Losh 
do_bsd_sstk(void)446dfa1d915SWarner Losh static inline abi_long do_bsd_sstk(void)
447dfa1d915SWarner Losh {
448dfa1d915SWarner Losh     /* see sys_sstk() in vm_mmap.c */
449dfa1d915SWarner Losh     return -TARGET_EOPNOTSUPP;
450dfa1d915SWarner Losh }
451dfa1d915SWarner Losh 
4526765e988SStacey Son #endif /* BSD_USER_BSD_MEM_H */
453