Statistics
| Branch: | Revision:

root / linux-user / mmap.c @ 530e7615

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