Statistics
| Branch: | Revision:

root / linux-user / mmap.c @ 0776590d

History | View | Annotate | Download (14.3 kB)

1
/*
2
 *  mmap support for qemu
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <stdarg.h>
23
#include <string.h>
24
#include <unistd.h>
25
#include <errno.h>
26
#include <sys/mman.h>
27

    
28
#include "qemu.h"
29

    
30
//#define DEBUG_MMAP
31

    
32
/* NOTE: all the constants are the HOST ones, but addresses are target. */
33
int target_mprotect(abi_ulong start, abi_ulong len, int prot)
34
{
35
    abi_ulong end, host_start, host_end, addr;
36
    int prot1, ret;
37

    
38
#ifdef DEBUG_MMAP
39
    printf("mprotect: start=0x" TARGET_FMT_lx
40
           "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
41
           prot & PROT_READ ? 'r' : '-',
42
           prot & PROT_WRITE ? 'w' : '-',
43
           prot & PROT_EXEC ? 'x' : '-');
44
#endif
45

    
46
    if ((start & ~TARGET_PAGE_MASK) != 0)
47
        return -EINVAL;
48
    len = TARGET_PAGE_ALIGN(len);
49
    end = start + len;
50
    if (end < start)
51
        return -EINVAL;
52
    prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
53
    if (len == 0)
54
        return 0;
55

    
56
    host_start = start & qemu_host_page_mask;
57
    host_end = HOST_PAGE_ALIGN(end);
58
    if (start > host_start) {
59
        /* handle host page containing start */
60
        prot1 = prot;
61
        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
62
            prot1 |= page_get_flags(addr);
63
        }
64
        if (host_end == host_start + qemu_host_page_size) {
65
            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
66
                prot1 |= page_get_flags(addr);
67
            }
68
            end = host_end;
69
        }
70
        ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
71
        if (ret != 0)
72
            return ret;
73
        host_start += qemu_host_page_size;
74
    }
75
    if (end < host_end) {
76
        prot1 = prot;
77
        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
78
            prot1 |= page_get_flags(addr);
79
        }
80
        ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
81
                       prot1 & PAGE_BITS);
82
        if (ret != 0)
83
            return ret;
84
        host_end -= qemu_host_page_size;
85
    }
86

    
87
    /* handle the pages in the middle */
88
    if (host_start < host_end) {
89
        ret = mprotect(g2h(host_start), host_end - host_start, prot);
90
        if (ret != 0)
91
            return ret;
92
    }
93
    page_set_flags(start, start + len, prot | PAGE_VALID);
94
    return 0;
95
}
96

    
97
/* map an incomplete host page */
98
static int mmap_frag(abi_ulong real_start,
99
                     abi_ulong start, abi_ulong end,
100
                     int prot, int flags, int fd, abi_ulong offset)
101
{
102
    abi_ulong real_end, addr;
103
    void *host_start;
104
    int prot1, prot_new;
105

    
106
    real_end = real_start + qemu_host_page_size;
107
    host_start = g2h(real_start);
108

    
109
    /* get the protection of the target pages outside the mapping */
110
    prot1 = 0;
111
    for(addr = real_start; addr < real_end; addr++) {
112
        if (addr < start || addr >= end)
113
            prot1 |= page_get_flags(addr);
114
    }
115

    
116
    if (prot1 == 0) {
117
        /* no page was there, so we allocate one */
118
        void *p = mmap(host_start, qemu_host_page_size, prot,
119
                       flags | MAP_ANONYMOUS, -1, 0);
120
        if (p == MAP_FAILED)
121
            return -1;
122
        prot1 = prot;
123
    }
124
    prot1 &= PAGE_BITS;
125

    
126
    prot_new = prot | prot1;
127
    if (!(flags & MAP_ANONYMOUS)) {
128
        /* msync() won't work here, so we return an error if write is
129
           possible while it is a shared mapping */
130
        if ((flags & MAP_TYPE) == MAP_SHARED &&
131
            (prot & PROT_WRITE))
132
            return -EINVAL;
133

    
134
        /* adjust protection to be able to read */
135
        if (!(prot1 & PROT_WRITE))
136
            mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
137

    
138
        /* read the corresponding file data */
139
        pread(fd, g2h(start), end - start, offset);
140

    
141
        /* put final protection */
142
        if (prot_new != (prot1 | PROT_WRITE))
143
            mprotect(host_start, qemu_host_page_size, prot_new);
144
    } else {
145
        /* just update the protection */
146
        if (prot_new != prot1) {
147
            mprotect(host_start, qemu_host_page_size, prot_new);
148
        }
149
    }
150
    return 0;
151
}
152

    
153
#if defined(__CYGWIN__)
154
/* Cygwin doesn't have a whole lot of address space.  */
155
static abi_ulong mmap_next_start = 0x18000000;
156
#else
157
static abi_ulong mmap_next_start = 0x40000000;
158
#endif
159

    
160
unsigned long last_brk;
161

    
162
/* find a free memory area of size 'size'. The search starts at
163
   'start'. If 'start' == 0, then a default start address is used.
164
   Return -1 if error.
165
*/
166
/* page_init() marks pages used by the host as reserved to be sure not
167
   to use them. */
168
static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
169
{
170
    abi_ulong addr, addr1, addr_start;
171
    int prot;
172
    unsigned long new_brk;
173

    
174
    new_brk = (unsigned long)sbrk(0);
175
    if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
176
        /* This is a hack to catch the host allocating memory with brk().
177
           If it uses mmap then we loose.
178
           FIXME: We really want to avoid the host allocating memory in
179
           the first place, and maybe leave some slack to avoid switching
180
           to mmap.  */
181
        page_set_flags(last_brk & TARGET_PAGE_MASK,
182
                       TARGET_PAGE_ALIGN(new_brk),
183
                       PAGE_RESERVED); 
184
    }
185
    last_brk = new_brk;
186

    
187
    size = HOST_PAGE_ALIGN(size);
188
    start = start & qemu_host_page_mask;
189
    addr = start;
190
    if (addr == 0)
191
        addr = mmap_next_start;
192
    addr_start = addr;
193
    for(;;) {
194
        prot = 0;
195
        for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
196
            prot |= page_get_flags(addr1);
197
        }
198
        if (prot == 0)
199
            break;
200
        addr += qemu_host_page_size;
201
        /* we found nothing */
202
        if (addr == addr_start)
203
            return (abi_ulong)-1;
204
    }
205
    if (start == 0)
206
        mmap_next_start = addr + size;
207
    return addr;
208
}
209

    
210
/* NOTE: all the constants are the HOST ones */
211
abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
212
                     int flags, int fd, abi_ulong offset)
213
{
214
    abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
215
    unsigned long host_start;
216

    
217
#ifdef DEBUG_MMAP
218
    {
219
        printf("mmap: start=0x" TARGET_FMT_lx
220
               " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
221
               start, len,
222
               prot & PROT_READ ? 'r' : '-',
223
               prot & PROT_WRITE ? 'w' : '-',
224
               prot & PROT_EXEC ? 'x' : '-');
225
        if (flags & MAP_FIXED)
226
            printf("MAP_FIXED ");
227
        if (flags & MAP_ANONYMOUS)
228
            printf("MAP_ANON ");
229
        switch(flags & MAP_TYPE) {
230
        case MAP_PRIVATE:
231
            printf("MAP_PRIVATE ");
232
            break;
233
        case MAP_SHARED:
234
            printf("MAP_SHARED ");
235
            break;
236
        default:
237
            printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
238
            break;
239
        }
240
        printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
241
    }
242
#endif
243

    
244
    if (offset & ~TARGET_PAGE_MASK) {
245
        errno = EINVAL;
246
        return -1;
247
    }
248

    
249
    len = TARGET_PAGE_ALIGN(len);
250
    if (len == 0)
251
        return start;
252
    real_start = start & qemu_host_page_mask;
253

    
254
    if (!(flags & MAP_FIXED)) {
255
        abi_ulong mmap_start;
256
        void *p;
257
        host_offset = offset & qemu_host_page_mask;
258
        host_len = len + offset - host_offset;
259
        host_len = HOST_PAGE_ALIGN(host_len);
260
        mmap_start = mmap_find_vma(real_start, host_len);
261
        if (mmap_start == (abi_ulong)-1) {
262
            errno = ENOMEM;
263
            return -1;
264
        }
265
        /* Note: we prefer to control the mapping address. It is
266
           especially important if qemu_host_page_size >
267
           qemu_real_host_page_size */
268
        p = mmap(g2h(mmap_start),
269
                 host_len, prot, flags | MAP_FIXED, fd, host_offset);
270
        if (p == MAP_FAILED)
271
            return -1;
272
        /* update start so that it points to the file position at 'offset' */
273
        host_start = (unsigned long)p;
274
        if (!(flags & MAP_ANONYMOUS))
275
            host_start += offset - host_offset;
276
        start = h2g(host_start);
277
    } else {
278
        int flg;
279
        target_ulong addr;
280

    
281
        if (start & ~TARGET_PAGE_MASK) {
282
            errno = EINVAL;
283
            return -1;
284
        }
285
        end = start + len;
286
        real_end = HOST_PAGE_ALIGN(end);
287

    
288
        for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
289
            flg = page_get_flags(addr);
290
            if (flg & PAGE_RESERVED) {
291
                errno = ENXIO;
292
                return -1;
293
            }
294
        }
295

    
296
        /* worst case: we cannot map the file because the offset is not
297
           aligned, so we read it */
298
        if (!(flags & MAP_ANONYMOUS) &&
299
            (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
300
            /* msync() won't work here, so we return an error if write is
301
               possible while it is a shared mapping */
302
            if ((flags & MAP_TYPE) == MAP_SHARED &&
303
                (prot & PROT_WRITE)) {
304
                errno = EINVAL;
305
                return -1;
306
            }
307
            retaddr = target_mmap(start, len, prot | PROT_WRITE,
308
                                  MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
309
                                  -1, 0);
310
            if (retaddr == -1)
311
                return -1;
312
            pread(fd, g2h(start), len, offset);
313
            if (!(prot & PROT_WRITE)) {
314
                ret = target_mprotect(start, len, prot);
315
                if (ret != 0)
316
                    return ret;
317
            }
318
            goto the_end;
319
        }
320
        
321
        /* handle the start of the mapping */
322
        if (start > real_start) {
323
            if (real_end == real_start + qemu_host_page_size) {
324
                /* one single host page */
325
                ret = mmap_frag(real_start, start, end,
326
                                prot, flags, fd, offset);
327
                if (ret == -1)
328
                    return ret;
329
                goto the_end1;
330
            }
331
            ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
332
                            prot, flags, fd, offset);
333
            if (ret == -1)
334
                return ret;
335
            real_start += qemu_host_page_size;
336
        }
337
        /* handle the end of the mapping */
338
        if (end < real_end) {
339
            ret = mmap_frag(real_end - qemu_host_page_size,
340
                            real_end - qemu_host_page_size, real_end,
341
                            prot, flags, fd,
342
                            offset + real_end - qemu_host_page_size - start);
343
            if (ret == -1)
344
                return -1;
345
            real_end -= qemu_host_page_size;
346
        }
347

    
348
        /* map the middle (easier) */
349
        if (real_start < real_end) {
350
            void *p;
351
            unsigned long offset1;
352
            if (flags & MAP_ANONYMOUS)
353
                offset1 = 0;
354
            else
355
                offset1 = offset + real_start - start;
356
            p = mmap(g2h(real_start), real_end - real_start,
357
                     prot, flags, fd, offset1);
358
            if (p == MAP_FAILED)
359
                return -1;
360
        }
361
    }
362
 the_end1:
363
    page_set_flags(start, start + len, prot | PAGE_VALID);
364
 the_end:
365
#ifdef DEBUG_MMAP
366
    printf("ret=0x" TARGET_FMT_lx "\n", start);
367
    page_dump(stdout);
368
    printf("\n");
369
#endif
370
    return start;
371
}
372

    
373
int target_munmap(abi_ulong start, abi_ulong len)
374
{
375
    abi_ulong end, real_start, real_end, addr;
376
    int prot, ret;
377

    
378
#ifdef DEBUG_MMAP
379
    printf("munmap: start=0x%lx len=0x%lx\n", start, len);
380
#endif
381
    if (start & ~TARGET_PAGE_MASK)
382
        return -EINVAL;
383
    len = TARGET_PAGE_ALIGN(len);
384
    if (len == 0)
385
        return -EINVAL;
386
    end = start + len;
387
    real_start = start & qemu_host_page_mask;
388
    real_end = HOST_PAGE_ALIGN(end);
389

    
390
    if (start > real_start) {
391
        /* handle host page containing start */
392
        prot = 0;
393
        for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
394
            prot |= page_get_flags(addr);
395
        }
396
        if (real_end == real_start + qemu_host_page_size) {
397
            for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
398
                prot |= page_get_flags(addr);
399
            }
400
            end = real_end;
401
        }
402
        if (prot != 0)
403
            real_start += qemu_host_page_size;
404
    }
405
    if (end < real_end) {
406
        prot = 0;
407
        for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
408
            prot |= page_get_flags(addr);
409
        }
410
        if (prot != 0)
411
            real_end -= qemu_host_page_size;
412
    }
413

    
414
    /* unmap what we can */
415
    if (real_start < real_end) {
416
        ret = munmap(g2h(real_start), real_end - real_start);
417
        if (ret != 0)
418
            return ret;
419
    }
420

    
421
    page_set_flags(start, start + len, 0);
422
    return 0;
423
}
424

    
425
/* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
426
   blocks which have been allocated starting on a host page */
427
abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
428
                       abi_ulong new_size, unsigned long flags,
429
                       abi_ulong new_addr)
430
{
431
    int prot;
432
    unsigned long host_addr;
433

    
434
    /* XXX: use 5 args syscall */
435
    host_addr = (long)mremap(g2h(old_addr), old_size, new_size, flags);
436
    if (host_addr == -1)
437
        return -1;
438
    new_addr = h2g(host_addr);
439
    prot = page_get_flags(old_addr);
440
    page_set_flags(old_addr, old_addr + old_size, 0);
441
    page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
442
    return new_addr;
443
}
444

    
445
int target_msync(abi_ulong start, abi_ulong len, int flags)
446
{
447
    abi_ulong end;
448

    
449
    if (start & ~TARGET_PAGE_MASK)
450
        return -EINVAL;
451
    len = TARGET_PAGE_ALIGN(len);
452
    end = start + len;
453
    if (end < start)
454
        return -EINVAL;
455
    if (end == start)
456
        return 0;
457

    
458
    start &= qemu_host_page_mask;
459
    return msync(g2h(start), end - start, flags);
460
}