ZZ: Direct I/O (device read/write) mmgetmdlbytecount/mmgetmdlbyteoffset/mmgetmdlvirtualaddress

Source: Internet
Author: User

Recently, I have been looking at WDM drivers, but some of them are not very clear about memory, such as the specific structure and usage of MDL.

I searched for the specific usage of MDL on the network, and learned the significance of the macro mmgetmdlbyteoffset. I always thought this value should be 0. This is not the case. This value is an offset relative to page START addrss. That is to say, when VA is changed to Pa, it does not start from page START address, but from a certain offset. I have not understood why. However, there are some differences in the use of such a situation. For example, if the error DMA transmission occurs, the MDL needs to be built again.


ZZ: http://www.cnblogs.com/lzjsky/archive/2010/11/19/1881926.html

This method requires you to set the do_direct_io attribute for the device after the device object is created.

Unlike the buffer read/write device, the operating system locks the buffer in user mode. Then, the operating system maps the buffer in the kernel mode address space. In this way, the user-mode buffer and the kernel-mode buffer point to the physical memory in the same region. No matter how the operating system switches the process, the kernel mode address remains unchanged.

The operating system (I/O manager) First locks the address in user mode, and the operating system (I/O manager) records this memory segment with the memory Descriptor Table (MDL data structure. For example. The user-mode buffer is continuous in the virtual memory, but it may be discrete in the physical memory.

MDL records this virtual memory, and the size of this virtual memory is stored in MDL-> bytecount. The first header address of this virtual memory is MDL-> startva, the offset of the first address of the virtual memory for the first header address is MDL-> byteoffset. Therefore, the first address of the virtual memory is:

MDL-> startva + MDL-> byteoffset

The following are several macros defined by DDK, so that we can get these values.

# Define mmgetmdlbytecount (MDL)-> bytecount)

# Define mmgetmdlbyteoffset (MDL)-> byteoffset)

# Define mmgetmdlvirtualaddress (MDL )\

(Pvoid) (pchar) (MDL)-> startva) + (MDL)-> byteoffset ))

Example:

Ntstatus read (in pdevice_object deviceobject, in pirp IRP) {pio_statck_location irpsp; ulong readlength; ulong mdllength; ulong mdloffset; pvoid mdladdress; ulong kerneladdress; // obtain the current stack unit irpsp = iogetcurrentirpstacklocation (IRP); // obtain the number of bytes to be read at the application layer readlength = irpsp-> parameters. read. length; // obtain the length of the virtual buffer locked at the application layer. mdllength = mmgetmdlbytecount (IRP-> mdladdress); // obtain the offset mdloffset = mmgetmdlbyteoffset (IRP-> mdladdress) of the locked buffer ); // obtain the virtual address mdladdress = mmgetmdlbyteoffset (IRP-> mdladdress) of the locked buffer; // obtain the ing address kerneladdress = mmgetsystemaddressformdlsafe (IRP-> mdladdress) of the virtual buffer in kernel mode ); IRP-> iostatus. status = STATUS_SUCCESS; IRP-> iostatus. information = 0; iocompleterequest (IRP, io_no_increment); retrun STATUS_SUCCESS ;}



 

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.