| 1 | /* |
| 2 | * memory management system call shims and definitions |
| 3 | * |
| 4 | * Copyright (c) 2013-2015 Stacey D. Son |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Copyright (c) 1982, 1986, 1993 |
| 11 | * The Regents of the University of California. All rights reserved. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in the |
| 20 | * documentation and/or other materials provided with the distribution. |
| 21 | * 4. Neither the name of the University nor the names of its contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 | * SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef BSD_USER_BSD_MEM_H |
| 39 | #define BSD_USER_BSD_MEM_H |
| 40 | |
| 41 | #include <sys/ipc.h> |
| 42 | #include <sys/shm.h> |
| 43 | |
| 44 | #include "qemu-bsd.h" |
| 45 | #include "exec/mmap-lock.h" |
| 46 | #include "exec/page-protection.h" |
| 47 | #include "user/page-protection.h" |
| 48 | |
| 49 | extern struct bsd_shm_regions bsd_shm_regions[]; |
| 50 | extern abi_ulong target_brk; |
| 51 | extern abi_ulong initial_target_brk; |
| 52 | |
| 53 | /* mmap(2) */ |
| 54 | static inline abi_long do_bsd_mmap(void *cpu_env, abi_long arg1, abi_long arg2, |
| 55 | abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7, |
| 56 | abi_long arg8) |
| 57 | { |
| 58 | if (regpairs_aligned(cpu_env) != 0) { |
| 59 | arg6 = arg7; |
| 60 | arg7 = arg8; |
| 61 | } |
| 62 | return get_errno(target_mmap(arg1, arg2, arg3, |
| 63 | target_to_host_bitmask(arg4, mmap_flags_tbl), |
| 64 | arg5, target_arg64(arg6, arg7))); |
| 65 | } |
| 66 | |
| 67 | /* munmap(2) */ |
| 68 | static inline abi_long do_bsd_munmap(abi_long arg1, abi_long arg2) |
| 69 | { |
| 70 | return get_errno(target_munmap(arg1, arg2)); |
| 71 | } |
| 72 | |
| 73 | /* mprotect(2) */ |
| 74 | static inline abi_long do_bsd_mprotect(abi_long arg1, abi_long arg2, |
| 75 | abi_long arg3) |
| 76 | { |
| 77 | return get_errno(target_mprotect(arg1, arg2, arg3)); |
| 78 | } |
| 79 | |
| 80 | /* msync(2) */ |
| 81 | static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long flags) |
| 82 | { |
| 83 | if (!guest_range_valid_untagged(addr, len)) { |
| 84 | /* It seems odd, but POSIX wants this to be ENOMEM */ |
| 85 | return -TARGET_ENOMEM; |
| 86 | } |
| 87 | |
| 88 | return get_errno(msync(g2h_untagged(addr), len, flags)); |
| 89 | } |
| 90 | |
| 91 | /* mlock(2) */ |
| 92 | static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2) |
| 93 | { |
| 94 | if (!guest_range_valid_untagged(arg1, arg2)) { |
| 95 | return -TARGET_EINVAL; |
| 96 | } |
| 97 | return get_errno(mlock(g2h_untagged(arg1), arg2)); |
| 98 | } |
| 99 | |
| 100 | /* munlock(2) */ |
| 101 | static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2) |
| 102 | { |
| 103 | if (!guest_range_valid_untagged(arg1, arg2)) { |
| 104 | return -TARGET_EINVAL; |
| 105 | } |
| 106 | return get_errno(munlock(g2h_untagged(arg1), arg2)); |
| 107 | } |
| 108 | |
| 109 | /* mlockall(2) */ |
| 110 | static inline abi_long do_bsd_mlockall(abi_long arg1) |
| 111 | { |
| 112 | return get_errno(mlockall(arg1)); |
| 113 | } |
| 114 | |
| 115 | /* munlockall(2) */ |
| 116 | static inline abi_long do_bsd_munlockall(void) |
| 117 | { |
| 118 | return get_errno(munlockall()); |
| 119 | } |
| 120 | |
| 121 | /* madvise(2) */ |
| 122 | static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2, |
| 123 | abi_long arg3) |
| 124 | { |
| 125 | abi_ulong len; |
| 126 | int ret = 0; |
| 127 | abi_long start = arg1; |
| 128 | abi_long len_in = arg2; |
| 129 | abi_long advice = arg3; |
| 130 | |
| 131 | if (start & ~TARGET_PAGE_MASK) { |
| 132 | return -TARGET_EINVAL; |
| 133 | } |
| 134 | if (len_in == 0) { |
| 135 | return 0; |
| 136 | } |
| 137 | len = TARGET_PAGE_ALIGN(len_in); |
| 138 | if (len == 0 || !guest_range_valid_untagged(start, len)) { |
| 139 | return -TARGET_EINVAL; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Most advice values are hints, so ignoring and returning success is ok. |
| 144 | * |
| 145 | * However, some advice values such as MADV_DONTNEED, are not hints and |
| 146 | * need to be emulated. |
| 147 | * |
| 148 | * A straight passthrough for those may not be safe because qemu sometimes |
| 149 | * turns private file-backed mappings into anonymous mappings. |
| 150 | * If all guest pages have PAGE_PASSTHROUGH set, mappings have the |
| 151 | * same semantics for the host as for the guest. |
| 152 | * |
| 153 | * MADV_DONTNEED is passed through, if possible. |
| 154 | * If passthrough isn't possible, we nevertheless (wrongly!) return |
| 155 | * success, which is broken but some userspace programs fail to work |
| 156 | * otherwise. Completely implementing such emulation is quite complicated |
| 157 | * though. |
| 158 | */ |
| 159 | mmap_lock(); |
| 160 | switch (advice) { |
| 161 | case MADV_DONTNEED: |
| 162 | if (page_check_range(start, len, PAGE_PASSTHROUGH)) { |
| 163 | ret = get_errno(madvise(g2h_untagged(start), len, advice)); |
| 164 | if (ret == 0) { |
| 165 | page_reset_target_data(start, start + len - 1); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | mmap_unlock(); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | /* minherit(2) */ |
| 175 | static inline abi_long do_bsd_minherit(abi_long addr, abi_long len, |
| 176 | abi_long inherit) |
| 177 | { |
| 178 | return get_errno(minherit(g2h_untagged(addr), len, inherit)); |
| 179 | } |
| 180 | |
| 181 | /* mincore(2) */ |
| 182 | static inline abi_long do_bsd_mincore(abi_ulong target_addr, abi_ulong len, |
| 183 | abi_ulong target_vec) |
| 184 | { |
| 185 | abi_long ret; |
| 186 | void *p; |
| 187 | abi_ulong vec_len = DIV_ROUND_UP(len, TARGET_PAGE_SIZE); |
| 188 | |
| 189 | if (!guest_range_valid_untagged(target_addr, len) |
| 190 | || !page_check_range(target_addr, len, PAGE_VALID)) { |
| 191 | return -TARGET_EFAULT; |
| 192 | } |
| 193 | |
| 194 | p = lock_user(VERIFY_WRITE, target_vec, vec_len, 0); |
| 195 | if (p == NULL) { |
| 196 | return -TARGET_EFAULT; |
| 197 | } |
| 198 | ret = get_errno(mincore(g2h_untagged(target_addr), len, p)); |
| 199 | unlock_user(p, target_vec, vec_len); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /* do_brk() must return target values and target errnos. */ |
| 205 | static inline abi_long do_obreak(abi_ulong brk_val) |
| 206 | { |
| 207 | abi_long mapped_addr; |
| 208 | abi_ulong new_brk; |
| 209 | abi_ulong old_brk; |
| 210 | |
| 211 | /* brk pointers are always untagged */ |
| 212 | |
| 213 | /* do not allow to shrink below initial brk value */ |
| 214 | if (brk_val < initial_target_brk) { |
| 215 | return target_brk; |
| 216 | } |
| 217 | |
| 218 | new_brk = TARGET_PAGE_ALIGN(brk_val); |
| 219 | old_brk = TARGET_PAGE_ALIGN(target_brk); |
| 220 | |
| 221 | /* new and old target_brk might be on the same page */ |
| 222 | if (new_brk == old_brk) { |
| 223 | target_brk = brk_val; |
| 224 | return target_brk; |
| 225 | } |
| 226 | |
| 227 | /* Release heap if necessary */ |
| 228 | if (new_brk < old_brk) { |
| 229 | target_munmap(new_brk, old_brk - new_brk); |
| 230 | |
| 231 | target_brk = brk_val; |
| 232 | return target_brk; |
| 233 | } |
| 234 | |
| 235 | mapped_addr = target_mmap(old_brk, new_brk - old_brk, |
| 236 | PROT_READ | PROT_WRITE, |
| 237 | MAP_FIXED | MAP_EXCL | MAP_ANON | MAP_PRIVATE, |
| 238 | -1, 0); |
| 239 | |
| 240 | if (mapped_addr == old_brk) { |
| 241 | target_brk = brk_val; |
| 242 | return target_brk; |
| 243 | } |
| 244 | |
| 245 | /* For everything else, return the previous break. */ |
| 246 | return target_brk; |
| 247 | } |
| 248 | |
| 249 | /* shm_open(2) */ |
| 250 | static inline abi_long do_bsd_shm_open(abi_ulong arg1, abi_long arg2, |
| 251 | abi_long arg3) |
| 252 | { |
| 253 | int ret; |
| 254 | void *p; |
| 255 | |
| 256 | if (arg1 == (uintptr_t)SHM_ANON) { |
| 257 | p = SHM_ANON; |
| 258 | } else { |
| 259 | p = lock_user_string(arg1); |
| 260 | if (p == NULL) { |
| 261 | return -TARGET_EFAULT; |
| 262 | } |
| 263 | } |
| 264 | ret = get_errno(shm_open(p, target_to_host_bitmask(arg2, fcntl_flags_tbl), |
| 265 | arg3)); |
| 266 | |
| 267 | if (p != SHM_ANON) { |
| 268 | unlock_user(p, arg1, 0); |
| 269 | } |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | /* shm_unlink(2) */ |
| 275 | static inline abi_long do_bsd_shm_unlink(abi_ulong arg1) |
| 276 | { |
| 277 | int ret; |
| 278 | void *p; |
| 279 | |
| 280 | p = lock_user_string(arg1); |
| 281 | if (p == NULL) { |
| 282 | return -TARGET_EFAULT; |
| 283 | } |
| 284 | ret = get_errno(shm_unlink(p)); /* XXX path(p)? */ |
| 285 | unlock_user(p, arg1, 0); |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | /* shmget(2) */ |
| 291 | static inline abi_long do_bsd_shmget(abi_long arg1, abi_ulong arg2, |
| 292 | abi_long arg3) |
| 293 | { |
| 294 | return get_errno(shmget(arg1, arg2, arg3)); |
| 295 | } |
| 296 | |
| 297 | /* shmctl(2) */ |
| 298 | static inline abi_long do_bsd_shmctl(abi_long shmid, abi_long cmd, |
| 299 | abi_ulong buff) |
| 300 | { |
| 301 | struct shmid_ds dsarg; |
| 302 | abi_long ret = -TARGET_EINVAL; |
| 303 | |
| 304 | cmd &= 0xff; |
| 305 | |
| 306 | switch (cmd) { |
| 307 | case IPC_STAT: |
| 308 | if (target_to_host_shmid_ds(&dsarg, buff)) { |
| 309 | return -TARGET_EFAULT; |
| 310 | } |
| 311 | ret = get_errno(shmctl(shmid, cmd, &dsarg)); |
| 312 | if (host_to_target_shmid_ds(buff, &dsarg)) { |
| 313 | return -TARGET_EFAULT; |
| 314 | } |
| 315 | break; |
| 316 | |
| 317 | case IPC_SET: |
| 318 | if (target_to_host_shmid_ds(&dsarg, buff)) { |
| 319 | return -TARGET_EFAULT; |
| 320 | } |
| 321 | ret = get_errno(shmctl(shmid, cmd, &dsarg)); |
| 322 | break; |
| 323 | |
| 324 | case IPC_RMID: |
| 325 | ret = get_errno(shmctl(shmid, cmd, NULL)); |
| 326 | break; |
| 327 | |
| 328 | default: |
| 329 | ret = -TARGET_EINVAL; |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* shmat(2) */ |
| 337 | static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg) |
| 338 | { |
| 339 | abi_ulong raddr; |
| 340 | abi_long ret; |
| 341 | struct shmid_ds shm_info; |
| 342 | |
| 343 | /* Find out the length of the shared memory segment. */ |
| 344 | ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); |
| 345 | if (is_error(ret)) { |
| 346 | /* Can't get the length */ |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { |
| 351 | return -TARGET_EINVAL; |
| 352 | } |
| 353 | |
| 354 | WITH_MMAP_LOCK_GUARD() { |
| 355 | void *host_raddr; |
| 356 | |
| 357 | if (shmaddr) { |
| 358 | host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); |
| 359 | } else { |
| 360 | abi_ulong alignment; |
| 361 | abi_ulong mmap_start; |
| 362 | |
| 363 | alignment = 0; /* alignment above page size not required */ |
| 364 | mmap_start = mmap_find_vma(0, shm_info.shm_segsz, alignment); |
| 365 | |
| 366 | if (mmap_start == -1) { |
| 367 | return -TARGET_ENOMEM; |
| 368 | } |
| 369 | host_raddr = shmat(shmid, g2h_untagged(mmap_start), |
| 370 | shmflg | SHM_REMAP); |
| 371 | } |
| 372 | |
| 373 | if (host_raddr == (void *)-1) { |
| 374 | return get_errno(-1); |
| 375 | } |
| 376 | raddr = h2g(host_raddr); |
| 377 | |
| 378 | page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, |
| 379 | PAGE_VALID | PAGE_READ | |
| 380 | (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE), |
| 381 | PAGE_VALID); |
| 382 | |
| 383 | for (int i = 0; i < N_BSD_SHM_REGIONS; i++) { |
| 384 | if (bsd_shm_regions[i].start == 0) { |
| 385 | bsd_shm_regions[i].start = raddr; |
| 386 | bsd_shm_regions[i].size = shm_info.shm_segsz; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return raddr; |
| 393 | } |
| 394 | |
| 395 | /* shmdt(2) */ |
| 396 | static inline abi_long do_bsd_shmdt(abi_ulong shmaddr) |
| 397 | { |
| 398 | abi_long ret; |
| 399 | |
| 400 | WITH_MMAP_LOCK_GUARD() { |
| 401 | int i; |
| 402 | |
| 403 | for (i = 0; i < N_BSD_SHM_REGIONS; ++i) { |
| 404 | if (bsd_shm_regions[i].start == shmaddr) { |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (i == N_BSD_SHM_REGIONS) { |
| 410 | return -TARGET_EINVAL; |
| 411 | } |
| 412 | |
| 413 | ret = get_errno(shmdt(g2h_untagged(shmaddr))); |
| 414 | if (ret == 0) { |
| 415 | abi_ulong size = bsd_shm_regions[i].size; |
| 416 | |
| 417 | bsd_shm_regions[i].start = 0; |
| 418 | page_set_flags(shmaddr, shmaddr + size - 1, 0, PAGE_VALID); |
| 419 | mmap_reserve(shmaddr, size); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static inline abi_long do_bsd_vadvise(void) |
| 427 | { |
| 428 | /* See sys_ovadvise() in vm_unix.c */ |
| 429 | return -TARGET_EINVAL; |
| 430 | } |
| 431 | #endif /* BSD_USER_BSD_MEM_H */ |