Windows NT Driver >> Pascal >> File
DO_DIRECT_IO(DMA)
參考資訊:
1. Source Code
2. delphidriverdevelopmentkit
由於我們練習的驅動程式位於上層,本身並沒有觸及過多DMA的細節操作,因此,DO_DIRECT_IO的DMA作法會跟PIO一樣,唯一的區別就是取得DMA位址的方式,必須使用特殊的API取得,這篇練習在於說明有這樣的記憶體差別,當使用者實際觸及DMA驅動程式時,必須詳細閱讀DMA的操作方式,而關於DO_DIRECT_IO(DMA)的流程,可以參考如下圖表說明(IRP_MJ_READ):
記憶體指標:
IRP | Buffer | Length |
---|---|---|
IRP_MJ_READ | (MDL) MmGetMdlVirtualAddress |
(MDL) MmGetMdlByteCount |
IRP_MJ_WRITE | (MDL) MmGetMdlVirtualAddress |
(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:= MmGetMdlVirtualAddress(pIrp^.MdlAddress); 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:= MmGetMdlVirtualAddress(pIrp^.MdlAddress); len:= MmGetMdlByteCount(pIrp^.MdlAddress); memcpy(@szBuf[0], src, len); DbgPrint('Buffer: %s, Length: %d', [src, 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.
結果