tstPageFusion.cpp revision c58f1213e628a545081c70e26c6b67a841cff880
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * VBoxService - Guest page sharing testcase
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2011 Oracle Corporation
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * available from http://www.virtualbox.org. This file is free software;
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * General Public License (GPL) as published by the Free Software
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2f139cbf73f9adba01382892f33558ad7bcb40cbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync/*******************************************************************************
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync* Header Files *
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync*******************************************************************************/
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync/*******************************************************************************
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync* Global Variables *
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync*******************************************************************************/
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync#include <process.h> /* Needed for file version information. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsynctypedef NTSTATUS (WINAPI *PFNZWQUERYSYSTEMINFORMATION)(ULONG, PVOID, ULONG, PULONG);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic PFNZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync/* Page counters. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic unsigned cNotPresentPages = 0;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic unsigned cWritablePages = 0;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic unsigned cSharedPages = 0;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic unsigned cPrivatePages = 0;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * Registers a new module with the VMM
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * @param pModule Module ptr
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncvoid VBoxServicePageSharingCheckModule(MODULEENTRY32 *pModule)
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("Check module %s base %p size %x\n", pModule->szModule, pBaseAddress, dwModuleSize);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync int rc = VbglR3PageIsShared((uint32_t)pLastBaseAddress, &fShared, &uPageFlags);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync int rc = VbglR3PageIsShared((RTGCPTR)pLastBaseAddress, &fShared, &uPageFlags);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("VbglR3PageIsShared %p failed with %d\n", pLastBaseAddress, rc);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("%s RW 0x%p - ", pModule->szModule, pBaseAddress);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("%s SHARED 0x%p - ", pModule->szModule, pBaseAddress);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("%s PRIV 0x%p - ", pModule->szModule, pBaseAddress);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("%s NP 0x%p - ", pModule->szModule, pBaseAddress);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * Inspect all loaded modules for the specified process
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * @param dwProcessId Process id
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncvoid VBoxServicePageSharingInspectModules(DWORD dwProcessId)
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("VBoxServicePageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /** todo when changing this make sure VBoxService.exe is excluded! */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync continue; /* ignore executables for now. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * Inspect all running processes for executables and dlls that might be worth sharing
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * with other VMs.
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync VBoxServicePageSharingInspectModules(GetCurrentProcessId());
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("cNotPresentPages = %d\n", cNotPresentPages);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* Check all loaded kernel modules. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync NTSTATUS ret = ZwQuerySystemInformation(SystemModuleInformation, (PVOID)&cbBuffer, 0, &cbBuffer);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("ZwQuerySystemInformation returned length 0\n");
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync ret = ZwQuerySystemInformation(SystemModuleInformation, pBuffer, cbBuffer, &cbBuffer);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("ZwQuerySystemInformation returned %x (1)\n", ret);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync for (unsigned i = 0; i < pSystemModules->NumberOfModules; i++)
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* User-mode modules seem to have no flags set; skip them as we detected them above. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* New module; register it. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync strcpy(ModuleInfo.szModule, &pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName]);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* skip \Systemroot\system32 */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync char *lpPath = strchr(&pSystemModules->Modules[i].FullPathName[1], '\\');
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync ModuleInfo.modBaseAddr = (BYTE *)pSystemModules->Modules[i].ImageBase;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync ModuleInfo.modBaseSize = pSystemModules->Modules[i].ImageSize;
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync printf("cNotPresentPages = %d\n", cNotPresentPages);
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* @todo other platforms */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync/** @copydoc VBOXSERVICE::pfnInit */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic DECLCALLBACK(int) VBoxServicePageSharingInit(void)
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync ZwQuerySystemInformation = (PFNZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* @todo report system name and version */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync /* Never fail here. */
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsyncstatic DECLCALLBACK(void) VBoxServicePageSharingTerm(void)
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * Init globals and such.
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * Connect to the kernel part before daemonizing so we can fail
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * and complain if there is some kind of problem. We need to initialize
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * the guest lib *before* we do the pre-init just in case one of services
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync * needs do to some initial stuff with it.