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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
|
/* Copyright (c) 2001, Stanford University
* All rights reserved
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#include "cr_spu.h"
#include "cr_net.h"
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_string.h"
#include "cr_net.h"
#include "cr_environment.h"
#include "cr_process.h"
#include "cr_rand.h"
#include "cr_netserver.h"
#include "stub.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <iprt/initterm.h>
#include <iprt/thread.h>
#include <iprt/errcore.h>
#include <iprt/asm.h>
#ifndef WINDOWS
# include <sys/types.h>
# include <unistd.h>
#endif
#ifdef VBOX_WITH_WDDM
#include <d3d9types.h>
#include <D3dumddi.h>
#endif
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
# include <VBoxCrHgsmi.h>
#endif
/**
* If you change this, see the comments in tilesortspu_context.c
*/
#define MAGIC_CONTEXT_BASE 500
#define CONFIG_LOOKUP_FILE ".crconfigs"
#ifdef WINDOWS
#define PYTHON_EXE "python.exe"
#else
#define PYTHON_EXE "python"
#endif
static bool stub_initialized = 0;
#ifdef WINDOWS
static CRmutex stub_init_mutex;
#define STUB_INIT_LOCK() do { crLockMutex(&stub_init_mutex); } while (0)
#define STUB_INIT_UNLOCK() do { crUnlockMutex(&stub_init_mutex); } while (0)
#else
#define STUB_INIT_LOCK() do { } while (0)
#define STUB_INIT_UNLOCK() do { } while (0)
#endif
/* NOTE: 'SPUDispatchTable glim' is declared in NULLfuncs.py now */
/* NOTE: 'SPUDispatchTable stubThreadsafeDispatch' is declared in tsfuncs.c */
Stub stub;
#ifdef CHROMIUM_THREADSAFE
static bool g_stubIsCurrentContextTSDInited;
CRtsd g_stubCurrentContextTSD;
#endif
#ifndef VBOX_NO_NATIVEGL
static void stubInitNativeDispatch( void )
{
# define MAX_FUNCS 1000
SPUNamedFunctionTable gl_funcs[MAX_FUNCS];
int numFuncs;
numFuncs = crLoadOpenGL( &stub.wsInterface, gl_funcs );
stub.haveNativeOpenGL = (numFuncs > 0);
/* XXX call this after context binding */
numFuncs += crLoadOpenGLExtensions( &stub.wsInterface, gl_funcs + numFuncs );
CRASSERT(numFuncs < MAX_FUNCS);
crSPUInitDispatchTable( &stub.nativeDispatch );
crSPUInitDispatch( &stub.nativeDispatch, gl_funcs );
crSPUInitDispatchNops( &stub.nativeDispatch );
# undef MAX_FUNCS
}
#endif /* !VBOX_NO_NATIVEGL */
/** Pointer to the SPU's real glClear and glViewport functions */
static ClearFunc_t origClear;
static ViewportFunc_t origViewport;
static SwapBuffersFunc_t origSwapBuffers;
static DrawBufferFunc_t origDrawBuffer;
static ScissorFunc_t origScissor;
static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
{
bool bForceUpdate = false;
bool bChanged = false;
#ifdef WINDOWS
/** @todo install hook and track for WM_DISPLAYCHANGE */
{
DEVMODE devMode;
devMode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
if (devMode.dmPelsWidth!=window->dmPelsWidth || devMode.dmPelsHeight!=window->dmPelsHeight)
{
crDebug("Resolution changed(%d,%d), forcing window Pos/Size update", devMode.dmPelsWidth, devMode.dmPelsHeight);
window->dmPelsWidth = devMode.dmPelsWidth;
window->dmPelsHeight = devMode.dmPelsHeight;
bForceUpdate = true;
}
}
#endif
bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
#if defined(GLX) || defined (WINDOWS)
if (stub.trackWindowVisibleRgn)
{
bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
}
#endif
if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
const int mapped = stubIsWindowVisible(window);
if (mapped != window->mapped) {
crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
window->mapped = mapped;
bChanged = true;
}
}
if (bFlushOnChange && bChanged)
{
stub.spu->dispatch_table.Flush();
}
}
static bool stubSystemWindowExist(WindowInfo *pWindow)
{
#ifdef WINDOWS
if (pWindow->hWnd!=WindowFromDC(pWindow->drawable))
{
return false;
}
#else
Window root;
int x, y;
unsigned int border, depth, w, h;
Display *dpy;
dpy = stubGetWindowDisplay(pWindow);
XLOCK(dpy);
if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
{
XUNLOCK(dpy);
return false;
}
XUNLOCK(dpy);
#endif
return true;
}
static void stubCheckWindowsCB(unsigned long key, void *data1, void *data2)
{
WindowInfo *pWindow = (WindowInfo *) data1;
ContextInfo *pCtx = (ContextInfo *) data2;
(void)key;
if (pWindow == pCtx->currentDrawable
|| pWindow->type!=CHROMIUM
|| pWindow->pOwner!=pCtx)
{
return;
}
if (!stubSystemWindowExist(pWindow))
{
#ifdef WINDOWS
stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->hWnd);
#else
stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->drawable);
#endif
return;
}
stubCheckWindowState(pWindow, GL_FALSE);
}
static void stubCheckWindowsState(void)
{
ContextInfo *context = stubGetCurrentContext();
CRASSERT(stub.trackWindowSize || stub.trackWindowPos);
if (!context)
return;
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
if (stub.bRunningUnderWDDM)
return;
#endif
/* Try to keep a consistent locking order. */
crHashtableLock(stub.windowTable);
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
crLockMutex(&stub.mutex);
#endif
stubCheckWindowState(context->currentDrawable, GL_TRUE);
crHashtableWalkUnlocked(stub.windowTable, stubCheckWindowsCB, context);
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
crUnlockMutex(&stub.mutex);
#endif
crHashtableUnlock(stub.windowTable);
}
/**
* Override the head SPU's glClear function.
* We're basically trapping this function so that we can poll the
* application window size at a regular interval.
*/
static void SPU_APIENTRY trapClear(GLbitfield mask)
{
stubCheckWindowsState();
/* call the original SPU glClear function */
origClear(mask);
}
/**
* As above, but for glViewport. Most apps call glViewport before
* glClear when a window is resized.
*/
static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
{
stubCheckWindowsState();
/* call the original SPU glViewport function */
origViewport(x, y, w, h);
}
/*static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
{
stubCheckWindowsState();
origSwapBuffers(window, flags);
}
static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
{
stubCheckWindowsState();
origDrawBuffer(buf);
}*/
#if 0 /* unused */
static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
{
int winX, winY;
unsigned int winW, winH;
WindowInfo *pWindow;
ContextInfo *context = stubGetCurrentContext();
(void)x; (void)y; (void)w; (void)h;
pWindow = context->currentDrawable;
stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
origScissor(0, 0, winW, winH);
}
#endif /* unused */
/**
* Use the GL function pointers in \<spu\> to initialize the static glim
* dispatch table.
*/
static void stubInitSPUDispatch(SPU *spu)
{
crSPUInitDispatchTable( &stub.spuDispatch );
crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
/* patch-in special glClear/Viewport function to track window sizing */
origClear = stub.spuDispatch.Clear;
origViewport = stub.spuDispatch.Viewport;
origSwapBuffers = stub.spuDispatch.SwapBuffers;
origDrawBuffer = stub.spuDispatch.DrawBuffer;
origScissor = stub.spuDispatch.Scissor;
stub.spuDispatch.Clear = trapClear;
stub.spuDispatch.Viewport = trapViewport;
/*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
}
crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
}
#if 0 /** @todo stubSPUTearDown & stubSPUTearDownLocked are not referenced */
// Callback function, used to destroy all created contexts
static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
{
(void)data1; (void)data2;
stubDestroyContext(key);
}
/**
* This is called when we exit.
* We call all the SPU's cleanup functions.
*/
static void stubSPUTearDownLocked(void)
{
crDebug("stubSPUTearDownLocked");
#ifdef WINDOWS
# ifndef CR_NEWWINTRACK
stubUninstallWindowMessageHook();
# endif
#endif
#ifdef CR_NEWWINTRACK
ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
#endif
//delete all created contexts
stubMakeCurrent( NULL, NULL);
/* the lock order is windowTable->contextTable (see wglMakeCurrent_prox, glXMakeCurrent)
* this is why we need to take a windowTable lock since we will later do stub.windowTable access & locking */
crHashtableLock(stub.windowTable);
crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
crHashtableUnlock(stub.windowTable);
/* shutdown, now trap any calls to a NULL dispatcher */
crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
crSPUUnloadChain(stub.spu);
stub.spu = NULL;
#ifndef Linux
crUnloadOpenGL();
#endif
#ifndef WINDOWS
crNetTearDown();
#endif
#ifdef GLX
if (stub.xshmSI.shmid>=0)
{
shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
shmdt(stub.xshmSI.shmaddr);
}
crFreeHashtable(stub.pGLXPixmapsHash, crFree);
#endif
crFreeHashtable(stub.windowTable, crFree);
crFreeHashtable(stub.contextTable, NULL);
crMemset(&stub, 0, sizeof(stub));
}
/**
* This is called when we exit.
* We call all the SPU's cleanup functions.
*/
static void stubSPUTearDown(void)
{
STUB_INIT_LOCK();
if (stub_initialized)
{
stubSPUTearDownLocked();
stub_initialized = 0;
}
STUB_INIT_UNLOCK();
}
#endif /** @todo stubSPUTearDown & stubSPUTearDownLocked are not referenced */
static void stubSPUSafeTearDown(void)
{
#ifdef CHROMIUM_THREADSAFE
CRmutex *mutex;
#endif
if (!stub_initialized) return;
stub_initialized = 0;
#ifdef CHROMIUM_THREADSAFE
mutex = &stub.mutex;
crLockMutex(mutex);
#endif
crDebug("stubSPUSafeTearDown");
#ifdef WINDOWS
# ifndef CR_NEWWINTRACK
stubUninstallWindowMessageHook();
# endif
#endif
#if defined(CR_NEWWINTRACK)
crUnlockMutex(mutex);
# if defined(WINDOWS)
if (stub.hSyncThread && RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
{
HANDLE hNative;
DWORD ec=0;
hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
false, RTThreadGetNative(stub.hSyncThread));
if (!hNative)
{
crWarning("Failed to get handle for sync thread(%#x)", GetLastError());
}
else
{
crDebug("Got handle %p for thread %#x", hNative, RTThreadGetNative(stub.hSyncThread));
}
ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
{
RTThreadWait(stub.hSyncThread, 1000, NULL);
/*Same issue as on linux, RTThreadWait exits before system thread is terminated, which leads
* to issues as our dll goes to be unloaded.
*@todo
*We usually call this function from DllMain which seems to be holding some lock and thus we have to
* kill thread via TerminateThread.
*/
if (WaitForSingleObject(hNative, 100)==WAIT_TIMEOUT)
{
crDebug("Wait failed, terminating");
if (!TerminateThread(hNative, 1))
{
crDebug("TerminateThread failed");
}
}
if (GetExitCodeThread(hNative, &ec))
{
crDebug("Thread %p exited with ec=%i", hNative, ec);
}
else
{
crDebug("GetExitCodeThread failed(%#x)", GetLastError());
}
}
else
{
crDebug("Sync thread killed before DLL_PROCESS_DETACH");
}
if (hNative)
{
CloseHandle(hNative);
}
}
#else
if (stub.hSyncThread!=NIL_RTTHREAD)
{
ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
{
int rc = RTThreadWait(stub.hSyncThread, RT_INDEFINITE_WAIT, NULL);
if (RT_FAILURE(rc))
{
WARN(("RTThreadWait_join failed %i", rc));
}
}
}
#endif
crLockMutex(mutex);
#endif
#ifndef WINDOWS
crNetTearDown();
#endif
#ifdef CHROMIUM_THREADSAFE
crUnlockMutex(mutex);
crFreeMutex(mutex);
#endif
crMemset(&stub, 0, sizeof(stub));
}
static void stubExitHandler(void)
{
stubSPUSafeTearDown();
signal(SIGTERM, SIG_DFL);
signal(SIGINT, SIG_DFL);
}
/**
* Called when we receive a SIGTERM signal.
*/
static void stubSignalHandler(int signo)
{
(void)signo;
stubSPUSafeTearDown();
exit(0); /* this causes stubExitHandler() to be called */
}
#ifndef RT_OS_WINDOWS
# ifdef CHROMIUM_THREADSAFE
static void stubThreadTlsDtor(void *pvValue)
{
ContextInfo *pCtx = (ContextInfo*)pvValue;
VBoxTlsRefRelease(pCtx);
}
# endif
#endif
/**
* Init variables in the stub structure, install signal handler.
*/
static void stubInitVars(void)
{
WindowInfo *defaultWin;
#ifdef CHROMIUM_THREADSAFE
crInitMutex(&stub.mutex);
#endif
/* At the very least we want CR_RGB_BIT. */
stub.haveNativeOpenGL = GL_FALSE;
stub.spu = NULL;
stub.appDrawCursor = 0;
stub.minChromiumWindowWidth = 0;
stub.minChromiumWindowHeight = 0;
stub.maxChromiumWindowWidth = 0;
stub.maxChromiumWindowHeight = 0;
stub.matchChromiumWindowCount = 0;
stub.matchChromiumWindowID = NULL;
stub.matchWindowTitle = NULL;
stub.ignoreFreeglutMenus = 0;
stub.threadSafe = GL_FALSE;
stub.trackWindowSize = 0;
stub.trackWindowPos = 0;
stub.trackWindowVisibility = 0;
stub.trackWindowVisibleRgn = 0;
stub.mothershipPID = 0;
stub.spu_dir = NULL;
stub.freeContextNumber = MAGIC_CONTEXT_BASE;
stub.contextTable = crAllocHashtable();
#ifndef RT_OS_WINDOWS
# ifdef CHROMIUM_THREADSAFE
if (!g_stubIsCurrentContextTSDInited)
{
crInitTSDF(&g_stubCurrentContextTSD, stubThreadTlsDtor);
g_stubIsCurrentContextTSDInited = true;
}
# endif
#endif
stubSetCurrentContext(NULL);
stub.windowTable = crAllocHashtable();
#ifdef CR_NEWWINTRACK
stub.bShutdownSyncThread = false;
stub.hSyncThread = NIL_RTTHREAD;
#endif
defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
defaultWin->type = CHROMIUM;
defaultWin->spuWindow = 0; /* window 0 always exists */
#ifdef WINDOWS
defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
#elif defined(GLX)
defaultWin->pVisibleRegions = NULL;
defaultWin->cVisibleRegions = 0;
#endif
crHashtableAdd(stub.windowTable, 0, defaultWin);
#if 1
atexit(stubExitHandler);
signal(SIGTERM, stubSignalHandler);
signal(SIGINT, stubSignalHandler);
#ifndef WINDOWS
signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
#endif
#else
(void) stubExitHandler;
(void) stubSignalHandler;
#endif
}
#if 0 /* unused */
/**
* Return a free port number for the mothership to use, or -1 if we
* can't find one.
*/
static int
GenerateMothershipPort(void)
{
const int MAX_PORT = 10100;
unsigned short port;
/* generate initial port number randomly */
crRandAutoSeed();
port = (unsigned short) crRandInt(10001, MAX_PORT);
#ifdef WINDOWS
/* XXX should implement a free port check here */
return port;
#else
/*
* See if this port number really is free, try another if needed.
*/
{
struct sockaddr_in servaddr;
int so_reuseaddr = 1;
int sock, k;
/* create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
CRASSERT(sock > 2);
/* deallocate socket/port when we exit */
k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *) &so_reuseaddr, sizeof(so_reuseaddr));
CRASSERT(k == 0);
/* initialize the servaddr struct */
crMemset(&servaddr, 0, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
while (port < MAX_PORT) {
/* Bind to the given port number, return -1 if we fail */
servaddr.sin_port = htons((unsigned short) port);
k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (k) {
/* failed to create port. try next one. */
port++;
}
else {
/* free the socket/port now so mothership can make it */
close(sock);
return port;
}
}
}
#endif /* WINDOWS */
return -1;
}
/**
* Try to determine which mothership configuration to use for this program.
*/
static char **
LookupMothershipConfig(const char *procName)
{
const int procNameLen = crStrlen(procName);
FILE *f;
const char *home;
char configPath[1000];
/* first, check if the CR_CONFIG env var is set */
{
const char *conf = crGetenv("CR_CONFIG");
if (conf && crStrlen(conf) > 0)
return crStrSplit(conf, " ");
}
/* second, look up config name from config file */
home = crGetenv("HOME");
if (home)
sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
else
crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
/* Check if the CR_CONFIG_PATH env var is set. */
{
const char *conf = crGetenv("CR_CONFIG_PATH");
if (conf)
crStrcpy(configPath, conf); /* from env var */
}
f = fopen(configPath, "r");
if (!f) {
return NULL;
}
while (!feof(f)) {
char line[1000];
char **args;
fgets(line, 999, f);
line[crStrlen(line) - 1] = 0; /* remove trailing newline */
if (crStrncmp(line, procName, procNameLen) == 0 &&
(line[procNameLen] == ' ' || line[procNameLen] == '\t'))
{
crWarning("Using Chromium configuration for %s from %s",
procName, configPath);
args = crStrSplit(line + procNameLen + 1, " ");
return args;
}
}
fclose(f);
return NULL;
}
static int Mothership_Awake = 0;
/**
* Signal handler to determine when mothership is ready.
*/
static void
MothershipPhoneHome(int signo)
{
crDebug("Got signal %d: mothership is awake!", signo);
Mothership_Awake = 1;
}
#endif /* 0 */
static void stubSetDefaultConfigurationOptions(void)
{
unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
stub.appDrawCursor = 0;
stub.minChromiumWindowWidth = 0;
stub.minChromiumWindowHeight = 0;
stub.maxChromiumWindowWidth = 0;
stub.maxChromiumWindowHeight = 0;
stub.matchChromiumWindowID = NULL;
stub.numIgnoreWindowID = 0;
stub.matchWindowTitle = NULL;
stub.ignoreFreeglutMenus = 0;
stub.trackWindowSize = 1;
stub.trackWindowPos = 1;
stub.trackWindowVisibility = 1;
stub.trackWindowVisibleRgn = 1;
stub.matchChromiumWindowCount = 0;
stub.spu_dir = NULL;
crNetSetRank(0);
crNetSetContextRange(32, 35);
crNetSetNodeRange("iam0", "iamvis20");
crNetSetKey(key,sizeof(key));
stub.force_pbuffers = 0;
#ifdef WINDOWS
# ifdef VBOX_WITH_WDDM
stub.bRunningUnderWDDM = false;
# endif
#endif
}
#ifdef CR_NEWWINTRACK
# ifdef VBOX_WITH_WDDM
static void stubDispatchVisibleRegions(WindowInfo *pWindow)
{
DWORD dwCount;
LPRGNDATA lpRgnData;
dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
lpRgnData = crAlloc(dwCount);
if (lpRgnData)
{
GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
crFree(lpRgnData);
}
else crWarning("GetRegionData failed, VisibleRegions update failed");
}
# endif /* VBOX_WITH_WDDM */
static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
{
WindowInfo *pWindow = (WindowInfo *) data1;
(void)key; (void) data2;
if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
{
return;
}
stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
if (!stubSystemWindowExist(pWindow))
{
#ifdef WINDOWS
stubDestroyWindow(0, (GLint)pWindow->hWnd);
#else
stubDestroyWindow(0, (GLint)pWindow->drawable);
#endif
/*No need to flush here as crWindowDestroy does it*/
return;
}
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
if (stub.bRunningUnderWDDM)
return;
#endif
stubCheckWindowState(pWindow, GL_TRUE);
}
static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
{
#ifdef WINDOWS
MSG msg;
# ifdef VBOX_WITH_WDDM
HMODULE hVBoxD3D = NULL;
GLint spuConnection = 0;
# endif
#endif
(void) pvUser;
crDebug("Sync thread started");
#ifdef WINDOWS
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
# ifdef VBOX_WITH_WDDM
hVBoxD3D = NULL;
if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
{
crDebug("GetModuleHandleEx failed err %d", GetLastError());
hVBoxD3D = NULL;
}
if (hVBoxD3D)
{
crDebug("running with " VBOX_MODNAME_DISPD3D);
stub.trackWindowVisibleRgn = 0;
stub.bRunningUnderWDDM = true;
}
# endif /* VBOX_WITH_WDDM */
#endif /* WINDOWS */
crLockMutex(&stub.mutex);
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
spuConnection =
#endif
stub.spu->dispatch_table.VBoxPackSetInjectThread(NULL);
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
if (stub.bRunningUnderWDDM && !spuConnection)
{
crError("VBoxPackSetInjectThread failed!");
}
#endif
crUnlockMutex(&stub.mutex);
RTThreadUserSignal(ThreadSelf);
while(!stub.bShutdownSyncThread)
{
#ifdef WINDOWS
if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
# ifdef VBOX_WITH_WDDM
if (stub.bRunningUnderWDDM)
{
}
else
# endif
{
crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
RTThreadSleep(50);
}
}
else
{
if (WM_QUIT==msg.message)
{
crDebug("Sync thread got WM_QUIT");
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
#else
/* Try to keep a consistent locking order. */
crHashtableLock(stub.windowTable);
crLockMutex(&stub.mutex);
crHashtableWalkUnlocked(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
crUnlockMutex(&stub.mutex);
crHashtableUnlock(stub.windowTable);
RTThreadSleep(50);
#endif
}
#ifdef VBOX_WITH_WDDM
if (spuConnection)
{
stub.spu->dispatch_table.VBoxConDestroy(spuConnection);
}
if (hVBoxD3D)
{
FreeLibrary(hVBoxD3D);
}
#endif
crDebug("Sync thread stopped");
return 0;
}
#endif /* CR_NEWWINTRACK */
/**
* Do one-time initializations for the faker.
* Returns TRUE on success, FALSE otherwise.
*/
static bool
stubInitLocked(void)
{
/* Here is where we contact the mothership to find out what we're supposed
* to be doing. Networking code in a DLL initializer. I sure hope this
* works :)
*
* HOW can I pass the mothership address to this if I already know it?
*/
char response[1024];
char **spuchain;
int num_spus;
int *spu_ids;
char **spu_names;
const char *app_id;
int i;
int disable_sync = 0;
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
HMODULE hVBoxD3D = NULL;
#endif
stubInitVars();
crGetProcName(response, 1024);
crDebug("Stub launched for %s", response);
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
/** @todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread
* as at the start compiz runs our code under XGrabServer.
*/
if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real") || !crStrcmp(response, "compiz.real")
|| !crStrcmp(response, "compiz-bin"))
{
disable_sync = 1;
}
#endif
/** @todo check if it'd be of any use on other than guests, no use for windows */
app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
crNetInit( NULL, NULL );
#ifndef WINDOWS
{
CRNetServer ns;
ns.name = "vboxhgcm://host:0";
ns.buffer_size = 1024;
crNetServerConnect(&ns
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, NULL
#endif
);
if (!ns.conn)
{
crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
# ifdef VBOXOGL_FAKEDRI
return false;
# else
exit(1);
# endif
}
else
{
crNetFreeConnection(ns.conn);
}
}
#endif
strcpy(response, "2 0 feedback 1 pack");
spuchain = crStrSplit( response, " " );
num_spus = crStrToInt( spuchain[0] );
spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
for (i = 0 ; i < num_spus ; i++)
{
spu_ids[i] = crStrToInt( spuchain[2*i+1] );
spu_names[i] = crStrdup( spuchain[2*i+2] );
crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
}
stubSetDefaultConfigurationOptions();
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
hVBoxD3D = NULL;
if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
{
crDebug("GetModuleHandleEx failed err %d", GetLastError());
hVBoxD3D = NULL;
}
if (hVBoxD3D)
{
disable_sync = 1;
crDebug("running with %s", VBOX_MODNAME_DISPD3D);
stub.trackWindowVisibleRgn = 0;
/** @todo should we enable that? */
stub.trackWindowSize = 0;
stub.trackWindowPos = 0;
stub.trackWindowVisibility = 0;
stub.bRunningUnderWDDM = true;
}
#endif
stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
crFree( spuchain );
crFree( spu_ids );
for (i = 0; i < num_spus; ++i)
crFree(spu_names[i]);
crFree( spu_names );
// spu chain load failed somewhere
if (!stub.spu) {
return false;
}
crSPUInitDispatchTable( &glim );
/* This is unlikely to change -- We still want to initialize our dispatch
* table with the functions of the first SPU in the chain. */
stubInitSPUDispatch( stub.spu );
/* we need to plug one special stub function into the dispatch table */
glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
#if !defined(VBOX_NO_NATIVEGL)
/* Load pointers to native OpenGL functions into stub.nativeDispatch */
stubInitNativeDispatch();
#endif
/*crDebug("stub init");
raise(SIGINT);*/
#ifdef WINDOWS
# ifndef CR_NEWWINTRACK
stubInstallWindowMessageHook();
# endif
#endif
#ifdef CR_NEWWINTRACK
{
int rc;
RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
if (!disable_sync)
{
crDebug("Starting sync thread");
rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
if (RT_FAILURE(rc))
{
crError("Failed to start sync thread! (%x)", rc);
}
RTThreadUserWait(stub.hSyncThread, 60 * 1000);
RTThreadUserReset(stub.hSyncThread);
crDebug("Going on");
}
}
#endif
#ifdef GLX
stub.xshmSI.shmid = -1;
stub.bShmInitFailed = GL_FALSE;
stub.pGLXPixmapsHash = crAllocHashtable();
stub.bXExtensionsChecked = GL_FALSE;
stub.bHaveXComposite = GL_FALSE;
stub.bHaveXFixes = GL_FALSE;
#endif
return true;
}
/**
* Do one-time initializations for the faker.
* Returns TRUE on success, FALSE otherwise.
*/
bool
stubInit(void)
{
bool bRc = true;
/* we need to serialize the initialization, otherwise racing is possible
* for XPDM-based d3d when a d3d switcher is testing the gl lib in two or more threads
* NOTE: the STUB_INIT_LOCK/UNLOCK is a NOP for non-win currently */
STUB_INIT_LOCK();
if (!stub_initialized)
bRc = stub_initialized = stubInitLocked();
STUB_INIT_UNLOCK();
return bRc;
}
/* Sigh -- we can't do initialization at load time, since Windows forbids
* the loading of other libraries from DLLMain. */
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#if 1//def DEBUG_misha
/* debugging: this is to be able to catch first-chance notifications
* for exceptions other than EXCEPTION_BREAKPOINT in kernel debugger */
# define VDBG_VEHANDLER
#endif
#ifdef VDBG_VEHANDLER
# include <dbghelp.h>
# include <cr_string.h>
static PVOID g_VBoxVehHandler = NULL;
static DWORD g_VBoxVehEnable = 0;
/* generate a crash dump on exception */
#define VBOXVEH_F_DUMP 0x00000001
/* generate a debugger breakpoint exception */
#define VBOXVEH_F_BREAK 0x00000002
/* exit on exception */
#define VBOXVEH_F_EXIT 0x00000004
static DWORD g_VBoxVehFlags = 0;
typedef BOOL WINAPI FNVBOXDBG_MINIDUMPWRITEDUMP(HANDLE hProcess,
DWORD ProcessId,
HANDLE hFile,
MINIDUMP_TYPE DumpType,
PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
typedef FNVBOXDBG_MINIDUMPWRITEDUMP *PFNVBOXDBG_MINIDUMPWRITEDUMP;
static HMODULE g_hVBoxMdDbgHelp = NULL;
static PFNVBOXDBG_MINIDUMPWRITEDUMP g_pfnVBoxMdMiniDumpWriteDump = NULL;
static size_t g_cVBoxMdFilePrefixLen = 0;
static WCHAR g_aszwVBoxMdFilePrefix[MAX_PATH];
static WCHAR g_aszwVBoxMdDumpCount = 0;
static MINIDUMP_TYPE g_enmVBoxMdDumpType = MiniDumpNormal
| MiniDumpWithDataSegs
| MiniDumpWithFullMemory
| MiniDumpWithHandleData
//// | MiniDumpFilterMemory
//// | MiniDumpScanMemory
// | MiniDumpWithUnloadedModules
//// | MiniDumpWithIndirectlyReferencedMemory
//// | MiniDumpFilterModulePaths
// | MiniDumpWithProcessThreadData
// | MiniDumpWithPrivateReadWriteMemory
//// | MiniDumpWithoutOptionalData
// | MiniDumpWithFullMemoryInfo
// | MiniDumpWithThreadInfo
// | MiniDumpWithCodeSegs
// | MiniDumpWithFullAuxiliaryState
// | MiniDumpWithPrivateWriteCopyMemory
// | MiniDumpIgnoreInaccessibleMemory
// | MiniDumpWithTokenInformation
//// | MiniDumpWithModuleHeaders
//// | MiniDumpFilterTriage
;
#define VBOXMD_DUMP_DIR_DEFAULT "C:\\dumps"
#define VBOXMD_DUMP_NAME_PREFIX_W L"VBoxDmp_"
static HMODULE loadSystemDll(const char *pszName)
{
#ifndef DEBUG
char szPath[MAX_PATH];
UINT cchPath = GetSystemDirectoryA(szPath, sizeof(szPath));
size_t cbName = strlen(pszName) + 1;
if (cchPath + 1 + cbName > sizeof(szPath))
{
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return NULL;
}
szPath[cchPath] = '\\';
memcpy(&szPath[cchPath + 1], pszName, cbName);
return LoadLibraryA(szPath);
#else
return LoadLibraryA(pszName);
#endif
}
static DWORD vboxMdMinidumpCreate(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
WCHAR aszwMdFileName[MAX_PATH];
HANDLE hProcess = GetCurrentProcess();
DWORD ProcessId = GetCurrentProcessId();
MINIDUMP_EXCEPTION_INFORMATION ExceptionInfo;
HANDLE hFile;
DWORD winErr = ERROR_SUCCESS;
if (!g_pfnVBoxMdMiniDumpWriteDump)
{
if (!g_hVBoxMdDbgHelp)
{
g_hVBoxMdDbgHelp = loadSystemDll("DbgHelp.dll");
if (!g_hVBoxMdDbgHelp)
return GetLastError();
}
g_pfnVBoxMdMiniDumpWriteDump = (PFNVBOXDBG_MINIDUMPWRITEDUMP)GetProcAddress(g_hVBoxMdDbgHelp, "MiniDumpWriteDump");
if (!g_pfnVBoxMdMiniDumpWriteDump)
return GetLastError();
}
++g_aszwVBoxMdDumpCount;
memcpy(aszwMdFileName, g_aszwVBoxMdFilePrefix, g_cVBoxMdFilePrefixLen * sizeof (g_aszwVBoxMdFilePrefix[0]));
swprintf(aszwMdFileName + g_cVBoxMdFilePrefixLen, RT_ELEMENTS(aszwMdFileName) - g_cVBoxMdFilePrefixLen, L"%d_%d.dmp", ProcessId, g_aszwVBoxMdDumpCount);
hFile = CreateFileW(aszwMdFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return GetLastError();
ExceptionInfo.ThreadId = GetCurrentThreadId();
ExceptionInfo.ExceptionPointers = pExceptionInfo;
ExceptionInfo.ClientPointers = FALSE;
if (!g_pfnVBoxMdMiniDumpWriteDump(hProcess, ProcessId, hFile, g_enmVBoxMdDumpType, &ExceptionInfo, NULL, NULL))
winErr = GetLastError();
CloseHandle(hFile);
return winErr;
}
LONG WINAPI vboxVDbgVectoredHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
PCONTEXT pContextRecord = pExceptionInfo->ContextRecord;
switch (pExceptionRecord->ExceptionCode)
{
case EXCEPTION_BREAKPOINT:
case EXCEPTION_ACCESS_VIOLATION:
case EXCEPTION_STACK_OVERFLOW:
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_ILLEGAL_INSTRUCTION:
if (g_VBoxVehFlags & VBOXVEH_F_BREAK)
{
BOOL fBreak = TRUE;
#ifndef DEBUG_misha
if (pExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
{
HANDLE hProcess = GetCurrentProcess();
BOOL fDebuggerPresent = FALSE;
/* we do not want to generate breakpoint exceptions recursively, so do it only when running under debugger */
if (CheckRemoteDebuggerPresent(hProcess, &fDebuggerPresent))
fBreak = !!fDebuggerPresent;
else
fBreak = FALSE; /* <- the function has failed, don't break for sanity */
}
#endif
if (fBreak)
{
RT_BREAKPOINT();
}
}
if (g_VBoxVehFlags & VBOXVEH_F_DUMP)
vboxMdMinidumpCreate(pExceptionInfo);
if (g_VBoxVehFlags & VBOXVEH_F_EXIT)
exit(1);
break;
default:
break;
}
return EXCEPTION_CONTINUE_SEARCH;
}
void vboxVDbgVEHandlerRegister()
{
CRASSERT(!g_VBoxVehHandler);
g_VBoxVehHandler = AddVectoredExceptionHandler(1,vboxVDbgVectoredHandler);
CRASSERT(g_VBoxVehHandler);
}
void vboxVDbgVEHandlerUnregister()
{
ULONG uResult;
if (g_VBoxVehHandler)
{
uResult = RemoveVectoredExceptionHandler(g_VBoxVehHandler);
CRASSERT(uResult);
g_VBoxVehHandler = NULL;
}
}
#endif
/* Windows crap */
BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
{
(void) lpvReserved;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
CRNetServer ns;
const char * env;
#if defined(DEBUG_misha)
HMODULE hCrUtil;
char aName[MAX_PATH];
GetModuleFileNameA(hDLLInst, aName, RT_ELEMENTS(aName));
crDbgCmdSymLoadPrint(aName, hDLLInst);
hCrUtil = GetModuleHandleA("VBoxOGLcrutil.dll");
Assert(hCrUtil);
crDbgCmdSymLoadPrint("VBoxOGLcrutil.dll", hCrUtil);
#endif
#ifdef CHROMIUM_THREADSAFE
crInitTSD(&g_stubCurrentContextTSD);
#endif
crInitMutex(&stub_init_mutex);
#ifdef VDBG_VEHANDLER
env = crGetenv("CR_DBG_VEH_ENABLE");
g_VBoxVehEnable = crStrParseI32(env,
# ifdef DEBUG_misha
1
# else
0
# endif
);
if (g_VBoxVehEnable)
{
char procName[1024];
size_t cProcName;
size_t cChars;
env = crGetenv("CR_DBG_VEH_FLAGS");
g_VBoxVehFlags = crStrParseI32(env,
0
# ifdef DEBUG_misha
| VBOXVEH_F_BREAK
# else
| VBOXVEH_F_DUMP
# endif
);
env = crGetenv("CR_DBG_VEH_DUMP_DIR");
if (!env)
env = VBOXMD_DUMP_DIR_DEFAULT;
g_cVBoxMdFilePrefixLen = strlen(env);
if (RT_ELEMENTS(g_aszwVBoxMdFilePrefix) <= g_cVBoxMdFilePrefixLen + 26 + (sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR)) / sizeof (WCHAR))
{
g_cVBoxMdFilePrefixLen = 0;
env = "";
}
mbstowcs_s(&cChars, g_aszwVBoxMdFilePrefix, g_cVBoxMdFilePrefixLen + 1, env, _TRUNCATE);
Assert(cChars == g_cVBoxMdFilePrefixLen + 1);
g_cVBoxMdFilePrefixLen = cChars - 1;
if (g_cVBoxMdFilePrefixLen && g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen - 1] != L'\\')
g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen++] = L'\\';
memcpy(g_aszwVBoxMdFilePrefix + g_cVBoxMdFilePrefixLen, VBOXMD_DUMP_NAME_PREFIX_W, sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR));
g_cVBoxMdFilePrefixLen += (sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR)) / sizeof (WCHAR);
crGetProcName(procName, RT_ELEMENTS(procName));
cProcName = strlen(procName);
if (RT_ELEMENTS(g_aszwVBoxMdFilePrefix) > g_cVBoxMdFilePrefixLen + cProcName + 1 + 26)
{
mbstowcs_s(&cChars, g_aszwVBoxMdFilePrefix + g_cVBoxMdFilePrefixLen, cProcName + 1, procName, _TRUNCATE);
Assert(cChars == cProcName + 1);
g_cVBoxMdFilePrefixLen += cChars - 1;
g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen++] = L'_';
}
/* sanity */
g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen] = L'\0';
env = crGetenv("CR_DBG_VEH_DUMP_TYPE");
g_enmVBoxMdDumpType = crStrParseI32(env,
MiniDumpNormal
| MiniDumpWithDataSegs
| MiniDumpWithFullMemory
| MiniDumpWithHandleData
//// | MiniDumpFilterMemory
//// | MiniDumpScanMemory
// | MiniDumpWithUnloadedModules
//// | MiniDumpWithIndirectlyReferencedMemory
//// | MiniDumpFilterModulePaths
// | MiniDumpWithProcessThreadData
// | MiniDumpWithPrivateReadWriteMemory
//// | MiniDumpWithoutOptionalData
// | MiniDumpWithFullMemoryInfo
// | MiniDumpWithThreadInfo
// | MiniDumpWithCodeSegs
// | MiniDumpWithFullAuxiliaryState
// | MiniDumpWithPrivateWriteCopyMemory
// | MiniDumpIgnoreInaccessibleMemory
// | MiniDumpWithTokenInformation
//// | MiniDumpWithModuleHeaders
//// | MiniDumpFilterTriage
);
vboxVDbgVEHandlerRegister();
}
#endif
crNetInit(NULL, NULL);
ns.name = "vboxhgcm://host:0";
ns.buffer_size = 1024;
crNetServerConnect(&ns
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
, NULL
#endif
);
if (!ns.conn)
{
crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
#ifdef VDBG_VEHANDLER
if (g_VBoxVehEnable)
vboxVDbgVEHandlerUnregister();
#endif
return FALSE;
}
else
{
crNetFreeConnection(ns.conn);
}
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
VBoxCrHgsmiInit();
#endif
break;
}
case DLL_PROCESS_DETACH:
{
/* do exactly the same thing as for DLL_THREAD_DETACH since
* DLL_THREAD_DETACH is not called for the thread doing DLL_PROCESS_DETACH according to msdn docs */
stubSetCurrentContext(NULL);
if (stub_initialized)
{
CRASSERT(stub.spu);
stub.spu->dispatch_table.VBoxDetachThread();
}
#if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
VBoxCrHgsmiTerm();
#endif
stubSPUSafeTearDown();
#ifdef CHROMIUM_THREADSAFE
crFreeTSD(&g_stubCurrentContextTSD);
#endif
#ifdef VDBG_VEHANDLER
if (g_VBoxVehEnable)
vboxVDbgVEHandlerUnregister();
#endif
break;
}
case DLL_THREAD_ATTACH:
{
if (stub_initialized)
{
CRASSERT(stub.spu);
stub.spu->dispatch_table.VBoxAttachThread();
}
break;
}
case DLL_THREAD_DETACH:
{
stubSetCurrentContext(NULL);
if (stub_initialized)
{
CRASSERT(stub.spu);
stub.spu->dispatch_table.VBoxDetachThread();
}
break;
}
default:
break;
}
return TRUE;
}
#endif
|