VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServicePageSharing.cpp@ 98831

Last change on this file since 98831 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 KB
Line 
1/* $Id: VBoxServicePageSharing.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxService - Guest page sharing.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/** @page pg_vgsvc_pagesharing VBoxService - Page Sharing
30 *
31 * The Page Sharing subservice is responsible for finding memory mappings
32 * suitable page fusions.
33 *
34 * It is the driving force behind the Page Fusion feature in VirtualBox.
35 * Working with PGM and GMM (ring-0) thru the VMMDev interface. Every so often
36 * it reenumerates the memory mappings (executables and shared libraries) of the
37 * guest OS and reports additions and removals to GMM. For each mapping there
38 * is a filename and version as well as and address range and subsections. GMM
39 * will match the mapping with mapping with the same name and version from other
40 * VMs and see if there are any identical pages between the two.
41 *
42 * To increase the hit rate and reduce the volatility, the service launches a
43 * child process which loads all the Windows system DLLs it can. The child
44 * process is necessary as the DLLs are loaded without running the init code,
45 * and therefore not actually callable for other VBoxService code (may crash).
46 *
47 * This is currently only implemented on Windows. There is no technical reason
48 * for it not to be doable for all the other guests too, it's just a matter of
49 * customer demand and engineering time.
50 *
51 */
52
53
54/*********************************************************************************************************************************
55* Header Files *
56*********************************************************************************************************************************/
57#include <iprt/assert.h>
58#include <iprt/avl.h>
59#include <iprt/asm.h>
60#include <iprt/mem.h>
61#include <iprt/ldr.h>
62#include <iprt/process.h>
63#include <iprt/env.h>
64#include <iprt/stream.h>
65#include <iprt/file.h>
66#include <iprt/string.h>
67#include <iprt/semaphore.h>
68#include <iprt/string.h>
69#include <iprt/system.h>
70#include <iprt/thread.h>
71#include <iprt/time.h>
72#include <VBox/err.h>
73#include <VBox/VMMDev.h>
74#include <VBox/VBoxGuestLib.h>
75
76#ifdef RT_OS_WINDOWS
77#include <iprt/nt/nt-and-windows.h>
78# include <tlhelp32.h>
79# include <psapi.h>
80# include <winternl.h>
81#endif
82
83#include "VBoxServiceInternal.h"
84#include "VBoxServiceUtils.h"
85
86
87/*********************************************************************************************************************************
88* Header Files *
89*********************************************************************************************************************************/
90typedef struct
91{
92 AVLPVNODECORE Core;
93#ifdef RT_OS_WINDOWS
94 HMODULE hModule;
95 char szFileVersion[16];
96 MODULEENTRY32 Info;
97#endif
98} VGSVCPGSHKNOWNMOD, *PVGSVCPGSHKNOWNMOD;
99
100
101/*********************************************************************************************************************************
102* Global Variables *
103*********************************************************************************************************************************/
104/** The semaphore we're blocking on. */
105static RTSEMEVENTMULTI g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
106
107static PAVLPVNODECORE g_pKnownModuleTree = NULL;
108static uint64_t g_idSession = 0;
109
110
111/*********************************************************************************************************************************
112* Internal Functions *
113*********************************************************************************************************************************/
114static DECLCALLBACK(int) vgsvcPageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser);
115
116
117#ifdef RT_OS_WINDOWS
118
119/**
120 * Registers a new module with the VMM
121 * @param pModule Module ptr
122 * @param fValidateMemory Validate/touch memory pages or not
123 */
124static void vgsvcPageSharingRegisterModule(PVGSVCPGSHKNOWNMOD pModule, bool fValidateMemory)
125{
126 VMMDEVSHAREDREGIONDESC aRegions[VMMDEVSHAREDREGIONDESC_MAX];
127 DWORD dwModuleSize = pModule->Info.modBaseSize;
128 BYTE *pBaseAddress = pModule->Info.modBaseAddr;
129
130 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule\n");
131
132 DWORD dwDummy;
133 DWORD cbVersion = GetFileVersionInfoSize(pModule->Info.szExePath, &dwDummy);
134 if (!cbVersion)
135 {
136 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: GetFileVersionInfoSize failed with %d\n", GetLastError());
137 return;
138 }
139 BYTE *pVersionInfo = (BYTE *)RTMemAllocZ(cbVersion);
140 if (!pVersionInfo)
141 return;
142
143 if (!GetFileVersionInfo(pModule->Info.szExePath, 0, cbVersion, pVersionInfo))
144 {
145 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: GetFileVersionInfo failed with %d\n", GetLastError());
146 goto end;
147 }
148
149 /* Fetch default code page. */
150 struct LANGANDCODEPAGE
151 {
152 WORD wLanguage;
153 WORD wCodePage;
154 } *lpTranslate;
155
156 UINT cbTranslate;
157 BOOL fRet = VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"), (LPVOID *)&lpTranslate, &cbTranslate);
158 if ( !fRet
159 || cbTranslate < 4)
160 {
161 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: VerQueryValue failed with %d (cb=%d)\n", GetLastError(), cbTranslate);
162 goto end;
163 }
164
165 unsigned i;
166 UINT cbFileVersion;
167 char *pszFileVersion = NULL; /* Shut up MSC */
168 unsigned cTranslationBlocks = cbTranslate/sizeof(struct LANGANDCODEPAGE);
169
170 pModule->szFileVersion[0] = '\0';
171 for (i = 0; i < cTranslationBlocks; i++)
172 {
173 /* Fetch file version string. */
174 char szFileVersionLocation[256];
175
176/** @todo r=bird: Mixing ANSI and TCHAR crap again. This code is a mess. We
177 * always use the wide version of the API and convert to UTF-8/whatever. */
178
179 RTStrPrintf(szFileVersionLocation, sizeof(szFileVersionLocation),
180 "\\StringFileInfo\\%04x%04x\\FileVersion", lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
181 fRet = VerQueryValue(pVersionInfo, szFileVersionLocation, (LPVOID *)&pszFileVersion, &cbFileVersion);
182 if (fRet)
183 {
184 RTStrCopy(pModule->szFileVersion, sizeof(pModule->szFileVersion), pszFileVersion);
185 break;
186 }
187 }
188 if (i == cTranslationBlocks)
189 {
190 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: no file version found!\n");
191 goto end;
192 }
193
194 unsigned idxRegion = 0;
195
196 if (fValidateMemory)
197 {
198 do
199 {
200 MEMORY_BASIC_INFORMATION MemInfo;
201 SIZE_T cbRet = VirtualQuery(pBaseAddress, &MemInfo, sizeof(MemInfo));
202 Assert(cbRet);
203 if (!cbRet)
204 {
205 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: VirtualQueryEx failed with %d\n", GetLastError());
206 break;
207 }
208
209 if ( MemInfo.State == MEM_COMMIT
210 && MemInfo.Type == MEM_IMAGE)
211 {
212 switch (MemInfo.Protect)
213 {
214 case PAGE_EXECUTE:
215 case PAGE_EXECUTE_READ:
216 case PAGE_READONLY:
217 {
218 char *pRegion = (char *)MemInfo.BaseAddress;
219
220 /* Skip the first region as it only contains the image file header. */
221 if (pRegion != (char *)pModule->Info.modBaseAddr)
222 {
223 /* Touch all pages. */
224 while ((uintptr_t)pRegion < (uintptr_t)MemInfo.BaseAddress + MemInfo.RegionSize)
225 {
226 /* Try to trick the optimizer to leave the page touching code in place. */
227 ASMProbeReadByte(pRegion);
228 pRegion += PAGE_SIZE;
229 }
230 }
231#ifdef RT_ARCH_X86
232 aRegions[idxRegion].GCRegionAddr = (RTGCPTR32)MemInfo.BaseAddress;
233#else
234 aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)MemInfo.BaseAddress;
235#endif
236 aRegions[idxRegion].cbRegion = MemInfo.RegionSize;
237 idxRegion++;
238
239 break;
240 }
241
242 default:
243 break; /* ignore */
244 }
245 }
246
247 pBaseAddress = (BYTE *)MemInfo.BaseAddress + MemInfo.RegionSize;
248 if (dwModuleSize > MemInfo.RegionSize)
249 dwModuleSize -= MemInfo.RegionSize;
250 else
251 {
252 dwModuleSize = 0;
253 break;
254 }
255
256 if (idxRegion >= RT_ELEMENTS(aRegions))
257 break; /* out of room */
258 }
259 while (dwModuleSize);
260 }
261 else
262 {
263 /* We can't probe kernel memory ranges, so pretend it's one big region. */
264#ifdef RT_ARCH_X86
265 aRegions[idxRegion].GCRegionAddr = (RTGCPTR32)pBaseAddress;
266#else
267 aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)pBaseAddress;
268#endif
269 aRegions[idxRegion].cbRegion = dwModuleSize;
270 idxRegion++;
271 }
272 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: VbglR3RegisterSharedModule %s %s base=%p size=%x cregions=%d\n", pModule->Info.szModule, pModule->szFileVersion, pModule->Info.modBaseAddr, pModule->Info.modBaseSize, idxRegion);
273 int rc = VbglR3RegisterSharedModule(pModule->Info.szModule, pModule->szFileVersion, (uintptr_t)pModule->Info.modBaseAddr,
274 pModule->Info.modBaseSize, idxRegion, aRegions);
275 if (RT_FAILURE(rc))
276 VGSvcVerbose(3, "vgsvcPageSharingRegisterModule: VbglR3RegisterSharedModule failed with %Rrc\n", rc);
277
278end:
279 RTMemFree(pVersionInfo);
280 return;
281}
282
283
284/**
285 * Inspect all loaded modules for the specified process
286 *
287 * @param dwProcessId Process id
288 * @param ppNewTree The module tree we're assembling from modules found
289 * in this process. Modules found are moved from
290 * g_pKnownModuleTree or created new.
291 */
292static void vgsvcPageSharingInspectModules(DWORD dwProcessId, PAVLPVNODECORE *ppNewTree)
293{
294 /* Get a list of all the modules in this process. */
295 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE /* no child process handle inheritance */, dwProcessId);
296 if (hProcess == NULL)
297 {
298 VGSvcVerbose(3, "vgsvcPageSharingInspectModules: OpenProcess %x failed with %d\n", dwProcessId, GetLastError());
299 return;
300 }
301
302 HANDLE hSnapshot = g_pfnCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
303 if (hSnapshot == INVALID_HANDLE_VALUE)
304 {
305 VGSvcVerbose(3, "vgsvcPageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
306 CloseHandle(hProcess);
307 return;
308 }
309
310 VGSvcVerbose(3, "vgsvcPageSharingInspectModules\n");
311
312 MODULEENTRY32 ModuleInfo;
313 BOOL bRet;
314
315 ModuleInfo.dwSize = sizeof(ModuleInfo);
316 bRet = g_pfnModule32First(hSnapshot, &ModuleInfo);
317 do
318 {
319 /** @todo when changing this make sure VBoxService.exe is excluded! */
320 char *pszDot = strrchr(ModuleInfo.szModule, '.');
321 if ( pszDot
322 && (pszDot[1] == 'e' || pszDot[1] == 'E'))
323 continue; /* ignore executables for now. */
324
325 /* Found it before? */
326 PAVLPVNODECORE pRec = RTAvlPVGet(ppNewTree, ModuleInfo.modBaseAddr);
327 if (!pRec)
328 {
329 pRec = RTAvlPVRemove(&g_pKnownModuleTree, ModuleInfo.modBaseAddr);
330 if (!pRec)
331 {
332 /* New module; register it. */
333 PVGSVCPGSHKNOWNMOD pModule = (PVGSVCPGSHKNOWNMOD)RTMemAllocZ(sizeof(*pModule));
334 Assert(pModule);
335 if (!pModule)
336 break;
337
338 pModule->Info = ModuleInfo;
339 pModule->Core.Key = ModuleInfo.modBaseAddr;
340 pModule->hModule = LoadLibraryEx(ModuleInfo.szExePath, 0, DONT_RESOLVE_DLL_REFERENCES);
341 if (pModule->hModule)
342 vgsvcPageSharingRegisterModule(pModule, true /* validate pages */);
343
344 VGSvcVerbose(3, "\n\n MODULE NAME: %s", ModuleInfo.szModule );
345 VGSvcVerbose(3, "\n executable = %s", ModuleInfo.szExePath );
346 VGSvcVerbose(3, "\n process ID = 0x%08X", ModuleInfo.th32ProcessID );
347 VGSvcVerbose(3, "\n base address = %#010p", (uintptr_t) ModuleInfo.modBaseAddr );
348 VGSvcVerbose(3, "\n base size = %d", ModuleInfo.modBaseSize );
349
350 pRec = &pModule->Core;
351 }
352 bool ret = RTAvlPVInsert(ppNewTree, pRec);
353 Assert(ret); NOREF(ret);
354 }
355 } while (g_pfnModule32Next(hSnapshot, &ModuleInfo));
356
357 CloseHandle(hSnapshot);
358 CloseHandle(hProcess);
359}
360
361
362/**
363 * Inspect all running processes for executables and dlls that might be worth sharing
364 * with other VMs.
365 *
366 */
367static void vgsvcPageSharingInspectGuest(void)
368{
369 VGSvcVerbose(3, "vgsvcPageSharingInspectGuest\n");
370 PAVLPVNODECORE pNewTree = NULL;
371
372 /*
373 * Check loaded modules for all running processes.
374 */
375 if ( g_pfnProcess32First
376 && g_pfnProcess32Next
377 && g_pfnModule32First
378 && g_pfnModule32Next
379 && g_pfnCreateToolhelp32Snapshot)
380 {
381 HANDLE hSnapshot = g_pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
382 if (hSnapshot == INVALID_HANDLE_VALUE)
383 {
384 VGSvcVerbose(3, "vgsvcPageSharingInspectGuest: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
385 return;
386 }
387
388 DWORD const dwProcessId = GetCurrentProcessId();
389
390 PROCESSENTRY32 ProcessInfo;
391 ProcessInfo.dwSize = sizeof(ProcessInfo);
392 g_pfnProcess32First(hSnapshot, &ProcessInfo);
393
394 do
395 {
396 /* Skip our own process. */
397 if (ProcessInfo.th32ProcessID != dwProcessId)
398 vgsvcPageSharingInspectModules(ProcessInfo.th32ProcessID, &pNewTree);
399 }
400 while (g_pfnProcess32Next(hSnapshot, &ProcessInfo));
401
402 CloseHandle(hSnapshot);
403 }
404
405 /*
406 * Check all loaded kernel modules.
407 */
408 if (g_pfnZwQuerySystemInformation)
409 {
410 ULONG cbBuffer = 0;
411 PVOID pBuffer = NULL;
412 PRTL_PROCESS_MODULES pSystemModules;
413
414 NTSTATUS ret = g_pfnZwQuerySystemInformation(SystemModuleInformation, (PVOID)&cbBuffer, 0, &cbBuffer);
415 if (!cbBuffer)
416 {
417 VGSvcVerbose(1, "ZwQuerySystemInformation returned length 0\n");
418 goto skipkernelmodules;
419 }
420
421 pBuffer = RTMemAllocZ(cbBuffer);
422 if (!pBuffer)
423 goto skipkernelmodules;
424
425 ret = g_pfnZwQuerySystemInformation(SystemModuleInformation, pBuffer, cbBuffer, &cbBuffer);
426 if (ret != STATUS_SUCCESS)
427 {
428 VGSvcVerbose(1, "ZwQuerySystemInformation returned %x (1)\n", ret);
429 goto skipkernelmodules;
430 }
431
432 pSystemModules = (PRTL_PROCESS_MODULES)pBuffer;
433 for (unsigned i = 0; i < pSystemModules->NumberOfModules; i++)
434 {
435 VGSvcVerbose(4, "\n\n KERNEL MODULE NAME: %s", pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName] );
436 VGSvcVerbose(4, "\n executable = %s", pSystemModules->Modules[i].FullPathName );
437 VGSvcVerbose(4, "\n flags = 0x%08X\n", pSystemModules->Modules[i].Flags);
438
439 /* User-mode modules seem to have no flags set; skip them as we detected them above. */
440 if (pSystemModules->Modules[i].Flags == 0)
441 continue;
442
443 /* Found it before? */
444 PAVLPVNODECORE pRec = RTAvlPVGet(&pNewTree, pSystemModules->Modules[i].ImageBase);
445 if (!pRec)
446 {
447 pRec = RTAvlPVRemove(&g_pKnownModuleTree, pSystemModules->Modules[i].ImageBase);
448 if (!pRec)
449 {
450 /* New module; register it. */
451 char szFullFilePath[512];
452 PVGSVCPGSHKNOWNMOD pModule = (PVGSVCPGSHKNOWNMOD)RTMemAllocZ(sizeof(*pModule));
453 Assert(pModule);
454 if (!pModule)
455 break;
456
457/** @todo FullPathName not an UTF-8 string is! An ANSI string it is
458 * according to the SYSTEM locale. Best use RtlAnsiStringToUnicodeString to
459 * convert to UTF-16. */
460 strcpy(pModule->Info.szModule,
461 (const char *)&pSystemModules->Modules[i].FullPathName[pSystemModules->Modules[i].OffsetToFileName]);
462 GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
463
464 /* skip \Systemroot\system32 */
465 char *lpPath = strchr((char *)&pSystemModules->Modules[i].FullPathName[1], '\\');
466 if (!lpPath)
467 {
468 /* Seen just file names in XP; try to locate the file in the system32 and system32\drivers directories. */
469 RTStrCat(szFullFilePath, sizeof(szFullFilePath), "\\");
470 RTStrCat(szFullFilePath, sizeof(szFullFilePath), (const char *)pSystemModules->Modules[i].FullPathName);
471 VGSvcVerbose(3, "Unexpected kernel module name try %s\n", szFullFilePath);
472 if (RTFileExists(szFullFilePath) == false)
473 {
474 GetSystemDirectoryA(szFullFilePath, sizeof(szFullFilePath));
475 RTStrCat(szFullFilePath, sizeof(szFullFilePath), "\\drivers\\");
476 RTStrCat(szFullFilePath, sizeof(szFullFilePath), (const char *)pSystemModules->Modules[i].FullPathName);
477 VGSvcVerbose(3, "Unexpected kernel module name try %s\n", szFullFilePath);
478 if (RTFileExists(szFullFilePath) == false)
479 {
480 VGSvcVerbose(1, "Unexpected kernel module name %s\n", pSystemModules->Modules[i].FullPathName);
481 RTMemFree(pModule);
482 continue;
483 }
484 }
485 }
486 else
487 {
488 lpPath = strchr(lpPath + 1, '\\');
489 if (!lpPath)
490 {
491 VGSvcVerbose(1, "Unexpected kernel module name %s (2)\n", pSystemModules->Modules[i].FullPathName);
492 RTMemFree(pModule);
493 continue;
494 }
495
496 RTStrCat(szFullFilePath, sizeof(szFullFilePath), lpPath);
497 }
498
499 strcpy(pModule->Info.szExePath, szFullFilePath);
500 pModule->Info.modBaseAddr = (BYTE *)pSystemModules->Modules[i].ImageBase;
501 pModule->Info.modBaseSize = pSystemModules->Modules[i].ImageSize;
502
503 pModule->Core.Key = pSystemModules->Modules[i].ImageBase;
504 vgsvcPageSharingRegisterModule(pModule, false /* don't check memory pages */);
505
506 VGSvcVerbose(3, "\n\n KERNEL MODULE NAME: %s", pModule->Info.szModule );
507 VGSvcVerbose(3, "\n executable = %s", pModule->Info.szExePath );
508 VGSvcVerbose(3, "\n base address = %#010p", (uintptr_t)pModule->Info.modBaseAddr );
509 VGSvcVerbose(3, "\n flags = 0x%08X", pSystemModules->Modules[i].Flags);
510 VGSvcVerbose(3, "\n base size = %d", pModule->Info.modBaseSize );
511
512 pRec = &pModule->Core;
513 }
514 bool fRet = RTAvlPVInsert(&pNewTree, pRec);
515 Assert(fRet); NOREF(fRet);
516 }
517 }
518skipkernelmodules:
519 if (pBuffer)
520 RTMemFree(pBuffer);
521 }
522
523 /* Delete leftover modules in the old tree. */
524 RTAvlPVDestroy(&g_pKnownModuleTree, vgsvcPageSharingEmptyTreeCallback, NULL);
525
526 /* Check all registered modules. */
527 VbglR3CheckSharedModules();
528
529 /* Activate new module tree. */
530 g_pKnownModuleTree = pNewTree;
531}
532
533
534/**
535 * RTAvlPVDestroy callback.
536 */
537static DECLCALLBACK(int) vgsvcPageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *pvUser)
538{
539 PVGSVCPGSHKNOWNMOD pModule = (PVGSVCPGSHKNOWNMOD)pNode;
540 bool *pfUnregister = (bool *)pvUser;
541
542 VGSvcVerbose(3, "vgsvcPageSharingEmptyTreeCallback %s %s\n", pModule->Info.szModule, pModule->szFileVersion);
543
544 /* Dereference module in the hypervisor. */
545 if ( !pfUnregister
546 || *pfUnregister)
547 {
548 int rc = VbglR3UnregisterSharedModule(pModule->Info.szModule, pModule->szFileVersion,
549 (uintptr_t)pModule->Info.modBaseAddr, pModule->Info.modBaseSize);
550 AssertRC(rc);
551 }
552
553 if (pModule->hModule)
554 FreeLibrary(pModule->hModule);
555 RTMemFree(pNode);
556 return 0;
557}
558
559
560#else /* !RT_OS_WINDOWS */
561
562static void vgsvcPageSharingInspectGuest(void)
563{
564 /** @todo other platforms */
565}
566
567#endif /* !RT_OS_WINDOWS */
568
569/** @interface_method_impl{VBOXSERVICE,pfnInit} */
570static DECLCALLBACK(int) vgsvcPageSharingInit(void)
571{
572 VGSvcVerbose(3, "vgsvcPageSharingInit\n");
573
574 int rc = RTSemEventMultiCreate(&g_PageSharingEvent);
575 AssertRCReturn(rc, rc);
576
577#ifdef RT_OS_WINDOWS
578 rc = VbglR3GetSessionId(&g_idSession);
579 if (RT_FAILURE(rc))
580 {
581 if (rc == VERR_IO_GEN_FAILURE)
582 VGSvcVerbose(0, "PageSharing: Page sharing support is not available by the host\n");
583 else
584 VGSvcError("vgsvcPageSharingInit: Failed with rc=%Rrc\n", rc);
585
586 rc = VERR_SERVICE_DISABLED;
587
588 RTSemEventMultiDestroy(g_PageSharingEvent);
589 g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
590
591 }
592#endif
593
594 return rc;
595}
596
597
598/**
599 * @interface_method_impl{VBOXSERVICE,pfnWorker}
600 */
601static DECLCALLBACK(int) vgsvcPageSharingWorker(bool volatile *pfShutdown)
602{
603 /*
604 * Tell the control thread that it can continue
605 * spawning services.
606 */
607 RTThreadUserSignal(RTThreadSelf());
608
609 /*
610 * Now enter the loop retrieving runtime data continuously.
611 */
612 for (;;)
613 {
614 bool fEnabled = VbglR3PageSharingIsEnabled();
615 VGSvcVerbose(3, "vgsvcPageSharingWorker: enabled=%d\n", fEnabled);
616
617 if (fEnabled)
618 vgsvcPageSharingInspectGuest();
619
620 /*
621 * Block for a minute.
622 *
623 * The event semaphore takes care of ignoring interruptions and it
624 * allows us to implement service wakeup later.
625 */
626 if (*pfShutdown)
627 break;
628 int rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
629 if (*pfShutdown)
630 break;
631 if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
632 {
633 VGSvcError("vgsvcPageSharingWorker: RTSemEventMultiWait failed; rc=%Rrc\n", rc);
634 break;
635 }
636#ifdef RT_OS_WINDOWS
637 uint64_t idNewSession = g_idSession;
638 rc = VbglR3GetSessionId(&idNewSession);
639 AssertRC(rc);
640
641 if (idNewSession != g_idSession)
642 {
643 bool fUnregister = false;
644
645 VGSvcVerbose(3, "vgsvcPageSharingWorker: VM was restored!!\n");
646 /* The VM was restored, so reregister all modules the next time. */
647 RTAvlPVDestroy(&g_pKnownModuleTree, vgsvcPageSharingEmptyTreeCallback, &fUnregister);
648 g_pKnownModuleTree = NULL;
649
650 g_idSession = idNewSession;
651 }
652#endif
653 }
654
655 RTSemEventMultiDestroy(g_PageSharingEvent);
656 g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
657
658 VGSvcVerbose(3, "vgsvcPageSharingWorker: finished thread\n");
659 return 0;
660}
661
662#ifdef RT_OS_WINDOWS
663
664/**
665 * This gets control when VBoxService is launched with "pagefusion" by
666 * vgsvcPageSharingWorkerProcess().
667 *
668 * @returns RTEXITCODE_SUCCESS.
669 *
670 * @remarks It won't normally return since the parent drops the shutdown hint
671 * via RTProcTerminate().
672 */
673RTEXITCODE VGSvcPageSharingWorkerChild(void)
674{
675 VGSvcVerbose(3, "vgsvcPageSharingInitFork\n");
676
677 bool fShutdown = false;
678 vgsvcPageSharingInit();
679 vgsvcPageSharingWorker(&fShutdown);
680
681 return RTEXITCODE_SUCCESS;
682}
683
684
685/**
686 * @interface_method_impl{VBOXSERVICE,pfnWorker}
687 */
688static DECLCALLBACK(int) vgsvcPageSharingWorkerProcess(bool volatile *pfShutdown)
689{
690 RTPROCESS hProcess = NIL_RTPROCESS;
691 int rc;
692
693 /*
694 * Tell the control thread that it can continue
695 * spawning services.
696 */
697 RTThreadUserSignal(RTThreadSelf());
698
699 /*
700 * Now enter the loop retrieving runtime data continuously.
701 */
702 for (;;)
703 {
704 bool fEnabled = VbglR3PageSharingIsEnabled();
705 VGSvcVerbose(3, "vgsvcPageSharingWorkerProcess: enabled=%d\n", fEnabled);
706
707 /*
708 * Start a 2nd VBoxService process to deal with page fusion as we do
709 * not wish to dummy load dlls into this process. (First load with
710 * DONT_RESOLVE_DLL_REFERENCES, 2nd normal -> dll init routines not called!)
711 */
712 if ( fEnabled
713 && hProcess == NIL_RTPROCESS)
714 {
715 char szExeName[256];
716 char *pszExeName = RTProcGetExecutablePath(szExeName, sizeof(szExeName));
717 if (pszExeName)
718 {
719 char const *papszArgs[3];
720 papszArgs[0] = pszExeName;
721 papszArgs[1] = "pagefusion";
722 papszArgs[2] = NULL;
723 rc = RTProcCreate(pszExeName, papszArgs, RTENV_DEFAULT, 0 /* normal child */, &hProcess);
724 if (RT_FAILURE(rc))
725 VGSvcError("vgsvcPageSharingWorkerProcess: RTProcCreate %s failed; rc=%Rrc\n", pszExeName, rc);
726 }
727 }
728
729 /*
730 * Block for a minute.
731 *
732 * The event semaphore takes care of ignoring interruptions and it
733 * allows us to implement service wakeup later.
734 */
735 if (*pfShutdown)
736 break;
737 rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
738 if (*pfShutdown)
739 break;
740 if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
741 {
742 VGSvcError("vgsvcPageSharingWorkerProcess: RTSemEventMultiWait failed; rc=%Rrc\n", rc);
743 break;
744 }
745 }
746
747 if (hProcess != NIL_RTPROCESS)
748 RTProcTerminate(hProcess);
749
750 VGSvcVerbose(3, "vgsvcPageSharingWorkerProcess: finished thread\n");
751 return 0;
752}
753
754#endif /* RT_OS_WINDOWS */
755
756/**
757 * @interface_method_impl{VBOXSERVICE,pfnStop}
758 */
759static DECLCALLBACK(void) vgsvcPageSharingStop(void)
760{
761 RTSemEventMultiSignal(g_PageSharingEvent);
762}
763
764
765/**
766 * @interface_method_impl{VBOXSERVICE,pfnTerm}
767 */
768static DECLCALLBACK(void) vgsvcPageSharingTerm(void)
769{
770 if (g_PageSharingEvent != NIL_RTSEMEVENTMULTI)
771 {
772 RTSemEventMultiDestroy(g_PageSharingEvent);
773 g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
774 }
775}
776
777
778/**
779 * The 'pagesharing' service description.
780 */
781VBOXSERVICE g_PageSharing =
782{
783 /* pszName. */
784 "pagesharing",
785 /* pszDescription. */
786 "Page Sharing",
787 /* pszUsage. */
788 NULL,
789 /* pszOptions. */
790 NULL,
791 /* methods */
792 VGSvcDefaultPreInit,
793 VGSvcDefaultOption,
794 vgsvcPageSharingInit,
795#ifdef RT_OS_WINDOWS
796 vgsvcPageSharingWorkerProcess,
797#else
798 vgsvcPageSharingWorker,
799#endif
800 vgsvcPageSharingStop,
801 vgsvcPageSharingTerm
802};
803
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette