1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/* $Id: VBoxServicePageSharing.cpp 29640 2010-05-18 14:30:50Z vboxsync $ */
/** @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;
* you can redistribute it and/or modify it under the terms of the GNU
* 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/assert.h>
#include <iprt/avl.h>
#include <iprt/asm.h>
#include <iprt/mem.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/semaphore.h>
#include <iprt/system.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The semaphore we're blocking on. */
static RTSEMEVENTMULTI g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
#if defined(RT_OS_WINDOWS) && !defined(TARGET_NT4)
#include <tlhelp32.h>
#include <psapi.h>
typedef struct
{
AVLPVNODECORE Core;
HMODULE hModule;
char szFileVersion[16];
MODULEENTRY32 Info;
} KNOWN_MODULE, *PKNOWN_MODULE;
static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *);
static PAVLPVNODECORE pKnownModuleTree = NULL;
/**
* Registers a new module with the VMM
* @param pModule Module ptr
*/
void VBoxServicePageSharingRegisterModule(PKNOWN_MODULE pModule)
{
VMMDEVSHAREDREGIONDESC aRegions[VMMDEVSHAREDREGIONDESC_MAX];
DWORD dwModuleSize = pModule->Info.modBaseSize;
BYTE *pBaseAddress = pModule->Info.modBaseAddr;
DWORD cbVersionSize, dummy;
BYTE *pVersionInfo;
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule\n");
cbVersionSize = GetFileVersionInfoSize(pModule->Info.szExePath, &dummy);
if (!cbVersionSize)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfoSize failed with %d\n", GetLastError());
return;
}
pVersionInfo = (BYTE *)RTMemAlloc(cbVersionSize);
if (!pVersionInfo)
return;
if (!GetFileVersionInfo(pModule->Info.szExePath, 0, cbVersionSize, pVersionInfo))
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: GetFileVersionInfo failed with %d\n", GetLastError());
goto end;
}
/* Fetch default code page. */
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
UINT cbTranslate;
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;
UINT cbFileVersion;
char *lpszFileVersion;
unsigned cTranslationBlocks = cbTranslate/sizeof(struct LANGANDCODEPAGE);
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)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: no file version found!\n");
goto end;
}
_snprintf(pModule->szFileVersion, sizeof(pModule->szFileVersion), "%s", lpszFileVersion);
pModule->szFileVersion[RT_ELEMENTS(pModule->szFileVersion) - 1] = 0;
unsigned idxRegion = 0;
do
{
MEMORY_BASIC_INFORMATION MemInfo;
SIZE_T ret = VirtualQuery(pBaseAddress, &MemInfo, sizeof(MemInfo));
Assert(ret);
if (!ret)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingRegisterModule: VirtualQueryEx failed with %d\n", GetLastError());
break;
}
if ( MemInfo.State == MEM_COMMIT
&& MemInfo.Type == MEM_IMAGE)
{
switch (MemInfo.Protect)
{
case PAGE_EXECUTE:
case PAGE_EXECUTE_READ:
case PAGE_READONLY:
{
char *pRegion = (char *)MemInfo.BaseAddress;
/* Skip the first region as it only contains the image file header. */
if (pRegion != (char *)pModule->Info.modBaseAddr)
{
/* Touch all pages. */
while (pRegion < (char *)MemInfo.BaseAddress + MemInfo.RegionSize)
{
/* Try to trick the optimizer to leave the page touching code in place. */
ASMProbeReadByte(pRegion);
pRegion += PAGE_SIZE;
}
}
aRegions[idxRegion].GCRegionAddr = (RTGCPTR64)MemInfo.BaseAddress;
aRegions[idxRegion].cbRegion = MemInfo.RegionSize;
idxRegion++;
break;
}
default:
break; /* ignore */
}
}
pBaseAddress = (BYTE *)MemInfo.BaseAddress + MemInfo.RegionSize;
if (dwModuleSize > MemInfo.RegionSize)
{
dwModuleSize -= MemInfo.RegionSize;
}
else
{
dwModuleSize = 0;
break;
}
if (idxRegion >= RT_ELEMENTS(aRegions))
break; /* out of room */
}
while (dwModuleSize);
VBoxServiceVerbose(3, "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, (RTGCPTR64)pModule->Info.modBaseAddr,
pModule->Info.modBaseSize, idxRegion, aRegions);
// AssertRC(rc);
if (RT_FAILURE(rc))
VBoxServiceVerbose(3, "VbglR3RegisterSharedModule failed with %d\n", rc);
end:
RTMemFree(pVersionInfo);
return;
}
/**
* Inspect all loaded modules for the specified process
* @param dwProcessId Process id
*/
void VBoxServicePageSharingInspectModules(DWORD dwProcessId, PAVLPVNODECORE *ppNewTree)
{
HANDLE hProcess, hSnapshot;
/* Get a list of all the modules in this process. */
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
FALSE /* no child process handle inheritance */, dwProcessId);
if (hProcess == NULL)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: OpenProcess %x failed with %d\n", dwProcessId, GetLastError());
return;
}
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules: CreateToolhelp32Snapshot failed with %d\n", GetLastError());
CloseHandle(hProcess);
return;
}
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectModules\n");
MODULEENTRY32 ModuleInfo;
BOOL bRet;
ModuleInfo.dwSize = sizeof(ModuleInfo);
bRet = Module32First(hSnapshot, &ModuleInfo);
do
{
/** todo when changing this make sure VBoxService.exe is excluded! */
char *pszDot = strrchr(ModuleInfo.szModule, '.');
if ( pszDot
&& (pszDot[1] == 'e' || pszDot[1] == 'E'))
continue; /* ignore executables for now. */
/* Found it before? */
PAVLPVNODECORE pRec = RTAvlPVGet(ppNewTree, ModuleInfo.modBaseAddr);
if (!pRec)
{
pRec = RTAvlPVRemove(&pKnownModuleTree, ModuleInfo.modBaseAddr);
if (!pRec)
{
/* New module; register it. */
PKNOWN_MODULE pModule = (PKNOWN_MODULE)RTMemAllocZ(sizeof(*pModule));
Assert(pModule);
if (!pModule)
break;
pModule->Info = ModuleInfo;
pModule->Core.Key = ModuleInfo.modBaseAddr;
pModule->hModule = LoadLibraryEx(ModuleInfo.szExePath, 0, DONT_RESOLVE_DLL_REFERENCES);
if (pModule->hModule)
VBoxServicePageSharingRegisterModule(pModule);
pRec = &pModule->Core;
}
bool ret = RTAvlPVInsert(ppNewTree, pRec);
Assert(ret); NOREF(ret);
VBoxServiceVerbose(3, "\n\n MODULE NAME: %s", ModuleInfo.szModule );
VBoxServiceVerbose(3, "\n executable = %s", ModuleInfo.szExePath );
VBoxServiceVerbose(3, "\n process ID = 0x%08X", ModuleInfo.th32ProcessID );
VBoxServiceVerbose(3, "\n base address = 0x%08X", (DWORD) ModuleInfo.modBaseAddr );
VBoxServiceVerbose(3, "\n base size = %d", ModuleInfo.modBaseSize );
}
}
while (Module32Next(hSnapshot, &ModuleInfo));
CloseHandle(hSnapshot);
CloseHandle(hProcess);
}
/**
* Inspect all running processes for executables and dlls that might be worth sharing
* with other VMs.
*
*/
void VBoxServicePageSharingInspectGuest()
{
HANDLE hSnapshot;
PAVLPVNODECORE pNewTree = NULL;
DWORD dwProcessId = GetCurrentProcessId();
VBoxServiceVerbose(3, "VBoxServicePageSharingInspectGuest\n");
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
VBoxServiceVerbose(3, "CreateToolhelp32Snapshot failed with %d\n", GetLastError());
return;
}
/* Check loaded modules for all running processes. */
PROCESSENTRY32 ProcessInfo;
ProcessInfo.dwSize = sizeof(ProcessInfo);
Process32First(hSnapshot, &ProcessInfo);
do
{
/* Skip our own process. */
if (ProcessInfo.th32ProcessID != dwProcessId)
VBoxServicePageSharingInspectModules(ProcessInfo.th32ProcessID, &pNewTree);
}
while (Process32Next(hSnapshot, &ProcessInfo));
CloseHandle(hSnapshot);
/* Delete leftover modules in the old tree. */
RTAvlPVDestroy(&pKnownModuleTree, VBoxServicePageSharingEmptyTreeCallback, NULL);
/* Check all registered modules. */
VbglR3CheckSharedModules();
/* Activate new module tree. */
pKnownModuleTree = pNewTree;
}
/**
* RTAvlPVDestroy callback.
*/
static DECLCALLBACK(int) VBoxServicePageSharingEmptyTreeCallback(PAVLPVNODECORE pNode, void *)
{
PKNOWN_MODULE pModule = (PKNOWN_MODULE)pNode;
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);
AssertRC(rc);
if (pModule->hModule)
FreeLibrary(pModule->hModule);
RTMemFree(pNode);
return 0;
}
#elif TARGET_NT4
void VBoxServicePageSharingInspectGuest()
{
/* not implemented */
}
#else
void VBoxServicePageSharingInspectGuest()
{
/* @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)
{
NOREF(ppszShort);
NOREF(argc);
NOREF(argv);
NOREF(pi);
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServicePageSharingInit(void)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingInit\n");
int rc = RTSemEventMultiCreate(&g_PageSharingEvent);
AssertRCReturn(rc, rc);
/* @todo report system name and version */
/* Never fail here. */
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnWorker */
DECLCALLBACK(int) VBoxServicePageSharingWorker(bool volatile *pfShutdown)
{
/*
* Tell the control thread that it can continue
* spawning services.
*/
RTThreadUserSignal(RTThreadSelf());
/*
* Now enter the loop retrieving runtime data continuously.
*/
for (;;)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: enabled=%d\n", VbglR3PageSharingIsEnabled());
if (VbglR3PageSharingIsEnabled())
VBoxServicePageSharingInspectGuest();
/*
* Block for a minute.
*
* The event semaphore takes care of ignoring interruptions and it
* allows us to implement service wakeup later.
*/
if (*pfShutdown)
break;
int rc = RTSemEventMultiWait(g_PageSharingEvent, 60000);
if (*pfShutdown)
break;
if (rc != VERR_TIMEOUT && RT_FAILURE(rc))
{
VBoxServiceError("RTSemEventMultiWait failed; rc=%Rrc\n", rc);
break;
}
}
RTSemEventMultiDestroy(g_PageSharingEvent);
g_PageSharingEvent = NIL_RTSEMEVENTMULTI;
VBoxServiceVerbose(3, "VBoxServicePageSharingWorker: finished thread\n");
return 0;
}
/** @copydoc VBOXSERVICE::pfnTerm */
static DECLCALLBACK(void) VBoxServicePageSharingTerm(void)
{
VBoxServiceVerbose(3, "VBoxServicePageSharingTerm\n");
return;
}
/** @copydoc VBOXSERVICE::pfnStop */
static DECLCALLBACK(void) VBoxServicePageSharingStop(void)
{
RTSemEventMultiSignal(g_PageSharingEvent);
}
/**
* The 'pagesharing' service description.
*/
VBOXSERVICE g_PageSharing =
{
/* pszName. */
"pagesharing",
/* pszDescription. */
"Page Sharing",
/* pszUsage. */
NULL,
/* pszOptions. */
NULL,
/* methods */
VBoxServicePageSharingPreInit,
VBoxServicePageSharingOption,
VBoxServicePageSharingInit,
VBoxServicePageSharingWorker,
VBoxServicePageSharingStop,
VBoxServicePageSharingTerm
};
|