[15532] | 1 | /* Copyright (c) 2001, Stanford University
|
---|
| 2 | * All rights reserved
|
---|
| 3 | *
|
---|
| 4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include "cr_spu.h"
|
---|
| 8 | #include "cr_net.h"
|
---|
| 9 | #include "cr_error.h"
|
---|
| 10 | #include "cr_mem.h"
|
---|
| 11 | #include "cr_string.h"
|
---|
| 12 | #include "cr_net.h"
|
---|
| 13 | #include "cr_environment.h"
|
---|
| 14 | #include "cr_process.h"
|
---|
| 15 | #include "cr_rand.h"
|
---|
[17921] | 16 | #include "cr_netserver.h"
|
---|
[15532] | 17 | #include "stub.h"
|
---|
| 18 | #include <stdlib.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | #include <signal.h>
|
---|
[30440] | 21 | #include <iprt/initterm.h>
|
---|
| 22 | #include <iprt/thread.h>
|
---|
| 23 | #include <iprt/err.h>
|
---|
| 24 | #include <iprt/asm.h>
|
---|
[15532] | 25 | #ifndef WINDOWS
|
---|
[30440] | 26 | # include <sys/types.h>
|
---|
| 27 | # include <unistd.h>
|
---|
[15532] | 28 | #endif
|
---|
| 29 |
|
---|
[30513] | 30 | #ifdef VBOX_WITH_WDDM
|
---|
| 31 | #include <d3d9types.h>
|
---|
| 32 | #include <D3dumddi.h>
|
---|
[36848] | 33 | #include "../../WINNT/Graphics/Video/common/wddm/VBoxMPIf.h"
|
---|
[30513] | 34 | #endif
|
---|
| 35 |
|
---|
[62847] | 36 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
| 37 | # include <VBox/VBoxCrHgsmi.h>
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
[15532] | 40 | /**
|
---|
| 41 | * If you change this, see the comments in tilesortspu_context.c
|
---|
| 42 | */
|
---|
| 43 | #define MAGIC_CONTEXT_BASE 500
|
---|
| 44 |
|
---|
| 45 | #define CONFIG_LOOKUP_FILE ".crconfigs"
|
---|
| 46 |
|
---|
| 47 | #ifdef WINDOWS
|
---|
| 48 | #define PYTHON_EXE "python.exe"
|
---|
| 49 | #else
|
---|
| 50 | #define PYTHON_EXE "python"
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
[40281] | 53 | static bool stub_initialized = 0;
|
---|
[39485] | 54 | #ifdef WINDOWS
|
---|
| 55 | static CRmutex stub_init_mutex;
|
---|
| 56 | #define STUB_INIT_LOCK() do { crLockMutex(&stub_init_mutex); } while (0)
|
---|
| 57 | #define STUB_INIT_UNLOCK() do { crUnlockMutex(&stub_init_mutex); } while (0)
|
---|
| 58 | #else
|
---|
| 59 | #define STUB_INIT_LOCK() do { } while (0)
|
---|
| 60 | #define STUB_INIT_UNLOCK() do { } while (0)
|
---|
| 61 | #endif
|
---|
[15532] | 62 |
|
---|
| 63 | /* NOTE: 'SPUDispatchTable glim' is declared in NULLfuncs.py now */
|
---|
| 64 | /* NOTE: 'SPUDispatchTable stubThreadsafeDispatch' is declared in tsfuncs.c */
|
---|
| 65 | Stub stub;
|
---|
[39568] | 66 | #ifdef CHROMIUM_THREADSAFE
|
---|
[39602] | 67 | static bool g_stubIsCurrentContextTSDInited;
|
---|
[39568] | 68 | CRtsd g_stubCurrentContextTSD;
|
---|
| 69 | #endif
|
---|
[15532] | 70 |
|
---|
| 71 |
|
---|
[63204] | 72 | #ifndef VBOX_NO_NATIVEGL
|
---|
[15532] | 73 | static void stubInitNativeDispatch( void )
|
---|
| 74 | {
|
---|
[63204] | 75 | # define MAX_FUNCS 1000
|
---|
[15532] | 76 | SPUNamedFunctionTable gl_funcs[MAX_FUNCS];
|
---|
| 77 | int numFuncs;
|
---|
| 78 |
|
---|
| 79 | numFuncs = crLoadOpenGL( &stub.wsInterface, gl_funcs );
|
---|
| 80 |
|
---|
| 81 | stub.haveNativeOpenGL = (numFuncs > 0);
|
---|
| 82 |
|
---|
| 83 | /* XXX call this after context binding */
|
---|
| 84 | numFuncs += crLoadOpenGLExtensions( &stub.wsInterface, gl_funcs + numFuncs );
|
---|
| 85 |
|
---|
| 86 | CRASSERT(numFuncs < MAX_FUNCS);
|
---|
| 87 |
|
---|
| 88 | crSPUInitDispatchTable( &stub.nativeDispatch );
|
---|
| 89 | crSPUInitDispatch( &stub.nativeDispatch, gl_funcs );
|
---|
| 90 | crSPUInitDispatchNops( &stub.nativeDispatch );
|
---|
[63204] | 91 | # undef MAX_FUNCS
|
---|
[15532] | 92 | }
|
---|
[63204] | 93 | #endif /* !VBOX_NO_NATIVEGL */
|
---|
[15532] | 94 |
|
---|
| 95 |
|
---|
| 96 | /** Pointer to the SPU's real glClear and glViewport functions */
|
---|
| 97 | static ClearFunc_t origClear;
|
---|
| 98 | static ViewportFunc_t origViewport;
|
---|
[16718] | 99 | static SwapBuffersFunc_t origSwapBuffers;
|
---|
| 100 | static DrawBufferFunc_t origDrawBuffer;
|
---|
[20029] | 101 | static ScissorFunc_t origScissor;
|
---|
[15532] | 102 |
|
---|
[30440] | 103 | static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
|
---|
[15532] | 104 | {
|
---|
| 105 | bool bForceUpdate = false;
|
---|
[30440] | 106 | bool bChanged = false;
|
---|
[15532] | 107 |
|
---|
| 108 | #ifdef WINDOWS
|
---|
| 109 | /* @todo install hook and track for WM_DISPLAYCHANGE */
|
---|
| 110 | {
|
---|
| 111 | DEVMODE devMode;
|
---|
| 112 |
|
---|
| 113 | devMode.dmSize = sizeof(DEVMODE);
|
---|
| 114 | EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
|
---|
[38636] | 115 |
|
---|
[15532] | 116 | if (devMode.dmPelsWidth!=window->dmPelsWidth || devMode.dmPelsHeight!=window->dmPelsHeight)
|
---|
| 117 | {
|
---|
| 118 | crDebug("Resolution changed(%d,%d), forcing window Pos/Size update", devMode.dmPelsWidth, devMode.dmPelsHeight);
|
---|
| 119 | window->dmPelsWidth = devMode.dmPelsWidth;
|
---|
| 120 | window->dmPelsHeight = devMode.dmPelsHeight;
|
---|
| 121 | bForceUpdate = true;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | #endif
|
---|
| 125 |
|
---|
[30440] | 126 | bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
|
---|
[15532] | 127 |
|
---|
[16480] | 128 | #if defined(GLX) || defined (WINDOWS)
|
---|
[15532] | 129 | if (stub.trackWindowVisibleRgn)
|
---|
| 130 | {
|
---|
[30440] | 131 | bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
|
---|
[15532] | 132 | }
|
---|
| 133 | #endif
|
---|
[16456] | 134 |
|
---|
| 135 | if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
|
---|
| 136 | const int mapped = stubIsWindowVisible(window);
|
---|
| 137 | if (mapped != window->mapped) {
|
---|
| 138 | crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
|
---|
| 139 | stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
|
---|
| 140 | window->mapped = mapped;
|
---|
[30440] | 141 | bChanged = true;
|
---|
[16456] | 142 | }
|
---|
| 143 | }
|
---|
[30440] | 144 |
|
---|
| 145 | if (bFlushOnChange && bChanged)
|
---|
| 146 | {
|
---|
| 147 | stub.spu->dispatch_table.Flush();
|
---|
| 148 | }
|
---|
[15532] | 149 | }
|
---|
| 150 |
|
---|
[26909] | 151 | static bool stubSystemWindowExist(WindowInfo *pWindow)
|
---|
| 152 | {
|
---|
| 153 | #ifdef WINDOWS
|
---|
[34295] | 154 | if (pWindow->hWnd!=WindowFromDC(pWindow->drawable))
|
---|
[26909] | 155 | {
|
---|
| 156 | return false;
|
---|
| 157 | }
|
---|
| 158 | #else
|
---|
| 159 | Window root;
|
---|
| 160 | int x, y;
|
---|
| 161 | unsigned int border, depth, w, h;
|
---|
[30474] | 162 | Display *dpy;
|
---|
[15532] | 163 |
|
---|
[30474] | 164 | dpy = stubGetWindowDisplay(pWindow);
|
---|
| 165 |
|
---|
| 166 | XLOCK(dpy);
|
---|
| 167 | if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
|
---|
[26909] | 168 | {
|
---|
[30474] | 169 | XUNLOCK(dpy);
|
---|
[26909] | 170 | return false;
|
---|
| 171 | }
|
---|
[30474] | 172 | XUNLOCK(dpy);
|
---|
[26909] | 173 | #endif
|
---|
| 174 |
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | static void stubCheckWindowsCB(unsigned long key, void *data1, void *data2)
|
---|
| 179 | {
|
---|
| 180 | WindowInfo *pWindow = (WindowInfo *) data1;
|
---|
| 181 | ContextInfo *pCtx = (ContextInfo *) data2;
|
---|
[63204] | 182 | (void)key;
|
---|
[26909] | 183 |
|
---|
| 184 | if (pWindow == pCtx->currentDrawable
|
---|
| 185 | || pWindow->type!=CHROMIUM
|
---|
| 186 | || pWindow->pOwner!=pCtx)
|
---|
| 187 | {
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (!stubSystemWindowExist(pWindow))
|
---|
| 192 | {
|
---|
| 193 | #ifdef WINDOWS
|
---|
[42518] | 194 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->hWnd);
|
---|
[26909] | 195 | #else
|
---|
[42518] | 196 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->drawable);
|
---|
[26909] | 197 | #endif
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[30440] | 201 | stubCheckWindowState(pWindow, GL_FALSE);
|
---|
[26909] | 202 | }
|
---|
| 203 |
|
---|
| 204 | static void stubCheckWindowsState(void)
|
---|
| 205 | {
|
---|
[39568] | 206 | ContextInfo *context = stubGetCurrentContext();
|
---|
| 207 |
|
---|
[26909] | 208 | CRASSERT(stub.trackWindowSize || stub.trackWindowPos);
|
---|
| 209 |
|
---|
[39568] | 210 | if (!context)
|
---|
[26909] | 211 | return;
|
---|
| 212 |
|
---|
[34295] | 213 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
| 214 | if (stub.bRunningUnderWDDM)
|
---|
| 215 | return;
|
---|
| 216 | #endif
|
---|
| 217 |
|
---|
[49470] | 218 | /* Try to keep a consistent locking order. */
|
---|
| 219 | crHashtableLock(stub.windowTable);
|
---|
[30440] | 220 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
| 221 | crLockMutex(&stub.mutex);
|
---|
| 222 | #endif
|
---|
[26909] | 223 |
|
---|
[39568] | 224 | stubCheckWindowState(context->currentDrawable, GL_TRUE);
|
---|
[49470] | 225 | crHashtableWalkUnlocked(stub.windowTable, stubCheckWindowsCB, context);
|
---|
[30440] | 226 |
|
---|
| 227 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
| 228 | crUnlockMutex(&stub.mutex);
|
---|
| 229 | #endif
|
---|
[49470] | 230 | crHashtableUnlock(stub.windowTable);
|
---|
[26909] | 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
[15532] | 234 | /**
|
---|
| 235 | * Override the head SPU's glClear function.
|
---|
| 236 | * We're basically trapping this function so that we can poll the
|
---|
| 237 | * application window size at a regular interval.
|
---|
| 238 | */
|
---|
| 239 | static void SPU_APIENTRY trapClear(GLbitfield mask)
|
---|
| 240 | {
|
---|
[26909] | 241 | stubCheckWindowsState();
|
---|
[15532] | 242 | /* call the original SPU glClear function */
|
---|
| 243 | origClear(mask);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * As above, but for glViewport. Most apps call glViewport before
|
---|
| 248 | * glClear when a window is resized.
|
---|
| 249 | */
|
---|
| 250 | static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
|
---|
| 251 | {
|
---|
[26909] | 252 | stubCheckWindowsState();
|
---|
[20616] | 253 | /* call the original SPU glViewport function */
|
---|
[46528] | 254 | origViewport(x, y, w, h);
|
---|
[15532] | 255 | }
|
---|
| 256 |
|
---|
[63204] | 257 | /*static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
|
---|
[16718] | 258 | {
|
---|
[26909] | 259 | stubCheckWindowsState();
|
---|
[16718] | 260 | origSwapBuffers(window, flags);
|
---|
| 261 | }
|
---|
[15532] | 262 |
|
---|
[16718] | 263 | static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
|
---|
| 264 | {
|
---|
[26909] | 265 | stubCheckWindowsState();
|
---|
[16718] | 266 | origDrawBuffer(buf);
|
---|
[63204] | 267 | }*/
|
---|
[16718] | 268 |
|
---|
[63204] | 269 | #if 0 /* unused */
|
---|
[20029] | 270 | static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
|
---|
| 271 | {
|
---|
[20616] | 272 | int winX, winY;
|
---|
| 273 | unsigned int winW, winH;
|
---|
| 274 | WindowInfo *pWindow;
|
---|
[39568] | 275 | ContextInfo *context = stubGetCurrentContext();
|
---|
[63204] | 276 | (void)x; (void)y; (void)w; (void)h;
|
---|
| 277 |
|
---|
[39568] | 278 | pWindow = context->currentDrawable;
|
---|
[20616] | 279 | stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
|
---|
| 280 | origScissor(0, 0, winW, winH);
|
---|
[20029] | 281 | }
|
---|
[63204] | 282 | #endif /* unused */
|
---|
[20029] | 283 |
|
---|
[15532] | 284 | /**
|
---|
| 285 | * Use the GL function pointers in <spu> to initialize the static glim
|
---|
| 286 | * dispatch table.
|
---|
| 287 | */
|
---|
| 288 | static void stubInitSPUDispatch(SPU *spu)
|
---|
| 289 | {
|
---|
| 290 | crSPUInitDispatchTable( &stub.spuDispatch );
|
---|
| 291 | crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
|
---|
| 292 |
|
---|
| 293 | if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
|
---|
| 294 | /* patch-in special glClear/Viewport function to track window sizing */
|
---|
| 295 | origClear = stub.spuDispatch.Clear;
|
---|
| 296 | origViewport = stub.spuDispatch.Viewport;
|
---|
[16718] | 297 | origSwapBuffers = stub.spuDispatch.SwapBuffers;
|
---|
| 298 | origDrawBuffer = stub.spuDispatch.DrawBuffer;
|
---|
[20029] | 299 | origScissor = stub.spuDispatch.Scissor;
|
---|
[15532] | 300 | stub.spuDispatch.Clear = trapClear;
|
---|
| 301 | stub.spuDispatch.Viewport = trapViewport;
|
---|
[30486] | 302 |
|
---|
[16718] | 303 | /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
|
---|
| 304 | stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
|
---|
[15532] | 305 | }
|
---|
| 306 |
|
---|
| 307 | crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[63204] | 310 | #if 0 /** @todo stubSPUTearDown & stubSPUTearDownLocked are not referenced */
|
---|
| 311 |
|
---|
[15532] | 312 | // Callback function, used to destroy all created contexts
|
---|
| 313 | static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
|
---|
| 314 | {
|
---|
[63204] | 315 | (void)data1; (void)data2;
|
---|
[15532] | 316 | stubDestroyContext(key);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /**
|
---|
| 320 | * This is called when we exit.
|
---|
| 321 | * We call all the SPU's cleanup functions.
|
---|
| 322 | */
|
---|
[39485] | 323 | static void stubSPUTearDownLocked(void)
|
---|
[15532] | 324 | {
|
---|
[39485] | 325 | crDebug("stubSPUTearDownLocked");
|
---|
[15532] | 326 |
|
---|
| 327 | #ifdef WINDOWS
|
---|
[30440] | 328 | # ifndef CR_NEWWINTRACK
|
---|
[15532] | 329 | stubUninstallWindowMessageHook();
|
---|
[30440] | 330 | # endif
|
---|
[15532] | 331 | #endif
|
---|
[30440] | 332 |
|
---|
| 333 | #ifdef CR_NEWWINTRACK
|
---|
| 334 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
| 335 | #endif
|
---|
[38636] | 336 |
|
---|
[15532] | 337 | //delete all created contexts
|
---|
| 338 | stubMakeCurrent( NULL, NULL);
|
---|
[39631] | 339 |
|
---|
| 340 | /* the lock order is windowTable->contextTable (see wglMakeCurrent_prox, glXMakeCurrent)
|
---|
| 341 | * this is why we need to take a windowTable lock since we will later do stub.windowTable access & locking */
|
---|
| 342 | crHashtableLock(stub.windowTable);
|
---|
[15532] | 343 | crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
|
---|
[39631] | 344 | crHashtableUnlock(stub.windowTable);
|
---|
[15532] | 345 |
|
---|
| 346 | /* shutdown, now trap any calls to a NULL dispatcher */
|
---|
| 347 | crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
|
---|
| 348 |
|
---|
| 349 | crSPUUnloadChain(stub.spu);
|
---|
| 350 | stub.spu = NULL;
|
---|
| 351 |
|
---|
[17919] | 352 | #ifndef Linux
|
---|
[15532] | 353 | crUnloadOpenGL();
|
---|
[17919] | 354 | #endif
|
---|
[15532] | 355 |
|
---|
[34887] | 356 | #ifndef WINDOWS
|
---|
[15532] | 357 | crNetTearDown();
|
---|
[34887] | 358 | #endif
|
---|
[15532] | 359 |
|
---|
[16640] | 360 | #ifdef GLX
|
---|
| 361 | if (stub.xshmSI.shmid>=0)
|
---|
| 362 | {
|
---|
| 363 | shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
|
---|
| 364 | shmdt(stub.xshmSI.shmaddr);
|
---|
| 365 | }
|
---|
[25383] | 366 | crFreeHashtable(stub.pGLXPixmapsHash, crFree);
|
---|
[16640] | 367 | #endif
|
---|
| 368 |
|
---|
[25383] | 369 | crFreeHashtable(stub.windowTable, crFree);
|
---|
| 370 | crFreeHashtable(stub.contextTable, NULL);
|
---|
[30624] | 371 |
|
---|
| 372 | crMemset(&stub, 0, sizeof(stub));
|
---|
[39485] | 373 |
|
---|
[15532] | 374 | }
|
---|
| 375 |
|
---|
[39485] | 376 | /**
|
---|
| 377 | * This is called when we exit.
|
---|
| 378 | * We call all the SPU's cleanup functions.
|
---|
| 379 | */
|
---|
| 380 | static void stubSPUTearDown(void)
|
---|
| 381 | {
|
---|
| 382 | STUB_INIT_LOCK();
|
---|
| 383 | if (stub_initialized)
|
---|
| 384 | {
|
---|
| 385 | stubSPUTearDownLocked();
|
---|
| 386 | stub_initialized = 0;
|
---|
| 387 | }
|
---|
| 388 | STUB_INIT_UNLOCK();
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[63204] | 391 | #endif /** @todo stubSPUTearDown & stubSPUTearDownLocked are not referenced */
|
---|
| 392 |
|
---|
[15532] | 393 | static void stubSPUSafeTearDown(void)
|
---|
| 394 | {
|
---|
[17922] | 395 | #ifdef CHROMIUM_THREADSAFE
|
---|
| 396 | CRmutex *mutex;
|
---|
| 397 | #endif
|
---|
| 398 |
|
---|
[17919] | 399 | if (!stub_initialized) return;
|
---|
| 400 | stub_initialized = 0;
|
---|
| 401 |
|
---|
[15532] | 402 | #ifdef CHROMIUM_THREADSAFE
|
---|
[17922] | 403 | mutex = &stub.mutex;
|
---|
[15532] | 404 | crLockMutex(mutex);
|
---|
| 405 | #endif
|
---|
| 406 | crDebug("stubSPUSafeTearDown");
|
---|
[30440] | 407 |
|
---|
[15532] | 408 | #ifdef WINDOWS
|
---|
[30440] | 409 | # ifndef CR_NEWWINTRACK
|
---|
[15532] | 410 | stubUninstallWindowMessageHook();
|
---|
[30440] | 411 | # endif
|
---|
[15532] | 412 | #endif
|
---|
[30440] | 413 |
|
---|
[30531] | 414 | #if defined(CR_NEWWINTRACK)
|
---|
[30440] | 415 | crUnlockMutex(mutex);
|
---|
[30531] | 416 | # if defined(WINDOWS)
|
---|
[47151] | 417 | if (stub.hSyncThread && RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
|
---|
[30440] | 418 | {
|
---|
[34912] | 419 | HANDLE hNative;
|
---|
| 420 | DWORD ec=0;
|
---|
| 421 |
|
---|
| 422 | hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
|
---|
| 423 | false, RTThreadGetNative(stub.hSyncThread));
|
---|
| 424 | if (!hNative)
|
---|
| 425 | {
|
---|
| 426 | crWarning("Failed to get handle for sync thread(%#x)", GetLastError());
|
---|
| 427 | }
|
---|
| 428 | else
|
---|
| 429 | {
|
---|
| 430 | crDebug("Got handle %p for thread %#x", hNative, RTThreadGetNative(stub.hSyncThread));
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[30440] | 433 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
[34912] | 434 |
|
---|
[30440] | 435 | if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
|
---|
| 436 | {
|
---|
| 437 | RTThreadWait(stub.hSyncThread, 1000, NULL);
|
---|
[34912] | 438 |
|
---|
| 439 | /*Same issue as on linux, RTThreadWait exits before system thread is terminated, which leads
|
---|
| 440 | * to issues as our dll goes to be unloaded.
|
---|
[38636] | 441 | *@todo
|
---|
[34912] | 442 | *We usually call this function from DllMain which seems to be holding some lock and thus we have to
|
---|
| 443 | * kill thread via TerminateThread.
|
---|
| 444 | */
|
---|
| 445 | if (WaitForSingleObject(hNative, 100)==WAIT_TIMEOUT)
|
---|
| 446 | {
|
---|
| 447 | crDebug("Wait failed, terminating");
|
---|
| 448 | if (!TerminateThread(hNative, 1))
|
---|
| 449 | {
|
---|
| 450 | crDebug("TerminateThread failed");
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 | if (GetExitCodeThread(hNative, &ec))
|
---|
| 454 | {
|
---|
| 455 | crDebug("Thread %p exited with ec=%i", hNative, ec);
|
---|
| 456 | }
|
---|
| 457 | else
|
---|
| 458 | {
|
---|
| 459 | crDebug("GetExitCodeThread failed(%#x)", GetLastError());
|
---|
| 460 | }
|
---|
[30440] | 461 | }
|
---|
| 462 | else
|
---|
| 463 | {
|
---|
| 464 | crDebug("Sync thread killed before DLL_PROCESS_DETACH");
|
---|
| 465 | }
|
---|
[34912] | 466 |
|
---|
| 467 | if (hNative)
|
---|
| 468 | {
|
---|
| 469 | CloseHandle(hNative);
|
---|
| 470 | }
|
---|
[30440] | 471 | }
|
---|
[30531] | 472 | #else
|
---|
[32420] | 473 | if (stub.hSyncThread!=NIL_RTTHREAD)
|
---|
[30531] | 474 | {
|
---|
[32420] | 475 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
[30531] | 476 | {
|
---|
[51194] | 477 | int rc = RTThreadWait(stub.hSyncThread, RT_INDEFINITE_WAIT, NULL);
|
---|
| 478 | if (RT_FAILURE(rc))
|
---|
[32420] | 479 | {
|
---|
[51194] | 480 | WARN(("RTThreadWait_join failed %i", rc));
|
---|
[32420] | 481 | }
|
---|
[30531] | 482 | }
|
---|
| 483 | }
|
---|
| 484 | #endif
|
---|
[30440] | 485 | crLockMutex(mutex);
|
---|
| 486 | #endif
|
---|
| 487 |
|
---|
[34887] | 488 | #ifndef WINDOWS
|
---|
[30440] | 489 | crNetTearDown();
|
---|
[34887] | 490 | #endif
|
---|
| 491 |
|
---|
[15532] | 492 | #ifdef CHROMIUM_THREADSAFE
|
---|
| 493 | crUnlockMutex(mutex);
|
---|
| 494 | crFreeMutex(mutex);
|
---|
| 495 | #endif
|
---|
[30624] | 496 | crMemset(&stub, 0, sizeof(stub));
|
---|
[15532] | 497 | }
|
---|
| 498 |
|
---|
| 499 |
|
---|
| 500 | static void stubExitHandler(void)
|
---|
| 501 | {
|
---|
| 502 | stubSPUSafeTearDown();
|
---|
[52552] | 503 | signal(SIGTERM, SIG_DFL);
|
---|
| 504 | signal(SIGINT, SIG_DFL);
|
---|
[15532] | 505 | }
|
---|
| 506 |
|
---|
| 507 | /**
|
---|
| 508 | * Called when we receive a SIGTERM signal.
|
---|
| 509 | */
|
---|
| 510 | static void stubSignalHandler(int signo)
|
---|
| 511 | {
|
---|
[63204] | 512 | (void)signo;
|
---|
[15532] | 513 | stubSPUSafeTearDown();
|
---|
| 514 | exit(0); /* this causes stubExitHandler() to be called */
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[39602] | 517 | #ifndef RT_OS_WINDOWS
|
---|
| 518 | # ifdef CHROMIUM_THREADSAFE
|
---|
[57426] | 519 | static void stubThreadTlsDtor(void *pvValue)
|
---|
[39602] | 520 | {
|
---|
| 521 | ContextInfo *pCtx = (ContextInfo*)pvValue;
|
---|
| 522 | VBoxTlsRefRelease(pCtx);
|
---|
| 523 | }
|
---|
| 524 | # endif
|
---|
| 525 | #endif
|
---|
[15532] | 526 |
|
---|
[39602] | 527 |
|
---|
[15532] | 528 | /**
|
---|
| 529 | * Init variables in the stub structure, install signal handler.
|
---|
| 530 | */
|
---|
| 531 | static void stubInitVars(void)
|
---|
| 532 | {
|
---|
| 533 | WindowInfo *defaultWin;
|
---|
| 534 |
|
---|
| 535 | #ifdef CHROMIUM_THREADSAFE
|
---|
| 536 | crInitMutex(&stub.mutex);
|
---|
| 537 | #endif
|
---|
| 538 |
|
---|
| 539 | /* At the very least we want CR_RGB_BIT. */
|
---|
| 540 | stub.haveNativeOpenGL = GL_FALSE;
|
---|
| 541 | stub.spu = NULL;
|
---|
| 542 | stub.appDrawCursor = 0;
|
---|
| 543 | stub.minChromiumWindowWidth = 0;
|
---|
| 544 | stub.minChromiumWindowHeight = 0;
|
---|
| 545 | stub.maxChromiumWindowWidth = 0;
|
---|
| 546 | stub.maxChromiumWindowHeight = 0;
|
---|
| 547 | stub.matchChromiumWindowCount = 0;
|
---|
| 548 | stub.matchChromiumWindowID = NULL;
|
---|
| 549 | stub.matchWindowTitle = NULL;
|
---|
[26117] | 550 | stub.ignoreFreeglutMenus = 0;
|
---|
[15532] | 551 | stub.threadSafe = GL_FALSE;
|
---|
| 552 | stub.trackWindowSize = 0;
|
---|
| 553 | stub.trackWindowPos = 0;
|
---|
| 554 | stub.trackWindowVisibility = 0;
|
---|
| 555 | stub.trackWindowVisibleRgn = 0;
|
---|
| 556 | stub.mothershipPID = 0;
|
---|
| 557 | stub.spu_dir = NULL;
|
---|
| 558 |
|
---|
| 559 | stub.freeContextNumber = MAGIC_CONTEXT_BASE;
|
---|
| 560 | stub.contextTable = crAllocHashtable();
|
---|
[39568] | 561 | #ifndef RT_OS_WINDOWS
|
---|
| 562 | # ifdef CHROMIUM_THREADSAFE
|
---|
[39602] | 563 | if (!g_stubIsCurrentContextTSDInited)
|
---|
| 564 | {
|
---|
| 565 | crInitTSDF(&g_stubCurrentContextTSD, stubThreadTlsDtor);
|
---|
| 566 | g_stubIsCurrentContextTSDInited = true;
|
---|
| 567 | }
|
---|
[39568] | 568 | # endif
|
---|
| 569 | #endif
|
---|
| 570 | stubSetCurrentContext(NULL);
|
---|
[15532] | 571 |
|
---|
| 572 | stub.windowTable = crAllocHashtable();
|
---|
| 573 |
|
---|
[30440] | 574 | #ifdef CR_NEWWINTRACK
|
---|
| 575 | stub.bShutdownSyncThread = false;
|
---|
| 576 | stub.hSyncThread = NIL_RTTHREAD;
|
---|
| 577 | #endif
|
---|
| 578 |
|
---|
[15532] | 579 | defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
|
---|
| 580 | defaultWin->type = CHROMIUM;
|
---|
| 581 | defaultWin->spuWindow = 0; /* window 0 always exists */
|
---|
| 582 | #ifdef WINDOWS
|
---|
| 583 | defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
|
---|
[16480] | 584 | #elif defined(GLX)
|
---|
| 585 | defaultWin->pVisibleRegions = NULL;
|
---|
[16718] | 586 | defaultWin->cVisibleRegions = 0;
|
---|
[15532] | 587 | #endif
|
---|
| 588 | crHashtableAdd(stub.windowTable, 0, defaultWin);
|
---|
| 589 |
|
---|
| 590 | #if 1
|
---|
| 591 | atexit(stubExitHandler);
|
---|
| 592 | signal(SIGTERM, stubSignalHandler);
|
---|
| 593 | signal(SIGINT, stubSignalHandler);
|
---|
| 594 | #ifndef WINDOWS
|
---|
| 595 | signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
|
---|
| 596 | #endif
|
---|
| 597 | #else
|
---|
| 598 | (void) stubExitHandler;
|
---|
| 599 | (void) stubSignalHandler;
|
---|
| 600 | #endif
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 |
|
---|
[57187] | 604 | #if 0 /* unused */
|
---|
| 605 |
|
---|
[15532] | 606 | /**
|
---|
| 607 | * Return a free port number for the mothership to use, or -1 if we
|
---|
| 608 | * can't find one.
|
---|
| 609 | */
|
---|
| 610 | static int
|
---|
| 611 | GenerateMothershipPort(void)
|
---|
| 612 | {
|
---|
| 613 | const int MAX_PORT = 10100;
|
---|
| 614 | unsigned short port;
|
---|
| 615 |
|
---|
| 616 | /* generate initial port number randomly */
|
---|
| 617 | crRandAutoSeed();
|
---|
| 618 | port = (unsigned short) crRandInt(10001, MAX_PORT);
|
---|
| 619 |
|
---|
| 620 | #ifdef WINDOWS
|
---|
| 621 | /* XXX should implement a free port check here */
|
---|
| 622 | return port;
|
---|
| 623 | #else
|
---|
| 624 | /*
|
---|
| 625 | * See if this port number really is free, try another if needed.
|
---|
| 626 | */
|
---|
| 627 | {
|
---|
| 628 | struct sockaddr_in servaddr;
|
---|
| 629 | int so_reuseaddr = 1;
|
---|
| 630 | int sock, k;
|
---|
| 631 |
|
---|
| 632 | /* create socket */
|
---|
| 633 | sock = socket(AF_INET, SOCK_STREAM, 0);
|
---|
| 634 | CRASSERT(sock > 2);
|
---|
| 635 |
|
---|
| 636 | /* deallocate socket/port when we exit */
|
---|
| 637 | k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
| 638 | (char *) &so_reuseaddr, sizeof(so_reuseaddr));
|
---|
| 639 | CRASSERT(k == 0);
|
---|
| 640 |
|
---|
| 641 | /* initialize the servaddr struct */
|
---|
| 642 | crMemset(&servaddr, 0, sizeof(servaddr) );
|
---|
| 643 | servaddr.sin_family = AF_INET;
|
---|
| 644 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
| 645 |
|
---|
| 646 | while (port < MAX_PORT) {
|
---|
| 647 | /* Bind to the given port number, return -1 if we fail */
|
---|
| 648 | servaddr.sin_port = htons((unsigned short) port);
|
---|
| 649 | k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
|
---|
| 650 | if (k) {
|
---|
| 651 | /* failed to create port. try next one. */
|
---|
| 652 | port++;
|
---|
| 653 | }
|
---|
| 654 | else {
|
---|
| 655 | /* free the socket/port now so mothership can make it */
|
---|
| 656 | close(sock);
|
---|
| 657 | return port;
|
---|
| 658 | }
|
---|
| 659 | }
|
---|
| 660 | }
|
---|
| 661 | #endif /* WINDOWS */
|
---|
| 662 | return -1;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | /**
|
---|
| 667 | * Try to determine which mothership configuration to use for this program.
|
---|
| 668 | */
|
---|
| 669 | static char **
|
---|
| 670 | LookupMothershipConfig(const char *procName)
|
---|
| 671 | {
|
---|
| 672 | const int procNameLen = crStrlen(procName);
|
---|
| 673 | FILE *f;
|
---|
| 674 | const char *home;
|
---|
| 675 | char configPath[1000];
|
---|
| 676 |
|
---|
| 677 | /* first, check if the CR_CONFIG env var is set */
|
---|
| 678 | {
|
---|
| 679 | const char *conf = crGetenv("CR_CONFIG");
|
---|
| 680 | if (conf && crStrlen(conf) > 0)
|
---|
| 681 | return crStrSplit(conf, " ");
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /* second, look up config name from config file */
|
---|
| 685 | home = crGetenv("HOME");
|
---|
| 686 | if (home)
|
---|
| 687 | sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
|
---|
| 688 | else
|
---|
| 689 | crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
|
---|
| 690 | /* Check if the CR_CONFIG_PATH env var is set. */
|
---|
| 691 | {
|
---|
| 692 | const char *conf = crGetenv("CR_CONFIG_PATH");
|
---|
| 693 | if (conf)
|
---|
| 694 | crStrcpy(configPath, conf); /* from env var */
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | f = fopen(configPath, "r");
|
---|
| 698 | if (!f) {
|
---|
| 699 | return NULL;
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | while (!feof(f)) {
|
---|
| 703 | char line[1000];
|
---|
| 704 | char **args;
|
---|
| 705 | fgets(line, 999, f);
|
---|
| 706 | line[crStrlen(line) - 1] = 0; /* remove trailing newline */
|
---|
| 707 | if (crStrncmp(line, procName, procNameLen) == 0 &&
|
---|
[38636] | 708 | (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
|
---|
[15532] | 709 | {
|
---|
| 710 | crWarning("Using Chromium configuration for %s from %s",
|
---|
| 711 | procName, configPath);
|
---|
| 712 | args = crStrSplit(line + procNameLen + 1, " ");
|
---|
| 713 | return args;
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
| 716 | fclose(f);
|
---|
| 717 | return NULL;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 | static int Mothership_Awake = 0;
|
---|
| 722 |
|
---|
| 723 |
|
---|
| 724 | /**
|
---|
| 725 | * Signal handler to determine when mothership is ready.
|
---|
| 726 | */
|
---|
| 727 | static void
|
---|
| 728 | MothershipPhoneHome(int signo)
|
---|
| 729 | {
|
---|
| 730 | crDebug("Got signal %d: mothership is awake!", signo);
|
---|
| 731 | Mothership_Awake = 1;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[57187] | 734 | #endif /* 0 */
|
---|
| 735 |
|
---|
[63204] | 736 | static void stubSetDefaultConfigurationOptions(void)
|
---|
[15532] | 737 | {
|
---|
| 738 | unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
---|
| 739 |
|
---|
| 740 | stub.appDrawCursor = 0;
|
---|
| 741 | stub.minChromiumWindowWidth = 0;
|
---|
| 742 | stub.minChromiumWindowHeight = 0;
|
---|
| 743 | stub.maxChromiumWindowWidth = 0;
|
---|
| 744 | stub.maxChromiumWindowHeight = 0;
|
---|
| 745 | stub.matchChromiumWindowID = NULL;
|
---|
| 746 | stub.numIgnoreWindowID = 0;
|
---|
| 747 | stub.matchWindowTitle = NULL;
|
---|
[26117] | 748 | stub.ignoreFreeglutMenus = 0;
|
---|
[15532] | 749 | stub.trackWindowSize = 1;
|
---|
| 750 | stub.trackWindowPos = 1;
|
---|
| 751 | stub.trackWindowVisibility = 1;
|
---|
| 752 | stub.trackWindowVisibleRgn = 1;
|
---|
| 753 | stub.matchChromiumWindowCount = 0;
|
---|
| 754 | stub.spu_dir = NULL;
|
---|
| 755 | crNetSetRank(0);
|
---|
| 756 | crNetSetContextRange(32, 35);
|
---|
| 757 | crNetSetNodeRange("iam0", "iamvis20");
|
---|
| 758 | crNetSetKey(key,sizeof(key));
|
---|
| 759 | stub.force_pbuffers = 0;
|
---|
[20616] | 760 |
|
---|
| 761 | #ifdef WINDOWS
|
---|
[34295] | 762 | # ifdef VBOX_WITH_WDDM
|
---|
[46528] | 763 | stub.bRunningUnderWDDM = false;
|
---|
[34295] | 764 | # endif
|
---|
[20616] | 765 | #endif
|
---|
[15532] | 766 | }
|
---|
| 767 |
|
---|
[30440] | 768 | #ifdef CR_NEWWINTRACK
|
---|
[30513] | 769 | # ifdef VBOX_WITH_WDDM
|
---|
[63204] | 770 | static void stubDispatchVisibleRegions(WindowInfo *pWindow)
|
---|
[30513] | 771 | {
|
---|
| 772 | DWORD dwCount;
|
---|
| 773 | LPRGNDATA lpRgnData;
|
---|
| 774 |
|
---|
| 775 | dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
|
---|
| 776 | lpRgnData = crAlloc(dwCount);
|
---|
| 777 |
|
---|
| 778 | if (lpRgnData)
|
---|
| 779 | {
|
---|
| 780 | GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
|
---|
| 781 | crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
|
---|
| 782 | stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
|
---|
| 783 | crFree(lpRgnData);
|
---|
| 784 | }
|
---|
| 785 | else crWarning("GetRegionData failed, VisibleRegions update failed");
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
|
---|
| 789 | {
|
---|
| 790 | HRGN hRgn, hTmpRgn;
|
---|
| 791 | uint32_t i;
|
---|
| 792 |
|
---|
| 793 | if (pRegions->RectsInfo.cRects<=start)
|
---|
| 794 | {
|
---|
| 795 | return INVALID_HANDLE_VALUE;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | hRgn = CreateRectRgn(0, 0, 0, 0);
|
---|
| 799 | for (i=start; i<pRegions->RectsInfo.cRects; ++i)
|
---|
| 800 | {
|
---|
| 801 | hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
|
---|
| 802 | CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
|
---|
| 803 | DeleteObject(hTmpRgn);
|
---|
| 804 | }
|
---|
| 805 | return hRgn;
|
---|
| 806 | }
|
---|
| 807 |
|
---|
[43487] | 808 | # endif /* VBOX_WITH_WDDM */
|
---|
[30513] | 809 |
|
---|
[30440] | 810 | static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
|
---|
| 811 | {
|
---|
| 812 | WindowInfo *pWindow = (WindowInfo *) data1;
|
---|
[63204] | 813 | (void)key; (void) data2;
|
---|
[30440] | 814 |
|
---|
| 815 | if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
|
---|
| 816 | {
|
---|
| 817 | return;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
|
---|
| 821 |
|
---|
| 822 | if (!stubSystemWindowExist(pWindow))
|
---|
| 823 | {
|
---|
| 824 | #ifdef WINDOWS
|
---|
[42518] | 825 | stubDestroyWindow(0, (GLint)pWindow->hWnd);
|
---|
[30440] | 826 | #else
|
---|
[42518] | 827 | stubDestroyWindow(0, (GLint)pWindow->drawable);
|
---|
[30440] | 828 | #endif
|
---|
| 829 | /*No need to flush here as crWindowDestroy does it*/
|
---|
| 830 | return;
|
---|
| 831 | }
|
---|
| 832 |
|
---|
[34295] | 833 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
| 834 | if (stub.bRunningUnderWDDM)
|
---|
| 835 | return;
|
---|
| 836 | #endif
|
---|
[30440] | 837 | stubCheckWindowState(pWindow, GL_TRUE);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
|
---|
| 841 | {
|
---|
| 842 | #ifdef WINDOWS
|
---|
| 843 | MSG msg;
|
---|
[30513] | 844 | # ifdef VBOX_WITH_WDDM
|
---|
| 845 | HMODULE hVBoxD3D = NULL;
|
---|
[42499] | 846 | GLint spuConnection = 0;
|
---|
[30513] | 847 | # endif
|
---|
[30440] | 848 | #endif
|
---|
| 849 |
|
---|
| 850 | (void) pvUser;
|
---|
| 851 |
|
---|
| 852 | crDebug("Sync thread started");
|
---|
| 853 | #ifdef WINDOWS
|
---|
| 854 | PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
[30513] | 855 | # ifdef VBOX_WITH_WDDM
|
---|
[46627] | 856 | hVBoxD3D = NULL;
|
---|
| 857 | if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
|
---|
[30513] | 858 | {
|
---|
[46627] | 859 | crDebug("GetModuleHandleEx failed err %d", GetLastError());
|
---|
| 860 | hVBoxD3D = NULL;
|
---|
[35127] | 861 | }
|
---|
| 862 |
|
---|
| 863 | if (hVBoxD3D)
|
---|
| 864 | {
|
---|
[42536] | 865 | crDebug("running with " VBOX_MODNAME_DISPD3D);
|
---|
[32552] | 866 | stub.trackWindowVisibleRgn = 0;
|
---|
[34295] | 867 | stub.bRunningUnderWDDM = true;
|
---|
[30513] | 868 | }
|
---|
[42499] | 869 | # endif /* VBOX_WITH_WDDM */
|
---|
| 870 | #endif /* WINDOWS */
|
---|
[30440] | 871 |
|
---|
| 872 | crLockMutex(&stub.mutex);
|
---|
[42499] | 873 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
| 874 | spuConnection =
|
---|
| 875 | #endif
|
---|
| 876 | stub.spu->dispatch_table.VBoxPackSetInjectThread(NULL);
|
---|
| 877 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
[42523] | 878 | if (stub.bRunningUnderWDDM && !spuConnection)
|
---|
[42499] | 879 | {
|
---|
| 880 | crError("VBoxPackSetInjectThread failed!");
|
---|
| 881 | }
|
---|
| 882 | #endif
|
---|
[30440] | 883 | crUnlockMutex(&stub.mutex);
|
---|
| 884 |
|
---|
| 885 | RTThreadUserSignal(ThreadSelf);
|
---|
| 886 |
|
---|
| 887 | while(!stub.bShutdownSyncThread)
|
---|
| 888 | {
|
---|
| 889 | #ifdef WINDOWS
|
---|
| 890 | if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
|
---|
| 891 | {
|
---|
[30513] | 892 | # ifdef VBOX_WITH_WDDM
|
---|
[46757] | 893 | if (stub.bRunningUnderWDDM)
|
---|
[30513] | 894 | {
|
---|
[46757] | 895 |
|
---|
[30513] | 896 | }
|
---|
| 897 | else
|
---|
| 898 | # endif
|
---|
| 899 | {
|
---|
| 900 | crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
|
---|
| 901 | RTThreadSleep(50);
|
---|
| 902 | }
|
---|
[30440] | 903 | }
|
---|
| 904 | else
|
---|
| 905 | {
|
---|
| 906 | if (WM_QUIT==msg.message)
|
---|
| 907 | {
|
---|
| 908 | crDebug("Sync thread got WM_QUIT");
|
---|
| 909 | break;
|
---|
| 910 | }
|
---|
| 911 | else
|
---|
| 912 | {
|
---|
| 913 | TranslateMessage(&msg);
|
---|
| 914 | DispatchMessage(&msg);
|
---|
| 915 | }
|
---|
| 916 | }
|
---|
| 917 | #else
|
---|
[49470] | 918 | /* Try to keep a consistent locking order. */
|
---|
| 919 | crHashtableLock(stub.windowTable);
|
---|
[30440] | 920 | crLockMutex(&stub.mutex);
|
---|
[49470] | 921 | crHashtableWalkUnlocked(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
|
---|
[30440] | 922 | crUnlockMutex(&stub.mutex);
|
---|
[49470] | 923 | crHashtableUnlock(stub.windowTable);
|
---|
[30440] | 924 | RTThreadSleep(50);
|
---|
| 925 | #endif
|
---|
| 926 | }
|
---|
| 927 |
|
---|
[30513] | 928 | #ifdef VBOX_WITH_WDDM
|
---|
[42499] | 929 | if (spuConnection)
|
---|
| 930 | {
|
---|
| 931 | stub.spu->dispatch_table.VBoxConDestroy(spuConnection);
|
---|
| 932 | }
|
---|
[30513] | 933 | if (hVBoxD3D)
|
---|
| 934 | {
|
---|
| 935 | FreeLibrary(hVBoxD3D);
|
---|
| 936 | }
|
---|
| 937 | #endif
|
---|
[30440] | 938 | crDebug("Sync thread stopped");
|
---|
| 939 | return 0;
|
---|
| 940 | }
|
---|
[43487] | 941 | #endif /* CR_NEWWINTRACK */
|
---|
[30440] | 942 |
|
---|
[15532] | 943 | /**
|
---|
| 944 | * Do one-time initializations for the faker.
|
---|
| 945 | * Returns TRUE on success, FALSE otherwise.
|
---|
| 946 | */
|
---|
[39485] | 947 | static bool
|
---|
| 948 | stubInitLocked(void)
|
---|
[15532] | 949 | {
|
---|
| 950 | /* Here is where we contact the mothership to find out what we're supposed
|
---|
[38636] | 951 | * to be doing. Networking code in a DLL initializer. I sure hope this
|
---|
| 952 | * works :)
|
---|
| 953 | *
|
---|
[15532] | 954 | * HOW can I pass the mothership address to this if I already know it?
|
---|
| 955 | */
|
---|
[38636] | 956 |
|
---|
[15532] | 957 | char response[1024];
|
---|
| 958 | char **spuchain;
|
---|
| 959 | int num_spus;
|
---|
| 960 | int *spu_ids;
|
---|
| 961 | char **spu_names;
|
---|
| 962 | const char *app_id;
|
---|
| 963 | int i;
|
---|
[30487] | 964 | int disable_sync = 0;
|
---|
[47151] | 965 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
| 966 | HMODULE hVBoxD3D = NULL;
|
---|
| 967 | #endif
|
---|
[15532] | 968 |
|
---|
| 969 | stubInitVars();
|
---|
| 970 |
|
---|
[30440] | 971 | crGetProcName(response, 1024);
|
---|
| 972 | crDebug("Stub launched for %s", response);
|
---|
| 973 |
|
---|
[30487] | 974 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
[38864] | 975 | /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread
|
---|
| 976 | * as at the start compiz runs our code under XGrabServer.
|
---|
| 977 | */
|
---|
| 978 | if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real") || !crStrcmp(response, "compiz.real")
|
---|
[30598] | 979 | || !crStrcmp(response, "compiz-bin"))
|
---|
[30487] | 980 | {
|
---|
| 981 | disable_sync = 1;
|
---|
| 982 | }
|
---|
| 983 | #endif
|
---|
| 984 |
|
---|
[15532] | 985 | /* @todo check if it'd be of any use on other than guests, no use for windows */
|
---|
| 986 | app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
|
---|
| 987 |
|
---|
| 988 | crNetInit( NULL, NULL );
|
---|
[17921] | 989 |
|
---|
| 990 | #ifndef WINDOWS
|
---|
| 991 | {
|
---|
| 992 | CRNetServer ns;
|
---|
| 993 |
|
---|
| 994 | ns.name = "vboxhgcm://host:0";
|
---|
| 995 | ns.buffer_size = 1024;
|
---|
[42499] | 996 | crNetServerConnect(&ns
|
---|
| 997 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
| 998 | , NULL
|
---|
| 999 | #endif
|
---|
| 1000 | );
|
---|
[17921] | 1001 | if (!ns.conn)
|
---|
| 1002 | {
|
---|
| 1003 | crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
|
---|
[58584] | 1004 | # ifdef VBOXOGL_FAKEDRI
|
---|
[17921] | 1005 | return false;
|
---|
[58584] | 1006 | # else
|
---|
| 1007 | exit(1);
|
---|
| 1008 | # endif
|
---|
[17921] | 1009 | }
|
---|
| 1010 | else
|
---|
| 1011 | {
|
---|
| 1012 | crNetFreeConnection(ns.conn);
|
---|
| 1013 | }
|
---|
| 1014 | }
|
---|
| 1015 | #endif
|
---|
| 1016 |
|
---|
[21308] | 1017 | strcpy(response, "2 0 feedback 1 pack");
|
---|
[15532] | 1018 | spuchain = crStrSplit( response, " " );
|
---|
| 1019 | num_spus = crStrToInt( spuchain[0] );
|
---|
| 1020 | spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
|
---|
| 1021 | spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
|
---|
| 1022 | for (i = 0 ; i < num_spus ; i++)
|
---|
| 1023 | {
|
---|
| 1024 | spu_ids[i] = crStrToInt( spuchain[2*i+1] );
|
---|
| 1025 | spu_names[i] = crStrdup( spuchain[2*i+2] );
|
---|
| 1026 | crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | stubSetDefaultConfigurationOptions();
|
---|
| 1030 |
|
---|
[47161] | 1031 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
| 1032 | hVBoxD3D = NULL;
|
---|
| 1033 | if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
|
---|
| 1034 | {
|
---|
| 1035 | crDebug("GetModuleHandleEx failed err %d", GetLastError());
|
---|
| 1036 | hVBoxD3D = NULL;
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 | if (hVBoxD3D)
|
---|
| 1040 | {
|
---|
| 1041 | disable_sync = 1;
|
---|
| 1042 | crDebug("running with %s", VBOX_MODNAME_DISPD3D);
|
---|
| 1043 | stub.trackWindowVisibleRgn = 0;
|
---|
| 1044 | /* @todo: should we enable that? */
|
---|
| 1045 | stub.trackWindowSize = 0;
|
---|
| 1046 | stub.trackWindowPos = 0;
|
---|
| 1047 | stub.trackWindowVisibility = 0;
|
---|
| 1048 | stub.bRunningUnderWDDM = true;
|
---|
| 1049 | }
|
---|
| 1050 | #endif
|
---|
| 1051 |
|
---|
[15532] | 1052 | stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
|
---|
| 1053 |
|
---|
| 1054 | crFree( spuchain );
|
---|
| 1055 | crFree( spu_ids );
|
---|
| 1056 | for (i = 0; i < num_spus; ++i)
|
---|
| 1057 | crFree(spu_names[i]);
|
---|
| 1058 | crFree( spu_names );
|
---|
| 1059 |
|
---|
| 1060 | // spu chain load failed somewhere
|
---|
| 1061 | if (!stub.spu) {
|
---|
| 1062 | return false;
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | crSPUInitDispatchTable( &glim );
|
---|
| 1066 |
|
---|
[38636] | 1067 | /* This is unlikely to change -- We still want to initialize our dispatch
|
---|
[15532] | 1068 | * table with the functions of the first SPU in the chain. */
|
---|
| 1069 | stubInitSPUDispatch( stub.spu );
|
---|
| 1070 |
|
---|
| 1071 | /* we need to plug one special stub function into the dispatch table */
|
---|
| 1072 | glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
|
---|
| 1073 |
|
---|
[18088] | 1074 | #if !defined(VBOX_NO_NATIVEGL)
|
---|
[15532] | 1075 | /* Load pointers to native OpenGL functions into stub.nativeDispatch */
|
---|
| 1076 | stubInitNativeDispatch();
|
---|
[17839] | 1077 | #endif
|
---|
[15532] | 1078 |
|
---|
[16381] | 1079 | /*crDebug("stub init");
|
---|
| 1080 | raise(SIGINT);*/
|
---|
| 1081 |
|
---|
[15532] | 1082 | #ifdef WINDOWS
|
---|
[30440] | 1083 | # ifndef CR_NEWWINTRACK
|
---|
[15532] | 1084 | stubInstallWindowMessageHook();
|
---|
[30440] | 1085 | # endif
|
---|
[15532] | 1086 | #endif
|
---|
| 1087 |
|
---|
[30440] | 1088 | #ifdef CR_NEWWINTRACK
|
---|
| 1089 | {
|
---|
| 1090 | int rc;
|
---|
| 1091 |
|
---|
[44457] | 1092 | RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
[30440] | 1093 |
|
---|
[30487] | 1094 | if (!disable_sync)
|
---|
| 1095 | {
|
---|
| 1096 | crDebug("Starting sync thread");
|
---|
[30440] | 1097 |
|
---|
[30487] | 1098 | rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
|
---|
| 1099 | if (RT_FAILURE(rc))
|
---|
| 1100 | {
|
---|
| 1101 | crError("Failed to start sync thread! (%x)", rc);
|
---|
| 1102 | }
|
---|
| 1103 | RTThreadUserWait(stub.hSyncThread, 60 * 1000);
|
---|
| 1104 | RTThreadUserReset(stub.hSyncThread);
|
---|
| 1105 |
|
---|
| 1106 | crDebug("Going on");
|
---|
[30440] | 1107 | }
|
---|
| 1108 | }
|
---|
| 1109 | #endif
|
---|
| 1110 |
|
---|
[16640] | 1111 | #ifdef GLX
|
---|
| 1112 | stub.xshmSI.shmid = -1;
|
---|
| 1113 | stub.bShmInitFailed = GL_FALSE;
|
---|
[25383] | 1114 | stub.pGLXPixmapsHash = crAllocHashtable();
|
---|
[32420] | 1115 |
|
---|
| 1116 | stub.bXExtensionsChecked = GL_FALSE;
|
---|
| 1117 | stub.bHaveXComposite = GL_FALSE;
|
---|
| 1118 | stub.bHaveXFixes = GL_FALSE;
|
---|
[16640] | 1119 | #endif
|
---|
| 1120 |
|
---|
[15532] | 1121 | return true;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
[39485] | 1124 | /**
|
---|
| 1125 | * Do one-time initializations for the faker.
|
---|
| 1126 | * Returns TRUE on success, FALSE otherwise.
|
---|
| 1127 | */
|
---|
| 1128 | bool
|
---|
| 1129 | stubInit(void)
|
---|
| 1130 | {
|
---|
| 1131 | bool bRc = true;
|
---|
| 1132 | /* we need to serialize the initialization, otherwise racing is possible
|
---|
| 1133 | * for XPDM-based d3d when a d3d switcher is testing the gl lib in two or more threads
|
---|
| 1134 | * NOTE: the STUB_INIT_LOCK/UNLOCK is a NOP for non-win currently */
|
---|
| 1135 | STUB_INIT_LOCK();
|
---|
| 1136 | if (!stub_initialized)
|
---|
| 1137 | bRc = stub_initialized = stubInitLocked();
|
---|
| 1138 | STUB_INIT_UNLOCK();
|
---|
| 1139 | return bRc;
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
[38636] | 1142 | /* Sigh -- we can't do initialization at load time, since Windows forbids
|
---|
[15532] | 1143 | * the loading of other libraries from DLLMain. */
|
---|
| 1144 |
|
---|
| 1145 | #ifdef WINDOWS
|
---|
| 1146 | #define WIN32_LEAN_AND_MEAN
|
---|
| 1147 | #include <windows.h>
|
---|
| 1148 |
|
---|
[47151] | 1149 | #if 1//def DEBUG_misha
|
---|
[39485] | 1150 | /* debugging: this is to be able to catch first-chance notifications
|
---|
| 1151 | * for exceptions other than EXCEPTION_BREAKPOINT in kernel debugger */
|
---|
[41638] | 1152 | # define VDBG_VEHANDLER
|
---|
[39485] | 1153 | #endif
|
---|
| 1154 |
|
---|
| 1155 | #ifdef VDBG_VEHANDLER
|
---|
[47151] | 1156 | # include <dbghelp.h>
|
---|
[51673] | 1157 | # include <cr_string.h>
|
---|
[47151] | 1158 | static PVOID g_VBoxVehHandler = NULL;
|
---|
| 1159 | static DWORD g_VBoxVehEnable = 0;
|
---|
| 1160 |
|
---|
| 1161 | /* generate a crash dump on exception */
|
---|
| 1162 | #define VBOXVEH_F_DUMP 0x00000001
|
---|
| 1163 | /* generate a debugger breakpoint exception */
|
---|
| 1164 | #define VBOXVEH_F_BREAK 0x00000002
|
---|
| 1165 | /* exit on exception */
|
---|
| 1166 | #define VBOXVEH_F_EXIT 0x00000004
|
---|
| 1167 |
|
---|
[51673] | 1168 | static DWORD g_VBoxVehFlags = 0;
|
---|
[47151] | 1169 |
|
---|
| 1170 | typedef BOOL WINAPI FNVBOXDBG_MINIDUMPWRITEDUMP(HANDLE hProcess,
|
---|
| 1171 | DWORD ProcessId,
|
---|
| 1172 | HANDLE hFile,
|
---|
| 1173 | MINIDUMP_TYPE DumpType,
|
---|
| 1174 | PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
|
---|
| 1175 | PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
|
---|
| 1176 | PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
|
---|
| 1177 | typedef FNVBOXDBG_MINIDUMPWRITEDUMP *PFNVBOXDBG_MINIDUMPWRITEDUMP;
|
---|
| 1178 |
|
---|
| 1179 | static HMODULE g_hVBoxMdDbgHelp = NULL;
|
---|
| 1180 | static PFNVBOXDBG_MINIDUMPWRITEDUMP g_pfnVBoxMdMiniDumpWriteDump = NULL;
|
---|
[51673] | 1181 | static size_t g_cVBoxMdFilePrefixLen = 0;
|
---|
[47151] | 1182 | static WCHAR g_aszwVBoxMdFilePrefix[MAX_PATH];
|
---|
| 1183 | static WCHAR g_aszwVBoxMdDumpCount = 0;
|
---|
| 1184 | static MINIDUMP_TYPE g_enmVBoxMdDumpType = MiniDumpNormal
|
---|
| 1185 | | MiniDumpWithDataSegs
|
---|
| 1186 | | MiniDumpWithFullMemory
|
---|
| 1187 | | MiniDumpWithHandleData
|
---|
| 1188 | //// | MiniDumpFilterMemory
|
---|
| 1189 | //// | MiniDumpScanMemory
|
---|
| 1190 | // | MiniDumpWithUnloadedModules
|
---|
| 1191 | //// | MiniDumpWithIndirectlyReferencedMemory
|
---|
| 1192 | //// | MiniDumpFilterModulePaths
|
---|
| 1193 | // | MiniDumpWithProcessThreadData
|
---|
| 1194 | // | MiniDumpWithPrivateReadWriteMemory
|
---|
| 1195 | //// | MiniDumpWithoutOptionalData
|
---|
| 1196 | // | MiniDumpWithFullMemoryInfo
|
---|
| 1197 | // | MiniDumpWithThreadInfo
|
---|
| 1198 | // | MiniDumpWithCodeSegs
|
---|
| 1199 | // | MiniDumpWithFullAuxiliaryState
|
---|
| 1200 | // | MiniDumpWithPrivateWriteCopyMemory
|
---|
| 1201 | // | MiniDumpIgnoreInaccessibleMemory
|
---|
| 1202 | // | MiniDumpWithTokenInformation
|
---|
| 1203 | //// | MiniDumpWithModuleHeaders
|
---|
| 1204 | //// | MiniDumpFilterTriage
|
---|
| 1205 | ;
|
---|
| 1206 |
|
---|
| 1207 |
|
---|
| 1208 |
|
---|
[51673] | 1209 | #define VBOXMD_DUMP_DIR_DEFAULT "C:\\dumps"
|
---|
| 1210 | #define VBOXMD_DUMP_NAME_PREFIX_W L"VBoxDmp_"
|
---|
[47151] | 1211 |
|
---|
| 1212 | static HMODULE loadSystemDll(const char *pszName)
|
---|
| 1213 | {
|
---|
| 1214 | char szPath[MAX_PATH];
|
---|
| 1215 | UINT cchPath = GetSystemDirectoryA(szPath, sizeof(szPath));
|
---|
| 1216 | size_t cbName = strlen(pszName) + 1;
|
---|
| 1217 | if (cchPath + 1 + cbName > sizeof(szPath))
|
---|
| 1218 | {
|
---|
| 1219 | SetLastError(ERROR_FILENAME_EXCED_RANGE);
|
---|
| 1220 | return NULL;
|
---|
| 1221 | }
|
---|
| 1222 | szPath[cchPath] = '\\';
|
---|
| 1223 | memcpy(&szPath[cchPath + 1], pszName, cbName);
|
---|
| 1224 | return LoadLibraryA(szPath);
|
---|
| 1225 | }
|
---|
| 1226 |
|
---|
| 1227 | static DWORD vboxMdMinidumpCreate(struct _EXCEPTION_POINTERS *pExceptionInfo)
|
---|
| 1228 | {
|
---|
| 1229 | WCHAR aszwMdFileName[MAX_PATH];
|
---|
| 1230 | HANDLE hProcess = GetCurrentProcess();
|
---|
| 1231 | DWORD ProcessId = GetCurrentProcessId();
|
---|
| 1232 | MINIDUMP_EXCEPTION_INFORMATION ExceptionInfo;
|
---|
| 1233 | HANDLE hFile;
|
---|
| 1234 | DWORD winErr = ERROR_SUCCESS;
|
---|
| 1235 |
|
---|
| 1236 | if (!g_pfnVBoxMdMiniDumpWriteDump)
|
---|
| 1237 | {
|
---|
| 1238 | if (!g_hVBoxMdDbgHelp)
|
---|
| 1239 | {
|
---|
| 1240 | g_hVBoxMdDbgHelp = loadSystemDll("DbgHelp.dll");
|
---|
| 1241 | if (!g_hVBoxMdDbgHelp)
|
---|
| 1242 | return GetLastError();
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
| 1245 | g_pfnVBoxMdMiniDumpWriteDump = (PFNVBOXDBG_MINIDUMPWRITEDUMP)GetProcAddress(g_hVBoxMdDbgHelp, "MiniDumpWriteDump");
|
---|
| 1246 | if (!g_pfnVBoxMdMiniDumpWriteDump)
|
---|
| 1247 | return GetLastError();
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | ++g_aszwVBoxMdDumpCount;
|
---|
| 1251 |
|
---|
| 1252 | memcpy(aszwMdFileName, g_aszwVBoxMdFilePrefix, g_cVBoxMdFilePrefixLen * sizeof (g_aszwVBoxMdFilePrefix[0]));
|
---|
| 1253 | swprintf(aszwMdFileName + g_cVBoxMdFilePrefixLen, RT_ELEMENTS(aszwMdFileName) - g_cVBoxMdFilePrefixLen, L"%d_%d.dmp", ProcessId, g_aszwVBoxMdDumpCount);
|
---|
| 1254 |
|
---|
| 1255 | hFile = CreateFileW(aszwMdFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
---|
| 1256 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
| 1257 | return GetLastError();
|
---|
| 1258 |
|
---|
| 1259 | ExceptionInfo.ThreadId = GetCurrentThreadId();
|
---|
| 1260 | ExceptionInfo.ExceptionPointers = pExceptionInfo;
|
---|
| 1261 | ExceptionInfo.ClientPointers = FALSE;
|
---|
| 1262 |
|
---|
| 1263 | if (!g_pfnVBoxMdMiniDumpWriteDump(hProcess, ProcessId, hFile, g_enmVBoxMdDumpType, &ExceptionInfo, NULL, NULL))
|
---|
| 1264 | winErr = GetLastError();
|
---|
| 1265 |
|
---|
| 1266 | CloseHandle(hFile);
|
---|
| 1267 | return winErr;
|
---|
| 1268 | }
|
---|
| 1269 |
|
---|
[39485] | 1270 | LONG WINAPI vboxVDbgVectoredHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
|
---|
| 1271 | {
|
---|
| 1272 | PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
|
---|
| 1273 | PCONTEXT pContextRecord = pExceptionInfo->ContextRecord;
|
---|
| 1274 | switch (pExceptionRecord->ExceptionCode)
|
---|
| 1275 | {
|
---|
| 1276 | case EXCEPTION_BREAKPOINT:
|
---|
| 1277 | case EXCEPTION_ACCESS_VIOLATION:
|
---|
| 1278 | case EXCEPTION_STACK_OVERFLOW:
|
---|
| 1279 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
---|
| 1280 | case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
---|
| 1281 | case EXCEPTION_FLT_INVALID_OPERATION:
|
---|
| 1282 | case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
---|
| 1283 | case EXCEPTION_ILLEGAL_INSTRUCTION:
|
---|
[47151] | 1284 | if (g_VBoxVehFlags & VBOXVEH_F_BREAK)
|
---|
| 1285 | {
|
---|
| 1286 | BOOL fBreak = TRUE;
|
---|
[48057] | 1287 | #ifndef DEBUG_misha
|
---|
[47151] | 1288 | if (pExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
|
---|
| 1289 | {
|
---|
| 1290 | HANDLE hProcess = GetCurrentProcess();
|
---|
| 1291 | BOOL fDebuggerPresent = FALSE;
|
---|
| 1292 | /* we do not want to generate breakpoint exceptions recursively, so do it only when running under debugger */
|
---|
| 1293 | if (CheckRemoteDebuggerPresent(hProcess, &fDebuggerPresent))
|
---|
| 1294 | fBreak = !!fDebuggerPresent;
|
---|
| 1295 | else
|
---|
| 1296 | fBreak = FALSE; /* <- the function has failed, don't break for sanity */
|
---|
| 1297 | }
|
---|
[48057] | 1298 | #endif
|
---|
[47151] | 1299 |
|
---|
| 1300 | if (fBreak)
|
---|
| 1301 | {
|
---|
| 1302 | RT_BREAKPOINT();
|
---|
| 1303 | }
|
---|
| 1304 | }
|
---|
| 1305 |
|
---|
| 1306 | if (g_VBoxVehFlags & VBOXVEH_F_DUMP)
|
---|
| 1307 | vboxMdMinidumpCreate(pExceptionInfo);
|
---|
| 1308 |
|
---|
| 1309 | if (g_VBoxVehFlags & VBOXVEH_F_EXIT)
|
---|
[43487] | 1310 | exit(1);
|
---|
[39485] | 1311 | break;
|
---|
| 1312 | default:
|
---|
| 1313 | break;
|
---|
| 1314 | }
|
---|
| 1315 | return EXCEPTION_CONTINUE_SEARCH;
|
---|
| 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | void vboxVDbgVEHandlerRegister()
|
---|
| 1319 | {
|
---|
[47151] | 1320 | CRASSERT(!g_VBoxVehHandler);
|
---|
| 1321 | g_VBoxVehHandler = AddVectoredExceptionHandler(1,vboxVDbgVectoredHandler);
|
---|
| 1322 | CRASSERT(g_VBoxVehHandler);
|
---|
[39485] | 1323 | }
|
---|
| 1324 |
|
---|
| 1325 | void vboxVDbgVEHandlerUnregister()
|
---|
| 1326 | {
|
---|
| 1327 | ULONG uResult;
|
---|
[47151] | 1328 | if (g_VBoxVehHandler)
|
---|
[39485] | 1329 | {
|
---|
[47151] | 1330 | uResult = RemoveVectoredExceptionHandler(g_VBoxVehHandler);
|
---|
[39485] | 1331 | CRASSERT(uResult);
|
---|
[47151] | 1332 | g_VBoxVehHandler = NULL;
|
---|
[39485] | 1333 | }
|
---|
| 1334 | }
|
---|
| 1335 | #endif
|
---|
| 1336 |
|
---|
[15532] | 1337 | /* Windows crap */
|
---|
| 1338 | BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
|
---|
| 1339 | {
|
---|
| 1340 | (void) lpvReserved;
|
---|
| 1341 |
|
---|
[38636] | 1342 | switch (fdwReason)
|
---|
[15532] | 1343 | {
|
---|
| 1344 | case DLL_PROCESS_ATTACH:
|
---|
| 1345 | {
|
---|
| 1346 | CRNetServer ns;
|
---|
[51673] | 1347 | const char * env;
|
---|
[52437] | 1348 | #if defined(DEBUG_misha)
|
---|
| 1349 | HMODULE hCrUtil;
|
---|
| 1350 | char aName[MAX_PATH];
|
---|
[15532] | 1351 |
|
---|
[52437] | 1352 | GetModuleFileNameA(hDLLInst, aName, RT_ELEMENTS(aName));
|
---|
| 1353 | crDbgCmdSymLoadPrint(aName, hDLLInst);
|
---|
| 1354 |
|
---|
| 1355 | hCrUtil = GetModuleHandleA("VBoxOGLcrutil.dll");
|
---|
| 1356 | Assert(hCrUtil);
|
---|
| 1357 | crDbgCmdSymLoadPrint("VBoxOGLcrutil.dll", hCrUtil);
|
---|
| 1358 | #endif
|
---|
[39568] | 1359 | #ifdef CHROMIUM_THREADSAFE
|
---|
| 1360 | crInitTSD(&g_stubCurrentContextTSD);
|
---|
| 1361 | #endif
|
---|
| 1362 |
|
---|
[39485] | 1363 | crInitMutex(&stub_init_mutex);
|
---|
| 1364 |
|
---|
| 1365 | #ifdef VDBG_VEHANDLER
|
---|
[51673] | 1366 | env = crGetenv("CR_DBG_VEH_ENABLE");
|
---|
| 1367 | g_VBoxVehEnable = crStrParseI32(env,
|
---|
[48057] | 1368 | # ifdef DEBUG_misha
|
---|
[51673] | 1369 | 1
|
---|
| 1370 | # else
|
---|
| 1371 | 0
|
---|
[48057] | 1372 | # endif
|
---|
[51673] | 1373 | );
|
---|
| 1374 |
|
---|
[47151] | 1375 | if (g_VBoxVehEnable)
|
---|
[51673] | 1376 | {
|
---|
| 1377 | char procName[1024];
|
---|
| 1378 | size_t cProcName;
|
---|
| 1379 | size_t cChars;
|
---|
| 1380 |
|
---|
| 1381 | env = crGetenv("CR_DBG_VEH_FLAGS");
|
---|
| 1382 | g_VBoxVehFlags = crStrParseI32(env,
|
---|
| 1383 | 0
|
---|
| 1384 | # ifdef DEBUG_misha
|
---|
| 1385 | | VBOXVEH_F_BREAK
|
---|
| 1386 | # else
|
---|
| 1387 | | VBOXVEH_F_DUMP
|
---|
| 1388 | # endif
|
---|
| 1389 | );
|
---|
| 1390 |
|
---|
| 1391 | env = crGetenv("CR_DBG_VEH_DUMP_DIR");
|
---|
| 1392 | if (!env)
|
---|
| 1393 | env = VBOXMD_DUMP_DIR_DEFAULT;
|
---|
| 1394 |
|
---|
| 1395 | g_cVBoxMdFilePrefixLen = strlen(env);
|
---|
| 1396 |
|
---|
| 1397 | if (RT_ELEMENTS(g_aszwVBoxMdFilePrefix) <= g_cVBoxMdFilePrefixLen + 26 + (sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR)) / sizeof (WCHAR))
|
---|
| 1398 | {
|
---|
| 1399 | g_cVBoxMdFilePrefixLen = 0;
|
---|
| 1400 | env = "";
|
---|
| 1401 | }
|
---|
| 1402 |
|
---|
| 1403 | mbstowcs_s(&cChars, g_aszwVBoxMdFilePrefix, g_cVBoxMdFilePrefixLen + 1, env, _TRUNCATE);
|
---|
| 1404 |
|
---|
| 1405 | Assert(cChars == g_cVBoxMdFilePrefixLen + 1);
|
---|
| 1406 |
|
---|
| 1407 | g_cVBoxMdFilePrefixLen = cChars - 1;
|
---|
| 1408 |
|
---|
| 1409 | if (g_cVBoxMdFilePrefixLen && g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen - 1] != L'\\')
|
---|
| 1410 | g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen++] = L'\\';
|
---|
| 1411 |
|
---|
| 1412 | memcpy(g_aszwVBoxMdFilePrefix + g_cVBoxMdFilePrefixLen, VBOXMD_DUMP_NAME_PREFIX_W, sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR));
|
---|
| 1413 | g_cVBoxMdFilePrefixLen += (sizeof (VBOXMD_DUMP_NAME_PREFIX_W) - sizeof (WCHAR)) / sizeof (WCHAR);
|
---|
| 1414 |
|
---|
| 1415 | crGetProcName(procName, RT_ELEMENTS(procName));
|
---|
| 1416 | cProcName = strlen(procName);
|
---|
| 1417 |
|
---|
| 1418 | if (RT_ELEMENTS(g_aszwVBoxMdFilePrefix) > g_cVBoxMdFilePrefixLen + cProcName + 1 + 26)
|
---|
| 1419 | {
|
---|
| 1420 | mbstowcs_s(&cChars, g_aszwVBoxMdFilePrefix + g_cVBoxMdFilePrefixLen, cProcName + 1, procName, _TRUNCATE);
|
---|
| 1421 | Assert(cChars == cProcName + 1);
|
---|
| 1422 | g_cVBoxMdFilePrefixLen += cChars - 1;
|
---|
| 1423 | g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen++] = L'_';
|
---|
| 1424 | }
|
---|
| 1425 |
|
---|
| 1426 | /* sanity */
|
---|
| 1427 | g_aszwVBoxMdFilePrefix[g_cVBoxMdFilePrefixLen] = L'\0';
|
---|
| 1428 |
|
---|
| 1429 | env = crGetenv("CR_DBG_VEH_DUMP_TYPE");
|
---|
| 1430 |
|
---|
| 1431 | g_enmVBoxMdDumpType = crStrParseI32(env,
|
---|
| 1432 | MiniDumpNormal
|
---|
| 1433 | | MiniDumpWithDataSegs
|
---|
| 1434 | | MiniDumpWithFullMemory
|
---|
| 1435 | | MiniDumpWithHandleData
|
---|
| 1436 | //// | MiniDumpFilterMemory
|
---|
| 1437 | //// | MiniDumpScanMemory
|
---|
| 1438 | // | MiniDumpWithUnloadedModules
|
---|
| 1439 | //// | MiniDumpWithIndirectlyReferencedMemory
|
---|
| 1440 | //// | MiniDumpFilterModulePaths
|
---|
| 1441 | // | MiniDumpWithProcessThreadData
|
---|
| 1442 | // | MiniDumpWithPrivateReadWriteMemory
|
---|
| 1443 | //// | MiniDumpWithoutOptionalData
|
---|
| 1444 | // | MiniDumpWithFullMemoryInfo
|
---|
| 1445 | // | MiniDumpWithThreadInfo
|
---|
| 1446 | // | MiniDumpWithCodeSegs
|
---|
| 1447 | // | MiniDumpWithFullAuxiliaryState
|
---|
| 1448 | // | MiniDumpWithPrivateWriteCopyMemory
|
---|
| 1449 | // | MiniDumpIgnoreInaccessibleMemory
|
---|
| 1450 | // | MiniDumpWithTokenInformation
|
---|
| 1451 | //// | MiniDumpWithModuleHeaders
|
---|
| 1452 | //// | MiniDumpFilterTriage
|
---|
| 1453 | );
|
---|
| 1454 |
|
---|
[47151] | 1455 | vboxVDbgVEHandlerRegister();
|
---|
[51673] | 1456 | }
|
---|
[39485] | 1457 | #endif
|
---|
| 1458 |
|
---|
[15532] | 1459 | crNetInit(NULL, NULL);
|
---|
| 1460 | ns.name = "vboxhgcm://host:0";
|
---|
| 1461 | ns.buffer_size = 1024;
|
---|
[42499] | 1462 | crNetServerConnect(&ns
|
---|
| 1463 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
| 1464 | , NULL
|
---|
| 1465 | #endif
|
---|
| 1466 | );
|
---|
[15532] | 1467 | if (!ns.conn)
|
---|
| 1468 | {
|
---|
| 1469 | crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
|
---|
[39485] | 1470 | #ifdef VDBG_VEHANDLER
|
---|
[47151] | 1471 | if (g_VBoxVehEnable)
|
---|
| 1472 | vboxVDbgVEHandlerUnregister();
|
---|
[39485] | 1473 | #endif
|
---|
[15532] | 1474 | return FALSE;
|
---|
| 1475 | }
|
---|
| 1476 | else
|
---|
[46757] | 1477 | {
|
---|
[15532] | 1478 | crNetFreeConnection(ns.conn);
|
---|
[46757] | 1479 | }
|
---|
[15532] | 1480 |
|
---|
[55825] | 1481 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
| 1482 | VBoxCrHgsmiInit();
|
---|
| 1483 | #endif
|
---|
[15532] | 1484 | break;
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | case DLL_PROCESS_DETACH:
|
---|
| 1488 | {
|
---|
[40431] | 1489 | /* do exactly the same thing as for DLL_THREAD_DETACH since
|
---|
| 1490 | * DLL_THREAD_DETACH is not called for the thread doing DLL_PROCESS_DETACH according to msdn docs */
|
---|
| 1491 | stubSetCurrentContext(NULL);
|
---|
| 1492 | if (stub_initialized)
|
---|
| 1493 | {
|
---|
| 1494 | CRASSERT(stub.spu);
|
---|
| 1495 | stub.spu->dispatch_table.VBoxDetachThread();
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
[55825] | 1498 |
|
---|
| 1499 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
| 1500 | VBoxCrHgsmiTerm();
|
---|
| 1501 | #endif
|
---|
| 1502 |
|
---|
[15532] | 1503 | stubSPUSafeTearDown();
|
---|
[39485] | 1504 |
|
---|
[39602] | 1505 | #ifdef CHROMIUM_THREADSAFE
|
---|
| 1506 | crFreeTSD(&g_stubCurrentContextTSD);
|
---|
| 1507 | #endif
|
---|
| 1508 |
|
---|
[39485] | 1509 | #ifdef VDBG_VEHANDLER
|
---|
[47151] | 1510 | if (g_VBoxVehEnable)
|
---|
| 1511 | vboxVDbgVEHandlerUnregister();
|
---|
[39485] | 1512 | #endif
|
---|
[15532] | 1513 | break;
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
| 1516 | case DLL_THREAD_ATTACH:
|
---|
[33988] | 1517 | {
|
---|
| 1518 | if (stub_initialized)
|
---|
| 1519 | {
|
---|
| 1520 | CRASSERT(stub.spu);
|
---|
[40431] | 1521 | stub.spu->dispatch_table.VBoxAttachThread();
|
---|
[33988] | 1522 | }
|
---|
[15532] | 1523 | break;
|
---|
[33988] | 1524 | }
|
---|
[15532] | 1525 |
|
---|
| 1526 | case DLL_THREAD_DETACH:
|
---|
[33988] | 1527 | {
|
---|
[39568] | 1528 | stubSetCurrentContext(NULL);
|
---|
[33988] | 1529 | if (stub_initialized)
|
---|
| 1530 | {
|
---|
| 1531 | CRASSERT(stub.spu);
|
---|
[40431] | 1532 | stub.spu->dispatch_table.VBoxDetachThread();
|
---|
[33988] | 1533 | }
|
---|
[15532] | 1534 | break;
|
---|
[33988] | 1535 | }
|
---|
[15532] | 1536 |
|
---|
| 1537 | default:
|
---|
| 1538 | break;
|
---|
| 1539 | }
|
---|
| 1540 |
|
---|
| 1541 | return TRUE;
|
---|
| 1542 | }
|
---|
| 1543 | #endif
|
---|