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:
PROT_READ
(0b00001): Read permissions.
PROT_WRITE
(0b00010): Write permissions.
MAP_FIXED
(0b00100): Use hint as a hard requirement.
MAP_ANON
(0b01000): Mapping is not backed by any file, and fd
is
ignored.
MAP_WC
(0b10000): Map using write-combining when possible.
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:
EINVAL
: Bad hints or parameters.
ENOMEM
: The operation could not be completed due to a lack of memory.
EACCES
: MAC disallowed this.