VirtualBox

Changeset 6796 in vbox for trunk/src/VBox/VMM/PDMLdr.cpp


Ignore:
Timestamp:
Feb 4, 2008 6:19:58 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
27892
Message:

Fixed init problems wrt. VM ownership by implementing the UVM structure (U = user mode) and moving problematic ring-3 stuff over there (emt+reqs, r3heap, stam, loader[VMMR0.r0]). Big change, but it works fine here... :-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/PDMLdr.cpp

    r5999 r6796  
    2727#include <VBox/vmm.h>
    2828#include <VBox/vm.h>
     29#include <VBox/uvm.h>
    2930#include <VBox/sup.h>
    3031#include <VBox/param.h>
     
    5960*   Internal Functions                                                         *
    6061*******************************************************************************/
    61 static DECLCALLBACK(int) pdmr3GetImportGC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
    62 static int      pdmR3LoadR0(PVM pVM, const char *pszFilename, const char *pszName);
     62static DECLCALLBACK(int) pdmR3GetImportGC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
     63static int      pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName);
    6364static char *   pdmR3FileGC(const char *pszFile);
    6465static char *   pdmR3FileR0(const char *pszFile);
     
    6970
    7071/**
    71  * Loads the VMMR0.r0 module before the VM is created.
    72  *
    73  * The opqaue VMMR0 module pointer is passed on to PDMR3Init later in
    74  * the init process or PDMR3LdrUnloadVMMR0 in case of some init failure before PDMR3Init.
     72 * Loads the VMMR0.r0 module early in the init process.
    7573 *
    7674 * @returns VBox status code.
    77  * @param   ppvOpaque       Where to return the opaque VMMR0.r0 module handle one success.
    78  *
    79  * @remarks Yes, this is a kind of hacky and should go away. See @todo in VMR3Create.
    80  */
    81 PDMR3DECL(int) PDMR3LdrLoadVMMR0(void **ppvOpaque)
    82 {
    83     *ppvOpaque = NULL;
    84 
    85     /*
    86      * Resolve the filename and allocate the module list node.
    87      */
    88     char *pszFilename = pdmR3FileR0(VMMR0_MAIN_MODULE_NAME);
    89     PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
    90     if (!pModule)
    91     {
    92         RTMemTmpFree(pszFilename);
    93         return VERR_NO_MEMORY;
    94     }
    95     strcpy(pModule->szName, VMMR0_MAIN_MODULE_NAME);
    96     pModule->eType = PDMMOD_TYPE_R0;
    97     strcpy(pModule->szFilename, pszFilename);
    98     RTMemTmpFree(pszFilename);
    99 
    100     /*
    101      * Ask the support library to load it.
    102      */
    103     void *pvImageBase;
    104     int rc = SUPLoadModule(pModule->szFilename, pModule->szName, &pvImageBase);
    105     if (RT_SUCCESS(rc))
    106     {
    107         pModule->hLdrMod = NIL_RTLDRMOD;
    108         pModule->ImageBase = (uintptr_t)pvImageBase;
    109         *ppvOpaque = pModule;
    110 
    111         Log(("PDMR3LdrLoadVMMR0: Loaded %s at %VGvx (%s)\n", pModule->szName, (RTGCPTR)pModule->ImageBase, pModule->szFilename));
    112         return VINF_SUCCESS;
    113     }
    114 
    115     LogRel(("PDMR3LdrLoadVMMR0: rc=%Vrc szName=%s szFilename=%s\n", rc, pModule->szName, pModule->szFilename));
    116     RTMemFree(pModule);
    117     return rc;
    118 }
    119 
    120 
    121 /**
    122  * Register the VMMR0.r0 module with the created VM or unload it if
    123  * we failed to create the VM (pVM == NULL).
    124  *
    125  * @param   pVM         The VM pointer. NULL if we failed to create the VM and
    126  *                      the module should be unloaded and freed.
    127  * @param   pvOpaque    The value returned by PDMR3LDrLoadVMMR0().
    128  *
    129  * @remarks Yes, this is a kind of hacky and should go away. See @todo in VMR3Create.
    130  */
    131 PDMR3DECL(void) PDMR3LdrLoadVMMR0Part2(PVM pVM, void *pvOpaque)
    132 {
    133     PPDMMOD pModule = (PPDMMOD)pvOpaque;
    134     AssertPtrReturnVoid(pModule);
    135 
    136     if (pVM)
    137     {
    138         /*
    139          * Register the R0 module loaded by PDMR3LdrLoadVMMR0
    140          */
    141         Assert(!pVM->pdm.s.pModules);
    142         pModule->pNext = pVM->pdm.s.pModules;
    143         pVM->pdm.s.pModules = pModule;
    144     }
    145     else
    146     {
    147         /*
    148          * Failed, unload the module.
    149          */
    150         int rc2 = SUPFreeModule((void *)(uintptr_t)pModule->ImageBase);
    151         AssertRC(rc2);
    152         pModule->ImageBase = 0;
    153         RTMemFree(pvOpaque);
    154     }
     75 * @param   pUVM            Pointer to the user mode VM structure.
     76 */
     77PDMR3DECL(int) PDMR3LdrLoadVMMR0U(PUVM pUVM)
     78{
     79    return pdmR3LoadR0U(pUVM, NULL, VMMR0_MAIN_MODULE_NAME);
    15580}
    15681
     
    16388 *
    16489 * @returns VBox stutus code.
    165  * @param   pVM         VM handle.
     90 * @param   pUVM        Pointer to the user mode VM structure.
    16691 * @param   pvVMMR0Mod  The opqaue returned by PDMR3LdrLoadVMMR0.
    16792 */
    168 int pdmR3LdrInit(PVM pVM)
     93int pdmR3LdrInitU(PUVM pUVM)
    16994{
    17095#ifdef PDMLDR_FAKE_MODE
     
    176101     * Load the mandatory GC module, the VMMR0.r0 is loaded before VM creation.
    177102     */
    178     return PDMR3LoadGC(pVM, NULL, VMMGC_MAIN_MODULE_NAME);
     103    return PDMR3LoadGC(pUVM->pVM, NULL, VMMGC_MAIN_MODULE_NAME);
    179104#endif
    180105}
     
    187112 *
    188113 * @param   pVM         The VM handle.
    189  */
    190 void pdmR3LdrTerm(PVM pVM)
     114 *
     115 * @remarks This is normally called twice during termination.
     116 */
     117void pdmR3LdrTermU(PUVM pUVM)
    191118{
    192119    /*
    193120     * Free the modules.
    194121     */
    195     PPDMMOD pModule = pVM->pdm.s.pModules;
     122    PPDMMOD pModule = pUVM->pdm.s.pModules;
     123    pUVM->pdm.s.pModules = NULL;
    196124    while (pModule)
    197125    {
     
    240168 * process so that components can resolve GC symbols during relocation.
    241169 *
    242  * @param   pVM         VM handle.
     170 * @param   pUVM        Pointer to the user mode VM structure.
    243171 * @param   offDelta    Relocation delta relative to old location.
    244172 */
    245 PDMR3DECL(void) PDMR3LdrRelocate(PVM pVM, RTGCINTPTR offDelta)
     173PDMR3DECL(void) PDMR3LdrRelocateU(PUVM pUVM, RTGCINTPTR offDelta)
    246174{
    247175    LogFlow(("PDMR3LdrRelocate: offDelta=%VGv\n", offDelta));
     
    250178     * GC Modules.
    251179     */
    252     if (pVM->pdm.s.pModules)
     180    if (pUVM->pdm.s.pModules)
    253181    {
    254182        /*
     
    260188        /* pass 1 */
    261189        PPDMMOD pCur;
    262         for (pCur = pVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
     190        for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
    263191        {
    264192            if (pCur->eType == PDMMOD_TYPE_GC)
    265193            {
    266194                pCur->OldImageBase = pCur->ImageBase;
    267                 pCur->ImageBase = MMHyperHC2GC(pVM, pCur->pvBits);
     195                pCur->ImageBase = MMHyperHC2GC(pUVM->pVM, pCur->pvBits);
    268196            }
    269197        }
    270198
    271199        /* pass 2 */
    272         for (pCur = pVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
     200        for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
    273201        {
    274202            if (pCur->eType == PDMMOD_TYPE_GC)
    275203            {
    276204                PDMGETIMPORTARGS Args;
    277                 Args.pVM = pVM;
     205                Args.pVM = pUVM->pVM;
    278206                Args.pModule = pCur;
    279207                int rc = RTLdrRelocate(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pCur->OldImageBase,
    280                                        pdmr3GetImportGC, &Args);
     208                                       pdmR3GetImportGC, &Args);
    281209                AssertFatalMsgRC(rc, ("RTLdrRelocate failed, rc=%d\n", rc));
    282                 DBGFR3ModuleRelocate(pVM, pCur->OldImageBase, pCur->ImageBase, RTLdrSize(pCur->hLdrMod),
     210                DBGFR3ModuleRelocate(pUVM->pVM, pCur->OldImageBase, pCur->ImageBase, RTLdrSize(pCur->hLdrMod),
    283211                                     pCur->szFilename, pCur->szName);
    284212            }
     
    301229 *
    302230 * @returns VBox status code.
    303  * @param   pVM             The VM to load it into.
     231 * @param   pUVM            Pointer to the user mode VM structure.
    304232 * @param   pszFilename     Filename of the module binary.
    305233 * @param   pszName         Module name. Case sensitive and the length is limited!
    306234 */
    307 int pdmR3LoadR3(PVM pVM, const char *pszFilename, const char *pszName)
     235int pdmR3LoadR3U(PUVM pUVM, const char *pszFilename, const char *pszName)
    308236{
    309237    /*
    310238     * Validate input.
    311239     */
    312     AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
     240    AssertMsg(pUVM->pVM->pdm.s.offVM, ("bad init order!\n"));
    313241    Assert(pszFilename);
    314242    size_t cchFilename = strlen(pszFilename);
     
    325253     * Try lookup the name and see if the module exists.
    326254     */
    327     for (pCur = pVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
     255    for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
    328256    {
    329257        if (!strcmp(pCur->szName, pszName))
     
    353281    if (VBOX_SUCCESS(rc))
    354282    {
    355         pModule->pNext = pVM->pdm.s.pModules;
    356         pVM->pdm.s.pModules = pModule;
     283        pModule->pNext = pUVM->pdm.s.pModules;
     284        pUVM->pdm.s.pModules = pModule;
    357285        return rc;
    358286    }
     
    360288    /* Something went wrong, most likely module not found. Don't consider other unlikely errors */
    361289    RTMemFree(pModule);
    362     return VMSetError(pVM, rc, RT_SRC_POS, N_("Unable to load R3 module %s"), pszFilename);
     290    return VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Unable to load R3 module %s"), pszFilename);
    363291}
    364292
     
    375303 * @param   pvUser          User argument.
    376304 */
    377 static DECLCALLBACK(int) pdmr3GetImportGC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
     305static DECLCALLBACK(int) pdmR3GetImportGC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
    378306{
    379307    PVM         pVM = ((PPDMGETIMPORTARGS)pvUser)->pVM;
     
    427355     * Search for module.
    428356     */
    429     PPDMMOD  pCur = pVM->pdm.s.pModules;
     357    PPDMMOD  pCur = pVM->pUVM->pdm.s.pModules;
    430358    while (pCur)
    431359    {
     
    478406     */
    479407    AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
    480     PPDMMOD  pCur = pVM->pdm.s.pModules;
     408    PPDMMOD  pCur = pVM->pUVM->pdm.s.pModules;
    481409    while (pCur)
    482410    {
     
    541469                Args.pVM = pVM;
    542470                Args.pModule = pModule;
    543                 rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmr3GetImportGC, &Args);
     471                rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmR3GetImportGC, &Args);
    544472                if (VBOX_SUCCESS(rc))
    545473                {
     
    547475                     * Insert the module.
    548476                     */
    549                     if (pVM->pdm.s.pModules)
     477                    PUVM pUVM = pVM->pUVM;
     478                    if (pUVM->pdm.s.pModules)
    550479                    {
    551480                        /* we don't expect this list to be very long, so rather save the tail pointer. */
    552                         PPDMMOD pCur = pVM->pdm.s.pModules;
     481                        PPDMMOD pCur = pUVM->pdm.s.pModules;
    553482                        while (pCur->pNext)
    554483                            pCur = pCur->pNext;
     
    556485                    }
    557486                    else
    558                         pVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
     487                        pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
    559488                    Log(("PDM: GC Module at %VGvx %s (%s)\n", (RTGCPTR)pModule->ImageBase, pszName, pszFilename));
    560489                    RTMemTmpFree(pszFile);
     
    587516 *
    588517 * @returns VBox status code.
    589  * @param   pVM             The VM to load it into.
     518 * @param   pUVM            Pointer to the user mode VM structure.
    590519 * @param   pszFilename     Filename of the module binary.
    591520 * @param   pszName         Module name. Case sensitive and the length is limited!
    592521 */
    593 static int pdmR3LoadR0(PVM pVM, const char *pszFilename, const char *pszName)
     522static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName)
    594523{
    595524    /*
    596525     * Validate input.
    597526     */
    598     AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
    599     PPDMMOD  pCur = pVM->pdm.s.pModules;
     527    PPDMMOD  pCur = pUVM->pdm.s.pModules;
    600528    while (pCur)
    601529    {
     
    608536        pCur = pCur->pNext;
    609537    }
    610     AssertReturn(strcmp(pszName, VMMR0_MAIN_MODULE_NAME), VERR_INTERNAL_ERROR);
    611538
    612539    /*
     
    645572         * Insert the module.
    646573         */
    647         if (pVM->pdm.s.pModules)
     574        if (pUVM->pdm.s.pModules)
    648575        {
    649576            /* we don't expect this list to be very long, so rather save the tail pointer. */
    650             PPDMMOD pCur = pVM->pdm.s.pModules;
     577            PPDMMOD pCur = pUVM->pdm.s.pModules;
    651578            while (pCur->pNext)
    652579                pCur = pCur->pNext;
     
    654581        }
    655582        else
    656             pVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
     583            pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
    657584        Log(("PDM: GC Module at %VGvx %s (%s)\n", (RTGCPTR)pModule->ImageBase, pszName, pszFilename));
    658585        RTMemTmpFree(pszFile);
     
    662589    RTMemFree(pModule);
    663590    RTMemTmpFree(pszFile);
    664     LogRel(("pdmR3LoadR0: pszName=\"%s\" rc=%Vrc\n", pszName, rc));
     591    LogRel(("pdmR3LoadR0U: pszName=\"%s\" rc=%Vrc\n", pszName, rc));
    665592
    666593    /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
    667     if (VBOX_FAILURE(rc))
    668         return VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load R0 module %s"), pszFilename);
     594    if (VBOX_FAILURE(rc) && pUVM->pVM) /** @todo VMR3SetErrorU. */
     595        return VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Cannot load R0 module %s"), pszFilename);
    669596    return rc;
    670597}
     
    692619     * Find the module.
    693620     */
    694     for (PPDMMOD pModule = pVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
     621    for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
    695622    {
    696623        if (    pModule->eType == PDMMOD_TYPE_R3
     
    747674     * Find the module.
    748675     */
    749     for (PPDMMOD pModule = pVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
     676    for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
    750677    {
    751678        if (    pModule->eType == PDMMOD_TYPE_R0
     
    794721        AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
    795722        PPDMMOD pModule;
    796         for (pModule = pVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
     723        for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
    797724            if (    pModule->eType == PDMMOD_TYPE_R0
    798725                &&  !strcmp(pModule->szName, pszModule))
     
    800727        if (!pModule)
    801728        {
    802             int rc = pdmR3LoadR0(pVM, NULL, pszModule);
     729            int rc = pdmR3LoadR0U(pVM->pUVM, NULL, pszModule);
    803730            AssertMsgRCReturn(rc, ("pszModule=%s rc=%Vrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
    804731        }
     
    836763     * Find the module.
    837764     */
    838     for (PPDMMOD pModule = pVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
     765    for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
    839766    {
    840767        if (    pModule->eType == PDMMOD_TYPE_GC
     
    891818        AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
    892819        PPDMMOD pModule;
    893         for (pModule = pVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
     820        for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
    894821            if (    pModule->eType == PDMMOD_TYPE_GC
    895822                &&  !strcmp(pModule->szName, pszModule))
     
    10781005    int     rc = VERR_MODULE_NOT_FOUND;
    10791006    PPDMMOD pCur;
    1080     for (pCur = pVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
     1007    for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
    10811008    {
    10821009        /* Skip anything which isn't in GC. */
     
    11951122{
    11961123    PPDMMOD pCur;
    1197     for (pCur = pVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
     1124    for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
    11981125    {
    11991126        int rc = pfnCallback(pVM,
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