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.

367 lines
10 KiB

  1. /*++
  2. Copyright (c) 1996-1998 Microsoft Corporation
  3. Module Name:
  4. cldskwmi.c
  5. Abstract:
  6. km wmi tracing code.
  7. Will be shared between our drivers.
  8. Authors:
  9. GorN 10-Aug-1999
  10. Environment:
  11. kernel mode only
  12. Notes:
  13. Revision History:
  14. Comments:
  15. This code is a quick hack to enable WMI tracing in cluster drivers.
  16. It should eventually go away.
  17. WmlTinySystemControl will be replaced with WmilibSystemControl from wmilib.sys .
  18. WmlTrace or equivalent will be added to the kernel in addition to IoWMIWriteEvent(&TraceBuffer);
  19. --*/
  20. #include <ntos.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <wmistr.h>
  24. #include <evntrace.h>
  25. #include "wmlkm.h"
  26. BOOLEAN
  27. WmlpFindGuid(
  28. IN PWML_CONTROL_GUID_REG GuidList,
  29. IN ULONG GuidCount,
  30. IN LPGUID Guid,
  31. OUT PULONG GuidIndex
  32. )
  33. /*++
  34. Routine Description:
  35. This routine will search the list of guids registered and return
  36. the index for the one that was registered.
  37. Arguments:
  38. GuidList is the list of guids to search
  39. GuidCount is the count of guids in the list
  40. Guid is the guid being searched for
  41. *GuidIndex returns the index to the guid
  42. Return Value:
  43. TRUE if guid is found else FALSE
  44. --*/
  45. {
  46. ULONG i;
  47. for (i = 0; i < GuidCount; i++)
  48. {
  49. if (IsEqualGUID(Guid, &GuidList[i].Guid))
  50. {
  51. *GuidIndex = i;
  52. return(TRUE);
  53. }
  54. }
  55. return(FALSE);
  56. }
  57. NTSTATUS
  58. WmlTinySystemControl(
  59. IN OUT PWML_TINY_INFO WmiLibInfo,
  60. IN PDEVICE_OBJECT DeviceObject,
  61. IN PIRP Irp
  62. )
  63. /*++
  64. Routine Description:
  65. Dispatch routine for IRP_MJ_SYSTEM_CONTROL. This routine will process
  66. all wmi requests received, forwarding them if they are not for this
  67. driver or determining if the guid is valid and if so passing it to
  68. the driver specific function for handing wmi requests.
  69. Arguments:
  70. WmiLibInfo has the WMI information control block
  71. DeviceObject - Supplies a pointer to the device object for this request.
  72. Irp - Supplies the Irp making the request.
  73. Return Value:
  74. status
  75. --*/
  76. {
  77. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  78. ULONG bufferSize;
  79. PUCHAR buffer;
  80. NTSTATUS status;
  81. ULONG retSize;
  82. UCHAR minorFunction;
  83. ULONG guidIndex;
  84. ULONG instanceCount;
  85. ULONG instanceIndex;
  86. //
  87. // If the irp is not a WMI irp or it is not targetted at this device
  88. // or this device has not regstered with WMI then just forward it on.
  89. minorFunction = irpStack->MinorFunction;
  90. if ((minorFunction > IRP_MN_EXECUTE_METHOD) ||
  91. (irpStack->Parameters.WMI.ProviderId != (ULONG_PTR)DeviceObject) ||
  92. ((minorFunction != IRP_MN_REGINFO) &&
  93. (WmiLibInfo->GuidCount == 0) || (WmiLibInfo->ControlGuids == NULL) ))
  94. {
  95. //
  96. // IRP is not for us so forward if there is a lower device object
  97. if (WmiLibInfo->LowerDeviceObject != NULL)
  98. {
  99. IoSkipCurrentIrpStackLocation(Irp);
  100. return(IoCallDriver(WmiLibInfo->LowerDeviceObject, Irp));
  101. } else {
  102. status = STATUS_INVALID_DEVICE_REQUEST;
  103. Irp->IoStatus.Status = status;
  104. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  105. return(status);
  106. }
  107. }
  108. buffer = (PUCHAR)irpStack->Parameters.WMI.Buffer;
  109. bufferSize = irpStack->Parameters.WMI.BufferSize;
  110. if (minorFunction != IRP_MN_REGINFO)
  111. {
  112. //
  113. // For all requests other than query registration info we are passed
  114. // a guid. Determine if the guid is one that is supported by the
  115. // device.
  116. if (WmlpFindGuid(WmiLibInfo->ControlGuids,
  117. WmiLibInfo->GuidCount,
  118. (LPGUID)irpStack->Parameters.WMI.DataPath,
  119. &guidIndex) )
  120. {
  121. status = STATUS_SUCCESS;
  122. } else {
  123. status = STATUS_WMI_GUID_NOT_FOUND;
  124. }
  125. if (!NT_SUCCESS(status))
  126. {
  127. Irp->IoStatus.Status = status;
  128. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  129. return(status);
  130. }
  131. }
  132. switch(minorFunction)
  133. {
  134. case IRP_MN_REGINFO:
  135. {
  136. ULONG guidCount;
  137. PWML_CONTROL_GUID_REG guidList;
  138. PWMIREGINFOW wmiRegInfo;
  139. PWMIREGGUIDW wmiRegGuid;
  140. PDEVICE_OBJECT pdo;
  141. PUNICODE_STRING regPath;
  142. PWCHAR stringPtr;
  143. ULONG registryPathOffset;
  144. ULONG bufferNeeded;
  145. ULONG i;
  146. UNICODE_STRING nullRegistryPath;
  147. regPath = WmiLibInfo->DriverRegPath;
  148. guidList = WmiLibInfo->ControlGuids;
  149. guidCount = WmiLibInfo->GuidCount;
  150. if (regPath == NULL)
  151. {
  152. // No registry path specified. This is a bad thing for
  153. // the device to do, but is not fatal
  154. nullRegistryPath.Buffer = NULL;
  155. nullRegistryPath.Length = 0;
  156. nullRegistryPath.MaximumLength = 0;
  157. regPath = &nullRegistryPath;
  158. }
  159. registryPathOffset = FIELD_OFFSET(WMIREGINFOW, WmiRegGuid) +
  160. guidCount * sizeof(WMIREGGUIDW);
  161. bufferNeeded = registryPathOffset +
  162. regPath->Length + sizeof(USHORT);
  163. if (bufferNeeded <= bufferSize)
  164. {
  165. retSize = bufferNeeded;
  166. RtlZeroMemory(buffer, bufferNeeded);
  167. wmiRegInfo = (PWMIREGINFO)buffer;
  168. wmiRegInfo->BufferSize = bufferNeeded;
  169. // wmiRegInfo->NextWmiRegInfo = 0;
  170. // wmiRegInfo->MofResourceName = 0;
  171. wmiRegInfo->RegistryPath = registryPathOffset;
  172. wmiRegInfo->GuidCount = guidCount;
  173. for (i = 0; i < guidCount; i++)
  174. {
  175. wmiRegGuid = &wmiRegInfo->WmiRegGuid[i];
  176. wmiRegGuid->Guid = guidList[i].Guid;
  177. wmiRegGuid->Flags = WMIREG_FLAG_TRACED_GUID | WMIREG_FLAG_TRACE_CONTROL_GUID;
  178. // wmiRegGuid->InstanceInfo = 0;
  179. // wmiRegGuid->InstanceCount = 0;
  180. }
  181. stringPtr = (PWCHAR)((PUCHAR)buffer + registryPathOffset);
  182. *stringPtr++ = regPath->Length;
  183. RtlCopyMemory(stringPtr,
  184. regPath->Buffer,
  185. regPath->Length);
  186. status = STATUS_SUCCESS;
  187. } else {
  188. status = STATUS_BUFFER_TOO_SMALL;
  189. *((PULONG)buffer) = bufferNeeded;
  190. retSize = sizeof(ULONG);
  191. }
  192. Irp->IoStatus.Status = status;
  193. Irp->IoStatus.Information = retSize;
  194. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  195. return(status);
  196. }
  197. case IRP_MN_ENABLE_EVENTS:
  198. case IRP_MN_DISABLE_EVENTS:
  199. {
  200. PWNODE_HEADER Wnode = irpStack->Parameters.WMI.Buffer;
  201. PWML_CONTROL_GUID_REG Ctx = WmiLibInfo->ControlGuids + guidIndex;
  202. if (irpStack->Parameters.WMI.BufferSize >= sizeof(WNODE_HEADER)) {
  203. status = STATUS_SUCCESS;
  204. if (minorFunction == IRP_MN_DISABLE_EVENTS) {
  205. Ctx->EnableFlags64 = 0;
  206. } else {
  207. Ctx->LoggerHandle = (TRACEHANDLE)( Wnode->HistoricalContext );
  208. Ctx->EnableLevel = WmiGetLoggerEnableLevel(Ctx->LoggerHandle);
  209. Ctx->EnableFlags = WmiGetLoggerEnableFlags(Ctx->LoggerHandle);
  210. }
  211. } else {
  212. status = STATUS_INVALID_PARAMETER;
  213. }
  214. break;
  215. }
  216. case IRP_MN_ENABLE_COLLECTION:
  217. case IRP_MN_DISABLE_COLLECTION:
  218. {
  219. status = STATUS_SUCCESS;
  220. break;
  221. }
  222. case IRP_MN_QUERY_ALL_DATA:
  223. case IRP_MN_QUERY_SINGLE_INSTANCE:
  224. case IRP_MN_CHANGE_SINGLE_INSTANCE:
  225. case IRP_MN_CHANGE_SINGLE_ITEM:
  226. case IRP_MN_EXECUTE_METHOD:
  227. {
  228. status = STATUS_INVALID_DEVICE_REQUEST;
  229. break;
  230. }
  231. default:
  232. {
  233. status = STATUS_INVALID_DEVICE_REQUEST;
  234. break;
  235. }
  236. }
  237. Irp->IoStatus.Status = status;
  238. Irp->IoStatus.Information = 0;
  239. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  240. return(status);
  241. }
  242. typedef struct _TRACE_BUFFER {
  243. union {
  244. EVENT_TRACE_HEADER Trace;
  245. WNODE_HEADER Wnode;
  246. };
  247. MOF_FIELD MofFields[MAX_MOF_FIELDS + 1];
  248. } TRACE_BUFFER, *PTRACE_BUFFER;
  249. //////////////////////////////////////////////////////////////////////
  250. // 0 | Size | ProviderId | 0 |Size.HT.Mk | Typ.Lev.Version|
  251. // 2 | L o g g e r H a n d l e | 2 | T h r e a d I d |
  252. // 4 | T i m e S t a m p | 4 | T i m e S t a m p |
  253. // 6 | G U I D L o w | 6 | GUID Ptr / Guid L o w |
  254. // 8 | G U I D H I g h | 8 | G U I D H i g h |
  255. // 10 | ClientCtx | Flags | 10 |KernelTime | UserTime |
  256. //////////////////////////////////////////////////////////////////////
  257. ULONG
  258. WmlTrace(
  259. IN ULONG Type,
  260. IN LPCGUID TraceGuid,
  261. IN TRACEHANDLE LoggerHandle,
  262. ... // Pairs: Length, Address
  263. )
  264. {
  265. TRACE_BUFFER TraceBuffer;
  266. ((PULONG)&TraceBuffer)[1] = Type;
  267. TraceBuffer.Wnode.HistoricalContext = LoggerHandle; // [KM]
  268. TraceBuffer.Trace.Guid = *TraceGuid;
  269. TraceBuffer.Wnode.Flags =
  270. WNODE_FLAG_USE_MOF_PTR | // MOF data are dereferenced
  271. WNODE_FLAG_TRACED_GUID; // Trace Event, not a WMI event
  272. {
  273. PMOF_FIELD ptr = TraceBuffer.MofFields;
  274. va_list ap;
  275. va_start(ap, LoggerHandle);
  276. do {
  277. if ( 0 == (ptr->Length = (ULONG)va_arg (ap, size_t)) ) {
  278. break;
  279. }
  280. ptr->DataPtr = (ULONGLONG)va_arg(ap, PVOID);
  281. } while ( ++ptr < &TraceBuffer.MofFields[MAX_MOF_FIELDS] );
  282. va_end(ap);
  283. TraceBuffer.Wnode.BufferSize = (ULONG) ((ULONG_PTR)ptr - (ULONG_PTR)&TraceBuffer);
  284. }
  285. IoWMIWriteEvent(&TraceBuffer); // [KM]
  286. return STATUS_SUCCESS;
  287. }