root / linux-user / mmap.c @ 45bc1f52
History | View | Annotate | Download (16.8 kB)
1 | 54936004 | bellard | /*
|
---|---|---|---|
2 | 54936004 | bellard | * mmap support for qemu
|
3 | 5fafdf24 | ths | *
|
4 | 54936004 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 54936004 | bellard | *
|
6 | 54936004 | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 54936004 | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 54936004 | bellard | * the Free Software Foundation; either version 2 of the License, or
|
9 | 54936004 | bellard | * (at your option) any later version.
|
10 | 54936004 | bellard | *
|
11 | 54936004 | bellard | * This program is distributed in the hope that it will be useful,
|
12 | 54936004 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 54936004 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 54936004 | bellard | * GNU General Public License for more details.
|
15 | 54936004 | bellard | *
|
16 | 54936004 | bellard | * You should have received a copy of the GNU General Public License
|
17 | 54936004 | bellard | * along with this program; if not, write to the Free Software
|
18 | 54936004 | bellard | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 | 54936004 | bellard | */
|
20 | 54936004 | bellard | #include <stdlib.h> |
21 | 54936004 | bellard | #include <stdio.h> |
22 | 54936004 | bellard | #include <stdarg.h> |
23 | 54936004 | bellard | #include <string.h> |
24 | 54936004 | bellard | #include <unistd.h> |
25 | 54936004 | bellard | #include <errno.h> |
26 | 54936004 | bellard | #include <sys/mman.h> |
27 | 54936004 | bellard | |
28 | 54936004 | bellard | #include "qemu.h" |
29 | 78f5bf1e | blueswir1 | #include "qemu-common.h" |
30 | 54936004 | bellard | |
31 | 54936004 | bellard | //#define DEBUG_MMAP
|
32 | 54936004 | bellard | |
33 | c8a706fe | pbrook | #if defined(USE_NPTL)
|
34 | c8a706fe | pbrook | pthread_mutex_t mmap_mutex; |
35 | c8a706fe | pbrook | static int __thread mmap_lock_count; |
36 | c8a706fe | pbrook | |
37 | c8a706fe | pbrook | void mmap_lock(void) |
38 | c8a706fe | pbrook | { |
39 | c8a706fe | pbrook | if (mmap_lock_count++ == 0) { |
40 | c8a706fe | pbrook | pthread_mutex_lock(&mmap_mutex); |
41 | c8a706fe | pbrook | } |
42 | c8a706fe | pbrook | } |
43 | c8a706fe | pbrook | |
44 | c8a706fe | pbrook | void mmap_unlock(void) |
45 | c8a706fe | pbrook | { |
46 | c8a706fe | pbrook | if (--mmap_lock_count == 0) { |
47 | c8a706fe | pbrook | pthread_mutex_unlock(&mmap_mutex); |
48 | c8a706fe | pbrook | } |
49 | c8a706fe | pbrook | } |
50 | d5975363 | pbrook | |
51 | d5975363 | pbrook | /* Grab lock to make sure things are in a consistent state after fork(). */
|
52 | d5975363 | pbrook | void mmap_fork_start(void) |
53 | d5975363 | pbrook | { |
54 | d5975363 | pbrook | if (mmap_lock_count)
|
55 | d5975363 | pbrook | abort(); |
56 | d5975363 | pbrook | pthread_mutex_lock(&mmap_mutex); |
57 | d5975363 | pbrook | } |
58 | d5975363 | pbrook | |
59 | d5975363 | pbrook | void mmap_fork_end(int child) |
60 | d5975363 | pbrook | { |
61 | d5975363 | pbrook | if (child)
|
62 | d5975363 | pbrook | pthread_mutex_init(&mmap_mutex, NULL);
|
63 | d5975363 | pbrook | else
|
64 | d5975363 | pbrook | pthread_mutex_unlock(&mmap_mutex); |
65 | d5975363 | pbrook | } |
66 | c8a706fe | pbrook | #else
|
67 | c8a706fe | pbrook | /* We aren't threadsafe to start with, so no need to worry about locking. */
|
68 | c8a706fe | pbrook | void mmap_lock(void) |
69 | c8a706fe | pbrook | { |
70 | c8a706fe | pbrook | } |
71 | c8a706fe | pbrook | |
72 | c8a706fe | pbrook | void mmap_unlock(void) |
73 | c8a706fe | pbrook | { |
74 | c8a706fe | pbrook | } |
75 | c8a706fe | pbrook | #endif
|
76 | c8a706fe | pbrook | |
77 | 17e2377a | pbrook | void *qemu_vmalloc(size_t size)
|
78 | 17e2377a | pbrook | { |
79 | 17e2377a | pbrook | void *p;
|
80 | 17e2377a | pbrook | unsigned long addr; |
81 | 17e2377a | pbrook | mmap_lock(); |
82 | 17e2377a | pbrook | /* Use map and mark the pages as used. */
|
83 | 17e2377a | pbrook | p = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
84 | 17e2377a | pbrook | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
85 | 17e2377a | pbrook | |
86 | 17e2377a | pbrook | addr = (unsigned long)p; |
87 | 17e2377a | pbrook | if (addr == (target_ulong) addr) {
|
88 | 17e2377a | pbrook | /* Allocated region overlaps guest address space.
|
89 | 17e2377a | pbrook | This may recurse. */
|
90 | 17e2377a | pbrook | page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size), |
91 | 17e2377a | pbrook | PAGE_RESERVED); |
92 | 17e2377a | pbrook | } |
93 | 17e2377a | pbrook | |
94 | 17e2377a | pbrook | mmap_unlock(); |
95 | 17e2377a | pbrook | return p;
|
96 | 17e2377a | pbrook | } |
97 | 17e2377a | pbrook | |
98 | 17e2377a | pbrook | void *qemu_malloc(size_t size)
|
99 | 17e2377a | pbrook | { |
100 | 17e2377a | pbrook | char * p;
|
101 | 17e2377a | pbrook | size += 16;
|
102 | 17e2377a | pbrook | p = qemu_vmalloc(size); |
103 | 17e2377a | pbrook | *(size_t *)p = size; |
104 | 17e2377a | pbrook | return p + 16; |
105 | 17e2377a | pbrook | } |
106 | 17e2377a | pbrook | |
107 | 17e2377a | pbrook | /* We use map, which is always zero initialized. */
|
108 | 17e2377a | pbrook | void * qemu_mallocz(size_t size)
|
109 | 17e2377a | pbrook | { |
110 | 17e2377a | pbrook | return qemu_malloc(size);
|
111 | 17e2377a | pbrook | } |
112 | 17e2377a | pbrook | |
113 | 17e2377a | pbrook | void qemu_free(void *ptr) |
114 | 17e2377a | pbrook | { |
115 | 17e2377a | pbrook | /* FIXME: We should unmark the reserved pages here. However this gets
|
116 | 17e2377a | pbrook | complicated when one target page spans multiple host pages, so we
|
117 | 17e2377a | pbrook | don't bother. */
|
118 | 17e2377a | pbrook | size_t *p; |
119 | 17e2377a | pbrook | p = (size_t *)((char *)ptr - 16); |
120 | 17e2377a | pbrook | munmap(p, *p); |
121 | 17e2377a | pbrook | } |
122 | 17e2377a | pbrook | |
123 | 53a5960a | pbrook | /* NOTE: all the constants are the HOST ones, but addresses are target. */
|
124 | 992f48a0 | blueswir1 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
125 | 54936004 | bellard | { |
126 | 992f48a0 | blueswir1 | abi_ulong end, host_start, host_end, addr; |
127 | 54936004 | bellard | int prot1, ret;
|
128 | 54936004 | bellard | |
129 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
130 | a5b85f79 | ths | printf("mprotect: start=0x" TARGET_FMT_lx
|
131 | 80210bcd | ths | "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, |
132 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
133 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
134 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
135 | 54936004 | bellard | #endif
|
136 | 54936004 | bellard | |
137 | 54936004 | bellard | if ((start & ~TARGET_PAGE_MASK) != 0) |
138 | 54936004 | bellard | return -EINVAL;
|
139 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
140 | 54936004 | bellard | end = start + len; |
141 | 54936004 | bellard | if (end < start)
|
142 | 54936004 | bellard | return -EINVAL;
|
143 | 171cd1cd | balrog | prot &= PROT_READ | PROT_WRITE | PROT_EXEC; |
144 | 54936004 | bellard | if (len == 0) |
145 | 54936004 | bellard | return 0; |
146 | 3b46e624 | ths | |
147 | c8a706fe | pbrook | mmap_lock(); |
148 | 83fb7adf | bellard | host_start = start & qemu_host_page_mask; |
149 | 54936004 | bellard | host_end = HOST_PAGE_ALIGN(end); |
150 | 54936004 | bellard | if (start > host_start) {
|
151 | 54936004 | bellard | /* handle host page containing start */
|
152 | 54936004 | bellard | prot1 = prot; |
153 | 54936004 | bellard | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
154 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
155 | 54936004 | bellard | } |
156 | 83fb7adf | bellard | if (host_end == host_start + qemu_host_page_size) {
|
157 | d418c81e | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
158 | d418c81e | bellard | prot1 |= page_get_flags(addr); |
159 | d418c81e | bellard | } |
160 | d418c81e | bellard | end = host_end; |
161 | d418c81e | bellard | } |
162 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); |
163 | 54936004 | bellard | if (ret != 0) |
164 | c8a706fe | pbrook | goto error;
|
165 | 83fb7adf | bellard | host_start += qemu_host_page_size; |
166 | 54936004 | bellard | } |
167 | 54936004 | bellard | if (end < host_end) {
|
168 | 54936004 | bellard | prot1 = prot; |
169 | 54936004 | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
170 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
171 | 54936004 | bellard | } |
172 | 5fafdf24 | ths | ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, |
173 | 54936004 | bellard | prot1 & PAGE_BITS); |
174 | 54936004 | bellard | if (ret != 0) |
175 | c8a706fe | pbrook | goto error;
|
176 | 83fb7adf | bellard | host_end -= qemu_host_page_size; |
177 | 54936004 | bellard | } |
178 | 3b46e624 | ths | |
179 | 54936004 | bellard | /* handle the pages in the middle */
|
180 | 54936004 | bellard | if (host_start < host_end) {
|
181 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), host_end - host_start, prot); |
182 | 54936004 | bellard | if (ret != 0) |
183 | c8a706fe | pbrook | goto error;
|
184 | 54936004 | bellard | } |
185 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
186 | c8a706fe | pbrook | mmap_unlock(); |
187 | 54936004 | bellard | return 0; |
188 | c8a706fe | pbrook | error:
|
189 | c8a706fe | pbrook | mmap_unlock(); |
190 | c8a706fe | pbrook | return ret;
|
191 | 54936004 | bellard | } |
192 | 54936004 | bellard | |
193 | 54936004 | bellard | /* map an incomplete host page */
|
194 | 992f48a0 | blueswir1 | static int mmap_frag(abi_ulong real_start, |
195 | 992f48a0 | blueswir1 | abi_ulong start, abi_ulong end, |
196 | 992f48a0 | blueswir1 | int prot, int flags, int fd, abi_ulong offset) |
197 | 54936004 | bellard | { |
198 | 80210bcd | ths | abi_ulong real_end, addr; |
199 | 53a5960a | pbrook | void *host_start;
|
200 | 54936004 | bellard | int prot1, prot_new;
|
201 | 54936004 | bellard | |
202 | 53a5960a | pbrook | real_end = real_start + qemu_host_page_size; |
203 | 53a5960a | pbrook | host_start = g2h(real_start); |
204 | 54936004 | bellard | |
205 | 54936004 | bellard | /* get the protection of the target pages outside the mapping */
|
206 | 54936004 | bellard | prot1 = 0;
|
207 | 53a5960a | pbrook | for(addr = real_start; addr < real_end; addr++) {
|
208 | 54936004 | bellard | if (addr < start || addr >= end)
|
209 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
210 | 54936004 | bellard | } |
211 | 3b46e624 | ths | |
212 | 54936004 | bellard | if (prot1 == 0) { |
213 | 54936004 | bellard | /* no page was there, so we allocate one */
|
214 | 80210bcd | ths | void *p = mmap(host_start, qemu_host_page_size, prot,
|
215 | 80210bcd | ths | flags | MAP_ANONYMOUS, -1, 0); |
216 | 80210bcd | ths | if (p == MAP_FAILED)
|
217 | 80210bcd | ths | return -1; |
218 | 53a5960a | pbrook | prot1 = prot; |
219 | 54936004 | bellard | } |
220 | 54936004 | bellard | prot1 &= PAGE_BITS; |
221 | 54936004 | bellard | |
222 | 54936004 | bellard | prot_new = prot | prot1; |
223 | 54936004 | bellard | if (!(flags & MAP_ANONYMOUS)) {
|
224 | 54936004 | bellard | /* msync() won't work here, so we return an error if write is
|
225 | 54936004 | bellard | possible while it is a shared mapping */
|
226 | 54936004 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
227 | 54936004 | bellard | (prot & PROT_WRITE)) |
228 | 54936004 | bellard | return -EINVAL;
|
229 | 54936004 | bellard | |
230 | 54936004 | bellard | /* adjust protection to be able to read */
|
231 | 54936004 | bellard | if (!(prot1 & PROT_WRITE))
|
232 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
233 | 3b46e624 | ths | |
234 | 54936004 | bellard | /* read the corresponding file data */
|
235 | 53a5960a | pbrook | pread(fd, g2h(start), end - start, offset); |
236 | 3b46e624 | ths | |
237 | 54936004 | bellard | /* put final protection */
|
238 | 54936004 | bellard | if (prot_new != (prot1 | PROT_WRITE))
|
239 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
240 | 54936004 | bellard | } else {
|
241 | 54936004 | bellard | /* just update the protection */
|
242 | 54936004 | bellard | if (prot_new != prot1) {
|
243 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
244 | 54936004 | bellard | } |
245 | 54936004 | bellard | } |
246 | 54936004 | bellard | return 0; |
247 | 54936004 | bellard | } |
248 | 54936004 | bellard | |
249 | a03e2d42 | bellard | #if defined(__CYGWIN__)
|
250 | a03e2d42 | bellard | /* Cygwin doesn't have a whole lot of address space. */
|
251 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x18000000; |
252 | a03e2d42 | bellard | #else
|
253 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x40000000; |
254 | a03e2d42 | bellard | #endif
|
255 | a03e2d42 | bellard | |
256 | 0776590d | pbrook | unsigned long last_brk; |
257 | 0776590d | pbrook | |
258 | a03e2d42 | bellard | /* find a free memory area of size 'size'. The search starts at
|
259 | a03e2d42 | bellard | 'start'. If 'start' == 0, then a default start address is used.
|
260 | a03e2d42 | bellard | Return -1 if error.
|
261 | a03e2d42 | bellard | */
|
262 | 50a9569b | balrog | /* page_init() marks pages used by the host as reserved to be sure not
|
263 | a03e2d42 | bellard | to use them. */
|
264 | a03e2d42 | bellard | static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
|
265 | a03e2d42 | bellard | { |
266 | a03e2d42 | bellard | abi_ulong addr, addr1, addr_start; |
267 | a03e2d42 | bellard | int prot;
|
268 | 0776590d | pbrook | unsigned long new_brk; |
269 | 0776590d | pbrook | |
270 | 0776590d | pbrook | new_brk = (unsigned long)sbrk(0); |
271 | 0776590d | pbrook | if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
|
272 | 0776590d | pbrook | /* This is a hack to catch the host allocating memory with brk().
|
273 | 0776590d | pbrook | If it uses mmap then we loose.
|
274 | 0776590d | pbrook | FIXME: We really want to avoid the host allocating memory in
|
275 | 0776590d | pbrook | the first place, and maybe leave some slack to avoid switching
|
276 | 0776590d | pbrook | to mmap. */
|
277 | 0776590d | pbrook | page_set_flags(last_brk & TARGET_PAGE_MASK, |
278 | 0776590d | pbrook | TARGET_PAGE_ALIGN(new_brk), |
279 | 0776590d | pbrook | PAGE_RESERVED); |
280 | 0776590d | pbrook | } |
281 | 0776590d | pbrook | last_brk = new_brk; |
282 | a03e2d42 | bellard | |
283 | a03e2d42 | bellard | size = HOST_PAGE_ALIGN(size); |
284 | a03e2d42 | bellard | start = start & qemu_host_page_mask; |
285 | a03e2d42 | bellard | addr = start; |
286 | a03e2d42 | bellard | if (addr == 0) |
287 | a03e2d42 | bellard | addr = mmap_next_start; |
288 | a03e2d42 | bellard | addr_start = addr; |
289 | a03e2d42 | bellard | for(;;) {
|
290 | a03e2d42 | bellard | prot = 0;
|
291 | a03e2d42 | bellard | for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
|
292 | a03e2d42 | bellard | prot |= page_get_flags(addr1); |
293 | a03e2d42 | bellard | } |
294 | a03e2d42 | bellard | if (prot == 0) |
295 | a03e2d42 | bellard | break;
|
296 | a03e2d42 | bellard | addr += qemu_host_page_size; |
297 | a03e2d42 | bellard | /* we found nothing */
|
298 | a03e2d42 | bellard | if (addr == addr_start)
|
299 | a03e2d42 | bellard | return (abi_ulong)-1; |
300 | a03e2d42 | bellard | } |
301 | a03e2d42 | bellard | if (start == 0) |
302 | a03e2d42 | bellard | mmap_next_start = addr + size; |
303 | a03e2d42 | bellard | return addr;
|
304 | a03e2d42 | bellard | } |
305 | a03e2d42 | bellard | |
306 | 54936004 | bellard | /* NOTE: all the constants are the HOST ones */
|
307 | 992f48a0 | blueswir1 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
|
308 | 992f48a0 | blueswir1 | int flags, int fd, abi_ulong offset) |
309 | 54936004 | bellard | { |
310 | 992f48a0 | blueswir1 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
311 | a5b85f79 | ths | unsigned long host_start; |
312 | 54936004 | bellard | |
313 | c8a706fe | pbrook | mmap_lock(); |
314 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
315 | 54936004 | bellard | { |
316 | a5b85f79 | ths | printf("mmap: start=0x" TARGET_FMT_lx
|
317 | 80210bcd | ths | " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", |
318 | 5fafdf24 | ths | start, len, |
319 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
320 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
321 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
322 | 54936004 | bellard | if (flags & MAP_FIXED)
|
323 | 54936004 | bellard | printf("MAP_FIXED ");
|
324 | 54936004 | bellard | if (flags & MAP_ANONYMOUS)
|
325 | 54936004 | bellard | printf("MAP_ANON ");
|
326 | 54936004 | bellard | switch(flags & MAP_TYPE) {
|
327 | 54936004 | bellard | case MAP_PRIVATE:
|
328 | 54936004 | bellard | printf("MAP_PRIVATE ");
|
329 | 54936004 | bellard | break;
|
330 | 54936004 | bellard | case MAP_SHARED:
|
331 | 54936004 | bellard | printf("MAP_SHARED ");
|
332 | 54936004 | bellard | break;
|
333 | 54936004 | bellard | default:
|
334 | 54936004 | bellard | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
|
335 | 54936004 | bellard | break;
|
336 | 54936004 | bellard | } |
337 | a5b85f79 | ths | printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); |
338 | 54936004 | bellard | } |
339 | 54936004 | bellard | #endif
|
340 | 54936004 | bellard | |
341 | e89f07d3 | pbrook | if (offset & ~TARGET_PAGE_MASK) {
|
342 | e89f07d3 | pbrook | errno = EINVAL; |
343 | c8a706fe | pbrook | goto fail;
|
344 | e89f07d3 | pbrook | } |
345 | 54936004 | bellard | |
346 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
347 | 54936004 | bellard | if (len == 0) |
348 | c8a706fe | pbrook | goto the_end;
|
349 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
350 | 54936004 | bellard | |
351 | 54936004 | bellard | if (!(flags & MAP_FIXED)) {
|
352 | a03e2d42 | bellard | abi_ulong mmap_start; |
353 | a03e2d42 | bellard | void *p;
|
354 | a03e2d42 | bellard | host_offset = offset & qemu_host_page_mask; |
355 | a03e2d42 | bellard | host_len = len + offset - host_offset; |
356 | a03e2d42 | bellard | host_len = HOST_PAGE_ALIGN(host_len); |
357 | a03e2d42 | bellard | mmap_start = mmap_find_vma(real_start, host_len); |
358 | a03e2d42 | bellard | if (mmap_start == (abi_ulong)-1) { |
359 | a03e2d42 | bellard | errno = ENOMEM; |
360 | c8a706fe | pbrook | goto fail;
|
361 | 54936004 | bellard | } |
362 | a03e2d42 | bellard | /* Note: we prefer to control the mapping address. It is
|
363 | a03e2d42 | bellard | especially important if qemu_host_page_size >
|
364 | a03e2d42 | bellard | qemu_real_host_page_size */
|
365 | a03e2d42 | bellard | p = mmap(g2h(mmap_start), |
366 | a03e2d42 | bellard | host_len, prot, flags | MAP_FIXED, fd, host_offset); |
367 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
368 | c8a706fe | pbrook | goto fail;
|
369 | a03e2d42 | bellard | /* update start so that it points to the file position at 'offset' */
|
370 | a03e2d42 | bellard | host_start = (unsigned long)p; |
371 | a03e2d42 | bellard | if (!(flags & MAP_ANONYMOUS))
|
372 | a03e2d42 | bellard | host_start += offset - host_offset; |
373 | a03e2d42 | bellard | start = h2g(host_start); |
374 | a03e2d42 | bellard | } else {
|
375 | 7ab240ad | balrog | int flg;
|
376 | 7ab240ad | balrog | target_ulong addr; |
377 | 7ab240ad | balrog | |
378 | a03e2d42 | bellard | if (start & ~TARGET_PAGE_MASK) {
|
379 | e89f07d3 | pbrook | errno = EINVAL; |
380 | c8a706fe | pbrook | goto fail;
|
381 | e89f07d3 | pbrook | } |
382 | a03e2d42 | bellard | end = start + len; |
383 | a03e2d42 | bellard | real_end = HOST_PAGE_ALIGN(end); |
384 | 7ab240ad | balrog | |
385 | 45bc1f52 | aurel32 | /*
|
386 | 45bc1f52 | aurel32 | * Test if requested memory area fits target address space
|
387 | 45bc1f52 | aurel32 | * It can fail only on 64-bit host with 32-bit target.
|
388 | 45bc1f52 | aurel32 | * On any other target/host host mmap() handles this error correctly.
|
389 | 45bc1f52 | aurel32 | */
|
390 | 45bc1f52 | aurel32 | if ((unsigned long)start + len - 1 > (abi_ulong) -1) { |
391 | 45bc1f52 | aurel32 | errno = EINVAL; |
392 | 45bc1f52 | aurel32 | goto fail;
|
393 | 45bc1f52 | aurel32 | } |
394 | 45bc1f52 | aurel32 | |
395 | 7ab240ad | balrog | for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
396 | 7ab240ad | balrog | flg = page_get_flags(addr); |
397 | 7ab240ad | balrog | if (flg & PAGE_RESERVED) {
|
398 | 7ab240ad | balrog | errno = ENXIO; |
399 | c8a706fe | pbrook | goto fail;
|
400 | 7ab240ad | balrog | } |
401 | 7ab240ad | balrog | } |
402 | 7ab240ad | balrog | |
403 | a03e2d42 | bellard | /* worst case: we cannot map the file because the offset is not
|
404 | a03e2d42 | bellard | aligned, so we read it */
|
405 | a03e2d42 | bellard | if (!(flags & MAP_ANONYMOUS) &&
|
406 | a03e2d42 | bellard | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
407 | a03e2d42 | bellard | /* msync() won't work here, so we return an error if write is
|
408 | a03e2d42 | bellard | possible while it is a shared mapping */
|
409 | a03e2d42 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
410 | a03e2d42 | bellard | (prot & PROT_WRITE)) { |
411 | a03e2d42 | bellard | errno = EINVAL; |
412 | c8a706fe | pbrook | goto fail;
|
413 | a03e2d42 | bellard | } |
414 | a03e2d42 | bellard | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
415 | a03e2d42 | bellard | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
416 | a03e2d42 | bellard | -1, 0); |
417 | a03e2d42 | bellard | if (retaddr == -1) |
418 | c8a706fe | pbrook | goto fail;
|
419 | a03e2d42 | bellard | pread(fd, g2h(start), len, offset); |
420 | a03e2d42 | bellard | if (!(prot & PROT_WRITE)) {
|
421 | a03e2d42 | bellard | ret = target_mprotect(start, len, prot); |
422 | c8a706fe | pbrook | if (ret != 0) { |
423 | c8a706fe | pbrook | start = ret; |
424 | c8a706fe | pbrook | goto the_end;
|
425 | c8a706fe | pbrook | } |
426 | a03e2d42 | bellard | } |
427 | a03e2d42 | bellard | goto the_end;
|
428 | 54936004 | bellard | } |
429 | a03e2d42 | bellard | |
430 | a03e2d42 | bellard | /* handle the start of the mapping */
|
431 | a03e2d42 | bellard | if (start > real_start) {
|
432 | a03e2d42 | bellard | if (real_end == real_start + qemu_host_page_size) {
|
433 | a03e2d42 | bellard | /* one single host page */
|
434 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, end, |
435 | a03e2d42 | bellard | prot, flags, fd, offset); |
436 | a03e2d42 | bellard | if (ret == -1) |
437 | c8a706fe | pbrook | goto fail;
|
438 | a03e2d42 | bellard | goto the_end1;
|
439 | a03e2d42 | bellard | } |
440 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
441 | 54936004 | bellard | prot, flags, fd, offset); |
442 | 54936004 | bellard | if (ret == -1) |
443 | c8a706fe | pbrook | goto fail;
|
444 | a03e2d42 | bellard | real_start += qemu_host_page_size; |
445 | a03e2d42 | bellard | } |
446 | a03e2d42 | bellard | /* handle the end of the mapping */
|
447 | a03e2d42 | bellard | if (end < real_end) {
|
448 | a03e2d42 | bellard | ret = mmap_frag(real_end - qemu_host_page_size, |
449 | a03e2d42 | bellard | real_end - qemu_host_page_size, real_end, |
450 | a03e2d42 | bellard | prot, flags, fd, |
451 | a03e2d42 | bellard | offset + real_end - qemu_host_page_size - start); |
452 | a03e2d42 | bellard | if (ret == -1) |
453 | c8a706fe | pbrook | goto fail;
|
454 | a03e2d42 | bellard | real_end -= qemu_host_page_size; |
455 | 54936004 | bellard | } |
456 | 3b46e624 | ths | |
457 | a03e2d42 | bellard | /* map the middle (easier) */
|
458 | a03e2d42 | bellard | if (real_start < real_end) {
|
459 | a03e2d42 | bellard | void *p;
|
460 | a03e2d42 | bellard | unsigned long offset1; |
461 | a03e2d42 | bellard | if (flags & MAP_ANONYMOUS)
|
462 | a03e2d42 | bellard | offset1 = 0;
|
463 | a03e2d42 | bellard | else
|
464 | a03e2d42 | bellard | offset1 = offset + real_start - start; |
465 | a03e2d42 | bellard | p = mmap(g2h(real_start), real_end - real_start, |
466 | a03e2d42 | bellard | prot, flags, fd, offset1); |
467 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
468 | c8a706fe | pbrook | goto fail;
|
469 | a03e2d42 | bellard | } |
470 | 54936004 | bellard | } |
471 | 54936004 | bellard | the_end1:
|
472 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
473 | 54936004 | bellard | the_end:
|
474 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
475 | 2e0ded9c | edgar_igl | printf("ret=0x" TARGET_FMT_lx "\n", start); |
476 | 54936004 | bellard | page_dump(stdout); |
477 | 54936004 | bellard | printf("\n");
|
478 | 54936004 | bellard | #endif
|
479 | c8a706fe | pbrook | mmap_unlock(); |
480 | 54936004 | bellard | return start;
|
481 | c8a706fe | pbrook | fail:
|
482 | c8a706fe | pbrook | mmap_unlock(); |
483 | c8a706fe | pbrook | return -1; |
484 | 54936004 | bellard | } |
485 | 54936004 | bellard | |
486 | 992f48a0 | blueswir1 | int target_munmap(abi_ulong start, abi_ulong len)
|
487 | 54936004 | bellard | { |
488 | 992f48a0 | blueswir1 | abi_ulong end, real_start, real_end, addr; |
489 | 54936004 | bellard | int prot, ret;
|
490 | 54936004 | bellard | |
491 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
492 | 54936004 | bellard | printf("munmap: start=0x%lx len=0x%lx\n", start, len);
|
493 | 54936004 | bellard | #endif
|
494 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
495 | 54936004 | bellard | return -EINVAL;
|
496 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
497 | 54936004 | bellard | if (len == 0) |
498 | 54936004 | bellard | return -EINVAL;
|
499 | c8a706fe | pbrook | mmap_lock(); |
500 | 54936004 | bellard | end = start + len; |
501 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
502 | 53a5960a | pbrook | real_end = HOST_PAGE_ALIGN(end); |
503 | 54936004 | bellard | |
504 | 53a5960a | pbrook | if (start > real_start) {
|
505 | 54936004 | bellard | /* handle host page containing start */
|
506 | 54936004 | bellard | prot = 0;
|
507 | 53a5960a | pbrook | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
508 | 54936004 | bellard | prot |= page_get_flags(addr); |
509 | 54936004 | bellard | } |
510 | 53a5960a | pbrook | if (real_end == real_start + qemu_host_page_size) {
|
511 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
512 | d418c81e | bellard | prot |= page_get_flags(addr); |
513 | d418c81e | bellard | } |
514 | 53a5960a | pbrook | end = real_end; |
515 | d418c81e | bellard | } |
516 | 54936004 | bellard | if (prot != 0) |
517 | 53a5960a | pbrook | real_start += qemu_host_page_size; |
518 | 54936004 | bellard | } |
519 | 53a5960a | pbrook | if (end < real_end) {
|
520 | 54936004 | bellard | prot = 0;
|
521 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
522 | 54936004 | bellard | prot |= page_get_flags(addr); |
523 | 54936004 | bellard | } |
524 | 54936004 | bellard | if (prot != 0) |
525 | 53a5960a | pbrook | real_end -= qemu_host_page_size; |
526 | 54936004 | bellard | } |
527 | 3b46e624 | ths | |
528 | c8a706fe | pbrook | ret = 0;
|
529 | 54936004 | bellard | /* unmap what we can */
|
530 | 53a5960a | pbrook | if (real_start < real_end) {
|
531 | 4118a970 | j_mayer | ret = munmap(g2h(real_start), real_end - real_start); |
532 | 54936004 | bellard | } |
533 | 54936004 | bellard | |
534 | c8a706fe | pbrook | if (ret == 0) |
535 | c8a706fe | pbrook | page_set_flags(start, start + len, 0);
|
536 | c8a706fe | pbrook | mmap_unlock(); |
537 | c8a706fe | pbrook | return ret;
|
538 | 54936004 | bellard | } |
539 | 54936004 | bellard | |
540 | 54936004 | bellard | /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
|
541 | 54936004 | bellard | blocks which have been allocated starting on a host page */
|
542 | 992f48a0 | blueswir1 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
543 | 992f48a0 | blueswir1 | abi_ulong new_size, unsigned long flags, |
544 | 992f48a0 | blueswir1 | abi_ulong new_addr) |
545 | 54936004 | bellard | { |
546 | 54936004 | bellard | int prot;
|
547 | a5b85f79 | ths | unsigned long host_addr; |
548 | 54936004 | bellard | |
549 | c8a706fe | pbrook | mmap_lock(); |
550 | 54936004 | bellard | /* XXX: use 5 args syscall */
|
551 | a5b85f79 | ths | host_addr = (long)mremap(g2h(old_addr), old_size, new_size, flags);
|
552 | c8a706fe | pbrook | if (host_addr == -1) { |
553 | c8a706fe | pbrook | new_addr = -1;
|
554 | c8a706fe | pbrook | } else {
|
555 | c8a706fe | pbrook | new_addr = h2g(host_addr); |
556 | c8a706fe | pbrook | prot = page_get_flags(old_addr); |
557 | c8a706fe | pbrook | page_set_flags(old_addr, old_addr + old_size, 0);
|
558 | c8a706fe | pbrook | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); |
559 | c8a706fe | pbrook | } |
560 | c8a706fe | pbrook | mmap_unlock(); |
561 | 54936004 | bellard | return new_addr;
|
562 | 54936004 | bellard | } |
563 | 54936004 | bellard | |
564 | 992f48a0 | blueswir1 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
565 | 54936004 | bellard | { |
566 | 992f48a0 | blueswir1 | abi_ulong end; |
567 | 54936004 | bellard | |
568 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
569 | 54936004 | bellard | return -EINVAL;
|
570 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
571 | 54936004 | bellard | end = start + len; |
572 | d418c81e | bellard | if (end < start)
|
573 | d418c81e | bellard | return -EINVAL;
|
574 | d418c81e | bellard | if (end == start)
|
575 | d418c81e | bellard | return 0; |
576 | 3b46e624 | ths | |
577 | 83fb7adf | bellard | start &= qemu_host_page_mask; |
578 | 53a5960a | pbrook | return msync(g2h(start), end - start, flags);
|
579 | 54936004 | bellard | } |