1 | /* $Id: VBoxServicePageSharing.cpp 29958 2010-06-01 15:24:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest page sharing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/avl.h>
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/mem.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include <iprt/system.h>
|
---|
30 | #include <iprt/thread.h>
|
---|
31 | #include <iprt/time.h>
|
---|
32 | #include <VBox/VBoxGuestLib.h>
|
---|
33 | #include "VBoxServiceInternal.h"
|
---|
34 | #include "VBoxServiceUtils.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Global Variables *
|
---|
39 | *******************************************************************************/
|
---|
40 |
|
---|
41 | /** The semaphore we're blocking on. */
|
---|
42 | static RTSEMEVENTMULTI g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
|
---|
43 |
|
---|
44 | #if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
|
---|
45 | #include <tlhelp32.h>
|
---|
46 | #include <psapi.h>
|
---|
47 | #include <winternl.h>
|
---|
48 |
|
---|
49 | typedef struct
|
---|
50 | {
|
---|
51 | AVLPVNODECORE Core;
|
---|
52 | HMODULE hModule;
|
---|
53 | char szFileVersion[16];
|
---|
54 | MODULEENTRY32 Info;
|
---|
55 | } KNOWN_MODULE, *PKNOWN_MODULE;
|
---|
56 |
|
---|
57 | #define SystemModuleInformation 11
|
---|
58 |
|
---|
59 | typedef struct _RTL_PROCESS_MODULE_INFORMATION
|
---|
60 | {
|
---|
61 | ULONG Section;
|
---|
62 | PVOID MappedBase;
|
---|
63 | PVOID ImageBase;
|
---|
64 | ULONG ImageSize;
|
---|
65 | ULONG Flags;
|
---|
66 | USHORT LoadOrderIndex;
|
---|
67 | USHORT InitOrderIndex;
|
---|
68 | USHORT LoadCount;
|
---|
69 | USHORT OffsetToFileName;
|
---|
70 | CHAR FullPathName[256];
|
---|
71 | } RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
|
---|
72 |
|
---|
73 | typedef struct _RTL_PROCESS_MODULES
|
---|
74 | {
|
---|
75 | ULONG NumberOfModules;
|
---|
76 | RTL_PROCESS_MODULE_INFORMATION Modules[1];
|
---|
77 | } RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
|
---|
78 |
|
---|
79 | typedef NTSTATUS (WINAPI *PFNZWQUERYSYSTEMINFORMATION)(ULONG, PVOID, ULONG, PULONG);
|
---|
80 | static PFNZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;
|
---|
81 | static HMODULE hNtdll = 0;
|
---|
82 |
|
---|
83 |
|
---|
84 | static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *);
|
---|
85 |
|
---|
86 | static PAVLPVNODECORE pKnownModuleTree = NULL;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Registers a new module with the VMM
|
---|
90 | * @param pModule Module ptr
|
---|
91 | * @param fValidateMemory Validate/touch memory pages or not
|
---|
92 | */
|
---|
93 | void VBoxServicePageSharingRegisterModule(PKNOWN_MODULE pModule, bool fValidateMemory)
|
---|
94 | {
|
---|
95 | VMMDEVSHAREDREGIONDESC aRegions[VMMDEVSHAREDREGIONDESC_MAX];
|
---|
96 | DWORD dwModuleSize = pModule->Info.modBaseSize;
|
---|
97 | BYTE *pBaseAddress = pModule->Info.modBaseAddr;
|
---|
98 | DWORD cbVersionSize, dummy;
|
---|
99 | BYTE *pVersionInfo;
|
---|
100 |
|
---|
101 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule\n");
|
---|
102 |
|
---|
103 | cbVersionSize = GetFileVersionInfoSize(pModule->Info.szExePath, &dummy);
|
---|
104 | if (!cbVersionSize)
|
---|
105 | {
|
---|
106 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfoSize failed with %d\n", GetLastError());
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | pVersionInfo = (BYTE *)RTMemAlloc(cbVersionSize);
|
---|
110 | if (!pVersionInfo)
|
---|
111 | return;
|
---|
112 |
|
---|
113 | if (!GetFileVersionInfo(pModule->Info.szExePath, 0, cbVersionSize, pVersionInfo))
|
---|
114 | {
|
---|
115 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfo failed with %d\n", GetLastError());
|
---|
116 | goto end;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* Fetch default code page. */
|
---|
120 | struct LANGANDCODEPAGE {
|
---|
121 | WORD wLanguage;
|
---|
122 | WORD wCodePage;
|
---|
123 | } *lpTranslate;
|
---|
124 |
|
---|
125 | UINT cbTranslate;
|
---|
126 | BOOL ret = VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"), (LPVOID *)&lpTranslate, &cbTranslate);
|
---|
127 | if ( !ret
|
---|
128 | || cbTranslate < 4)
|
---|
129 | {
|
---|
130 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VerQueryValue failed with %d (cb=%d)\n", GetLastError(), cbTranslate);
|
---|
131 | goto end;
|
---|
132 | }
|
---|
133 |
|
---|
134 | unsigned i;
|
---|
135 | UINT cbFileVersion;
|
---|
136 | char *lpszFileVersion;
|
---|
137 | unsigned cTranslationBlocks = cbTranslate/sizeof(struct LANGANDCODEPAGE);
|
---|
138 |
|
---|
139 | for(i = 0; i < cTranslationBlocks; i++)
|
---|
140 | {
|
---|
141 | /* Fetch file version string. */
|
---|
142 | char szFileVersionLocation[256];
|
---|
143 |
|
---|
144 | sprintf(szFileVersionLocation, TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
|
---|
145 | ret = VerQueryValue(pVersionInfo, szFileVersionLocation, (LPVOID *)&lpszFileVersion, &cbFileVersion);
|
---|
146 | if (ret)
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | if (i == cTranslationBlocks)
|
---|
150 | {
|
---|
151 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: no file version found!\n");
|
---|
152 | goto end;
|
---|
153 | }
|
---|
154 |
|
---|
155 | _snprintf(pModule->szFileVersion, sizeof(pModule->szFileVersion), "%s", lpszFileVersion);
|
---|
156 | pModule->szFileVersion[RT_ELEMENTS(pModule->szFileVersion) - 1] = 0;
|
---|
157 |
|
---|
158 | unsigned idxRegion = 0;
|
---|
159 |
|
---|
160 | if (fValidateMemory)
|
---|
161 | {
|
---|
162 | do
|
---|
163 | {
|
---|
164 | MEMORY_BASIC_INFORMATION MemInfo;
|
---|
165 |
|
---|
166 | SIZE_T ret = VirtualQuery(pBaseAddress, &MemInfo, sizeof(MemInfo));
|
---|
167 | Assert(ret);
|
---|
168 | if (!ret)
|
---|
169 | {
|
---|
170 | VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VirtualQueryEx failed with %d\n", GetLastError());
|
---|
171 | break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if ( MemInfo.State == MEM_COMMIT
|
---|
175 | && MemInfo.Type == MEM_IMAGE)
|
---|
176 | {
|
---|
177 | switch (MemInfo.Protect)
|
---|
178 | {
|
---|
179 | case PAGE_EXECUTE:
|
---|
180 | case PAGE_EXECUTE_READ:
|
---|
181 | case PAGE_READONLY:
|
---|
182 | {
|
---|
183 | char *pRegion = (char *)MemInfo.BaseAddress;
|
---|
184 |
|
---|
185 | /* Skip the first region as it only contains the image file header. */
|
---|
186 | if (pRegion != (char *)pModule->Info.modBaseAddr)
|
---|
187 | {
|
---|
188 | /* Touch all pages. */
|
---|
189 | while (pRegion < (char *)MemInfo.BaseAddress + MemInfo.RegionSize)
|
---|
190 | {
|
---|
191 | /* Try to trick the optimizer to leave the page touching code in place. */
|
---|
192 | ASMProbeReadByte(pRegion);
|
---|
193 | pRegion += PAGE_SIZE;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)MemInfo.BaseAddress;
|
---|
197 | aRegions[idxRegion].cbRegion = MemInfo.RegionSize;
|
---|
198 | idxRegion++;
|
---|
199 |
|
---|
200 | break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | default:
|
---|
204 | break; /* ignore */
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | pBaseAddress = (BYTE *)MemInfo.BaseAddress + MemInfo.RegionSize;
|
---|
209 | if (dwModuleSize > MemInfo.RegionSize)
|
---|
210 | {
|
---|
211 | dwModuleSize -= MemInfo.RegionSize;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | dwModuleSize = 0;
|
---|
216 | break;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (idxRegion >= RT_ELEMENTS(aRegions))
|
---|
220 | break; /* out of room */
|
---|
221 | }
|
---|
222 | while (dwModuleSize);
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | /* We can't probe kernel memory ranges, so pretend it's one big region. */
|
---|
227 | aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)pBaseAddress;
|
---|
228 | aRegions[idxRegion].cbRegion = dwModuleSize;
|
---|
229 | idxRegion++;
|
---|
230 | }
|
---|
231 | VBoxServiceVerbose(3, "VbglR3RegisterSharedModule %s %s base=%p size=%x cregions=%d\n", pModule->Info.szModule, pModule->szFileVersion, pModule->Info.modBaseAddr, pModule->Info.modBaseSize, idxRegion);
|
---|
232 | int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr,
|
---|
233 | pModule->Info.modBaseSize, idxRegion, aRegions);
|
---|
234 | // AssertRC(rc);
|
---|
235 | if (RT_FAILURE(rc))
|
---|
236 | VBoxServiceVerbose(3, "VbglR3RegisterSharedModule failed with %d\n", rc);
|
---|
237 |
|
---|
238 | end:
|
---|
239 | RTMemFree(pVersionInfo);
|
---|
240 | return;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Inspect all loaded modules for the specified process
|
---|
245 | * @param dwProcessId Process id
|
---|
246 | */
|
---|
247 | void VBoxServicePageSharingInspectModules(DWORD dwProcessId, PAVLPVNODECORE *ppNewTree)
|
---|
248 | {
|
---|
249 | HANDLE hProcess, hSnapshot;
|
---|
250 |
|
---|
251 | /* Get a list of all the modules in this process. */
|
---|
252 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
|
---|
253 | FALSE /* no child process handle inheritance */, dwProcessId);
|
---|
254 | if (hProcess == NULL)
|
---|
255 | {
|
---|
256 | VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: OpenProcess %x failed with %d\n", dwProcessId, GetLastError());
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
|
---|
261 | if (hSnapshot == INVALID_HANDLE_VALUE)
|
---|
262 | {
|
---|
263 | VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
|
---|
264 | CloseHandle(hProcess);
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules\n");
|
---|
269 |
|
---|
270 | MODULEENTRY32 ModuleInfo;
|
---|
271 | BOOL bRet;
|
---|
272 |
|
---|
273 | ModuleInfo.dwSize = sizeof(ModuleInfo);
|
---|
274 | bRet = Module32First(hSnapshot, &ModuleInfo);
|
---|
275 | do
|
---|
276 | {
|
---|
277 | /** todo when changing this make sure VBoxService.exe is excluded! */
|
---|
278 | char *pszDot = strrchr(ModuleInfo.szModule, '.');
|
---|
279 | if ( pszDot
|
---|
280 | && (pszDot[1] == 'e' || pszDot[1] == 'E'))
|
---|
281 | continue; /* ignore executables for now. */
|
---|
282 |
|
---|
283 | /* Found it before? */
|
---|
284 | PAVLPVNODECORE pRec = RTAvlPVGet(ppNewTree, ModuleInfo.modBaseAddr);
|
---|
285 | if (!pRec)
|
---|
286 | {
|
---|
287 | pRec = RTAvlPVRemove(&pKnownModuleTree, ModuleInfo.modBaseAddr);
|
---|
288 | if (!pRec)
|
---|
289 | {
|
---|
290 | /* New module; register it. */
|
---|
291 | PKNOWN_MODULE pModule = (PKNOWN_MODULE)RTMemAllocZ(sizeof(*pModule));
|
---|
292 | Assert(pModule);
|
---|
293 | if (!pModule)
|
---|
294 | break;
|
---|
295 |
|
---|
296 | pModule->Info = ModuleInfo;
|
---|
297 | pModule->Core.Key = ModuleInfo.modBaseAddr;
|
---|
298 | pModule->hModule = LoadLibraryEx(ModuleInfo.szExePath, 0, DONT_RESOLVE_DLL_REFERENCES);
|
---|
299 | if (pModule->hModule)
|
---|
300 | VBoxServicePageSharingRegisterModule(pModule, true /* validate pages */);
|
---|
301 |
|
---|
302 | VBoxServiceVerbose(3, "\n\n MODULE NAME: %s", ModuleInfo.szModule );
|
---|
303 | VBoxServiceVerbose(3, "\n executable = %s", ModuleInfo.szExePath );
|
---|
304 | VBoxServiceVerbose(3, "\n process ID = 0x%08X", ModuleInfo.th32ProcessID );
|
---|
305 | VBoxServiceVerbose(3, "\n base address = 0x%08X", (DWORD) ModuleInfo.modBaseAddr );
|
---|
306 | VBoxServiceVerbose(3, "\n base size = %d", ModuleInfo.modBaseSize );
|
---|
307 |
|
---|
308 | pRec = &pModule->Core;
|
---|
309 | }
|
---|
310 | bool ret = RTAvlPVInsert(ppNewTree, pRec);
|
---|
311 | Assert(ret); NOREF(ret);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | while (Module32Next(hSnapshot, &ModuleInfo));
|
---|
315 |
|
---|
316 | CloseHandle(hSnapshot);
|
---|
317 | CloseHandle(hProcess);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Inspect all running processes for executables and dlls that might be worth sharing
|
---|
322 | * with other VMs.
|
---|
323 | *
|
---|
324 | */
|
---|
325 | void VBoxServicePageSharingInspectGuest()
|
---|
326 | {
|
---|
327 | HANDLE hSnapshot;
|
---|
328 | PAVLPVNODECORE pNewTree = NULL;
|
---|
329 | DWORD dwProcessId = GetCurrentProcessId();
|
---|
330 |
|
---|
331 | VBoxServiceVerbose(3, "VBoxServicePageSharingInspectGuest\n");
|
---|
332 |
|
---|
333 | hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
---|
334 | if (hSnapshot == INVALID_HANDLE_VALUE)
|
---|
335 | {
|
---|
336 | VBoxServiceVerbose(3, "CreateToolhelp32Snapshot failed with %d\n", GetLastError());
|
---|
337 | return;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* Check loaded modules for all running processes. */
|
---|
341 | PROCESSENTRY32 ProcessInfo;
|
---|
342 |
|
---|
343 | ProcessInfo.dwSize = sizeof(ProcessInfo);
|
---|
344 | Process32First(hSnapshot, &ProcessInfo);
|
---|
345 |
|
---|
346 | do
|
---|
347 | {
|
---|
348 | /* Skip our own process. */
|
---|
349 | if (ProcessInfo.th32ProcessID != dwProcessId)
|
---|
350 | VBoxServicePageSharingInspectModules(ProcessInfo.th32ProcessID, &pNewTree);
|
---|
351 | }
|
---|
352 | while (Process32Next(hSnapshot, &ProcessInfo));
|
---|
353 |
|
---|
354 | CloseHandle(hSnapshot);
|
---|
355 |
|
---|
356 | #if 0
|
---|
357 | /* Check all loaded kernel modules. */
|
---|
358 | if (ZwQuerySystemInformation)
|
---|
359 | {
|
---|
360 | ULONG cbBuffer = 0;
|
---|
361 | PVOID pBuffer = NULL;
|
---|
362 | PRTL_PROCESS_MODULES pSystemModules;
|
---|
363 |
|
---|
364 | NTSTATUS ret = ZwQuerySystemInformation(SystemModuleInformation, (PVOID)&cbBuffer, 0, &cbBuffer);
|
---|
365 | if (ret != STATUS_INFO_LENGTH_MISMATCH)
|
---|
366 | {
|
---|
367 | VBoxServiceVerbose(1, "ZwQuerySystemInformation returned %x (1)\n", ret);
|
---|
368 | goto skipkernelmodules;
|
---|
369 | }
|
---|
370 |
|
---|
371 | pBuffer = RTMemAllocZ(cbBuffer);
|
---|
372 | if (!pBuffer)
|
---|
373 | goto skipkernelmodules;
|
---|
374 |
|
---|
375 | ret = ZwQuerySystemInformation(SystemModuleInformation, pBuffer, cbBuffer, &cbBuffer);
|
---|
376 | if (ret != STATUS_SUCCESS)
|
---|
377 | {
|
---|
378 | VBoxServiceVerbose(1, "ZwQuerySystemInformation returned %x (1)\n", ret);
|
---|
379 | goto skipkernelmodules;
|
---|
380 | }
|
---|
381 |
|
---|
382 | pSystemModules = (PRTL_PROCESS_MODULES)pBuffer;
|
---|
383 | for (unsigned i = 0; i < pSystemModules->NumberOfModules; i++)
|
---|
384 | {
|
---|
385 | /* Found it before? */
|
---|
386 | PAVLPVNODECORE pRec = RTAvlPVGet(&pNewTree, pSystemModules->Modules[i].ImageBase);
|
---|
387 | if (!pRec)
|
---|
388 | {
|
---|
389 | pRec = RTAvlPVRemove(&pKnownModuleTree, pSystemModules->Modules[i].ImageBase);
|
---|
390 | if (!pRec)
|
---|
391 | {
|
---|
392 | /* New module; register it. */
|
---|
393 | char szFullFilePath[512];
|
---|
394 | PKNOWN_MODULE pModule = (PKNOWN_MODULE)RTMemAllocZ(sizeof(*pModule));
|
---|
395 | Assert(pModule);
|
---|
396 | if (!pModule)
|
---|
397 | break;
|
---|
398 |
|
---|
399 | strcpy(pModule->Info.szModule, &pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName]);
|
---|
400 | GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
|
---|
401 |
|
---|
402 | /* skip \Systemroot\system32 */
|
---|
403 | char *lpPath = strstr(pSystemModules->Modules[i].FullPathName, "\\system32");
|
---|
404 | if (!lpPath)
|
---|
405 | {
|
---|
406 | VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
|
---|
407 | RTMemFree(pModule);
|
---|
408 | break;
|
---|
409 | }
|
---|
410 |
|
---|
411 | lpPath = strchr(lpPath+1, '\\');
|
---|
412 | if (!lpPath)
|
---|
413 | {
|
---|
414 | VBoxServiceVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
|
---|
415 | RTMemFree(pModule);
|
---|
416 | break;
|
---|
417 | }
|
---|
418 |
|
---|
419 | strcat(szFullFilePath, lpPath);
|
---|
420 | strcpy(pModule->Info.szExePath, szFullFilePath);
|
---|
421 | pModule->Info.modBaseAddr = (BYTE *)pSystemModules->Modules[i].ImageBase;
|
---|
422 | pModule->Info.modBaseSize = pSystemModules->Modules[i].ImageSize;
|
---|
423 |
|
---|
424 | pModule->Core.Key = pSystemModules->Modules[i].ImageBase;
|
---|
425 | VBoxServicePageSharingRegisterModule(pModule, false /* don't check memory pages */);
|
---|
426 |
|
---|
427 | VBoxServiceVerbose(3, "\n\n KERNEL MODULE NAME: %s", pModule->Info.szModule );
|
---|
428 | VBoxServiceVerbose(3, "\n executable = %s", pModule->Info.szExePath );
|
---|
429 | VBoxServiceVerbose(3, "\n base address = 0x%08X", (DWORD) pModule->Info.modBaseAddr );
|
---|
430 | VBoxServiceVerbose(3, "\n base size = %d", pModule->Info.modBaseSize );
|
---|
431 |
|
---|
432 | pRec = &pModule->Core;
|
---|
433 | }
|
---|
434 | bool ret = RTAvlPVInsert(&pNewTree, pRec);
|
---|
435 | Assert(ret); NOREF(ret);
|
---|
436 | }
|
---|
437 | }
|
---|
438 | skipkernelmodules:
|
---|
439 | if (pBuffer)
|
---|
440 | RTMemFree(pBuffer);
|
---|
441 | }
|
---|
442 | #endif
|
---|
443 |
|
---|
444 | /* Delete leftover modules in the old tree. */
|
---|
445 | RTAvlPVDestroy(&pKnownModuleTree, VBoxServicePageSharingEmptyTreeCallback, NULL);
|
---|
446 |
|
---|
447 | /* Check all registered modules. */
|
---|
448 | VbglR3CheckSharedModules();
|
---|
449 |
|
---|
450 | /* Activate new module tree. */
|
---|
451 | pKnownModuleTree = pNewTree;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * RTAvlPVDestroy callback.
|
---|
456 | */
|
---|
457 | static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *)
|
---|
458 | {
|
---|
459 | PKNOWN_MODULE pModule = (PKNOWN_MODULE)pNode;
|
---|
460 |
|
---|
461 | VBoxServiceVerbose(3, "VBoxServicePageSharingEmptyTreeCallback %s %s\n", pModule->Info.szModule, pModule->szFileVersion);
|
---|
462 |
|
---|
463 | /* Defererence module in the hypervisor. */
|
---|
464 | int rc = VbglR3UnregisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (RTGCPTR64)pModule->Info.modBaseAddr, pModule->Info.modBaseSize);
|
---|
465 | AssertRC(rc);
|
---|
466 |
|
---|
467 | if (pModule->hModule)
|
---|
468 | FreeLibrary(pModule->hModule);
|
---|
469 | RTMemFree(pNode);
|
---|
470 | return 0;
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | #elif TARGET_NT4
|
---|
475 | void VBoxServicePageSharingInspectGuest()
|
---|
476 | {
|
---|
477 | /* not implemented */
|
---|
478 | }
|
---|
479 | #else
|
---|
480 | void VBoxServicePageSharingInspectGuest()
|
---|
481 | {
|
---|
482 | /* @todo other platforms */
|
---|
483 | }
|
---|
484 | #endif
|
---|
485 |
|
---|
486 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
487 | static DECLCALLBACK(int) VBoxServicePageSharingPreInit(void)
|
---|
488 | {
|
---|
489 | return VINF_SUCCESS;
|
---|
490 | }
|
---|
491 |
|
---|
492 |
|
---|
493 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
494 | static DECLCALLBACK(int) VBoxServicePageSharingOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
495 | {
|
---|
496 | NOREF(ppszShort);
|
---|
497 | NOREF(argc);
|
---|
498 | NOREF(argv);
|
---|
499 | NOREF(pi);
|
---|
500 | return VINF_SUCCESS;
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
505 | static DECLCALLBACK(int) VBoxServicePageSharingInit(void)
|
---|
506 | {
|
---|
507 | VBoxServiceVerbose(3, "VBoxServicePageSharingInit\n");
|
---|
508 |
|
---|
509 | int rc = RTSemEventMultiCreate(&g_PageSharingEvent);
|
---|
510 | AssertRCReturn(rc, rc);
|
---|
511 |
|
---|
512 | #if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
|
---|
513 | hNtdll = LoadLibrary("ntdll.dll");
|
---|
514 |
|
---|
515 | if (hNtdll)
|
---|
516 | ZwQuerySystemInformation = (PFNZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
|
---|
517 | #endif
|
---|
518 |
|
---|
519 | /* @todo report system name and version */
|
---|
520 | /* Never fail here. */
|
---|
521 | return VINF_SUCCESS;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
525 | DECLCALLBACK(int) VBoxServicePageSharingWorker(bool volatile *pfShutdown)
|
---|
526 | {
|
---|
527 | /*
|
---|
528 | * Tell the control thread that it can continue
|
---|
529 | * spawning services.
|
---|
530 | */
|
---|
531 | RTThreadUserSignal(RTThreadSelf());
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * 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
|
---|
535 | * before will end up crashing the process as the dll's init routine was never called.
|
---|
536 | *
|
---|
537 | * We have to use this feature as we can't simply execute all init code in our service process.
|
---|
538 | *
|
---|
539 | */
|
---|
540 | int rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
|
---|
541 | if (*pfShutdown)
|
---|
542 | goto end;
|
---|
543 |
|
---|
544 | if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
|
---|
545 | {
|
---|
546 | VBoxServiceError("RTSemEventMultiWait failed; rc=%Rrc\n", rc);
|
---|
547 | goto end;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * Now enter the loop retrieving runtime data continuously.
|
---|
552 | */
|
---|
553 | for (;;)
|
---|
554 | {
|
---|
555 | VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: enabled=%d\n", VbglR3PageSharingIsEnabled());
|
---|
556 |
|
---|
557 | if (VbglR3PageSharingIsEnabled())
|
---|
558 | VBoxServicePageSharingInspectGuest();
|
---|
559 |
|
---|
560 | /*
|
---|
561 | * Block for a minute.
|
---|
562 | *
|
---|
563 | * The event semaphore takes care of ignoring interruptions and it
|
---|
564 | * allows us to implement service wakeup later.
|
---|
565 | */
|
---|
566 | if (*pfShutdown)
|
---|
567 | break;
|
---|
568 | rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
|
---|
569 | if (*pfShutdown)
|
---|
570 | break;
|
---|
571 | if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
|
---|
572 | {
|
---|
573 | VBoxServiceError("RTSemEventMultiWait failed; rc=%Rrc\n", rc);
|
---|
574 | break;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | end:
|
---|
579 | RTSemEventMultiDestroy(g_PageSharingEvent);
|
---|
580 | g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
|
---|
581 |
|
---|
582 | VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: finished thread\n");
|
---|
583 | return 0;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
587 | static DECLCALLBACK(void) VBoxServicePageSharingTerm(void)
|
---|
588 | {
|
---|
589 | VBoxServiceVerbose(3, "VBoxServicePageSharingTerm\n");
|
---|
590 |
|
---|
591 | #if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
|
---|
592 | if (hNtdll)
|
---|
593 | FreeLibrary(hNtdll);
|
---|
594 | #endif
|
---|
595 | return;
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
600 | static DECLCALLBACK(void) VBoxServicePageSharingStop(void)
|
---|
601 | {
|
---|
602 | RTSemEventMultiSignal(g_PageSharingEvent);
|
---|
603 | }
|
---|
604 |
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * The 'pagesharing' service description.
|
---|
608 | */
|
---|
609 | VBOXSERVICE g_PageSharing =
|
---|
610 | {
|
---|
611 | /* pszName. */
|
---|
612 | "pagesharing",
|
---|
613 | /* pszDescription. */
|
---|
614 | "Page Sharing",
|
---|
615 | /* pszUsage. */
|
---|
616 | NULL,
|
---|
617 | /* pszOptions. */
|
---|
618 | NULL,
|
---|
619 | /* methods */
|
---|
620 | VBoxServicePageSharingPreInit,
|
---|
621 | VBoxServicePageSharingOption,
|
---|
622 | VBoxServicePageSharingInit,
|
---|
623 | VBoxServicePageSharingWorker,
|
---|
624 | VBoxServicePageSharingStop,
|
---|
625 | VBoxServicePageSharingTerm
|
---|
626 | };
|
---|