Windows NT Driver >> Pascal >> File

DO_DIRECT_IO(PIO)


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

DO_DIRECT_IO的概念就是直接Mapping User Buffer,然後Driver使用該Mapped的MDL(Memory Description List)操作,相較於DO_BUFFERED_IO,因為不須I/O Manager更新回User Buffer,因此,效率會比較好,而如果Driver會操作到DMA(Direct Memory Access)記憶體的話,不適用此方式,雖然司徒目前還沒有寫過DMA相關的Windows Driver,不過依據Microsoft的說明文件來看,使用DMA的話,必須使用另一個API(MmGetMdlVirtualAddress)去取得DMA實體記憶體位址,反之,如果不是DMA的操作,就是PIO(Programmed I/O)方式。

參考如下微軟的圖表說明(IRP_MJ_READ):

記憶體指標:

IRP Buffer Length
IRP_MJ_READ (MDL)
MmGetSystemAddressForMdlSafe
(MDL)
MmGetMdlByteCount
IRP_MJ_WRITE (MDL)
MmGetSystemAddressForMdlSafe
(MDL)
MmGetMdlByteCount

main.pas

unit main;

interface
  uses 
    DDDK;
    
  const 
    DEV_NAME = '\Device\MyDriver';
    SYM_NAME = '\DosDevices\MyDriver';
    
  function _DriverEntry(pOurDriver:PDriverObject; pOurRegistry:PUnicodeString):NTSTATUS; stdcall;

implementation
var
  szBuf: array[0..255] of char;
  
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 IrpRead(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  len: ULONG;
  dst: PChar;

begin
  DbgPrint('IRP_MJ_READ', []);
  
  len:= strlen(@szBuf[0]) + 1;
  dst:= MmGetSystemAddressForMdlSafe(pIrp^.MdlAddress, LowPagePriority);
  memcpy(dst, @szBuf[0], len);

  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= len;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpWrite(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  len: ULONG;
  src: PChar;

begin
  DbgPrint('IRP_MJ_WRITE', []);

  src:= MmGetSystemAddressForMdlSafe(pIrp^.MdlAddress, LowPagePriority);
  len:= MmGetMdlByteCount(pIrp^.MdlAddress);
  memcpy(@szBuf[0], src, len);
  DbgPrint('Buffer: %s, Length: %d', [szBuf, len]);
 
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= len;
  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;

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_READ]  := @IrpRead;
    pOurDriver^.MajorFunction[IRP_MJ_WRITE] := @IrpWrite;
    pOurDriver^.MajorFunction[IRP_MJ_CLOSE] := @IrpClose;
    pOurDriver^.DriverUnload := @Unload;
    pOurDevice^.Flags:= pOurDevice^.Flags or DO_DIRECT_IO;
    pOurDevice^.Flags:= pOurDevice^.Flags and not DO_DEVICE_INITIALIZING;
    Result:= IoCreateSymbolicLink(@szSymName, @suDevName);
  end;
end;
end.

IrpFile()收到IRP_MJ_WRITE時,Driver複製User Buffer的內容到szBuffer,而收到IRP_MJ_READ時,將szBuffer內容又複製回User Buffer,完成暫存的功能,IoStatus.Information的數值就是ReadFile()或WriteFile()完成的長度。

app.pas

program main;

{$APPTYPE CONSOLE}

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

var
  fd: DWORD;
  ret: DWORD;
  len: DWORD;
  szBuf: array[0..255] of char;

begin
  fd:= CreateFile('\\.\MyDriver', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (fd <> INVALID_HANDLE_VALUE) then
  begin
    StrCopy(szBuf, 'I am error');
    len:= strlen(szBuf)+1;
    WriteLn(Output, Format('WR: %s, %d', [szBuf, len]));
    WriteFile(fd, szBuf, len, ret, Nil);
    ReadFile(fd, szBuf, len, ret, Nil);
    WriteLn(Output, Format('RD: %s, %d', [szBuf, ret]));
    CloseHandle(fd);
  end else
  begin
    WriteLn(Output, 'failed to open mydriver');
  end;
end.

結果


返回上一頁