驅動程式 - Windows Driver Model (WDM) - 使用範例 - Pascal (DDDK) - Use Spin Lock



參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk

main.pas

unit main;
 
interface
    uses
        DDDK;
         
    const
        MAX_THREAD = 3;
        DEV_NAME = '\Device\MyDriver';
        SYM_NAME = '\DosDevices\MyDriver';
 
    function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall;
 
implementation
var
    myLock : KSPIN_LOCK;
    pNextDevice : PDEVICE_OBJECT;
 
procedure RunMe(v:ULONG); stdcall;
var
    c0 : ULONG;
    c1 : ULONG;
    oldIrql : KIRQL;
 
begin
    DbgPrint('Thread%d, Locking', [v]);
    KeAcquireSpinLock(@myLock, @oldIrql);
    DbgPrint('Thread%d, Locked', [v]);
 
    for c0 :=0 to 10000 do
        for c1 :=0 to 10000 do
            ;
 
    DbgPrint('Thread%d, Unlocking', [v]);
    KeReleaseSpinLock(@myLock, oldIrql);
    DbgPrint('Thread%d, Unlocked', [v]);
end;
 
procedure MyThread(pParam : Pointer); stdcall;
var
    tt : LARGE_INTEGER;
    
begin
    tt.HighPart := tt.HighPart or -1;
    tt.LowPart := ULONG(-10000000);
    KeDelayExecutionThread(KernelMode, FALSE, @tt);
 
    RunMe(ULONG(pParam));
    PsTerminateSystemThread(STATUS_SUCCESS);
end;
 
procedure Unload(pMyDriver : PDRIVER_OBJECT); stdcall;
begin
end;
 
function IrpPnp(pMyDevice : PDEVICE_OBJECT; pIrp : PIRP) : NTSTATUS; stdcall;
var
    pStack : PIO_STACK_LOCATION;
    suSymName : UNICODE_STRING;
           
begin
    pStack := IoGetCurrentIrpStackLocation(pIrp);
    if pStack^.MinorFunction = IRP_MN_REMOVE_DEVICE then
        begin
            RtlInitUnicodeString(@suSymName, SYM_NAME);
            IoDetachDevice(pNextDevice);
            IoDeleteDevice(pMyDevice);
            IoDeleteSymbolicLink(@suSymName);
            IoCompleteRequest(pIrp, IO_NO_INCREMENT);
            Result := STATUS_SUCCESS;
        end
    else
        begin
            IoSkipCurrentIrpStackLocation(pIrp);
            Result := IoCallDriver(pNextDevice, pIrp);
        end;
end;
 
function AddDevice(pMyDriver : PDRIVER_OBJECT; pPhyDevice : PDEVICE_OBJECT) : NTSTATUS; stdcall;
var
    suDevName : UNICODE_STRING;
    suSymName : UNICODE_STRING;
    pMyDevice : PDEVICE_OBJECT;
     
begin
    RtlInitUnicodeString(@suDevName, DEV_NAME);
    RtlInitUnicodeString(@suSymName, SYM_NAME);
    IoCreateDevice(pMyDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pMyDevice);
    pNextDevice := IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice);
    pMyDevice^.Flags := pMyDevice^.Flags or DO_BUFFERED_IO;
    pMyDevice^.Flags := pMyDevice^.Flags and not DO_DEVICE_INITIALIZING;
    Result := IoCreateSymbolicLink(@suSymName, @suDevName);
end;
 
function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall;
var
    cc : ULONG;
    hThread : Handle;
    status : NTSTATUS;
 
begin
    pMyDriver^.MajorFunction[IRP_MJ_PNP] := @IrpPnp;
    pMyDriver^.DriverExtension^.AddDevice := @AddDevice;
    pMyDriver^.DriverUnload := @Unload;
 
    KeInitializeSpinLock(@myLock);
    for cc := 0 to (MAX_THREAD-1) do
    begin
        status := PsCreateSystemThread(@hThread, THREAD_ALL_ACCESS, nil, Handle(-1), nil, MyThread, Pointer(cc));
        if NT_SUCCESS(status) then
        begin
            ZwClose(hThread);
        end;
    end;
    Result := STATUS_SUCCESS;
end;
end.

使用四顆CPU測試


每次只會有一顆CPU進入執行