Source code of Windows XP (NT5)
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.

659 lines
15 KiB

  1. /*++
  2. Module Name:
  3. mca.c
  4. Abstract:
  5. Driver that inserts MCE into the hal on IA64
  6. Author:
  7. Environment:
  8. Kernel mode
  9. Notes:
  10. Revision History:
  11. --*/
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <ntddk.h>
  16. #include <wmilib.h>
  17. //
  18. // Device names for the MCA driver
  19. //
  20. #define MCA_DEVICE_NAME_U L"\\Device\\mcahct" // ANSI Name
  21. #define GenerateMCEGuid { 0x3001bce4, 0xd9b6, 0x4167, { 0xb5, 0xe1, 0x39, 0xa7, 0x28, 0x59, 0xe2, 0x67 } }
  22. GUID McaGenerateMCEGuid = GenerateMCEGuid;
  23. UNICODE_STRING McaRegPath;
  24. NTSTATUS
  25. DriverEntry(
  26. IN PDRIVER_OBJECT DriverObject,
  27. IN PUNICODE_STRING RegistryPath
  28. );
  29. NTSTATUS
  30. MCACleanup(
  31. IN PDEVICE_OBJECT DeviceObject,
  32. IN PIRP Irp
  33. );
  34. VOID
  35. MCAUnload(
  36. IN PDRIVER_OBJECT DriverObject
  37. );
  38. NTSTATUS
  39. MCASystemControl(
  40. IN PDEVICE_OBJECT DeviceObject,
  41. IN PIRP Irp
  42. );
  43. NTSTATUS
  44. McaExecuteWmiMethod (
  45. IN PDEVICE_OBJECT DeviceObject,
  46. IN PIRP Irp,
  47. IN ULONG GuidIndex,
  48. IN ULONG InstanceIndex,
  49. IN ULONG MethodId,
  50. IN ULONG InBufferSize,
  51. IN ULONG OutBufferSize,
  52. IN PUCHAR Buffer
  53. );
  54. NTSTATUS
  55. McaInsertMceRecord(
  56. HAL_SET_INFORMATION_CLASS InfoClass,
  57. ULONG BufferSize,
  58. PUCHAR Buffer
  59. );
  60. NTSTATUS
  61. McaQueryWmiDataBlock(
  62. IN PDEVICE_OBJECT DeviceObject,
  63. IN PIRP Irp,
  64. IN ULONG GuidIndex,
  65. IN ULONG InstanceIndex,
  66. IN ULONG InstanceCount,
  67. IN OUT PULONG InstanceLengthArray,
  68. IN ULONG BufferAvail,
  69. OUT PUCHAR Buffer
  70. );
  71. NTSTATUS
  72. McaQueryWmiRegInfo(
  73. IN PDEVICE_OBJECT DeviceObject,
  74. OUT ULONG *RegFlags,
  75. OUT PUNICODE_STRING InstanceName,
  76. OUT PUNICODE_STRING *RegistryPath,
  77. OUT PUNICODE_STRING MofResourceName,
  78. OUT PDEVICE_OBJECT *Pdo
  79. );
  80. void McaGenerateMce(
  81. ULONG Code
  82. );
  83. //
  84. // This temporary buffer holds the data between the Machine Check error
  85. // notification from HAL and the asynchronous IOCTL completion to the
  86. // application
  87. //
  88. typedef struct _DEVICE_EXTENSION {
  89. PDEVICE_OBJECT DeviceObject;
  90. WMILIB_CONTEXT WmilibContext;
  91. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  92. #ifdef ALLOC_PRAGMA
  93. #pragma alloc_text(INIT, DriverEntry)
  94. #endif // ALLOC_PRAGMA
  95. WMIGUIDREGINFO McaGuidList[] =
  96. {
  97. {
  98. &McaGenerateMCEGuid, // Guid
  99. 1, // # of instances in each device
  100. 0 // Flag as expensive to collect
  101. }
  102. };
  103. #define McaGuidCount (sizeof(McaGuidList) / sizeof(WMIGUIDREGINFO))
  104. #define GenerateMCEGuidIndex 0
  105. NTSTATUS
  106. DriverEntry(
  107. IN PDRIVER_OBJECT DriverObject,
  108. IN PUNICODE_STRING RegistryPath
  109. )
  110. /*++
  111. Routine Description:
  112. This routine does the driver specific initialization at entry time
  113. Arguments:
  114. DriverObject: Pointer to the driver object
  115. RegistryPath: Path to driver's registry key
  116. Return Value:
  117. Success or failure
  118. --*/
  119. {
  120. UNICODE_STRING UnicodeString;
  121. NTSTATUS Status = STATUS_SUCCESS;
  122. PDEVICE_EXTENSION Extension;
  123. PDEVICE_OBJECT McaDeviceObject;
  124. PWMILIB_CONTEXT WmilibContext;
  125. McaRegPath.Length = 0;
  126. McaRegPath.MaximumLength = RegistryPath->Length;
  127. McaRegPath.Buffer = ExAllocatePoolWithTag(PagedPool,
  128. RegistryPath->Length+2,
  129. 'iMCA');
  130. RtlCopyUnicodeString(&McaRegPath, RegistryPath);
  131. //
  132. // Create device object for MCA device.
  133. //
  134. RtlInitUnicodeString(&UnicodeString, MCA_DEVICE_NAME_U);
  135. //
  136. // Device is created as exclusive since only a single thread can send
  137. // I/O requests to this device
  138. //
  139. Status = IoCreateDevice(
  140. DriverObject,
  141. sizeof(DEVICE_EXTENSION),
  142. &UnicodeString,
  143. FILE_DEVICE_UNKNOWN,
  144. 0,
  145. TRUE,
  146. &McaDeviceObject
  147. );
  148. if (!NT_SUCCESS( Status )) {
  149. DbgPrint("Mca DriverEntry: IoCreateDevice failed\n");
  150. return Status;
  151. }
  152. McaDeviceObject->Flags |= DO_BUFFERED_IO;
  153. Extension = McaDeviceObject->DeviceExtension;
  154. RtlZeroMemory(Extension, sizeof(DEVICE_EXTENSION));
  155. Extension->DeviceObject = McaDeviceObject;
  156. //
  157. // Set up the device driver entry points.
  158. //
  159. DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = MCASystemControl;
  160. DriverObject->MajorFunction[IRP_MJ_CLEANUP] = MCACleanup;
  161. DriverObject->DriverUnload = MCAUnload;
  162. //
  163. // Register with WMI
  164. //
  165. WmilibContext = &Extension->WmilibContext;
  166. WmilibContext->GuidList = McaGuidList;
  167. WmilibContext->GuidCount = McaGuidCount;
  168. WmilibContext->QueryWmiRegInfo = McaQueryWmiRegInfo;
  169. WmilibContext->QueryWmiDataBlock = McaQueryWmiDataBlock;
  170. WmilibContext->ExecuteWmiMethod = McaExecuteWmiMethod;
  171. Status = IoWMIRegistrationControl(McaDeviceObject,
  172. WMIREG_ACTION_REGISTER);
  173. if (! NT_SUCCESS(Status))
  174. {
  175. DbgPrint("Mca DriverEntry: IoWmiRegistrationControl failed\n");
  176. IoDeleteDevice(McaDeviceObject);
  177. return(Status);
  178. }
  179. return STATUS_SUCCESS;
  180. }
  181. NTSTATUS
  182. MCASystemControl(
  183. IN PDEVICE_OBJECT DeviceObject,
  184. IN PIRP Irp
  185. )
  186. /*++
  187. Routine Description:
  188. This routine is the dispatch routine for the WMI requests to driver.
  189. It accepts an I/O Request Packet, performs the request, and then
  190. returns with the appropriate status.
  191. Arguments:
  192. DeviceObject: Pointer to the device object
  193. Irp: Incoming Irp
  194. Return Value:
  195. Success or failure
  196. --*/
  197. {
  198. SYSCTL_IRP_DISPOSITION Disposition;
  199. NTSTATUS Status;
  200. PDEVICE_EXTENSION Extension = DeviceObject->DeviceExtension;
  201. PWMILIB_CONTEXT WmilibContext = &Extension->WmilibContext;
  202. //
  203. // Call Wmilib helper function to crack the irp. If this is a wmi irp
  204. // that is targetted for this device then WmiSystemControl will callback
  205. // at the appropriate callback routine.
  206. //
  207. Status = WmiSystemControl(WmilibContext,
  208. DeviceObject,
  209. Irp,
  210. &Disposition);
  211. switch(Disposition)
  212. {
  213. case IrpProcessed:
  214. {
  215. //
  216. // This irp has been processed and may be completed or
  217. // pending.
  218. //
  219. return(Status);
  220. break;
  221. }
  222. case IrpNotCompleted:
  223. {
  224. //
  225. // This irp has not been completed, but has been fully processed.
  226. // we will complete it now.
  227. //
  228. break;
  229. }
  230. case IrpForward:
  231. case IrpNotWmi:
  232. {
  233. //
  234. // This irp is either not a WMI irp or is a WMI irp targetted
  235. // at a device lower in the stack.
  236. //
  237. Status = Irp->IoStatus.Status;
  238. break;
  239. }
  240. default:
  241. {
  242. //
  243. // We really should never get here, but if we do just
  244. // forward.... //
  245. ASSERT(FALSE);
  246. Status = Irp->IoStatus.Status;
  247. break;
  248. }
  249. }
  250. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  251. return (Status);
  252. }
  253. NTSTATUS
  254. MCACleanup(
  255. IN PDEVICE_OBJECT DeviceObject,
  256. IN PIRP Irp
  257. )
  258. /*++
  259. Routine Description:
  260. This is the dispatch routine for cleanup requests.
  261. All queued IRPs are completed with STATUS_CANCELLED.
  262. Arguments:
  263. DeviceObject: Pointer to the device object
  264. Irp: Incoming Irp
  265. Return Value:
  266. Success or failure
  267. --*/
  268. {
  269. PDEVICE_EXTENSION Extension = DeviceObject->DeviceExtension;
  270. //
  271. // Complete the Cleanup Dispatch with STATUS_SUCCESS
  272. //
  273. Irp->IoStatus.Status = STATUS_SUCCESS;
  274. Irp->IoStatus.Information = 0;
  275. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  276. return(STATUS_SUCCESS);
  277. }
  278. VOID
  279. MCAUnload(
  280. IN PDRIVER_OBJECT DriverObject
  281. )
  282. /*++
  283. Routine Description:
  284. Dispatch routine for unloads
  285. Arguments:
  286. DeviceObject: Pointer to the device object
  287. Return Value:
  288. None
  289. --*/
  290. {
  291. NTSTATUS Status;
  292. STRING DosString;
  293. UNICODE_STRING DosUnicodeString;
  294. //
  295. // Unregister with WMI
  296. //
  297. IoWMIRegistrationControl(DriverObject->DeviceObject,
  298. WMIREG_ACTION_DEREGISTER);
  299. //
  300. // Delete the device object
  301. //
  302. IoDeleteDevice(DriverObject->DeviceObject);
  303. return;
  304. }
  305. NTSTATUS
  306. McaQueryWmiRegInfo(
  307. IN PDEVICE_OBJECT DeviceObject,
  308. OUT ULONG *RegFlags,
  309. OUT PUNICODE_STRING InstanceName,
  310. OUT PUNICODE_STRING *RegistryPath,
  311. OUT PUNICODE_STRING MofResourceName,
  312. OUT PDEVICE_OBJECT *Pdo
  313. )
  314. /*++
  315. Routine Description:
  316. This routine is a callback into the driver to retrieve the list of
  317. guids or data blocks that the driver wants to register with WMI. This
  318. routine may not pend or block. Driver should NOT call
  319. ClassWmiCompleteRequest.
  320. Arguments:
  321. DeviceObject is the device whose data block is being queried
  322. *RegFlags returns with a set of flags that describe the guids being
  323. registered for this device. If the device wants enable and disable
  324. collection callbacks before receiving queries for the registered
  325. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  326. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  327. the instance name is determined from the PDO associated with the
  328. device object. Note that the PDO must have an associated devnode. If
  329. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  330. name for the device.
  331. InstanceName returns with the instance name for the guids if
  332. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  333. caller will call ExFreePool with the buffer returned.
  334. *RegistryPath returns with the registry path of the driver
  335. Return Value:
  336. status
  337. --*/
  338. {
  339. ANSI_STRING AnsiString;
  340. NTSTATUS Status;
  341. PAGED_CODE();
  342. RtlInitAnsiString(&AnsiString, "SMBiosData");
  343. Status = RtlAnsiStringToUnicodeString(InstanceName, &AnsiString, TRUE);
  344. *RegistryPath = &McaRegPath;
  345. return(Status);
  346. }
  347. NTSTATUS
  348. McaQueryWmiDataBlock(
  349. IN PDEVICE_OBJECT DeviceObject,
  350. IN PIRP Irp,
  351. IN ULONG GuidIndex,
  352. IN ULONG InstanceIndex,
  353. IN ULONG InstanceCount,
  354. IN OUT PULONG InstanceLengthArray,
  355. IN ULONG BufferAvail,
  356. OUT PUCHAR Buffer
  357. )
  358. /*++
  359. Routine Description:
  360. This routine is a callback into the driver to query for the contents of
  361. all instances of a data block. When the driver has finished filling the
  362. data block it must call IoWMICompleteRequest to complete the irp. The
  363. driver can return STATUS_PENDING if the irp cannot be completed
  364. immediately.
  365. Arguments:
  366. DeviceObject is the device whose data block is being queried. In the case
  367. of the PnPId guid this is the device object of the device on whose
  368. behalf the request is being processed.
  369. Irp is the Irp that makes this request
  370. GuidIndex is the index into the list of guids provided when the
  371. device registered
  372. InstanceCount is the number of instnaces expected to be returned for
  373. the data block.
  374. InstanceLengthArray is a pointer to an array of ULONG that returns the
  375. lengths of each instance of the data block. If this is NULL then
  376. there was not enough space in the output buffer to fufill the request
  377. so the irp should be completed with the buffer needed.
  378. BufferAvail on entry has the maximum size available to write the data
  379. blocks.
  380. Buffer on return is filled with the returned data blocks. Note that each
  381. instance of the data block must be aligned on a 8 byte boundry.
  382. Return Value:
  383. status
  384. --*/
  385. {
  386. NTSTATUS status;
  387. ULONG sizeNeeded = 0;
  388. PAGED_CODE();
  389. switch (GuidIndex)
  390. {
  391. case GenerateMCEGuidIndex:
  392. {
  393. sizeNeeded = sizeof(ULONG);
  394. if (BufferAvail >= sizeNeeded)
  395. {
  396. *((PULONG)Buffer) = 0;
  397. *InstanceLengthArray = sizeNeeded;
  398. } else {
  399. status = STATUS_BUFFER_TOO_SMALL;
  400. }
  401. break;
  402. }
  403. default:
  404. {
  405. status = STATUS_WMI_GUID_NOT_FOUND;
  406. break;
  407. }
  408. }
  409. status = WmiCompleteRequest( DeviceObject,
  410. Irp,
  411. status,
  412. sizeNeeded,
  413. IO_NO_INCREMENT);
  414. return(status);
  415. }
  416. NTSTATUS
  417. McaInsertMceRecord(
  418. HAL_SET_INFORMATION_CLASS InfoClass,
  419. ULONG BufferSize,
  420. PUCHAR Buffer
  421. )
  422. {
  423. NTSTATUS status;
  424. status = HalSetSystemInformation(InfoClass,
  425. BufferSize,
  426. Buffer);
  427. #if DBG
  428. if (! NT_SUCCESS(status))
  429. {
  430. DbgPrint("Mcahct: Sending class %d MCE record to Hal failed %x\n",
  431. InfoClass,
  432. status
  433. );
  434. }
  435. #endif
  436. return(status);
  437. }
  438. NTSTATUS
  439. McaExecuteWmiMethod (
  440. IN PDEVICE_OBJECT DeviceObject,
  441. IN PIRP Irp,
  442. IN ULONG GuidIndex,
  443. IN ULONG InstanceIndex,
  444. IN ULONG MethodId,
  445. IN ULONG InBufferSize,
  446. IN ULONG OutBufferSize,
  447. IN PUCHAR Buffer
  448. )
  449. {
  450. NTSTATUS status;
  451. ULONG sizeNeeded;
  452. PAGED_CODE();
  453. if (GuidIndex == GenerateMCEGuidIndex)
  454. {
  455. switch (MethodId)
  456. {
  457. //
  458. // MCA insertion by ID
  459. //
  460. case 1:
  461. {
  462. if (InBufferSize == sizeof(ULONG))
  463. {
  464. sizeNeeded = sizeof(NTSTATUS);
  465. if (OutBufferSize >= sizeNeeded)
  466. {
  467. McaGenerateMce(*((PULONG)Buffer));
  468. status = STATUS_SUCCESS;
  469. *((NTSTATUS *)Buffer) = status;
  470. status = STATUS_SUCCESS;
  471. }
  472. } else {
  473. status = STATUS_INVALID_PARAMETER;
  474. }
  475. break;
  476. }
  477. //
  478. // Corrected CMC insertion by fully formed MCA exception
  479. //
  480. case 2:
  481. {
  482. status = McaInsertMceRecord(HalCmcLog,
  483. InBufferSize,
  484. Buffer);
  485. sizeNeeded = 0;
  486. break;
  487. }
  488. //
  489. // Corrected CPE insertion by fully formed MCA exception
  490. //
  491. case 3:
  492. {
  493. status = McaInsertMceRecord(HalCpeLog,
  494. InBufferSize,
  495. Buffer);
  496. sizeNeeded = 0;
  497. break;
  498. }
  499. //
  500. // Fatal MCA insertion by fully formed MCA exception
  501. //
  502. case 4:
  503. {
  504. status = McaInsertMceRecord(HalMcaLog,
  505. InBufferSize,
  506. Buffer);
  507. sizeNeeded = 0;
  508. break;
  509. }
  510. default:
  511. {
  512. status = STATUS_WMI_ITEMID_NOT_FOUND;
  513. }
  514. }
  515. } else {
  516. status = STATUS_WMI_GUID_NOT_FOUND;
  517. }
  518. status = WmiCompleteRequest(
  519. DeviceObject,
  520. Irp,
  521. status,
  522. sizeNeeded,
  523. IO_NO_INCREMENT);
  524. return(status);
  525. }