change debug_out format of addr to JZ_FMT_plx
[qemu/qemu-JZ.git] / linux-user / mmap.c
blob8e81cdec74f363d3aaf2f617c0e396adc9e49599
1 /*
2 * mmap support for qemu
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 * MA 02110-1301, USA.
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/mman.h>
28 #include <linux/mman.h>
29 #include <linux/unistd.h>
31 #include "qemu.h"
32 #include "qemu-common.h"
34 //#define DEBUG_MMAP
36 #if defined(USE_NPTL)
37 pthread_mutex_t mmap_mutex;
38 static int __thread mmap_lock_count;
40 void mmap_lock(void)
42 if (mmap_lock_count++ == 0) {
43 pthread_mutex_lock(&mmap_mutex);
47 void mmap_unlock(void)
49 if (--mmap_lock_count == 0) {
50 pthread_mutex_unlock(&mmap_mutex);
54 /* Grab lock to make sure things are in a consistent state after fork(). */
55 void mmap_fork_start(void)
57 if (mmap_lock_count)
58 abort();
59 pthread_mutex_lock(&mmap_mutex);
62 void mmap_fork_end(int child)
64 if (child)
65 pthread_mutex_init(&mmap_mutex, NULL);
66 else
67 pthread_mutex_unlock(&mmap_mutex);
69 #else
70 /* We aren't threadsafe to start with, so no need to worry about locking. */
71 void mmap_lock(void)
75 void mmap_unlock(void)
78 #endif
80 void *qemu_vmalloc(size_t size)
82 void *p;
83 unsigned long addr;
84 mmap_lock();
85 /* Use map and mark the pages as used. */
86 p = mmap(NULL, size, PROT_READ | PROT_WRITE,
87 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
89 addr = (unsigned long)p;
90 if (addr == (target_ulong) addr) {
91 /* Allocated region overlaps guest address space.
92 This may recurse. */
93 page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size),
94 PAGE_RESERVED);
97 mmap_unlock();
98 return p;
101 void *qemu_malloc(size_t size)
103 char * p;
104 size += 16;
105 p = qemu_vmalloc(size);
106 *(size_t *)p = size;
107 return p + 16;
110 /* We use map, which is always zero initialized. */
111 void * qemu_mallocz(size_t size)
113 return qemu_malloc(size);
116 void qemu_free(void *ptr)
118 /* FIXME: We should unmark the reserved pages here. However this gets
119 complicated when one target page spans multiple host pages, so we
120 don't bother. */
121 size_t *p;
122 p = (size_t *)((char *)ptr - 16);
123 munmap(p, *p);
126 /* NOTE: all the constants are the HOST ones, but addresses are target. */
127 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
129 abi_ulong end, host_start, host_end, addr;
130 int prot1, ret;
132 #ifdef DEBUG_MMAP
133 printf("mprotect: start=0x" TARGET_FMT_lx
134 "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
135 prot & PROT_READ ? 'r' : '-',
136 prot & PROT_WRITE ? 'w' : '-',
137 prot & PROT_EXEC ? 'x' : '-');
138 #endif
140 if ((start & ~TARGET_PAGE_MASK) != 0)
141 return -EINVAL;
142 len = TARGET_PAGE_ALIGN(len);
143 end = start + len;
144 if (end < start)
145 return -EINVAL;
146 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
147 if (len == 0)
148 return 0;
150 mmap_lock();
151 host_start = start & qemu_host_page_mask;
152 host_end = HOST_PAGE_ALIGN(end);
153 if (start > host_start) {
154 /* handle host page containing start */
155 prot1 = prot;
156 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
157 prot1 |= page_get_flags(addr);
159 if (host_end == host_start + qemu_host_page_size) {
160 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
161 prot1 |= page_get_flags(addr);
163 end = host_end;
165 ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
166 if (ret != 0)
167 goto error;
168 host_start += qemu_host_page_size;
170 if (end < host_end) {
171 prot1 = prot;
172 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
173 prot1 |= page_get_flags(addr);
175 ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
176 prot1 & PAGE_BITS);
177 if (ret != 0)
178 goto error;
179 host_end -= qemu_host_page_size;
182 /* handle the pages in the middle */
183 if (host_start < host_end) {
184 ret = mprotect(g2h(host_start), host_end - host_start, prot);
185 if (ret != 0)
186 goto error;
188 page_set_flags(start, start + len, prot | PAGE_VALID);
189 mmap_unlock();
190 return 0;
191 error:
192 mmap_unlock();
193 return ret;
196 /* map an incomplete host page */
197 static int mmap_frag(abi_ulong real_start,
198 abi_ulong start, abi_ulong end,
199 int prot, int flags, int fd, abi_ulong offset)
201 abi_ulong real_end, addr;
202 void *host_start;
203 int prot1, prot_new;
205 real_end = real_start + qemu_host_page_size;
206 host_start = g2h(real_start);
208 /* get the protection of the target pages outside the mapping */
209 prot1 = 0;
210 for(addr = real_start; addr < real_end; addr++) {
211 if (addr < start || addr >= end)
212 prot1 |= page_get_flags(addr);
215 if (prot1 == 0) {
216 /* no page was there, so we allocate one */
217 void *p = mmap(host_start, qemu_host_page_size, prot,
218 flags | MAP_ANONYMOUS, -1, 0);
219 if (p == MAP_FAILED)
220 return -1;
221 prot1 = prot;
223 prot1 &= PAGE_BITS;
225 prot_new = prot | prot1;
226 if (!(flags & MAP_ANONYMOUS)) {
227 /* msync() won't work here, so we return an error if write is
228 possible while it is a shared mapping */
229 if ((flags & MAP_TYPE) == MAP_SHARED &&
230 (prot & PROT_WRITE))
231 return -EINVAL;
233 /* adjust protection to be able to read */
234 if (!(prot1 & PROT_WRITE))
235 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
237 /* read the corresponding file data */
238 pread(fd, g2h(start), end - start, offset);
240 /* put final protection */
241 if (prot_new != (prot1 | PROT_WRITE))
242 mprotect(host_start, qemu_host_page_size, prot_new);
243 } else {
244 /* just update the protection */
245 if (prot_new != prot1) {
246 mprotect(host_start, qemu_host_page_size, prot_new);
249 return 0;
252 #if defined(__CYGWIN__)
253 /* Cygwin doesn't have a whole lot of address space. */
254 static abi_ulong mmap_next_start = 0x18000000;
255 #else
256 static abi_ulong mmap_next_start = 0x40000000;
257 #endif
259 unsigned long last_brk;
261 /* find a free memory area of size 'size'. The search starts at
262 'start'. If 'start' == 0, then a default start address is used.
263 Return -1 if error.
265 /* page_init() marks pages used by the host as reserved to be sure not
266 to use them. */
267 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
269 abi_ulong addr, addr1, addr_start;
270 int prot;
271 unsigned long new_brk;
273 new_brk = (unsigned long)sbrk(0);
274 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
275 /* This is a hack to catch the host allocating memory with brk().
276 If it uses mmap then we loose.
277 FIXME: We really want to avoid the host allocating memory in
278 the first place, and maybe leave some slack to avoid switching
279 to mmap. */
280 page_set_flags(last_brk & TARGET_PAGE_MASK,
281 TARGET_PAGE_ALIGN(new_brk),
282 PAGE_RESERVED);
284 last_brk = new_brk;
286 size = HOST_PAGE_ALIGN(size);
287 start = start & qemu_host_page_mask;
288 addr = start;
289 if (addr == 0)
290 addr = mmap_next_start;
291 addr_start = addr;
292 for(;;) {
293 prot = 0;
294 for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
295 prot |= page_get_flags(addr1);
297 if (prot == 0)
298 break;
299 addr += qemu_host_page_size;
300 /* we found nothing */
301 if (addr == addr_start)
302 return (abi_ulong)-1;
304 if (start == 0)
305 mmap_next_start = addr + size;
306 return addr;
309 /* NOTE: all the constants are the HOST ones */
310 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
311 int flags, int fd, abi_ulong offset)
313 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
314 unsigned long host_start;
316 mmap_lock();
317 #ifdef DEBUG_MMAP
319 printf("mmap: start=0x" TARGET_FMT_lx
320 " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
321 start, len,
322 prot & PROT_READ ? 'r' : '-',
323 prot & PROT_WRITE ? 'w' : '-',
324 prot & PROT_EXEC ? 'x' : '-');
325 if (flags & MAP_FIXED)
326 printf("MAP_FIXED ");
327 if (flags & MAP_ANONYMOUS)
328 printf("MAP_ANON ");
329 switch(flags & MAP_TYPE) {
330 case MAP_PRIVATE:
331 printf("MAP_PRIVATE ");
332 break;
333 case MAP_SHARED:
334 printf("MAP_SHARED ");
335 break;
336 default:
337 printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
338 break;
340 printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
342 #endif
344 if (offset & ~TARGET_PAGE_MASK) {
345 errno = EINVAL;
346 goto fail;
349 len = TARGET_PAGE_ALIGN(len);
350 if (len == 0)
351 goto the_end;
352 real_start = start & qemu_host_page_mask;
354 if (!(flags & MAP_FIXED)) {
355 abi_ulong mmap_start;
356 void *p;
357 host_offset = offset & qemu_host_page_mask;
358 host_len = len + offset - host_offset;
359 host_len = HOST_PAGE_ALIGN(host_len);
360 mmap_start = mmap_find_vma(real_start, host_len);
361 if (mmap_start == (abi_ulong)-1) {
362 errno = ENOMEM;
363 goto fail;
365 /* Note: we prefer to control the mapping address. It is
366 especially important if qemu_host_page_size >
367 qemu_real_host_page_size */
368 p = mmap(g2h(mmap_start),
369 host_len, prot, flags | MAP_FIXED, fd, host_offset);
370 if (p == MAP_FAILED)
371 goto fail;
372 /* update start so that it points to the file position at 'offset' */
373 host_start = (unsigned long)p;
374 if (!(flags & MAP_ANONYMOUS))
375 host_start += offset - host_offset;
376 start = h2g(host_start);
377 } else {
378 int flg;
379 target_ulong addr;
381 if (start & ~TARGET_PAGE_MASK) {
382 errno = EINVAL;
383 goto fail;
385 end = start + len;
386 real_end = HOST_PAGE_ALIGN(end);
389 * Test if requested memory area fits target address space
390 * It can fail only on 64-bit host with 32-bit target.
391 * On any other target/host host mmap() handles this error correctly.
393 if ((unsigned long)start + len - 1 > (abi_ulong) -1) {
394 errno = EINVAL;
395 goto fail;
398 for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
399 flg = page_get_flags(addr);
400 if (flg & PAGE_RESERVED) {
401 errno = ENXIO;
402 goto fail;
406 /* worst case: we cannot map the file because the offset is not
407 aligned, so we read it */
408 if (!(flags & MAP_ANONYMOUS) &&
409 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
410 /* msync() won't work here, so we return an error if write is
411 possible while it is a shared mapping */
412 if ((flags & MAP_TYPE) == MAP_SHARED &&
413 (prot & PROT_WRITE)) {
414 errno = EINVAL;
415 goto fail;
417 retaddr = target_mmap(start, len, prot | PROT_WRITE,
418 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
419 -1, 0);
420 if (retaddr == -1)
421 goto fail;
422 pread(fd, g2h(start), len, offset);
423 if (!(prot & PROT_WRITE)) {
424 ret = target_mprotect(start, len, prot);
425 if (ret != 0) {
426 start = ret;
427 goto the_end;
430 goto the_end;
433 /* handle the start of the mapping */
434 if (start > real_start) {
435 if (real_end == real_start + qemu_host_page_size) {
436 /* one single host page */
437 ret = mmap_frag(real_start, start, end,
438 prot, flags, fd, offset);
439 if (ret == -1)
440 goto fail;
441 goto the_end1;
443 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
444 prot, flags, fd, offset);
445 if (ret == -1)
446 goto fail;
447 real_start += qemu_host_page_size;
449 /* handle the end of the mapping */
450 if (end < real_end) {
451 ret = mmap_frag(real_end - qemu_host_page_size,
452 real_end - qemu_host_page_size, real_end,
453 prot, flags, fd,
454 offset + real_end - qemu_host_page_size - start);
455 if (ret == -1)
456 goto fail;
457 real_end -= qemu_host_page_size;
460 /* map the middle (easier) */
461 if (real_start < real_end) {
462 void *p;
463 unsigned long offset1;
464 if (flags & MAP_ANONYMOUS)
465 offset1 = 0;
466 else
467 offset1 = offset + real_start - start;
468 p = mmap(g2h(real_start), real_end - real_start,
469 prot, flags, fd, offset1);
470 if (p == MAP_FAILED)
471 goto fail;
474 the_end1:
475 page_set_flags(start, start + len, prot | PAGE_VALID);
476 the_end:
477 #ifdef DEBUG_MMAP
478 printf("ret=0x" TARGET_FMT_lx "\n", start);
479 page_dump(stdout);
480 printf("\n");
481 #endif
482 mmap_unlock();
483 return start;
484 fail:
485 mmap_unlock();
486 return -1;
489 int target_munmap(abi_ulong start, abi_ulong len)
491 abi_ulong end, real_start, real_end, addr;
492 int prot, ret;
494 #ifdef DEBUG_MMAP
495 printf("munmap: start=0x%lx len=0x%lx\n", start, len);
496 #endif
497 if (start & ~TARGET_PAGE_MASK)
498 return -EINVAL;
499 len = TARGET_PAGE_ALIGN(len);
500 if (len == 0)
501 return -EINVAL;
502 mmap_lock();
503 end = start + len;
504 real_start = start & qemu_host_page_mask;
505 real_end = HOST_PAGE_ALIGN(end);
507 if (start > real_start) {
508 /* handle host page containing start */
509 prot = 0;
510 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
511 prot |= page_get_flags(addr);
513 if (real_end == real_start + qemu_host_page_size) {
514 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
515 prot |= page_get_flags(addr);
517 end = real_end;
519 if (prot != 0)
520 real_start += qemu_host_page_size;
522 if (end < real_end) {
523 prot = 0;
524 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
525 prot |= page_get_flags(addr);
527 if (prot != 0)
528 real_end -= qemu_host_page_size;
531 ret = 0;
532 /* unmap what we can */
533 if (real_start < real_end) {
534 ret = munmap(g2h(real_start), real_end - real_start);
537 if (ret == 0)
538 page_set_flags(start, start + len, 0);
539 mmap_unlock();
540 return ret;
543 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
544 abi_ulong new_size, unsigned long flags,
545 abi_ulong new_addr)
547 int prot;
548 void *host_addr;
550 mmap_lock();
552 if (flags & MREMAP_FIXED)
553 host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
554 old_size, new_size,
555 flags,
556 new_addr);
557 else if (flags & MREMAP_MAYMOVE) {
558 abi_ulong mmap_start;
560 mmap_start = mmap_find_vma(0, new_size);
562 if (mmap_start == -1) {
563 errno = ENOMEM;
564 host_addr = MAP_FAILED;
565 } else
566 host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
567 old_size, new_size,
568 flags | MREMAP_FIXED,
569 g2h(mmap_start));
570 } else {
571 host_addr = mremap(g2h(old_addr), old_size, new_size, flags);
572 /* Check if address fits target address space */
573 if ((unsigned long)host_addr + new_size > (abi_ulong)-1) {
574 /* Revert mremap() changes */
575 host_addr = mremap(g2h(old_addr), new_size, old_size, flags);
576 errno = ENOMEM;
577 host_addr = MAP_FAILED;
581 if (host_addr == MAP_FAILED) {
582 new_addr = -1;
583 } else {
584 new_addr = h2g(host_addr);
585 prot = page_get_flags(old_addr);
586 page_set_flags(old_addr, old_addr + old_size, 0);
587 page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
589 mmap_unlock();
590 return new_addr;
593 int target_msync(abi_ulong start, abi_ulong len, int flags)
595 abi_ulong end;
597 if (start & ~TARGET_PAGE_MASK)
598 return -EINVAL;
599 len = TARGET_PAGE_ALIGN(len);
600 end = start + len;
601 if (end < start)
602 return -EINVAL;
603 if (end == start)
604 return 0;
606 start &= qemu_host_page_mask;
607 return msync(g2h(start), end - start, flags);