VBoxServicePageSharing.cpp revision 462e60a19d02a99b2b1a5c08dff74bb0808d707c
/* $Id$ */
/** @file
* VBoxService - Guest page sharing.
*/
/*
* Copyright (C) 2006-2012 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
{
static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser);
static uint64_t g_idSession = 0;
/**
* 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, "VBoxServicePageSharingRegisterModule: VbglR3RegisterSharedModule %s %s base=%p size=%x cregions=%d\n", pModule->Info.szModule, pModule->szFileVersion, pModule->Info.modBaseAddr, pModule->Info.modBaseSize, idxRegion);
int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (uintptr_t)pModule->Info.modBaseAddr,
if (RT_FAILURE(rc))
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VbglR3RegisterSharedModule failed with %Rrc\n", 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)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectGuest: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
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++)
{
VBoxServiceVerbose(4, "\n\n KERNEL MODULE NAME: %s", pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName] );
/* User-mode modules seem to have no flags set; skip them as we detected them above. */
continue;
/* 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)
{
/* Seen just file names in XP; try to locate the file in the system32 and system32\drivers directories. */
if (RTFileExists(szFullFilePath) == false)
{
if (RTFileExists(szFullFilePath) == false)
{
VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
continue;
}
}
}
else
{
if (!lpPath)
{
VBoxServiceVerbose(1, "Unexpected kernel module name %s (2)\n", pSystemModules->Modules[i].FullPathName);
continue;
}
}
}
}
}
if (pBuffer)
}
/* Delete leftover modules in the old tree. */
/* Check all registered modules. */
/* Activate new module tree. */
}
/**
* RTAvlPVDestroy callback.
*/
static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser)
{
bool *pfUnregister = (bool *)pvUser;
VBoxServiceVerbose(3, "VBoxServicePageSharingEmptyTreeCallback %s %s\n", pModule->Info.szModule, pModule->szFileVersion);
/* Dereference module in the hypervisor. */
if ( !pfUnregister
|| *pfUnregister)
{
}
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 -1;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServicePageSharingInit(void)
{
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
if (hNtdll)
ZwQuerySystemInformation = (PFNZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
if (RT_FAILURE(rc))
{
if (rc == VERR_IO_GEN_FAILURE)
VBoxServiceVerbose(0, "PageSharing: Page sharing support is not available by the host\n");
else
}
#endif
return rc;
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
/*
* Tell the control thread that it can continue
* spawning services.
*/
/*
* Now enter the loop retrieving runtime data continuously.
*/
for (;;)
{
if (fEnabled)
/*
* 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;
}
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
if (idNewSession != g_idSession)
{
bool fUnregister = false;
/* The VM was restored, so reregister all modules the next time. */
}
#endif
}
return 0;
}
#ifdef RT_OS_WINDOWS
/**
* This gets control when VBoxService is launched with -pagefusionfork by
* VBoxServicePageSharingWorkerProcess().
*
* @returns RTEXITCODE_SUCCESS.
*
* @remarks It won't normally return since the parent drops the shutdown hint
* via RTProcTerminate().
*/
{
bool fShutdown = false;
return RTEXITCODE_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
int rc;
/*
* Tell the control thread that it can continue
* spawning services.
*/
/*
* Now enter the loop retrieving runtime data continuously.
*/
for (;;)
{
/*
* Start a 2nd VBoxService process to deal with page fusion as we do
* not wish to dummy load dlls into this process. (First load with
* DONT_RESOLVE_DLL_REFERENCES, 2nd normal -> dll init routines not called!)
*/
if ( fEnabled
&& hProcess == NIL_RTPROCESS)
{
char szExeName[256];
if (pszExeName)
{
char const *papszArgs[3];
papszArgs[0] = pszExeName;
if (RT_FAILURE(rc))
VBoxServiceError("VBoxServicePageSharingWorkerProcess: RTProcCreate %s failed; rc=%Rrc\n", pszExeName, rc);
}
}
/*
* 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;
}
}
if (hProcess != NIL_RTPROCESS)
return 0;
}
#endif /* RT_OS_WINDOWS */
/** @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 */
#ifdef RT_OS_WINDOWS
#else
#endif
};