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.

1179 lines
30 KiB

  1. /*++
  2. Copyright (c) 1990-2000 Microsoft Corporation All Rights Reserved
  3. Module Name:
  4. wmi.c
  5. Abstract:
  6. This module handle all the WMI Irps.
  7. Environment:
  8. Kernel mode
  9. Revision History:
  10. 10-26-1998 Eliyas Yakub
  11. 10-10-2000 Todd Carpenter - re-written & updated based on wmifilt.sys
  12. --*/
  13. #include "processor.h"
  14. #include "wmi.h"
  15. PCHAR
  16. WMIMinorFunctionString (
  17. UCHAR MinorFunction
  18. );
  19. #ifdef ALLOC_PRAGMA
  20. #pragma alloc_text(PAGE,ProcessorWmiRegistration)
  21. #pragma alloc_text(PAGE,ProcessorWmiDeRegistration)
  22. #pragma alloc_text(PAGE,ProcessorSystemControl)
  23. #pragma alloc_text(PAGE,ProcessorSetWmiDataItem)
  24. #pragma alloc_text(PAGE,ProcessorSetWmiDataBlock)
  25. #pragma alloc_text(PAGE,ProcessorQueryWmiDataBlock)
  26. #pragma alloc_text(PAGE,ProcessorQueryWmiRegInfo)
  27. #endif
  28. WMIGUIDREGINFO ProcessorWmiGuidList[] = {
  29. {&PROCESSOR_STATUS_WMI_GUID, 1, 0},
  30. {&PROCESSOR_METHOD_WMI_GUID, 1, 0},
  31. {&PROCESSOR_TRACING_WMI_GUID, 0, WMIREG_FLAG_TRACED_GUID |
  32. WMIREG_FLAG_TRACE_CONTROL_GUID},
  33. {&PROCESSOR_PERFMON_WMI_GUID, 1, 0},
  34. {&PROCESSOR_PSTATE_EVENT_WMI_GUID, 1, 0},
  35. {&PROCESSOR_NEW_PSTATES_EVENT_WMI_GUID, 1, 0},
  36. {&PROCESSOR_NEW_CSTATES_EVENT_WMI_GUID, 1, 0}
  37. };
  38. //
  39. // These correspond to the indexes of the above GUIDs
  40. //
  41. typedef enum {
  42. ProcessorWmiStatusId,
  43. ProcessorWmiMethodId,
  44. ProcessorWmiTracingId,
  45. ProcessorWmiPerfMonId,
  46. ProcessorWmiPStateEventId,
  47. ProcessorWmiNewPStatesEventId,
  48. ProcessorWmiNewCStatesEventId
  49. } PROCESSOR_WMI_GUID_LIST;
  50. ULONG WmiTraceEnable;
  51. TRACEHANDLE WmiTraceHandle;
  52. BOOLEAN UsingGlobalWmiTraceHandle;
  53. WMI_EVENT NewPStatesEvent = {0,
  54. ProcessorWmiNewPStatesEventId,
  55. sizeof(NEW_PSTATES_EVENT),
  56. (LPGUID)&PROCESSOR_NEW_PSTATES_EVENT_WMI_GUID};
  57. WMI_EVENT NewCStatesEvent = {0,
  58. ProcessorWmiNewCStatesEventId,
  59. 0,
  60. (LPGUID)&PROCESSOR_NEW_CSTATES_EVENT_WMI_GUID};
  61. WMI_EVENT PStateEvent = {0,
  62. ProcessorWmiPStateEventId,
  63. sizeof(PSTATE_EVENT),
  64. (LPGUID)&PROCESSOR_PSTATE_EVENT_WMI_GUID};
  65. NTSTATUS
  66. ProcessorWmiRegistration (
  67. PFDO_DATA FdoData
  68. )
  69. /*++
  70. Routine Description
  71. Registers with WMI as a data provider for this
  72. instance of the device
  73. --*/
  74. {
  75. DebugEnter();
  76. PAGED_CODE();
  77. FdoData->WmiLibInfo.GuidCount = PROCESSOR_WMI_GUID_LIST_SIZE;
  78. FdoData->WmiLibInfo.GuidList = ProcessorWmiGuidList;
  79. FdoData->WmiLibInfo.QueryWmiRegInfo = ProcessorQueryWmiRegInfo;
  80. FdoData->WmiLibInfo.QueryWmiDataBlock = ProcessorQueryWmiDataBlock;
  81. FdoData->WmiLibInfo.SetWmiDataBlock = ProcessorSetWmiDataBlock;
  82. FdoData->WmiLibInfo.SetWmiDataItem = ProcessorSetWmiDataItem;
  83. FdoData->WmiLibInfo.ExecuteWmiMethod = ProcessorExecuteWmiMethod;
  84. FdoData->WmiLibInfo.WmiFunctionControl = ProcessorWmiFunctionControl;
  85. //
  86. // Register with WMI
  87. //
  88. return IoWMIRegistrationControl(FdoData->Self, WMIREG_ACTION_REGISTER);
  89. }
  90. NTSTATUS
  91. ProcessorWmiDeRegistration (
  92. PFDO_DATA FdoData
  93. )
  94. /*++
  95. Routine Description
  96. Inform WMI to remove this DeviceObject from its
  97. list of providers. This function also
  98. decrements the reference count of the deviceobject.
  99. --*/
  100. {
  101. DebugEnter();
  102. PAGED_CODE();
  103. return IoWMIRegistrationControl(FdoData->Self, WMIREG_ACTION_DEREGISTER);
  104. }
  105. NTSTATUS
  106. ProcessorSystemControl (
  107. IN PDEVICE_OBJECT DeviceObject,
  108. IN PIRP Irp
  109. )
  110. /*++
  111. Routine Description
  112. Dispatch routine for System Control IRPs
  113. (MajorFunction == IRP_MJ_SYSTEM_CONTROL)
  114. Arguments:
  115. DeviceObject - Targetted device object
  116. Irp - Io Request Packet
  117. Return Value:
  118. NT status code
  119. --*/
  120. {
  121. PFDO_DATA fdoData;
  122. SYSCTL_IRP_DISPOSITION disposition;
  123. NTSTATUS status;
  124. PIO_STACK_LOCATION stack;
  125. DebugEnter();
  126. PAGED_CODE();
  127. stack = IoGetCurrentIrpStackLocation (Irp);
  128. DebugPrint((TRACE, " %s\n", WMIMinorFunctionString(stack->MinorFunction)));
  129. fdoData = (PFDO_DATA) DeviceObject->DeviceExtension;
  130. ProcessorIoIncrement(fdoData);
  131. if (fdoData->DevicePnPState == Deleted) {
  132. Irp->IoStatus.Status = status = STATUS_DELETE_PENDING;
  133. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  134. ProcessorIoDecrement (fdoData);
  135. return status;
  136. }
  137. status = WmiSystemControl(&fdoData->WmiLibInfo,
  138. DeviceObject,
  139. Irp,
  140. &disposition);
  141. switch (disposition) {
  142. case IrpProcessed:
  143. //
  144. // This irp has been processed and may be completed or pending.
  145. //
  146. break;
  147. case IrpNotCompleted:
  148. //
  149. // This irp has not been completed, but has been fully processed.
  150. // we will complete it now
  151. //
  152. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  153. break;
  154. case IrpForward:
  155. case IrpNotWmi:
  156. //
  157. // This irp is either not a WMI irp or is a WMI irp targeted
  158. // at a device lower in the stack.
  159. //
  160. IoSkipCurrentIrpStackLocation(Irp);
  161. status = IoCallDriver(fdoData->NextLowerDriver, Irp);
  162. break;
  163. default:
  164. //
  165. // We really should never get here, but if we do just forward....
  166. //
  167. DebugAssert(!"WmiSystemControl() returned unknown disposition");
  168. IoSkipCurrentIrpStackLocation (Irp);
  169. status = IoCallDriver(fdoData->NextLowerDriver, Irp);
  170. break;
  171. }
  172. ProcessorIoDecrement(fdoData);
  173. return status;
  174. }
  175. //
  176. // WMI System Call back functions
  177. //
  178. NTSTATUS
  179. ProcessorSetWmiDataItem (
  180. IN PDEVICE_OBJECT DeviceObject,
  181. IN PIRP Irp,
  182. IN ULONG GuidIndex,
  183. IN ULONG InstanceIndex,
  184. IN ULONG DataItemId,
  185. IN ULONG BufferSize,
  186. IN PUCHAR Buffer
  187. )
  188. /*++
  189. Routine Description:
  190. This routine is a callback into the driver to change the contents of
  191. a data block. If teh driver can change teh data block within
  192. the callback it should call WmiCompleteRequest to complete the irp before
  193. returning to the caller. Or the driver can return STATUS_PENDING if the
  194. irp cannot be completed immediately and must then call WmiCompleteRequest
  195. once the data is changed.
  196. Arguments:
  197. DeviceObject is the device whose data block is being changed
  198. Irp is the Irp that makes this request
  199. GuidIndex is the index into the list of guids provided when the
  200. device registered
  201. InstanceIndex is the index that denotes which instance of the data block
  202. is being queried.
  203. DataItemId has the id of the data item being set
  204. BufferSize has the size of the data item passed
  205. Buffer has the new values for the data item
  206. Return Value:
  207. status
  208. --*/
  209. {
  210. NTSTATUS status;
  211. PFDO_DATA fdoData;
  212. PIO_STACK_LOCATION stack;
  213. DebugEnter();
  214. PAGED_CODE();
  215. stack = IoGetCurrentIrpStackLocation(Irp);
  216. fdoData = (PFDO_DATA) DeviceObject->DeviceExtension;
  217. switch (GuidIndex) {
  218. case ProcessorWmiStatusId:
  219. case ProcessorWmiMethodId:
  220. case ProcessorWmiPerfMonId:
  221. status = STATUS_WMI_READ_ONLY;
  222. break;
  223. default:
  224. status = STATUS_WMI_GUID_NOT_FOUND;
  225. }
  226. status = WmiCompleteRequest(DeviceObject,
  227. Irp,
  228. status,
  229. 0,
  230. IO_NO_INCREMENT);
  231. return status;
  232. }
  233. NTSTATUS
  234. ProcessorSetWmiDataBlock (
  235. IN PDEVICE_OBJECT DeviceObject,
  236. IN PIRP Irp,
  237. IN ULONG GuidIndex,
  238. IN ULONG InstanceIndex,
  239. IN ULONG BufferSize,
  240. IN PUCHAR Buffer
  241. )
  242. /*++
  243. Routine Description:
  244. This routine is a callback into the driver to change the contents of
  245. a data block. If the driver can change the data block within
  246. the callback it should call WmiCompleteRequest to complete the irp before
  247. returning to the caller. Or the driver can return STATUS_PENDING if the
  248. irp cannot be completed immediately and must then call WmiCompleteRequest
  249. once the data is changed.
  250. Arguments:
  251. DeviceObject is the device whose data block is being queried
  252. Irp is the Irp that makes this request
  253. GuidIndex is the index into the list of guids provided when the
  254. device registered
  255. InstanceIndex is the index that denotes which instance of the data block
  256. is being queried.
  257. BufferSize has the size of the data block passed
  258. Buffer has the new values for the data block
  259. Return Value:
  260. status
  261. --*/
  262. {
  263. NTSTATUS status;
  264. PFDO_DATA fdoData;
  265. PIO_STACK_LOCATION stack;
  266. DebugEnter();
  267. PAGED_CODE();
  268. stack = IoGetCurrentIrpStackLocation(Irp);
  269. fdoData = (PFDO_DATA) DeviceObject->DeviceExtension;
  270. switch (GuidIndex) {
  271. case ProcessorWmiStatusId:
  272. case ProcessorWmiMethodId:
  273. case ProcessorWmiPerfMonId:
  274. status = STATUS_WMI_READ_ONLY;
  275. break;
  276. default:
  277. status = STATUS_WMI_GUID_NOT_FOUND;
  278. }
  279. status = WmiCompleteRequest(DeviceObject,
  280. Irp,
  281. status,
  282. 0,
  283. IO_NO_INCREMENT);
  284. return status;
  285. }
  286. NTSTATUS
  287. ProcessorQueryWmiDataBlock (
  288. IN PDEVICE_OBJECT DeviceObject,
  289. IN PIRP Irp,
  290. IN ULONG GuidIndex,
  291. IN ULONG InstanceIndex,
  292. IN ULONG InstanceCount,
  293. IN OUT PULONG InstanceLengthArray,
  294. IN ULONG BufferAvail,
  295. OUT PUCHAR Buffer
  296. )
  297. /*++
  298. Routine Description:
  299. This routine is a callback into the driver to query for the contents of
  300. all instances of a data block. If the driver can satisfy the query within
  301. the callback it should call WmiCompleteRequest to complete the irp before
  302. returning to the caller. Or the driver can return STATUS_PENDING if the
  303. irp cannot be completed immediately and must then call WmiCompleteRequest
  304. once the query is satisfied.
  305. Arguments:
  306. DeviceObject is the device whose data block is being queried
  307. Irp is the Irp that makes this request
  308. GuidIndex is the index into the list of guids provided when the
  309. device registered
  310. InstanceCount is the number of instnaces expected to be returned for
  311. the data block.
  312. InstanceLengthArray is a pointer to an array of ULONG that returns the
  313. lengths of each instance of the data block. If this is NULL then
  314. there was not enough space in the output buffer to fufill the request
  315. so the irp should be completed with the buffer needed.
  316. BufferAvail on entry has the maximum size available to write the data
  317. blocks.
  318. Buffer on return is filled with the returned data blocks. Note that each
  319. instance of the data block must be aligned on a 8 byte boundry.
  320. Return Value:
  321. status
  322. --*/
  323. {
  324. NTSTATUS status;
  325. PFDO_DATA fdoData;
  326. ULONG perfStateSize = 0;
  327. ULONG sizeNeeded = 0;
  328. PIO_STACK_LOCATION stack;
  329. DebugEnter();
  330. PAGED_CODE();
  331. stack = IoGetCurrentIrpStackLocation(Irp);
  332. fdoData = (PFDO_DATA) DeviceObject->DeviceExtension;
  333. switch (GuidIndex) {
  334. case ProcessorWmiStatusId:
  335. DebugPrint((TRACE, "ProcessorWmiStatusId\n"));
  336. if (fdoData->PerfStates) {
  337. AcquireProcessorPerfStateLock(fdoData);
  338. perfStateSize = sizeof(PROCESSOR_PERFORMANCE_STATES) +
  339. (sizeof(PROCESSOR_PERFORMANCE_STATE) *
  340. (fdoData->PerfStates->Count - 1));
  341. sizeNeeded = sizeof(PROCESSOR_WMI_STATUS_DATA) +
  342. (sizeof(PROCESSOR_PERFORMANCE_STATE) *
  343. (fdoData->PerfStates->Count - 1));
  344. if (BufferAvail >= sizeNeeded) {
  345. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->CurrentPerfState = fdoData->CurrentPerfState;
  346. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->LastRequestedThrottle = fdoData->LastRequestedThrottle;
  347. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->LastTransitionResult = fdoData->LastTransitionResult;
  348. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->ThrottleValue = fdoData->ThrottleValue;
  349. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->LowestPerfState = fdoData->LowestPerfState;
  350. ((PPROCESSOR_WMI_STATUS_DATA)Buffer)->UsingLegacyInterface = (ULONG) fdoData->LegacyInterface;
  351. RtlCopyMemory(&((PPROCESSOR_WMI_STATUS_DATA)Buffer)->PerfStates,
  352. fdoData->PerfStates,
  353. perfStateSize);
  354. status = STATUS_SUCCESS;
  355. } else {
  356. status = STATUS_BUFFER_TOO_SMALL;
  357. }
  358. ReleaseProcessorPerfStateLock(fdoData);
  359. } else {
  360. DebugPrint((ERROR, "ProcessorQueryWmiDataBlock(): PerfStates == NULL\n"));
  361. status = STATUS_WMI_GUID_NOT_FOUND;
  362. }
  363. break;
  364. case ProcessorWmiMethodId:
  365. DebugPrint((TRACE, "ProcessorWmiMethodId\n"));
  366. //
  367. // Method classes do not have any data within them, but must repond
  368. // successfully to queries so that WMI method operation work successfully.
  369. //
  370. //sizeNeeded = sizeof(USHORT);
  371. sizeNeeded = 0;
  372. status = STATUS_SUCCESS;
  373. //if (BufferAvail >= sizeNeeded) {
  374. //status = STATUS_SUCCESS;
  375. //} else {
  376. //status = STATUS_BUFFER_TOO_SMALL;
  377. //}
  378. break;
  379. case ProcessorWmiPerfMonId:
  380. DebugPrint((TRACE, "ProcessorWmiPerfMonId\n"));
  381. if (fdoData->PerfStates) {
  382. sizeNeeded = sizeof(PROCESSOR_PERFORMANCE_STATE);
  383. if (BufferAvail >= sizeNeeded) {
  384. AcquireProcessorPerfStateLock(fdoData);
  385. RtlCopyMemory(Buffer,
  386. &fdoData->PerfStates->State[fdoData->CurrentPerfState],
  387. sizeNeeded);
  388. ReleaseProcessorPerfStateLock(fdoData);
  389. status = STATUS_SUCCESS;
  390. } else {
  391. status = STATUS_BUFFER_TOO_SMALL;
  392. }
  393. } else {
  394. DebugPrint((ERROR, "ProcessorQueryWmiDataBlock(): PerfStates == NULL\n"));
  395. status = STATUS_WMI_GUID_NOT_FOUND;
  396. }
  397. break;
  398. default:
  399. status = STATUS_WMI_GUID_NOT_FOUND;
  400. }
  401. InstanceLengthArray[0] = sizeNeeded;
  402. status = WmiCompleteRequest(DeviceObject,
  403. Irp,
  404. status,
  405. sizeNeeded,
  406. IO_NO_INCREMENT);
  407. DebugExitStatus(status);
  408. return status;
  409. }
  410. NTSTATUS
  411. ProcessorQueryWmiRegInfo(
  412. IN PDEVICE_OBJECT DeviceObject,
  413. OUT PULONG RegFlags,
  414. OUT PUNICODE_STRING InstanceName,
  415. OUT PUNICODE_STRING *RegistryPath,
  416. OUT PUNICODE_STRING ResourceName,
  417. OUT PDEVICE_OBJECT *Pdo
  418. )
  419. /*++
  420. Routine Description:
  421. This routine is a callback into the driver to retrieve the list of
  422. guids or data blocks that the driver wants to register with WMI. This
  423. routine may not pend or block. Driver should NOT call
  424. WmiCompleteRequest.
  425. Arguments:
  426. DeviceObject is the device whose data block is being queried
  427. *RegFlags returns with a set of flags that describe the guids being
  428. registered for this device. If the device wants enable and disable
  429. collection callbacks before receiving queries for the registered
  430. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  431. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  432. the instance name is determined from the PDO associated with the
  433. device object. Note that the PDO must have an associated devnode. If
  434. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  435. name for the device.
  436. InstanceName returns with the instance name for the guids if
  437. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  438. caller will call ExFreePool with the buffer returned.
  439. *RegistryPath returns with the registry path of the driver
  440. *MofResourceName returns with the name of the MOF resource attached to
  441. the binary file. If the driver does not have a mof resource attached
  442. then this can be returned as NULL.
  443. *Pdo returns with the device object for the PDO associated with this
  444. device if the WMIREG_FLAG_INSTANCE_PDO flag is returned in
  445. *RegFlags.
  446. Return Value:
  447. status
  448. --*/
  449. {
  450. PFDO_DATA fdoData;
  451. DebugEnter();
  452. PAGED_CODE();
  453. fdoData = DeviceObject->DeviceExtension;
  454. *RegFlags = WMIREG_FLAG_INSTANCE_PDO;
  455. *RegistryPath = &Globals.RegistryPath;
  456. *Pdo = fdoData->UnderlyingPDO;
  457. RtlInitUnicodeString(ResourceName, PROCESSOR_MOF_RESOURCE_NAME);
  458. return STATUS_SUCCESS;
  459. }
  460. NTSTATUS
  461. ProcessorExecuteWmiMethod(
  462. IN PDEVICE_OBJECT DeviceObject,
  463. IN PIRP Irp,
  464. IN ULONG GuidIndex,
  465. IN ULONG InstanceIndex,
  466. IN ULONG MethodId,
  467. IN ULONG InBufferSize,
  468. IN ULONG OutBufferSize,
  469. IN PUCHAR Buffer
  470. )
  471. /*++
  472. Routine Description:
  473. This routine is a callback into the driver to execute a method. If
  474. the driver can complete the method within the callback it should
  475. call WmiCompleteRequest to complete the irp before returning to the
  476. caller. Or the driver can return STATUS_PENDING if the irp cannot be
  477. completed immediately and must then call WmiCompleteRequest once the
  478. data is changed.
  479. Arguments:
  480. DeviceObject is the device whose method is being executed
  481. Irp is the Irp that makes this request
  482. GuidIndex is the index into the list of guids provided when the
  483. device registered
  484. MethodId has the id of the method being called
  485. InBufferSize has the size of the data block passed in as the input to
  486. the method.
  487. OutBufferSize on entry has the maximum size available to write the
  488. returned data block.
  489. Buffer is filled with the input buffer on entry and returns with
  490. the output data block
  491. Return Value:
  492. status
  493. --*/
  494. {
  495. ULONG sizeNeeded = 0;
  496. NTSTATUS status;
  497. PFDO_DATA devExt = DeviceObject->DeviceExtension;
  498. DebugEnter();
  499. //
  500. // Execute a Method, or Fire an Event
  501. //
  502. if (GuidIndex == ProcessorWmiMethodId) {
  503. switch (MethodId) {
  504. case WmiFunctionSetProcessorPerfState:
  505. {
  506. ULONG newPerfState = 0;
  507. ULONG rc = 0;
  508. if (InstanceIndex != 0) {
  509. status = STATUS_WMI_INSTANCE_NOT_FOUND;
  510. break;
  511. }
  512. if (InBufferSize < sizeof(ULONG)) {
  513. DebugPrint((TRACE, "WmiFunctionSetProcessorPerfState: InBuffer too small: 0x%x\n",
  514. InBufferSize));
  515. status = STATUS_BUFFER_TOO_SMALL;
  516. sizeNeeded = sizeof(ULONG);
  517. break;
  518. }
  519. if (OutBufferSize < sizeof(ULONG)) {
  520. DebugPrint((TRACE, "WmiFunctionSetProcessorPerfState: OutBuffer too small: 0x%x\n",
  521. OutBufferSize));
  522. status = STATUS_BUFFER_TOO_SMALL;
  523. sizeNeeded = sizeof(ULONG);
  524. break;
  525. }
  526. //
  527. // This functionality is only supported on debug builds
  528. //
  529. #if DBG || ENABLE_STATE_CHANGE
  530. newPerfState = *((PULONG)Buffer);
  531. rc = SetProcessorPerformanceState(newPerfState, devExt);
  532. RtlCopyMemory(Buffer, (PUCHAR)&rc, sizeof(rc));
  533. sizeNeeded = sizeof(rc);
  534. status = STATUS_SUCCESS;
  535. #else
  536. sizeNeeded = 0;
  537. status = STATUS_WMI_ITEMID_NOT_FOUND;
  538. #endif
  539. break;
  540. }
  541. default:
  542. DebugPrint((TRACE, "ProcessorExecuteWmiMethod: Uknown MethodId of 0x%x\n", MethodId));
  543. status = STATUS_WMI_ITEMID_NOT_FOUND;
  544. }
  545. } else {
  546. status = STATUS_WMI_GUID_NOT_FOUND;
  547. }
  548. status = WmiCompleteRequest(DeviceObject,
  549. Irp,
  550. status,
  551. sizeNeeded,
  552. IO_NO_INCREMENT);
  553. return status;
  554. }
  555. NTSTATUS
  556. ProcessorWmiFunctionControl(
  557. IN PDEVICE_OBJECT DeviceObject,
  558. IN PIRP Irp,
  559. IN ULONG GuidIndex,
  560. IN WMIENABLEDISABLECONTROL Function,
  561. IN BOOLEAN Enable
  562. )
  563. /*++
  564. Routine Description:
  565. This routine is a callback into the driver to enabled or disable event
  566. generation or data block collection. A device should only expect a
  567. single enable when the first event or data consumer enables events or
  568. data collection and a single disable when the last event or data
  569. consumer disables events or data collection. Data blocks will only
  570. receive collection enable/disable if they were registered as requiring
  571. it. If the driver can complete enabling/disabling within the callback it
  572. should call WmiCompleteRequest to complete the irp before returning to
  573. the caller. Or the driver can return STATUS_PENDING if the irp cannot be
  574. completed immediately and must then call WmiCompleteRequest once the
  575. data is changed.
  576. Arguments:
  577. DeviceObject is the device object
  578. GuidIndex is the index into the list of guids provided when the
  579. device registered
  580. Function specifies which functionality is being enabled or disabled
  581. (currently either WmiEventControl or WmiDataBlockControl)
  582. Enable is TRUE then the function is being enabled else disabled
  583. Return Value:
  584. status
  585. --*/
  586. {
  587. NTSTATUS status = STATUS_SUCCESS;
  588. PIO_STACK_LOCATION irpStack;
  589. PWNODE_HEADER Wnode;
  590. PFDO_DATA devExt;
  591. DebugEnter();
  592. DebugAssert(DeviceObject);
  593. if (Function) {
  594. DebugPrint((ERROR, "ProcessorWmiFunctionControl doesn't handle WmiDataBlockControl requests\n"));
  595. return STATUS_UNSUCCESSFUL;
  596. }
  597. irpStack = IoGetCurrentIrpStackLocation(Irp);
  598. Wnode = (PWNODE_HEADER)irpStack->Parameters.WMI.Buffer;
  599. DebugAssert(irpStack->Parameters.WMI.BufferSize >= sizeof(WNODE_HEADER));
  600. devExt = DeviceObject->DeviceExtension;
  601. //
  602. // Enable/Disable events
  603. //
  604. switch (GuidIndex) {
  605. case ProcessorWmiTracingId:
  606. //
  607. // NOTE: for the TraceLog event, level is passed in as the HIWORD(Wnode->Version)
  608. // from tracelog.exe -level
  609. //
  610. //
  611. if (Enable) {
  612. InterlockedExchange(&WmiTraceEnable, 1);
  613. WmiTraceHandle = ((PWNODE_HEADER)irpStack->Parameters.WMI.Buffer)->HistoricalContext;
  614. UsingGlobalWmiTraceHandle = FALSE;
  615. } else {
  616. InterlockedExchange(&WmiTraceEnable, 0);
  617. WmiTraceHandle = 0;
  618. }
  619. break;
  620. case ProcessorWmiPStateEventId:
  621. if (devExt->PerfStates) {
  622. InterlockedExchange(&PStateEvent.Enabled, Enable);
  623. }
  624. break;
  625. case ProcessorWmiNewPStatesEventId:
  626. if (devExt->PssPackage) {
  627. InterlockedExchange(&NewPStatesEvent.Enabled, Enable);
  628. }
  629. break;
  630. case ProcessorWmiNewCStatesEventId:
  631. if (devExt->CstPresent) {
  632. InterlockedExchange(&NewCStatesEvent.Enabled, Enable);
  633. }
  634. break;
  635. default:
  636. status = STATUS_WMI_GUID_NOT_FOUND;
  637. break;
  638. }
  639. status = WmiCompleteRequest(DeviceObject,
  640. Irp,
  641. status,
  642. 0,
  643. IO_NO_INCREMENT);
  644. return status;
  645. }
  646. NTSTATUS
  647. _cdecl
  648. ProcessorWmiLogEvent(
  649. IN ULONG LogLevel,
  650. IN ULONG LogType,
  651. IN LPGUID TraceGuid,
  652. IN PUCHAR Format,
  653. ...
  654. )
  655. /*++
  656. Routine Description:
  657. Arguments:
  658. Return Value:
  659. --*/
  660. {
  661. va_list list;
  662. UCHAR eventString[PROCESSOR_EVENT_BUFFER_SIZE+1];
  663. NTSTATUS status = STATUS_SUCCESS;
  664. LARGE_INTEGER timeStamp;
  665. WMI_TRACE_INFO eventInfo;
  666. PEVENT_TRACE_HEADER wnodeEventItem;
  667. KIRQL irql;
  668. if (KeGetCurrentIrql() >= DISPATCH_LEVEL) {
  669. goto ProcessorWmiLogEventExit;
  670. }
  671. if (WmiTraceEnable) {
  672. //
  673. // record the time
  674. //
  675. KeQuerySystemTime(&timeStamp);
  676. va_start(list, Format);
  677. eventString[PROCESSOR_EVENT_BUFFER_SIZE]=0;
  678. _vsnprintf(eventString, PROCESSOR_EVENT_BUFFER_SIZE, Format, list);
  679. DebugAssert(WmiTraceHandle);
  680. eventInfo.TraceData.DataPtr = (ULONG64) eventString;
  681. eventInfo.TraceData.Length = strlen(eventString);
  682. eventInfo.TraceData.DataType = 0;
  683. eventInfo.TraceHeader.Size = sizeof(WMI_TRACE_INFO);
  684. eventInfo.TraceHeader.Class.Type = (UCHAR) LogType;
  685. eventInfo.TraceHeader.Class.Level = (UCHAR) LogLevel;
  686. eventInfo.TraceHeader.TimeStamp = timeStamp;
  687. eventInfo.TraceHeader.GuidPtr = (ULONGLONG) TraceGuid;
  688. eventInfo.TraceHeader.Flags = WNODE_FLAG_TRACED_GUID |
  689. WNODE_FLAG_USE_TIMESTAMP |
  690. WNODE_FLAG_USE_MOF_PTR |
  691. WNODE_FLAG_USE_GUID_PTR;
  692. wnodeEventItem = &eventInfo.TraceHeader;
  693. ((PWNODE_HEADER)wnodeEventItem)->HistoricalContext = WmiTraceHandle;
  694. //
  695. // Fire the event. Since this is a trace event and not a standard
  696. // WMI event, IoWMIWriteEvent will not attempt to free the buffer
  697. // passed into it.
  698. //
  699. status = IoWMIWriteEvent((PVOID)wnodeEventItem);
  700. if (!NT_SUCCESS(status)) {
  701. DebugPrint((ERROR, "IoWMIWriteEvent Failed! rc=0x%x len(%u)\n", status, strlen(eventString)));
  702. if (status == STATUS_INVALID_HANDLE) {
  703. DebugPrint((ERROR, "Invalid Handle == %I64x\n", WmiTraceHandle));
  704. }
  705. //
  706. // According to the powers that be, if we are using the global
  707. // handle, and the IoWMIWriteEvent() fails, then we need to stop
  708. // using the global handle
  709. //
  710. //if (UsingGlobalWmiTraceHandle) {
  711. // InterlockedExchange(&WmiTraceEnable, 0);
  712. // UsingGlobalWmiTraceHandle = FALSE;
  713. // WmiTraceHandle = 0;
  714. //}
  715. }
  716. }
  717. ProcessorWmiLogEventExit:
  718. return status;
  719. }
  720. NTSTATUS
  721. ProcessorFireWmiEvent(
  722. IN PFDO_DATA DeviceExtension,
  723. IN PWMI_EVENT Event,
  724. IN PVOID Data
  725. )
  726. /*++
  727. Routine Description:
  728. Arguments:
  729. Return Value:
  730. --*/
  731. {
  732. NTSTATUS status = STATUS_SUCCESS;
  733. PUCHAR eventData = NULL;
  734. PWNODE_SINGLE_INSTANCE wnode;
  735. DebugEnter();
  736. DebugAssert(Event);
  737. if (!Event->Enabled) {
  738. status = STATUS_SUCCESS;
  739. goto ProcessorFireWmiEventExit;
  740. }
  741. if (Data && Event->DataSize) {
  742. eventData = ExAllocatePoolWithTag(NonPagedPool,
  743. Event->DataSize,
  744. PROCESSOR_POOL_TAG);
  745. if (!eventData) {
  746. status = STATUS_INSUFFICIENT_RESOURCES;
  747. goto ProcessorFireWmiEventExit;
  748. }
  749. RtlCopyMemory(eventData, Data, Event->DataSize);
  750. }
  751. //
  752. // FireEvent... WmiFireEvent will free the memory
  753. //
  754. status = WmiFireEvent(DeviceExtension->Self,
  755. Event->Guid,
  756. 0,
  757. Event->DataSize,
  758. (PVOID) eventData);
  759. ProcessorFireWmiEventExit:
  760. DebugExitStatus(status);
  761. return status;
  762. }
  763. VOID
  764. ProcessorEnableGlobalLogging(
  765. VOID
  766. )
  767. /*++
  768. Routine Description:
  769. Arguments:
  770. Return Value:
  771. --*/
  772. {
  773. WmiSetLoggerId(WMI_GLOBAL_LOGGER_ID, &WmiTraceHandle);
  774. InterlockedExchange(&WmiTraceEnable, 1);
  775. UsingGlobalWmiTraceHandle = TRUE;
  776. }
  777. #if DBG
  778. PCHAR
  779. WMIMinorFunctionString (
  780. UCHAR MinorFunction
  781. )
  782. {
  783. switch (MinorFunction) {
  784. case IRP_MN_QUERY_ALL_DATA:
  785. return "IRP_MN_QUERY_ALL_DATA";
  786. case IRP_MN_QUERY_SINGLE_INSTANCE:
  787. return "IRP_MN_QUERY_SINGLE_INSTANCE";
  788. case IRP_MN_CHANGE_SINGLE_INSTANCE:
  789. return "IRP_MN_CHANGE_SINGLE_INSTANCE";
  790. case IRP_MN_CHANGE_SINGLE_ITEM:
  791. return "IRP_MN_CHANGE_SINGLE_ITEM";
  792. case IRP_MN_ENABLE_EVENTS:
  793. return "IRP_MN_ENABLE_EVENTS";
  794. case IRP_MN_DISABLE_EVENTS:
  795. return "IRP_MN_DISABLE_EVENTS";
  796. case IRP_MN_ENABLE_COLLECTION:
  797. return "IRP_MN_ENABLE_COLLECTION";
  798. case IRP_MN_DISABLE_COLLECTION:
  799. return "IRP_MN_DISABLE_COLLECTION";
  800. case IRP_MN_REGINFO:
  801. return "IRP_MN_REGINFO";
  802. case IRP_MN_EXECUTE_METHOD:
  803. return "IRP_MN_EXECUTE_METHOD";
  804. case IRP_MN_REGINFO_EX:
  805. return "IRP_MN_REGINFO_EX";
  806. default:
  807. return "Unknown IRP_MJ_SYSTEM_CONTROL";
  808. }
  809. }
  810. #endif