Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 4: Virtual Process Memory


The implementation offilemap_faultuses thereadpagemethod of the underlying mapping and there-
fore adopts the aboveaddress_spaceconcept, as you will see in the concept description in Chapter 8.

4.7 Memory Mappings


Now that we are familiar with the data structures and address space operations related to memory
mappings, we move on in this section to examine the interaction between the kernel and the applications
when mappings are created. As we know, the C standard library features themmapfunction to install
mappings. Two system calls —mmapandmmap2— are provided on the kernel side. Some architectures
implement both versions [e.g., IA-64, and Sparc(64)], others only the first (AMD64) or only the second
(IA-32). Both have the same set of parameters.

asmlinkage unsigned long sys_mmap{2}(unsigned long addr, unsigned long len,
unsigned long prot, unsigned long flags, unsigned long fd,
unsigned long off)

Both calls create a mapping of lengthlenat positionposin the virtual user address space whose access
permissions are defined inprot.flagsis a flag set used to set a number of parameters. The relevant file
is identified by means of its file descriptor infd.

The difference betweenmmapandmmap2lies in the meaning of the offset (off). In both calls, it indicates
the point in the file at which mapping is to begin. Formmap, the position is specified in bytes, whereas
the unit used bymmap2is pages (PAGE_SIZE) — this enables file sections to be mapped even if the file is
larger than the address space available.

Typically, the C standard library provides only a single function for the creation of memory mappings
by applications. This function call is then translated internally to the system call appropriate to the archi-
tecture.

Themunmapsystem call is invoked to remove a mapping. There is no need for amunmap2system call
because no file offset is required — just the virtual address of the mapping.

4.7.1 Creating Mappings


The call syntax formmapandmmap2has already been introduced above, so I only need briefly list the most
important flags that can be set:

❑ MAP_FIXEDspecifies that no other address than the one given may be used for the mapping. If
this flag is not set, the kernel is free to change the desired address if, for example, a mapping
already resides there (the existing mapping would otherwise be overwritten).
❑ MAP_SHAREDmust be used when an object (usually a file) is to be shared between several pro-
cesses.
❑ MAP_PRIVATEcreates a private mapping that is separated from the contents of the source — write
operations on the mapped region have no effect on the data in the file.
❑ MAP_ANONYMOUScreates ananonymousmapping that is not associated with any data source — the
fdandoffparameters are ignored. This type of mapping can be used to allocatemalloc-like
memory for applications.
Free download pdf