Statistics
| Branch: | Revision:

root / linux-user / mmap.c @ eda52953

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