]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <stdarg.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <sys/mman.h> | |
27 | ||
28 | #include "qemu.h" | |
29 | ||
30 | //#define DEBUG_MMAP | |
31 | ||
53a5960a | 32 | /* NOTE: all the constants are the HOST ones, but addresses are target. */ |
992f48a0 | 33 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
54936004 | 34 | { |
992f48a0 | 35 | abi_ulong end, host_start, host_end, addr; |
54936004 FB |
36 | int prot1, ret; |
37 | ||
38 | #ifdef DEBUG_MMAP | |
a5b85f79 | 39 | printf("mprotect: start=0x" TARGET_FMT_lx |
80210bcd | 40 | "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, |
54936004 FB |
41 | prot & PROT_READ ? 'r' : '-', |
42 | prot & PROT_WRITE ? 'w' : '-', | |
43 | prot & PROT_EXEC ? 'x' : '-'); | |
44 | #endif | |
45 | ||
46 | if ((start & ~TARGET_PAGE_MASK) != 0) | |
47 | return -EINVAL; | |
48 | len = TARGET_PAGE_ALIGN(len); | |
49 | end = start + len; | |
50 | if (end < start) | |
51 | return -EINVAL; | |
171cd1cd | 52 | prot &= PROT_READ | PROT_WRITE | PROT_EXEC; |
54936004 FB |
53 | if (len == 0) |
54 | return 0; | |
3b46e624 | 55 | |
83fb7adf | 56 | host_start = start & qemu_host_page_mask; |
54936004 FB |
57 | host_end = HOST_PAGE_ALIGN(end); |
58 | if (start > host_start) { | |
59 | /* handle host page containing start */ | |
60 | prot1 = prot; | |
61 | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { | |
62 | prot1 |= page_get_flags(addr); | |
63 | } | |
83fb7adf | 64 | if (host_end == host_start + qemu_host_page_size) { |
d418c81e FB |
65 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { |
66 | prot1 |= page_get_flags(addr); | |
67 | } | |
68 | end = host_end; | |
69 | } | |
53a5960a | 70 | ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); |
54936004 FB |
71 | if (ret != 0) |
72 | return ret; | |
83fb7adf | 73 | host_start += qemu_host_page_size; |
54936004 FB |
74 | } |
75 | if (end < host_end) { | |
54936004 FB |
76 | prot1 = prot; |
77 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { | |
78 | prot1 |= page_get_flags(addr); | |
79 | } | |
5fafdf24 | 80 | ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, |
54936004 FB |
81 | prot1 & PAGE_BITS); |
82 | if (ret != 0) | |
83 | return ret; | |
83fb7adf | 84 | host_end -= qemu_host_page_size; |
54936004 | 85 | } |
3b46e624 | 86 | |
54936004 FB |
87 | /* handle the pages in the middle */ |
88 | if (host_start < host_end) { | |
53a5960a | 89 | ret = mprotect(g2h(host_start), host_end - host_start, prot); |
54936004 FB |
90 | if (ret != 0) |
91 | return ret; | |
92 | } | |
54936004 FB |
93 | page_set_flags(start, start + len, prot | PAGE_VALID); |
94 | return 0; | |
95 | } | |
96 | ||
97 | /* map an incomplete host page */ | |
992f48a0 BS |
98 | static int mmap_frag(abi_ulong real_start, |
99 | abi_ulong start, abi_ulong end, | |
100 | int prot, int flags, int fd, abi_ulong offset) | |
54936004 | 101 | { |
80210bcd | 102 | abi_ulong real_end, addr; |
53a5960a | 103 | void *host_start; |
54936004 FB |
104 | int prot1, prot_new; |
105 | ||
53a5960a PB |
106 | real_end = real_start + qemu_host_page_size; |
107 | host_start = g2h(real_start); | |
54936004 FB |
108 | |
109 | /* get the protection of the target pages outside the mapping */ | |
110 | prot1 = 0; | |
53a5960a | 111 | for(addr = real_start; addr < real_end; addr++) { |
54936004 FB |
112 | if (addr < start || addr >= end) |
113 | prot1 |= page_get_flags(addr); | |
114 | } | |
3b46e624 | 115 | |
54936004 FB |
116 | if (prot1 == 0) { |
117 | /* no page was there, so we allocate one */ | |
80210bcd TS |
118 | void *p = mmap(host_start, qemu_host_page_size, prot, |
119 | flags | MAP_ANONYMOUS, -1, 0); | |
120 | if (p == MAP_FAILED) | |
121 | return -1; | |
53a5960a | 122 | prot1 = prot; |
54936004 FB |
123 | } |
124 | prot1 &= PAGE_BITS; | |
125 | ||
126 | prot_new = prot | prot1; | |
127 | if (!(flags & MAP_ANONYMOUS)) { | |
128 | /* msync() won't work here, so we return an error if write is | |
129 | possible while it is a shared mapping */ | |
130 | if ((flags & MAP_TYPE) == MAP_SHARED && | |
131 | (prot & PROT_WRITE)) | |
132 | return -EINVAL; | |
133 | ||
134 | /* adjust protection to be able to read */ | |
135 | if (!(prot1 & PROT_WRITE)) | |
53a5960a | 136 | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
3b46e624 | 137 | |
54936004 | 138 | /* read the corresponding file data */ |
53a5960a | 139 | pread(fd, g2h(start), end - start, offset); |
3b46e624 | 140 | |
54936004 FB |
141 | /* put final protection */ |
142 | if (prot_new != (prot1 | PROT_WRITE)) | |
53a5960a | 143 | mprotect(host_start, qemu_host_page_size, prot_new); |
54936004 FB |
144 | } else { |
145 | /* just update the protection */ | |
146 | if (prot_new != prot1) { | |
53a5960a | 147 | mprotect(host_start, qemu_host_page_size, prot_new); |
54936004 FB |
148 | } |
149 | } | |
150 | return 0; | |
151 | } | |
152 | ||
a03e2d42 FB |
153 | #if defined(__CYGWIN__) |
154 | /* Cygwin doesn't have a whole lot of address space. */ | |
155 | static abi_ulong mmap_next_start = 0x18000000; | |
156 | #else | |
157 | static abi_ulong mmap_next_start = 0x40000000; | |
158 | #endif | |
159 | ||
160 | /* find a free memory area of size 'size'. The search starts at | |
161 | 'start'. If 'start' == 0, then a default start address is used. | |
162 | Return -1 if error. | |
163 | */ | |
50a9569b | 164 | /* page_init() marks pages used by the host as reserved to be sure not |
a03e2d42 FB |
165 | to use them. */ |
166 | static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) | |
167 | { | |
168 | abi_ulong addr, addr1, addr_start; | |
169 | int prot; | |
170 | ||
171 | size = HOST_PAGE_ALIGN(size); | |
172 | start = start & qemu_host_page_mask; | |
173 | addr = start; | |
174 | if (addr == 0) | |
175 | addr = mmap_next_start; | |
176 | addr_start = addr; | |
177 | for(;;) { | |
178 | prot = 0; | |
179 | for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) { | |
180 | prot |= page_get_flags(addr1); | |
181 | } | |
182 | if (prot == 0) | |
183 | break; | |
184 | addr += qemu_host_page_size; | |
185 | /* we found nothing */ | |
186 | if (addr == addr_start) | |
187 | return (abi_ulong)-1; | |
188 | } | |
189 | if (start == 0) | |
190 | mmap_next_start = addr + size; | |
191 | return addr; | |
192 | } | |
193 | ||
54936004 | 194 | /* NOTE: all the constants are the HOST ones */ |
992f48a0 BS |
195 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, |
196 | int flags, int fd, abi_ulong offset) | |
54936004 | 197 | { |
992f48a0 | 198 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
a5b85f79 | 199 | unsigned long host_start; |
54936004 FB |
200 | |
201 | #ifdef DEBUG_MMAP | |
202 | { | |
a5b85f79 | 203 | printf("mmap: start=0x" TARGET_FMT_lx |
80210bcd | 204 | " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", |
5fafdf24 | 205 | start, len, |
54936004 FB |
206 | prot & PROT_READ ? 'r' : '-', |
207 | prot & PROT_WRITE ? 'w' : '-', | |
208 | prot & PROT_EXEC ? 'x' : '-'); | |
209 | if (flags & MAP_FIXED) | |
210 | printf("MAP_FIXED "); | |
211 | if (flags & MAP_ANONYMOUS) | |
212 | printf("MAP_ANON "); | |
213 | switch(flags & MAP_TYPE) { | |
214 | case MAP_PRIVATE: | |
215 | printf("MAP_PRIVATE "); | |
216 | break; | |
217 | case MAP_SHARED: | |
218 | printf("MAP_SHARED "); | |
219 | break; | |
220 | default: | |
221 | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE); | |
222 | break; | |
223 | } | |
a5b85f79 | 224 | printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); |
54936004 FB |
225 | } |
226 | #endif | |
227 | ||
e89f07d3 PB |
228 | if (offset & ~TARGET_PAGE_MASK) { |
229 | errno = EINVAL; | |
230 | return -1; | |
231 | } | |
54936004 FB |
232 | |
233 | len = TARGET_PAGE_ALIGN(len); | |
234 | if (len == 0) | |
235 | return start; | |
53a5960a | 236 | real_start = start & qemu_host_page_mask; |
54936004 FB |
237 | |
238 | if (!(flags & MAP_FIXED)) { | |
a03e2d42 FB |
239 | abi_ulong mmap_start; |
240 | void *p; | |
241 | host_offset = offset & qemu_host_page_mask; | |
242 | host_len = len + offset - host_offset; | |
243 | host_len = HOST_PAGE_ALIGN(host_len); | |
244 | mmap_start = mmap_find_vma(real_start, host_len); | |
245 | if (mmap_start == (abi_ulong)-1) { | |
246 | errno = ENOMEM; | |
247 | return -1; | |
54936004 | 248 | } |
a03e2d42 FB |
249 | /* Note: we prefer to control the mapping address. It is |
250 | especially important if qemu_host_page_size > | |
251 | qemu_real_host_page_size */ | |
252 | p = mmap(g2h(mmap_start), | |
253 | host_len, prot, flags | MAP_FIXED, fd, host_offset); | |
254 | if (p == MAP_FAILED) | |
255 | return -1; | |
256 | /* update start so that it points to the file position at 'offset' */ | |
257 | host_start = (unsigned long)p; | |
258 | if (!(flags & MAP_ANONYMOUS)) | |
259 | host_start += offset - host_offset; | |
260 | start = h2g(host_start); | |
261 | } else { | |
7ab240ad AZ |
262 | int flg; |
263 | target_ulong addr; | |
264 | ||
a03e2d42 | 265 | if (start & ~TARGET_PAGE_MASK) { |
e89f07d3 PB |
266 | errno = EINVAL; |
267 | return -1; | |
268 | } | |
a03e2d42 FB |
269 | end = start + len; |
270 | real_end = HOST_PAGE_ALIGN(end); | |
7ab240ad AZ |
271 | |
272 | for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) { | |
273 | flg = page_get_flags(addr); | |
274 | if (flg & PAGE_RESERVED) { | |
275 | errno = ENXIO; | |
276 | return -1; | |
277 | } | |
278 | } | |
279 | ||
a03e2d42 FB |
280 | /* worst case: we cannot map the file because the offset is not |
281 | aligned, so we read it */ | |
282 | if (!(flags & MAP_ANONYMOUS) && | |
283 | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { | |
284 | /* msync() won't work here, so we return an error if write is | |
285 | possible while it is a shared mapping */ | |
286 | if ((flags & MAP_TYPE) == MAP_SHARED && | |
287 | (prot & PROT_WRITE)) { | |
288 | errno = EINVAL; | |
289 | return -1; | |
290 | } | |
291 | retaddr = target_mmap(start, len, prot | PROT_WRITE, | |
292 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, | |
293 | -1, 0); | |
294 | if (retaddr == -1) | |
295 | return -1; | |
296 | pread(fd, g2h(start), len, offset); | |
297 | if (!(prot & PROT_WRITE)) { | |
298 | ret = target_mprotect(start, len, prot); | |
299 | if (ret != 0) | |
300 | return ret; | |
301 | } | |
302 | goto the_end; | |
54936004 | 303 | } |
a03e2d42 FB |
304 | |
305 | /* handle the start of the mapping */ | |
306 | if (start > real_start) { | |
307 | if (real_end == real_start + qemu_host_page_size) { | |
308 | /* one single host page */ | |
309 | ret = mmap_frag(real_start, start, end, | |
310 | prot, flags, fd, offset); | |
311 | if (ret == -1) | |
312 | return ret; | |
313 | goto the_end1; | |
314 | } | |
315 | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, | |
54936004 FB |
316 | prot, flags, fd, offset); |
317 | if (ret == -1) | |
318 | return ret; | |
a03e2d42 FB |
319 | real_start += qemu_host_page_size; |
320 | } | |
321 | /* handle the end of the mapping */ | |
322 | if (end < real_end) { | |
323 | ret = mmap_frag(real_end - qemu_host_page_size, | |
324 | real_end - qemu_host_page_size, real_end, | |
325 | prot, flags, fd, | |
326 | offset + real_end - qemu_host_page_size - start); | |
327 | if (ret == -1) | |
328 | return -1; | |
329 | real_end -= qemu_host_page_size; | |
54936004 | 330 | } |
3b46e624 | 331 | |
a03e2d42 FB |
332 | /* map the middle (easier) */ |
333 | if (real_start < real_end) { | |
334 | void *p; | |
335 | unsigned long offset1; | |
336 | if (flags & MAP_ANONYMOUS) | |
337 | offset1 = 0; | |
338 | else | |
339 | offset1 = offset + real_start - start; | |
340 | p = mmap(g2h(real_start), real_end - real_start, | |
341 | prot, flags, fd, offset1); | |
342 | if (p == MAP_FAILED) | |
343 | return -1; | |
344 | } | |
54936004 FB |
345 | } |
346 | the_end1: | |
347 | page_set_flags(start, start + len, prot | PAGE_VALID); | |
348 | the_end: | |
349 | #ifdef DEBUG_MMAP | |
2e0ded9c | 350 | printf("ret=0x" TARGET_FMT_lx "\n", start); |
54936004 FB |
351 | page_dump(stdout); |
352 | printf("\n"); | |
353 | #endif | |
354 | return start; | |
355 | } | |
356 | ||
992f48a0 | 357 | int target_munmap(abi_ulong start, abi_ulong len) |
54936004 | 358 | { |
992f48a0 | 359 | abi_ulong end, real_start, real_end, addr; |
54936004 FB |
360 | int prot, ret; |
361 | ||
362 | #ifdef DEBUG_MMAP | |
363 | printf("munmap: start=0x%lx len=0x%lx\n", start, len); | |
364 | #endif | |
365 | if (start & ~TARGET_PAGE_MASK) | |
366 | return -EINVAL; | |
367 | len = TARGET_PAGE_ALIGN(len); | |
368 | if (len == 0) | |
369 | return -EINVAL; | |
370 | end = start + len; | |
53a5960a PB |
371 | real_start = start & qemu_host_page_mask; |
372 | real_end = HOST_PAGE_ALIGN(end); | |
54936004 | 373 | |
53a5960a | 374 | if (start > real_start) { |
54936004 FB |
375 | /* handle host page containing start */ |
376 | prot = 0; | |
53a5960a | 377 | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { |
54936004 FB |
378 | prot |= page_get_flags(addr); |
379 | } | |
53a5960a PB |
380 | if (real_end == real_start + qemu_host_page_size) { |
381 | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { | |
d418c81e FB |
382 | prot |= page_get_flags(addr); |
383 | } | |
53a5960a | 384 | end = real_end; |
d418c81e | 385 | } |
54936004 | 386 | if (prot != 0) |
53a5960a | 387 | real_start += qemu_host_page_size; |
54936004 | 388 | } |
53a5960a | 389 | if (end < real_end) { |
54936004 | 390 | prot = 0; |
53a5960a | 391 | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { |
54936004 FB |
392 | prot |= page_get_flags(addr); |
393 | } | |
394 | if (prot != 0) | |
53a5960a | 395 | real_end -= qemu_host_page_size; |
54936004 | 396 | } |
3b46e624 | 397 | |
54936004 | 398 | /* unmap what we can */ |
53a5960a | 399 | if (real_start < real_end) { |
4118a970 | 400 | ret = munmap(g2h(real_start), real_end - real_start); |
54936004 FB |
401 | if (ret != 0) |
402 | return ret; | |
403 | } | |
404 | ||
405 | page_set_flags(start, start + len, 0); | |
406 | return 0; | |
407 | } | |
408 | ||
409 | /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED | |
410 | blocks which have been allocated starting on a host page */ | |
992f48a0 BS |
411 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
412 | abi_ulong new_size, unsigned long flags, | |
413 | abi_ulong new_addr) | |
54936004 FB |
414 | { |
415 | int prot; | |
a5b85f79 | 416 | unsigned long host_addr; |
54936004 FB |
417 | |
418 | /* XXX: use 5 args syscall */ | |
a5b85f79 TS |
419 | host_addr = (long)mremap(g2h(old_addr), old_size, new_size, flags); |
420 | if (host_addr == -1) | |
421 | return -1; | |
422 | new_addr = h2g(host_addr); | |
54936004 FB |
423 | prot = page_get_flags(old_addr); |
424 | page_set_flags(old_addr, old_addr + old_size, 0); | |
425 | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); | |
426 | return new_addr; | |
427 | } | |
428 | ||
992f48a0 | 429 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
54936004 | 430 | { |
992f48a0 | 431 | abi_ulong end; |
54936004 FB |
432 | |
433 | if (start & ~TARGET_PAGE_MASK) | |
434 | return -EINVAL; | |
435 | len = TARGET_PAGE_ALIGN(len); | |
54936004 | 436 | end = start + len; |
d418c81e FB |
437 | if (end < start) |
438 | return -EINVAL; | |
439 | if (end == start) | |
440 | return 0; | |
3b46e624 | 441 | |
83fb7adf | 442 | start &= qemu_host_page_mask; |
53a5960a | 443 | return msync(g2h(start), end - start, flags); |
54936004 | 444 | } |