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