1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20
21#include "qemu.h"
22#include "qemu-common.h"
23
24static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
25static __thread int mmap_lock_count;
26
27void mmap_lock(void)
28{
29 if (mmap_lock_count++ == 0) {
30 pthread_mutex_lock(&mmap_mutex);
31 }
32}
33
34void mmap_unlock(void)
35{
36 if (--mmap_lock_count == 0) {
37 pthread_mutex_unlock(&mmap_mutex);
38 }
39}
40
41bool have_mmap_lock(void)
42{
43 return mmap_lock_count > 0 ? true : false;
44}
45
46
47void mmap_fork_start(void)
48{
49 if (mmap_lock_count)
50 abort();
51 pthread_mutex_lock(&mmap_mutex);
52}
53
54void mmap_fork_end(int child)
55{
56 if (child)
57 pthread_mutex_init(&mmap_mutex, NULL);
58 else
59 pthread_mutex_unlock(&mmap_mutex);
60}
61
62
63int target_mprotect(abi_ulong start, abi_ulong len, int prot)
64{
65 abi_ulong end, host_start, host_end, addr;
66 int prot1, ret;
67
68 qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
69 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
70 prot & PROT_READ ? 'r' : '-',
71 prot & PROT_WRITE ? 'w' : '-',
72 prot & PROT_EXEC ? 'x' : '-');
73 if ((start & ~TARGET_PAGE_MASK) != 0)
74 return -EINVAL;
75 len = TARGET_PAGE_ALIGN(len);
76 end = start + len;
77 if (end < start)
78 return -EINVAL;
79 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
80 if (len == 0)
81 return 0;
82
83 mmap_lock();
84 host_start = start & qemu_host_page_mask;
85 host_end = HOST_PAGE_ALIGN(end);
86 if (start > host_start) {
87
88 prot1 = prot;
89 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
90 prot1 |= page_get_flags(addr);
91 }
92 if (host_end == host_start + qemu_host_page_size) {
93 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
94 prot1 |= page_get_flags(addr);
95 }
96 end = host_end;
97 }
98 ret = mprotect(g2h_untagged(host_start),
99 qemu_host_page_size, prot1 & PAGE_BITS);
100 if (ret != 0)
101 goto error;
102 host_start += qemu_host_page_size;
103 }
104 if (end < host_end) {
105 prot1 = prot;
106 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
107 prot1 |= page_get_flags(addr);
108 }
109 ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
110 qemu_host_page_size, prot1 & PAGE_BITS);
111 if (ret != 0)
112 goto error;
113 host_end -= qemu_host_page_size;
114 }
115
116
117 if (host_start < host_end) {
118 ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
119 if (ret != 0)
120 goto error;
121 }
122 page_set_flags(start, start + len, prot | PAGE_VALID);
123 mmap_unlock();
124 return 0;
125error:
126 mmap_unlock();
127 return ret;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151static int mmap_frag(abi_ulong real_start,
152 abi_ulong start, abi_ulong end,
153 int prot, int flags, int fd, abi_ulong offset)
154{
155 abi_ulong real_end, addr;
156 void *host_start;
157 int prot1, prot_new;
158
159 real_end = real_start + qemu_host_page_size;
160 host_start = g2h_untagged(real_start);
161
162
163 prot1 = 0;
164 for (addr = real_start; addr < real_end; addr++) {
165 if (addr < start || addr >= end)
166 prot1 |= page_get_flags(addr);
167 }
168
169 if (prot1 == 0) {
170
171 void *p = mmap(host_start, qemu_host_page_size, prot,
172 flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
173 if (p == MAP_FAILED)
174 return -1;
175 prot1 = prot;
176 }
177 prot1 &= PAGE_BITS;
178
179 prot_new = prot | prot1;
180 if (fd != -1) {
181
182
183 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
184 (prot & PROT_WRITE))
185 return -1;
186
187
188 if (!(prot1 & PROT_WRITE))
189 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
190
191
192 if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
193 return -1;
194 }
195
196
197 if (prot_new != (prot1 | PROT_WRITE))
198 mprotect(host_start, qemu_host_page_size, prot_new);
199 } else {
200 if (prot_new != prot1) {
201 mprotect(host_start, qemu_host_page_size, prot_new);
202 }
203 if (prot_new & PROT_WRITE) {
204 memset(g2h_untagged(start), 0, end - start);
205 }
206 }
207 return 0;
208}
209
210#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
211# define TASK_UNMAPPED_BASE (1ul << 38)
212#else
213# define TASK_UNMAPPED_BASE 0x40000000
214#endif
215abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
216
217unsigned long last_brk;
218
219
220
221
222
223static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
224 abi_ulong alignment)
225{
226 abi_ulong addr;
227 abi_ulong end_addr;
228 int prot;
229 int looped = 0;
230
231 if (size > reserved_va) {
232 return (abi_ulong)-1;
233 }
234
235 size = HOST_PAGE_ALIGN(size) + alignment;
236 end_addr = start + size;
237 if (end_addr > reserved_va) {
238 end_addr = reserved_va;
239 }
240 addr = end_addr - qemu_host_page_size;
241
242 while (1) {
243 if (addr > end_addr) {
244 if (looped) {
245 return (abi_ulong)-1;
246 }
247 end_addr = reserved_va;
248 addr = end_addr - qemu_host_page_size;
249 looped = 1;
250 continue;
251 }
252 prot = page_get_flags(addr);
253 if (prot) {
254 end_addr = addr;
255 }
256 if (end_addr - addr >= size) {
257 break;
258 }
259 addr -= qemu_host_page_size;
260 }
261
262 if (start == mmap_next_start) {
263 mmap_next_start = addr;
264 }
265
266 if (alignment != 0) {
267 addr = (addr + alignment) & ~(alignment - 1);
268 }
269 return addr;
270}
271
272
273
274
275
276
277
278static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
279 abi_ulong alignment)
280{
281 void *ptr, *prev;
282 abi_ulong addr;
283 int flags;
284 int wrapped, repeat;
285
286
287 if (start == 0) {
288 start = mmap_next_start;
289 } else {
290 start &= qemu_host_page_mask;
291 }
292
293 size = HOST_PAGE_ALIGN(size);
294
295 if (reserved_va) {
296 return mmap_find_vma_reserved(start, size,
297 (alignment != 0 ? 1 << alignment : 0));
298 }
299
300 addr = start;
301 wrapped = repeat = 0;
302 prev = 0;
303 flags = MAP_ANON | MAP_PRIVATE;
304 if (alignment != 0) {
305 flags |= MAP_ALIGNED(alignment);
306 }
307
308 for (;; prev = ptr) {
309
310
311
312
313
314
315
316 ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
317 flags, -1, 0);
318
319
320 if (ptr == MAP_FAILED) {
321 return (abi_ulong)-1;
322 }
323
324
325
326
327
328 repeat = (ptr == prev ? repeat + 1 : 0);
329
330 if (h2g_valid(ptr + size - 1)) {
331 addr = h2g(ptr);
332
333 if ((addr & ~TARGET_PAGE_MASK) == 0) {
334
335 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
336 mmap_next_start = addr + size;
337 }
338 return addr;
339 }
340
341
342 switch (repeat) {
343 case 0:
344
345
346
347
348
349 addr = TARGET_PAGE_ALIGN(addr);
350 break;
351 case 1:
352
353
354
355
356 addr &= TARGET_PAGE_MASK;
357 break;
358 case 2:
359
360 addr = 0;
361 break;
362 default:
363
364 addr = -1;
365 break;
366 }
367 } else {
368
369
370
371
372 addr = (repeat ? -1 : 0);
373 }
374
375
376 munmap(ptr, size);
377
378
379 if (addr == (abi_ulong)-1) {
380 return (abi_ulong)-1;
381 } else if (addr == 0) {
382 if (wrapped) {
383 return (abi_ulong)-1;
384 }
385 wrapped = 1;
386
387
388
389
390 addr = TARGET_PAGE_SIZE;
391 } else if (wrapped && addr >= start) {
392 return (abi_ulong)-1;
393 }
394 }
395}
396
397abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
398{
399 return mmap_find_vma_aligned(start, size, 0);
400}
401
402
403abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
404 int flags, int fd, off_t offset)
405{
406 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
407
408 mmap_lock();
409 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
410 qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
411 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
412 start, len,
413 prot & PROT_READ ? 'r' : '-',
414 prot & PROT_WRITE ? 'w' : '-',
415 prot & PROT_EXEC ? 'x' : '-');
416 if (flags & MAP_ALIGNMENT_MASK) {
417 qemu_log("MAP_ALIGNED(%u) ",
418 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
419 }
420 if (flags & MAP_GUARD) {
421 qemu_log("MAP_GUARD ");
422 }
423 if (flags & MAP_FIXED) {
424 qemu_log("MAP_FIXED ");
425 }
426 if (flags & MAP_ANON) {
427 qemu_log("MAP_ANON ");
428 }
429 if (flags & MAP_EXCL) {
430 qemu_log("MAP_EXCL ");
431 }
432 if (flags & MAP_PRIVATE) {
433 qemu_log("MAP_PRIVATE ");
434 }
435 if (flags & MAP_SHARED) {
436 qemu_log("MAP_SHARED ");
437 }
438 if (flags & MAP_NOCORE) {
439 qemu_log("MAP_NOCORE ");
440 }
441 if (flags & MAP_STACK) {
442 qemu_log("MAP_STACK ");
443 }
444 qemu_log("fd=%d offset=0x%lx\n", fd, offset);
445 }
446
447 if ((flags & MAP_ANON) && fd != -1) {
448 errno = EINVAL;
449 goto fail;
450 }
451 if (flags & MAP_STACK) {
452 if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
453 (PROT_READ | PROT_WRITE))) {
454 errno = EINVAL;
455 goto fail;
456 }
457 }
458 if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
459 offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
460
461 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
462 errno = EINVAL;
463 goto fail;
464 }
465
466 if (offset & ~TARGET_PAGE_MASK) {
467 errno = EINVAL;
468 goto fail;
469 }
470
471 if (len == 0) {
472 errno = EINVAL;
473 goto fail;
474 }
475
476
477 len = TARGET_PAGE_ALIGN(len);
478 if (len == 0) {
479 errno = ENOMEM;
480 goto fail;
481 }
482
483 real_start = start & qemu_host_page_mask;
484 host_offset = offset & qemu_host_page_mask;
485
486
487
488
489
490 if (!(flags & MAP_FIXED)) {
491 host_len = len + offset - host_offset;
492 host_len = HOST_PAGE_ALIGN(host_len);
493 if ((flags & MAP_ALIGNMENT_MASK) != 0)
494 start = mmap_find_vma_aligned(real_start, host_len,
495 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
496 else
497 start = mmap_find_vma(real_start, host_len);
498 if (start == (abi_ulong)-1) {
499 errno = ENOMEM;
500 goto fail;
501 }
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) {
519 struct stat sb;
520
521 if (fstat(fd, &sb) == -1) {
522 goto fail;
523 }
524
525
526 if (offset + len > sb.st_size) {
527
528
529
530
531
532 len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
533 }
534 }
535
536 if (!(flags & MAP_FIXED)) {
537 unsigned long host_start;
538 void *p;
539
540 host_len = len + offset - host_offset;
541 host_len = HOST_PAGE_ALIGN(host_len);
542
543
544
545
546
547
548 p = mmap(g2h_untagged(start), host_len, prot,
549 flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
550 if (p == MAP_FAILED)
551 goto fail;
552
553 host_start = (unsigned long)p;
554 if (fd != -1) {
555 p = mmap(g2h_untagged(start), len, prot,
556 flags | MAP_FIXED, fd, host_offset);
557 if (p == MAP_FAILED) {
558 munmap(g2h_untagged(start), host_len);
559 goto fail;
560 }
561 host_start += offset - host_offset;
562 }
563 start = h2g(host_start);
564 } else {
565 if (start & ~TARGET_PAGE_MASK) {
566 errno = EINVAL;
567 goto fail;
568 }
569 end = start + len;
570 real_end = HOST_PAGE_ALIGN(end);
571
572
573
574
575
576
577 if (!guest_range_valid_untagged(start, len)) {
578 errno = EINVAL;
579 goto fail;
580 }
581
582
583
584
585
586 if (fd != -1 &&
587 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
588
589
590
591
592 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
593 (prot & PROT_WRITE)) {
594 errno = EINVAL;
595 goto fail;
596 }
597 retaddr = target_mmap(start, len, prot | PROT_WRITE,
598 MAP_FIXED | MAP_PRIVATE | MAP_ANON,
599 -1, 0);
600 if (retaddr == -1)
601 goto fail;
602 if (pread(fd, g2h_untagged(start), len, offset) == -1) {
603 goto fail;
604 }
605 if (!(prot & PROT_WRITE)) {
606 ret = target_mprotect(start, len, prot);
607 assert(ret == 0);
608 }
609 goto the_end;
610 }
611
612
613 if ((flags & MAP_EXCL) && page_check_range(start, len, 0) < 0) {
614 errno = EINVAL;
615 goto fail;
616 }
617
618
619 if (start > real_start) {
620 if (real_end == real_start + qemu_host_page_size) {
621
622 ret = mmap_frag(real_start, start, end,
623 prot, flags, fd, offset);
624 if (ret == -1)
625 goto fail;
626 goto the_end1;
627 }
628 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
629 prot, flags, fd, offset);
630 if (ret == -1)
631 goto fail;
632 real_start += qemu_host_page_size;
633 }
634
635 if (end < real_end) {
636 ret = mmap_frag(real_end - qemu_host_page_size,
637 real_end - qemu_host_page_size, end,
638 prot, flags, fd,
639 offset + real_end - qemu_host_page_size - start);
640 if (ret == -1)
641 goto fail;
642 real_end -= qemu_host_page_size;
643 }
644
645
646 if (real_start < real_end) {
647 void *p;
648 unsigned long offset1;
649 if (flags & MAP_ANON)
650 offset1 = 0;
651 else
652 offset1 = offset + real_start - start;
653 p = mmap(g2h_untagged(real_start), real_end - real_start,
654 prot, flags, fd, offset1);
655 if (p == MAP_FAILED)
656 goto fail;
657 }
658 }
659 the_end1:
660 page_set_flags(start, start + len, prot | PAGE_VALID);
661 the_end:
662#ifdef DEBUG_MMAP
663 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
664 page_dump(stdout);
665 printf("\n");
666#endif
667 tb_invalidate_phys_range(start, start + len);
668 mmap_unlock();
669 return start;
670fail:
671 mmap_unlock();
672 return -1;
673}
674
675static void mmap_reserve(abi_ulong start, abi_ulong size)
676{
677 abi_ulong real_start;
678 abi_ulong real_end;
679 abi_ulong addr;
680 abi_ulong end;
681 int prot;
682
683 real_start = start & qemu_host_page_mask;
684 real_end = HOST_PAGE_ALIGN(start + size);
685 end = start + size;
686 if (start > real_start) {
687
688 prot = 0;
689 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
690 prot |= page_get_flags(addr);
691 }
692 if (real_end == real_start + qemu_host_page_size) {
693 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
694 prot |= page_get_flags(addr);
695 }
696 end = real_end;
697 }
698 if (prot != 0) {
699 real_start += qemu_host_page_size;
700 }
701 }
702 if (end < real_end) {
703 prot = 0;
704 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
705 prot |= page_get_flags(addr);
706 }
707 if (prot != 0) {
708 real_end -= qemu_host_page_size;
709 }
710 }
711 if (real_start != real_end) {
712 mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
713 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
714 }
715}
716
717int target_munmap(abi_ulong start, abi_ulong len)
718{
719 abi_ulong end, real_start, real_end, addr;
720 int prot, ret;
721
722#ifdef DEBUG_MMAP
723 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
724 TARGET_ABI_FMT_lx "\n",
725 start, len);
726#endif
727 if (start & ~TARGET_PAGE_MASK)
728 return -EINVAL;
729 len = TARGET_PAGE_ALIGN(len);
730 if (len == 0)
731 return -EINVAL;
732 mmap_lock();
733 end = start + len;
734 real_start = start & qemu_host_page_mask;
735 real_end = HOST_PAGE_ALIGN(end);
736
737 if (start > real_start) {
738
739 prot = 0;
740 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
741 prot |= page_get_flags(addr);
742 }
743 if (real_end == real_start + qemu_host_page_size) {
744 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
745 prot |= page_get_flags(addr);
746 }
747 end = real_end;
748 }
749 if (prot != 0)
750 real_start += qemu_host_page_size;
751 }
752 if (end < real_end) {
753 prot = 0;
754 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
755 prot |= page_get_flags(addr);
756 }
757 if (prot != 0)
758 real_end -= qemu_host_page_size;
759 }
760
761 ret = 0;
762
763 if (real_start < real_end) {
764 if (reserved_va) {
765 mmap_reserve(real_start, real_end - real_start);
766 } else {
767 ret = munmap(g2h_untagged(real_start), real_end - real_start);
768 }
769 }
770
771 if (ret == 0) {
772 page_set_flags(start, start + len, 0);
773 tb_invalidate_phys_range(start, start + len);
774 }
775 mmap_unlock();
776 return ret;
777}
778
779int target_msync(abi_ulong start, abi_ulong len, int flags)
780{
781 abi_ulong end;
782
783 if (start & ~TARGET_PAGE_MASK)
784 return -EINVAL;
785 len = TARGET_PAGE_ALIGN(len);
786 end = start + len;
787 if (end < start)
788 return -EINVAL;
789 if (end == start)
790 return 0;
791
792 start &= qemu_host_page_mask;
793 return msync(g2h_untagged(start), end - start, flags);
794}
795