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 the mmap function to install mappings. Two system calls — mmap and mmap2 — 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 length len at position pos in the virtual user address space whose access permissions are defined in prot. flags is a flag set used to set a number of parameters. The relevant file is identified by means of its file descriptor in fd.

The difference between mmap and mmap2 lies in the meaning of the offset (off). In both calls, it indicates the point in the file at which mapping is to begin. For mmap, the position is specified in bytes, whereas the unit used by mmap2 is 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 architecture.

The munmap system call is invoked to remove a mapping. There is no need for a munmap2 system call because no file offset is required — just the virtual address of the mapping.

Continue reading here: Creating Mappings

Was this article helpful?

0 0