Statistics
| Branch: | Revision:

root / bsd-user / mmap.c @ baa8c602

History | View | Annotate | Download (16.1 kB)

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