VirtualBox

Changeset 108724 in vbox for trunk/src


Ignore:
Timestamp:
Mar 24, 2025 6:53:21 PM (3 weeks ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
168147
Message:

HostDrivers/Support: Make SUPR3 work on hosts where the page size is not known during compile time, bugref:10391

Location:
trunk/src/VBox/HostDrivers/Support
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/Makefile.kmk

    r106998 r108724  
    4949# Targets
    5050#
    51 if "$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH)" != "linux.arm64" || defined(VBOX_WITH_STATIC_ARM64_PAGE_SHIFT) # @todo Required by LoadGenerator in the validationkit but uses PAGE_SIZE++
    52  LIBRARIES += SUPR3 SUPR3Static
    53 endif
     51LIBRARIES += SUPR3 SUPR3Static
    5452if   defined(VBOX_WITH_HARDENING) \
    5553  && !defined(VBOX_ONLY_VALIDATIONKIT)
  • trunk/src/VBox/HostDrivers/Support/SUPLib.cpp

    r108481 r108724  
    7575#include <iprt/path.h>
    7676#include <iprt/string.h>
     77#include <iprt/system.h>
    7778#include <iprt/env.h>
    7879#include <iprt/rand.h>
     
    551552
    552553        /* fake the GIP. */
    553         g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
     554        g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(SUP_PAGE_SIZE);
    554555        if (g_pSUPGlobalInfoPage)
    555556        {
    556557            g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
    557             g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
     558            g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)SUP_PAGE_OFFSET_MASK;
    558559            /* the page is supposed to be invalid, so don't set the magic. */
    559560            return VINF_SUCCESS;
     
    10021003SUPR3DECL(int) supR3PageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
    10031004{
     1005    uint32_t  const cbPage = SUP_PAGE_SIZE;
     1006
    10041007    /*
    10051008     * Validate.
    10061009     */
    10071010    AssertPtr(pvStart);
    1008     AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
     1011    AssertMsg(RT_ALIGN_P(pvStart, cbPage) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
    10091012    AssertPtr(paPages);
    10101013
     
    10121015    if (RT_UNLIKELY(g_uSupFakeMode))
    10131016    {
    1014         RTHCPHYS    Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
     1017        RTHCPHYS    Phys = (uintptr_t)pvStart + cbPage * 1024;
    10151018        size_t      iPage = cPages;
    10161019        while (iPage-- > 0)
    1017             paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
     1020            paPages[iPage].Phys = Phys + (iPage << SUP_PAGE_SHIFT);
    10181021        return VINF_SUCCESS;
    10191022    }
     
    10681071     */
    10691072    AssertPtr(pvStart);
    1070     AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
     1073    AssertMsg(RT_ALIGN_P(pvStart, SUP_PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
    10711074
    10721075    /* fake */
     
    11171120
    11181121
     1122#ifdef VBOX_WITH_R0_MODULES
    11191123/**
    11201124 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
     
    11351139    return rc;
    11361140}
     1141#endif /* VBOX_WITH_R0_MODULES */
    11371142
    11381143
     
    11481153        *pR0Ptr = NIL_RTR0PTR;
    11491154    AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
    1150     AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
     1155    AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_SIZE / SUP_PAGE_SIZE, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
    11511156    AssertReturn(!fFlags, VERR_INVALID_FLAGS);
    11521157
     
    11571162    {
    11581163        int rc = SUPR3PageAlloc(cPages, 0 /*fFlags*/, ppvPages);
    1159         Assert(RT_FAILURE(rc) || ASMMemIsZero(*ppvPages, cPages << PAGE_SHIFT));
     1164        Assert(RT_FAILURE(rc) || ASMMemIsZero(*ppvPages, cPages << SUP_PAGE_SHIFT));
    11601165        if (pR0Ptr)
    11611166            *pR0Ptr = NIL_RTR0PTR;
     
    11761181        return VERR_WRONG_ORDER;
    11771182
     1183#ifdef VBOX_WITH_R0_MODULES
    11781184    /*
    11791185     * Use fallback for non-R0 mapping?
     
    12401246        rc = VERR_NO_TMP_MEMORY;
    12411247    return rc;
    1242 
    1243 }
    1244 
    1245 
     1248#else
     1249    AssertFailedReturn(VERR_NOT_SUPPORTED);
     1250#endif /* VBOX_WITH_R0_MODULES */
     1251}
     1252
     1253
     1254#ifdef VBOX_WITH_R0_MODULES
    12461255SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
    12471256{
     
    12821291    return rc;
    12831292}
     1293#endif
    12841294
    12851295
     
    12901300     */
    12911301    AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
    1292     Assert(!(off & PAGE_OFFSET_MASK));
    1293     Assert(!(cb & PAGE_OFFSET_MASK) && cb);
     1302    Assert(!(off & SUP_PAGE_OFFSET_MASK));
     1303    Assert(!(cb & SUP_PAGE_OFFSET_MASK) && cb);
    12941304    AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
    12951305
     
    14761486    if (RT_UNLIKELY(g_uSupFakeMode))
    14771487    {
    1478         *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
     1488        *ppvPages = RTMemPageAllocZ((size_t)cPages * SUP_PAGE_SIZE);
    14791489        if (!*ppvPages)
    14801490            return VERR_NO_LOW_MEMORY;
    14811491
    14821492        /* fake physical addresses. */
    1483         RTHCPHYS    Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
     1493        RTHCPHYS    Phys = (uintptr_t)*ppvPages + SUP_PAGE_SIZE * 1024;
    14841494        size_t      iPage = cPages;
    14851495        while (iPage-- > 0)
    1486             paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
     1496            paPages[iPage].Phys = Phys + (iPage << SUP_PAGE_SHIFT);
    14871497        return VINF_SUCCESS;
    14881498    }
     
    15441554    if (RT_UNLIKELY(g_uSupFakeMode))
    15451555    {
    1546         RTMemPageFree(pv, cPages * PAGE_SIZE);
     1556        RTMemPageFree(pv, cPages * SUP_PAGE_SIZE);
    15471557        return VINF_SUCCESS;
    15481558    }
  • trunk/src/VBox/HostDrivers/Support/SUPLibInternal.h

    r107800 r108724  
    4444#include <VBox/types.h>
    4545#include <iprt/stdarg.h>
     46#if defined(RT_OS_LINUX) && defined(RT_ARCH_ARM64)
     47# include <iprt/system.h>
     48#endif
    4649
    4750
     
    8386# undef  SUP_HARDENED_SUID
    8487#endif
     88
     89/** @def SUP_PAGE_SIZE
     90 * Page size of the host, can evaluate to a function call on certain hosts where
     91 * the page size is not known during compile time.
     92 */
     93#if defined(RT_OS_LINUX) && defined(RT_ARCH_ARM64)
     94# define SUP_PAGE_SIZE RTSystemGetPageSize()
     95#else
     96# define SUP_PAGE_SIZE HOST_PAGE_SIZE
     97#endif
     98
     99/** @def SUP_PAGE_SHIFT
     100 * Page shift of the host, can evaluate to a function call on certain hosts where
     101 * the page size is not known during compile time.
     102 */
     103#if defined(RT_OS_LINUX) && defined(RT_ARCH_ARM64)
     104# define SUP_PAGE_SHIFT RTSystemGetPageShift()
     105#else
     106# define SUP_PAGE_SHIFT HOST_PAGE_SHIFT
     107#endif
     108
     109/** @def SUP_PAGE_OFFSET_MASK
     110 * Page shift of the host, can evaluate to a function call on certain hosts where
     111 * the page size is not known during compile time.
     112 */
     113#if defined(RT_OS_LINUX) && defined(RT_ARCH_ARM64)
     114# define SUP_PAGE_OFFSET_MASK RTSystemGetPageOffsetMask()
     115#else
     116# define SUP_PAGE_OFFSET_MASK HOST_PAGE_OFFSET_MASK
     117#endif
     118
    85119
    86120#ifdef IN_SUP_HARDENED_R3
  • trunk/src/VBox/HostDrivers/Support/SUPLibLdr.cpp

    r107675 r108724  
    413413     * a little more complicated...
    414414     */
     415    uint32_t  const cbPage      = SUP_PAGE_SIZE;
     416    uint32_t  const cPageShift  = SUP_PAGE_SHIFT;
     417    uintptr_t const offPageMask = SUP_PAGE_OFFSET_MASK;
    415418    if (pArgs->uStartRva < pArgs->uEndRva)
    416419    {
    417         if (((pArgs->uEndRva - 1) >> PAGE_SHIFT) != (uRvaSeg >> PAGE_SHIFT))
     420        if (((pArgs->uEndRva - 1) >> cPageShift) != (uRvaSeg >> cPageShift))
    418421        {
    419422            /* No common page, so make the new segment start on a page boundrary. */
    420             cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
    421             uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
     423            cbMapped += uRvaSeg & offPageMask;
     424            uRvaSeg &= ~(uint32_t)offPageMask;
    422425            Assert(pArgs->uEndRva <= uRvaSeg);
    423426            Log2(("supLoadModuleCompileSegmentsCB: -> new, no common\n"));
     
    427430            /* The current segment includes the memory protections of the
    428431               previous, so include the common page in it: */
    429             uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
     432            uint32_t const cbCommon = cbPage - (uRvaSeg & offPageMask);
    430433            if (cbCommon >= cbMapped)
    431434            {
     
    444447            /* The new segment includes the memory protections of the
    445448               previous, so include the common page in it: */
    446             cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
    447             uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
     449            cbMapped += uRvaSeg & offPageMask;
     450            uRvaSeg &= ~(uint32_t)offPageMask;
    448451            if (uRvaSeg == pArgs->uStartRva)
    449452            {
     
    453456                return VINF_SUCCESS; /* Current segment was smaller than a page. */
    454457            }
    455             Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into new\n", (uint32_t)(pSeg->RVA & PAGE_OFFSET_MASK)));
     458            Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into new\n", (uint32_t)(pSeg->RVA & offPageMask)));
    456459        }
    457460        else
     
    459462            /* Create a new segment for the common page with the combined protection. */
    460463            Log2(("supLoadModuleCompileSegmentsCB: -> it's complicated...\n"));
    461             pArgs->uEndRva &= ~(uint32_t)PAGE_OFFSET_MASK;
     464            pArgs->uEndRva &= ~(uint32_t)offPageMask;
    462465            if (pArgs->uEndRva > pArgs->uStartRva)
    463466            {
     
    477480            pArgs->fProt |= fProt;
    478481
    479             uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
     482            uint32_t const cbCommon = cbPage - (uRvaSeg & offPageMask);
    480483            if (cbCommon >= cbMapped)
    481484            {
     
    485488            cbMapped -= cbCommon;
    486489            uRvaSeg  += cbCommon;
    487             Assert(uRvaSeg - pArgs->uStartRva == PAGE_SIZE);
     490            Assert(uRvaSeg - pArgs->uStartRva == cbPage);
    488491        }
    489492
     
    507510
    508511    /* Start the new segment. */
    509     Assert(!(uRvaSeg & PAGE_OFFSET_MASK));
     512    Assert(!(uRvaSeg & offPageMask));
    510513    pArgs->fProt     = fProt;
    511514    pArgs->uStartRva = uRvaSeg;
  • trunk/src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp

    r106061 r108724  
    6464#include <VBox/types.h>
    6565#include <iprt/string.h>
    66 #include <iprt/system.h>
    6766#include <VBox/err.h>
    6867#include <VBox/param.h>
     
    10099     * Check if madvise works.
    101100     */
    102     void *pv = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     101    uint32_t const cbPage = SUP_PAGE_SIZE;
     102    void *pv = mmap(NULL, cbPage, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    103103    if (pv == MAP_FAILED)
    104104        return VERR_NO_MEMORY;
    105     pThis->fSysMadviseWorks = (0 == madvise(pv, PAGE_SIZE, MADV_DONTFORK));
    106     munmap(pv, PAGE_SIZE);
     105    pThis->fSysMadviseWorks = (0 == madvise(pv, cbPage, MADV_DONTFORK));
     106    munmap(pv, cbPage);
    107107
    108108    /*
     
    256256#endif
    257257
    258     size_t cbMmap = cPages << PAGE_SHIFT;
     258    uint32_t const cbPage = SUP_PAGE_SIZE;
     259    uint32_t const cPageShift = SUP_PAGE_SHIFT;
     260
     261    size_t cbMmap = cPages << cPageShift;
    259262    if (   !pThis->fSysMadviseWorks
    260263        && (fFlags & (SUP_PAGE_ALLOC_F_FOR_LOCKING | SUP_PAGE_ALLOC_F_LARGE_PAGES)) == SUP_PAGE_ALLOC_F_FOR_LOCKING)
    261         cbMmap += PAGE_SIZE * 2;
     264        cbMmap += cbPage * 2;
    262265
    263266    uint8_t *pbPages = (uint8_t *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, fMmap, -1, 0);
     
    268271        fMmap &= ~MAP_HUGETLB;
    269272        if (!pThis->fSysMadviseWorks && (fFlags & SUP_PAGE_ALLOC_F_FOR_LOCKING))
    270             cbMmap = (cPages + 2) << PAGE_SHIFT;
     273            cbMmap = (cPages + 2) << cPageShift;
    271274        pbPages = (uint8_t *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, fMmap, -1, 0);
    272275    }
     
    313316             * area struct of the very same size as the mmap area.
    314317             */
    315             mprotect(pbPages,                      PAGE_SIZE, PROT_NONE);
    316             mprotect(pbPages + cbMmap - PAGE_SIZE, PAGE_SIZE, PROT_NONE);
    317             pbPages += PAGE_SHIFT;
     318            mprotect(pbPages,                   cbPage, PROT_NONE);
     319            mprotect(pbPages + cbMmap - cbPage, cbPage, PROT_NONE);
     320            pbPages += cPageShift;
    318321        }
    319322
     
    322325         * so I qualified it with SUP_PAGE_ALLOC_F_FOR_LOCKING (unused) for now... */
    323326        if (fFlags & SUP_PAGE_ALLOC_F_FOR_LOCKING)
    324             memset(pbPages, 0, cPages << PAGE_SHIFT);
     327            memset(pbPages, 0, cPages << cPageShift);
    325328
    326329        *ppvPages = pbPages;
     
    334337{
    335338    NOREF(pThis);
    336     munmap(pvPages, cPages << PAGE_SHIFT);
     339    munmap(pvPages, cPages << SUP_PAGE_SHIFT);
    337340    return VINF_SUCCESS;
    338341}
Note: See TracChangeset for help on using the changeset viewer.

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