Blame man-pages-posix-2013-a/man3p/mmap.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH MMAP "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
mmap
Packit 7cfc04
\(em map pages of memory
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <sys/mman.h>
Packit 7cfc04
.P
Packit 7cfc04
void *mmap(void *\fIaddr\fP, size_t \fIlen\fP, int \fIprot\fP, int \fIflags\fP,
Packit 7cfc04
    int \fIfildes\fP, off_t \fIoff\fP);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall establish a mapping between an address space
Packit 7cfc04
of a process and a memory object.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall be supported for the following memory objects:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
Regular files
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
Shared memory objects
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
Typed memory objects
Packit 7cfc04
.P
Packit 7cfc04
Support for any other type of file is unspecified.
Packit 7cfc04
.P
Packit 7cfc04
The format of the call is as follows:
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
\fIpa\fR=\fImmap\fR(\fIaddr\fP, \fIlen\fP, \fIprot\fP, \fIflags\fP, \fIfildes\fP, \fIoff\fP);
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall establish a mapping between the address space of the
Packit 7cfc04
process at an address
Packit 7cfc04
.IR pa
Packit 7cfc04
for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes to the memory object represented by the file descriptor
Packit 7cfc04
.IR fildes
Packit 7cfc04
at offset
Packit 7cfc04
.IR off
Packit 7cfc04
for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes. The value of
Packit 7cfc04
.IR pa
Packit 7cfc04
is an implementation-defined function of the parameter
Packit 7cfc04
.IR addr
Packit 7cfc04
and the values of
Packit 7cfc04
.IR flags ,
Packit 7cfc04
further described below. A successful
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
call shall return
Packit 7cfc04
.IR pa
Packit 7cfc04
as its result. The address range starting at
Packit 7cfc04
.IR pa
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes shall be legitimate for the possible (not necessarily current)
Packit 7cfc04
address space of the process. The range of bytes starting at
Packit 7cfc04
.IR off
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes shall be legitimate for the possible (not necessarily current)
Packit 7cfc04
offsets in the memory object represented by
Packit 7cfc04
.IR fildes .
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR fildes
Packit 7cfc04
represents a typed memory object opened with either the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE flag or the POSIX_TYPED_MEM_ALLOCATE_CONTIG
Packit 7cfc04
flag, the memory object to be mapped
Packit 7cfc04
shall be that portion of the typed memory object allocated by the
Packit 7cfc04
implementation as specified below. In this case, if
Packit 7cfc04
.IR off
Packit 7cfc04
is non-zero, the behavior of
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
is undefined. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
refers to a valid typed memory object that is not accessible from the
Packit 7cfc04
calling process,
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall fail.
Packit 7cfc04
.P
Packit 7cfc04
The mapping established by
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall replace any previous mappings for those whole pages containing
Packit 7cfc04
any part of the address space of the process starting at
Packit 7cfc04
.IR pa
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes.
Packit 7cfc04
.P
Packit 7cfc04
If the size of the mapped file changes after the call to
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
as a result of some other operation on the mapped file, the effect of
Packit 7cfc04
references to portions of the mapped region that correspond to added or
Packit 7cfc04
removed portions of the file is unspecified.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR len
Packit 7cfc04
is zero,
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall fail and no mapping shall be established.
Packit 7cfc04
.P
Packit 7cfc04
The parameter
Packit 7cfc04
.IR prot
Packit 7cfc04
determines whether read, write, execute, or some combination of
Packit 7cfc04
accesses are permitted to the data being mapped. The
Packit 7cfc04
.IR prot
Packit 7cfc04
shall be either PROT_NONE
Packit 7cfc04
or the bitwise-inclusive OR of one or more of the other flags in
Packit 7cfc04
the following table, defined in the
Packit 7cfc04
.IR <sys/mman.h> 
Packit 7cfc04
header.
Packit 7cfc04
.TS
Packit 7cfc04
center box tab(!);
Packit 7cfc04
cB | cB
Packit 7cfc04
lw(1.5i) | lw(2i).
Packit 7cfc04
Symbolic Constant!Description
Packit 7cfc04
_
Packit 7cfc04
PROT_READ!Data can be read.
Packit 7cfc04
PROT_WRITE!Data can be written.
Packit 7cfc04
PROT_EXEC!Data can be executed.
Packit 7cfc04
PROT_NONE!Data cannot be accessed.
Packit 7cfc04
.TE
Packit 7cfc04
.P
Packit 7cfc04
If an implementation cannot support the combination of access types
Packit 7cfc04
specified by
Packit 7cfc04
.IR prot ,
Packit 7cfc04
the call to
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall fail.
Packit 7cfc04
.P
Packit 7cfc04
An implementation may permit accesses other than those specified by
Packit 7cfc04
.IR prot ;
Packit 7cfc04
however, the implementation shall not permit a write to succeed
Packit 7cfc04
where PROT_WRITE has not been set and shall not permit any access where
Packit 7cfc04
PROT_NONE alone has been set. The implementation shall support at least
Packit 7cfc04
the following values of
Packit 7cfc04
.IR prot :
Packit 7cfc04
PROT_NONE, PROT_READ, PROT_WRITE, and the bitwise-inclusive OR of
Packit 7cfc04
PROT_READ and PROT_WRITE. The file descriptor
Packit 7cfc04
.IR fildes
Packit 7cfc04
shall have been opened with read permission, regardless of the
Packit 7cfc04
protection options specified. If PROT_WRITE is specified, the
Packit 7cfc04
application shall ensure that it has opened the file descriptor
Packit 7cfc04
.IR fildes
Packit 7cfc04
with write permission unless MAP_PRIVATE is specified in the
Packit 7cfc04
.IR flags
Packit 7cfc04
parameter as described below.
Packit 7cfc04
.P
Packit 7cfc04
The parameter
Packit 7cfc04
.IR flags
Packit 7cfc04
provides other information about the handling of the mapped data.
Packit 7cfc04
The value of
Packit 7cfc04
.IR flags
Packit 7cfc04
is the bitwise-inclusive OR of these options, defined in
Packit 7cfc04
.IR <sys/mman.h> :
Packit 7cfc04
.TS
Packit 7cfc04
center box tab(!);
Packit 7cfc04
cB | cB
Packit 7cfc04
lw(1.5i) | lw(2i).
Packit 7cfc04
Symbolic Constant!Description
Packit 7cfc04
_
Packit 7cfc04
MAP_SHARED!Changes are shared.
Packit 7cfc04
MAP_PRIVATE!Changes are private.
Packit 7cfc04
MAP_FIXED!Interpret \fIaddr\fP exactly.
Packit 7cfc04
.TE
Packit 7cfc04
.P
Packit 7cfc04
It is implementation-defined whether MAP_FIXED shall be supported.
Packit 7cfc04
MAP_FIXED shall be supported on XSI-conformant systems.
Packit 7cfc04
.P
Packit 7cfc04
MAP_SHARED and MAP_PRIVATE describe the disposition of write references
Packit 7cfc04
to the memory object. If MAP_SHARED is specified, write references
Packit 7cfc04
shall change the underlying object. If MAP_PRIVATE is specified,
Packit 7cfc04
modifications to the mapped data by the calling process shall be visible
Packit 7cfc04
only to the calling process and shall not change the underlying object.
Packit 7cfc04
It is unspecified whether modifications to the underlying object done
Packit 7cfc04
after the MAP_PRIVATE mapping is established are visible through the
Packit 7cfc04
MAP_PRIVATE mapping. Either MAP_SHARED or MAP_PRIVATE can be
Packit 7cfc04
specified, but not both. The mapping type is retained across
Packit 7cfc04
\fIfork\fR().
Packit 7cfc04
.P
Packit 7cfc04
The state of synchronization objects such as mutexes, semaphores,
Packit 7cfc04
barriers, and conditional variables placed in shared memory mapped with
Packit 7cfc04
MAP_SHARED becomes undefined when the last region in any process
Packit 7cfc04
containing the synchronization object is unmapped.
Packit 7cfc04
.P
Packit 7cfc04
When
Packit 7cfc04
.IR fildes
Packit 7cfc04
represents a typed memory object opened with either the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE flag or the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE_CONTIG flag,
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall, if there are enough resources available, map
Packit 7cfc04
.IR len
Packit 7cfc04
bytes allocated from the corresponding typed memory object which were
Packit 7cfc04
not previously allocated to any process in any processor that may
Packit 7cfc04
access that typed memory object. If there are not enough resources
Packit 7cfc04
available, the function shall fail. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
represents a typed memory object opened with the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE_CONTIG flag, these allocated bytes shall be
Packit 7cfc04
contiguous within the typed memory object. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
represents a typed memory object opened with the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE flag, these allocated bytes may be composed of
Packit 7cfc04
non-contiguous fragments within the typed memory object. If
Packit 7cfc04
.IR fildes
Packit 7cfc04
represents a typed memory object opened with neither the
Packit 7cfc04
POSIX_TYPED_MEM_ALLOCATE_CONTIG flag nor the POSIX_TYPED_MEM_ALLOCATE
Packit 7cfc04
flag,
Packit 7cfc04
.IR len
Packit 7cfc04
bytes starting at offset
Packit 7cfc04
.IR off
Packit 7cfc04
within the typed memory object are mapped, exactly as when mapping a
Packit 7cfc04
file or shared memory object. In this case, if two processes map an
Packit 7cfc04
area of typed memory using the same
Packit 7cfc04
.IR off
Packit 7cfc04
and
Packit 7cfc04
.IR len
Packit 7cfc04
values and using file descriptors that refer to the same memory pool
Packit 7cfc04
(either from the same port or from a different port), both processes
Packit 7cfc04
shall map the same region of storage.
Packit 7cfc04
.P
Packit 7cfc04
When MAP_FIXED is set in the
Packit 7cfc04
.IR flags
Packit 7cfc04
argument, the implementation is informed that the value of
Packit 7cfc04
.IR pa
Packit 7cfc04
shall be
Packit 7cfc04
.IR addr ,
Packit 7cfc04
exactly. If MAP_FIXED is set,
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
may return MAP_FAILED and set
Packit 7cfc04
.IR errno
Packit 7cfc04
to
Packit 7cfc04
.BR [EINVAL] .
Packit 7cfc04
If a MAP_FIXED request is successful, the mapping established by
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
replaces any previous mappings for the pages in the range
Packit 7cfc04
[\fIpa\fP,\fIpa\fP+\fIlen\fR) of the process.
Packit 7cfc04
.P
Packit 7cfc04
When MAP_FIXED is not set, the implementation uses
Packit 7cfc04
.IR addr
Packit 7cfc04
in an implementation-defined manner to arrive at
Packit 7cfc04
.IR pa .
Packit 7cfc04
The
Packit 7cfc04
.IR pa
Packit 7cfc04
so chosen shall be an area of the address space that the implementation
Packit 7cfc04
deems suitable for a mapping of
Packit 7cfc04
.IR len
Packit 7cfc04
bytes to the file. All implementations interpret an
Packit 7cfc04
.IR addr
Packit 7cfc04
value of 0 as granting the implementation complete freedom in selecting
Packit 7cfc04
.IR pa ,
Packit 7cfc04
subject to constraints described below. A non-zero value of
Packit 7cfc04
.IR addr
Packit 7cfc04
is taken to be a suggestion of a process address near which the mapping
Packit 7cfc04
should be placed. When the implementation selects a value for
Packit 7cfc04
.IR pa ,
Packit 7cfc04
it never places a mapping at address 0, nor does it replace any extant
Packit 7cfc04
mapping.
Packit 7cfc04
.P
Packit 7cfc04
If MAP_FIXED is specified and
Packit 7cfc04
.IR addr
Packit 7cfc04
is non-zero, it shall have the same remainder as the
Packit 7cfc04
.IR off
Packit 7cfc04
parameter, modulo the page size as returned by
Packit 7cfc04
\fIsysconf\fR()
Packit 7cfc04
when passed _SC_PAGESIZE or _SC_PAGE_SIZE. The implementation may
Packit 7cfc04
require that off is a multiple of the page size. If MAP_FIXED is
Packit 7cfc04
specified, the implementation may require that
Packit 7cfc04
.IR addr
Packit 7cfc04
is a multiple of the page size. The system performs mapping operations
Packit 7cfc04
over whole pages. Thus, while the parameter
Packit 7cfc04
.IR len
Packit 7cfc04
need not meet a size or alignment constraint, the system shall include,
Packit 7cfc04
in any mapping operation, any partial page specified by the address
Packit 7cfc04
range starting at
Packit 7cfc04
.IR pa
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes.
Packit 7cfc04
.P
Packit 7cfc04
The system shall always zero-fill any partial page at the end of an
Packit 7cfc04
object. Further, the system shall never write out any modified
Packit 7cfc04
portions of the last page of an object which are beyond its end.
Packit 7cfc04
References within the address range starting at
Packit 7cfc04
.IR pa
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes to whole pages following the end of an object shall result in
Packit 7cfc04
delivery of a SIGBUS signal.
Packit 7cfc04
.P
Packit 7cfc04
An implementation may generate SIGBUS signals when a reference would
Packit 7cfc04
cause an error in the mapped object, such as out-of-space condition.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall add an extra reference to the file associated with the
Packit 7cfc04
file descriptor
Packit 7cfc04
.IR fildes
Packit 7cfc04
which is not removed by a subsequent
Packit 7cfc04
\fIclose\fR()
Packit 7cfc04
on that file descriptor. This reference shall be removed when there are
Packit 7cfc04
no more mappings to the file.
Packit 7cfc04
.P
Packit 7cfc04
The last data access timestamp of the mapped file may be marked for
Packit 7cfc04
update at any time between the
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
call and the corresponding
Packit 7cfc04
\fImunmap\fR()
Packit 7cfc04
call. The initial read or write reference to a mapped region shall cause
Packit 7cfc04
the file's last data access timestamp to be marked for update if it has
Packit 7cfc04
not already been marked for update.
Packit 7cfc04
.P
Packit 7cfc04
The last data modification and last file status change timestamps
Packit 7cfc04
of a file that is mapped with MAP_SHARED and PROT_WRITE shall be
Packit 7cfc04
marked
Packit 7cfc04
for update at some point in the interval between a write reference to
Packit 7cfc04
the mapped region and the next call to
Packit 7cfc04
\fImsync\fR()
Packit 7cfc04
with MS_ASYNC or MS_SYNC for that portion of the file by any process.
Packit 7cfc04
If there is no such call and if the underlying file is modified
Packit 7cfc04
as a result of a write reference, then these timestamps shall be marked
Packit 7cfc04
for update at some time after the write reference.
Packit 7cfc04
.P
Packit 7cfc04
There may be implementation-defined limits on the number of memory
Packit 7cfc04
regions that can be mapped (per process or per system).
Packit 7cfc04
.P
Packit 7cfc04
If such a limit is imposed, whether the number of memory regions that
Packit 7cfc04
can be mapped by a process is decreased by the use of
Packit 7cfc04
\fIshmat\fR()
Packit 7cfc04
is implementation-defined.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
fails for reasons other than
Packit 7cfc04
.BR [EBADF] ,
Packit 7cfc04
.BR [EINVAL] ,
Packit 7cfc04
or
Packit 7cfc04
.BR [ENOTSUP] ,
Packit 7cfc04
some of the mappings in the address range starting at
Packit 7cfc04
.IR addr
Packit 7cfc04
and continuing for
Packit 7cfc04
.IR len
Packit 7cfc04
bytes may have been unmapped.
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
Upon successful completion, the
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall return the address at which the mapping was placed (\c
Packit 7cfc04
.IR pa );
Packit 7cfc04
otherwise, it shall return a value of MAP_FAILED and set
Packit 7cfc04
.IR errno
Packit 7cfc04
to indicate the error. The symbol MAP_FAILED is defined in the
Packit 7cfc04
.IR <sys/mman.h> 
Packit 7cfc04
header. No successful return from
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
shall return the value MAP_FAILED.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EACCES
Packit 7cfc04
The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument is not open for read, regardless of the protection specified,
Packit 7cfc04
or
Packit 7cfc04
.IR fildes
Packit 7cfc04
is not open for write and PROT_WRITE was specified for a MAP_SHARED
Packit 7cfc04
type mapping.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EAGAIN
Packit 7cfc04
The mapping could not be locked in memory, if required by
Packit 7cfc04
\fImlockall\fR(),
Packit 7cfc04
due to a lack of resources.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EBADF
Packit 7cfc04
The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument is not a valid open file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINVAL
Packit 7cfc04
The value of
Packit 7cfc04
.IR len
Packit 7cfc04
is zero.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINVAL
Packit 7cfc04
The value of
Packit 7cfc04
.IR flags
Packit 7cfc04
is invalid (neither MAP_PRIVATE nor MAP_SHARED is set).
Packit 7cfc04
.TP
Packit 7cfc04
.BR EMFILE
Packit 7cfc04
The number of mapped regions would exceed an implementation-defined
Packit 7cfc04
limit (per process or per system).
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENODEV
Packit 7cfc04
The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument refers to a file whose type is not supported by
Packit 7cfc04
\fImmap\fR().
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOMEM
Packit 7cfc04
MAP_FIXED was specified, and the range
Packit 7cfc04
[\fIaddr\fP,\fIaddr\fP+\fIlen\fR) exceeds that allowed for the
Packit 7cfc04
address space of a process; or, if MAP_FIXED was not specified and
Packit 7cfc04
there is insufficient room in the address space to effect the mapping.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOMEM
Packit 7cfc04
The mapping could not be locked in memory, if required by
Packit 7cfc04
\fImlockall\fR(),
Packit 7cfc04
because it would require more space than the system is able to supply.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOMEM
Packit 7cfc04
Not enough unallocated memory resources remain in the typed memory
Packit 7cfc04
object designated by
Packit 7cfc04
.IR fildes
Packit 7cfc04
to allocate
Packit 7cfc04
.IR len
Packit 7cfc04
bytes.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOTSUP
Packit 7cfc04
MAP_FIXED or MAP_PRIVATE was specified in the
Packit 7cfc04
.IR flags
Packit 7cfc04
argument and the implementation does not support this functionality.
Packit 7cfc04
.RS 12 
Packit 7cfc04
.P
Packit 7cfc04
The implementation does not support the combination of accesses
Packit 7cfc04
requested in the
Packit 7cfc04
.IR prot
Packit 7cfc04
argument.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENXIO
Packit 7cfc04
Addresses in the range [\fIoff\fP,\fIoff\fP+\fIlen\fR) are invalid
Packit 7cfc04
for the object specified by
Packit 7cfc04
.IR fildes .
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENXIO
Packit 7cfc04
MAP_FIXED was specified in
Packit 7cfc04
.IR flags
Packit 7cfc04
and the combination of
Packit 7cfc04
.IR addr ,
Packit 7cfc04
.IR len ,
Packit 7cfc04
and
Packit 7cfc04
.IR off
Packit 7cfc04
is invalid for the object specified by
Packit 7cfc04
.IR fildes .
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENXIO
Packit 7cfc04
The
Packit 7cfc04
.IR fildes
Packit 7cfc04
argument refers to a typed memory object that is not accessible from
Packit 7cfc04
the calling process.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EOVERFLOW
Packit 7cfc04
The file is a regular file and the value of
Packit 7cfc04
.IR off
Packit 7cfc04
plus
Packit 7cfc04
.IR len
Packit 7cfc04
exceeds the offset maximum established in the open file description
Packit 7cfc04
associated with
Packit 7cfc04
.IR fildes .
Packit 7cfc04
.br
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function may fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EINVAL
Packit 7cfc04
The
Packit 7cfc04
.IR addr
Packit 7cfc04
argument (if MAP_FIXED was specified) or
Packit 7cfc04
.IR off
Packit 7cfc04
is not a multiple of the page size as returned by
Packit 7cfc04
\fIsysconf\fR(),
Packit 7cfc04
or is considered invalid by the implementation.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
None.
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
Use of
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
may reduce the amount of memory available to other memory allocation
Packit 7cfc04
functions.
Packit 7cfc04
.P
Packit 7cfc04
Use of MAP_FIXED may result in unspecified behavior in further use of
Packit 7cfc04
\fImalloc\fR()
Packit 7cfc04
and
Packit 7cfc04
\fIshmat\fR().
Packit 7cfc04
The use of MAP_FIXED is discouraged, as it may prevent an
Packit 7cfc04
implementation from making the most effective use of resources. Most
Packit 7cfc04
implementations require that
Packit 7cfc04
.IR off
Packit 7cfc04
and
Packit 7cfc04
.IR addr
Packit 7cfc04
are multiples of the page size as returned by
Packit 7cfc04
\fIsysconf\fR().
Packit 7cfc04
.P
Packit 7cfc04
The application must ensure correct synchronization when using
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
in conjunction with any other file access method, such as
Packit 7cfc04
\fIread\fR()
Packit 7cfc04
and
Packit 7cfc04
\fIwrite\fR(),
Packit 7cfc04
standard input/output, and
Packit 7cfc04
\fIshmat\fR().
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function allows access to resources via address space manipulations,
Packit 7cfc04
instead of
Packit 7cfc04
\fIread\fR()/\c
Packit 7cfc04
\fIwrite\fR().
Packit 7cfc04
Once a file is mapped, all a process has to do to access it is use the
Packit 7cfc04
data at the address to which the file was mapped. So, using
Packit 7cfc04
pseudo-code to illustrate the way in which an existing program might be
Packit 7cfc04
changed to use
Packit 7cfc04
\fImmap\fR(),
Packit 7cfc04
the following:
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
fildes = open(...)
Packit 7cfc04
lseek(fildes, some_offset)
Packit 7cfc04
read(fildes, buf, len)
Packit 7cfc04
/* Use data in buf. */
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.P
Packit 7cfc04
becomes:
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
fildes = open(...)
Packit 7cfc04
address = mmap(0, len, PROT_READ, MAP_PRIVATE, fildes, some_offset)
Packit 7cfc04
/* Use data at address. */
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
After considering several other alternatives, it was decided to adopt
Packit 7cfc04
the
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
definition found in SVR4 for mapping memory objects into process
Packit 7cfc04
address spaces. The SVR4 definition is minimal, in that it describes
Packit 7cfc04
only what has been built, and what appears to be necessary for a
Packit 7cfc04
general and portable mapping facility.
Packit 7cfc04
.P
Packit 7cfc04
Note that while
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
was first designed for mapping files, it is actually a general-purpose
Packit 7cfc04
mapping facility. It can be used to map any appropriate object, such
Packit 7cfc04
as memory, files, devices, and so on, into the address space of a
Packit 7cfc04
process.
Packit 7cfc04
.P
Packit 7cfc04
When a mapping is established, it is possible that the implementation
Packit 7cfc04
may need to map more than is requested into the address space of the
Packit 7cfc04
process because of hardware requirements. An application, however,
Packit 7cfc04
cannot count on this behavior. Implementations that do not use a paged
Packit 7cfc04
architecture may simply allocate a common memory region and return the
Packit 7cfc04
address of it; such implementations probably do not allocate any more
Packit 7cfc04
than is necessary. References past the end of the requested area are
Packit 7cfc04
unspecified.
Packit 7cfc04
.P
Packit 7cfc04
If an application requests a mapping that would overlay existing
Packit 7cfc04
mappings in the process, it might be desirable that an implementation
Packit 7cfc04
detect this and inform the application. However, the default, portable
Packit 7cfc04
(not MAP_FIXED)
Packit 7cfc04
operation does not overlay existing mappings. On the other hand, if the
Packit 7cfc04
program specifies a fixed address mapping (which requires some
Packit 7cfc04
implementation knowledge to determine a suitable address, if the
Packit 7cfc04
function is supported at all), then the program is presumed to be
Packit 7cfc04
successfully managing its own address space and should be trusted when
Packit 7cfc04
it asks to map over existing data structures. Furthermore, it is also
Packit 7cfc04
desirable to make as few system calls as possible, and it might be
Packit 7cfc04
considered onerous to require an
Packit 7cfc04
\fImunmap\fR()
Packit 7cfc04
before an
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
to the same address range. This volume of POSIX.1\(hy2008 specifies that the new mappings
Packit 7cfc04
replace any existing mappings, following existing practice in this
Packit 7cfc04
regard.
Packit 7cfc04
.P
Packit 7cfc04
It is not expected that all hardware implementations are able to
Packit 7cfc04
support all combinations of permissions at all addresses.
Packit 7cfc04
Implementations are required to disallow write
Packit 7cfc04
access to mappings without write permission and to disallow access to
Packit 7cfc04
mappings without any access permission. Other than these restrictions,
Packit 7cfc04
implementations may allow access types other than those requested by
Packit 7cfc04
the application. For example, if the application requests only
Packit 7cfc04
PROT_WRITE, the implementation may also allow read access. A call to
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
fails if the implementation cannot support allowing all the access
Packit 7cfc04
requested by the application. For example, some implementations
Packit 7cfc04
cannot support a request for both write access and execute access
Packit 7cfc04
simultaneously. All implementations must support requests for no access,
Packit 7cfc04
read access, write access, and both read and write access. Strictly
Packit 7cfc04
conforming code must only rely on the required checks. These restrictions
Packit 7cfc04
allow for portability across a wide range of hardware.
Packit 7cfc04
.P
Packit 7cfc04
The MAP_FIXED address treatment is likely to fail for non-page-aligned
Packit 7cfc04
values and for certain architecture-dependent address ranges.
Packit 7cfc04
Conforming implementations cannot count on being able to choose address
Packit 7cfc04
values for MAP_FIXED without utilizing non-portable,
Packit 7cfc04
implementation-defined knowledge. Nonetheless, MAP_FIXED is provided
Packit 7cfc04
as a standard interface conforming to existing practice for utilizing
Packit 7cfc04
such knowledge when it is available.
Packit 7cfc04
.P
Packit 7cfc04
Similarly, in order to allow implementations that do not support
Packit 7cfc04
virtual addresses, support for directly specifying any mapping
Packit 7cfc04
addresses via MAP_FIXED is not required and thus a conforming
Packit 7cfc04
application may not count on it.
Packit 7cfc04
.P
Packit 7cfc04
The MAP_PRIVATE
Packit 7cfc04
function can be implemented efficiently when memory protection hardware
Packit 7cfc04
is available. When such hardware is not available, implementations can
Packit 7cfc04
implement such ``mappings''
Packit 7cfc04
by simply making a real copy of the relevant data into process private
Packit 7cfc04
memory, though this tends to behave similarly to
Packit 7cfc04
\fIread\fR().
Packit 7cfc04
.P
Packit 7cfc04
The function has been defined to allow for many different models of
Packit 7cfc04
using shared memory. However, all uses are not equally portable across
Packit 7cfc04
all machine architectures. In particular, the
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function allows the system as well as the application to specify the
Packit 7cfc04
address at which to map a specific region of a memory object. The most
Packit 7cfc04
portable way to use the function is always to let the system choose
Packit 7cfc04
the address, specifying NULL as the value for the argument
Packit 7cfc04
.IR addr
Packit 7cfc04
and not to specify MAP_FIXED.
Packit 7cfc04
.P
Packit 7cfc04
If it is intended that a particular region of a memory object be mapped
Packit 7cfc04
at the same address in a group of processes (on machines where this is
Packit 7cfc04
even possible), then MAP_FIXED can be used to pass in the desired
Packit 7cfc04
mapping address. The system can still be used to choose the desired
Packit 7cfc04
address if the first such mapping is made without specifying MAP_FIXED,
Packit 7cfc04
and then the resulting mapping address can be passed to subsequent
Packit 7cfc04
processes for them to pass in via MAP_FIXED. The availability of a
Packit 7cfc04
specific address range cannot be guaranteed, in general.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
function can be used to map a region of memory that is larger than the
Packit 7cfc04
current size of the object. Memory access within the mapping but
Packit 7cfc04
beyond the current end of the underlying objects may result in SIGBUS
Packit 7cfc04
signals being sent to the process. The reason for this is that the
Packit 7cfc04
size of the object can be manipulated by other processes and can change
Packit 7cfc04
at any moment. The implementation should tell the application that a
Packit 7cfc04
memory reference is outside the object where this can be detected;
Packit 7cfc04
otherwise, written data may be lost and read data may not reflect
Packit 7cfc04
actual data in the object.
Packit 7cfc04
.P
Packit 7cfc04
Note that references beyond the end of the object do not extend the
Packit 7cfc04
object as the new end cannot be determined precisely by most virtual
Packit 7cfc04
memory hardware. Instead, the size can be directly manipulated by
Packit 7cfc04
\fIftruncate\fR().
Packit 7cfc04
.P
Packit 7cfc04
Process memory locking does apply to shared memory regions, and the
Packit 7cfc04
MEMLOCK_FUTURE argument to
Packit 7cfc04
\fImlockall\fR()
Packit 7cfc04
can be relied upon to cause new shared memory regions to be
Packit 7cfc04
automatically locked.
Packit 7cfc04
.P
Packit 7cfc04
Existing implementations of
Packit 7cfc04
\fImmap\fR()
Packit 7cfc04
return the value \(mi1 when unsuccessful. Since the casting of this
Packit 7cfc04
value to type
Packit 7cfc04
.BR "void *"
Packit 7cfc04
cannot be guaranteed by the ISO\ C standard to be distinct from a successful
Packit 7cfc04
value, this volume of POSIX.1\(hy2008 defines the symbol MAP_FAILED,
Packit 7cfc04
which a conforming implementation does not return as the result of a
Packit 7cfc04
successful call.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIexec\fR\^",
Packit 7cfc04
.IR "\fIfcntl\fR\^(\|)",
Packit 7cfc04
.IR "\fIfork\fR\^(\|)",
Packit 7cfc04
.IR "\fIlockf\fR\^(\|)",
Packit 7cfc04
.IR "\fImsync\fR\^(\|)",
Packit 7cfc04
.IR "\fImunmap\fR\^(\|)",
Packit 7cfc04
.IR "\fImprotect\fR\^(\|)",
Packit 7cfc04
.IR "\fIposix_typed_mem_open\fR\^(\|)",
Packit 7cfc04
.IR "\fIshmat\fR\^(\|)",
Packit 7cfc04
.IR "\fIsysconf\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fB<sys_mman.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .