參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk
main.c
#include <wdm.h> #define DEV_NAME L"\\Device\\MyDriver" #define SYM_NAME L"\\DosDevices\\MyDriver" #define MAX_THREAD 3 KSPIN_LOCK myLock = { 0 }; PVOID pThread[MAX_THREAD] = { 0 }; PDEVICE_OBJECT pNextDevice = NULL; void RunMe(int t) { int c0 = 0; int c1 = 0; KIRQL oldirql = 0; DbgPrint("Thread%d, Locking", t); KeAcquireSpinLock(&myLock, &oldirql); DbgPrint("Thread%d, Locked", t); for (c0 = 0; c0 < 10000; c0++) { for(c1 = 0; c1 < 10000; c1++) { } } DbgPrint("Thread%d, Unlocking", t); KeReleaseSpinLock(&myLock, oldirql); DbgPrint("Thread%d, Unlocked", t); } void MyThread(PVOID pParam) { LARGE_INTEGER stTime = { 0 }; stTime.HighPart |= -1; stTime.LowPart = -10000000; KeDelayExecutionThread(KernelMode, FALSE, &stTime); RunMe((int)pParam); PsTerminateSystemThread(STATUS_SUCCESS); } NTSTATUS AddDevice(PDRIVER_OBJECT pMyDriver, PDEVICE_OBJECT pPhyDevice) { PDEVICE_OBJECT pMyDevice = NULL; UNICODE_STRING usDeviceName = { 0 }; UNICODE_STRING usSymbolName = { 0 }; RtlInitUnicodeString(&usDeviceName, DEV_NAME); IoCreateDevice(pMyDriver, 0, &usDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pMyDevice); RtlInitUnicodeString(&usSymbolName, SYM_NAME); IoCreateSymbolicLink(&usSymbolName, &usDeviceName); pNextDevice = IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice); pMyDevice->Flags &= ~DO_DEVICE_INITIALIZING; pMyDevice->Flags |= DO_BUFFERED_IO; return STATUS_SUCCESS; } void Unload(PDRIVER_OBJECT pMyDriver) { pMyDriver = pMyDriver; } NTSTATUS IrpPnp(PDEVICE_OBJECT pMyDevice, PIRP pIrp) { int cc = 0; UNICODE_STRING usSymbolName = {0}; PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); if (pStack->MinorFunction == IRP_MN_REMOVE_DEVICE) { for (cc = 0; cc < MAX_THREAD; cc++) { if (pThread[cc] != NULL) { KeWaitForSingleObject(pThread[cc], Executive, KernelMode, FALSE, NULL); ObDereferenceObject(pThread[cc]); } } RtlInitUnicodeString(&usSymbolName, SYM_NAME); IoDeleteSymbolicLink(&usSymbolName); IoDetachDevice(pNextDevice); IoDeleteDevice(pMyDevice); IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_SUCCESS; } IoSkipCurrentIrpStackLocation(pIrp); return IoCallDriver(pNextDevice, pIrp); } NTSTATUS DriverEntry(PDRIVER_OBJECT pMyDriver, PUNICODE_STRING pMyRegistry) { int cc = 0; HANDLE hThread = 0; NTSTATUS status = 0; pMyDriver->MajorFunction[IRP_MJ_PNP] = IrpPnp; pMyDriver->DriverExtension->AddDevice = AddDevice; pMyDriver->DriverUnload = Unload; KeInitializeSpinLock(&myLock); for (cc = 0; cc < MAX_THREAD; cc++) { status = PsCreateSystemThread(&hThread, THREAD_ALL_ACCESS, NULL, (PHANDLE)-1, NULL, MyThread, (PVOID)cc); if (status == STATUS_SUCCESS) { ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL, KernelMode, &pThread[cc], NULL); ZwClose(hThread); } } return STATUS_SUCCESS; }
使用四顆CPU測試
每次只會有一顆CPU進入執行