1 /* 2 * memory management system call shims and definitions 3 * 4 * Copyright (c) 2013-15 Stacey D. Son 5 * 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. 10 * 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. 15 * 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/>. 18 */ 19 20 /* 21 * Copyright (c) 1982, 1986, 1993 22 * The Regents of the University of California. All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 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. 35 * 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 46 * SUCH DAMAGE. 47 */ 48 49 #ifndef BSD_USER_BSD_MEM_H 50 #define BSD_USER_BSD_MEM_H 51 52 #include <sys/types.h> 53 #include <sys/ipc.h> 54 #include <sys/mman.h> 55 #include <sys/shm.h> 56 #include <fcntl.h> 57 58 #include "qemu-bsd.h" 59 60 extern struct bsd_shm_regions bsd_shm_regions[]; 61 extern abi_ulong target_brk; 62 extern abi_ulong initial_target_brk; 63 64 /* mmap(2) */ 65 static inline abi_long do_bsd_mmap(void *cpu_env, abi_long arg1, abi_long arg2, 66 abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7, 67 abi_long arg8) 68 { 69 if (regpairs_aligned(cpu_env) != 0) { 70 arg6 = arg7; 71 arg7 = arg8; 72 } 73 return get_errno(target_mmap(arg1, arg2, arg3, 74 target_to_host_bitmask(arg4, mmap_flags_tbl), 75 arg5, target_arg64(arg6, arg7))); 76 } 77 78 /* munmap(2) */ 79 static inline abi_long do_bsd_munmap(abi_long arg1, abi_long arg2) 80 { 81 return get_errno(target_munmap(arg1, arg2)); 82 } 83 84 /* mprotect(2) */ 85 static inline abi_long do_bsd_mprotect(abi_long arg1, abi_long arg2, 86 abi_long arg3) 87 { 88 return get_errno(target_mprotect(arg1, arg2, arg3)); 89 } 90 91 /* msync(2) */ 92 static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long flags) 93 { 94 if (!guest_range_valid_untagged(addr, len)) { 95 /* It seems odd, but POSIX wants this to be ENOMEM */ 96 return -TARGET_ENOMEM; 97 } 98 99 return get_errno(msync(g2h_untagged(addr), len, flags)); 100 } 101 102 /* mlock(2) */ 103 static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2) 104 { 105 if (!guest_range_valid_untagged(arg1, arg2)) { 106 return -TARGET_EINVAL; 107 } 108 return get_errno(mlock(g2h_untagged(arg1), arg2)); 109 } 110 111 /* munlock(2) */ 112 static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2) 113 { 114 if (!guest_range_valid_untagged(arg1, arg2)) { 115 return -TARGET_EINVAL; 116 } 117 return get_errno(munlock(g2h_untagged(arg1), arg2)); 118 } 119 120 /* mlockall(2) */ 121 static inline abi_long do_bsd_mlockall(abi_long arg1) 122 { 123 return get_errno(mlockall(arg1)); 124 } 125 126 /* munlockall(2) */ 127 static inline abi_long do_bsd_munlockall(void) 128 { 129 return get_errno(munlockall()); 130 } 131 132 /* minherit(2) */ 133 static inline abi_long do_bsd_minherit(abi_long addr, abi_long len, 134 abi_long inherit) 135 { 136 return get_errno(minherit(g2h_untagged(addr), len, inherit)); 137 } 138 139 #endif /* BSD_USER_BSD_MEM_H */ 140