Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1059 lines
35 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. gdisup.c
  5. Abstract:
  6. This is the NT Watchdog driver implementation.
  7. This module implements support routines for
  8. watchdog in win32k.
  9. Author:
  10. Michael Maciesowicz (mmacie) 05-May-2000
  11. Environment:
  12. Kernel mode only.
  13. Notes:
  14. This module cannot be moved to win32k since routines defined here can
  15. be called at any time and it is possible that win32k may not be mapped
  16. into running process space at this time (e.g. TS session).
  17. Revision History:
  18. --*/
  19. //
  20. // TODO: This module needs major rework.
  21. //
  22. // 1. We should eliminate all global variables from here and move them into
  23. // GDI context structure.
  24. //
  25. // 2. We should extract generic logging routines
  26. // (e.g. WdWriteErrorLogEntry(pdo, className), WdWriteEventToRegistry(...),
  27. // WdBreakPoint(...) so we can use them for any device class, not just Display.
  28. //
  29. // 3. We should use IoAllocateWorkItem - we could drop some globals then.
  30. //
  31. #include "gdisup.h"
  32. #ifdef ALLOC_PRAGMA
  33. #pragma alloc_text (PAGE, WdpBugCheckStuckDriver)
  34. #endif
  35. WD_BUGCHECK_DATA
  36. g_WdpBugCheckData = {0, 0, 0, 0, 0};
  37. WORK_QUEUE_ITEM
  38. g_WdpWorkQueueItem;
  39. LONG
  40. g_lWdpDisplayHandlerState = WD_HANDLER_IDLE;
  41. WATCHDOGAPI
  42. VOID
  43. WdDdiWatchdogDpcCallback(
  44. IN PKDPC pDpc,
  45. IN PVOID pDeferredContext,
  46. IN PVOID pSystemArgument1,
  47. IN PVOID pSystemArgument2
  48. )
  49. /*++
  50. Routine Description:
  51. This function is a DPC callback routine for GDI watchdog. It is only
  52. called when GDI watchdog times out before it is cancelled. It schedules
  53. a work item to bugcheck the machine in the context of system worker
  54. thread.
  55. Arguments:
  56. pDpc - Supplies a pointer to a DPC object.
  57. pDeferredContext - Supplies a pointer to a GDI defined context.
  58. pSystemArgument1 - Supplies a pointer to a spinning thread object (PKTHREAD).
  59. pSystemArgument2 - Supplies a pointer to a watchdog object (PDEFERRED_WATCHDOG).
  60. Return Value:
  61. None.
  62. --*/
  63. {
  64. //
  65. // Make sure we handle only one event at the time.
  66. //
  67. // Note: Timeout and recovery events for the same watchdog object are
  68. // synchronized already in timer DPC.
  69. //
  70. WDD_TRACE_CALL((PDEFERRED_WATCHDOG)pSystemArgument1, WddWdDdiWatchdogDpcCallback);
  71. if (InterlockedCompareExchange(&g_lWdpDisplayHandlerState,
  72. WD_HANDLER_BUSY,
  73. WD_HANDLER_IDLE) == WD_HANDLER_IDLE)
  74. {
  75. g_WdpBugCheckData.ulBugCheckCode = THREAD_STUCK_IN_DEVICE_DRIVER;
  76. g_WdpBugCheckData.ulpBugCheckParameter1 = (ULONG_PTR)pSystemArgument1;
  77. g_WdpBugCheckData.ulpBugCheckParameter2 = (ULONG_PTR)pSystemArgument2;
  78. g_WdpBugCheckData.ulpBugCheckParameter3 = (ULONG_PTR)pDeferredContext;
  79. g_WdpBugCheckData.ulpBugCheckParameter4++;
  80. ExInitializeWorkItem(&g_WdpWorkQueueItem, WdpBugCheckStuckDriver, &g_WdpBugCheckData);
  81. ExQueueWorkItem(&g_WdpWorkQueueItem, CriticalWorkQueue);
  82. }
  83. else
  84. {
  85. //
  86. // Resume watchdog event processing.
  87. //
  88. WdCompleteEvent(pSystemArgument2, (PKTHREAD)pSystemArgument1);
  89. }
  90. return;
  91. } // WdDdiWatchdogDpcCallback()
  92. VOID
  93. WdpBugCheckStuckDriver(
  94. IN PVOID pvContext
  95. )
  96. /*++
  97. Routine Description:
  98. This function is a worker callback routine for GDI watchdog DPC.
  99. Arguments:
  100. pvContext - Supplies a pointer to a watchdog defined context.
  101. Return Value:
  102. None.
  103. --*/
  104. {
  105. static BOOLEAN s_bFirstTime = TRUE;
  106. static BOOLEAN s_bDbgBreak = FALSE;
  107. static BOOLEAN s_bEventLogged = FALSE;
  108. static ULONG s_ulTrapOnce = WD_DEFAULT_TRAP_ONCE;
  109. static ULONG s_ulDisableBugcheck = WD_DEFAULT_DISABLE_BUGCHECK;
  110. static ULONG s_ulBreakPointDelay = WD_GDI_STRESS_BREAK_POINT_DELAY;
  111. static ULONG s_ulCurrentBreakPointDelay = WD_GDI_STRESS_BREAK_POINT_DELAY;
  112. static ULONG s_ulBreakCount = 0;
  113. static ULONG s_ulEventCount = 0;
  114. static ULONG s_ulEaRecovery = 0;
  115. static ULONG s_ulFullRecovery = 0;
  116. PWD_BUGCHECK_DATA pBugCheckData;
  117. PKTHREAD pThread;
  118. PDEFERRED_WATCHDOG pWatch;
  119. PUNICODE_STRING pUnicodeDriverName;
  120. PDEVICE_OBJECT pFdo;
  121. PDEVICE_OBJECT pPdo;
  122. PWD_GDI_DPC_CONTEXT pDpcContext;
  123. NTSTATUS ntStatus;
  124. WD_EVENT_TYPE lastEvent;
  125. PAGED_CODE();
  126. ASSERT(NULL != pvContext);
  127. pBugCheckData = (PWD_BUGCHECK_DATA)pvContext;
  128. pThread = (PKTHREAD)(pBugCheckData->ulpBugCheckParameter1);
  129. pWatch = (PDEFERRED_WATCHDOG)(pBugCheckData->ulpBugCheckParameter2);
  130. pDpcContext = (PWD_GDI_DPC_CONTEXT)(pBugCheckData->ulpBugCheckParameter3);
  131. ASSERT(NULL != pDpcContext);
  132. pUnicodeDriverName = &(pDpcContext->DisplayDriverName);
  133. WDD_TRACE_CALL(pWatch, WddWdpBugCheckStuckDriver);
  134. //
  135. // Note: pThread is NULL for recovery events.
  136. //
  137. ASSERT(NULL != pWatch);
  138. ASSERT(NULL != pUnicodeDriverName);
  139. pFdo = WdGetDeviceObject(pWatch);
  140. pPdo = WdGetLowestDeviceObject(pWatch);
  141. ASSERT(NULL != pFdo);
  142. ASSERT(NULL != pPdo);
  143. lastEvent = WdGetLastEvent(pWatch);
  144. ASSERT((WdTimeoutEvent == lastEvent) || (WdRecoveryEvent == lastEvent));
  145. //
  146. // Grab configuration data from the registry on first timeout.
  147. //
  148. if (TRUE == s_bFirstTime)
  149. {
  150. ULONG ulDefaultTrapOnce = WD_DEFAULT_TRAP_ONCE;
  151. ULONG ulDefaultDisableBugcheck = WD_DEFAULT_DISABLE_BUGCHECK;
  152. ULONG ulDefaultBreakPointDelay = WD_GDI_STRESS_BREAK_POINT_DELAY;
  153. ULONG ulDefaultBreakCount = 0;
  154. ULONG ulDefaultEventCount = 0;
  155. ULONG ulDefaultEaRecovery = 0;
  156. ULONG ulDefaultFullRecovery = 0;
  157. RTL_QUERY_REGISTRY_TABLE queryTable[] =
  158. {
  159. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"TrapOnce", &s_ulTrapOnce, REG_DWORD, &ulDefaultTrapOnce, 4},
  160. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"DisableBugcheck", &s_ulDisableBugcheck, REG_DWORD, &ulDefaultDisableBugcheck, 4},
  161. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"BreakPointDelay", &s_ulBreakPointDelay, REG_DWORD, &ulDefaultBreakPointDelay, 4},
  162. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"BreakCount", &s_ulBreakCount, REG_DWORD, &ulDefaultBreakCount, 4},
  163. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"EaRecovery", &s_ulEaRecovery, REG_DWORD, &ulDefaultEaRecovery, 4},
  164. {NULL, RTL_QUERY_REGISTRY_DIRECT, L"FullRecovery", &s_ulFullRecovery, REG_DWORD, &ulDefaultFullRecovery, 4},
  165. {NULL, 0, NULL}
  166. };
  167. //
  168. // Get configurable values and accumulated statistics from registry.
  169. //
  170. RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
  171. WD_KEY_WATCHDOG_DISPLAY,
  172. queryTable,
  173. NULL,
  174. NULL);
  175. //
  176. // Rolling down counter to workaround GDI slowness in some stress cases.
  177. //
  178. s_ulCurrentBreakPointDelay = s_ulBreakPointDelay;
  179. #if !defined(_X86_) && !defined(_IA64_)
  180. //
  181. // For now, only recover on x86 and ia64.
  182. //
  183. s_ulEaRecovery = 0;
  184. #endif
  185. }
  186. //
  187. // Handle current event.
  188. //
  189. if (WdTimeoutEvent == lastEvent)
  190. {
  191. //
  192. // Timeout.
  193. //
  194. ULONG ulDebuggerNotPresent;
  195. BOOLEAN bBreakIn;
  196. ASSERT(NULL != pThread);
  197. ulDebuggerNotPresent = 1;
  198. bBreakIn = FALSE;
  199. KdRefreshDebuggerNotPresent();
  200. if ((TRUE == KD_DEBUGGER_ENABLED) && (FALSE == KD_DEBUGGER_NOT_PRESENT))
  201. {
  202. //
  203. // Give a chance to debug a spinning code if kernel debugger is connected.
  204. //
  205. ulDebuggerNotPresent = 0;
  206. if ((0 == s_ulTrapOnce) || (FALSE == s_bDbgBreak))
  207. {
  208. //
  209. // Print out info to debugger and break in if we timed out enought times already.
  210. // Hopefuly one day GDI becomes fast enough and we won't have to set any delays.
  211. //
  212. if (0 == s_ulCurrentBreakPointDelay)
  213. {
  214. s_ulCurrentBreakPointDelay = s_ulBreakPointDelay;
  215. DbgPrint("\n");
  216. DbgPrint("*******************************************************************************\n");
  217. DbgPrint("* *\n");
  218. DbgPrint("* The watchdog detected a timeout condition. We broke into the debugger to *\n");
  219. DbgPrint("* allow a chance for debugging this failure. *\n");
  220. DbgPrint("* *\n");
  221. DbgPrint("* Intercepted bugcheck code and arguments are listed below this message. *\n");
  222. DbgPrint("* You can use them the same way as you would in case of the actual break, *\n");
  223. DbgPrint("* i.e. execute .thread Arg1 then kv to identify an offending thread. *\n");
  224. DbgPrint("* *\n");
  225. DbgPrint("*******************************************************************************\n");
  226. DbgPrint("\n");
  227. DbgPrint("*** Intercepted Fatal System Error: 0x%08X\n", pBugCheckData->ulBugCheckCode);
  228. DbgPrint(" (0x%p,0x%p,0x%p,0x%p)\n\n",
  229. pBugCheckData->ulpBugCheckParameter1,
  230. pBugCheckData->ulpBugCheckParameter2,
  231. pBugCheckData->ulpBugCheckParameter3,
  232. pBugCheckData->ulpBugCheckParameter4);
  233. DbgPrint("Driver at fault: %ws\n\n", pUnicodeDriverName->Buffer);
  234. bBreakIn = TRUE;
  235. s_bDbgBreak = TRUE;
  236. s_ulBreakCount++;
  237. }
  238. else
  239. {
  240. DbgPrint("Watchdog: Timeout in %ws. Break in %d\n",
  241. pUnicodeDriverName->Buffer,
  242. s_ulCurrentBreakPointDelay);
  243. s_ulCurrentBreakPointDelay--;
  244. }
  245. }
  246. //
  247. // Make sure we won't bugcheck if we have kernel debugger connected.
  248. //
  249. s_ulDisableBugcheck = 1;
  250. }
  251. else if (0 == s_ulDisableBugcheck)
  252. {
  253. s_ulBreakCount++;
  254. }
  255. //
  256. // Log error (only once unless we recover).
  257. //
  258. if ((FALSE == s_bEventLogged) && ((TRUE == bBreakIn) || ulDebuggerNotPresent))
  259. {
  260. PIO_ERROR_LOG_PACKET pIoErrorLogPacket;
  261. ULONG ulPacketSize;
  262. USHORT usNumberOfStrings;
  263. PWCHAR wszDeviceClass = L"display";
  264. ULONG ulClassSize = sizeof (L"display");
  265. ulPacketSize = sizeof (IO_ERROR_LOG_PACKET);
  266. usNumberOfStrings = 0;
  267. //
  268. // For event log message:
  269. //
  270. // %1 = fixed device description (this is set by event log itself)
  271. // %2 = string 1 = device class starting in lower case
  272. // %3 = string 2 = driver name
  273. //
  274. if ((ulPacketSize + ulClassSize) <= ERROR_LOG_MAXIMUM_SIZE)
  275. {
  276. ulPacketSize += ulClassSize;
  277. usNumberOfStrings++;
  278. //
  279. // We're looking at MaximumLength since it includes terminating UNICODE_NULL.
  280. //
  281. if ((ulPacketSize + pUnicodeDriverName->MaximumLength) <= ERROR_LOG_MAXIMUM_SIZE)
  282. {
  283. ulPacketSize += pUnicodeDriverName->MaximumLength;
  284. usNumberOfStrings++;
  285. }
  286. }
  287. pIoErrorLogPacket = IoAllocateErrorLogEntry(pFdo, (UCHAR)ulPacketSize);
  288. if (pIoErrorLogPacket)
  289. {
  290. pIoErrorLogPacket->MajorFunctionCode = 0;
  291. pIoErrorLogPacket->RetryCount = 0;
  292. pIoErrorLogPacket->DumpDataSize = 0;
  293. pIoErrorLogPacket->NumberOfStrings = usNumberOfStrings;
  294. pIoErrorLogPacket->StringOffset = (USHORT)FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData);
  295. pIoErrorLogPacket->EventCategory = 0;
  296. pIoErrorLogPacket->ErrorCode = IO_ERR_THREAD_STUCK_IN_DEVICE_DRIVER;
  297. pIoErrorLogPacket->UniqueErrorValue = 0;
  298. pIoErrorLogPacket->FinalStatus = STATUS_SUCCESS;
  299. pIoErrorLogPacket->SequenceNumber = 0;
  300. pIoErrorLogPacket->IoControlCode = 0;
  301. pIoErrorLogPacket->DeviceOffset.QuadPart = 0;
  302. if (usNumberOfStrings > 0)
  303. {
  304. RtlCopyMemory(&(pIoErrorLogPacket->DumpData[0]),
  305. wszDeviceClass,
  306. ulClassSize);
  307. if (usNumberOfStrings > 1)
  308. {
  309. RtlCopyMemory((PUCHAR)&(pIoErrorLogPacket->DumpData[0]) + ulClassSize,
  310. pUnicodeDriverName->Buffer,
  311. pUnicodeDriverName->MaximumLength);
  312. }
  313. }
  314. IoWriteErrorLogEntry(pIoErrorLogPacket);
  315. s_bEventLogged = TRUE;
  316. }
  317. }
  318. //
  319. // Write reliability info into registry. Setting ShutdownEventPending will trigger winlogon
  320. // to run savedump where we're doing our boot-time handling of watchdog events for DrWatson.
  321. //
  322. // Note: We are only allowed to set ShutdownEventPending, savedump is the only component
  323. // allowed to clear this value. Even if we recover from watchdog timeout we'll keep this
  324. // value set, savedump will be able to figure out if we recovered or not.
  325. //
  326. if (TRUE == s_bFirstTime)
  327. {
  328. ULONG ulValue = 1;
  329. //
  330. // Set ShutdownEventPending flag.
  331. //
  332. ntStatus = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  333. WD_KEY_RELIABILITY,
  334. L"ShutdownEventPending",
  335. REG_DWORD,
  336. &ulValue,
  337. sizeof (ulValue));
  338. if (NT_SUCCESS(ntStatus))
  339. {
  340. WdpFlushRegistryKey(pWatch, WD_KEY_RELIABILITY);
  341. }
  342. else
  343. {
  344. //
  345. // Reliability key should be always reliable there.
  346. //
  347. ASSERT(FALSE);
  348. }
  349. }
  350. //
  351. // Write watchdog event info into registry.
  352. //
  353. if ((0 == s_ulTrapOnce) || (TRUE == s_bFirstTime))
  354. {
  355. //
  356. // Is Watchdog\Display key already there?
  357. //
  358. ntStatus = RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE,
  359. WD_KEY_WATCHDOG_DISPLAY);
  360. if (!NT_SUCCESS(ntStatus))
  361. {
  362. //
  363. // Is Watchdog key already there?
  364. //
  365. ntStatus = RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE,
  366. WD_KEY_WATCHDOG);
  367. if (!NT_SUCCESS(ntStatus))
  368. {
  369. //
  370. // Create a new key.
  371. //
  372. ntStatus = RtlCreateRegistryKey(RTL_REGISTRY_ABSOLUTE,
  373. WD_KEY_WATCHDOG);
  374. }
  375. if (NT_SUCCESS(ntStatus))
  376. {
  377. //
  378. // Create a new key.
  379. //
  380. ntStatus = RtlCreateRegistryKey(RTL_REGISTRY_ABSOLUTE,
  381. WD_KEY_WATCHDOG_DISPLAY);
  382. }
  383. }
  384. if (NT_SUCCESS(ntStatus))
  385. {
  386. PVOID pvPropertyBuffer;
  387. ULONG ulLength;
  388. ULONG ulValue;
  389. //
  390. // Set values maintained by watchdog.
  391. //
  392. ulValue = 1;
  393. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  394. WD_KEY_WATCHDOG_DISPLAY,
  395. L"EventFlag",
  396. REG_DWORD,
  397. &ulValue,
  398. sizeof (ulValue));
  399. s_ulEventCount++;
  400. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  401. WD_KEY_WATCHDOG_DISPLAY,
  402. L"EventCount",
  403. REG_DWORD,
  404. &s_ulEventCount,
  405. sizeof (s_ulEventCount));
  406. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  407. WD_KEY_WATCHDOG_DISPLAY,
  408. L"BreakCount",
  409. REG_DWORD,
  410. &s_ulBreakCount,
  411. sizeof (s_ulBreakCount));
  412. ulValue = !s_ulDisableBugcheck;
  413. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  414. WD_KEY_WATCHDOG_DISPLAY,
  415. L"BugcheckTriggered",
  416. REG_DWORD,
  417. &ulValue,
  418. sizeof (ulValue));
  419. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  420. WD_KEY_WATCHDOG_DISPLAY,
  421. L"DebuggerNotPresent",
  422. REG_DWORD,
  423. &ulDebuggerNotPresent,
  424. sizeof (ulDebuggerNotPresent));
  425. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  426. WD_KEY_WATCHDOG_DISPLAY,
  427. L"DriverName",
  428. REG_SZ,
  429. pUnicodeDriverName->Buffer,
  430. pUnicodeDriverName->MaximumLength);
  431. //
  432. // Delete other values in case allocation or property read fails.
  433. //
  434. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  435. WD_KEY_WATCHDOG_DISPLAY,
  436. L"DeviceClass");
  437. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  438. WD_KEY_WATCHDOG_DISPLAY,
  439. L"DeviceDescription");
  440. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  441. WD_KEY_WATCHDOG_DISPLAY,
  442. L"DeviceFriendlyName");
  443. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  444. WD_KEY_WATCHDOG_DISPLAY,
  445. L"HardwareID");
  446. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  447. WD_KEY_WATCHDOG_DISPLAY,
  448. L"Manufacturer");
  449. //
  450. // Allocate buffer for device properties reads.
  451. //
  452. // Note: Legacy devices don't have PDOs and we can't query properties
  453. // for them. Calling IoGetDeviceProperty() with FDO upsets Verifier.
  454. // In legacy case lowest device object is the same as FDO, we check
  455. // against this and if this is the case we won't allocate property
  456. // buffer and we'll skip the next block.
  457. //
  458. if (pFdo != pPdo)
  459. {
  460. pvPropertyBuffer = ExAllocatePoolWithTag(PagedPool,
  461. WD_MAX_PROPERTY_SIZE,
  462. WD_TAG);
  463. }
  464. else
  465. {
  466. pvPropertyBuffer = NULL;
  467. }
  468. if (pvPropertyBuffer)
  469. {
  470. //
  471. // Read and save device properties.
  472. //
  473. ntStatus = IoGetDeviceProperty(pPdo,
  474. DevicePropertyClassName,
  475. WD_MAX_PROPERTY_SIZE,
  476. pvPropertyBuffer,
  477. &ulLength);
  478. if (NT_SUCCESS(ntStatus))
  479. {
  480. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  481. WD_KEY_WATCHDOG_DISPLAY,
  482. L"DeviceClass",
  483. REG_SZ,
  484. pvPropertyBuffer,
  485. ulLength);
  486. }
  487. ntStatus = IoGetDeviceProperty(pPdo,
  488. DevicePropertyDeviceDescription,
  489. WD_MAX_PROPERTY_SIZE,
  490. pvPropertyBuffer,
  491. &ulLength);
  492. if (NT_SUCCESS(ntStatus))
  493. {
  494. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  495. WD_KEY_WATCHDOG_DISPLAY,
  496. L"DeviceDescription",
  497. REG_SZ,
  498. pvPropertyBuffer,
  499. ulLength);
  500. }
  501. ntStatus = IoGetDeviceProperty(pPdo,
  502. DevicePropertyFriendlyName,
  503. WD_MAX_PROPERTY_SIZE,
  504. pvPropertyBuffer,
  505. &ulLength);
  506. if (NT_SUCCESS(ntStatus))
  507. {
  508. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  509. WD_KEY_WATCHDOG_DISPLAY,
  510. L"DeviceFriendlyName",
  511. REG_SZ,
  512. pvPropertyBuffer,
  513. ulLength);
  514. }
  515. ntStatus = IoGetDeviceProperty(pPdo,
  516. DevicePropertyHardwareID,
  517. WD_MAX_PROPERTY_SIZE,
  518. pvPropertyBuffer,
  519. &ulLength);
  520. if (NT_SUCCESS(ntStatus))
  521. {
  522. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  523. WD_KEY_WATCHDOG_DISPLAY,
  524. L"HardwareID",
  525. REG_MULTI_SZ,
  526. pvPropertyBuffer,
  527. ulLength);
  528. }
  529. ntStatus = IoGetDeviceProperty(pPdo,
  530. DevicePropertyManufacturer,
  531. WD_MAX_PROPERTY_SIZE,
  532. pvPropertyBuffer,
  533. &ulLength);
  534. if (NT_SUCCESS(ntStatus))
  535. {
  536. RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  537. WD_KEY_WATCHDOG_DISPLAY,
  538. L"Manufacturer",
  539. REG_SZ,
  540. pvPropertyBuffer,
  541. ulLength);
  542. }
  543. //
  544. // Release property buffer.
  545. //
  546. ExFreePool(pvPropertyBuffer);
  547. pvPropertyBuffer = NULL;
  548. }
  549. }
  550. //
  551. // Flush registry in case we're going to break in / bugcheck or if this is first time.
  552. //
  553. if ((TRUE == s_bFirstTime) || (TRUE == bBreakIn) || (0 == s_ulDisableBugcheck))
  554. {
  555. WdpFlushRegistryKey(pWatch, WD_KEY_WATCHDOG_DISPLAY);
  556. }
  557. }
  558. //
  559. // Bugcheck machine without kernel debugger connected and with bugcheck EA enabled.
  560. // Bugcheck EA is enabled on SKUs below Server.
  561. //
  562. if (1 == ulDebuggerNotPresent)
  563. {
  564. if (0 == s_ulDisableBugcheck)
  565. {
  566. if (s_ulEaRecovery == FALSE)
  567. {
  568. KeBugCheckEx(pBugCheckData->ulBugCheckCode,
  569. pBugCheckData->ulpBugCheckParameter1,
  570. pBugCheckData->ulpBugCheckParameter2,
  571. (ULONG_PTR)pUnicodeDriverName,
  572. pBugCheckData->ulpBugCheckParameter4);
  573. }
  574. }
  575. if (s_ulEaRecovery)
  576. {
  577. //
  578. // Try to recover from the EA hang.
  579. //
  580. WdpInjectExceptionIntoThread(pThread, pDpcContext);
  581. }
  582. }
  583. else
  584. {
  585. if (TRUE == bBreakIn)
  586. {
  587. DbgBreakPoint();
  588. if (s_ulEaRecovery)
  589. {
  590. WdpInjectExceptionIntoThread(pThread, pDpcContext);
  591. }
  592. }
  593. }
  594. }
  595. else
  596. {
  597. if (FALSE == s_ulEaRecovery)
  598. {
  599. //
  600. // Recovery - knock down EventFlag in registry and update statics.
  601. //
  602. RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  603. WD_KEY_WATCHDOG_DISPLAY,
  604. L"EventFlag");
  605. }
  606. s_bEventLogged = FALSE;
  607. s_ulCurrentBreakPointDelay = s_ulBreakPointDelay;
  608. }
  609. //
  610. // Reenable event processing in this module.
  611. //
  612. s_bFirstTime = FALSE;
  613. InterlockedExchange(&g_lWdpDisplayHandlerState, WD_HANDLER_IDLE);
  614. //
  615. // Dereference objects and resume watchdog event processing.
  616. //
  617. ObDereferenceObject(pFdo);
  618. ObDereferenceObject(pPdo);
  619. WdCompleteEvent(pWatch, pThread);
  620. return;
  621. } // WdpBugCheckStuckDriver()
  622. VOID
  623. WdpKernelApc(
  624. IN PKAPC pApc,
  625. OUT PKNORMAL_ROUTINE *pNormalRoutine,
  626. IN OUT PVOID pvNormalContext,
  627. IN OUT PVOID *ppvSystemArgument1,
  628. IN OUT PVOID *ppvSystemArgument2
  629. )
  630. /*++
  631. Routine Description:
  632. This APC runs in the context of spinning thread and is responsible
  633. for raising THREAD_STUCK exception.
  634. Arguments:
  635. pApc - Not used.
  636. pNormalRoutine - Not used.
  637. pvNormalContext - Not used.
  638. ppvSystemArgument1 - Supplies a pointer to WD_GDI_CONTEXT_DATA.
  639. ppvSystemArgument2 - Not used.
  640. Return Value:
  641. None.
  642. --*/
  643. {
  644. PKEVENT pInjectionEvent;
  645. CONTEXT context;
  646. PWD_GDI_CONTEXT_DATA pContextData;
  647. ULONG_PTR ulpImageStart;
  648. ULONG_PTR ulpImageStop;
  649. PETHREAD pThread;
  650. NTSTATUS ntStatus;
  651. PLDEV pldev;
  652. ASSERT(NULL != ppvSystemArgument1);
  653. UNREFERENCED_PARAMETER(pApc);
  654. UNREFERENCED_PARAMETER(pNormalRoutine);
  655. UNREFERENCED_PARAMETER(pvNormalContext);
  656. UNREFERENCED_PARAMETER(ppvSystemArgument2);
  657. pContextData = (PWD_GDI_CONTEXT_DATA)*ppvSystemArgument1;
  658. pInjectionEvent = pContextData->pInjectionEvent;
  659. pldev = *pContextData->ppldevDrivers;
  660. pThread = PsGetCurrentThread();
  661. //
  662. // Initialize the context.
  663. //
  664. RtlZeroMemory(&context, sizeof (context));
  665. context.ContextFlags = CONTEXT_CONTROL;
  666. //
  667. // Get the kernel context for this thread.
  668. //
  669. if (NT_SUCCESS(PsGetContextThread(pThread, &context, KernelMode)))
  670. {
  671. //
  672. // We can safely touch the pldev's (which live in session space)
  673. // because this thread came from a process that has the session
  674. // space mapped in.
  675. //
  676. while (pldev)
  677. {
  678. if (pldev->pGdiDriverInfo)
  679. {
  680. ulpImageStart = (ULONG_PTR)pldev->pGdiDriverInfo->ImageAddress;
  681. ulpImageStop = ulpImageStart + (ULONG_PTR)pldev->pGdiDriverInfo->ImageLength - 1;
  682. //
  683. // Modify the context to inject a fault into the thread
  684. // when it starts running again (after APC returns).
  685. //
  686. #if defined (_X86_)
  687. if ((context.Eip >= ulpImageStart) && (context.Eip <= ulpImageStop))
  688. {
  689. //
  690. // We should decrement the stack pointer, and store the
  691. // return address to "fake" a call instruction. However,
  692. // this is not allowed. So instead, lets just put the
  693. // return address in the current stack location. This isn't
  694. // quite right, but should make the stack unwind code happier
  695. // then if we do nothing.
  696. //
  697. //context.Esp -= 4;
  698. //*((PULONG)context.Esp) = context.Eip;
  699. context.Eip = (ULONG)WdpRaiseExceptionInThread;
  700. //
  701. // Set the modified context record.
  702. //
  703. PsSetContextThread(pThread, &context, KernelMode);
  704. pContextData->bRecoveryAttempted = TRUE;
  705. break;
  706. }
  707. #elif defined (_IA64_)
  708. if ((context.StIIP >= ulpImageStart) && (context.StIIP <= ulpImageStop))
  709. {
  710. FRAME_MARKER cfm;
  711. PULONGLONG pullTemp = (PULONGLONG)WdpRaiseExceptionInThread;
  712. //
  713. // Set the return address.
  714. //
  715. context.BrRp = context.StIIP;
  716. //
  717. // Update the frame markers.
  718. //
  719. context.RsPFS = context.StIFS & 0x3fffffffffi64;
  720. context.RsPFS |= (context.ApEC & (0x3fi64 << 52));
  721. context.RsPFS |= (((context.StIPSR >> PSR_CPL) & 0x3) << 62);
  722. cfm.u.Ulong64 = context.StIFS;
  723. cfm.u.f.sof -= cfm.u.f.sol;
  724. cfm.u.f.sol = 0;
  725. cfm.u.f.sor = 0;
  726. cfm.u.f.rrbgr = 0;
  727. cfm.u.f.rrbfr = 0;
  728. cfm.u.f.rrbpr = 0;
  729. context.StIFS = cfm.u.Ulong64;
  730. context.StIFS |= 0x8000000000000000;
  731. //
  732. // Emulate the call.
  733. //
  734. context.StIIP = *pullTemp;
  735. context.IntGp = *(pullTemp+1);
  736. context.StIPSR &= ~((ULONGLONG) 3 << PSR_RI);
  737. //
  738. // Set the modified context record.
  739. //
  740. PsSetContextThread(pThread, &context, KernelMode);
  741. pContextData->bRecoveryAttempted = TRUE;
  742. break;
  743. }
  744. #endif
  745. }
  746. pldev = pldev->pldevNext;
  747. }
  748. //
  749. // Single our event so the caller knows we did something.
  750. //
  751. KeSetEvent(pInjectionEvent, 0, FALSE);
  752. }
  753. } // WdpKernelApc()
  754. VOID
  755. WdpInjectExceptionIntoThread(
  756. PKTHREAD pThread,
  757. PWD_GDI_DPC_CONTEXT pDpcContext
  758. )
  759. /*++
  760. Routine Description:
  761. This routine schedules APC to run in the spinning thread's context.
  762. Arguments:
  763. pThread - Supplies a pointer to the spinning thread.
  764. ppvSystemArgument1 - Supplies a pointer to WD_GDI_DPC_CONTEXT.
  765. Return Value:
  766. None.
  767. --*/
  768. {
  769. KAPC apc;
  770. KEVENT injectionEvent;
  771. WD_GDI_CONTEXT_DATA contextData;
  772. ASSERT(NULL != pThread);
  773. ASSERT(NULL != pDpcContext);
  774. KeInitializeEvent(&injectionEvent, NotificationEvent, FALSE);
  775. KeInitializeApc(&apc,
  776. pThread,
  777. OriginalApcEnvironment,
  778. WdpKernelApc,
  779. NULL,
  780. NULL,
  781. KernelMode,
  782. NULL);
  783. contextData.pThread = pThread;
  784. contextData.pInjectionEvent = &injectionEvent;
  785. contextData.ppldevDrivers = pDpcContext->ppldevDrivers;
  786. contextData.bRecoveryAttempted = FALSE;
  787. if (KeInsertQueueApc(&apc, &contextData, NULL, 0))
  788. {
  789. KeWaitForSingleObject(&injectionEvent,
  790. Executive,
  791. KernelMode,
  792. FALSE,
  793. NULL);
  794. //
  795. // If we attempted a recovery, raise a hard error dialog, to
  796. // notify the user of the situation.
  797. //
  798. if (contextData.bRecoveryAttempted)
  799. {
  800. ULONG Response;
  801. PUNICODE_STRING DriverName = &pDpcContext->DisplayDriverName;
  802. ExRaiseHardError(STATUS_HUNG_DISPLAY_DRIVER_THREAD,
  803. 1,
  804. 1,
  805. (PULONG_PTR)&DriverName,
  806. OptionOk,
  807. &Response);
  808. }
  809. //
  810. // BUGBUG: Is this required?
  811. //
  812. KeClearEvent(&injectionEvent);
  813. }
  814. } // WdpInjectExceptionIntoThread()
  815. VOID
  816. WdpRaiseExceptionInThread()
  817. /*++
  818. Routine Description:
  819. This routine raises THREAD_STUCK exception in the spinning thread's context.
  820. Arguments:
  821. None.
  822. Return Value:
  823. None.
  824. --*/
  825. {
  826. ExRaiseStatus(WD_SE_THREAD_STUCK);
  827. } // WdpRaiseExceptionInThread()