Wdk tips (2): io_stack_location

Source: Internet
Author: User
ArticleDirectory
    • Iocopycurrentirpstacklocationtonext
    • Ioskipcurrentirpstacklocation
    • Iogetnextirpstacklocation
    • Iosetnextirpstacklocation

As mentioned above, the driver model of the NT kernel does not fully use the function call stack. Instead, it uses an io_stack_location to store the driver call sequence. We know that the push and pop operations on function call stacks are implemented by the compiler. You can even write them without understanding the details.CodeBut the driver development is different. You need to take care of the Call Sequence, including when to go to the stack, when to go out of the stack, and what is retained in the stack, otherwise, bsod is not far ahead.

Io_stack_location-related functions include ioskipcurrentirpstacklocation, iosetnextirpstacklocation, iogetnextirpstacklocation, iocopycurrentirpstacklocationtonext, and iocalldriver.

Iocopycurrentirpstacklocationtonext

This function copies all content in current location to next location. It is usually used after you set completeroutine. Function implementation is very simple and stored in WDM. h In the form of macros:

 

# Define iocopycurrentirpstacklocationtonext (IRP) {\ pio_stack_location _ irpsp; \ pio_stack_location _ nextirpsp; \ _ irpsp = iogetcurrentirpstacklocation (IRP )); \ _ nextirpsp = iogetnextirpstacklocation (IRP); \ rtlcopymemory (_ nextirpsp, _ irpsp, field_offset (io_stack_location, completionroutine )); \__ nextirpsp-> control = 0 ;}

As shown above, it is actually an rtlcopymemory. It is generally used when the driver at this layer needs completeroutine. Common call sequences are as follows:

 

 
Irpstack = iogetcurrentirpstacklocation (IRP );//... Your code with irpstackiocopycurrentirpstacklocationtonext (IRP); iosetcompletionroutine (IRP, completeroutine, deviceextension, true); iocalldriver (deviceextension-> nextlower, IRP );

Note that iocalldriver will delete the stack location by one.

Ioskipcurrentirpstacklocation

The implementation of this function is simpler.

 

# Define ioskipcurrentirpstacklocation (IRP) {\ (IRP)-> currentlocation ++; \ (IRP)-> tail. Overlay. currentstacklocation ++ ;}

That is, the current location is set to the previous layer. This function is generally used with iocalldriver.

 

 
Ioskipcurrentirpstacklocation (IRP); // location + 1 iocalldriver (deviceextension-> nextlower, IRP); // location-1

After the preceding two steps are completed, the location is exactly the same as the caller, and the content in io_stack_location remains unchanged. Filter Driver commonly uses this method to forward IRP: receives an IRP, obtains or modifies its data, and continues forwarding, because the location is not changed, the completeroutine set by the upper-layer driver will still be called by the driver under the filter, and the filter will be the same as transparent.

Iogetnextirpstacklocation

This function obtains the pointer of the next layer of location.

 

 
# Define iogetnextirpstacklocation (IRP) (\ (IRP)-> tail. Overlay. currentstacklocation-1)

After obtaining the pointer, you can set iocontrolcode and other content and send it to the lower-layer driver for processing. It is generally used together with iocalldriver.

 

Nextstack = iogetnextirpstacklocation (IRP); nextstack-> majorfunction = irp_mj_internal_device_control; nextstack-> parameters. deviceiocontrol. iocontrolcode = Code; nextstack-> parameters. deviceiocontrol. outputbufferlength = 0; nextstack-> parameters. deviceiocontrol. inputbufferlength = 0; iocalldriver (deviceextension-> nextlower, IRP );
Iosetnextirpstacklocation

The iosetnextirpstacklocation function is implemented as follows:

 

 
# Define iosetnextirpstacklocation (IRP) {\ (IRP)-> currentlocation ++; \ (IRP)-> tail. Overlay. currentstacklocation -;}

opposite to ioskipcurrentirpstacklocation, it sets location to the next level. We know that functions such as iocalldriver will help us to reduce location by 1 when forwarding IRPs, so we need to take the initiative to reduce location to a rare moment, generally because of this need to use: you call ioallocateirp to generate an IRP and use the current location of the IRP. Because the current location of the newly generated IRP is unavailable, you must take the initiative to reduce it to a level. As the wdk documentation says, this requirement is extremely rare for two reasons: 1. In most cases, the IRP you handle is sent to you by others. 2. even if a new IRP is generated, the data required for the current layer does not need to be stored in the current location, because iosetcompleteroutine can accept a context field, and each time the completeroutine is called, the context field will also be returned to you. All the information required by the local driver can be placed in the context. However, we all know that there is an exception in everything. In fact, there is a need for iosetnextirpstacklocation. Let's look at an example.

assume that we need to allocate a new IRP and pass it to the lower-layer driver in a synchronous way. We first use ioallocateirp to obtain an IRP and set the corresponding content, call iogetnextirpstacklocation to obtain the lower-level location to set the control code to irp_mj_internal_device_control. Finally, call ioforwardirpsynchronously to synchronize the IRP. However, the problem arises: The ioforwardirpsynchronously function performs an iocopycurrentirpstacklocationtonext action internally, which replaces the content of next location with the content in current location, therefore, all the information about the control code we set is rushed out. What should we do? Since it will replace next with current, we will put the control code directly in current, and ioforwardirpsynchronously will copy it to the next layer. The desire is beautiful, but the cruel reality is that IRP is new, and there is no so-called current location! At this time, ioskipcurrentirpstacklocation will be very useful: Call ioskipcurrentirpstacklocation first to drop the location, then getcurrentirpstacklocation to get the current location, set the control code, and finally call ioforwardirpsynchronously to complete the IR.

You may have to ask: Why does ioforwardirpsynchronously take the initiative to help us with the copy to action. The answer is: there is no way to do this. I remember we said that the IO in the NT kernel is asynchronous, and the aforementioned so-called synchronous operations are also simulated using event: After the iocalldriver is called, wait is on an event, set event in completeroutine. We know the description of iocopycurrentirpstacklocationtonext. Since completeroutine is used, the iocopycurrentirpstacklocationtonext operation is no longer necessary.

 

Update:

Iosetnextirpstacklocation is also used. When you allocate an IRP, set completeroutine, and call iocalldriver, and everything is complete and so on, the problem arises: When the completeroutine you set is called, the input deviceobject is null. Why? Because completeroutine is set in next location, while deviceobject is saved in current location.
When an IRP is created, its current location is invalid, so there is no place to store the deivceobject. What should I do? Use iosetnextirpstacklocation as described above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.