9.9 mmap/mprotect/munmap

void *mmap(void *hint, size_t length, int protection, int flags, int fd,
   off_t offset);
int mprotect(void *addr, size_t len, int prot);
int munmap(void *address, size_t length);

mmap creates a new mapping in the virtual address space of the calling process. An address can be passed, if it is null, then the kernel gets to choose the address, else, it is taken as a hint about where to place the mapping. If a section of the mapping overlaps an existing mapping, it will be ignored.

fd may be used to specify a device or inode in the filesystem to back the memory region. For devices, the mmap operation has a different meaning for each device. For framebuffer devices, for example, one can make framebuffer windows this way. For inodes, the contents of the file will be used to initialize the requested memory window, starting to read from offset, filling with zeros the non-filled part, if any. This kind of filling does not advance the reading counter of the passed device or inode.

hint and length are required to be aligned to page boundaries for the running architecture, else it will fail.

protection and flags are a bitfield of the following flags:

munmap will unmap a range for the virtual address space of the calling process, this values must be the same as passed and returned by mmap, partial unmapping is allowed.

mprotect allows to change the permission of a range of memory of the passed length pointed by addr, previously mapped by the caller. The format of prot is the same as mmap.

mmap returns a pointer to the allocated area, or -1 on failure. munmap and mprotect both returns 0 on success and -1 on failure. All the functions set the following errno: