Windows NT Driver >> Pascal

Queue IRP


參考資訊:
1. Source Code
2. delphidriverdevelopmentkit

驅動程式作為User Application跟硬體的溝通橋樑,最常遇到的問題就是硬體處理速度不夠快,導致驅動程式需要等待硬體完成後,再繼續處理下一筆資料,但是驅動程式如果要跟User Application溝通這些同步事情,會產生額外的等待時間,因此,為了達到比較好的處理效能,一般驅動程式做會Queue Irp的動作,讓處理的間隔可以縮到最小,這也是這篇教學要說明的東西,教導使用者一些Queue Irp的基本觀念和作法。

首先要知道的就是,儲存Irp的地方在哪?在Windows驅動程式裡,可以使用List作為儲存媒介(使用者也可以使用其它方式儲存),使用步驟相當簡單,如下所示:
1. InitializeListHead()
2. InsertHeadList()
3. IsListEmpty()
4. RemoveHeadList()
首先初始化List(InitializeListHead()),接著插入(InsertHeadList())需要儲存的資料,接著判斷List是否還有資料(IsListEmpty()),假如還有資料,則取出(RemoveHeadList())處裡

Irp是處裡資料的最小單位,一個Irp通常代表處理一筆資料的訊息,因此,當硬體來不及處理資料時,驅動程式需要做Pending Irp的動作,讓User Application知道這筆資料稍後處理,Pending Irp的步驟如下:
1. IoMarkIrpPending()
2. IoCompleteRequest(STATUS_PENDING)
回傳Pending,告知I/O Manager,這筆Irp資料先Pending一下,稍後處理

接著就是User Application如何知道資料是否處理完畢或者被Pending了呢?在CreateFile()時,如果沒有用FILE_FLAG_OVERLAPPED,則代表每一筆資料都是必須完成才會回傳,此種寫法也稱為Blocking方式,如:呼叫WriteFile()後,必須等待驅動程式完成該Irp才會繼續往下,否則呼叫WriteFile()後,會卡在這個Win32 API,相反地,如果CreateFile()使用FILE_FLAG_OVERLAPPED,而呼叫WriteFile()時,驅動程式回傳Pending,則WriteFile()會馬上回傳ERROR_IO_PENDING,告知User Application,該筆資料驅動程式會稍後處裡,此種寫法也稱為Non-Blocking方式,那如何得知該筆資料處理完畢了呢?當使用Non-Blocking寫法,會在WriteFile()傳入一個OVERLAPPED參數,該參數裡面有一個Event,用來告知是否已經處理完畢了。

Queue IRP流程:
1. InsertHeadList()
2. IoMarkIrpPending()
3. IoCompleteRequest(STATUS_PENDING)
4. IsListEmpty()
5. RemoveHeadList()
6. IoCompleteRequest(STATUS_SUCCESS)

main.pas

unit main;

interface
  uses 
    DDDK;
    
  const 
    DEV_NAME = '\Device\MyDriver';
    SYM_NAME = '\DosDevices\MyDriver';
    IOCTL_QUEUE = $222000; // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
    IOCTL_PROCESS = $222004; // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
    
  function _DriverEntry(pOurDriver:PDriverObject; pOurRegistry:PUnicodeString):NTSTATUS; stdcall;

implementation
var 
  dpc: TKDpc;
  obj: KTIMER;
  queue: LIST_ENTRY;

procedure OnTimer(Dpc:KDPC; DeferredContext:Pointer; SystemArgument1:Pointer; SystemArgument2:Pointer); stdcall;
var
  irp: PIRP;
  plist: PLIST_ENTRY;
  
begin
  if IsListEmpty(@queue) = True then
  begin
    KeCancelTimer(@obj);
    DbgPrint('Finish', []);
  end
  else
  begin
    plist:= RemoveHeadList(@queue);
    
    // CONTAINING_RECORD(IRP.Tail.Overlay.ListEntry)
    irp:= Pointer(Integer(plist) - 88);
    irp^.IoStatus.Status:= STATUS_SUCCESS;
    irp^.IoStatus.Information:= 0;
    IoCompleteRequest(irp, IO_NO_INCREMENT);
    DbgPrint('Complete Irp', []);
  end;
end;

function IrpOpen(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
begin
  DbgPrint('IRP_MJ_CREATE', []);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= 0;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpClose(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
begin
  DbgPrint('IRP_MJ_CLOSE', []);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= 0;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpIOCTL(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  code: ULONG;
  tt: LARGE_INTEGER;
  psk: PIoStackLocation;
  
begin
  psk:= IoGetCurrentIrpStackLocation(pIrp);
  code:= psk^.Parameters.DeviceIoControl.IoControlCode;
  case code of 
  IOCTL_QUEUE:begin
    DbgPrint('IOCTL_QUEUE', []);
    
    InsertHeadList(@queue, @pIrp^.Tail.Overlay.s1.ListEntry);
    IoMarkIrpPending(pIrp);
    Result:= STATUS_PENDING;
    exit
  end;
  IOCTL_PROCESS:begin
    DbgPrint('IOCTL_PROCESS', []);
    tt.HighPart:= tt.HighPart or -1;
    tt.LowPart:= ULONG(-10000000);
    KeSetTimerEx(@obj, tt.LowPart, tt.HighPart, 1000, @dpc);
  end;
  end;
  
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= 0;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

procedure Unload(pOurDriver:PDriverObject); stdcall;
var
  szSymName: TUnicodeString;

begin
  RtlInitUnicodeString(@szSymName, SYM_NAME);
  IoDeleteSymbolicLink(@szSymName);
  IoDeleteDevice(pOurDriver^.DeviceObject);
end;

function _DriverEntry(pOurDriver:PDriverObject; pOurRegistry:PUnicodeString):NTSTATUS; stdcall;
var
  suDevName: TUnicodeString;
  szSymName: TUnicodeString;
  pOurDevice: PDeviceObject;
  
begin
  RtlInitUnicodeString(@suDevName, DEV_NAME);
  RtlInitUnicodeString(@szSymName, SYM_NAME);
  Result:= IoCreateDevice(pOurDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pOurDevice);

  if NT_SUCCESS(Result) then
  begin
    pOurDriver^.MajorFunction[IRP_MJ_CREATE]:= @IrpOpen;
    pOurDriver^.MajorFunction[IRP_MJ_CLOSE]  := @IrpClose;
    pOurDriver^.MajorFunction[IRP_MJ_DEVICE_CONTROL] := @IrpIOCTL;
    pOurDriver^.DriverUnload := @Unload;
    pOurDevice^.Flags:= pOurDevice^.Flags or DO_BUFFERED_IO;
    pOurDevice^.Flags:= pOurDevice^.Flags and not DO_DEVICE_INITIALIZING;
    InitializeListHead(@queue);
    KeInitializeTimer(@obj);
    KeInitializeDpc(@dpc, OnTimer, pOurDevice);
    Result:= IoCreateSymbolicLink(@szSymName, @suDevName);
  end;
end;
end.

DriverEntry()初始List以及DPC Timer,DPC Timer用來定期確認List是否還有資料需要處理,當然,使用Thread檢查或許更恰當些
IrpIOCTL()IOCTL_QUEUE代表該IOCTL會被Queue起來,稍後處理,而當收到IOCTL_PROCESS時,則啟動DPC Timer開始處理那些被Queue起來的Irp
OnTimer()處理Queue裡面的Irp

app.pas

program main;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  DIALOGS;

const
  METHOD_BUFFERED = 0;
  METHOD_IN_DIRECT = 1;
  METHOD_OUT_DIRECT = 2;
  METHOD_NEITHER = 3;
  FILE_ANY_ACCESS = 0;
  FILE_DEVICE_UNKNOWN = $22;

var
  fd: DWORD;
  ret: DWORD;
  cnt: DWORD;
  queue_code: DWORD;
  process_code: DWORD;
  ov: array [0..2] of OVERLAPPED;
  
begin
  fd:= CreateFile('\\.\MyDriver', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, Nil, OPEN_EXISTING, FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL, 0);
  if (fd <> INVALID_HANDLE_VALUE) then
  begin
    queue_code:= (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($800 shl 2) or (METHOD_BUFFERED);
    process_code:= (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($801 shl 2) or (METHOD_BUFFERED);
    
    for cnt:= 0 to 2 do
    begin
      ov[cnt].hEvent:= CreateEvent(Nil, TRUE, FALSE, Nil);
      WriteLn(Output, 'queue event');
      DeviceIoControl(fd, queue_code, Nil, 0, Nil, 0, ret, @ov[cnt]);
    end;
    
    WriteLn(Output, 'process all of events');
    DeviceIoControl(fd, process_code, Nil, 0, Nil, 0, ret, Nil);
    for cnt:= 0 to 2 do
    begin
      WaitForSingleObject(ov[cnt].hEvent, INFINITE);
      CloseHandle(ov[cnt].hEvent);
      WriteLn(Output, 'wait complete');
    end;
    CloseHandle(fd);
  end else
  begin
    WriteLn(Output, 'failed to open mydriver');
  end;
end.

結果


返回上一頁