handling IRPs 15: Building IRPs

Building IRPs

Drivers can create two types of IRPs:

·         Threaded IRPs, also called synchronous requests.

·         Nonthreaded IRPs, also called asynchronous requests.

Threaded IRPs

Threaded IRPs are bound to the current thread of execution when they are created. When the thread terminates, any threaded IRPs associated with it are automatically canceled.

Drivers create threaded IRPs by calling one of the following routines:

·         IoBuildSynchronousFsdRequest

·         IoBuildDeviceIoControlRequest

 

IoBuildSynchronousFsdRequest allocates and builds a threaded IRP for a read or write request.IoBuildDeviceIoControlRequest allocates and builds a threaded IRP to send an IOCTL.

When creating a threaded IRP, a driver specifies an event to signal when the IRP is complete and provides pointers to the buffers in which to return the requested data. When every sub-request in the IRP is complete (that is, when all the required drivers have completed the IRP), the entire request is complete. The I/O Manager then performs post-processing as follows:

·         Copies kernel-mode data to user-mode buffers to be returned to the caller. For example,ZwReadFile,IoBuildDeviceIoControlRequest, and similar system support routines pass user-mode buffers.

·         Signals the event, if the response is asynchronous. If the IoCallDriver routine returns STATUS_PENDING, the caller should wait on the event. 

·         Copies the value of the Irp->IoStatus.Status field to the Irp->UserIosb.Status field of the user I/O status block, if the response is asynchronous. IfIoCallDriver returns STATUS_PENDING, the caller can read this field after the event is signaled.

·         Copies the value of the Irp->IoStatus.Information field to the Irp‑>UserIosb.Information field of the user I/O status block, if the response is asynchronous and the I/O request succeeded. IfIoCallDriver returns STATUS_PENDING, the caller can read this field after the event is signaled. If the request returns an error status, the I/O Manager does not transfer any data to the user buffers, so the caller should treat the value atIrp‑>UserIosb.Information as zero; some driver writers might prefer to zero-initialize the field as an added precaution.  

·         Frees the IRP.

 

Nonthreaded IRPs

Nonthreaded IRPs are not associated with any thread. The driver that initiates a nonthreaded IRP must set a completion routine to “catch” the IRP when it is complete. The I/O Manager does not free nonthreaded IRPs; the driver that initiated the IRP must free it. Nonthreaded IRPs are intended for driver-to-driver communication.

Drivers create nonthreaded IRPs by calling one of the following routines:

·         IoBuildAsynchronousFsdRequest

·         IoAllocateIrp

 

IoBuildAsynchronousFsdRequest allocates and builds a nonthreaded IRP for a read or write request.IoAllocateIrp allocates an IRP for a driver to send to lower drivers in the same device stack or to another device stack.

IRP Cancellation

IRPs can be canceled (sometimes described as “recalled”). When an IRP is canceled, the driver that currently owns the IRP must complete it immediately.

A driver that requires special processing to cancel an IRP must either:

·         Supply an IoCancel routine to be notified when cancellation occurs and test theCancel flag in the IRP at various times during processing to determine whether the IRP has been canceled.

·         Use the new cancel-safe IRP queuing routines (IoCsqXxx).  

 

IRP cancellation can be difficult to code correctly because cancellation is inherently asynchronous and race conditions can occur at numerous points. The cancel-safe IRP queues greatly simplify cancellation logic and are strongly recommended for all new drivers. IRP cancellation is covered in the WDK and in the white paper titled “Cancel Logic in Windows Drivers.”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值