Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection between mmap user call to mmap kernel call

I am trying to understand how mmap works. User level call of mmap looks like below.

void *mmap(void *addr, size_t len, int prot, int flags,
       int fildes, off_t off); 

but kernel level mmap for a particular device driver looks like:

int <device_name>_mmap(struct file*fp, struct vm_area_struct *vma)

I also looked at the source code but I am not able to find the connection in between.

How does mmap for particular device gets its arguments "struct vm_area_struct *vma" ? Can you please help me understand that ? Appreciate your help.

like image 646
vindyz Avatar asked Mar 21 '12 02:03

vindyz


People also ask

Which kernel object is created during the mmap () call?

mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr.

What is mmap Linux kernel?

In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It implements demand paging because file contents are not immediately read from disk and initially use no physical RAM at all.

What is mmap () used for?

The mmap() function establishes a mapping between a process' address space and a stream file. The address space of the process from the address returned to the caller, for a length of len, is mapped onto a stream file starting at offset off.

What is Remap_pfn_range?

remap_pfn_range() maps physical memory (by means of kernel logical address) to a user space process. It is particularly useful for implementing the mmap() system call.


1 Answers

The mmap() library call is implemented by libc, which converts the offset in bytes to an offset in pages, then calls the mmap_pgoff() system call.

The mmap_pgoff() system call fetches the struct file * corresponding to the file descriptor argument, and calls do_mmap_pgoff().

do_mmap_pgoff() calculates the actual address and length that will be used based on the hint and the available address space, converts the provided flags into VM flags, and tests for permission to perform the mapping. It then calls mmap_region().

mmap_region() removes any prior mappings in the area being replaced by the new mapping, performs memory accounting and creates the new struct vm_area_struct describing the region of the address space being mapped (this encapsulates the address, length, offset and VM flags of the mapping). It then calls the file's ->mmap() implementation, passing the struct file * and struct vm_area_struct *. For device files this will be a call to the device's mmap implementation function.

like image 66
caf Avatar answered Sep 30 '22 09:09

caf