VirtualBox

Changeset 3596 in vbox for trunk/src


Ignore:
Timestamp:
Jul 12, 2007 6:42:03 PM (18 years ago)
Author:
vboxsync
Message:

fixed error detection in mmap()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp

    r2981 r3596  
    131131     */
    132132    void *pv = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    133     if (!pv)
     133    if (pv == MAP_FAILED)
    134134        return VERR_NO_MEMORY;
    135135    g_fSysMadviseWorks = (0 == madvise(pv, PAGE_SIZE, MADV_DONTFORK));
     
    253253int     suplibOsPageAlloc(size_t cPages, void **ppvPages)
    254254{
    255 #if 0
    256     int rc = posix_memalign(ppvPages, PAGE_SIZE, cPages << PAGE_SHIFT)
    257     if (!rc)
    258     {
    259         memset(*ppvPages, 0, cPages << PAGE_SHIFT);
    260         return VINF_SUCCESS;
    261     }
    262     return RTErrConvertFromErrno(rc);
    263 #else
    264255    size_t cbMmap = (g_fSysMadviseWorks ? cPages : cPages + 2) << PAGE_SHIFT;
    265256    char *pvPages = (char *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    266     if (pvPages)
    267     {
    268         if (g_fSysMadviseWorks)
    269         {
    270             /*
    271              * It is not fatal if we fail here but a forked child (e.g. the ALSA sound server)
    272              * could crash. Linux < 2.6.16 does not implement madvise(MADV_DONTFORK) but the
    273              * kernel seems to split bigger VMAs and that is all that we want -- later we set the
    274              * VM_DONTCOPY attribute in supdrvOSLockMemOne().
    275              */
    276             if (madvise (pvPages, cbMmap, MADV_DONTFORK))
    277                 LogRel(("SUPLib: madvise %p-%p failed\n", pvPages, cbMmap));
    278             *ppvPages = pvPages;
    279         }
    280         else
    281         {
    282             /*
    283              * madvise(MADV_DONTFORK) is not available (most probably Linux 2.4). Enclose any
    284              * mmapped region by two unmapped pages to guarantee that there is exactly one VM
    285              * area struct of the very same size as the mmap area.
    286              */
    287             mprotect(pvPages,                      PAGE_SIZE, PROT_NONE);
    288             mprotect(pvPages + cbMmap - PAGE_SIZE, PAGE_SIZE, PROT_NONE);
    289             *ppvPages = pvPages + PAGE_SIZE;
    290         }
    291         memset(*ppvPages, 0, cPages << PAGE_SHIFT);
    292         return VINF_SUCCESS;
    293     }
    294     return VERR_NO_MEMORY;
    295 #endif
     257    if (pvPages == MAP_FAILED)
     258        return VERR_NO_MEMORY;
     259
     260    if (g_fSysMadviseWorks)
     261    {
     262        /*
     263         * It is not fatal if we fail here but a forked child (e.g. the ALSA sound server)
     264         * could crash. Linux < 2.6.16 does not implement madvise(MADV_DONTFORK) but the
     265         * kernel seems to split bigger VMAs and that is all that we want -- later we set the
     266         * VM_DONTCOPY attribute in supdrvOSLockMemOne().
     267         */
     268        if (madvise (pvPages, cbMmap, MADV_DONTFORK))
     269            LogRel(("SUPLib: madvise %p-%p failed\n", pvPages, cbMmap));
     270        *ppvPages = pvPages;
     271    }
     272    else
     273    {
     274        /*
     275         * madvise(MADV_DONTFORK) is not available (most probably Linux 2.4). Enclose any
     276         * mmapped region by two unmapped pages to guarantee that there is exactly one VM
     277         * area struct of the very same size as the mmap area.
     278         */
     279        mprotect(pvPages,                      PAGE_SIZE, PROT_NONE);
     280        mprotect(pvPages + cbMmap - PAGE_SIZE, PAGE_SIZE, PROT_NONE);
     281        *ppvPages = pvPages + PAGE_SIZE;
     282    }
     283    memset(*ppvPages, 0, cPages << PAGE_SHIFT);
     284    return VINF_SUCCESS;
    296285}
    297286
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette