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#include "bsd-mman.h"
24#include "exec/exec-all.h"
25
26
27
28static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
29static __thread int mmap_lock_count;
30
31void mmap_lock(void)
32{
33 if (mmap_lock_count++ == 0) {
34 pthread_mutex_lock(&mmap_mutex);
35 }
36}
37
38void mmap_unlock(void)
39{
40 if (--mmap_lock_count == 0) {
41 pthread_mutex_unlock(&mmap_mutex);
42 }
43}
44
45bool have_mmap_lock(void)
46{
47 return mmap_lock_count > 0 ? true : false;
48}
49
50
51void mmap_fork_start(void)
52{
53 if (mmap_lock_count)
54 abort();
55 pthread_mutex_lock(&mmap_mutex);
56}
57
58void mmap_fork_end(int child)
59{
60 if (child)
61 pthread_mutex_init(&mmap_mutex, NULL);
62 else
63 pthread_mutex_unlock(&mmap_mutex);
64}
65
66
67int target_mprotect(abi_ulong start, abi_ulong len, int prot)
68{
69 abi_ulong end, host_start, host_end, addr;
70 int prot1, ret;
71
72#ifdef DEBUG_MMAP
73 printf("mprotect: start=0x" TARGET_FMT_lx
74 " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
75 prot & PROT_READ ? 'r' : '-',
76 prot & PROT_WRITE ? 'w' : '-',
77 prot & PROT_EXEC ? 'x' : '-');
78#endif
79
80 if ((start & ~TARGET_PAGE_MASK) != 0)
81 return -EINVAL;
82 len = TARGET_PAGE_ALIGN(len);
83 end = start + len;
84 if (end < start)
85 return -EINVAL;
86 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
87 if (len == 0)
88 return 0;
89
90 mmap_lock();
91 host_start = start & qemu_host_page_mask;
92 host_end = HOST_PAGE_ALIGN(end);
93 if (start > host_start) {
94
95 prot1 = prot;
96 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
97 prot1 |= page_get_flags(addr);
98 }
99 if (host_end == host_start + qemu_host_page_size) {
100 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
101 prot1 |= page_get_flags(addr);
102 }
103 end = host_end;
104 }
105 ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
106 if (ret != 0)
107 goto error;
108 host_start += qemu_host_page_size;
109 }
110 if (end < host_end) {
111 prot1 = prot;
112 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
113 prot1 |= page_get_flags(addr);
114 }
115 ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
116 prot1 & PAGE_BITS);
117 if (ret != 0)
118 goto error;
119 host_end -= qemu_host_page_size;
120 }
121
122
123 if (host_start < host_end) {
124 ret = mprotect(g2h(host_start), host_end - host_start, prot);
125 if (ret != 0)
126 goto error;
127 }
128 page_set_flags(start, start + len, prot | PAGE_VALID);
129 mmap_unlock();
130 return 0;
131error:
132 mmap_unlock();
133 return ret;
134}
135
136
137static int mmap_frag(abi_ulong real_start,
138 abi_ulong start, abi_ulong end,
139 int prot, int flags, int fd, abi_ulong offset)
140{
141 abi_ulong real_end, addr;
142 void *host_start;
143 int prot1, prot_new;
144
145 real_end = real_start + qemu_host_page_size;
146 host_start = g2h(real_start);
147
148
149 prot1 = 0;
150 for(addr = real_start; addr < real_end; addr++) {
151 if (addr < start || addr >= end)
152 prot1 |= page_get_flags(addr);
153 }
154
155 if (prot1 == 0) {
156
157 void *p = mmap(host_start, qemu_host_page_size, prot,
158 flags | MAP_ANON, -1, 0);
159 if (p == MAP_FAILED)
160 return -1;
161 prot1 = prot;
162 }
163 prot1 &= PAGE_BITS;
164
165 prot_new = prot | prot1;
166 if (!(flags & MAP_ANON)) {
167
168
169 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
170 (prot & PROT_WRITE))
171 return -1;
172
173
174 if (!(prot1 & PROT_WRITE))
175 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
176
177
178 pread(fd, g2h(start), end - start, offset);
179
180
181 if (prot_new != (prot1 | PROT_WRITE))
182 mprotect(host_start, qemu_host_page_size, prot_new);
183 } else {
184
185 if (prot_new != prot1) {
186 mprotect(host_start, qemu_host_page_size, prot_new);
187 }
188 }
189 return 0;
190}
191
192static abi_ulong mmap_next_start = 0x40000000;
193
194unsigned long last_brk;
195
196
197
198
199
200
201
202static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
203{
204 abi_ulong addr, addr1, addr_start;
205 int prot;
206 unsigned long new_brk;
207
208 new_brk = (unsigned long)sbrk(0);
209 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
210
211
212
213
214
215 page_set_flags(last_brk & TARGET_PAGE_MASK,
216 TARGET_PAGE_ALIGN(new_brk),
217 PAGE_RESERVED);
218 }
219 last_brk = new_brk;
220
221 size = HOST_PAGE_ALIGN(size);
222 start = start & qemu_host_page_mask;
223 addr = start;
224 if (addr == 0)
225 addr = mmap_next_start;
226 addr_start = addr;
227 for(;;) {
228 prot = 0;
229 for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
230 prot |= page_get_flags(addr1);
231 }
232 if (prot == 0)
233 break;
234 addr += qemu_host_page_size;
235
236 if (addr == addr_start)
237 return (abi_ulong)-1;
238 }
239 if (start == 0)
240 mmap_next_start = addr + size;
241 return addr;
242}
243
244
245abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
246 int flags, int fd, abi_ulong offset)
247{
248 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
249 unsigned long host_start;
250
251 mmap_lock();
252#ifdef DEBUG_MMAP
253 {
254 printf("mmap: start=0x" TARGET_FMT_lx
255 " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
256 start, len,
257 prot & PROT_READ ? 'r' : '-',
258 prot & PROT_WRITE ? 'w' : '-',
259 prot & PROT_EXEC ? 'x' : '-');
260 if (flags & MAP_FIXED)
261 printf("MAP_FIXED ");
262 if (flags & MAP_ANON)
263 printf("MAP_ANON ");
264 switch(flags & TARGET_BSD_MAP_FLAGMASK) {
265 case MAP_PRIVATE:
266 printf("MAP_PRIVATE ");
267 break;
268 case MAP_SHARED:
269 printf("MAP_SHARED ");
270 break;
271 default:
272 printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK);
273 break;
274 }
275 printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
276 }
277#endif
278
279 if (offset & ~TARGET_PAGE_MASK) {
280 errno = EINVAL;
281 goto fail;
282 }
283
284 len = TARGET_PAGE_ALIGN(len);
285 if (len == 0)
286 goto the_end;
287 real_start = start & qemu_host_page_mask;
288
289 if (!(flags & MAP_FIXED)) {
290 abi_ulong mmap_start;
291 void *p;
292 host_offset = offset & qemu_host_page_mask;
293 host_len = len + offset - host_offset;
294 host_len = HOST_PAGE_ALIGN(host_len);
295 mmap_start = mmap_find_vma(real_start, host_len);
296 if (mmap_start == (abi_ulong)-1) {
297 errno = ENOMEM;
298 goto fail;
299 }
300
301
302
303 p = mmap(g2h(mmap_start),
304 host_len, prot, flags | MAP_FIXED, fd, host_offset);
305 if (p == MAP_FAILED)
306 goto fail;
307
308 host_start = (unsigned long)p;
309 if (!(flags & MAP_ANON))
310 host_start += offset - host_offset;
311 start = h2g(host_start);
312 } else {
313 int flg;
314 target_ulong addr;
315
316 if (start & ~TARGET_PAGE_MASK) {
317 errno = EINVAL;
318 goto fail;
319 }
320 end = start + len;
321 real_end = HOST_PAGE_ALIGN(end);
322
323 for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
324 flg = page_get_flags(addr);
325 if (flg & PAGE_RESERVED) {
326 errno = ENXIO;
327 goto fail;
328 }
329 }
330
331
332
333 if (!(flags & MAP_ANON) &&
334 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
335
336
337 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
338 (prot & PROT_WRITE)) {
339 errno = EINVAL;
340 goto fail;
341 }
342 retaddr = target_mmap(start, len, prot | PROT_WRITE,
343 MAP_FIXED | MAP_PRIVATE | MAP_ANON,
344 -1, 0);
345 if (retaddr == -1)
346 goto fail;
347 pread(fd, g2h(start), len, offset);
348 if (!(prot & PROT_WRITE)) {
349 ret = target_mprotect(start, len, prot);
350 if (ret != 0) {
351 start = ret;
352 goto the_end;
353 }
354 }
355 goto the_end;
356 }
357
358
359 if (start > real_start) {
360 if (real_end == real_start + qemu_host_page_size) {
361
362 ret = mmap_frag(real_start, start, end,
363 prot, flags, fd, offset);
364 if (ret == -1)
365 goto fail;
366 goto the_end1;
367 }
368 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
369 prot, flags, fd, offset);
370 if (ret == -1)
371 goto fail;
372 real_start += qemu_host_page_size;
373 }
374
375 if (end < real_end) {
376 ret = mmap_frag(real_end - qemu_host_page_size,
377 real_end - qemu_host_page_size, real_end,
378 prot, flags, fd,
379 offset + real_end - qemu_host_page_size - start);
380 if (ret == -1)
381 goto fail;
382 real_end -= qemu_host_page_size;
383 }
384
385
386 if (real_start < real_end) {
387 void *p;
388 unsigned long offset1;
389 if (flags & MAP_ANON)
390 offset1 = 0;
391 else
392 offset1 = offset + real_start - start;
393 p = mmap(g2h(real_start), real_end - real_start,
394 prot, flags, fd, offset1);
395 if (p == MAP_FAILED)
396 goto fail;
397 }
398 }
399 the_end1:
400 page_set_flags(start, start + len, prot | PAGE_VALID);
401 the_end:
402#ifdef DEBUG_MMAP
403 printf("ret=0x" TARGET_FMT_lx "\n", start);
404 page_dump(stdout);
405 printf("\n");
406#endif
407 mmap_unlock();
408 return start;
409fail:
410 mmap_unlock();
411 return -1;
412}
413
414int target_munmap(abi_ulong start, abi_ulong len)
415{
416 abi_ulong end, real_start, real_end, addr;
417 int prot, ret;
418
419#ifdef DEBUG_MMAP
420 printf("munmap: start=0x%lx len=0x%lx\n", start, len);
421#endif
422 if (start & ~TARGET_PAGE_MASK)
423 return -EINVAL;
424 len = TARGET_PAGE_ALIGN(len);
425 if (len == 0)
426 return -EINVAL;
427 mmap_lock();
428 end = start + len;
429 real_start = start & qemu_host_page_mask;
430 real_end = HOST_PAGE_ALIGN(end);
431
432 if (start > real_start) {
433
434 prot = 0;
435 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
436 prot |= page_get_flags(addr);
437 }
438 if (real_end == real_start + qemu_host_page_size) {
439 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
440 prot |= page_get_flags(addr);
441 }
442 end = real_end;
443 }
444 if (prot != 0)
445 real_start += qemu_host_page_size;
446 }
447 if (end < real_end) {
448 prot = 0;
449 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
450 prot |= page_get_flags(addr);
451 }
452 if (prot != 0)
453 real_end -= qemu_host_page_size;
454 }
455
456 ret = 0;
457
458 if (real_start < real_end) {
459 ret = munmap(g2h(real_start), real_end - real_start);
460 }
461
462 if (ret == 0)
463 page_set_flags(start, start + len, 0);
464 mmap_unlock();
465 return ret;
466}
467
468int target_msync(abi_ulong start, abi_ulong len, int flags)
469{
470 abi_ulong end;
471
472 if (start & ~TARGET_PAGE_MASK)
473 return -EINVAL;
474 len = TARGET_PAGE_ALIGN(len);
475 end = start + len;
476 if (end < start)
477 return -EINVAL;
478 if (end == start)
479 return 0;
480
481 start &= qemu_host_page_mask;
482 return msync(g2h(start), end - start, flags);
483}
484