Statistics
| Branch: | Revision:

root / darwin-user / mmap.c @ f58ae59c

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