# More documentation on mmap_single



## faramir (Apr 26, 2013)

Hi, 

I am trying to memory-map I/O-physical memory to a user process which does a mmap call.(It is my small custom driver.) However, I am not able to find sufficient documentation on memory mapping physical memory to vm_object inside mmap_single. 

I have implemented d_mmap and it works without any issues. But I don*'*t want mmap to be called for every page two times. So, I am trying to explore mmap_single. Can someone direct me to some documentation?  

Thanks.


----------



## cpm@ (Apr 27, 2013)

A good start is read mmap(2). Here you have as example memfd driver that uses cdevpriv(9) and d_mmap_single().

Also see http://lists.freebsd.org/pipermail/freebsd-current/2012-August/036210.html.


----------



## faramir (May 9, 2013)

Hey thanks, 

I used the given reference and modified it for the device allocated memory. However, one thing I realized is I still need mmap along with mmap_single; mmap method as a handle to specify the physical address of the device allocated memory. That mmap handle gets called once for every page and then once for the whole range when a user actually tries to access the memory.  When the handler gets called for each offset, I do get context of the file_prev(application context). But when a user tries to access it I don't get any context. 

My pseudo code looks like: 


```
cdev method{
	.mmap = my_mmap;
	.mmap_single = my_mmap_single;

}

my_mmap(offset, *paddr)
{
     *paddr = actual_phy_addr_to_be_assigned; //based on offset.  
}
my_mmap_single(offset,**vm_object,nprot) {
    vm_object = vm_pager_allocator();
}
```

Am I doing something wrong or what is the rationale behind calling the mmap method once again when user space accesses the memory?


----------



## cpm@ (May 9, 2013)

Take a look to description from POSIX standard:
http://pubs.opengroup.org/onlinepubs/009695399/functions/mmap.html. 

Also read BSD mmap overview.

Furthermore, you might ask for help on freebsd-drivers@freebsd.org mailing list.


----------



## faramir (May 24, 2013)

Hi, 

I went through some other drivers and found the required implementation. Now I set a page fault handler, ctor and dtor in mmap_single calls and works well. However, when I was going through the BSD code, one of the thing I failed to understand was: why BSD chose not to provide the context when page faults happen on mmap'ed areas (when the mmap call is used and not mmap_single).


----------

