]>
Commit | Line | Data |
---|---|---|
54936004 FB |
1 | /* |
2 | * mmap support for qemu | |
5fafdf24 | 3 | * |
54936004 FB |
4 | * Copyright (c) 2003 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 | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
54936004 | 18 | */ |
d39594e9 | 19 | #include "qemu/osdep.h" |
225a206c | 20 | #include <sys/shm.h> |
11d96056 | 21 | #include "trace.h" |
10d0d505 | 22 | #include "exec/log.h" |
74781c08 | 23 | #include "exec/page-protection.h" |
54936004 | 24 | #include "qemu.h" |
3b249d26 | 25 | #include "user-internals.h" |
5423e6d3 | 26 | #include "user-mmap.h" |
8655b4c7 | 27 | #include "target_mman.h" |
044e95c8 | 28 | #include "qemu/interval-tree.h" |
54936004 | 29 | |
5a534314 PM |
30 | #ifdef TARGET_ARM |
31 | #include "target/arm/cpu-features.h" | |
32 | #endif | |
33 | ||
1e6eec8b | 34 | static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; |
dfd3f85c | 35 | static __thread int mmap_lock_count; |
c8a706fe PB |
36 | |
37 | void mmap_lock(void) | |
38 | { | |
39 | if (mmap_lock_count++ == 0) { | |
40 | pthread_mutex_lock(&mmap_mutex); | |
41 | } | |
42 | } | |
43 | ||
44 | void mmap_unlock(void) | |
45 | { | |
990ef918 | 46 | assert(mmap_lock_count > 0); |
c8a706fe PB |
47 | if (--mmap_lock_count == 0) { |
48 | pthread_mutex_unlock(&mmap_mutex); | |
49 | } | |
50 | } | |
d5975363 | 51 | |
301e40ed AB |
52 | bool have_mmap_lock(void) |
53 | { | |
54 | return mmap_lock_count > 0 ? true : false; | |
55 | } | |
56 | ||
d5975363 PB |
57 | /* Grab lock to make sure things are in a consistent state after fork(). */ |
58 | void mmap_fork_start(void) | |
59 | { | |
60 | if (mmap_lock_count) | |
61 | abort(); | |
62 | pthread_mutex_lock(&mmap_mutex); | |
63 | } | |
64 | ||
65 | void mmap_fork_end(int child) | |
66 | { | |
2b730f79 | 67 | if (child) { |
d5975363 | 68 | pthread_mutex_init(&mmap_mutex, NULL); |
2b730f79 | 69 | } else { |
d5975363 | 70 | pthread_mutex_unlock(&mmap_mutex); |
2b730f79 | 71 | } |
d5975363 | 72 | } |
c8a706fe | 73 | |
044e95c8 RH |
74 | /* Protected by mmap_lock. */ |
75 | static IntervalTreeRoot shm_regions; | |
76 | ||
77 | static void shm_region_add(abi_ptr start, abi_ptr last) | |
78 | { | |
79 | IntervalTreeNode *i = g_new0(IntervalTreeNode, 1); | |
80 | ||
81 | i->start = start; | |
82 | i->last = last; | |
83 | interval_tree_insert(i, &shm_regions); | |
84 | } | |
85 | ||
86 | static abi_ptr shm_region_find(abi_ptr start) | |
87 | { | |
88 | IntervalTreeNode *i; | |
89 | ||
90 | for (i = interval_tree_iter_first(&shm_regions, start, start); i; | |
91 | i = interval_tree_iter_next(i, start, start)) { | |
92 | if (i->start == start) { | |
93 | return i->last; | |
94 | } | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
99 | static void shm_region_rm_complete(abi_ptr start, abi_ptr last) | |
100 | { | |
101 | IntervalTreeNode *i, *n; | |
102 | ||
103 | for (i = interval_tree_iter_first(&shm_regions, start, last); i; i = n) { | |
104 | n = interval_tree_iter_next(i, start, last); | |
105 | if (i->start >= start && i->last <= last) { | |
106 | interval_tree_remove(i, &shm_regions); | |
107 | g_free(i); | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
9dba3ca5 RH |
112 | /* |
113 | * Validate target prot bitmask. | |
114 | * Return the prot bitmask for the host in *HOST_PROT. | |
115 | * Return 0 if the target prot bitmask is invalid, otherwise | |
116 | * the internal qemu page_flags (which will include PAGE_VALID). | |
117 | */ | |
0dd55812 | 118 | static int validate_prot_to_pageflags(int prot) |
9dba3ca5 RH |
119 | { |
120 | int valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM; | |
86b7c551 | 121 | int page_flags = (prot & PAGE_RWX) | PAGE_VALID; |
9dba3ca5 | 122 | |
be5d6f48 | 123 | #ifdef TARGET_AARCH64 |
d109b46d | 124 | { |
be5d6f48 | 125 | ARMCPU *cpu = ARM_CPU(thread_cpu); |
d109b46d RH |
126 | |
127 | /* | |
128 | * The PROT_BTI bit is only accepted if the cpu supports the feature. | |
129 | * Since this is the unusual case, don't bother checking unless | |
130 | * the bit has been requested. If set and valid, record the bit | |
131 | * within QEMU's page_flags. | |
132 | */ | |
133 | if ((prot & TARGET_PROT_BTI) && cpu_isar_feature(aa64_bti, cpu)) { | |
be5d6f48 RH |
134 | valid |= TARGET_PROT_BTI; |
135 | page_flags |= PAGE_BTI; | |
136 | } | |
d109b46d RH |
137 | /* Similarly for the PROT_MTE bit. */ |
138 | if ((prot & TARGET_PROT_MTE) && cpu_isar_feature(aa64_mte, cpu)) { | |
139 | valid |= TARGET_PROT_MTE; | |
140 | page_flags |= PAGE_MTE; | |
141 | } | |
be5d6f48 | 142 | } |
4c184e70 HD |
143 | #elif defined(TARGET_HPPA) |
144 | valid |= PROT_GROWSDOWN | PROT_GROWSUP; | |
be5d6f48 RH |
145 | #endif |
146 | ||
9dba3ca5 RH |
147 | return prot & ~valid ? 0 : page_flags; |
148 | } | |
149 | ||
0dd55812 RH |
150 | /* |
151 | * For the host, we need not pass anything except read/write/exec. | |
152 | * While PROT_SEM is allowed by all hosts, it is also ignored, so | |
153 | * don't bother transforming guest bit to host bit. Any other | |
154 | * target-specific prot bits will not be understood by the host | |
155 | * and will need to be encoded into page_flags for qemu emulation. | |
156 | * | |
157 | * Pages that are executable by the guest will never be executed | |
158 | * by the host, but the host will need to be able to read them. | |
159 | */ | |
160 | static int target_to_host_prot(int prot) | |
161 | { | |
162 | return (prot & (PROT_READ | PROT_WRITE)) | | |
163 | (prot & PROT_EXEC ? PROT_READ : 0); | |
164 | } | |
165 | ||
53a5960a | 166 | /* NOTE: all the constants are the HOST ones, but addresses are target. */ |
9dba3ca5 | 167 | int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) |
54936004 | 168 | { |
621ac47d | 169 | int host_page_size = qemu_real_host_page_size(); |
7bdc1acc RH |
170 | abi_ulong starts[3]; |
171 | abi_ulong lens[3]; | |
172 | int prots[3]; | |
173 | abi_ulong host_start, host_last, last; | |
174 | int prot1, ret, page_flags, nranges; | |
54936004 | 175 | |
9dba3ca5 | 176 | trace_target_mprotect(start, len, target_prot); |
54936004 | 177 | |
9dba3ca5 | 178 | if ((start & ~TARGET_PAGE_MASK) != 0) { |
78cf3390 | 179 | return -TARGET_EINVAL; |
9dba3ca5 | 180 | } |
0dd55812 | 181 | page_flags = validate_prot_to_pageflags(target_prot); |
9dba3ca5 RH |
182 | if (!page_flags) { |
183 | return -TARGET_EINVAL; | |
184 | } | |
7bdc1acc RH |
185 | if (len == 0) { |
186 | return 0; | |
187 | } | |
54936004 | 188 | len = TARGET_PAGE_ALIGN(len); |
46b12f46 | 189 | if (!guest_range_valid_untagged(start, len)) { |
78cf3390 | 190 | return -TARGET_ENOMEM; |
ebf9a363 | 191 | } |
3b46e624 | 192 | |
7bdc1acc | 193 | last = start + len - 1; |
621ac47d | 194 | host_start = start & -host_page_size; |
b36b2b1d | 195 | host_last = ROUND_UP(last, host_page_size) - 1; |
7bdc1acc RH |
196 | nranges = 0; |
197 | ||
198 | mmap_lock(); | |
199 | ||
621ac47d | 200 | if (host_last - host_start < host_page_size) { |
7bdc1acc | 201 | /* Single host page contains all guest pages: sum the prot. */ |
0dd55812 | 202 | prot1 = target_prot; |
7bdc1acc RH |
203 | for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) { |
204 | prot1 |= page_get_flags(a); | |
54936004 | 205 | } |
7bdc1acc RH |
206 | for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) { |
207 | prot1 |= page_get_flags(a + 1); | |
d418c81e | 208 | } |
7bdc1acc | 209 | starts[nranges] = host_start; |
621ac47d | 210 | lens[nranges] = host_page_size; |
7bdc1acc RH |
211 | prots[nranges] = prot1; |
212 | nranges++; | |
213 | } else { | |
214 | if (host_start < start) { | |
215 | /* Host page contains more than one guest page: sum the prot. */ | |
216 | prot1 = target_prot; | |
217 | for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) { | |
218 | prot1 |= page_get_flags(a); | |
219 | } | |
220 | /* If the resulting sum differs, create a new range. */ | |
221 | if (prot1 != target_prot) { | |
222 | starts[nranges] = host_start; | |
621ac47d | 223 | lens[nranges] = host_page_size; |
7bdc1acc RH |
224 | prots[nranges] = prot1; |
225 | nranges++; | |
621ac47d | 226 | host_start += host_page_size; |
7bdc1acc | 227 | } |
9dba3ca5 | 228 | } |
7bdc1acc RH |
229 | |
230 | if (last < host_last) { | |
231 | /* Host page contains more than one guest page: sum the prot. */ | |
232 | prot1 = target_prot; | |
233 | for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) { | |
234 | prot1 |= page_get_flags(a + 1); | |
235 | } | |
236 | /* If the resulting sum differs, create a new range. */ | |
237 | if (prot1 != target_prot) { | |
621ac47d | 238 | host_last -= host_page_size; |
7bdc1acc | 239 | starts[nranges] = host_last + 1; |
621ac47d | 240 | lens[nranges] = host_page_size; |
7bdc1acc RH |
241 | prots[nranges] = prot1; |
242 | nranges++; | |
243 | } | |
54936004 | 244 | } |
7bdc1acc RH |
245 | |
246 | /* Create a range for the middle, if any remains. */ | |
247 | if (host_start < host_last) { | |
248 | starts[nranges] = host_start; | |
249 | lens[nranges] = host_last - host_start + 1; | |
250 | prots[nranges] = target_prot; | |
251 | nranges++; | |
9dba3ca5 | 252 | } |
54936004 | 253 | } |
3b46e624 | 254 | |
7bdc1acc RH |
255 | for (int i = 0; i < nranges; ++i) { |
256 | ret = mprotect(g2h_untagged(starts[i]), lens[i], | |
257 | target_to_host_prot(prots[i])); | |
9dba3ca5 | 258 | if (ret != 0) { |
c8a706fe | 259 | goto error; |
9dba3ca5 | 260 | } |
54936004 | 261 | } |
aa98e2d8 | 262 | |
7bdc1acc | 263 | page_set_flags(start, last, page_flags); |
aa98e2d8 IL |
264 | ret = 0; |
265 | ||
7bdc1acc | 266 | error: |
c8a706fe PB |
267 | mmap_unlock(); |
268 | return ret; | |
54936004 FB |
269 | } |
270 | ||
2952b642 RH |
271 | /* |
272 | * Perform munmap on behalf of the target, with host parameters. | |
273 | * If reserved_va, we must replace the memory reservation. | |
274 | */ | |
275 | static int do_munmap(void *addr, size_t len) | |
276 | { | |
277 | if (reserved_va) { | |
278 | void *ptr = mmap(addr, len, PROT_NONE, | |
279 | MAP_FIXED | MAP_ANONYMOUS | |
280 | | MAP_PRIVATE | MAP_NORESERVE, -1, 0); | |
281 | return ptr == addr ? 0 : -1; | |
282 | } | |
283 | return munmap(addr, len); | |
284 | } | |
285 | ||
eb5027ac RH |
286 | /* |
287 | * Map an incomplete host page. | |
288 | * | |
289 | * Here be dragons. This case will not work if there is an existing | |
290 | * overlapping host page, which is file mapped, and for which the mapping | |
291 | * is beyond the end of the file. In that case, we will see SIGBUS when | |
292 | * trying to write a portion of this page. | |
293 | * | |
294 | * FIXME: Work around this with a temporary signal handler and longjmp. | |
295 | */ | |
99982beb RH |
296 | static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, |
297 | int prot, int flags, int fd, off_t offset) | |
54936004 | 298 | { |
621ac47d | 299 | int host_page_size = qemu_real_host_page_size(); |
99982beb | 300 | abi_ulong real_last; |
53a5960a | 301 | void *host_start; |
99982beb RH |
302 | int prot_old, prot_new; |
303 | int host_prot_old, host_prot_new; | |
54936004 | 304 | |
99982beb RH |
305 | if (!(flags & MAP_ANONYMOUS) |
306 | && (flags & MAP_TYPE) == MAP_SHARED | |
307 | && (prot & PROT_WRITE)) { | |
308 | /* | |
309 | * msync() won't work with the partial page, so we return an | |
310 | * error if write is possible while it is a shared mapping. | |
311 | */ | |
312 | errno = EINVAL; | |
313 | return false; | |
314 | } | |
315 | ||
621ac47d | 316 | real_last = real_start + host_page_size - 1; |
3e8f1628 | 317 | host_start = g2h_untagged(real_start); |
54936004 | 318 | |
99982beb RH |
319 | /* Get the protection of the target pages outside the mapping. */ |
320 | prot_old = 0; | |
321 | for (abi_ulong a = real_start; a < start; a += TARGET_PAGE_SIZE) { | |
322 | prot_old |= page_get_flags(a); | |
323 | } | |
324 | for (abi_ulong a = real_last; a > last; a -= TARGET_PAGE_SIZE) { | |
325 | prot_old |= page_get_flags(a); | |
54936004 | 326 | } |
3b46e624 | 327 | |
99982beb RH |
328 | if (prot_old == 0) { |
329 | /* | |
330 | * Since !(prot_old & PAGE_VALID), there were no guest pages | |
331 | * outside of the fragment we need to map. Allocate a new host | |
332 | * page to cover, discarding whatever else may have been present. | |
333 | */ | |
621ac47d | 334 | void *p = mmap(host_start, host_page_size, |
0dd55812 | 335 | target_to_host_prot(prot), |
80210bcd | 336 | flags | MAP_ANONYMOUS, -1, 0); |
ddcdd8c4 AO |
337 | if (p != host_start) { |
338 | if (p != MAP_FAILED) { | |
3bfa271e | 339 | do_munmap(p, host_page_size); |
ddcdd8c4 AO |
340 | errno = EEXIST; |
341 | } | |
99982beb | 342 | return false; |
2b730f79 | 343 | } |
99982beb | 344 | prot_old = prot; |
54936004 | 345 | } |
99982beb | 346 | prot_new = prot | prot_old; |
54936004 | 347 | |
99982beb RH |
348 | host_prot_old = target_to_host_prot(prot_old); |
349 | host_prot_new = target_to_host_prot(prot_new); | |
3b46e624 | 350 | |
99982beb RH |
351 | /* Adjust protection to be able to write. */ |
352 | if (!(host_prot_old & PROT_WRITE)) { | |
353 | host_prot_old |= PROT_WRITE; | |
621ac47d | 354 | mprotect(host_start, host_page_size, host_prot_old); |
99982beb | 355 | } |
3b46e624 | 356 | |
99982beb RH |
357 | /* Read or zero the new guest pages. */ |
358 | if (flags & MAP_ANONYMOUS) { | |
359 | memset(g2h_untagged(start), 0, last - start + 1); | |
54936004 | 360 | } else { |
99982beb RH |
361 | if (pread(fd, g2h_untagged(start), last - start + 1, offset) == -1) { |
362 | return false; | |
e6deac9c | 363 | } |
54936004 | 364 | } |
99982beb RH |
365 | |
366 | /* Put final protection */ | |
367 | if (host_prot_new != host_prot_old) { | |
621ac47d | 368 | mprotect(host_start, host_page_size, host_prot_new); |
99982beb RH |
369 | } |
370 | return true; | |
54936004 FB |
371 | } |
372 | ||
c8fb5cf9 | 373 | abi_ulong task_unmapped_base; |
da2b71fa | 374 | abi_ulong elf_et_dyn_base; |
c8fb5cf9 | 375 | abi_ulong mmap_next_start; |
a03e2d42 | 376 | |
2b730f79 RH |
377 | /* |
378 | * Subroutine of mmap_find_vma, used when we have pre-allocated | |
379 | * a chunk of guest address space. | |
380 | */ | |
30ab9ef2 RH |
381 | static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, |
382 | abi_ulong align) | |
68a1c816 | 383 | { |
4c13048e | 384 | target_ulong ret; |
68a1c816 | 385 | |
4c13048e RH |
386 | ret = page_find_range_empty(start, reserved_va, size, align); |
387 | if (ret == -1 && start > mmap_min_addr) { | |
388 | /* Restart at the beginning of the address space. */ | |
389 | ret = page_find_range_empty(mmap_min_addr, start - 1, size, align); | |
68a1c816 PB |
390 | } |
391 | ||
4c13048e | 392 | return ret; |
68a1c816 PB |
393 | } |
394 | ||
fe3b4152 KS |
395 | /* |
396 | * Find and reserve a free memory area of size 'size'. The search | |
397 | * starts at 'start'. | |
398 | * It must be called with mmap_lock() held. | |
399 | * Return -1 if error. | |
400 | */ | |
30ab9ef2 | 401 | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) |
a03e2d42 | 402 | { |
621ac47d | 403 | int host_page_size = qemu_real_host_page_size(); |
14f24e14 | 404 | void *ptr, *prev; |
fe3b4152 | 405 | abi_ulong addr; |
14f24e14 | 406 | int wrapped, repeat; |
fe3b4152 | 407 | |
621ac47d | 408 | align = MAX(align, host_page_size); |
443b7505 | 409 | |
fe3b4152 | 410 | /* If 'start' == 0, then a default start address is used. */ |
14f24e14 | 411 | if (start == 0) { |
fe3b4152 | 412 | start = mmap_next_start; |
14f24e14 | 413 | } else { |
621ac47d | 414 | start &= -host_page_size; |
14f24e14 | 415 | } |
30ab9ef2 | 416 | start = ROUND_UP(start, align); |
b36b2b1d | 417 | size = ROUND_UP(size, host_page_size); |
fe3b4152 | 418 | |
b76f21a7 | 419 | if (reserved_va) { |
30ab9ef2 | 420 | return mmap_find_vma_reserved(start, size, align); |
68a1c816 PB |
421 | } |
422 | ||
a03e2d42 | 423 | addr = start; |
14f24e14 RH |
424 | wrapped = repeat = 0; |
425 | prev = 0; | |
fe3b4152 | 426 | |
14f24e14 | 427 | for (;; prev = ptr) { |
fe3b4152 KS |
428 | /* |
429 | * Reserve needed memory area to avoid a race. | |
430 | * It should be discarded using: | |
431 | * - mmap() with MAP_FIXED flag | |
432 | * - mremap() with MREMAP_FIXED flag | |
433 | * - shmat() with SHM_REMAP flag | |
434 | */ | |
3e8f1628 | 435 | ptr = mmap(g2h_untagged(addr), size, PROT_NONE, |
2b730f79 | 436 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0); |
fe3b4152 KS |
437 | |
438 | /* ENOMEM, if host address space has no memory */ | |
14f24e14 | 439 | if (ptr == MAP_FAILED) { |
fe3b4152 | 440 | return (abi_ulong)-1; |
14f24e14 RH |
441 | } |
442 | ||
2b730f79 RH |
443 | /* |
444 | * Count the number of sequential returns of the same address. | |
445 | * This is used to modify the search algorithm below. | |
446 | */ | |
14f24e14 RH |
447 | repeat = (ptr == prev ? repeat + 1 : 0); |
448 | ||
449 | if (h2g_valid(ptr + size - 1)) { | |
450 | addr = h2g(ptr); | |
fe3b4152 | 451 | |
30ab9ef2 | 452 | if ((addr & (align - 1)) == 0) { |
14f24e14 | 453 | /* Success. */ |
c8fb5cf9 | 454 | if (start == mmap_next_start && addr >= task_unmapped_base) { |
14f24e14 RH |
455 | mmap_next_start = addr + size; |
456 | } | |
457 | return addr; | |
458 | } | |
fe3b4152 | 459 | |
14f24e14 RH |
460 | /* The address is not properly aligned for the target. */ |
461 | switch (repeat) { | |
462 | case 0: | |
2b730f79 RH |
463 | /* |
464 | * Assume the result that the kernel gave us is the | |
465 | * first with enough free space, so start again at the | |
466 | * next higher target page. | |
467 | */ | |
30ab9ef2 | 468 | addr = ROUND_UP(addr, align); |
14f24e14 RH |
469 | break; |
470 | case 1: | |
2b730f79 RH |
471 | /* |
472 | * Sometimes the kernel decides to perform the allocation | |
473 | * at the top end of memory instead. | |
474 | */ | |
30ab9ef2 | 475 | addr &= -align; |
14f24e14 RH |
476 | break; |
477 | case 2: | |
478 | /* Start over at low memory. */ | |
479 | addr = 0; | |
480 | break; | |
481 | default: | |
482 | /* Fail. This unaligned block must the last. */ | |
483 | addr = -1; | |
484 | break; | |
485 | } | |
486 | } else { | |
2b730f79 RH |
487 | /* |
488 | * Since the result the kernel gave didn't fit, start | |
489 | * again at low memory. If any repetition, fail. | |
490 | */ | |
14f24e14 RH |
491 | addr = (repeat ? -1 : 0); |
492 | } | |
493 | ||
494 | /* Unmap and try again. */ | |
fe3b4152 | 495 | munmap(ptr, size); |
fe3b4152 | 496 | |
14f24e14 | 497 | /* ENOMEM if we checked the whole of the target address space. */ |
d0b3e4f5 | 498 | if (addr == (abi_ulong)-1) { |
a03e2d42 | 499 | return (abi_ulong)-1; |
14f24e14 RH |
500 | } else if (addr == 0) { |
501 | if (wrapped) { | |
502 | return (abi_ulong)-1; | |
503 | } | |
504 | wrapped = 1; | |
2b730f79 RH |
505 | /* |
506 | * Don't actually use 0 when wrapping, instead indicate | |
507 | * that we'd truly like an allocation in low memory. | |
508 | */ | |
14f24e14 RH |
509 | addr = (mmap_min_addr > TARGET_PAGE_SIZE |
510 | ? TARGET_PAGE_ALIGN(mmap_min_addr) | |
511 | : TARGET_PAGE_SIZE); | |
512 | } else if (wrapped && addr >= start) { | |
513 | return (abi_ulong)-1; | |
514 | } | |
a03e2d42 | 515 | } |
a03e2d42 FB |
516 | } |
517 | ||
6ecc2557 RH |
518 | /* |
519 | * Record a successful mmap within the user-exec interval tree. | |
520 | */ | |
521 | static abi_long mmap_end(abi_ulong start, abi_ulong last, | |
522 | abi_ulong passthrough_start, | |
523 | abi_ulong passthrough_last, | |
524 | int flags, int page_flags) | |
525 | { | |
526 | if (flags & MAP_ANONYMOUS) { | |
527 | page_flags |= PAGE_ANON; | |
528 | } | |
529 | page_flags |= PAGE_RESET; | |
530 | if (passthrough_start > passthrough_last) { | |
531 | page_set_flags(start, last, page_flags); | |
532 | } else { | |
533 | if (start < passthrough_start) { | |
534 | page_set_flags(start, passthrough_start - 1, page_flags); | |
535 | } | |
536 | page_set_flags(passthrough_start, passthrough_last, | |
537 | page_flags | PAGE_PASSTHROUGH); | |
538 | if (passthrough_last < last) { | |
539 | page_set_flags(passthrough_last + 1, last, page_flags); | |
540 | } | |
541 | } | |
542 | shm_region_rm_complete(start, last); | |
543 | trace_target_mmap_complete(start); | |
544 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { | |
545 | FILE *f = qemu_log_trylock(); | |
546 | if (f) { | |
547 | fprintf(f, "page layout changed following mmap\n"); | |
548 | page_dump(f); | |
549 | qemu_log_unlock(f); | |
550 | } | |
551 | } | |
552 | return start; | |
553 | } | |
554 | ||
68098de9 RH |
555 | /* |
556 | * Special case host page size == target page size, | |
557 | * where there are no edge conditions. | |
558 | */ | |
559 | static abi_long mmap_h_eq_g(abi_ulong start, abi_ulong len, | |
560 | int host_prot, int flags, int page_flags, | |
561 | int fd, off_t offset) | |
562 | { | |
563 | void *p, *want_p = g2h_untagged(start); | |
564 | abi_ulong last; | |
565 | ||
566 | p = mmap(want_p, len, host_prot, flags, fd, offset); | |
567 | if (p == MAP_FAILED) { | |
568 | return -1; | |
569 | } | |
570 | /* If the host kernel does not support MAP_FIXED_NOREPLACE, emulate. */ | |
571 | if ((flags & MAP_FIXED_NOREPLACE) && p != want_p) { | |
572 | do_munmap(p, len); | |
573 | errno = EEXIST; | |
574 | return -1; | |
575 | } | |
576 | ||
577 | start = h2g(p); | |
578 | last = start + len - 1; | |
579 | return mmap_end(start, last, start, last, flags, page_flags); | |
580 | } | |
581 | ||
8080b2f8 RH |
582 | /* |
583 | * Special case host page size < target page size. | |
584 | * | |
585 | * The two special cases are increased guest alignment, and mapping | |
586 | * past the end of a file. | |
587 | * | |
588 | * When mapping files into a memory area larger than the file, | |
589 | * accesses to pages beyond the file size will cause a SIGBUS. | |
590 | * | |
591 | * For example, if mmaping a file of 100 bytes on a host with 4K | |
592 | * pages emulating a target with 8K pages, the target expects to | |
593 | * be able to access the first 8K. But the host will trap us on | |
594 | * any access beyond 4K. | |
595 | * | |
596 | * When emulating a target with a larger page-size than the hosts, | |
597 | * we may need to truncate file maps at EOF and add extra anonymous | |
598 | * pages up to the targets page boundary. | |
599 | * | |
600 | * This workaround only works for files that do not change. | |
601 | * If the file is later extended (e.g. ftruncate), the SIGBUS | |
602 | * vanishes and the proper behaviour is that changes within the | |
603 | * anon page should be reflected in the file. | |
604 | * | |
605 | * However, this case is rather common with executable images, | |
606 | * so the workaround is important for even trivial tests, whereas | |
607 | * the mmap of of a file being extended is less common. | |
608 | */ | |
609 | static abi_long mmap_h_lt_g(abi_ulong start, abi_ulong len, int host_prot, | |
610 | int mmap_flags, int page_flags, int fd, | |
611 | off_t offset, int host_page_size) | |
612 | { | |
613 | void *p, *want_p = g2h_untagged(start); | |
614 | off_t fileend_adj = 0; | |
615 | int flags = mmap_flags; | |
616 | abi_ulong last, pass_last; | |
617 | ||
618 | if (!(flags & MAP_ANONYMOUS)) { | |
619 | struct stat sb; | |
620 | ||
621 | if (fstat(fd, &sb) == -1) { | |
622 | return -1; | |
623 | } | |
624 | if (offset >= sb.st_size) { | |
625 | /* | |
626 | * The entire map is beyond the end of the file. | |
627 | * Transform it to an anonymous mapping. | |
628 | */ | |
629 | flags |= MAP_ANONYMOUS; | |
630 | fd = -1; | |
631 | offset = 0; | |
632 | } else if (offset + len > sb.st_size) { | |
633 | /* | |
634 | * A portion of the map is beyond the end of the file. | |
635 | * Truncate the file portion of the allocation. | |
636 | */ | |
637 | fileend_adj = offset + len - sb.st_size; | |
638 | } | |
639 | } | |
640 | ||
641 | if (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) { | |
642 | if (fileend_adj) { | |
643 | p = mmap(want_p, len, host_prot, flags | MAP_ANONYMOUS, -1, 0); | |
644 | } else { | |
645 | p = mmap(want_p, len, host_prot, flags, fd, offset); | |
646 | } | |
647 | if (p != want_p) { | |
648 | if (p != MAP_FAILED) { | |
649 | /* Host does not support MAP_FIXED_NOREPLACE: emulate. */ | |
650 | do_munmap(p, len); | |
651 | errno = EEXIST; | |
652 | } | |
653 | return -1; | |
654 | } | |
655 | ||
656 | if (fileend_adj) { | |
657 | void *t = mmap(p, len - fileend_adj, host_prot, | |
658 | (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED, | |
659 | fd, offset); | |
660 | ||
661 | if (t == MAP_FAILED) { | |
662 | int save_errno = errno; | |
663 | ||
664 | /* | |
665 | * We failed a map over the top of the successful anonymous | |
666 | * mapping above. The only failure mode is running out of VMAs, | |
667 | * and there's nothing that we can do to detect that earlier. | |
668 | * If we have replaced an existing mapping with MAP_FIXED, | |
669 | * then we cannot properly recover. It's a coin toss whether | |
670 | * it would be better to exit or continue here. | |
671 | */ | |
672 | if (!(flags & MAP_FIXED_NOREPLACE) && | |
673 | !page_check_range_empty(start, start + len - 1)) { | |
674 | qemu_log("QEMU target_mmap late failure: %s", | |
675 | strerror(save_errno)); | |
676 | } | |
677 | ||
678 | do_munmap(want_p, len); | |
679 | errno = save_errno; | |
680 | return -1; | |
681 | } | |
682 | } | |
683 | } else { | |
684 | size_t host_len, part_len; | |
685 | ||
686 | /* | |
687 | * Take care to align the host memory. Perform a larger anonymous | |
688 | * allocation and extract the aligned portion. Remap the file on | |
689 | * top of that. | |
690 | */ | |
691 | host_len = len + TARGET_PAGE_SIZE - host_page_size; | |
692 | p = mmap(want_p, host_len, host_prot, flags | MAP_ANONYMOUS, -1, 0); | |
693 | if (p == MAP_FAILED) { | |
694 | return -1; | |
695 | } | |
696 | ||
697 | part_len = (uintptr_t)p & (TARGET_PAGE_SIZE - 1); | |
698 | if (part_len) { | |
699 | part_len = TARGET_PAGE_SIZE - part_len; | |
700 | do_munmap(p, part_len); | |
701 | p += part_len; | |
702 | host_len -= part_len; | |
703 | } | |
704 | if (len < host_len) { | |
705 | do_munmap(p + len, host_len - len); | |
706 | } | |
707 | ||
708 | if (!(flags & MAP_ANONYMOUS)) { | |
709 | void *t = mmap(p, len - fileend_adj, host_prot, | |
710 | flags | MAP_FIXED, fd, offset); | |
711 | ||
712 | if (t == MAP_FAILED) { | |
713 | int save_errno = errno; | |
714 | do_munmap(p, len); | |
715 | errno = save_errno; | |
716 | return -1; | |
717 | } | |
718 | } | |
719 | ||
720 | start = h2g(p); | |
721 | } | |
722 | ||
723 | last = start + len - 1; | |
724 | if (fileend_adj) { | |
725 | pass_last = ROUND_UP(last - fileend_adj, host_page_size) - 1; | |
726 | } else { | |
727 | pass_last = last; | |
728 | } | |
729 | return mmap_end(start, last, start, pass_last, mmap_flags, page_flags); | |
730 | } | |
731 | ||
eb5027ac RH |
732 | /* |
733 | * Special case host page size > target page size. | |
734 | * | |
735 | * The two special cases are address and file offsets that are valid | |
736 | * for the guest that cannot be directly represented by the host. | |
737 | */ | |
738 | static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, | |
739 | int target_prot, int host_prot, | |
740 | int flags, int page_flags, int fd, | |
741 | off_t offset, int host_page_size) | |
742 | { | |
743 | void *p, *want_p = g2h_untagged(start); | |
744 | off_t host_offset = offset & -host_page_size; | |
745 | abi_ulong last, real_start, real_last; | |
746 | bool misaligned_offset = false; | |
747 | size_t host_len; | |
748 | ||
749 | if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { | |
750 | /* | |
751 | * Adjust the offset to something representable on the host. | |
752 | */ | |
753 | host_len = len + offset - host_offset; | |
754 | p = mmap(want_p, host_len, host_prot, flags, fd, host_offset); | |
755 | if (p == MAP_FAILED) { | |
756 | return -1; | |
757 | } | |
758 | ||
759 | /* Update start to the file position at offset. */ | |
760 | p += offset - host_offset; | |
761 | ||
762 | start = h2g(p); | |
763 | last = start + len - 1; | |
764 | return mmap_end(start, last, start, last, flags, page_flags); | |
765 | } | |
766 | ||
767 | if (!(flags & MAP_ANONYMOUS)) { | |
768 | misaligned_offset = (start ^ offset) & (host_page_size - 1); | |
769 | ||
770 | /* | |
771 | * The fallback for misalignment is a private mapping + read. | |
772 | * This carries none of semantics required of MAP_SHARED. | |
773 | */ | |
774 | if (misaligned_offset && (flags & MAP_TYPE) != MAP_PRIVATE) { | |
775 | errno = EINVAL; | |
776 | return -1; | |
777 | } | |
778 | } | |
779 | ||
780 | last = start + len - 1; | |
781 | real_start = start & -host_page_size; | |
782 | real_last = ROUND_UP(last, host_page_size) - 1; | |
783 | ||
784 | /* | |
785 | * Handle the start and end of the mapping. | |
786 | */ | |
787 | if (real_start < start) { | |
788 | abi_ulong real_page_last = real_start + host_page_size - 1; | |
789 | if (last <= real_page_last) { | |
790 | /* Entire allocation a subset of one host page. */ | |
791 | if (!mmap_frag(real_start, start, last, target_prot, | |
792 | flags, fd, offset)) { | |
793 | return -1; | |
794 | } | |
795 | return mmap_end(start, last, -1, 0, flags, page_flags); | |
796 | } | |
797 | ||
798 | if (!mmap_frag(real_start, start, real_page_last, target_prot, | |
799 | flags, fd, offset)) { | |
800 | return -1; | |
801 | } | |
802 | real_start = real_page_last + 1; | |
803 | } | |
804 | ||
805 | if (last < real_last) { | |
806 | abi_ulong real_page_start = real_last - host_page_size + 1; | |
807 | if (!mmap_frag(real_page_start, real_page_start, last, | |
808 | target_prot, flags, fd, | |
809 | offset + real_page_start - start)) { | |
810 | return -1; | |
811 | } | |
812 | real_last = real_page_start - 1; | |
813 | } | |
814 | ||
815 | if (real_start > real_last) { | |
816 | return mmap_end(start, last, -1, 0, flags, page_flags); | |
817 | } | |
818 | ||
819 | /* | |
820 | * Handle the middle of the mapping. | |
821 | */ | |
822 | ||
823 | host_len = real_last - real_start + 1; | |
824 | want_p += real_start - start; | |
825 | ||
826 | if (flags & MAP_ANONYMOUS) { | |
827 | p = mmap(want_p, host_len, host_prot, flags, -1, 0); | |
828 | } else if (!misaligned_offset) { | |
829 | p = mmap(want_p, host_len, host_prot, flags, fd, | |
830 | offset + real_start - start); | |
831 | } else { | |
832 | p = mmap(want_p, host_len, host_prot | PROT_WRITE, | |
833 | flags | MAP_ANONYMOUS, -1, 0); | |
834 | } | |
835 | if (p != want_p) { | |
836 | if (p != MAP_FAILED) { | |
837 | do_munmap(p, host_len); | |
838 | errno = EEXIST; | |
839 | } | |
840 | return -1; | |
841 | } | |
842 | ||
843 | if (misaligned_offset) { | |
844 | /* TODO: The read could be short. */ | |
845 | if (pread(fd, p, host_len, offset + real_start - start) != host_len) { | |
846 | do_munmap(p, host_len); | |
847 | return -1; | |
848 | } | |
849 | if (!(host_prot & PROT_WRITE)) { | |
850 | mprotect(p, host_len, host_prot); | |
851 | } | |
852 | } | |
853 | ||
854 | return mmap_end(start, last, -1, 0, flags, page_flags); | |
855 | } | |
856 | ||
d558c395 | 857 | static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, |
e8cec51b | 858 | int target_prot, int flags, int page_flags, |
d558c395 | 859 | int fd, off_t offset) |
54936004 | 860 | { |
621ac47d | 861 | int host_page_size = qemu_real_host_page_size(); |
68098de9 | 862 | int host_prot; |
54936004 | 863 | |
2b730f79 | 864 | /* |
ad87d26e RH |
865 | * For reserved_va, we are in full control of the allocation. |
866 | * Find a suitable hole and convert to MAP_FIXED. | |
2b730f79 | 867 | */ |
68098de9 RH |
868 | if (reserved_va) { |
869 | if (flags & MAP_FIXED_NOREPLACE) { | |
870 | /* Validate that the chosen range is empty. */ | |
871 | if (!page_check_range_empty(start, start + len - 1)) { | |
872 | errno = EEXIST; | |
873 | return -1; | |
874 | } | |
875 | flags = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; | |
876 | } else if (!(flags & MAP_FIXED)) { | |
eb5027ac RH |
877 | abi_ulong real_start = start & -host_page_size; |
878 | off_t host_offset = offset & -host_page_size; | |
68098de9 RH |
879 | size_t real_len = len + offset - host_offset; |
880 | abi_ulong align = MAX(host_page_size, TARGET_PAGE_SIZE); | |
881 | ||
882 | start = mmap_find_vma(real_start, real_len, align); | |
883 | if (start == (abi_ulong)-1) { | |
884 | errno = ENOMEM; | |
885 | return -1; | |
886 | } | |
887 | start += offset - host_offset; | |
888 | flags |= MAP_FIXED; | |
a5e7ee46 | 889 | } |
68098de9 RH |
890 | } |
891 | ||
892 | host_prot = target_to_host_prot(target_prot); | |
893 | ||
894 | if (host_page_size == TARGET_PAGE_SIZE) { | |
895 | return mmap_h_eq_g(start, len, host_prot, flags, | |
896 | page_flags, fd, offset); | |
8080b2f8 RH |
897 | } else if (host_page_size < TARGET_PAGE_SIZE) { |
898 | return mmap_h_lt_g(start, len, host_prot, flags, | |
899 | page_flags, fd, offset, host_page_size); | |
a03e2d42 | 900 | } else { |
eb5027ac RH |
901 | return mmap_h_gt_g(start, len, target_prot, host_prot, flags, |
902 | page_flags, fd, offset, host_page_size); | |
54936004 | 903 | } |
d558c395 RH |
904 | } |
905 | ||
906 | /* NOTE: all the constants are the HOST ones */ | |
907 | abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, | |
908 | int flags, int fd, off_t offset) | |
909 | { | |
910 | abi_long ret; | |
e8cec51b | 911 | int page_flags; |
d558c395 RH |
912 | |
913 | trace_target_mmap(start, len, target_prot, flags, fd, offset); | |
e8cec51b RH |
914 | |
915 | if (!len) { | |
916 | errno = EINVAL; | |
917 | return -1; | |
918 | } | |
919 | ||
920 | page_flags = validate_prot_to_pageflags(target_prot); | |
921 | if (!page_flags) { | |
922 | errno = EINVAL; | |
923 | return -1; | |
924 | } | |
925 | ||
926 | /* Also check for overflows... */ | |
927 | len = TARGET_PAGE_ALIGN(len); | |
928 | if (!len || len != (size_t)len) { | |
929 | errno = ENOMEM; | |
930 | return -1; | |
931 | } | |
932 | ||
933 | if (offset & ~TARGET_PAGE_MASK) { | |
934 | errno = EINVAL; | |
935 | return -1; | |
936 | } | |
937 | if (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) { | |
938 | if (start & ~TARGET_PAGE_MASK) { | |
939 | errno = EINVAL; | |
940 | return -1; | |
941 | } | |
942 | if (!guest_range_valid_untagged(start, len)) { | |
943 | errno = ENOMEM; | |
944 | return -1; | |
945 | } | |
946 | } | |
947 | ||
d558c395 RH |
948 | mmap_lock(); |
949 | ||
e8cec51b RH |
950 | ret = target_mmap__locked(start, len, target_prot, flags, |
951 | page_flags, fd, offset); | |
d558c395 | 952 | |
c8a706fe | 953 | mmap_unlock(); |
e8cec51b RH |
954 | |
955 | /* | |
956 | * If we're mapping shared memory, ensure we generate code for parallel | |
957 | * execution and flush old translations. This will work up to the level | |
958 | * supported by the host -- anything that requires EXCP_ATOMIC will not | |
959 | * be atomic with respect to an external process. | |
960 | */ | |
961 | if (ret != -1 && (flags & MAP_TYPE) != MAP_PRIVATE) { | |
962 | CPUState *cpu = thread_cpu; | |
963 | if (!(cpu->tcg_cflags & CF_PARALLEL)) { | |
964 | cpu->tcg_cflags |= CF_PARALLEL; | |
965 | tb_flush(cpu); | |
966 | } | |
967 | } | |
968 | ||
d558c395 | 969 | return ret; |
54936004 FB |
970 | } |
971 | ||
912ff698 | 972 | static int mmap_reserve_or_unmap(abi_ulong start, abi_ulong len) |
68a1c816 | 973 | { |
621ac47d | 974 | int host_page_size = qemu_real_host_page_size(); |
68a1c816 | 975 | abi_ulong real_start; |
260561d8 RH |
976 | abi_ulong real_last; |
977 | abi_ulong real_len; | |
978 | abi_ulong last; | |
979 | abi_ulong a; | |
558a4411 | 980 | void *host_start; |
68a1c816 PB |
981 | int prot; |
982 | ||
260561d8 | 983 | last = start + len - 1; |
621ac47d | 984 | real_start = start & -host_page_size; |
b36b2b1d | 985 | real_last = ROUND_UP(last, host_page_size) - 1; |
260561d8 RH |
986 | |
987 | /* | |
988 | * If guest pages remain on the first or last host pages, | |
989 | * adjust the deallocation to retain those guest pages. | |
990 | * The single page special case is required for the last page, | |
991 | * lest real_start overflow to zero. | |
992 | */ | |
621ac47d | 993 | if (real_last - real_start < host_page_size) { |
68a1c816 | 994 | prot = 0; |
260561d8 RH |
995 | for (a = real_start; a < start; a += TARGET_PAGE_SIZE) { |
996 | prot |= page_get_flags(a); | |
68a1c816 | 997 | } |
260561d8 RH |
998 | for (a = last; a < real_last; a += TARGET_PAGE_SIZE) { |
999 | prot |= page_get_flags(a + 1); | |
1000 | } | |
1001 | if (prot != 0) { | |
912ff698 | 1002 | return 0; |
260561d8 RH |
1003 | } |
1004 | } else { | |
1005 | for (prot = 0, a = real_start; a < start; a += TARGET_PAGE_SIZE) { | |
1006 | prot |= page_get_flags(a); | |
68a1c816 | 1007 | } |
2b730f79 | 1008 | if (prot != 0) { |
621ac47d | 1009 | real_start += host_page_size; |
2b730f79 | 1010 | } |
260561d8 RH |
1011 | |
1012 | for (prot = 0, a = last; a < real_last; a += TARGET_PAGE_SIZE) { | |
1013 | prot |= page_get_flags(a + 1); | |
68a1c816 | 1014 | } |
2b730f79 | 1015 | if (prot != 0) { |
621ac47d | 1016 | real_last -= host_page_size; |
260561d8 RH |
1017 | } |
1018 | ||
1019 | if (real_last < real_start) { | |
912ff698 | 1020 | return 0; |
2b730f79 | 1021 | } |
68a1c816 | 1022 | } |
260561d8 RH |
1023 | |
1024 | real_len = real_last - real_start + 1; | |
1025 | host_start = g2h_untagged(real_start); | |
1026 | ||
2952b642 | 1027 | return do_munmap(host_start, real_len); |
68a1c816 PB |
1028 | } |
1029 | ||
992f48a0 | 1030 | int target_munmap(abi_ulong start, abi_ulong len) |
54936004 | 1031 | { |
912ff698 RH |
1032 | int ret; |
1033 | ||
b7b18d26 AB |
1034 | trace_target_munmap(start, len); |
1035 | ||
2b730f79 | 1036 | if (start & ~TARGET_PAGE_MASK) { |
912ff698 RH |
1037 | errno = EINVAL; |
1038 | return -1; | |
2b730f79 | 1039 | } |
54936004 | 1040 | len = TARGET_PAGE_ALIGN(len); |
46b12f46 | 1041 | if (len == 0 || !guest_range_valid_untagged(start, len)) { |
912ff698 RH |
1042 | errno = EINVAL; |
1043 | return -1; | |
ebf9a363 MF |
1044 | } |
1045 | ||
c8a706fe | 1046 | mmap_lock(); |
912ff698 RH |
1047 | ret = mmap_reserve_or_unmap(start, len); |
1048 | if (likely(ret == 0)) { | |
1049 | page_set_flags(start, start + len - 1, 0); | |
1050 | shm_region_rm_complete(start, start + len - 1); | |
1051 | } | |
c8a706fe | 1052 | mmap_unlock(); |
d7b0c5d0 | 1053 | |
912ff698 | 1054 | return ret; |
54936004 FB |
1055 | } |
1056 | ||
992f48a0 BS |
1057 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
1058 | abi_ulong new_size, unsigned long flags, | |
1059 | abi_ulong new_addr) | |
54936004 FB |
1060 | { |
1061 | int prot; | |
f19412a2 | 1062 | void *host_addr; |
54936004 | 1063 | |
46b12f46 | 1064 | if (!guest_range_valid_untagged(old_addr, old_size) || |
ebf9a363 | 1065 | ((flags & MREMAP_FIXED) && |
46b12f46 | 1066 | !guest_range_valid_untagged(new_addr, new_size)) || |
ccc5ccc1 | 1067 | ((flags & MREMAP_MAYMOVE) == 0 && |
46b12f46 | 1068 | !guest_range_valid_untagged(old_addr, new_size))) { |
ebf9a363 MF |
1069 | errno = ENOMEM; |
1070 | return -1; | |
1071 | } | |
1072 | ||
c8a706fe | 1073 | mmap_lock(); |
f19412a2 | 1074 | |
68a1c816 | 1075 | if (flags & MREMAP_FIXED) { |
3e8f1628 RH |
1076 | host_addr = mremap(g2h_untagged(old_addr), old_size, new_size, |
1077 | flags, g2h_untagged(new_addr)); | |
68a1c816 | 1078 | |
b76f21a7 | 1079 | if (reserved_va && host_addr != MAP_FAILED) { |
2b730f79 RH |
1080 | /* |
1081 | * If new and old addresses overlap then the above mremap will | |
1082 | * already have failed with EINVAL. | |
1083 | */ | |
558a4411 | 1084 | mmap_reserve_or_unmap(old_addr, old_size); |
68a1c816 PB |
1085 | } |
1086 | } else if (flags & MREMAP_MAYMOVE) { | |
f19412a2 AJ |
1087 | abi_ulong mmap_start; |
1088 | ||
30ab9ef2 | 1089 | mmap_start = mmap_find_vma(0, new_size, TARGET_PAGE_SIZE); |
f19412a2 AJ |
1090 | |
1091 | if (mmap_start == -1) { | |
1092 | errno = ENOMEM; | |
1093 | host_addr = MAP_FAILED; | |
68a1c816 | 1094 | } else { |
3e8f1628 RH |
1095 | host_addr = mremap(g2h_untagged(old_addr), old_size, new_size, |
1096 | flags | MREMAP_FIXED, | |
1097 | g2h_untagged(mmap_start)); | |
b76f21a7 | 1098 | if (reserved_va) { |
558a4411 | 1099 | mmap_reserve_or_unmap(old_addr, old_size); |
c65ffe6d | 1100 | } |
68a1c816 | 1101 | } |
3af72a4d | 1102 | } else { |
ea800033 | 1103 | int page_flags = 0; |
b76f21a7 | 1104 | if (reserved_va && old_size < new_size) { |
68a1c816 PB |
1105 | abi_ulong addr; |
1106 | for (addr = old_addr + old_size; | |
1107 | addr < old_addr + new_size; | |
1108 | addr++) { | |
ea800033 | 1109 | page_flags |= page_get_flags(addr); |
68a1c816 PB |
1110 | } |
1111 | } | |
ea800033 | 1112 | if (page_flags == 0) { |
3e8f1628 RH |
1113 | host_addr = mremap(g2h_untagged(old_addr), |
1114 | old_size, new_size, flags); | |
56d19084 TK |
1115 | |
1116 | if (host_addr != MAP_FAILED) { | |
1117 | /* Check if address fits target address space */ | |
46b12f46 | 1118 | if (!guest_range_valid_untagged(h2g(host_addr), new_size)) { |
56d19084 | 1119 | /* Revert mremap() changes */ |
3e8f1628 RH |
1120 | host_addr = mremap(g2h_untagged(old_addr), |
1121 | new_size, old_size, flags); | |
56d19084 TK |
1122 | errno = ENOMEM; |
1123 | host_addr = MAP_FAILED; | |
1124 | } else if (reserved_va && old_size > new_size) { | |
558a4411 RH |
1125 | mmap_reserve_or_unmap(old_addr + old_size, |
1126 | old_size - new_size); | |
56d19084 | 1127 | } |
68a1c816 PB |
1128 | } |
1129 | } else { | |
1130 | errno = ENOMEM; | |
1131 | host_addr = MAP_FAILED; | |
1132 | } | |
f19412a2 AJ |
1133 | } |
1134 | ||
1135 | if (host_addr == MAP_FAILED) { | |
c8a706fe PB |
1136 | new_addr = -1; |
1137 | } else { | |
1138 | new_addr = h2g(host_addr); | |
1139 | prot = page_get_flags(old_addr); | |
49840a4a | 1140 | page_set_flags(old_addr, old_addr + old_size - 1, 0); |
044e95c8 | 1141 | shm_region_rm_complete(old_addr, old_addr + old_size - 1); |
49840a4a | 1142 | page_set_flags(new_addr, new_addr + new_size - 1, |
d9c58585 | 1143 | prot | PAGE_VALID | PAGE_RESET); |
044e95c8 | 1144 | shm_region_rm_complete(new_addr, new_addr + new_size - 1); |
c8a706fe PB |
1145 | } |
1146 | mmap_unlock(); | |
54936004 FB |
1147 | return new_addr; |
1148 | } | |
892a4f6a | 1149 | |
892a4f6a IL |
1150 | abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) |
1151 | { | |
e230ec09 | 1152 | abi_ulong len; |
892a4f6a IL |
1153 | int ret = 0; |
1154 | ||
1155 | if (start & ~TARGET_PAGE_MASK) { | |
1156 | return -TARGET_EINVAL; | |
1157 | } | |
e230ec09 | 1158 | if (len_in == 0) { |
892a4f6a IL |
1159 | return 0; |
1160 | } | |
e230ec09 RH |
1161 | len = TARGET_PAGE_ALIGN(len_in); |
1162 | if (len == 0 || !guest_range_valid_untagged(start, len)) { | |
892a4f6a IL |
1163 | return -TARGET_EINVAL; |
1164 | } | |
1165 | ||
4530deb1 HD |
1166 | /* Translate for some architectures which have different MADV_xxx values */ |
1167 | switch (advice) { | |
1168 | case TARGET_MADV_DONTNEED: /* alpha */ | |
1169 | advice = MADV_DONTNEED; | |
1170 | break; | |
1171 | case TARGET_MADV_WIPEONFORK: /* parisc */ | |
1172 | advice = MADV_WIPEONFORK; | |
1173 | break; | |
1174 | case TARGET_MADV_KEEPONFORK: /* parisc */ | |
1175 | advice = MADV_KEEPONFORK; | |
1176 | break; | |
1177 | /* we do not care about the other MADV_xxx values yet */ | |
1178 | } | |
1179 | ||
892a4f6a | 1180 | /* |
4530deb1 HD |
1181 | * Most advice values are hints, so ignoring and returning success is ok. |
1182 | * | |
1183 | * However, some advice values such as MADV_DONTNEED, MADV_WIPEONFORK and | |
1184 | * MADV_KEEPONFORK are not hints and need to be emulated. | |
892a4f6a | 1185 | * |
4530deb1 HD |
1186 | * A straight passthrough for those may not be safe because qemu sometimes |
1187 | * turns private file-backed mappings into anonymous mappings. | |
ecb796db RH |
1188 | * If all guest pages have PAGE_PASSTHROUGH set, mappings have the |
1189 | * same semantics for the host as for the guest. | |
892a4f6a | 1190 | * |
4530deb1 HD |
1191 | * We pass through MADV_WIPEONFORK and MADV_KEEPONFORK if possible and |
1192 | * return failure if not. | |
1193 | * | |
1194 | * MADV_DONTNEED is passed through as well, if possible. | |
1195 | * If passthrough isn't possible, we nevertheless (wrongly!) return | |
1196 | * success, which is broken but some userspace programs fail to work | |
1197 | * otherwise. Completely implementing such emulation is quite complicated | |
1198 | * though. | |
892a4f6a IL |
1199 | */ |
1200 | mmap_lock(); | |
4530deb1 HD |
1201 | switch (advice) { |
1202 | case MADV_WIPEONFORK: | |
1203 | case MADV_KEEPONFORK: | |
1204 | ret = -EINVAL; | |
1205 | /* fall through */ | |
1206 | case MADV_DONTNEED: | |
ecb796db | 1207 | if (page_check_range(start, len, PAGE_PASSTHROUGH)) { |
4530deb1 HD |
1208 | ret = get_errno(madvise(g2h_untagged(start), len, advice)); |
1209 | if ((advice == MADV_DONTNEED) && (ret == 0)) { | |
10310cbd | 1210 | page_reset_target_data(start, start + len - 1); |
4530deb1 | 1211 | } |
dbbf8975 | 1212 | } |
892a4f6a IL |
1213 | } |
1214 | mmap_unlock(); | |
1215 | ||
1216 | return ret; | |
1217 | } | |
225a206c RH |
1218 | |
1219 | #ifndef TARGET_FORCE_SHMLBA | |
1220 | /* | |
1221 | * For most architectures, SHMLBA is the same as the page size; | |
1222 | * some architectures have larger values, in which case they should | |
1223 | * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. | |
1224 | * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA | |
1225 | * and defining its own value for SHMLBA. | |
1226 | * | |
1227 | * The kernel also permits SHMLBA to be set by the architecture to a | |
1228 | * value larger than the page size without setting __ARCH_FORCE_SHMLBA; | |
1229 | * this means that addresses are rounded to the large size if | |
1230 | * SHM_RND is set but addresses not aligned to that size are not rejected | |
1231 | * as long as they are at least page-aligned. Since the only architecture | |
1232 | * which uses this is ia64 this code doesn't provide for that oddity. | |
1233 | */ | |
1234 | static inline abi_ulong target_shmlba(CPUArchState *cpu_env) | |
1235 | { | |
1236 | return TARGET_PAGE_SIZE; | |
1237 | } | |
1238 | #endif | |
1239 | ||
78bc8ed9 RH |
1240 | #if defined(__arm__) || defined(__mips__) || defined(__sparc__) |
1241 | #define HOST_FORCE_SHMLBA 1 | |
1242 | #else | |
1243 | #define HOST_FORCE_SHMLBA 0 | |
1244 | #endif | |
1245 | ||
225a206c RH |
1246 | abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, |
1247 | abi_ulong shmaddr, int shmflg) | |
1248 | { | |
1249 | CPUState *cpu = env_cpu(cpu_env); | |
225a206c | 1250 | struct shmid_ds shm_info; |
69fa2708 | 1251 | int ret; |
78bc8ed9 RH |
1252 | int h_pagesize; |
1253 | int t_shmlba, h_shmlba, m_shmlba; | |
1254 | size_t t_len, h_len, m_len; | |
225a206c RH |
1255 | |
1256 | /* shmat pointers are always untagged */ | |
1257 | ||
78bc8ed9 RH |
1258 | /* |
1259 | * Because we can't use host shmat() unless the address is sufficiently | |
1260 | * aligned for the host, we'll need to check both. | |
1261 | * TODO: Could be fixed with softmmu. | |
1262 | */ | |
1263 | t_shmlba = target_shmlba(cpu_env); | |
1264 | h_pagesize = qemu_real_host_page_size(); | |
1265 | h_shmlba = (HOST_FORCE_SHMLBA ? SHMLBA : h_pagesize); | |
1266 | m_shmlba = MAX(t_shmlba, h_shmlba); | |
1267 | ||
1268 | if (shmaddr) { | |
1269 | if (shmaddr & (m_shmlba - 1)) { | |
1270 | if (shmflg & SHM_RND) { | |
1271 | /* | |
1272 | * The guest is allowing the kernel to round the address. | |
1273 | * Assume that the guest is ok with us rounding to the | |
1274 | * host required alignment too. Anyway if we don't, we'll | |
1275 | * get an error from the kernel. | |
1276 | */ | |
1277 | shmaddr &= ~(m_shmlba - 1); | |
1278 | if (shmaddr == 0 && (shmflg & SHM_REMAP)) { | |
1279 | return -TARGET_EINVAL; | |
1280 | } | |
1281 | } else { | |
1282 | int require = TARGET_PAGE_SIZE; | |
1283 | #ifdef TARGET_FORCE_SHMLBA | |
1284 | require = t_shmlba; | |
1285 | #endif | |
1286 | /* | |
1287 | * Include host required alignment, as otherwise we cannot | |
1288 | * use host shmat at all. | |
1289 | */ | |
1290 | require = MAX(require, h_shmlba); | |
1291 | if (shmaddr & (require - 1)) { | |
1292 | return -TARGET_EINVAL; | |
1293 | } | |
1294 | } | |
1295 | } | |
1296 | } else { | |
1297 | if (shmflg & SHM_REMAP) { | |
1298 | return -TARGET_EINVAL; | |
1299 | } | |
1300 | } | |
1301 | /* All rounding now manually concluded. */ | |
1302 | shmflg &= ~SHM_RND; | |
1303 | ||
1304 | /* Find out the length of the shared memory segment. */ | |
225a206c RH |
1305 | ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); |
1306 | if (is_error(ret)) { | |
1307 | /* can't get length, bail out */ | |
1308 | return ret; | |
1309 | } | |
78bc8ed9 RH |
1310 | t_len = TARGET_PAGE_ALIGN(shm_info.shm_segsz); |
1311 | h_len = ROUND_UP(shm_info.shm_segsz, h_pagesize); | |
1312 | m_len = MAX(t_len, h_len); | |
225a206c | 1313 | |
78bc8ed9 | 1314 | if (!guest_range_valid_untagged(shmaddr, m_len)) { |
225a206c RH |
1315 | return -TARGET_EINVAL; |
1316 | } | |
1317 | ||
69fa2708 | 1318 | WITH_MMAP_LOCK_GUARD() { |
78bc8ed9 RH |
1319 | bool mapped = false; |
1320 | void *want, *test; | |
044e95c8 | 1321 | abi_ulong last; |
69fa2708 | 1322 | |
78bc8ed9 RH |
1323 | if (!shmaddr) { |
1324 | shmaddr = mmap_find_vma(0, m_len, m_shmlba); | |
1325 | if (shmaddr == -1) { | |
1326 | return -TARGET_ENOMEM; | |
1327 | } | |
1328 | mapped = !reserved_va; | |
1329 | } else if (shmflg & SHM_REMAP) { | |
1330 | /* | |
1331 | * If host page size > target page size, the host shmat may map | |
1332 | * more memory than the guest expects. Reject a mapping that | |
1333 | * would replace memory in the unexpected gap. | |
1334 | * TODO: Could be fixed with softmmu. | |
1335 | */ | |
1336 | if (t_len < h_len && | |
1337 | !page_check_range_empty(shmaddr + t_len, | |
1338 | shmaddr + h_len - 1)) { | |
1339 | return -TARGET_EINVAL; | |
1340 | } | |
69fa2708 | 1341 | } else { |
78bc8ed9 RH |
1342 | if (!page_check_range_empty(shmaddr, shmaddr + m_len - 1)) { |
1343 | return -TARGET_EINVAL; | |
1344 | } | |
1345 | } | |
69fa2708 | 1346 | |
78bc8ed9 RH |
1347 | /* All placement is now complete. */ |
1348 | want = (void *)g2h_untagged(shmaddr); | |
69fa2708 | 1349 | |
78bc8ed9 RH |
1350 | /* |
1351 | * Map anonymous pages across the entire range, then remap with | |
1352 | * the shared memory. This is required for a number of corner | |
1353 | * cases for which host and guest page sizes differ. | |
1354 | */ | |
1355 | if (h_len != t_len) { | |
1356 | int mmap_p = PROT_READ | (shmflg & SHM_RDONLY ? 0 : PROT_WRITE); | |
1357 | int mmap_f = MAP_PRIVATE | MAP_ANONYMOUS | |
fa527b44 | 1358 | | (reserved_va || mapped || (shmflg & SHM_REMAP) |
78bc8ed9 RH |
1359 | ? MAP_FIXED : MAP_FIXED_NOREPLACE); |
1360 | ||
1361 | test = mmap(want, m_len, mmap_p, mmap_f, -1, 0); | |
1362 | if (unlikely(test != want)) { | |
1363 | /* shmat returns EINVAL not EEXIST like mmap. */ | |
1364 | ret = (test == MAP_FAILED && errno != EEXIST | |
1365 | ? get_errno(-1) : -TARGET_EINVAL); | |
1366 | if (mapped) { | |
1367 | do_munmap(want, m_len); | |
1368 | } | |
1369 | return ret; | |
69fa2708 | 1370 | } |
78bc8ed9 | 1371 | mapped = true; |
69fa2708 RH |
1372 | } |
1373 | ||
78bc8ed9 RH |
1374 | if (reserved_va || mapped) { |
1375 | shmflg |= SHM_REMAP; | |
1376 | } | |
1377 | test = shmat(shmid, want, shmflg); | |
1378 | if (test == MAP_FAILED) { | |
1379 | ret = get_errno(-1); | |
1380 | if (mapped) { | |
1381 | do_munmap(want, m_len); | |
1382 | } | |
1383 | return ret; | |
69fa2708 | 1384 | } |
78bc8ed9 | 1385 | assert(test == want); |
69fa2708 | 1386 | |
78bc8ed9 RH |
1387 | last = shmaddr + m_len - 1; |
1388 | page_set_flags(shmaddr, last, | |
69fa2708 | 1389 | PAGE_VALID | PAGE_RESET | PAGE_READ | |
78bc8ed9 RH |
1390 | (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE) | |
1391 | (shmflg & SHM_EXEC ? PAGE_EXEC : 0)); | |
69fa2708 | 1392 | |
78bc8ed9 RH |
1393 | shm_region_rm_complete(shmaddr, last); |
1394 | shm_region_add(shmaddr, last); | |
69fa2708 | 1395 | } |
225a206c RH |
1396 | |
1397 | /* | |
1398 | * We're mapping shared memory, so ensure we generate code for parallel | |
1399 | * execution and flush old translations. This will work up to the level | |
1400 | * supported by the host -- anything that requires EXCP_ATOMIC will not | |
1401 | * be atomic with respect to an external process. | |
1402 | */ | |
1403 | if (!(cpu->tcg_cflags & CF_PARALLEL)) { | |
1404 | cpu->tcg_cflags |= CF_PARALLEL; | |
1405 | tb_flush(cpu); | |
1406 | } | |
1407 | ||
78bc8ed9 RH |
1408 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
1409 | FILE *f = qemu_log_trylock(); | |
1410 | if (f) { | |
1411 | fprintf(f, "page layout changed following shmat\n"); | |
1412 | page_dump(f); | |
1413 | qemu_log_unlock(f); | |
1414 | } | |
1415 | } | |
1416 | return shmaddr; | |
225a206c RH |
1417 | } |
1418 | ||
1419 | abi_long target_shmdt(abi_ulong shmaddr) | |
1420 | { | |
225a206c RH |
1421 | abi_long rv; |
1422 | ||
1423 | /* shmdt pointers are always untagged */ | |
1424 | ||
69fa2708 | 1425 | WITH_MMAP_LOCK_GUARD() { |
044e95c8 RH |
1426 | abi_ulong last = shm_region_find(shmaddr); |
1427 | if (last == 0) { | |
ceda5688 RH |
1428 | return -TARGET_EINVAL; |
1429 | } | |
1430 | ||
69fa2708 | 1431 | rv = get_errno(shmdt(g2h_untagged(shmaddr))); |
ceda5688 | 1432 | if (rv == 0) { |
044e95c8 | 1433 | abi_ulong size = last - shmaddr + 1; |
ceda5688 | 1434 | |
044e95c8 RH |
1435 | page_set_flags(shmaddr, last, 0); |
1436 | shm_region_rm_complete(shmaddr, last); | |
ceda5688 RH |
1437 | mmap_reserve_or_unmap(shmaddr, size); |
1438 | } | |
225a206c | 1439 | } |
225a206c RH |
1440 | return rv; |
1441 | } |