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.

360 lines
8.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. pciverifier.c
  5. Abstract:
  6. This module implements routines used to catch BIOS, hardware, and driver
  7. bugs.
  8. Author:
  9. Adrian J. Oney (AdriaO) 02/20/2001
  10. Revision History:
  11. --*/
  12. #include "pcip.h"
  13. #include <initguid.h>
  14. #include <wdmguid.h>
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text(INIT, PciVerifierInit)
  17. #pragma alloc_text(PAGE, PciVerifierUnload)
  18. //#pragma alloc_text(PAGEVRFY, PciVerifierProfileChangeCallback)
  19. //#pragma alloc_text(PAGEVRFY, PciVerifierEnsureTreeConsistancy)
  20. //#pragma alloc_text(PAGEVRFY, PciVerifierRetrieveFailureData)
  21. #endif
  22. BOOLEAN PciVerifierRegistered = FALSE;
  23. #ifdef ALLOC_DATA_PRAGMA
  24. //#pragma data_seg("PAGEVRFD")
  25. #endif
  26. PVOID PciVerifierNotificationHandle = NULL;
  27. //
  28. // This is the table of PCI verifier failures
  29. //
  30. VERIFIER_DATA PciVerifierFailureTable[] = {
  31. { PCI_VERIFIER_BRIDGE_REPROGRAMMED, VFFAILURE_FAIL_LOGO,
  32. 0,
  33. "The BIOS has reprogrammed the bus numbers of an active PCI device "
  34. "(!devstack %DevObj) during a dock or undock!" },
  35. { PCI_VERIFIER_PMCSR_TIMEOUT, VFFAILURE_FAIL_LOGO,
  36. 0,
  37. "A device in the system did not update it's PMCSR register in the spec "
  38. "mandated time (!devstack %DevObj, Power state D%Ulong)" },
  39. { PCI_VERIFIER_PROTECTED_CONFIGSPACE_ACCESS, VFFAILURE_FAIL_LOGO,
  40. 0,
  41. "A driver controlling a PCI device has tried to access OS controlled "
  42. "configuration space registers (!devstack %DevObj, Offset 0x%Ulong1, "
  43. "Length 0x%Ulong2)" },
  44. { PCI_VERIFIER_INVALID_WHICHSPACE, VFFAILURE_FAIL_UNDER_DEBUGGER,
  45. 0,
  46. "A driver controlling a PCI device has tried to read or write from "
  47. "an invalid space using IRP_MN_READ/WRITE_CONFIG or via BUS_INTERFACE_STANDARD. "
  48. "NB: These functions take WhichSpace parameters of the form PCI_WHICHSPACE_* "
  49. "and not a BUS_DATA_TYPE (!devstack %DevObj, WhichSpace 0x%Ulong1)" }
  50. };
  51. VOID
  52. PciVerifierInit(
  53. IN PDRIVER_OBJECT DriverObject
  54. )
  55. /*++
  56. Routine Description:
  57. This routine initializes the hardware verification support, enabling
  58. consistancy hooks and state checks where appropriate.
  59. Arguments:
  60. DriverObject - Pointer to our driver object.
  61. Return Value:
  62. None.
  63. --*/
  64. {
  65. NTSTATUS status;
  66. if (!VfIsVerificationEnabled(VFOBJTYPE_SYSTEM_BIOS, NULL)) {
  67. return;
  68. }
  69. status = IoRegisterPlugPlayNotification(
  70. EventCategoryHardwareProfileChange,
  71. 0,
  72. NULL,
  73. DriverObject,
  74. PciVerifierProfileChangeCallback,
  75. (PVOID) NULL,
  76. &PciVerifierNotificationHandle
  77. );
  78. if (NT_SUCCESS(status)) {
  79. PciVerifierRegistered = TRUE;
  80. }
  81. }
  82. VOID
  83. PciVerifierUnload(
  84. IN PDRIVER_OBJECT DriverObject
  85. )
  86. /*++
  87. Routine Description:
  88. This routine uninitializes the hardware verification support.
  89. Arguments:
  90. DriverObject - Pointer to our driver object.
  91. Return Value:
  92. None.
  93. --*/
  94. {
  95. NTSTATUS status;
  96. if (!PciVerifierRegistered) {
  97. return;
  98. }
  99. ASSERT(PciVerifierNotificationHandle);
  100. status = IoUnregisterPlugPlayNotification(PciVerifierNotificationHandle);
  101. ASSERT(NT_SUCCESS(status));
  102. PciVerifierRegistered = FALSE;
  103. }
  104. NTSTATUS
  105. PciVerifierProfileChangeCallback(
  106. IN PHWPROFILE_CHANGE_NOTIFICATION NotificationStructure,
  107. IN PVOID NotUsed
  108. )
  109. /*++
  110. Routine Description:
  111. This routine gets called back during hardware profile change events if
  112. hardware verification is enabled.
  113. Arguments:
  114. NotificationStructure - Describes the hardware profile event that occured.
  115. NotUsed - Not used
  116. Return Value:
  117. NTSTATUS.
  118. --*/
  119. {
  120. PAGED_CODE();
  121. if (IsEqualGUID((LPGUID) &NotificationStructure->Event,
  122. (LPGUID) &GUID_HWPROFILE_CHANGE_COMPLETE)) {
  123. //
  124. // This is a HW profile change complete message. Do some tests to
  125. // ensure our hardware hasn't been reprogrammed behind our back.
  126. //
  127. PciVerifierEnsureTreeConsistancy();
  128. }
  129. return STATUS_SUCCESS;
  130. }
  131. VOID
  132. PciVerifierEnsureTreeConsistancy(
  133. VOID
  134. )
  135. /*++
  136. Routine Description:
  137. This routine checks the device tree and ensures it's physical state matches
  138. the virtual state described by our structures. A deviation may mean someone
  139. has reprogrammed the hardware behind our back.
  140. Arguments:
  141. None.
  142. Return Value:
  143. None.
  144. --*/
  145. {
  146. PSINGLE_LIST_ENTRY nextEntry;
  147. PPCI_FDO_EXTENSION fdoExtension;
  148. PPCI_PDO_EXTENSION pdoExtension;
  149. PCI_COMMON_CONFIG commonConfig;
  150. PVERIFIER_DATA verifierData;
  151. //
  152. // Walk the list of FDO extensions and verifier the physical hardware
  153. // matches our virtual state. Owning the PciGlobalLock ensures the list
  154. // is locked.
  155. //
  156. ExAcquireFastMutex(&PciGlobalLock);
  157. //
  158. // Grab the bus renumbering lock. Note that this lock can be held when
  159. // child list locks are held.
  160. //
  161. ExAcquireFastMutex(&PciBusLock);
  162. for ( nextEntry = PciFdoExtensionListHead.Next;
  163. nextEntry != NULL;
  164. nextEntry = nextEntry->Next ) {
  165. fdoExtension = CONTAINING_RECORD(nextEntry, PCI_FDO_EXTENSION, List);
  166. if (PCI_IS_ROOT_FDO(fdoExtension)) {
  167. //
  168. // It's a root FDO, ignore it.
  169. //
  170. continue;
  171. }
  172. pdoExtension = PCI_BRIDGE_PDO(fdoExtension);
  173. if (pdoExtension->NotPresent ||
  174. (pdoExtension->PowerState.CurrentDeviceState == PowerDeviceD3)) {
  175. //
  176. // Don't touch.
  177. //
  178. continue;
  179. }
  180. if ((pdoExtension->HeaderType != PCI_BRIDGE_TYPE) &&
  181. (pdoExtension->HeaderType != PCI_CARDBUS_BRIDGE_TYPE)) {
  182. //
  183. // Nothing to verify - in fact, why are here, this is a bridge list!
  184. //
  185. ASSERT(0);
  186. continue;
  187. }
  188. //
  189. // Read in the common config (that should be enough)
  190. //
  191. PciReadDeviceConfig(
  192. pdoExtension,
  193. &commonConfig,
  194. 0,
  195. sizeof(PCI_COMMON_CONFIG)
  196. );
  197. //
  198. // Ensure bus numbers haven't changed. Note that P2P and Cardbus
  199. // bridges have their Primary, Secondary & Subordinate fields in the
  200. // same place.
  201. //
  202. if ((commonConfig.u.type1.PrimaryBus !=
  203. pdoExtension->Dependent.type1.PrimaryBus) ||
  204. (commonConfig.u.type1.SecondaryBus !=
  205. pdoExtension->Dependent.type1.SecondaryBus) ||
  206. (commonConfig.u.type1.SubordinateBus !=
  207. pdoExtension->Dependent.type1.SubordinateBus)) {
  208. verifierData = PciVerifierRetrieveFailureData(
  209. PCI_VERIFIER_BRIDGE_REPROGRAMMED
  210. );
  211. ASSERT(verifierData);
  212. VfFailSystemBIOS(
  213. PCI_VERIFIER_DETECTED_VIOLATION,
  214. PCI_VERIFIER_BRIDGE_REPROGRAMMED,
  215. verifierData->FailureClass,
  216. &verifierData->Flags,
  217. verifierData->FailureText,
  218. "%DevObj",
  219. pdoExtension->PhysicalDeviceObject
  220. );
  221. }
  222. }
  223. ExReleaseFastMutex(&PciBusLock);
  224. ExReleaseFastMutex(&PciGlobalLock);
  225. }
  226. PVERIFIER_DATA
  227. PciVerifierRetrieveFailureData(
  228. IN PCI_VFFAILURE VerifierFailure
  229. )
  230. /*++
  231. Routine Description:
  232. This routine retrieves the failure data corresponding to a particular PCI
  233. verifier failure event.
  234. Arguments:
  235. PCI Failure.
  236. Return Value:
  237. Verifier data corresponding to the failure.
  238. --*/
  239. {
  240. PVERIFIER_DATA verifierData;
  241. ULONG i;
  242. for(i=0;
  243. i<(sizeof(PciVerifierFailureTable)/sizeof(PciVerifierFailureTable[0]));
  244. i++) {
  245. verifierData = PciVerifierFailureTable + i;
  246. if (verifierData->VerifierFailure == VerifierFailure) {
  247. return verifierData;
  248. }
  249. }
  250. ASSERT(0);
  251. return NULL;
  252. }