Statistics
| Branch: | Revision:

root / darwin-user / mmap.c @ e1833e1f

History | View | Annotate | Download (12.9 kB)

1 831b7825 ths
/*
2 831b7825 ths
 *  mmap support for qemu
3 831b7825 ths
 *
4 831b7825 ths
 *  Copyright (c) 2003 Fabrice Bellard
5 831b7825 ths
 *
6 831b7825 ths
 *  This program is free software; you can redistribute it and/or modify
7 831b7825 ths
 *  it under the terms of the GNU General Public License as published by
8 831b7825 ths
 *  the Free Software Foundation; either version 2 of the License, or
9 831b7825 ths
 *  (at your option) any later version.
10 831b7825 ths
 *
11 831b7825 ths
 *  This program is distributed in the hope that it will be useful,
12 831b7825 ths
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 831b7825 ths
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 831b7825 ths
 *  GNU General Public License for more details.
15 831b7825 ths
 *
16 831b7825 ths
 *  You should have received a copy of the GNU General Public License
17 831b7825 ths
 *  along with this program; if not, write to the Free Software
18 831b7825 ths
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 831b7825 ths
 */
20 831b7825 ths
#include <stdlib.h>
21 831b7825 ths
#include <stdio.h>
22 831b7825 ths
#include <stdarg.h>
23 831b7825 ths
#include <string.h>
24 831b7825 ths
#include <unistd.h>
25 831b7825 ths
#include <errno.h>
26 831b7825 ths
#include <sys/mman.h>
27 831b7825 ths
28 831b7825 ths
#include "qemu.h"
29 831b7825 ths
30 831b7825 ths
//#define DEBUG_MMAP
31 831b7825 ths
32 831b7825 ths
/* NOTE: all the constants are the HOST ones */
33 831b7825 ths
int target_mprotect(unsigned long start, unsigned long len, int prot)
34 831b7825 ths
{
35 831b7825 ths
    unsigned long end, host_start, host_end, addr;
36 831b7825 ths
    int prot1, ret;
37 831b7825 ths
38 831b7825 ths
#ifdef DEBUG_MMAP
39 831b7825 ths
    printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start, len,
40 831b7825 ths
           prot & PROT_READ ? 'r' : '-',
41 831b7825 ths
           prot & PROT_WRITE ? 'w' : '-',
42 831b7825 ths
           prot & PROT_EXEC ? 'x' : '-');
43 831b7825 ths
#endif
44 831b7825 ths
45 831b7825 ths
    if ((start & ~TARGET_PAGE_MASK) != 0)
46 831b7825 ths
        return -EINVAL;
47 831b7825 ths
    len = TARGET_PAGE_ALIGN(len);
48 831b7825 ths
    end = start + len;
49 831b7825 ths
    if (end < start)
50 831b7825 ths
        return -EINVAL;
51 831b7825 ths
    if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
52 831b7825 ths
        return -EINVAL;
53 831b7825 ths
    if (len == 0)
54 831b7825 ths
        return 0;
55 831b7825 ths
56 831b7825 ths
    host_start = start & qemu_host_page_mask;
57 831b7825 ths
    host_end = HOST_PAGE_ALIGN(end);
58 831b7825 ths
    if (start > host_start) {
59 831b7825 ths
        /* handle host page containing start */
60 831b7825 ths
        prot1 = prot;
61 831b7825 ths
        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
62 831b7825 ths
            prot1 |= page_get_flags(addr);
63 831b7825 ths
        }
64 831b7825 ths
        if (host_end == host_start + qemu_host_page_size) {
65 831b7825 ths
            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
66 831b7825 ths
                prot1 |= page_get_flags(addr);
67 831b7825 ths
            }
68 831b7825 ths
            end = host_end;
69 831b7825 ths
        }
70 831b7825 ths
        ret = mprotect((void *)host_start, qemu_host_page_size, prot1 & PAGE_BITS);
71 831b7825 ths
        if (ret != 0)
72 831b7825 ths
            return ret;
73 831b7825 ths
        host_start += qemu_host_page_size;
74 831b7825 ths
    }
75 831b7825 ths
    if (end < host_end) {
76 831b7825 ths
        prot1 = prot;
77 831b7825 ths
        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
78 831b7825 ths
            prot1 |= page_get_flags(addr);
79 831b7825 ths
        }
80 831b7825 ths
        ret = mprotect((void *)(host_end - qemu_host_page_size), qemu_host_page_size,
81 831b7825 ths
                       prot1 & PAGE_BITS);
82 831b7825 ths
        if (ret != 0)
83 831b7825 ths
            return ret;
84 831b7825 ths
        host_end -= qemu_host_page_size;
85 831b7825 ths
    }
86 831b7825 ths
87 831b7825 ths
    /* handle the pages in the middle */
88 831b7825 ths
    if (host_start < host_end) {
89 831b7825 ths
        ret = mprotect((void *)host_start, host_end - host_start, prot);
90 831b7825 ths
        if (ret != 0)
91 831b7825 ths
            return ret;
92 831b7825 ths
    }
93 831b7825 ths
    page_set_flags(start, start + len, prot | PAGE_VALID);
94 831b7825 ths
    return 0;
95 831b7825 ths
}
96 831b7825 ths
97 831b7825 ths
/* map an incomplete host page */
98 831b7825 ths
int mmap_frag(unsigned long host_start,
99 831b7825 ths
               unsigned long start, unsigned long end,
100 831b7825 ths
               int prot, int flags, int fd, unsigned long offset)
101 831b7825 ths
{
102 831b7825 ths
    unsigned long host_end, ret, addr;
103 831b7825 ths
    int prot1, prot_new;
104 831b7825 ths
105 831b7825 ths
    host_end = host_start + qemu_host_page_size;
106 831b7825 ths
107 831b7825 ths
    /* get the protection of the target pages outside the mapping */
108 831b7825 ths
    prot1 = 0;
109 831b7825 ths
    for(addr = host_start; addr < host_end; addr++) {
110 831b7825 ths
        if (addr < start || addr >= end)
111 831b7825 ths
            prot1 |= page_get_flags(addr);
112 831b7825 ths
    }
113 831b7825 ths
114 831b7825 ths
    if (prot1 == 0) {
115 831b7825 ths
        /* no page was there, so we allocate one */
116 831b7825 ths
        ret = (long)mmap((void *)host_start, qemu_host_page_size, prot,
117 831b7825 ths
                         flags | MAP_ANONYMOUS, -1, 0);
118 831b7825 ths
        if (ret == -1)
119 831b7825 ths
            return ret;
120 831b7825 ths
    }
121 831b7825 ths
    prot1 &= PAGE_BITS;
122 831b7825 ths
123 831b7825 ths
    prot_new = prot | prot1;
124 831b7825 ths
    if (!(flags & MAP_ANONYMOUS)) {
125 831b7825 ths
        /* msync() won't work here, so we return an error if write is
126 831b7825 ths
           possible while it is a shared mapping */
127 831b7825 ths
#ifndef __APPLE__
128 831b7825 ths
        if ((flags & MAP_TYPE) == MAP_SHARED &&
129 831b7825 ths
#else
130 831b7825 ths
        if ((flags &  MAP_SHARED) &&
131 831b7825 ths
#endif
132 831b7825 ths
            (prot & PROT_WRITE))
133 831b7825 ths
            return -EINVAL;
134 831b7825 ths
135 831b7825 ths
        /* adjust protection to be able to read */
136 831b7825 ths
        if (!(prot1 & PROT_WRITE))
137 831b7825 ths
            mprotect((void *)host_start, qemu_host_page_size, prot1 | PROT_WRITE);
138 831b7825 ths
139 831b7825 ths
        /* read the corresponding file data */
140 831b7825 ths
        pread(fd, (void *)start, end - start, offset);
141 831b7825 ths
142 831b7825 ths
        /* put final protection */
143 831b7825 ths
        if (prot_new != (prot1 | PROT_WRITE))
144 831b7825 ths
            mprotect((void *)host_start, qemu_host_page_size, prot_new);
145 831b7825 ths
    } else {
146 831b7825 ths
        /* just update the protection */
147 831b7825 ths
        if (prot_new != prot1) {
148 831b7825 ths
            mprotect((void *)host_start, qemu_host_page_size, prot_new);
149 831b7825 ths
        }
150 831b7825 ths
    }
151 831b7825 ths
    return 0;
152 831b7825 ths
}
153 831b7825 ths
154 831b7825 ths
/* NOTE: all the constants are the HOST ones */
155 831b7825 ths
long target_mmap(unsigned long start, unsigned long len, int prot,
156 831b7825 ths
                 int flags, int fd, unsigned long offset)
157 831b7825 ths
{
158 831b7825 ths
    unsigned long ret, end, host_start, host_end, retaddr, host_offset, host_len;
159 831b7825 ths
#if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
160 831b7825 ths
    static unsigned long last_start = 0x40000000;
161 831b7825 ths
#endif
162 831b7825 ths
163 831b7825 ths
#ifdef DEBUG_MMAP
164 831b7825 ths
    {
165 831b7825 ths
        printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
166 831b7825 ths
               start, len,
167 831b7825 ths
               prot & PROT_READ ? 'r' : '-',
168 831b7825 ths
               prot & PROT_WRITE ? 'w' : '-',
169 831b7825 ths
               prot & PROT_EXEC ? 'x' : '-');
170 831b7825 ths
        if (flags & MAP_FIXED)
171 831b7825 ths
            printf("MAP_FIXED ");
172 831b7825 ths
        if (flags & MAP_ANONYMOUS)
173 831b7825 ths
            printf("MAP_ANON ");
174 831b7825 ths
#ifndef MAP_TYPE
175 831b7825 ths
# define MAP_TYPE 0x3
176 831b7825 ths
#endif
177 831b7825 ths
        switch(flags & MAP_TYPE) {
178 831b7825 ths
        case MAP_PRIVATE:
179 831b7825 ths
            printf("MAP_PRIVATE ");
180 831b7825 ths
            break;
181 831b7825 ths
        case MAP_SHARED:
182 831b7825 ths
            printf("MAP_SHARED ");
183 831b7825 ths
            break;
184 831b7825 ths
        default:
185 831b7825 ths
            printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
186 831b7825 ths
            break;
187 831b7825 ths
        }
188 831b7825 ths
        printf("fd=%d offset=%lx\n", fd, offset);
189 831b7825 ths
    }
190 831b7825 ths
#endif
191 831b7825 ths
192 831b7825 ths
    if (offset & ~TARGET_PAGE_MASK)
193 831b7825 ths
        return -EINVAL;
194 831b7825 ths
195 831b7825 ths
    len = TARGET_PAGE_ALIGN(len);
196 831b7825 ths
    if (len == 0)
197 831b7825 ths
        return start;
198 831b7825 ths
    host_start = start & qemu_host_page_mask;
199 831b7825 ths
200 831b7825 ths
    if (!(flags & MAP_FIXED)) {
201 831b7825 ths
#if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
202 e91c8a77 ths
        /* tell the kernel to search at the same place as i386 */
203 831b7825 ths
        if (host_start == 0) {
204 831b7825 ths
            host_start = last_start;
205 831b7825 ths
            last_start += HOST_PAGE_ALIGN(len);
206 831b7825 ths
        }
207 831b7825 ths
#endif
208 831b7825 ths
        if (qemu_host_page_size != qemu_real_host_page_size) {
209 831b7825 ths
            /* NOTE: this code is only for debugging with '-p' option */
210 831b7825 ths
            /* reserve a memory area */
211 831b7825 ths
            host_len = HOST_PAGE_ALIGN(len) + qemu_host_page_size - TARGET_PAGE_SIZE;
212 831b7825 ths
            host_start = (long)mmap((void *)host_start, host_len, PROT_NONE,
213 831b7825 ths
                                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
214 831b7825 ths
            if (host_start == -1)
215 831b7825 ths
                return host_start;
216 831b7825 ths
            host_end = host_start + host_len;
217 831b7825 ths
            start = HOST_PAGE_ALIGN(host_start);
218 831b7825 ths
            end = start + HOST_PAGE_ALIGN(len);
219 831b7825 ths
            if (start > host_start)
220 831b7825 ths
                munmap((void *)host_start, start - host_start);
221 831b7825 ths
            if (end < host_end)
222 831b7825 ths
                munmap((void *)end, host_end - end);
223 831b7825 ths
            /* use it as a fixed mapping */
224 831b7825 ths
            flags |= MAP_FIXED;
225 831b7825 ths
        } else {
226 831b7825 ths
            /* if not fixed, no need to do anything */
227 831b7825 ths
            host_offset = offset & qemu_host_page_mask;
228 831b7825 ths
            host_len = len + offset - host_offset;
229 831b7825 ths
            start = (long)mmap((void *)host_start, host_len,
230 831b7825 ths
                               prot, flags, fd, host_offset);
231 831b7825 ths
            if (start == -1)
232 831b7825 ths
                return start;
233 831b7825 ths
            /* update start so that it points to the file position at 'offset' */
234 831b7825 ths
            if (!(flags & MAP_ANONYMOUS))
235 831b7825 ths
                start += offset - host_offset;
236 831b7825 ths
            goto the_end1;
237 831b7825 ths
        }
238 831b7825 ths
    }
239 831b7825 ths
240 831b7825 ths
    if (start & ~TARGET_PAGE_MASK)
241 831b7825 ths
        return -EINVAL;
242 831b7825 ths
    end = start + len;
243 831b7825 ths
    host_end = HOST_PAGE_ALIGN(end);
244 831b7825 ths
245 831b7825 ths
    /* worst case: we cannot map the file because the offset is not
246 831b7825 ths
       aligned, so we read it */
247 831b7825 ths
    if (!(flags & MAP_ANONYMOUS) &&
248 831b7825 ths
        (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
249 831b7825 ths
        /* msync() won't work here, so we return an error if write is
250 831b7825 ths
           possible while it is a shared mapping */
251 831b7825 ths
#ifndef __APPLE__
252 831b7825 ths
        if ((flags & MAP_TYPE) == MAP_SHARED &&
253 831b7825 ths
#else
254 831b7825 ths
        if ((flags & MAP_SHARED) &&
255 831b7825 ths
#endif
256 831b7825 ths
            (prot & PROT_WRITE))
257 831b7825 ths
            return -EINVAL;
258 831b7825 ths
        retaddr = target_mmap(start, len, prot | PROT_WRITE,
259 831b7825 ths
                              MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
260 831b7825 ths
                              -1, 0);
261 831b7825 ths
        if (retaddr == -1)
262 831b7825 ths
            return retaddr;
263 831b7825 ths
        pread(fd, (void *)start, len, offset);
264 831b7825 ths
        if (!(prot & PROT_WRITE)) {
265 831b7825 ths
            ret = target_mprotect(start, len, prot);
266 831b7825 ths
            if (ret != 0)
267 831b7825 ths
                return ret;
268 831b7825 ths
        }
269 831b7825 ths
        goto the_end;
270 831b7825 ths
    }
271 831b7825 ths
272 831b7825 ths
    /* handle the start of the mapping */
273 831b7825 ths
    if (start > host_start) {
274 831b7825 ths
        if (host_end == host_start + qemu_host_page_size) {
275 831b7825 ths
            /* one single host page */
276 831b7825 ths
            ret = mmap_frag(host_start, start, end,
277 831b7825 ths
                            prot, flags, fd, offset);
278 831b7825 ths
            if (ret == -1)
279 831b7825 ths
                return ret;
280 831b7825 ths
            goto the_end1;
281 831b7825 ths
        }
282 831b7825 ths
        ret = mmap_frag(host_start, start, host_start + qemu_host_page_size,
283 831b7825 ths
                        prot, flags, fd, offset);
284 831b7825 ths
        if (ret == -1)
285 831b7825 ths
            return ret;
286 831b7825 ths
        host_start += qemu_host_page_size;
287 831b7825 ths
    }
288 831b7825 ths
    /* handle the end of the mapping */
289 831b7825 ths
    if (end < host_end) {
290 831b7825 ths
        ret = mmap_frag(host_end - qemu_host_page_size,
291 831b7825 ths
                        host_end - qemu_host_page_size, host_end,
292 831b7825 ths
                        prot, flags, fd,
293 831b7825 ths
                        offset + host_end - qemu_host_page_size - start);
294 831b7825 ths
        if (ret == -1)
295 831b7825 ths
            return ret;
296 831b7825 ths
        host_end -= qemu_host_page_size;
297 831b7825 ths
    }
298 831b7825 ths
299 831b7825 ths
    /* map the middle (easier) */
300 831b7825 ths
    if (host_start < host_end) {
301 831b7825 ths
        unsigned long offset1;
302 831b7825 ths
        if (flags & MAP_ANONYMOUS)
303 831b7825 ths
          offset1 = 0;
304 831b7825 ths
        else
305 831b7825 ths
          offset1 = offset + host_start - start;
306 831b7825 ths
        ret = (long)mmap((void *)host_start, host_end - host_start,
307 831b7825 ths
                         prot, flags, fd, offset1);
308 831b7825 ths
        if (ret == -1)
309 831b7825 ths
            return ret;
310 831b7825 ths
    }
311 831b7825 ths
 the_end1:
312 831b7825 ths
    page_set_flags(start, start + len, prot | PAGE_VALID);
313 831b7825 ths
 the_end:
314 831b7825 ths
#ifdef DEBUG_MMAP
315 831b7825 ths
    printf("target_mmap: ret=0x%lx\n", (long)start);
316 831b7825 ths
    page_dump(stdout);
317 831b7825 ths
    printf("\n");
318 831b7825 ths
#endif
319 831b7825 ths
    return start;
320 831b7825 ths
}
321 831b7825 ths
322 831b7825 ths
int target_munmap(unsigned long start, unsigned long len)
323 831b7825 ths
{
324 831b7825 ths
    unsigned long end, host_start, host_end, addr;
325 831b7825 ths
    int prot, ret;
326 831b7825 ths
327 831b7825 ths
#ifdef DEBUG_MMAP
328 831b7825 ths
    printf("munmap: start=0x%lx len=0x%lx\n", start, len);
329 831b7825 ths
#endif
330 831b7825 ths
    if (start & ~TARGET_PAGE_MASK)
331 831b7825 ths
        return -EINVAL;
332 831b7825 ths
    len = TARGET_PAGE_ALIGN(len);
333 831b7825 ths
    if (len == 0)
334 831b7825 ths
        return -EINVAL;
335 831b7825 ths
    end = start + len;
336 831b7825 ths
    host_start = start & qemu_host_page_mask;
337 831b7825 ths
    host_end = HOST_PAGE_ALIGN(end);
338 831b7825 ths
339 831b7825 ths
    if (start > host_start) {
340 831b7825 ths
        /* handle host page containing start */
341 831b7825 ths
        prot = 0;
342 831b7825 ths
        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
343 831b7825 ths
            prot |= page_get_flags(addr);
344 831b7825 ths
        }
345 831b7825 ths
        if (host_end == host_start + qemu_host_page_size) {
346 831b7825 ths
            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
347 831b7825 ths
                prot |= page_get_flags(addr);
348 831b7825 ths
            }
349 831b7825 ths
            end = host_end;
350 831b7825 ths
        }
351 831b7825 ths
        if (prot != 0)
352 831b7825 ths
            host_start += qemu_host_page_size;
353 831b7825 ths
    }
354 831b7825 ths
    if (end < host_end) {
355 831b7825 ths
        prot = 0;
356 831b7825 ths
        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
357 831b7825 ths
            prot |= page_get_flags(addr);
358 831b7825 ths
        }
359 831b7825 ths
        if (prot != 0)
360 831b7825 ths
            host_end -= qemu_host_page_size;
361 831b7825 ths
    }
362 831b7825 ths
363 831b7825 ths
    /* unmap what we can */
364 831b7825 ths
    if (host_start < host_end) {
365 831b7825 ths
        ret = munmap((void *)host_start, host_end - host_start);
366 831b7825 ths
        if (ret != 0)
367 831b7825 ths
            return ret;
368 831b7825 ths
    }
369 831b7825 ths
370 831b7825 ths
    page_set_flags(start, start + len, 0);
371 831b7825 ths
    return 0;
372 831b7825 ths
}
373 831b7825 ths
374 831b7825 ths
/* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
375 831b7825 ths
   blocks which have been allocated starting on a host page */
376 831b7825 ths
long target_mremap(unsigned long old_addr, unsigned long old_size,
377 831b7825 ths
                   unsigned long new_size, unsigned long flags,
378 831b7825 ths
                   unsigned long new_addr)
379 831b7825 ths
{
380 831b7825 ths
#ifndef __APPLE__
381 831b7825 ths
    /* XXX: use 5 args syscall */
382 831b7825 ths
    new_addr = (long)mremap((void *)old_addr, old_size, new_size, flags);
383 831b7825 ths
    if (new_addr == -1)
384 831b7825 ths
        return new_addr;
385 831b7825 ths
    prot = page_get_flags(old_addr);
386 831b7825 ths
    page_set_flags(old_addr, old_addr + old_size, 0);
387 831b7825 ths
    page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
388 831b7825 ths
    return new_addr;
389 831b7825 ths
#else
390 831b7825 ths
    qerror("target_mremap: unsupported\n");
391 831b7825 ths
#endif
392 831b7825 ths
393 831b7825 ths
}
394 831b7825 ths
395 831b7825 ths
int target_msync(unsigned long start, unsigned long len, int flags)
396 831b7825 ths
{
397 831b7825 ths
    unsigned long end;
398 831b7825 ths
399 831b7825 ths
    if (start & ~TARGET_PAGE_MASK)
400 831b7825 ths
        return -EINVAL;
401 831b7825 ths
    len = TARGET_PAGE_ALIGN(len);
402 831b7825 ths
    end = start + len;
403 831b7825 ths
    if (end < start)
404 831b7825 ths
        return -EINVAL;
405 831b7825 ths
    if (end == start)
406 831b7825 ths
        return 0;
407 831b7825 ths
408 831b7825 ths
    start &= qemu_host_page_mask;
409 831b7825 ths
    return msync((void *)start, end - start, flags);
410 831b7825 ths
}