Implementation of MMAP functions in driver development

Source: Internet
Author: User
When writing a device driver, if you want to map the device memory to the user space, you need to implement MMAP. by reading the ldd3 introduction above, you have a little understanding of MMAP implementation.
This article describes how to use
Int remap_pfn_range (struct vm_area_struct * VMA, unsigned long effec_addr,
Unsigned long PFN, unsigned long size, pgprot_t prot) function or
Int io_remap_page_range (struct vm_area_struct * VMA, unsigned long
Virt_addr, unsigned long phys_addr, unsigned long size, pgprot_t
And create a new page table. the difference between the two functions is that the first function is used when the PFN parameter points to the actual system Ram, and the second function is used when phys_addr points to the I/O memory. for the ARM platform, the system memory and port should be uniformly configured, so the two functions are equivalent.
Another function is the nopage function, which can be filled in the VMA struct. This function is called when there is no corresponding physical page on the virtual page, to allocate a physical page to a virtual page.
Int remap_page_range (unsigned long from, unsigned long phys_addr,
Unsigned long size, pgprot_t prot)

Here, from is the virtual address starting from the ing. This function constructs a page table within the range between the from and from + size of the virtual address space;

Phys_addr is the physical address mapped to the virtual address;
Size is the size of the mapped area;
Prot is the protection flag.

The remap_page_range processing process is to find the page Directory and page table (

Create a page table if necessary), clear the old content of the page table item, and enter its physical address and protection domain again.

Remap_page_range can process multiple consecutive physical pages. <Linux Device Driver> indicates that,

Remap_page_range can only be used to access retained pages and physical addresses in the physical memory.

In remap_page_range, the default nopage processing controls the zero page at the accessed virtual address of the ing. Therefore, after the memory is allocated, it is necessary to reserve the allocated memory, which is implemented through the mem_map_reserve function, which is to set the corresponding physical page

The pg_reserved flag.

Remap_pfn_range uses your page frame number to create a page table and map it to the user space!
Generally, your driver allocates a piece of memory, and then uses this piece of memory in the driver's MMAP.
The physical address is converted to the page frame number, and then the remap_pfn_range is called!
Assume that 1 is allocated through kmalloc (), get_free_pages (), and so on. Such memory pages cannot be mapped out through remap_pfn_range (). setpagereserverd () must be called for each page () only when it is marked as "retained", the pai_to_phys () function only obtains its physical address. The third parameter in remap_pfn_range () is the "frame" number on the physical page, that is, PFN, so your phys still need to ">
> Page_shift "Operation
Suppose 2 you are allocated through vmalloc. The same as above, the difference is that you want to use vmalloc_to_pfn.
3. It is recommended that you do not use the remap_pfn_page method for the physical memory page allocated with kmalloc, get_free_pages, and vmalloc. We recommend that you use the nopage method of VMA.
4. For memory of such devices, it is best to call pgprot_nocached (VMA->
Vm_page_prot) is passed to remap_pfn_range to prevent processor caching.

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.