VirtualBox

Changeset 99457 in vbox


Ignore:
Timestamp:
Apr 19, 2023 12:09:56 PM (2 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
156928
Message:

HostDrivers/testcase: Overhauled tstPin (Memory locking interface in ring 3) to use our testing framework; this also (hopefully) should fix the unit tests on the testboxes, as it now should comply with our exit strategy for testcases.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/testcase/tstPin.cpp

    r98103 r99457  
    4343#include <iprt/errcore.h>
    4444#include <iprt/initterm.h>
    45 #include <iprt/stream.h>
     45#include <iprt/string.h>
     46#include <iprt/test.h>
    4647#include <iprt/thread.h>
    47 #include <iprt/string.h>
     48
    4849
    4950#include "../SUPLibInternal.h"
     
    5253int main(int argc, char **argv)
    5354{
    54     int         rc;
    55     int         rcRet = 0;
    56     RTHCPHYS    HCPhys;
    57 
    58     RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_TRY_SUPLIB);
    59     rc = SUPR3Init(NULL);
    60     RTPrintf("SUPR3Init -> rc=%d\n", rc);
    61     rcRet += rc != 0;
    62     if (!rc)
    63     {
    64         /*
    65          * Simple test.
    66          */
    67         void *pv;
    68         rc = SUPR3PageAlloc(1, 0, &pv);
    69         AssertRC(rc);
    70         RTPrintf("pv=%p\n", pv);
    71         SUPPAGE aPages[1];
    72         rc = supR3PageLock(pv, 1, &aPages[0]);
    73         RTPrintf("rc=%d pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
    74         RTThreadSleep(1500);
     55    RTTEST hTest;
     56    RTEXITCODE rcExit = RTTestInitExAndCreate(argc, &argv, RTR3INIT_FLAGS_TRY_SUPLIB, "tstPin", &hTest);
     57    if (rcExit != RTEXITCODE_SUCCESS)
     58        return rcExit;
     59    RTTestBanner(hTest);
     60
     61    RTHCPHYS HCPhys;
     62
     63    /*
     64     * Simple test.
     65     */
     66    RTTestISub("Simple");
     67
     68    void *pv;
     69    int rc = SUPR3PageAlloc(1, 0, &pv);
     70    RTTESTI_CHECK_RC_OK(rc);
     71    RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc, pv=%p\n", rc, pv);
     72    SUPPAGE aPages[1];
     73    rc = supR3PageLock(pv, 1, &aPages[0]);
     74    RTTESTI_CHECK_RC_OK(rc);
     75    RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc pv=%p aPages[0]=%RHp\n", rc, pv, aPages[0]);
     76    RTThreadSleep(1500);
    7577#if 0
    76         RTPrintf("Unlocking...\n");
    77         RTThreadSleep(250);
    78         rc = SUPPageUnlock(pv);
    79         RTPrintf("rc=%d\n", rc);
    80         RTThreadSleep(1500);
     78    RTTestIPrintf(RTTESTLVL_DEBUG, "Unlocking...\n");
     79    RTThreadSleep(250);
     80    rc = SUPPageUnlock(pv);
     81    RTTestIPrintf(RTTESTLVL_DEBUG, "rc=%Rrc\n", rc);
     82    RTThreadSleep(1500);
    8183#endif
    8284
    83         /*
    84          * More extensive.
    85          */
    86         static struct
    87         {
    88             void       *pv;
    89             void       *pvAligned;
    90             SUPPAGE     aPages[16];
    91         } aPinnings[500];
    92         for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
    93         {
     85    RTTestISubDone();
     86
     87    RTTestISub("Extensive");
     88
     89    /*
     90     * More extensive.
     91     */
     92    static struct
     93    {
     94        void       *pv;
     95        void       *pvAligned;
     96        SUPPAGE     aPages[16];
     97    } aPinnings[500];
     98
     99    for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i++)
     100    {
     101        aPinnings[i].pv = NULL;
     102        SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, 0, &aPinnings[i].pv);
     103        aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
     104        rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
     105        if (RT_SUCCESS(rc))
     106        {
     107            RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
     108            memset(aPinnings[i].pv, 0xfa, 0x10000);
     109            unsigned c4GPluss = 0;
     110            for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
     111                if (aPinnings[i].aPages[j].Phys >= _4G)
     112                {
     113                    RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
     114                    c4GPluss++;
     115                }
     116            RTTestIPrintf(RTTESTLVL_DEBUG, "i=%d: c4GPluss=%d\n", i, c4GPluss);
     117        }
     118        else
     119        {
     120            RTTestIFailed("SUPPageLock() failed with rc=%Rrc\n", rc);
     121            SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
     122            aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
     123            break;
     124        }
     125    }
     126
     127    for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
     128    {
     129        if (aPinnings[i].pvAligned)
     130        {
     131            rc = supR3PageUnlock(aPinnings[i].pvAligned);
     132            RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("SUPPageUnlock(%p) -> rc=%Rrc\n", aPinnings[i].pvAligned, rc));
     133            memset(aPinnings[i].pv, 0xaf, 0x10000);
     134        }
     135    }
     136
     137    for (unsigned i = 0; i < RT_ELEMENTS(aPinnings); i += 2)
     138    {
     139        if (aPinnings[i].pv)
     140        {
     141            memset(aPinnings[i].pv, 0xcc, 0x10000);
     142            SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
    94143            aPinnings[i].pv = NULL;
    95             SUPR3PageAlloc(0x10000 >> PAGE_SHIFT, 0, &aPinnings[i].pv);
    96             aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
    97             rc = supR3PageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
    98             if (!rc)
    99             {
    100                 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
    101                 memset(aPinnings[i].pv, 0xfa, 0x10000);
    102                 unsigned c4GPluss = 0;
    103                 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
    104                     if (aPinnings[i].aPages[j].Phys >= _4G)
    105                     {
    106                         RTPrintf("%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
    107                         c4GPluss++;
    108                     }
    109                 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
    110             }
    111             else
    112             {
    113                 RTPrintf("SUPPageLock -> rc=%d\n", rc);
    114                 rcRet++;
    115                 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
    116                 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
    117                 break;
    118             }
    119         }
    120 
    121         for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
    122         {
    123             if (aPinnings[i].pvAligned)
    124             {
    125                 rc = supR3PageUnlock(aPinnings[i].pvAligned);
    126                 if (rc)
    127                 {
    128                     RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
    129                     rcRet++;
    130                 }
    131                 memset(aPinnings[i].pv, 0xaf, 0x10000);
    132             }
    133         }
    134 
    135         for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
    136         {
    137             if (aPinnings[i].pv)
    138             {
    139                 memset(aPinnings[i].pv, 0xcc, 0x10000);
    140                 SUPR3PageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
    141                 aPinnings[i].pv = NULL;
    142             }
    143         }
    144 
    145 
    146         /*
    147          * Allocate a bit of contiguous memory.
    148          */
    149         pv = SUPR3ContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
    150         rcRet += pv == NULL || HCPhys == 0;
     144        }
     145    }
     146
     147    RTTestISubDone();
     148
     149
     150    /*
     151     * Allocate a bit of contiguous memory.
     152     */
     153    RTTestISub("Contiguous memory");
     154
     155    size_t cbAlloc = RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT;
     156
     157    pv = SUPR3ContAlloc(cbAlloc, NULL, &HCPhys);
     158    if (pv && HCPhys)
     159    {
     160        RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
     161        void *pv0 = pv;
     162        memset(pv0, 0xaf, 15003);
     163        pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
    151164        if (pv && HCPhys)
    152165        {
    153             RTPrintf("SUPR3ContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
    154             void *pv0 = pv;
    155             memset(pv0, 0xaf, 15003);
    156             pv = SUPR3ContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, NULL, &HCPhys);
    157             rcRet += pv == NULL || HCPhys == 0;
    158             if (pv && HCPhys)
    159             {
    160                 RTPrintf("SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
    161                 memset(pv, 0xbf, 12999);
    162                 rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
    163                 rcRet += rc != 0;
    164                 if (rc)
    165                     RTPrintf("SUPR3ContFree failed! rc=%d\n", rc);
    166             }
    167             else
    168                 RTPrintf("SUPR3ContAlloc (2nd) failed!\n");
    169             memset(pv0, 0xaf, 15003);
    170             /* pv0 is intentionally not freed! */
     166            RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
     167            memset(pv, 0xbf, 12999);
     168            rc = SUPR3ContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
     169            if (RT_FAILURE(rc))
     170                RTTestIPrintf(RTTESTLVL_DEBUG, "SUPR3ContFree failed! rc=%Rrc\n", rc);
    171171        }
    172172        else
    173             RTPrintf("SUPR3ContAlloc failed!\n");
    174 
    175         /*
    176          * Allocate a big chunk of virtual memory and then lock it.
    177          */
    178         #define BIG_SIZE    72*1024*1024
    179         #define BIG_SIZEPP  (BIG_SIZE + PAGE_SIZE)
    180         pv = NULL;
    181         SUPR3PageAlloc(BIG_SIZEPP >> PAGE_SHIFT, 0, &pv);
    182         if (pv)
    183         {
    184             static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
    185             void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
    186             rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
    187             if (!rc)
    188             {
    189                 /* dump */
    190                 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
    191                 memset(pv, 0x42, BIG_SIZEPP);
    192                 #if 0
    193                 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
    194                     RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
    195                 #endif
    196 
    197                 /* unlock */
    198                 rc = supR3PageUnlock(pvAligned);
    199                 if (rc)
    200                 {
    201                     RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
    202                     rcRet++;
    203                 }
    204                 memset(pv, 0xcc, BIG_SIZEPP);
    205             }
    206             else
    207             {
    208                 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
    209                 rcRet++;
    210             }
    211             SUPR3PageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
    212         }
    213 
    214         rc = SUPR3Term(false /*fForced*/);
    215         RTPrintf("SUPR3Term -> rc=%d\n", rc);
    216         rcRet += rc != 0;
    217     }
    218 
    219     return rcRet;
     173            RTTestIFailed("SUPR3ContAlloc (2nd) failed!\n");
     174        memset(pv0, 0xaf, 15003);
     175        /* pv0 is intentionally not freed! */
     176    }
     177    else
     178        RTTestIFailed("SUPR3ContAlloc(%zu bytes) failed!\n", cbAlloc);
     179
     180    RTTestISubDone();
     181
     182    /*
     183     * Allocate a big chunk of virtual memory and then lock it.
     184     */
     185    RTTestISub("Big chunk");
     186
     187    #define BIG_SIZE    72*1024*1024
     188    #define BIG_SIZEPP  (BIG_SIZE + PAGE_SIZE)
     189    pv      = NULL;
     190    cbAlloc = BIG_SIZEPP >> PAGE_SHIFT;
     191    rc = SUPR3PageAlloc(cbAlloc, 0, &pv);
     192    if (RT_SUCCESS(rc))
     193    {
     194        AssertPtr(pv);
     195
     196        static SUPPAGE s_aPages[BIG_SIZE >> PAGE_SHIFT];
     197        void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
     198        rc = supR3PageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &s_aPages[0]);
     199        if (RT_SUCCESS(rc))
     200        {
     201            /* dump */
     202            RTTestIPrintf(RTTESTLVL_DEBUG, "SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
     203            memset(pv, 0x42, BIG_SIZEPP);
     204            #if 0
     205            for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
     206                RTTestIPrintf(RTTESTLVL_DEBUG, "%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)s_aPages[j].pvPhys);
     207            #endif
     208
     209            /* unlock */
     210            rc = supR3PageUnlock(pvAligned);
     211            if (RT_FAILURE(rc))
     212                RTTestIFailed("SUPPageUnlock(%p) failed with rc=%Rrc\n", pvAligned, rc);
     213            memset(pv, 0xcc, BIG_SIZEPP);
     214        }
     215        else
     216            RTTestIFailed("SUPPageLock(%p) failed with rc=%Rrc\n", pvAligned, rc);
     217        SUPR3PageFree(pv, cbAlloc);
     218    }
     219    else
     220        RTTestIFailed("SUPPageAlloc(%zu bytes) failed with rc=%Rrc\n", cbAlloc, rc);
     221
     222    RTTestISubDone();
     223
     224     /*
     225     * Summary.
     226     */
     227    return RTTestSummaryAndDestroy(hTest);
    220228}
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