~vcs-imports/qemu/git

« back to all changes in this revision

Viewing changes to linux-user/mmap.c

  • Committer: blueswir1
  • Date: 2007-09-25 17:30:09 UTC
  • Revision ID: git-v1:eb296a0a03eebd2d73718d31cd0d8a8db0b804de
 Remove the target dependency introduced by previous patch


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3239 c046a42c-6fe2-441c-8c8c-71466251a162

Show diffs side-by-side

added added

removed removed

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