VBoxServicePageSharing.cpp revision 760446f710619a9daa6cedc7f0601f49e4ea3442
/* $Id$ */
/** @file
* VBoxService - Guest page sharing.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The semaphore we're blocking on. */
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
#include <tlhelp32.h>
#include <psapi.h>
#include <winternl.h>
typedef struct
{
char szFileVersion[16];
#define SystemModuleInformation 11
typedef struct _RTL_PROCESS_MODULE_INFORMATION
{
typedef struct _RTL_PROCESS_MODULES
{
/**
* Registers a new module with the VMM
* @param pModule Module ptr
*/
{
if (!cbVersionSize)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfoSize failed with %d\n", GetLastError());
return;
}
if (!pVersionInfo)
return;
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfo failed with %d\n", GetLastError());
goto end;
}
/* Fetch default code page. */
struct LANGANDCODEPAGE {
} *lpTranslate;
BOOL ret = VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"), (LPVOID *)&lpTranslate, &cbTranslate);
if ( !ret
|| cbTranslate < 4)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VerQueryValue failed with %d (cb=%d)\n", GetLastError(), cbTranslate);
goto end;
}
unsigned i;
char *lpszFileVersion;
for(i = 0; i < cTranslationBlocks; i++)
{
/* Fetch file version string. */
char szFileVersionLocation[256];
sprintf(szFileVersionLocation, TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
ret = VerQueryValue(pVersionInfo, szFileVersionLocation, (LPVOID *)&lpszFileVersion, &cbFileVersion);
if (ret)
break;
}
if (i == cTranslationBlocks)
{
goto end;
}
unsigned idxRegion = 0;
if (fValidateMemory)
{
do
{
if (!ret)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VirtualQueryEx failed with %d\n", GetLastError());
break;
}
{
{
case PAGE_EXECUTE:
case PAGE_EXECUTE_READ:
case PAGE_READONLY:
{
/* Skip the first region as it only contains the image file header. */
{
/* Touch all pages. */
{
/* Try to trick the optimizer to leave the page touching code in place. */
}
}
#ifdef RT_ARCH_X86
#else
#endif
idxRegion++;
break;
}
default:
break; /* ignore */
}
}
{
}
else
{
dwModuleSize = 0;
break;
}
break; /* out of room */
}
while (dwModuleSize);
}
else
{
/* We can't probe kernel memory ranges, so pretend it's one big region. */
#ifdef RT_ARCH_X86
#else
#endif
idxRegion++;
}
VBoxServiceVerbose(3, "VbglR3RegisterSharedModule %s %s base=%p size=%x cregions=%d\n", pModule->Info.szModule, pModule->szFileVersion, pModule->Info.modBaseAddr, pModule->Info.modBaseSize, idxRegion);
#ifdef RT_ARCH_X86
int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR32)pModule->Info.modBaseAddr,
#else
int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr,
#endif
// AssertRC(rc);
if (RT_FAILURE(rc))
end:
return;
}
/**
* Inspect all loaded modules for the specified process
* @param dwProcessId Process id
*/
{
/* Get a list of all the modules in this process. */
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: OpenProcess %x failed with %d\n", dwProcessId, GetLastError());
return;
}
if (hSnapshot == INVALID_HANDLE_VALUE)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
return;
}
do
{
/** todo when changing this make sure VBoxService.exe is excluded! */
if ( pszDot
continue; /* ignore executables for now. */
/* Found it before? */
if (!pRec)
{
if (!pRec)
{
/* New module; register it. */
if (!pModule)
break;
}
}
}
}
/**
* Inspect all running processes for executables and dlls that might be worth sharing
* with other VMs.
*
*/
{
if (hSnapshot == INVALID_HANDLE_VALUE)
{
return;
}
/* Check loaded modules for all running processes. */
do
{
/* Skip our own process. */
}
/* Check all loaded kernel modules. */
{
if (!cbBuffer)
{
goto skipkernelmodules;
}
if (!pBuffer)
goto skipkernelmodules;
if (ret != STATUS_SUCCESS)
{
goto skipkernelmodules;
}
for (unsigned i = 0; i < pSystemModules->NumberOfModules; i++)
{
/* User-mode modules seem to have no flags set; skip them as we detected them above. */
continue;
if ( pszDot
continue; /* ignore executables for now. */
/* Found it before? */
if (!pRec)
{
if (!pRec)
{
/* New module; register it. */
char szFullFilePath[512];
if (!pModule)
break;
strcpy(pModule->Info.szModule, &pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName]);
/* skip \Systemroot\system32 */
if (!lpPath)
{
VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
break;
}
if (!lpPath)
{
VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
break;
}
}
}
}
if (pBuffer)
}
/* Delete leftover modules in the old tree. */
/* Check all registered modules. */
/* Activate new module tree. */
}
/**
* RTAvlPVDestroy callback.
*/
{
VBoxServiceVerbose(3, "VBoxServicePageSharingEmptyTreeCallback %s %s\n", pModule->Info.szModule, pModule->szFileVersion);
/* Defererence module in the hypervisor. */
int rc = VbglR3UnregisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr, pModule->Info.modBaseSize);
return 0;
}
{
/* not implemented */
}
#else
{
/* @todo other platforms */
}
#endif
/** @copydoc VBOXSERVICE::pfnPreInit */
static DECLCALLBACK(int) VBoxServicePageSharingPreInit(void)
{
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnOption */
static DECLCALLBACK(int) VBoxServicePageSharingOption(const char **ppszShort, int argc, char **argv, int *pi)
{
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServicePageSharingInit(void)
{
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
if (hNtdll)
ZwQuerySystemInformation = (PFNZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
#endif
/* @todo report system name and version */
/* Never fail here. */
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
/*
* Tell the control thread that it can continue
* spawning services.
*/
/*
* Block here first for a minute as using DONT_RESOLVE_DLL_REFERENCES is kind of risky; other code that uses LoadLibrary on a dll loaded like this
* before will end up crashing the process as the dll's init routine was never called.
*
* We have to use this feature as we can't simply execute all init code in our service process.
*
*/
if (*pfShutdown)
goto end;
{
goto end;
}
/*
* Now enter the loop retrieving runtime data continuously.
*/
for (;;)
{
if (VbglR3PageSharingIsEnabled())
/*
* Block for a minute.
*
* The event semaphore takes care of ignoring interruptions and it
* allows us to implement service wakeup later.
*/
if (*pfShutdown)
break;
if (*pfShutdown)
break;
{
break;
}
}
end:
return 0;
}
/** @copydoc VBOXSERVICE::pfnTerm */
static DECLCALLBACK(void) VBoxServicePageSharingTerm(void)
{
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
if (hNtdll)
#endif
return;
}
/** @copydoc VBOXSERVICE::pfnStop */
static DECLCALLBACK(void) VBoxServicePageSharingStop(void)
{
}
/**
* The 'pagesharing' service description.
*/
{
/* pszName. */
"pagesharing",
/* pszDescription. */
"Page Sharing",
/* pszUsage. */
NULL,
/* pszOptions. */
NULL,
/* methods */
};