qemu/bsd-user/mmap.c
<<
>>
Prefs
   1/*
   2 *  mmap support for qemu
   3 *
   4 *  Copyright (c) 2003 - 2008 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, see <http://www.gnu.org/licenses/>.
  18 */
  19#include <stdlib.h>
  20#include <stdio.h>
  21#include <stdarg.h>
  22#include <string.h>
  23#include <unistd.h>
  24#include <errno.h>
  25#include <sys/mman.h>
  26
  27#include "qemu.h"
  28#include "qemu-common.h"
  29#include "bsd-mman.h"
  30
  31//#define DEBUG_MMAP
  32
  33#if defined(CONFIG_USE_NPTL)
  34pthread_mutex_t mmap_mutex;
  35static int __thread mmap_lock_count;
  36
  37void mmap_lock(void)
  38{
  39    if (mmap_lock_count++ == 0) {
  40        pthread_mutex_lock(&mmap_mutex);
  41    }
  42}
  43
  44void 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().  */
  52void mmap_fork_start(void)
  53{
  54    if (mmap_lock_count)
  55        abort();
  56    pthread_mutex_lock(&mmap_mutex);
  57}
  58
  59void 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.  */
  68void mmap_lock(void)
  69{
  70}
  71
  72void mmap_unlock(void)
  73{
  74}
  75#endif
  76
  77/* NOTE: all the constants are the HOST ones, but addresses are target. */
  78int target_mprotect(abi_ulong start, abi_ulong len, int prot)
  79{
  80    abi_ulong end, host_start, host_end, addr;
  81    int prot1, ret;
  82
  83#ifdef DEBUG_MMAP
  84    printf("mprotect: start=0x" TARGET_FMT_lx
  85           " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
  86           prot & PROT_READ ? 'r' : '-',
  87           prot & PROT_WRITE ? 'w' : '-',
  88           prot & PROT_EXEC ? 'x' : '-');
  89#endif
  90
  91    if ((start & ~TARGET_PAGE_MASK) != 0)
  92        return -EINVAL;
  93    len = TARGET_PAGE_ALIGN(len);
  94    end = start + len;
  95    if (end < start)
  96        return -EINVAL;
  97    prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
  98    if (len == 0)
  99        return 0;
 100
 101    mmap_lock();
 102    host_start = start & qemu_host_page_mask;
 103    host_end = HOST_PAGE_ALIGN(end);
 104    if (start > host_start) {
 105        /* handle host page containing start */
 106        prot1 = prot;
 107        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
 108            prot1 |= page_get_flags(addr);
 109        }
 110        if (host_end == host_start + qemu_host_page_size) {
 111            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
 112                prot1 |= page_get_flags(addr);
 113            }
 114            end = host_end;
 115        }
 116        ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
 117        if (ret != 0)
 118            goto error;
 119        host_start += qemu_host_page_size;
 120    }
 121    if (end < host_end) {
 122        prot1 = prot;
 123        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
 124            prot1 |= page_get_flags(addr);
 125        }
 126        ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
 127                       prot1 & PAGE_BITS);
 128        if (ret != 0)
 129            goto error;
 130        host_end -= qemu_host_page_size;
 131    }
 132
 133    /* handle the pages in the middle */
 134    if (host_start < host_end) {
 135        ret = mprotect(g2h(host_start), host_end - host_start, prot);
 136        if (ret != 0)
 137            goto error;
 138    }
 139    page_set_flags(start, start + len, prot | PAGE_VALID);
 140    mmap_unlock();
 141    return 0;
 142error:
 143    mmap_unlock();
 144    return ret;
 145}
 146
 147/* map an incomplete host page */
 148static int mmap_frag(abi_ulong real_start,
 149                     abi_ulong start, abi_ulong end,
 150                     int prot, int flags, int fd, abi_ulong offset)
 151{
 152    abi_ulong real_end, addr;
 153    void *host_start;
 154    int prot1, prot_new;
 155
 156    real_end = real_start + qemu_host_page_size;
 157    host_start = g2h(real_start);
 158
 159    /* get the protection of the target pages outside the mapping */
 160    prot1 = 0;
 161    for(addr = real_start; addr < real_end; addr++) {
 162        if (addr < start || addr >= end)
 163            prot1 |= page_get_flags(addr);
 164    }
 165
 166    if (prot1 == 0) {
 167        /* no page was there, so we allocate one */
 168        void *p = mmap(host_start, qemu_host_page_size, prot,
 169                       flags | MAP_ANON, -1, 0);
 170        if (p == MAP_FAILED)
 171            return -1;
 172        prot1 = prot;
 173    }
 174    prot1 &= PAGE_BITS;
 175
 176    prot_new = prot | prot1;
 177    if (!(flags & MAP_ANON)) {
 178        /* msync() won't work here, so we return an error if write is
 179           possible while it is a shared mapping */
 180        if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
 181            (prot & PROT_WRITE))
 182            return -1;
 183
 184        /* adjust protection to be able to read */
 185        if (!(prot1 & PROT_WRITE))
 186            mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
 187
 188        /* read the corresponding file data */
 189        pread(fd, g2h(start), end - start, offset);
 190
 191        /* put final protection */
 192        if (prot_new != (prot1 | PROT_WRITE))
 193            mprotect(host_start, qemu_host_page_size, prot_new);
 194    } else {
 195        /* just update the protection */
 196        if (prot_new != prot1) {
 197            mprotect(host_start, qemu_host_page_size, prot_new);
 198        }
 199    }
 200    return 0;
 201}
 202
 203#if defined(__CYGWIN__)
 204/* Cygwin doesn't have a whole lot of address space.  */
 205static abi_ulong mmap_next_start = 0x18000000;
 206#else
 207static abi_ulong mmap_next_start = 0x40000000;
 208#endif
 209
 210unsigned long last_brk;
 211
 212/* find a free memory area of size 'size'. The search starts at
 213   'start'. If 'start' == 0, then a default start address is used.
 214   Return -1 if error.
 215*/
 216/* page_init() marks pages used by the host as reserved to be sure not
 217   to use them. */
 218static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
 219{
 220    abi_ulong addr, addr1, addr_start;
 221    int prot;
 222    unsigned long new_brk;
 223
 224    new_brk = (unsigned long)sbrk(0);
 225    if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
 226        /* This is a hack to catch the host allocating memory with brk().
 227           If it uses mmap then we loose.
 228           FIXME: We really want to avoid the host allocating memory in
 229           the first place, and maybe leave some slack to avoid switching
 230           to mmap.  */
 231        page_set_flags(last_brk & TARGET_PAGE_MASK,
 232                       TARGET_PAGE_ALIGN(new_brk),
 233                       PAGE_RESERVED);
 234    }
 235    last_brk = new_brk;
 236
 237    size = HOST_PAGE_ALIGN(size);
 238    start = start & qemu_host_page_mask;
 239    addr = start;
 240    if (addr == 0)
 241        addr = mmap_next_start;
 242    addr_start = addr;
 243    for(;;) {
 244        prot = 0;
 245        for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
 246            prot |= page_get_flags(addr1);
 247        }
 248        if (prot == 0)
 249            break;
 250        addr += qemu_host_page_size;
 251        /* we found nothing */
 252        if (addr == addr_start)
 253            return (abi_ulong)-1;
 254    }
 255    if (start == 0)
 256        mmap_next_start = addr + size;
 257    return addr;
 258}
 259
 260/* NOTE: all the constants are the HOST ones */
 261abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
 262                     int flags, int fd, abi_ulong offset)
 263{
 264    abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
 265    unsigned long host_start;
 266
 267    mmap_lock();
 268#ifdef DEBUG_MMAP
 269    {
 270        printf("mmap: start=0x" TARGET_FMT_lx
 271               " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
 272               start, len,
 273               prot & PROT_READ ? 'r' : '-',
 274               prot & PROT_WRITE ? 'w' : '-',
 275               prot & PROT_EXEC ? 'x' : '-');
 276        if (flags & MAP_FIXED)
 277            printf("MAP_FIXED ");
 278        if (flags & MAP_ANON)
 279            printf("MAP_ANON ");
 280        switch(flags & TARGET_BSD_MAP_FLAGMASK) {
 281        case MAP_PRIVATE:
 282            printf("MAP_PRIVATE ");
 283            break;
 284        case MAP_SHARED:
 285            printf("MAP_SHARED ");
 286            break;
 287        default:
 288            printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK);
 289            break;
 290        }
 291        printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
 292    }
 293#endif
 294
 295    if (offset & ~TARGET_PAGE_MASK) {
 296        errno = EINVAL;
 297        goto fail;
 298    }
 299
 300    len = TARGET_PAGE_ALIGN(len);
 301    if (len == 0)
 302        goto the_end;
 303    real_start = start & qemu_host_page_mask;
 304
 305    if (!(flags & MAP_FIXED)) {
 306        abi_ulong mmap_start;
 307        void *p;
 308        host_offset = offset & qemu_host_page_mask;
 309        host_len = len + offset - host_offset;
 310        host_len = HOST_PAGE_ALIGN(host_len);
 311        mmap_start = mmap_find_vma(real_start, host_len);
 312        if (mmap_start == (abi_ulong)-1) {
 313            errno = ENOMEM;
 314            goto fail;
 315        }
 316        /* Note: we prefer to control the mapping address. It is
 317           especially important if qemu_host_page_size >
 318           qemu_real_host_page_size */
 319        p = mmap(g2h(mmap_start),
 320                 host_len, prot, flags | MAP_FIXED, fd, host_offset);
 321        if (p == MAP_FAILED)
 322            goto fail;
 323        /* update start so that it points to the file position at 'offset' */
 324        host_start = (unsigned long)p;
 325        if (!(flags & MAP_ANON))
 326            host_start += offset - host_offset;
 327        start = h2g(host_start);
 328    } else {
 329        int flg;
 330        target_ulong addr;
 331
 332        if (start & ~TARGET_PAGE_MASK) {
 333            errno = EINVAL;
 334            goto fail;
 335        }
 336        end = start + len;
 337        real_end = HOST_PAGE_ALIGN(end);
 338
 339        for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
 340            flg = page_get_flags(addr);
 341            if (flg & PAGE_RESERVED) {
 342                errno = ENXIO;
 343                goto fail;
 344            }
 345        }
 346
 347        /* worst case: we cannot map the file because the offset is not
 348           aligned, so we read it */
 349        if (!(flags & MAP_ANON) &&
 350            (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
 351            /* msync() won't work here, so we return an error if write is
 352               possible while it is a shared mapping */
 353            if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
 354                (prot & PROT_WRITE)) {
 355                errno = EINVAL;
 356                goto fail;
 357            }
 358            retaddr = target_mmap(start, len, prot | PROT_WRITE,
 359                                  MAP_FIXED | MAP_PRIVATE | MAP_ANON,
 360                                  -1, 0);
 361            if (retaddr == -1)
 362                goto fail;
 363            pread(fd, g2h(start), len, offset);
 364            if (!(prot & PROT_WRITE)) {
 365                ret = target_mprotect(start, len, prot);
 366                if (ret != 0) {
 367                    start = ret;
 368                    goto the_end;
 369                }
 370            }
 371            goto the_end;
 372        }
 373
 374        /* handle the start of the mapping */
 375        if (start > real_start) {
 376            if (real_end == real_start + qemu_host_page_size) {
 377                /* one single host page */
 378                ret = mmap_frag(real_start, start, end,
 379                                prot, flags, fd, offset);
 380                if (ret == -1)
 381                    goto fail;
 382                goto the_end1;
 383            }
 384            ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
 385                            prot, flags, fd, offset);
 386            if (ret == -1)
 387                goto fail;
 388            real_start += qemu_host_page_size;
 389        }
 390        /* handle the end of the mapping */
 391        if (end < real_end) {
 392            ret = mmap_frag(real_end - qemu_host_page_size,
 393                            real_end - qemu_host_page_size, real_end,
 394                            prot, flags, fd,
 395                            offset + real_end - qemu_host_page_size - start);
 396            if (ret == -1)
 397                goto fail;
 398            real_end -= qemu_host_page_size;
 399        }
 400
 401        /* map the middle (easier) */
 402        if (real_start < real_end) {
 403            void *p;
 404            unsigned long offset1;
 405            if (flags & MAP_ANON)
 406                offset1 = 0;
 407            else
 408                offset1 = offset + real_start - start;
 409            p = mmap(g2h(real_start), real_end - real_start,
 410                     prot, flags, fd, offset1);
 411            if (p == MAP_FAILED)
 412                goto fail;
 413        }
 414    }
 415 the_end1:
 416    page_set_flags(start, start + len, prot | PAGE_VALID);
 417 the_end:
 418#ifdef DEBUG_MMAP
 419    printf("ret=0x" TARGET_FMT_lx "\n", start);
 420    page_dump(stdout);
 421    printf("\n");
 422#endif
 423    mmap_unlock();
 424    return start;
 425fail:
 426    mmap_unlock();
 427    return -1;
 428}
 429
 430int target_munmap(abi_ulong start, abi_ulong len)
 431{
 432    abi_ulong end, real_start, real_end, addr;
 433    int prot, ret;
 434
 435#ifdef DEBUG_MMAP
 436    printf("munmap: start=0x%lx len=0x%lx\n", start, len);
 437#endif
 438    if (start & ~TARGET_PAGE_MASK)
 439        return -EINVAL;
 440    len = TARGET_PAGE_ALIGN(len);
 441    if (len == 0)
 442        return -EINVAL;
 443    mmap_lock();
 444    end = start + len;
 445    real_start = start & qemu_host_page_mask;
 446    real_end = HOST_PAGE_ALIGN(end);
 447
 448    if (start > real_start) {
 449        /* handle host page containing start */
 450        prot = 0;
 451        for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
 452            prot |= page_get_flags(addr);
 453        }
 454        if (real_end == real_start + qemu_host_page_size) {
 455            for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
 456                prot |= page_get_flags(addr);
 457            }
 458            end = real_end;
 459        }
 460        if (prot != 0)
 461            real_start += qemu_host_page_size;
 462    }
 463    if (end < real_end) {
 464        prot = 0;
 465        for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
 466            prot |= page_get_flags(addr);
 467        }
 468        if (prot != 0)
 469            real_end -= qemu_host_page_size;
 470    }
 471
 472    ret = 0;
 473    /* unmap what we can */
 474    if (real_start < real_end) {
 475        ret = munmap(g2h(real_start), real_end - real_start);
 476    }
 477
 478    if (ret == 0)
 479        page_set_flags(start, start + len, 0);
 480    mmap_unlock();
 481    return ret;
 482}
 483
 484int target_msync(abi_ulong start, abi_ulong len, int flags)
 485{
 486    abi_ulong end;
 487
 488    if (start & ~TARGET_PAGE_MASK)
 489        return -EINVAL;
 490    len = TARGET_PAGE_ALIGN(len);
 491    end = start + len;
 492    if (end < start)
 493        return -EINVAL;
 494    if (end == start)
 495        return 0;
 496
 497    start &= qemu_host_page_mask;
 498    return msync(g2h(start), end - start, flags);
 499}
 500