blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mmap support for qemu |
| 3 | * |
| 4 | * Copyright (c) 2003 - 2008 Fabrice Bellard |
| 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 18 | */ |
Peter Maydell | 2231197 | 2016-01-29 17:49:53 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 20 | |
| 21 | #include "qemu.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 22 | |
Alex Bennée | 95992b6 | 2017-03-20 14:36:47 +0000 | [diff] [blame] | 23 | static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; |
Peter Maydell | 06943a6 | 2017-07-18 17:26:31 +0100 | [diff] [blame] | 24 | static __thread int mmap_lock_count; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 25 | |
| 26 | void mmap_lock(void) |
| 27 | { |
| 28 | if (mmap_lock_count++ == 0) { |
| 29 | pthread_mutex_lock(&mmap_mutex); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void mmap_unlock(void) |
| 34 | { |
Richard Henderson | 990ef91 | 2023-07-17 19:58:58 +0100 | [diff] [blame] | 35 | assert(mmap_lock_count > 0); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 36 | if (--mmap_lock_count == 0) { |
| 37 | pthread_mutex_unlock(&mmap_mutex); |
| 38 | } |
| 39 | } |
| 40 | |
Alex Bennée | 301e40e | 2016-10-27 16:10:00 +0100 | [diff] [blame] | 41 | bool have_mmap_lock(void) |
| 42 | { |
| 43 | return mmap_lock_count > 0 ? true : false; |
| 44 | } |
| 45 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 46 | /* Grab lock to make sure things are in a consistent state after fork(). */ |
| 47 | void mmap_fork_start(void) |
| 48 | { |
| 49 | if (mmap_lock_count) |
| 50 | abort(); |
| 51 | pthread_mutex_lock(&mmap_mutex); |
| 52 | } |
| 53 | |
| 54 | void mmap_fork_end(int child) |
| 55 | { |
| 56 | if (child) |
| 57 | pthread_mutex_init(&mmap_mutex, NULL); |
| 58 | else |
| 59 | pthread_mutex_unlock(&mmap_mutex); |
| 60 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 61 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 62 | /* NOTE: all the constants are the HOST ones, but addresses are target. */ |
| 63 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
| 64 | { |
| 65 | abi_ulong end, host_start, host_end, addr; |
| 66 | int prot1, ret; |
| 67 | |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 68 | qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx |
| 69 | " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len, |
| 70 | prot & PROT_READ ? 'r' : '-', |
| 71 | prot & PROT_WRITE ? 'w' : '-', |
| 72 | prot & PROT_EXEC ? 'x' : '-'); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 73 | if ((start & ~TARGET_PAGE_MASK) != 0) |
| 74 | return -EINVAL; |
| 75 | len = TARGET_PAGE_ALIGN(len); |
| 76 | end = start + len; |
| 77 | if (end < start) |
| 78 | return -EINVAL; |
| 79 | prot &= PROT_READ | PROT_WRITE | PROT_EXEC; |
| 80 | if (len == 0) |
| 81 | return 0; |
| 82 | |
| 83 | mmap_lock(); |
| 84 | host_start = start & qemu_host_page_mask; |
| 85 | host_end = HOST_PAGE_ALIGN(end); |
| 86 | if (start > host_start) { |
| 87 | /* handle host page containing start */ |
| 88 | prot1 = prot; |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 89 | for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 90 | prot1 |= page_get_flags(addr); |
| 91 | } |
| 92 | if (host_end == host_start + qemu_host_page_size) { |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 93 | for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 94 | prot1 |= page_get_flags(addr); |
| 95 | } |
| 96 | end = host_end; |
| 97 | } |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 98 | ret = mprotect(g2h_untagged(host_start), |
| 99 | qemu_host_page_size, prot1 & PAGE_BITS); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 100 | if (ret != 0) |
| 101 | goto error; |
| 102 | host_start += qemu_host_page_size; |
| 103 | } |
| 104 | if (end < host_end) { |
| 105 | prot1 = prot; |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 106 | for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 107 | prot1 |= page_get_flags(addr); |
| 108 | } |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 109 | ret = mprotect(g2h_untagged(host_end - qemu_host_page_size), |
| 110 | qemu_host_page_size, prot1 & PAGE_BITS); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 111 | if (ret != 0) |
| 112 | goto error; |
| 113 | host_end -= qemu_host_page_size; |
| 114 | } |
| 115 | |
| 116 | /* handle the pages in the middle */ |
| 117 | if (host_start < host_end) { |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 118 | ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 119 | if (ret != 0) |
| 120 | goto error; |
| 121 | } |
Richard Henderson | 49840a4 | 2023-03-06 01:51:09 +0300 | [diff] [blame] | 122 | page_set_flags(start, start + len - 1, prot | PAGE_VALID); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 123 | mmap_unlock(); |
| 124 | return 0; |
| 125 | error: |
| 126 | mmap_unlock(); |
| 127 | return ret; |
| 128 | } |
| 129 | |
Warner Losh | a6b2d06 | 2021-10-18 12:51:17 -0600 | [diff] [blame] | 130 | /* |
| 131 | * map an incomplete host page |
| 132 | * |
| 133 | * mmap_frag can be called with a valid fd, if flags doesn't contain one of |
| 134 | * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we |
| 135 | * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be |
| 136 | * added. |
| 137 | * |
| 138 | * * If fd is valid (not -1) we want to map the pages with MAP_ANON. |
| 139 | * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it |
| 140 | * will be rejected. See kern_mmap's enforcing of constraints for MAP_GUARD |
| 141 | * in sys/vm/vm_mmap.c. |
| 142 | * * If flags contains MAP_ANON it doesn't matter if we add it or not. |
| 143 | * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't |
| 144 | * matter if we add it or not either. See enforcing of constraints for |
| 145 | * MAP_STACK in kern_mmap. |
| 146 | * |
| 147 | * Don't add MAP_ANON for the flags that use fd == -1 without specifying the |
| 148 | * flags directly, with the assumption that future flags that require fd == -1 |
| 149 | * will also not require MAP_ANON. |
| 150 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 151 | static int mmap_frag(abi_ulong real_start, |
| 152 | abi_ulong start, abi_ulong end, |
| 153 | int prot, int flags, int fd, abi_ulong offset) |
| 154 | { |
| 155 | abi_ulong real_end, addr; |
| 156 | void *host_start; |
| 157 | int prot1, prot_new; |
| 158 | |
| 159 | real_end = real_start + qemu_host_page_size; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 160 | host_start = g2h_untagged(real_start); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 161 | |
| 162 | /* get the protection of the target pages outside the mapping */ |
| 163 | prot1 = 0; |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 164 | for (addr = real_start; addr < real_end; addr++) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 165 | if (addr < start || addr >= end) |
| 166 | prot1 |= page_get_flags(addr); |
| 167 | } |
| 168 | |
| 169 | if (prot1 == 0) { |
Warner Losh | a6b2d06 | 2021-10-18 12:51:17 -0600 | [diff] [blame] | 170 | /* no page was there, so we allocate one. See also above. */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 171 | void *p = mmap(host_start, qemu_host_page_size, prot, |
Warner Losh | a6b2d06 | 2021-10-18 12:51:17 -0600 | [diff] [blame] | 172 | flags | ((fd != -1) ? MAP_ANON : 0), -1, 0); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 173 | if (p == MAP_FAILED) |
| 174 | return -1; |
| 175 | prot1 = prot; |
| 176 | } |
| 177 | prot1 &= PAGE_BITS; |
| 178 | |
| 179 | prot_new = prot | prot1; |
Warner Losh | a6b2d06 | 2021-10-18 12:51:17 -0600 | [diff] [blame] | 180 | if (fd != -1) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 181 | /* msync() won't work here, so we return an error if write is |
| 182 | possible while it is a shared mapping */ |
blueswir1 | 6c173b3 | 2008-11-29 14:05:16 +0000 | [diff] [blame] | 183 | if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 184 | (prot & PROT_WRITE)) |
Blue Swirl | 059bca4 | 2010-01-31 13:41:07 +0000 | [diff] [blame] | 185 | return -1; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 186 | |
| 187 | /* adjust protection to be able to read */ |
| 188 | if (!(prot1 & PROT_WRITE)) |
| 189 | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
| 190 | |
| 191 | /* read the corresponding file data */ |
Mikaël Urankar | 26778ac | 2021-09-16 17:45:05 -0600 | [diff] [blame] | 192 | if (pread(fd, g2h_untagged(start), end - start, offset) == -1) { |
| 193 | return -1; |
| 194 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 195 | |
| 196 | /* put final protection */ |
| 197 | if (prot_new != (prot1 | PROT_WRITE)) |
| 198 | mprotect(host_start, qemu_host_page_size, prot_new); |
| 199 | } else { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 200 | if (prot_new != prot1) { |
| 201 | mprotect(host_start, qemu_host_page_size, prot_new); |
| 202 | } |
Mikaël Urankar | 948516a | 2017-07-08 13:13:31 +0200 | [diff] [blame] | 203 | if (prot_new & PROT_WRITE) { |
| 204 | memset(g2h_untagged(start), 0, end - start); |
| 205 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 210 | #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 |
| 211 | # define TASK_UNMAPPED_BASE (1ul << 38) |
| 212 | #else |
| 213 | # define TASK_UNMAPPED_BASE 0x40000000 |
| 214 | #endif |
| 215 | abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 216 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 217 | /* |
| 218 | * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest |
| 219 | * address space. |
| 220 | */ |
| 221 | static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, |
| 222 | abi_ulong alignment) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 223 | { |
Richard Henderson | f12294b | 2023-07-07 21:40:45 +0100 | [diff] [blame] | 224 | abi_ulong ret; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 225 | |
Richard Henderson | f12294b | 2023-07-07 21:40:45 +0100 | [diff] [blame] | 226 | ret = page_find_range_empty(start, reserved_va, size, alignment); |
| 227 | if (ret == -1 && start > TARGET_PAGE_SIZE) { |
| 228 | /* Restart at the beginning of the address space. */ |
| 229 | ret = page_find_range_empty(TARGET_PAGE_SIZE, start - 1, |
| 230 | size, alignment); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 231 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 232 | |
Richard Henderson | f12294b | 2023-07-07 21:40:45 +0100 | [diff] [blame] | 233 | return ret; |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Find and reserve a free memory area of size 'size'. The search |
| 238 | * starts at 'start'. |
| 239 | * It must be called with mmap_lock() held. |
| 240 | * Return -1 if error. |
| 241 | */ |
| 242 | static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, |
| 243 | abi_ulong alignment) |
| 244 | { |
| 245 | void *ptr, *prev; |
| 246 | abi_ulong addr; |
| 247 | int flags; |
| 248 | int wrapped, repeat; |
| 249 | |
| 250 | /* If 'start' == 0, then a default start address is used. */ |
| 251 | if (start == 0) { |
| 252 | start = mmap_next_start; |
| 253 | } else { |
| 254 | start &= qemu_host_page_mask; |
| 255 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 256 | |
| 257 | size = HOST_PAGE_ALIGN(size); |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 258 | |
| 259 | if (reserved_va) { |
| 260 | return mmap_find_vma_reserved(start, size, |
Warner Losh | 0f2f324 | 2023-07-28 10:29:27 -0600 | [diff] [blame] | 261 | (alignment != 0 ? 1 << alignment : |
| 262 | MAX(qemu_host_page_size, TARGET_PAGE_SIZE))); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 263 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 264 | |
| 265 | addr = start; |
| 266 | wrapped = repeat = 0; |
| 267 | prev = 0; |
Warner Losh | 953b69c | 2021-09-16 18:45:28 -0600 | [diff] [blame] | 268 | flags = MAP_ANON | MAP_PRIVATE; |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 269 | if (alignment != 0) { |
| 270 | flags |= MAP_ALIGNED(alignment); |
| 271 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 272 | |
| 273 | for (;; prev = ptr) { |
| 274 | /* |
| 275 | * Reserve needed memory area to avoid a race. |
| 276 | * It should be discarded using: |
| 277 | * - mmap() with MAP_FIXED flag |
| 278 | * - mremap() with MREMAP_FIXED flag |
| 279 | * - shmat() with SHM_REMAP flag |
| 280 | */ |
| 281 | ptr = mmap(g2h_untagged(addr), size, PROT_NONE, |
| 282 | flags, -1, 0); |
| 283 | |
| 284 | /* ENOMEM, if host address space has no memory */ |
| 285 | if (ptr == MAP_FAILED) { |
| 286 | return (abi_ulong)-1; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Count the number of sequential returns of the same address. |
| 291 | * This is used to modify the search algorithm below. |
| 292 | */ |
| 293 | repeat = (ptr == prev ? repeat + 1 : 0); |
| 294 | |
| 295 | if (h2g_valid(ptr + size - 1)) { |
| 296 | addr = h2g(ptr); |
| 297 | |
| 298 | if ((addr & ~TARGET_PAGE_MASK) == 0) { |
| 299 | /* Success. */ |
| 300 | if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) { |
| 301 | mmap_next_start = addr + size; |
| 302 | } |
| 303 | return addr; |
| 304 | } |
| 305 | |
| 306 | /* The address is not properly aligned for the target. */ |
| 307 | switch (repeat) { |
| 308 | case 0: |
| 309 | /* |
| 310 | * Assume the result that the kernel gave us is the |
| 311 | * first with enough free space, so start again at the |
| 312 | * next higher target page. |
| 313 | */ |
| 314 | addr = TARGET_PAGE_ALIGN(addr); |
| 315 | break; |
| 316 | case 1: |
| 317 | /* |
| 318 | * Sometimes the kernel decides to perform the allocation |
| 319 | * at the top end of memory instead. |
| 320 | */ |
| 321 | addr &= TARGET_PAGE_MASK; |
| 322 | break; |
| 323 | case 2: |
| 324 | /* Start over at low memory. */ |
| 325 | addr = 0; |
| 326 | break; |
| 327 | default: |
| 328 | /* Fail. This unaligned block must the last. */ |
| 329 | addr = -1; |
| 330 | break; |
| 331 | } |
| 332 | } else { |
| 333 | /* |
| 334 | * Since the result the kernel gave didn't fit, start |
| 335 | * again at low memory. If any repetition, fail. |
| 336 | */ |
| 337 | addr = (repeat ? -1 : 0); |
| 338 | } |
| 339 | |
| 340 | /* Unmap and try again. */ |
| 341 | munmap(ptr, size); |
| 342 | |
| 343 | /* ENOMEM if we checked the whole of the target address space. */ |
| 344 | if (addr == (abi_ulong)-1) { |
| 345 | return (abi_ulong)-1; |
| 346 | } else if (addr == 0) { |
| 347 | if (wrapped) { |
| 348 | return (abi_ulong)-1; |
| 349 | } |
| 350 | wrapped = 1; |
| 351 | /* |
| 352 | * Don't actually use 0 when wrapping, instead indicate |
| 353 | * that we'd truly like an allocation in low memory. |
| 354 | */ |
| 355 | addr = TARGET_PAGE_SIZE; |
| 356 | } else if (wrapped && addr >= start) { |
| 357 | return (abi_ulong)-1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) |
| 363 | { |
| 364 | return mmap_find_vma_aligned(start, size, 0); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* NOTE: all the constants are the HOST ones */ |
| 368 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 369 | int flags, int fd, off_t offset) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 370 | { |
| 371 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 372 | |
| 373 | mmap_lock(); |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 374 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
| 375 | qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx |
| 376 | " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", |
| 377 | start, len, |
| 378 | prot & PROT_READ ? 'r' : '-', |
| 379 | prot & PROT_WRITE ? 'w' : '-', |
| 380 | prot & PROT_EXEC ? 'x' : '-'); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 381 | if (flags & MAP_ALIGNMENT_MASK) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 382 | qemu_log("MAP_ALIGNED(%u) ", |
| 383 | (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 384 | } |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 385 | if (flags & MAP_GUARD) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 386 | qemu_log("MAP_GUARD "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 387 | } |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 388 | if (flags & MAP_FIXED) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 389 | qemu_log("MAP_FIXED "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 390 | } |
Warner Losh | 953b69c | 2021-09-16 18:45:28 -0600 | [diff] [blame] | 391 | if (flags & MAP_ANON) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 392 | qemu_log("MAP_ANON "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 393 | } |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 394 | if (flags & MAP_EXCL) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 395 | qemu_log("MAP_EXCL "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 396 | } |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 397 | if (flags & MAP_PRIVATE) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 398 | qemu_log("MAP_PRIVATE "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 399 | } |
| 400 | if (flags & MAP_SHARED) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 401 | qemu_log("MAP_SHARED "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 402 | } |
| 403 | if (flags & MAP_NOCORE) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 404 | qemu_log("MAP_NOCORE "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 405 | } |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 406 | if (flags & MAP_STACK) { |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 407 | qemu_log("MAP_STACK "); |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 408 | } |
Warner Losh | 45b8765 | 2021-09-16 18:47:19 -0600 | [diff] [blame] | 409 | qemu_log("fd=%d offset=0x%lx\n", fd, offset); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 410 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 411 | |
Warner Losh | 953b69c | 2021-09-16 18:45:28 -0600 | [diff] [blame] | 412 | if ((flags & MAP_ANON) && fd != -1) { |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 413 | errno = EINVAL; |
| 414 | goto fail; |
| 415 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 416 | if (flags & MAP_STACK) { |
| 417 | if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) != |
| 418 | (PROT_READ | PROT_WRITE))) { |
| 419 | errno = EINVAL; |
| 420 | goto fail; |
| 421 | } |
| 422 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 423 | if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || |
| 424 | offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | |
| 425 | /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */ |
| 426 | MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) { |
| 427 | errno = EINVAL; |
| 428 | goto fail; |
| 429 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 430 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 431 | if (offset & ~TARGET_PAGE_MASK) { |
| 432 | errno = EINVAL; |
| 433 | goto fail; |
| 434 | } |
| 435 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 436 | if (len == 0) { |
| 437 | errno = EINVAL; |
| 438 | goto fail; |
| 439 | } |
Warner Losh | 14837a3 | 2021-09-16 18:43:01 -0600 | [diff] [blame] | 440 | |
| 441 | /* Check for overflows */ |
| 442 | len = TARGET_PAGE_ALIGN(len); |
| 443 | if (len == 0) { |
| 444 | errno = ENOMEM; |
| 445 | goto fail; |
| 446 | } |
| 447 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 448 | real_start = start & qemu_host_page_mask; |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 449 | host_offset = offset & qemu_host_page_mask; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 450 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 451 | /* |
| 452 | * If the user is asking for the kernel to find a location, do that |
| 453 | * before we truncate the length for mapping files below. |
| 454 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 455 | if (!(flags & MAP_FIXED)) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 456 | host_len = len + offset - host_offset; |
| 457 | host_len = HOST_PAGE_ALIGN(host_len); |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 458 | if ((flags & MAP_ALIGNMENT_MASK) != 0) |
| 459 | start = mmap_find_vma_aligned(real_start, host_len, |
| 460 | (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT); |
| 461 | else |
| 462 | start = mmap_find_vma(real_start, host_len); |
| 463 | if (start == (abi_ulong)-1) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 464 | errno = ENOMEM; |
| 465 | goto fail; |
| 466 | } |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* |
| 470 | * When mapping files into a memory area larger than the file, accesses |
| 471 | * to pages beyond the file size will cause a SIGBUS. |
| 472 | * |
| 473 | * For example, if mmaping a file of 100 bytes on a host with 4K pages |
| 474 | * emulating a target with 8K pages, the target expects to be able to |
| 475 | * access the first 8K. But the host will trap us on any access beyond |
| 476 | * 4K. |
| 477 | * |
| 478 | * When emulating a target with a larger page-size than the hosts, we |
| 479 | * may need to truncate file maps at EOF and add extra anonymous pages |
| 480 | * up to the targets page boundary. |
| 481 | */ |
| 482 | |
Marc-André Lureau | 8e3b0cb | 2022-03-23 19:57:22 +0400 | [diff] [blame] | 483 | if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) { |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 484 | struct stat sb; |
| 485 | |
| 486 | if (fstat(fd, &sb) == -1) { |
| 487 | goto fail; |
| 488 | } |
| 489 | |
| 490 | /* Are we trying to create a map beyond EOF?. */ |
| 491 | if (offset + len > sb.st_size) { |
| 492 | /* |
| 493 | * If so, truncate the file map at eof aligned with |
| 494 | * the hosts real pagesize. Additional anonymous maps |
| 495 | * will be created beyond EOF. |
| 496 | */ |
| 497 | len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (!(flags & MAP_FIXED)) { |
| 502 | unsigned long host_start; |
| 503 | void *p; |
| 504 | |
| 505 | host_len = len + offset - host_offset; |
| 506 | host_len = HOST_PAGE_ALIGN(host_len); |
| 507 | |
| 508 | /* |
| 509 | * Note: we prefer to control the mapping address. It is |
| 510 | * especially important if qemu_host_page_size > |
| 511 | * qemu_real_host_page_size |
| 512 | */ |
| 513 | p = mmap(g2h_untagged(start), host_len, prot, |
Warner Losh | 953b69c | 2021-09-16 18:45:28 -0600 | [diff] [blame] | 514 | flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 515 | if (p == MAP_FAILED) |
| 516 | goto fail; |
| 517 | /* update start so that it points to the file position at 'offset' */ |
| 518 | host_start = (unsigned long)p; |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 519 | if (fd != -1) { |
| 520 | p = mmap(g2h_untagged(start), len, prot, |
| 521 | flags | MAP_FIXED, fd, host_offset); |
| 522 | if (p == MAP_FAILED) { |
| 523 | munmap(g2h_untagged(start), host_len); |
| 524 | goto fail; |
| 525 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 526 | host_start += offset - host_offset; |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 527 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 528 | start = h2g(host_start); |
| 529 | } else { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 530 | if (start & ~TARGET_PAGE_MASK) { |
| 531 | errno = EINVAL; |
| 532 | goto fail; |
| 533 | } |
| 534 | end = start + len; |
| 535 | real_end = HOST_PAGE_ALIGN(end); |
| 536 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 537 | /* |
| 538 | * Test if requested memory area fits target address space |
| 539 | * It can fail only on 64-bit host with 32-bit target. |
| 540 | * On any other target/host host mmap() handles this error correctly. |
| 541 | */ |
Kyle Evans | 0fc76b6 | 2018-11-08 14:39:47 -0600 | [diff] [blame] | 542 | if (!guest_range_valid_untagged(start, len)) { |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 543 | errno = EINVAL; |
| 544 | goto fail; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 547 | /* |
| 548 | * worst case: we cannot map the file because the offset is not |
| 549 | * aligned, so we read it |
| 550 | */ |
Warner Losh | a6b2d06 | 2021-10-18 12:51:17 -0600 | [diff] [blame] | 551 | if (fd != -1 && |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 552 | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 553 | /* |
| 554 | * msync() won't work here, so we return an error if write is |
| 555 | * possible while it is a shared mapping |
| 556 | */ |
blueswir1 | 6c173b3 | 2008-11-29 14:05:16 +0000 | [diff] [blame] | 557 | if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 558 | (prot & PROT_WRITE)) { |
| 559 | errno = EINVAL; |
| 560 | goto fail; |
| 561 | } |
| 562 | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
| 563 | MAP_FIXED | MAP_PRIVATE | MAP_ANON, |
| 564 | -1, 0); |
| 565 | if (retaddr == -1) |
| 566 | goto fail; |
Mikaël Urankar | 26778ac | 2021-09-16 17:45:05 -0600 | [diff] [blame] | 567 | if (pread(fd, g2h_untagged(start), len, offset) == -1) { |
| 568 | goto fail; |
| 569 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 570 | if (!(prot & PROT_WRITE)) { |
| 571 | ret = target_mprotect(start, len, prot); |
Warner Losh | 91a5add | 2021-09-17 09:16:54 -0600 | [diff] [blame] | 572 | assert(ret == 0); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 573 | } |
| 574 | goto the_end; |
| 575 | } |
| 576 | |
Kyle Evans | 0fc76b6 | 2018-11-08 14:39:47 -0600 | [diff] [blame] | 577 | /* Reject the mapping if any page within the range is mapped */ |
Richard Henderson | 9c255cb | 2023-07-07 21:40:38 +0100 | [diff] [blame] | 578 | if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) { |
Kyle Evans | 0fc76b6 | 2018-11-08 14:39:47 -0600 | [diff] [blame] | 579 | errno = EINVAL; |
| 580 | goto fail; |
| 581 | } |
| 582 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 583 | /* handle the start of the mapping */ |
| 584 | if (start > real_start) { |
| 585 | if (real_end == real_start + qemu_host_page_size) { |
| 586 | /* one single host page */ |
| 587 | ret = mmap_frag(real_start, start, end, |
| 588 | prot, flags, fd, offset); |
| 589 | if (ret == -1) |
| 590 | goto fail; |
| 591 | goto the_end1; |
| 592 | } |
| 593 | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
| 594 | prot, flags, fd, offset); |
| 595 | if (ret == -1) |
| 596 | goto fail; |
| 597 | real_start += qemu_host_page_size; |
| 598 | } |
| 599 | /* handle the end of the mapping */ |
| 600 | if (end < real_end) { |
| 601 | ret = mmap_frag(real_end - qemu_host_page_size, |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 602 | real_end - qemu_host_page_size, end, |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 603 | prot, flags, fd, |
| 604 | offset + real_end - qemu_host_page_size - start); |
| 605 | if (ret == -1) |
| 606 | goto fail; |
| 607 | real_end -= qemu_host_page_size; |
| 608 | } |
| 609 | |
| 610 | /* map the middle (easier) */ |
| 611 | if (real_start < real_end) { |
| 612 | void *p; |
| 613 | unsigned long offset1; |
| 614 | if (flags & MAP_ANON) |
| 615 | offset1 = 0; |
| 616 | else |
| 617 | offset1 = offset + real_start - start; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 618 | p = mmap(g2h_untagged(real_start), real_end - real_start, |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 619 | prot, flags, fd, offset1); |
| 620 | if (p == MAP_FAILED) |
| 621 | goto fail; |
| 622 | } |
| 623 | } |
| 624 | the_end1: |
Richard Henderson | 49840a4 | 2023-03-06 01:51:09 +0300 | [diff] [blame] | 625 | page_set_flags(start, start + len - 1, prot | PAGE_VALID); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 626 | the_end: |
| 627 | #ifdef DEBUG_MMAP |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 628 | printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 629 | page_dump(stdout); |
| 630 | printf("\n"); |
| 631 | #endif |
| 632 | mmap_unlock(); |
| 633 | return start; |
| 634 | fail: |
| 635 | mmap_unlock(); |
| 636 | return -1; |
| 637 | } |
| 638 | |
Stacey Son | 4e00b7d | 2023-09-25 21:27:08 +0300 | [diff] [blame] | 639 | void mmap_reserve(abi_ulong start, abi_ulong size) |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 640 | { |
| 641 | abi_ulong real_start; |
| 642 | abi_ulong real_end; |
| 643 | abi_ulong addr; |
| 644 | abi_ulong end; |
| 645 | int prot; |
| 646 | |
| 647 | real_start = start & qemu_host_page_mask; |
| 648 | real_end = HOST_PAGE_ALIGN(start + size); |
| 649 | end = start + size; |
| 650 | if (start > real_start) { |
| 651 | /* handle host page containing start */ |
| 652 | prot = 0; |
| 653 | for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { |
| 654 | prot |= page_get_flags(addr); |
| 655 | } |
| 656 | if (real_end == real_start + qemu_host_page_size) { |
| 657 | for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { |
| 658 | prot |= page_get_flags(addr); |
| 659 | } |
| 660 | end = real_end; |
| 661 | } |
| 662 | if (prot != 0) { |
| 663 | real_start += qemu_host_page_size; |
| 664 | } |
| 665 | } |
| 666 | if (end < real_end) { |
| 667 | prot = 0; |
| 668 | for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { |
| 669 | prot |= page_get_flags(addr); |
| 670 | } |
| 671 | if (prot != 0) { |
| 672 | real_end -= qemu_host_page_size; |
| 673 | } |
| 674 | } |
| 675 | if (real_start != real_end) { |
| 676 | mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE, |
Warner Losh | 953b69c | 2021-09-16 18:45:28 -0600 | [diff] [blame] | 677 | MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 681 | int target_munmap(abi_ulong start, abi_ulong len) |
| 682 | { |
| 683 | abi_ulong end, real_start, real_end, addr; |
| 684 | int prot, ret; |
| 685 | |
| 686 | #ifdef DEBUG_MMAP |
Warner Losh | 6a3b9bf | 2021-08-05 15:31:21 -0600 | [diff] [blame] | 687 | printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" |
| 688 | TARGET_ABI_FMT_lx "\n", |
| 689 | start, len); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 690 | #endif |
| 691 | if (start & ~TARGET_PAGE_MASK) |
| 692 | return -EINVAL; |
| 693 | len = TARGET_PAGE_ALIGN(len); |
| 694 | if (len == 0) |
| 695 | return -EINVAL; |
| 696 | mmap_lock(); |
| 697 | end = start + len; |
| 698 | real_start = start & qemu_host_page_mask; |
| 699 | real_end = HOST_PAGE_ALIGN(end); |
| 700 | |
| 701 | if (start > real_start) { |
| 702 | /* handle host page containing start */ |
| 703 | prot = 0; |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 704 | for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 705 | prot |= page_get_flags(addr); |
| 706 | } |
| 707 | if (real_end == real_start + qemu_host_page_size) { |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 708 | for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 709 | prot |= page_get_flags(addr); |
| 710 | } |
| 711 | end = real_end; |
| 712 | } |
| 713 | if (prot != 0) |
| 714 | real_start += qemu_host_page_size; |
| 715 | } |
| 716 | if (end < real_end) { |
| 717 | prot = 0; |
Warner Losh | 5be1d0b | 2021-04-23 09:05:57 -0600 | [diff] [blame] | 718 | for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 719 | prot |= page_get_flags(addr); |
| 720 | } |
| 721 | if (prot != 0) |
| 722 | real_end -= qemu_host_page_size; |
| 723 | } |
| 724 | |
| 725 | ret = 0; |
| 726 | /* unmap what we can */ |
| 727 | if (real_start < real_end) { |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 728 | if (reserved_va) { |
| 729 | mmap_reserve(real_start, real_end - real_start); |
| 730 | } else { |
| 731 | ret = munmap(g2h_untagged(real_start), real_end - real_start); |
| 732 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 735 | if (ret == 0) { |
Richard Henderson | 49840a4 | 2023-03-06 01:51:09 +0300 | [diff] [blame] | 736 | page_set_flags(start, start + len - 1, 0); |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 737 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 738 | mmap_unlock(); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
| 743 | { |
| 744 | abi_ulong end; |
| 745 | |
| 746 | if (start & ~TARGET_PAGE_MASK) |
| 747 | return -EINVAL; |
| 748 | len = TARGET_PAGE_ALIGN(len); |
| 749 | end = start + len; |
| 750 | if (end < start) |
| 751 | return -EINVAL; |
| 752 | if (end == start) |
| 753 | return 0; |
| 754 | |
| 755 | start &= qemu_host_page_mask; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 756 | return msync(g2h_untagged(start), end - start, flags); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 757 | } |