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