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.

225 lines
5.0 KiB

  1. #include <ntddk.h>
  2. #include <string.h>
  3. #include "crashdrv.h"
  4. #define MEMSIZE 4096
  5. #define FCN(cc) ((cc >> 2) & 0xFFFFFF)
  6. #define DEVICE_NAME L"\\Device\\CrashDrv"
  7. #define DOSDEVICE_NAME L"\\DosDevices\\CrashDrv"
  8. typedef VOID (*PTESTFUNC)(PULONG ub);
  9. PTESTFUNC tests[] =
  10. {
  11. NULL,
  12. CrashDrvBugCheck,
  13. CrashDrvStackOverFlow,
  14. CrashDrvSimpleTest,
  15. CrashDrvExceptionTest,
  16. CrashDrvHardError,
  17. CrashSpecial
  18. };
  19. #define MaxTests (sizeof(tests)/sizeof(PTESTFUNC))
  20. ULONG CrashDrvRequest;
  21. KEVENT CrashEvent;
  22. ULONG CrashRequest;
  23. PULONG Funk;
  24. NTSTATUS
  25. CrashDrvOpenClose(
  26. IN PDEVICE_OBJECT DeviceObject,
  27. IN PIRP Irp
  28. );
  29. VOID
  30. CrashDrvUnload(
  31. IN PDRIVER_OBJECT DriverObject
  32. );
  33. NTSTATUS
  34. CrashDrvIoControl(
  35. IN PDEVICE_OBJECT DeviceObject,
  36. IN PIRP Irp
  37. );
  38. VOID
  39. CrashThread(
  40. PVOID Context
  41. );
  42. NTSTATUS
  43. DriverEntry(
  44. IN PDRIVER_OBJECT DriverObject,
  45. IN PUNICODE_STRING RegistryPath
  46. )
  47. {
  48. UNICODE_STRING DeviceName;
  49. PDEVICE_OBJECT deviceObject;
  50. NTSTATUS status;
  51. UNICODE_STRING LinkObject;
  52. WCHAR LinkName[80];
  53. ULONG DeviceSize;
  54. HANDLE ThreadHandle;
  55. RtlInitUnicodeString( &DeviceName, DEVICE_NAME );
  56. status = IoCreateDevice( DriverObject,
  57. 0,
  58. &DeviceName,
  59. FILE_DEVICE_NULL,
  60. 0,
  61. FALSE,
  62. &deviceObject );
  63. if (!NT_SUCCESS( status )) {
  64. return status;
  65. }
  66. LinkName[0] = UNICODE_NULL;
  67. RtlInitUnicodeString(&LinkObject, LinkName);
  68. LinkObject.MaximumLength = sizeof(LinkName);
  69. RtlAppendUnicodeToString(&LinkObject, L"\\DosDevices");
  70. DeviceSize = sizeof(L"\\Device") - sizeof(UNICODE_NULL);
  71. DeviceName.Buffer += DeviceSize / sizeof(WCHAR);
  72. DeviceName.Length -= (USHORT)DeviceSize;
  73. RtlAppendUnicodeStringToString(&LinkObject, &DeviceName);
  74. DeviceName.Buffer -= DeviceSize / sizeof(WCHAR);
  75. DeviceName.Length += (USHORT)DeviceSize;
  76. status = IoCreateSymbolicLink(&LinkObject, &DeviceName);
  77. if (!NT_SUCCESS(status)) {
  78. IoDeleteDevice( deviceObject );
  79. return status;
  80. }
  81. DriverObject->MajorFunction[IRP_MJ_CREATE] = CrashDrvOpenClose;
  82. DriverObject->MajorFunction[IRP_MJ_CLOSE] = CrashDrvOpenClose;
  83. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = CrashDrvIoControl;
  84. DriverObject->DriverUnload = CrashDrvUnload;
  85. KeInitializeEvent( &CrashEvent, NotificationEvent, FALSE );
  86. Funk = ExAllocatePool( PagedPool, MEMSIZE );
  87. status = PsCreateSystemThread(
  88. &ThreadHandle,
  89. 0,
  90. NULL,
  91. 0,
  92. NULL,
  93. CrashThread,
  94. NULL
  95. );
  96. return STATUS_SUCCESS;
  97. }
  98. NTSTATUS
  99. CrashDrvOpenClose(
  100. IN PDEVICE_OBJECT DeviceObject,
  101. IN PIRP Irp
  102. )
  103. {
  104. NTSTATUS status = STATUS_SUCCESS;
  105. Irp->IoStatus.Status = status;
  106. Irp->IoStatus.Information = 0;
  107. status = Irp->IoStatus.Status;
  108. IoCompleteRequest( Irp, 0 );
  109. return status;
  110. }
  111. VOID
  112. CrashDrvUnload(
  113. IN PDRIVER_OBJECT DriverObject
  114. )
  115. {
  116. PDEVICE_OBJECT currentDevice = DriverObject->DeviceObject;
  117. UNICODE_STRING fullLinkName;
  118. while (currentDevice) {
  119. RtlInitUnicodeString( &fullLinkName, DOSDEVICE_NAME );
  120. IoDeleteSymbolicLink(&fullLinkName);
  121. IoDeleteDevice(currentDevice);
  122. currentDevice = DriverObject->DeviceObject;
  123. }
  124. }
  125. NTSTATUS
  126. CrashDrvIoControl(
  127. IN PDEVICE_OBJECT DeviceObject,
  128. IN PIRP Irp
  129. )
  130. {
  131. NTSTATUS Status = STATUS_SUCCESS;
  132. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
  133. PULONG ub;
  134. ub = (PULONG) MmGetSystemAddressForMdl( Irp->MdlAddress );
  135. if (IrpSp->Parameters.DeviceIoControl.IoControlCode == IOCTL_CRASHDRV_CHECK_REQUEST) {
  136. ub[0] = CrashDrvRequest;
  137. CrashDrvRequest = 0;
  138. } else {
  139. if (FCN(IrpSp->Parameters.DeviceIoControl.IoControlCode) > MaxTests) {
  140. DbgBreakPoint();
  141. } else {
  142. tests[FCN(IrpSp->Parameters.DeviceIoControl.IoControlCode)]( ub );
  143. }
  144. }
  145. Irp->IoStatus.Information = 0L;
  146. Irp->IoStatus.Status = Status;
  147. IoCompleteRequest( Irp, 0 );
  148. return Status;
  149. }
  150. VOID
  151. CrashThread(
  152. PVOID Context
  153. )
  154. {
  155. while( TRUE ) {
  156. KeWaitForSingleObject( &CrashEvent, Executive, KernelMode, FALSE, NULL );
  157. KeResetEvent( &CrashEvent );
  158. switch( CrashRequest ) {
  159. case KMODE_EXCEPTION_NOT_HANDLED:
  160. {
  161. ULONG i,j;
  162. i = 0;
  163. j = 0;
  164. i = j / i;
  165. }
  166. break;
  167. case IRQL_NOT_LESS_OR_EQUAL:
  168. {
  169. KIRQL irql;
  170. KeRaiseIrql( DISPATCH_LEVEL, &irql );
  171. Funk[0] = 0;
  172. KeLowerIrql( irql );
  173. }
  174. break;
  175. }
  176. }
  177. }