Statistics
| Branch: | Revision:

root / linux-user / mmap.c @ e69b4065

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