9
votes

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.

1
actually I did but not able to find the connectionvindyz
The vm_area_struct contains the information about the virtual address range to be mapped in the user process and the offset on the device the application wants to map. The file argument corresponds to the file descriptor on the user side.R.. GitHub STOP HELPING ICE

1 Answers

16
votes

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.