Chapter 4: Virtual Process Memory
A combination ofPROT_EXEC,PROT_READ,PROT_WRITE,andPROT_NONEvalues can be used to define access
permission inprot. Not all combinations are implemented for all processors, with the result that the
region may be granted more rights than those specified. Although the kernel does its best to set the
desired mode, it can only guarantee that the access permissions set are not more restrictive than those
specified.
For the sake of simplicity, the description below deals only withsys_mmap2(sys_mmapbehaves in a very
similar way on most other architectures: all arrive in thedo_mmap_pgofffunction discussed below). In
line with the convention discussed in Chapter 13, the function serves as the entry point for themmap2
system call and immediately delegates work todo_mmap2. There the kernel references the file descriptor
to find thefileinstance with all the characteristic data of the file being processed (Chapter 8 examines
this data structure more closely). The remaining work is delegated todo_mmap_pgoff.
do_mmap_pgoffis an architecture-independentfunction defined inmm/mmap.c. Figure 4-12 shows the asso-
ciated code flow diagram.
Already existing region?
Compute flags
do_mmap_pgoff
get_unmapped_area
mmap_region
find_vma_prepare
do_munmap
file->f_op->mmap
make_pages_present
Check memory limits
Create a new vm_area_struct
VM_LOCKED set?
Return start address of mapping
Figure 4-12: Code flow diagram fordo_mmap_pgoff.
do_mmap_pgoffused to be one of the longest functions in the kernel. It is now effectively split into two
parts, which are, however, still rather voluminous. One part has to thoroughly check the parameters
of the user application, and the second part has to take a very large number of special situations and
subtleties into consideration. As the latter make no valuable contribution to a general understanding of
the mechanism involved, we look only at a representative standard situation — mapping of a regular file
withMAP_SHARED— to avoid bloating our description, and the code flow diagram also applies just for
this case.