]>
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" |
54936004 | 23 | #include "qemu.h" |
3b249d26 | 24 | #include "user-internals.h" |
5423e6d3 | 25 | #include "user-mmap.h" |
8655b4c7 | 26 | #include "target_mman.h" |
044e95c8 | 27 | #include "qemu/interval-tree.h" |
54936004 | 28 | |
5a534314 PM |
29 | #ifdef TARGET_ARM |
30 | #include "target/arm/cpu-features.h" | |
31 | #endif | |
32 | ||
1e6eec8b | 33 | static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; |
dfd3f85c | 34 | static __thread int mmap_lock_count; |
c8a706fe PB |
35 | |
36 | void mmap_lock(void) | |
37 | { | |
38 | if (mmap_lock_count++ == 0) { | |
39 | pthread_mutex_lock(&mmap_mutex); | |
40 | } | |
41 | } | |
42 | ||
43 | void mmap_unlock(void) | |
44 | { | |
990ef918 | 45 | assert(mmap_lock_count > 0); |
c8a706fe PB |
46 | if (--mmap_lock_count == 0) { |
47 | pthread_mutex_unlock(&mmap_mutex); | |
48 | } | |
49 | } | |
d5975363 | 50 | |
301e40ed AB |
51 | bool have_mmap_lock(void) |
52 | { | |
53 | return mmap_lock_count > 0 ? true : false; | |
54 | } | |
55 | ||
d5975363 PB |
56 | /* Grab lock to make sure things are in a consistent state after fork(). */ |
57 | void mmap_fork_start(void) | |
58 | { | |
59 | if (mmap_lock_count) | |
60 | abort(); | |
61 | pthread_mutex_lock(&mmap_mutex); | |
62 | } | |
63 | ||
64 | void mmap_fork_end(int child) | |
65 | { | |
2b730f79 | 66 | if (child) { |
d5975363 | 67 | pthread_mutex_init(&mmap_mutex, NULL); |
2b730f79 | 68 | } else { |
d5975363 | 69 | pthread_mutex_unlock(&mmap_mutex); |
2b730f79 | 70 | } |
d5975363 | 71 | } |
c8a706fe | 72 | |
044e95c8 RH |
73 | /* Protected by mmap_lock. */ |
74 | static IntervalTreeRoot shm_regions; | |
75 | ||
76 | static void shm_region_add(abi_ptr start, abi_ptr last) | |
77 | { | |
78 | IntervalTreeNode *i = g_new0(IntervalTreeNode, 1); | |
79 | ||
80 | i->start = start; | |
81 | i->last = last; | |
82 | interval_tree_insert(i, &shm_regions); | |
83 | } | |
84 | ||
85 | static abi_ptr shm_region_find(abi_ptr start) | |
86 | { | |
87 | IntervalTreeNode *i; | |
88 | ||
89 | for (i = interval_tree_iter_first(&shm_regions, start, start); i; | |
90 | i = interval_tree_iter_next(i, start, start)) { | |
91 | if (i->start == start) { | |
92 | return i->last; | |
93 | } | |
94 | } | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static void shm_region_rm_complete(abi_ptr start, abi_ptr last) | |
99 | { | |
100 | IntervalTreeNode *i, *n; | |
101 | ||
102 | for (i = interval_tree_iter_first(&shm_regions, start, last); i; i = n) { | |
103 | n = interval_tree_iter_next(i, start, last); | |
104 | if (i->start >= start && i->last <= last) { | |
105 | interval_tree_remove(i, &shm_regions); | |
106 | g_free(i); | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
9dba3ca5 RH |
111 | /* |
112 | * Validate target prot bitmask. | |
113 | * Return the prot bitmask for the host in *HOST_PROT. | |
114 | * Return 0 if the target prot bitmask is invalid, otherwise | |
115 | * the internal qemu page_flags (which will include PAGE_VALID). | |
116 | */ | |
0dd55812 | 117 | static int validate_prot_to_pageflags(int prot) |
9dba3ca5 RH |
118 | { |
119 | int valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM; | |
120 | int page_flags = (prot & PAGE_BITS) | PAGE_VALID; | |
121 | ||
be5d6f48 | 122 | #ifdef TARGET_AARCH64 |
d109b46d | 123 | { |
be5d6f48 | 124 | ARMCPU *cpu = ARM_CPU(thread_cpu); |
d109b46d RH |
125 | |
126 | /* | |
127 | * The PROT_BTI bit is only accepted if the cpu supports the feature. | |
128 | * Since this is the unusual case, don't bother checking unless | |
129 | * the bit has been requested. If set and valid, record the bit | |
130 | * within QEMU's page_flags. | |
131 | */ | |
132 | if ((prot & TARGET_PROT_BTI) && cpu_isar_feature(aa64_bti, cpu)) { | |
be5d6f48 RH |
133 | valid |= TARGET_PROT_BTI; |
134 | page_flags |= PAGE_BTI; | |
135 | } | |
d109b46d RH |
136 | /* Similarly for the PROT_MTE bit. */ |
137 | if ((prot & TARGET_PROT_MTE) && cpu_isar_feature(aa64_mte, cpu)) { | |
138 | valid |= TARGET_PROT_MTE; | |
139 | page_flags |= PAGE_MTE; | |
140 | } | |
be5d6f48 | 141 | } |
4c184e70 HD |
142 | #elif defined(TARGET_HPPA) |
143 | valid |= PROT_GROWSDOWN | PROT_GROWSUP; | |
be5d6f48 RH |
144 | #endif |
145 | ||
9dba3ca5 RH |
146 | return prot & ~valid ? 0 : page_flags; |
147 | } | |
148 | ||
0dd55812 RH |
149 | /* |
150 | * For the host, we need not pass anything except read/write/exec. | |
151 | * While PROT_SEM is allowed by all hosts, it is also ignored, so | |
152 | * don't bother transforming guest bit to host bit. Any other | |
153 | * target-specific prot bits will not be understood by the host | |
154 | * and will need to be encoded into page_flags for qemu emulation. | |
155 | * | |
156 | * Pages that are executable by the guest will never be executed | |
157 | * by the host, but the host will need to be able to read them. | |
158 | */ | |
159 | static int target_to_host_prot(int prot) | |
160 | { | |
161 | return (prot & (PROT_READ | PROT_WRITE)) | | |
162 | (prot & PROT_EXEC ? PROT_READ : 0); | |
163 | } | |
164 | ||
53a5960a | 165 | /* NOTE: all the constants are the HOST ones, but addresses are target. */ |
9dba3ca5 | 166 | int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) |
54936004 | 167 | { |
621ac47d | 168 | int host_page_size = qemu_real_host_page_size(); |
7bdc1acc RH |
169 | abi_ulong starts[3]; |
170 | abi_ulong lens[3]; | |
171 | int prots[3]; | |
172 | abi_ulong host_start, host_last, last; | |
173 | int prot1, ret, page_flags, nranges; | |
54936004 | 174 | |
9dba3ca5 | 175 | trace_target_mprotect(start, len, target_prot); |
54936004 | 176 | |
9dba3ca5 | 177 | if ((start & ~TARGET_PAGE_MASK) != 0) { |
78cf3390 | 178 | return -TARGET_EINVAL; |
9dba3ca5 | 179 | } |
0dd55812 | 180 | page_flags = validate_prot_to_pageflags(target_prot); |
9dba3ca5 RH |
181 | if (!page_flags) { |
182 | return -TARGET_EINVAL; | |
183 | } | |
7bdc1acc RH |
184 | if (len == 0) { |
185 | return 0; | |
186 | } | |
54936004 | 187 | len = TARGET_PAGE_ALIGN(len); |
46b12f46 | 188 | if (!guest_range_valid_untagged(start, len)) { |
78cf3390 | 189 | return -TARGET_ENOMEM; |
ebf9a363 | 190 | } |
3b46e624 | 191 | |
7bdc1acc | 192 | last = start + len - 1; |
621ac47d | 193 | host_start = start & -host_page_size; |
b36b2b1d | 194 | host_last = ROUND_UP(last, host_page_size) - 1; |
7bdc1acc RH |
195 | nranges = 0; |
196 | ||
197 | mmap_lock(); | |
198 | ||
621ac47d | 199 | if (host_last - host_start < host_page_size) { |
7bdc1acc | 200 | /* Single host page contains all guest pages: sum the prot. */ |
0dd55812 | 201 | prot1 = target_prot; |
7bdc1acc RH |
202 | for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) { |
203 | prot1 |= page_get_flags(a); | |
54936004 | 204 | } |
7bdc1acc RH |
205 | for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) { |
206 | prot1 |= page_get_flags(a + 1); | |
d418c81e | 207 | } |
7bdc1acc | 208 | starts[nranges] = host_start; |
621ac47d | 209 | lens[nranges] = host_page_size; |
7bdc1acc RH |
210 | prots[nranges] = prot1; |
211 | nranges++; | |
212 | } else { | |
213 | if (host_start < start) { | |
214 | /* Host page contains more than one guest page: sum the prot. */ | |
215 | prot1 = target_prot; | |
216 | for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) { | |
217 | prot1 |= page_get_flags(a); | |
218 | } | |
219 | /* If the resulting sum differs, create a new range. */ | |
220 | if (prot1 != target_prot) { | |
221 | starts[nranges] = host_start; | |
621ac47d | 222 | lens[nranges] = host_page_size; |
7bdc1acc RH |
223 | prots[nranges] = prot1; |
224 | nranges++; | |
621ac47d | 225 | host_start += host_page_size; |
7bdc1acc | 226 | } |
9dba3ca5 | 227 | } |
7bdc1acc RH |
228 | |
229 | if (last < host_last) { | |
230 | /* Host page contains more than one guest page: sum the prot. */ | |
231 | prot1 = target_prot; | |
232 | for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) { | |
233 | prot1 |= page_get_flags(a + 1); | |
234 | } | |
235 | /* If the resulting sum differs, create a new range. */ | |
236 | if (prot1 != target_prot) { | |
621ac47d | 237 | host_last -= host_page_size; |
7bdc1acc | 238 | starts[nranges] = host_last + 1; |
621ac47d | 239 | lens[nranges] = host_page_size; |
7bdc1acc RH |
240 | prots[nranges] = prot1; |
241 | nranges++; | |
242 | } | |
54936004 | 243 | } |
7bdc1acc RH |
244 | |
245 | /* Create a range for the middle, if any remains. */ | |
246 | if (host_start < host_last) { | |
247 | starts[nranges] = host_start; | |
248 | lens[nranges] = host_last - host_start + 1; | |
249 | prots[nranges] = target_prot; | |
250 | nranges++; | |
9dba3ca5 | 251 | } |
54936004 | 252 | } |
3b46e624 | 253 | |
7bdc1acc RH |
254 | for (int i = 0; i < nranges; ++i) { |
255 | ret = mprotect(g2h_untagged(starts[i]), lens[i], | |
256 | target_to_host_prot(prots[i])); | |
9dba3ca5 | 257 | if (ret != 0) { |
c8a706fe | 258 | goto error; |
9dba3ca5 | 259 | } |
54936004 | 260 | } |
aa98e2d8 | 261 | |
7bdc1acc | 262 | page_set_flags(start, last, page_flags); |
aa98e2d8 IL |
263 | ret = 0; |
264 | ||
7bdc1acc | 265 | error: |
c8a706fe PB |
266 | mmap_unlock(); |
267 | return ret; | |
54936004 FB |
268 | } |
269 | ||
270 | /* map an incomplete host page */ | |
99982beb RH |
271 | static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, |
272 | int prot, int flags, int fd, off_t offset) | |
54936004 | 273 | { |
621ac47d | 274 | int host_page_size = qemu_real_host_page_size(); |
99982beb | 275 | abi_ulong real_last; |
53a5960a | 276 | void *host_start; |
99982beb RH |
277 | int prot_old, prot_new; |
278 | int host_prot_old, host_prot_new; | |
54936004 | 279 | |
99982beb RH |
280 | if (!(flags & MAP_ANONYMOUS) |
281 | && (flags & MAP_TYPE) == MAP_SHARED | |
282 | && (prot & PROT_WRITE)) { | |
283 | /* | |
284 | * msync() won't work with the partial page, so we return an | |
285 | * error if write is possible while it is a shared mapping. | |
286 | */ | |
287 | errno = EINVAL; | |
288 | return false; | |
289 | } | |
290 | ||
621ac47d | 291 | real_last = real_start + host_page_size - 1; |
3e8f1628 | 292 | host_start = g2h_untagged(real_start); |
54936004 | 293 | |
99982beb RH |
294 | /* Get the protection of the target pages outside the mapping. */ |
295 | prot_old = 0; | |
296 | for (abi_ulong a = real_start; a < start; a += TARGET_PAGE_SIZE) { | |
297 | prot_old |= page_get_flags(a); | |
298 | } | |
299 | for (abi_ulong a = real_last; a > last; a -= TARGET_PAGE_SIZE) { | |
300 | prot_old |= page_get_flags(a); | |
54936004 | 301 | } |
3b46e624 | 302 | |
99982beb RH |
303 | if (prot_old == 0) { |
304 | /* | |
305 | * Since !(prot_old & PAGE_VALID), there were no guest pages | |
306 | * outside of the fragment we need to map. Allocate a new host | |
307 | * page to cover, discarding whatever else may have been present. | |
308 | */ | |
621ac47d | 309 | void *p = mmap(host_start, host_page_size, |
0dd55812 | 310 | target_to_host_prot(prot), |
80210bcd | 311 | flags | MAP_ANONYMOUS, -1, 0); |
ddcdd8c4 AO |
312 | if (p != host_start) { |
313 | if (p != MAP_FAILED) { | |
621ac47d | 314 | munmap(p, host_page_size); |
ddcdd8c4 AO |
315 | errno = EEXIST; |
316 | } | |
99982beb | 317 | return false; |
2b730f79 | 318 | } |
99982beb | 319 | prot_old = prot; |
54936004 | 320 | } |
99982beb | 321 | prot_new = prot | prot_old; |
54936004 | 322 | |
99982beb RH |
323 | host_prot_old = target_to_host_prot(prot_old); |
324 | host_prot_new = target_to_host_prot(prot_new); | |
3b46e624 | 325 | |
99982beb RH |
326 | /* Adjust protection to be able to write. */ |
327 | if (!(host_prot_old & PROT_WRITE)) { | |
328 | host_prot_old |= PROT_WRITE; | |
621ac47d | 329 | mprotect(host_start, host_page_size, host_prot_old); |
99982beb | 330 | } |
3b46e624 | 331 | |
99982beb RH |
332 | /* Read or zero the new guest pages. */ |
333 | if (flags & MAP_ANONYMOUS) { | |
334 | memset(g2h_untagged(start), 0, last - start + 1); | |
54936004 | 335 | } else { |
99982beb RH |
336 | if (pread(fd, g2h_untagged(start), last - start + 1, offset) == -1) { |
337 | return false; | |
e6deac9c | 338 | } |
54936004 | 339 | } |
99982beb RH |
340 | |
341 | /* Put final protection */ | |
342 | if (host_prot_new != host_prot_old) { | |
621ac47d | 343 | mprotect(host_start, host_page_size, host_prot_new); |
99982beb RH |
344 | } |
345 | return true; | |
54936004 FB |
346 | } |
347 | ||
c8fb5cf9 | 348 | abi_ulong task_unmapped_base; |
da2b71fa | 349 | abi_ulong elf_et_dyn_base; |
c8fb5cf9 | 350 | abi_ulong mmap_next_start; |
a03e2d42 | 351 | |
2b730f79 RH |
352 | /* |
353 | * Subroutine of mmap_find_vma, used when we have pre-allocated | |
354 | * a chunk of guest address space. | |
355 | */ | |
30ab9ef2 RH |
356 | static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, |
357 | abi_ulong align) | |
68a1c816 | 358 | { |
4c13048e | 359 | target_ulong ret; |
68a1c816 | 360 | |
4c13048e RH |
361 | ret = page_find_range_empty(start, reserved_va, size, align); |
362 | if (ret == -1 && start > mmap_min_addr) { | |
363 | /* Restart at the beginning of the address space. */ | |
364 | ret = page_find_range_empty(mmap_min_addr, start - 1, size, align); | |
68a1c816 PB |
365 | } |
366 | ||
4c13048e | 367 | return ret; |
68a1c816 PB |
368 | } |
369 | ||
fe3b4152 KS |
370 | /* |
371 | * Find and reserve a free memory area of size 'size'. The search | |
372 | * starts at 'start'. | |
373 | * It must be called with mmap_lock() held. | |
374 | * Return -1 if error. | |
375 | */ | |
30ab9ef2 | 376 | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) |
a03e2d42 | 377 | { |
621ac47d | 378 | int host_page_size = qemu_real_host_page_size(); |
14f24e14 | 379 | void *ptr, *prev; |
fe3b4152 | 380 | abi_ulong addr; |
14f24e14 | 381 | int wrapped, repeat; |
fe3b4152 | 382 | |
621ac47d | 383 | align = MAX(align, host_page_size); |
443b7505 | 384 | |
fe3b4152 | 385 | /* If 'start' == 0, then a default start address is used. */ |
14f24e14 | 386 | if (start == 0) { |
fe3b4152 | 387 | start = mmap_next_start; |
14f24e14 | 388 | } else { |
621ac47d | 389 | start &= -host_page_size; |
14f24e14 | 390 | } |
30ab9ef2 | 391 | start = ROUND_UP(start, align); |
b36b2b1d | 392 | size = ROUND_UP(size, host_page_size); |
fe3b4152 | 393 | |
b76f21a7 | 394 | if (reserved_va) { |
30ab9ef2 | 395 | return mmap_find_vma_reserved(start, size, align); |
68a1c816 PB |
396 | } |
397 | ||
a03e2d42 | 398 | addr = start; |
14f24e14 RH |
399 | wrapped = repeat = 0; |
400 | prev = 0; | |
fe3b4152 | 401 | |
14f24e14 | 402 | for (;; prev = ptr) { |
fe3b4152 KS |
403 | /* |
404 | * Reserve needed memory area to avoid a race. | |
405 | * It should be discarded using: | |
406 | * - mmap() with MAP_FIXED flag | |
407 | * - mremap() with MREMAP_FIXED flag | |
408 | * - shmat() with SHM_REMAP flag | |
409 | */ | |
3e8f1628 | 410 | ptr = mmap(g2h_untagged(addr), size, PROT_NONE, |
2b730f79 | 411 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0); |
fe3b4152 KS |
412 | |
413 | /* ENOMEM, if host address space has no memory */ | |
14f24e14 | 414 | if (ptr == MAP_FAILED) { |
fe3b4152 | 415 | return (abi_ulong)-1; |
14f24e14 RH |
416 | } |
417 | ||
2b730f79 RH |
418 | /* |
419 | * Count the number of sequential returns of the same address. | |
420 | * This is used to modify the search algorithm below. | |
421 | */ | |
14f24e14 RH |
422 | repeat = (ptr == prev ? repeat + 1 : 0); |
423 | ||
424 | if (h2g_valid(ptr + size - 1)) { | |
425 | addr = h2g(ptr); | |
fe3b4152 | 426 | |
30ab9ef2 | 427 | if ((addr & (align - 1)) == 0) { |
14f24e14 | 428 | /* Success. */ |
c8fb5cf9 | 429 | if (start == mmap_next_start && addr >= task_unmapped_base) { |
14f24e14 RH |
430 | mmap_next_start = addr + size; |
431 | } | |
432 | return addr; | |
433 | } | |
fe3b4152 | 434 | |
14f24e14 RH |
435 | /* The address is not properly aligned for the target. */ |
436 | switch (repeat) { | |
437 | case 0: | |
2b730f79 RH |
438 | /* |
439 | * Assume the result that the kernel gave us is the | |
440 | * first with enough free space, so start again at the | |
441 | * next higher target page. | |
442 | */ | |
30ab9ef2 | 443 | addr = ROUND_UP(addr, align); |
14f24e14 RH |
444 | break; |
445 | case 1: | |
2b730f79 RH |
446 | /* |
447 | * Sometimes the kernel decides to perform the allocation | |
448 | * at the top end of memory instead. | |
449 | */ | |
30ab9ef2 | 450 | addr &= -align; |
14f24e14 RH |
451 | break; |
452 | case 2: | |
453 | /* Start over at low memory. */ | |
454 | addr = 0; | |
455 | break; | |
456 | default: | |
457 | /* Fail. This unaligned block must the last. */ | |
458 | addr = -1; | |
459 | break; | |
460 | } | |
461 | } else { | |
2b730f79 RH |
462 | /* |
463 | * Since the result the kernel gave didn't fit, start | |
464 | * again at low memory. If any repetition, fail. | |
465 | */ | |
14f24e14 RH |
466 | addr = (repeat ? -1 : 0); |
467 | } | |
468 | ||
469 | /* Unmap and try again. */ | |
fe3b4152 | 470 | munmap(ptr, size); |
fe3b4152 | 471 | |
14f24e14 | 472 | /* ENOMEM if we checked the whole of the target address space. */ |
d0b3e4f5 | 473 | if (addr == (abi_ulong)-1) { |
a03e2d42 | 474 | return (abi_ulong)-1; |
14f24e14 RH |
475 | } else if (addr == 0) { |
476 | if (wrapped) { | |
477 | return (abi_ulong)-1; | |
478 | } | |
479 | wrapped = 1; | |
2b730f79 RH |
480 | /* |
481 | * Don't actually use 0 when wrapping, instead indicate | |
482 | * that we'd truly like an allocation in low memory. | |
483 | */ | |
14f24e14 RH |
484 | addr = (mmap_min_addr > TARGET_PAGE_SIZE |
485 | ? TARGET_PAGE_ALIGN(mmap_min_addr) | |
486 | : TARGET_PAGE_SIZE); | |
487 | } else if (wrapped && addr >= start) { | |
488 | return (abi_ulong)-1; | |
489 | } | |
a03e2d42 | 490 | } |
a03e2d42 FB |
491 | } |
492 | ||
d558c395 RH |
493 | static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, |
494 | int target_prot, int flags, | |
495 | int fd, off_t offset) | |
54936004 | 496 | { |
621ac47d | 497 | int host_page_size = qemu_real_host_page_size(); |
f9cd8f5e RH |
498 | abi_ulong ret, last, real_start, real_last, retaddr, host_len; |
499 | abi_ulong passthrough_start = -1, passthrough_last = 0; | |
0dd55812 | 500 | int page_flags; |
55baec0f | 501 | off_t host_offset; |
54936004 | 502 | |
38138fab | 503 | if (!len) { |
e89f07d3 | 504 | errno = EINVAL; |
d558c395 | 505 | return -1; |
e89f07d3 | 506 | } |
54936004 | 507 | |
0dd55812 | 508 | page_flags = validate_prot_to_pageflags(target_prot); |
9dba3ca5 RH |
509 | if (!page_flags) { |
510 | errno = EINVAL; | |
d558c395 | 511 | return -1; |
9dba3ca5 RH |
512 | } |
513 | ||
38138fab | 514 | /* Also check for overflows... */ |
54936004 | 515 | len = TARGET_PAGE_ALIGN(len); |
38138fab AB |
516 | if (!len) { |
517 | errno = ENOMEM; | |
d558c395 | 518 | return -1; |
38138fab AB |
519 | } |
520 | ||
521 | if (offset & ~TARGET_PAGE_MASK) { | |
522 | errno = EINVAL; | |
d558c395 | 523 | return -1; |
38138fab AB |
524 | } |
525 | ||
228168cb RH |
526 | /* |
527 | * If we're mapping shared memory, ensure we generate code for parallel | |
528 | * execution and flush old translations. This will work up to the level | |
529 | * supported by the host -- anything that requires EXCP_ATOMIC will not | |
530 | * be atomic with respect to an external process. | |
531 | */ | |
532 | if (flags & MAP_SHARED) { | |
533 | CPUState *cpu = thread_cpu; | |
534 | if (!(cpu->tcg_cflags & CF_PARALLEL)) { | |
535 | cpu->tcg_cflags |= CF_PARALLEL; | |
536 | tb_flush(cpu); | |
537 | } | |
538 | } | |
539 | ||
621ac47d RH |
540 | real_start = start & -host_page_size; |
541 | host_offset = offset & -host_page_size; | |
a5e7ee46 | 542 | |
2b730f79 RH |
543 | /* |
544 | * If the user is asking for the kernel to find a location, do that | |
545 | * before we truncate the length for mapping files below. | |
546 | */ | |
03798605 | 547 | if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { |
a5e7ee46 | 548 | host_len = len + offset - host_offset; |
b36b2b1d | 549 | host_len = ROUND_UP(host_len, host_page_size); |
30ab9ef2 | 550 | start = mmap_find_vma(real_start, host_len, TARGET_PAGE_SIZE); |
a5e7ee46 RH |
551 | if (start == (abi_ulong)-1) { |
552 | errno = ENOMEM; | |
d558c395 | 553 | return -1; |
a5e7ee46 RH |
554 | } |
555 | } | |
54936004 | 556 | |
2b730f79 RH |
557 | /* |
558 | * When mapping files into a memory area larger than the file, accesses | |
559 | * to pages beyond the file size will cause a SIGBUS. | |
560 | * | |
561 | * For example, if mmaping a file of 100 bytes on a host with 4K pages | |
562 | * emulating a target with 8K pages, the target expects to be able to | |
563 | * access the first 8K. But the host will trap us on any access beyond | |
564 | * 4K. | |
565 | * | |
566 | * When emulating a target with a larger page-size than the hosts, we | |
567 | * may need to truncate file maps at EOF and add extra anonymous pages | |
568 | * up to the targets page boundary. | |
569 | */ | |
621ac47d | 570 | if (host_page_size < TARGET_PAGE_SIZE && !(flags & MAP_ANONYMOUS)) { |
35f2fd04 | 571 | struct stat sb; |
54c5a2ae | 572 | |
2b730f79 | 573 | if (fstat(fd, &sb) == -1) { |
d558c395 | 574 | return -1; |
2b730f79 | 575 | } |
54c5a2ae | 576 | |
2b730f79 RH |
577 | /* Are we trying to create a map beyond EOF?. */ |
578 | if (offset + len > sb.st_size) { | |
579 | /* | |
580 | * If so, truncate the file map at eof aligned with | |
581 | * the hosts real pagesize. Additional anonymous maps | |
582 | * will be created beyond EOF. | |
583 | */ | |
e56922ab | 584 | len = ROUND_UP(sb.st_size - offset, host_page_size); |
2b730f79 | 585 | } |
54c5a2ae EI |
586 | } |
587 | ||
03798605 | 588 | if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { |
55baec0f | 589 | uintptr_t host_start; |
0dd55812 | 590 | int host_prot; |
a03e2d42 | 591 | void *p; |
a5e7ee46 | 592 | |
a03e2d42 | 593 | host_len = len + offset - host_offset; |
b36b2b1d | 594 | host_len = ROUND_UP(host_len, host_page_size); |
0dd55812 | 595 | host_prot = target_to_host_prot(target_prot); |
a5e7ee46 | 596 | |
621ac47d | 597 | /* Note: we prefer to control the mapping address. */ |
3e8f1628 | 598 | p = mmap(g2h_untagged(start), host_len, host_prot, |
a5e7ee46 | 599 | flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0); |
9dba3ca5 | 600 | if (p == MAP_FAILED) { |
d558c395 | 601 | return -1; |
9dba3ca5 | 602 | } |
a03e2d42 | 603 | /* update start so that it points to the file position at 'offset' */ |
55baec0f | 604 | host_start = (uintptr_t)p; |
54c5a2ae | 605 | if (!(flags & MAP_ANONYMOUS)) { |
3e8f1628 | 606 | p = mmap(g2h_untagged(start), len, host_prot, |
54c5a2ae | 607 | flags | MAP_FIXED, fd, host_offset); |
8384274e | 608 | if (p == MAP_FAILED) { |
3e8f1628 | 609 | munmap(g2h_untagged(start), host_len); |
d558c395 | 610 | return -1; |
8384274e | 611 | } |
a03e2d42 | 612 | host_start += offset - host_offset; |
54c5a2ae | 613 | } |
a03e2d42 | 614 | start = h2g(host_start); |
f9cd8f5e | 615 | last = start + len - 1; |
f93b7695 | 616 | passthrough_start = start; |
f9cd8f5e | 617 | passthrough_last = last; |
a03e2d42 FB |
618 | } else { |
619 | if (start & ~TARGET_PAGE_MASK) { | |
e89f07d3 | 620 | errno = EINVAL; |
d558c395 | 621 | return -1; |
e89f07d3 | 622 | } |
f9cd8f5e | 623 | last = start + len - 1; |
b36b2b1d | 624 | real_last = ROUND_UP(last, host_page_size) - 1; |
7ab240ad | 625 | |
7d37435b PB |
626 | /* |
627 | * Test if requested memory area fits target address space | |
628 | * It can fail only on 64-bit host with 32-bit target. | |
629 | * On any other target/host host mmap() handles this error correctly. | |
630 | */ | |
f9cd8f5e | 631 | if (last < start || !guest_range_valid_untagged(start, len)) { |
ebf9a363 | 632 | errno = ENOMEM; |
d558c395 | 633 | return -1; |
45bc1f52 AJ |
634 | } |
635 | ||
c3dd50da AO |
636 | if (flags & MAP_FIXED_NOREPLACE) { |
637 | /* Validate that the chosen range is empty. */ | |
638 | if (!page_check_range_empty(start, last)) { | |
639 | errno = EEXIST; | |
d558c395 | 640 | return -1; |
c3dd50da AO |
641 | } |
642 | ||
643 | /* | |
644 | * With reserved_va, the entire address space is mmaped in the | |
645 | * host to ensure it isn't accidentally used for something else. | |
646 | * We have just checked that the guest address is not mapped | |
647 | * within the guest, but need to replace the host reservation. | |
648 | * | |
649 | * Without reserved_va, despite the guest address check above, | |
650 | * keep MAP_FIXED_NOREPLACE so that the guest does not overwrite | |
651 | * any host address mappings. | |
652 | */ | |
653 | if (reserved_va) { | |
654 | flags = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; | |
655 | } | |
03798605 RH |
656 | } |
657 | ||
2b730f79 RH |
658 | /* |
659 | * worst case: we cannot map the file because the offset is not | |
660 | * aligned, so we read it | |
661 | */ | |
a03e2d42 | 662 | if (!(flags & MAP_ANONYMOUS) && |
621ac47d | 663 | (offset & (host_page_size - 1)) != (start & (host_page_size - 1))) { |
2b730f79 RH |
664 | /* |
665 | * msync() won't work here, so we return an error if write is | |
666 | * possible while it is a shared mapping | |
667 | */ | |
0dd55812 RH |
668 | if ((flags & MAP_TYPE) == MAP_SHARED |
669 | && (target_prot & PROT_WRITE)) { | |
a03e2d42 | 670 | errno = EINVAL; |
d558c395 | 671 | return -1; |
a03e2d42 | 672 | } |
9dba3ca5 | 673 | retaddr = target_mmap(start, len, target_prot | PROT_WRITE, |
03798605 RH |
674 | (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) |
675 | | MAP_PRIVATE | MAP_ANONYMOUS, | |
a03e2d42 | 676 | -1, 0); |
2b730f79 | 677 | if (retaddr == -1) { |
d558c395 | 678 | return -1; |
2b730f79 RH |
679 | } |
680 | if (pread(fd, g2h_untagged(start), len, offset) == -1) { | |
d558c395 | 681 | return -1; |
2b730f79 | 682 | } |
0dd55812 | 683 | if (!(target_prot & PROT_WRITE)) { |
9dba3ca5 | 684 | ret = target_mprotect(start, len, target_prot); |
86abac06 | 685 | assert(ret == 0); |
a03e2d42 FB |
686 | } |
687 | goto the_end; | |
54936004 | 688 | } |
2b730f79 | 689 | |
a03e2d42 FB |
690 | /* handle the start of the mapping */ |
691 | if (start > real_start) { | |
621ac47d | 692 | if (real_last == real_start + host_page_size - 1) { |
a03e2d42 | 693 | /* one single host page */ |
f9cd8f5e | 694 | if (!mmap_frag(real_start, start, last, |
99982beb | 695 | target_prot, flags, fd, offset)) { |
d558c395 | 696 | return -1; |
2b730f79 | 697 | } |
a03e2d42 FB |
698 | goto the_end1; |
699 | } | |
99982beb | 700 | if (!mmap_frag(real_start, start, |
621ac47d | 701 | real_start + host_page_size - 1, |
99982beb | 702 | target_prot, flags, fd, offset)) { |
d558c395 | 703 | return -1; |
2b730f79 | 704 | } |
621ac47d | 705 | real_start += host_page_size; |
a03e2d42 FB |
706 | } |
707 | /* handle the end of the mapping */ | |
f9cd8f5e | 708 | if (last < real_last) { |
621ac47d | 709 | abi_ulong real_page = real_last - host_page_size + 1; |
f9cd8f5e | 710 | if (!mmap_frag(real_page, real_page, last, |
99982beb | 711 | target_prot, flags, fd, |
f9cd8f5e | 712 | offset + real_page - start)) { |
d558c395 | 713 | return -1; |
2b730f79 | 714 | } |
621ac47d | 715 | real_last -= host_page_size; |
54936004 | 716 | } |
3b46e624 | 717 | |
a03e2d42 | 718 | /* map the middle (easier) */ |
f9cd8f5e | 719 | if (real_start < real_last) { |
ddcdd8c4 | 720 | void *p, *want_p; |
55baec0f | 721 | off_t offset1; |
ddcdd8c4 | 722 | size_t len1; |
55baec0f | 723 | |
2b730f79 | 724 | if (flags & MAP_ANONYMOUS) { |
a03e2d42 | 725 | offset1 = 0; |
2b730f79 | 726 | } else { |
a03e2d42 | 727 | offset1 = offset + real_start - start; |
2b730f79 | 728 | } |
ddcdd8c4 AO |
729 | len1 = real_last - real_start + 1; |
730 | want_p = g2h_untagged(real_start); | |
731 | ||
732 | p = mmap(want_p, len1, target_to_host_prot(target_prot), | |
733 | flags, fd, offset1); | |
734 | if (p != want_p) { | |
735 | if (p != MAP_FAILED) { | |
736 | munmap(p, len1); | |
737 | errno = EEXIST; | |
738 | } | |
d558c395 | 739 | return -1; |
2b730f79 | 740 | } |
f93b7695 | 741 | passthrough_start = real_start; |
f9cd8f5e | 742 | passthrough_last = real_last; |
a03e2d42 | 743 | } |
54936004 FB |
744 | } |
745 | the_end1: | |
26bab757 RH |
746 | if (flags & MAP_ANONYMOUS) { |
747 | page_flags |= PAGE_ANON; | |
748 | } | |
d9c58585 | 749 | page_flags |= PAGE_RESET; |
f9cd8f5e RH |
750 | if (passthrough_start > passthrough_last) { |
751 | page_set_flags(start, last, page_flags); | |
f93b7695 IL |
752 | } else { |
753 | if (start < passthrough_start) { | |
49840a4a | 754 | page_set_flags(start, passthrough_start - 1, page_flags); |
f93b7695 | 755 | } |
f9cd8f5e | 756 | page_set_flags(passthrough_start, passthrough_last, |
f93b7695 | 757 | page_flags | PAGE_PASSTHROUGH); |
f9cd8f5e RH |
758 | if (passthrough_last < last) { |
759 | page_set_flags(passthrough_last + 1, last, page_flags); | |
f93b7695 IL |
760 | } |
761 | } | |
044e95c8 | 762 | shm_region_rm_complete(start, last); |
54936004 | 763 | the_end: |
d0e165ae | 764 | trace_target_mmap_complete(start); |
10d0d505 | 765 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
93756fdc RH |
766 | FILE *f = qemu_log_trylock(); |
767 | if (f) { | |
768 | fprintf(f, "page layout changed following mmap\n"); | |
769 | page_dump(f); | |
770 | qemu_log_unlock(f); | |
771 | } | |
10d0d505 | 772 | } |
54936004 | 773 | return start; |
d558c395 RH |
774 | } |
775 | ||
776 | /* NOTE: all the constants are the HOST ones */ | |
777 | abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, | |
778 | int flags, int fd, off_t offset) | |
779 | { | |
780 | abi_long ret; | |
781 | ||
782 | trace_target_mmap(start, len, target_prot, flags, fd, offset); | |
783 | mmap_lock(); | |
784 | ||
785 | ret = target_mmap__locked(start, len, target_prot, flags, fd, offset); | |
786 | ||
c8a706fe | 787 | mmap_unlock(); |
d558c395 | 788 | return ret; |
54936004 FB |
789 | } |
790 | ||
912ff698 | 791 | static int mmap_reserve_or_unmap(abi_ulong start, abi_ulong len) |
68a1c816 | 792 | { |
621ac47d | 793 | int host_page_size = qemu_real_host_page_size(); |
68a1c816 | 794 | abi_ulong real_start; |
260561d8 RH |
795 | abi_ulong real_last; |
796 | abi_ulong real_len; | |
797 | abi_ulong last; | |
798 | abi_ulong a; | |
558a4411 | 799 | void *host_start; |
68a1c816 PB |
800 | int prot; |
801 | ||
260561d8 | 802 | last = start + len - 1; |
621ac47d | 803 | real_start = start & -host_page_size; |
b36b2b1d | 804 | real_last = ROUND_UP(last, host_page_size) - 1; |
260561d8 RH |
805 | |
806 | /* | |
807 | * If guest pages remain on the first or last host pages, | |
808 | * adjust the deallocation to retain those guest pages. | |
809 | * The single page special case is required for the last page, | |
810 | * lest real_start overflow to zero. | |
811 | */ | |
621ac47d | 812 | if (real_last - real_start < host_page_size) { |
68a1c816 | 813 | prot = 0; |
260561d8 RH |
814 | for (a = real_start; a < start; a += TARGET_PAGE_SIZE) { |
815 | prot |= page_get_flags(a); | |
68a1c816 | 816 | } |
260561d8 RH |
817 | for (a = last; a < real_last; a += TARGET_PAGE_SIZE) { |
818 | prot |= page_get_flags(a + 1); | |
819 | } | |
820 | if (prot != 0) { | |
912ff698 | 821 | return 0; |
260561d8 RH |
822 | } |
823 | } else { | |
824 | for (prot = 0, a = real_start; a < start; a += TARGET_PAGE_SIZE) { | |
825 | prot |= page_get_flags(a); | |
68a1c816 | 826 | } |
2b730f79 | 827 | if (prot != 0) { |
621ac47d | 828 | real_start += host_page_size; |
2b730f79 | 829 | } |
260561d8 RH |
830 | |
831 | for (prot = 0, a = last; a < real_last; a += TARGET_PAGE_SIZE) { | |
832 | prot |= page_get_flags(a + 1); | |
68a1c816 | 833 | } |
2b730f79 | 834 | if (prot != 0) { |
621ac47d | 835 | real_last -= host_page_size; |
260561d8 RH |
836 | } |
837 | ||
838 | if (real_last < real_start) { | |
912ff698 | 839 | return 0; |
2b730f79 | 840 | } |
68a1c816 | 841 | } |
260561d8 RH |
842 | |
843 | real_len = real_last - real_start + 1; | |
844 | host_start = g2h_untagged(real_start); | |
845 | ||
558a4411 RH |
846 | if (reserved_va) { |
847 | void *ptr = mmap(host_start, real_len, PROT_NONE, | |
848 | MAP_FIXED | MAP_ANONYMOUS | |
849 | | MAP_PRIVATE | MAP_NORESERVE, -1, 0); | |
912ff698 | 850 | return ptr == host_start ? 0 : -1; |
558a4411 | 851 | } |
912ff698 | 852 | return munmap(host_start, real_len); |
68a1c816 PB |
853 | } |
854 | ||
992f48a0 | 855 | int target_munmap(abi_ulong start, abi_ulong len) |
54936004 | 856 | { |
912ff698 RH |
857 | int ret; |
858 | ||
b7b18d26 AB |
859 | trace_target_munmap(start, len); |
860 | ||
2b730f79 | 861 | if (start & ~TARGET_PAGE_MASK) { |
912ff698 RH |
862 | errno = EINVAL; |
863 | return -1; | |
2b730f79 | 864 | } |
54936004 | 865 | len = TARGET_PAGE_ALIGN(len); |
46b12f46 | 866 | if (len == 0 || !guest_range_valid_untagged(start, len)) { |
912ff698 RH |
867 | errno = EINVAL; |
868 | return -1; | |
ebf9a363 MF |
869 | } |
870 | ||
c8a706fe | 871 | mmap_lock(); |
912ff698 RH |
872 | ret = mmap_reserve_or_unmap(start, len); |
873 | if (likely(ret == 0)) { | |
874 | page_set_flags(start, start + len - 1, 0); | |
875 | shm_region_rm_complete(start, start + len - 1); | |
876 | } | |
c8a706fe | 877 | mmap_unlock(); |
d7b0c5d0 | 878 | |
912ff698 | 879 | return ret; |
54936004 FB |
880 | } |
881 | ||
992f48a0 BS |
882 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
883 | abi_ulong new_size, unsigned long flags, | |
884 | abi_ulong new_addr) | |
54936004 FB |
885 | { |
886 | int prot; | |
f19412a2 | 887 | void *host_addr; |
54936004 | 888 | |
46b12f46 | 889 | if (!guest_range_valid_untagged(old_addr, old_size) || |
ebf9a363 | 890 | ((flags & MREMAP_FIXED) && |
46b12f46 | 891 | !guest_range_valid_untagged(new_addr, new_size)) || |
ccc5ccc1 | 892 | ((flags & MREMAP_MAYMOVE) == 0 && |
46b12f46 | 893 | !guest_range_valid_untagged(old_addr, new_size))) { |
ebf9a363 MF |
894 | errno = ENOMEM; |
895 | return -1; | |
896 | } | |
897 | ||
c8a706fe | 898 | mmap_lock(); |
f19412a2 | 899 | |
68a1c816 | 900 | if (flags & MREMAP_FIXED) { |
3e8f1628 RH |
901 | host_addr = mremap(g2h_untagged(old_addr), old_size, new_size, |
902 | flags, g2h_untagged(new_addr)); | |
68a1c816 | 903 | |
b76f21a7 | 904 | if (reserved_va && host_addr != MAP_FAILED) { |
2b730f79 RH |
905 | /* |
906 | * If new and old addresses overlap then the above mremap will | |
907 | * already have failed with EINVAL. | |
908 | */ | |
558a4411 | 909 | mmap_reserve_or_unmap(old_addr, old_size); |
68a1c816 PB |
910 | } |
911 | } else if (flags & MREMAP_MAYMOVE) { | |
f19412a2 AJ |
912 | abi_ulong mmap_start; |
913 | ||
30ab9ef2 | 914 | mmap_start = mmap_find_vma(0, new_size, TARGET_PAGE_SIZE); |
f19412a2 AJ |
915 | |
916 | if (mmap_start == -1) { | |
917 | errno = ENOMEM; | |
918 | host_addr = MAP_FAILED; | |
68a1c816 | 919 | } else { |
3e8f1628 RH |
920 | host_addr = mremap(g2h_untagged(old_addr), old_size, new_size, |
921 | flags | MREMAP_FIXED, | |
922 | g2h_untagged(mmap_start)); | |
b76f21a7 | 923 | if (reserved_va) { |
558a4411 | 924 | mmap_reserve_or_unmap(old_addr, old_size); |
c65ffe6d | 925 | } |
68a1c816 | 926 | } |
3af72a4d | 927 | } else { |
ea800033 | 928 | int page_flags = 0; |
b76f21a7 | 929 | if (reserved_va && old_size < new_size) { |
68a1c816 PB |
930 | abi_ulong addr; |
931 | for (addr = old_addr + old_size; | |
932 | addr < old_addr + new_size; | |
933 | addr++) { | |
ea800033 | 934 | page_flags |= page_get_flags(addr); |
68a1c816 PB |
935 | } |
936 | } | |
ea800033 | 937 | if (page_flags == 0) { |
3e8f1628 RH |
938 | host_addr = mremap(g2h_untagged(old_addr), |
939 | old_size, new_size, flags); | |
56d19084 TK |
940 | |
941 | if (host_addr != MAP_FAILED) { | |
942 | /* Check if address fits target address space */ | |
46b12f46 | 943 | if (!guest_range_valid_untagged(h2g(host_addr), new_size)) { |
56d19084 | 944 | /* Revert mremap() changes */ |
3e8f1628 RH |
945 | host_addr = mremap(g2h_untagged(old_addr), |
946 | new_size, old_size, flags); | |
56d19084 TK |
947 | errno = ENOMEM; |
948 | host_addr = MAP_FAILED; | |
949 | } else if (reserved_va && old_size > new_size) { | |
558a4411 RH |
950 | mmap_reserve_or_unmap(old_addr + old_size, |
951 | old_size - new_size); | |
56d19084 | 952 | } |
68a1c816 PB |
953 | } |
954 | } else { | |
955 | errno = ENOMEM; | |
956 | host_addr = MAP_FAILED; | |
957 | } | |
f19412a2 AJ |
958 | } |
959 | ||
960 | if (host_addr == MAP_FAILED) { | |
c8a706fe PB |
961 | new_addr = -1; |
962 | } else { | |
963 | new_addr = h2g(host_addr); | |
964 | prot = page_get_flags(old_addr); | |
49840a4a | 965 | page_set_flags(old_addr, old_addr + old_size - 1, 0); |
044e95c8 | 966 | shm_region_rm_complete(old_addr, old_addr + old_size - 1); |
49840a4a | 967 | page_set_flags(new_addr, new_addr + new_size - 1, |
d9c58585 | 968 | prot | PAGE_VALID | PAGE_RESET); |
044e95c8 | 969 | shm_region_rm_complete(new_addr, new_addr + new_size - 1); |
c8a706fe PB |
970 | } |
971 | mmap_unlock(); | |
54936004 FB |
972 | return new_addr; |
973 | } | |
892a4f6a | 974 | |
892a4f6a IL |
975 | abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) |
976 | { | |
e230ec09 | 977 | abi_ulong len; |
892a4f6a IL |
978 | int ret = 0; |
979 | ||
980 | if (start & ~TARGET_PAGE_MASK) { | |
981 | return -TARGET_EINVAL; | |
982 | } | |
e230ec09 | 983 | if (len_in == 0) { |
892a4f6a IL |
984 | return 0; |
985 | } | |
e230ec09 RH |
986 | len = TARGET_PAGE_ALIGN(len_in); |
987 | if (len == 0 || !guest_range_valid_untagged(start, len)) { | |
892a4f6a IL |
988 | return -TARGET_EINVAL; |
989 | } | |
990 | ||
4530deb1 HD |
991 | /* Translate for some architectures which have different MADV_xxx values */ |
992 | switch (advice) { | |
993 | case TARGET_MADV_DONTNEED: /* alpha */ | |
994 | advice = MADV_DONTNEED; | |
995 | break; | |
996 | case TARGET_MADV_WIPEONFORK: /* parisc */ | |
997 | advice = MADV_WIPEONFORK; | |
998 | break; | |
999 | case TARGET_MADV_KEEPONFORK: /* parisc */ | |
1000 | advice = MADV_KEEPONFORK; | |
1001 | break; | |
1002 | /* we do not care about the other MADV_xxx values yet */ | |
1003 | } | |
1004 | ||
892a4f6a | 1005 | /* |
4530deb1 HD |
1006 | * Most advice values are hints, so ignoring and returning success is ok. |
1007 | * | |
1008 | * However, some advice values such as MADV_DONTNEED, MADV_WIPEONFORK and | |
1009 | * MADV_KEEPONFORK are not hints and need to be emulated. | |
892a4f6a | 1010 | * |
4530deb1 HD |
1011 | * A straight passthrough for those may not be safe because qemu sometimes |
1012 | * turns private file-backed mappings into anonymous mappings. | |
ecb796db RH |
1013 | * If all guest pages have PAGE_PASSTHROUGH set, mappings have the |
1014 | * same semantics for the host as for the guest. | |
892a4f6a | 1015 | * |
4530deb1 HD |
1016 | * We pass through MADV_WIPEONFORK and MADV_KEEPONFORK if possible and |
1017 | * return failure if not. | |
1018 | * | |
1019 | * MADV_DONTNEED is passed through as well, if possible. | |
1020 | * If passthrough isn't possible, we nevertheless (wrongly!) return | |
1021 | * success, which is broken but some userspace programs fail to work | |
1022 | * otherwise. Completely implementing such emulation is quite complicated | |
1023 | * though. | |
892a4f6a IL |
1024 | */ |
1025 | mmap_lock(); | |
4530deb1 HD |
1026 | switch (advice) { |
1027 | case MADV_WIPEONFORK: | |
1028 | case MADV_KEEPONFORK: | |
1029 | ret = -EINVAL; | |
1030 | /* fall through */ | |
1031 | case MADV_DONTNEED: | |
ecb796db | 1032 | if (page_check_range(start, len, PAGE_PASSTHROUGH)) { |
4530deb1 HD |
1033 | ret = get_errno(madvise(g2h_untagged(start), len, advice)); |
1034 | if ((advice == MADV_DONTNEED) && (ret == 0)) { | |
10310cbd | 1035 | page_reset_target_data(start, start + len - 1); |
4530deb1 | 1036 | } |
dbbf8975 | 1037 | } |
892a4f6a IL |
1038 | } |
1039 | mmap_unlock(); | |
1040 | ||
1041 | return ret; | |
1042 | } | |
225a206c RH |
1043 | |
1044 | #ifndef TARGET_FORCE_SHMLBA | |
1045 | /* | |
1046 | * For most architectures, SHMLBA is the same as the page size; | |
1047 | * some architectures have larger values, in which case they should | |
1048 | * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. | |
1049 | * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA | |
1050 | * and defining its own value for SHMLBA. | |
1051 | * | |
1052 | * The kernel also permits SHMLBA to be set by the architecture to a | |
1053 | * value larger than the page size without setting __ARCH_FORCE_SHMLBA; | |
1054 | * this means that addresses are rounded to the large size if | |
1055 | * SHM_RND is set but addresses not aligned to that size are not rejected | |
1056 | * as long as they are at least page-aligned. Since the only architecture | |
1057 | * which uses this is ia64 this code doesn't provide for that oddity. | |
1058 | */ | |
1059 | static inline abi_ulong target_shmlba(CPUArchState *cpu_env) | |
1060 | { | |
1061 | return TARGET_PAGE_SIZE; | |
1062 | } | |
1063 | #endif | |
1064 | ||
1065 | abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, | |
1066 | abi_ulong shmaddr, int shmflg) | |
1067 | { | |
1068 | CPUState *cpu = env_cpu(cpu_env); | |
1069 | abi_ulong raddr; | |
225a206c | 1070 | struct shmid_ds shm_info; |
69fa2708 | 1071 | int ret; |
225a206c RH |
1072 | abi_ulong shmlba; |
1073 | ||
1074 | /* shmat pointers are always untagged */ | |
1075 | ||
1076 | /* find out the length of the shared memory segment */ | |
1077 | ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); | |
1078 | if (is_error(ret)) { | |
1079 | /* can't get length, bail out */ | |
1080 | return ret; | |
1081 | } | |
1082 | ||
1083 | shmlba = target_shmlba(cpu_env); | |
1084 | ||
1085 | if (shmaddr & (shmlba - 1)) { | |
1086 | if (shmflg & SHM_RND) { | |
1087 | shmaddr &= ~(shmlba - 1); | |
1088 | } else { | |
1089 | return -TARGET_EINVAL; | |
1090 | } | |
1091 | } | |
1092 | if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { | |
1093 | return -TARGET_EINVAL; | |
1094 | } | |
1095 | ||
69fa2708 RH |
1096 | WITH_MMAP_LOCK_GUARD() { |
1097 | void *host_raddr; | |
044e95c8 | 1098 | abi_ulong last; |
69fa2708 RH |
1099 | |
1100 | if (shmaddr) { | |
1101 | host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); | |
1102 | } else { | |
1103 | abi_ulong mmap_start; | |
1104 | ||
1105 | /* In order to use the host shmat, we need to honor host SHMLBA. */ | |
1106 | mmap_start = mmap_find_vma(0, shm_info.shm_segsz, | |
1107 | MAX(SHMLBA, shmlba)); | |
1108 | ||
1109 | if (mmap_start == -1) { | |
1110 | return -TARGET_ENOMEM; | |
1111 | } | |
1112 | host_raddr = shmat(shmid, g2h_untagged(mmap_start), | |
1113 | shmflg | SHM_REMAP); | |
1114 | } | |
1115 | ||
1116 | if (host_raddr == (void *)-1) { | |
1117 | return get_errno(-1); | |
1118 | } | |
1119 | raddr = h2g(host_raddr); | |
044e95c8 | 1120 | last = raddr + shm_info.shm_segsz - 1; |
69fa2708 | 1121 | |
044e95c8 | 1122 | page_set_flags(raddr, last, |
69fa2708 RH |
1123 | PAGE_VALID | PAGE_RESET | PAGE_READ | |
1124 | (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); | |
1125 | ||
044e95c8 RH |
1126 | shm_region_rm_complete(raddr, last); |
1127 | shm_region_add(raddr, last); | |
69fa2708 | 1128 | } |
225a206c RH |
1129 | |
1130 | /* | |
1131 | * We're mapping shared memory, so ensure we generate code for parallel | |
1132 | * execution and flush old translations. This will work up to the level | |
1133 | * supported by the host -- anything that requires EXCP_ATOMIC will not | |
1134 | * be atomic with respect to an external process. | |
1135 | */ | |
1136 | if (!(cpu->tcg_cflags & CF_PARALLEL)) { | |
1137 | cpu->tcg_cflags |= CF_PARALLEL; | |
1138 | tb_flush(cpu); | |
1139 | } | |
1140 | ||
225a206c RH |
1141 | return raddr; |
1142 | } | |
1143 | ||
1144 | abi_long target_shmdt(abi_ulong shmaddr) | |
1145 | { | |
225a206c RH |
1146 | abi_long rv; |
1147 | ||
1148 | /* shmdt pointers are always untagged */ | |
1149 | ||
69fa2708 | 1150 | WITH_MMAP_LOCK_GUARD() { |
044e95c8 RH |
1151 | abi_ulong last = shm_region_find(shmaddr); |
1152 | if (last == 0) { | |
ceda5688 RH |
1153 | return -TARGET_EINVAL; |
1154 | } | |
1155 | ||
69fa2708 | 1156 | rv = get_errno(shmdt(g2h_untagged(shmaddr))); |
ceda5688 | 1157 | if (rv == 0) { |
044e95c8 | 1158 | abi_ulong size = last - shmaddr + 1; |
ceda5688 | 1159 | |
044e95c8 RH |
1160 | page_set_flags(shmaddr, last, 0); |
1161 | shm_region_rm_complete(shmaddr, last); | |
ceda5688 RH |
1162 | mmap_reserve_or_unmap(shmaddr, size); |
1163 | } | |
225a206c | 1164 | } |
225a206c RH |
1165 | return rv; |
1166 | } |