home | help
mmap(2) 		       System Calls Manual			 mmap(2)

NAME
     mmap - map pages of memory

SYNOPSIS
     #include <sys/mman.h>

     void *mmap(void *addr, size_t len, int prot, int flags,
	  int fildes, off_t off);

DESCRIPTION
     The mmap() function establishes a mapping between a process's address space
     and a virtual memory object.  The format of the call is as follows:

	    pa = mmap(addr, len, prot, flags, fildes, off);

     at an address pa for len bytes to the memory object represented by the file
     descriptor  fildes  at offset off for len bytes.  The value of pa is an im-
     plementation-dependent function of the addr argument and values  of  flags,
     described	below.	 A successful mmap() call returns pa as its result.  The
     address ranges covered by [pa, pa + len) and [off, off + len) must  be  le-
     gitimate  for  the  possible  (not  necessarily current) address space of a
     process and the object in question, respectively.

     The mmap() function allows [pa, pa + len) to extend beyond the end  of  the
     object, both at the time of the mmap() and while the mapping persists, such
     as when the file was created just before the mmap() and has no contents, or
     if the file is truncated.	Any reference to addresses beyond the end of the
     object, however, will result in the delivery of a SIGBUS or SIGSEGV signal.
     In  other	words,	mmap() cannot be used to implicitly extend the length of
     files.

     The mapping established by mmap() replaces any previous  mappings	for  the
     process's pages in the range [pa, pa + len).

     Mappings  established  from  fildes are not removed upon a close(2) of that
     descriptor.  Use munmap(2) to remove a mapping.

     The prot argument determines whether read, write, execute, or some combina-
     tion of accesses are permitted to the pages being	mapped.  The  protection
     options are defined in <sys/mman.h> as:

	    PROT_READ		     Page can be read.
	    PROT_WRITE		     Page can be written.
	    PROT_EXEC		     Page can be executed.
	    PROT_NONE		     Page can not be accessed.

     Not  all  implementations literally provide all possible combinations.  The
     PROT_WRITE protection option is often implemented	as  PROT_READ|PROT_WRITE
     and PROT_EXEC as PROT_READ|PROT_EXEC.  However, no implementation will per-
     mit  a write to succeed where PROT_WRITE has not been set.  The behavior of
     PROT_WRITE can be influenced by setting MAP_PRIVATE in the flags  argument,
     described below.

     The  flags  argument  provides  other information about the handling of the
     mapped pages.  The options are defined in <sys/mman.h> as:

	    MAP_SHARED		     Share changes.
	    MAP_PRIVATE 	     Changes are private.
	    MAP_FIXED		     Interpret addr exactly.
	    MAP_NORESERVE	     Don't reserve swap space.

     The MAP_SHARED and MAP_PRIVATE options describe the  disposition  of  write
     references  to the memory object.	If MAP_SHARED is specified, write refer-
     ences will change the memory object.  If MAP_PRIVATE is specified, the ini-
     tial write reference will create a private copy of the memory  object  page
     and  redirect the mapping to the copy.  The private copy is not created un-
     til the first write; until then, other users who  have  the  object  mapped
     MAP_SHARED can change the object.	Either MAP_SHARED or MAP_PRIVATE must be
     specified, but not both.  The mapping type is retained across a fork(2).

     The  MAP_FIXED option informs the system that the value of pa must be addr,
     exactly.  The use of MAP_FIXED is discouraged, as it may prevent an  imple-
     mentation from making the most effective use of system resources.

     When  MAP_FIXED  is  set  and the requested address is the same as previous
     mapping, the previous address is unmapped and the new mapping is created on
     top of the old one.

     When MAP_FIXED is not set, the system uses addr  in  an  implementation-de-
     fined  manner to arrive at pa.  The pa so chosen will be an area of the ad-
     dress space which the system deems suitable for a mapping of len  bytes  to
     the  specified object.  All implementations interpret an addr value of zero
     as granting the system complete freedom in selecting pa,  subject	to  con-
     straints  described  below.  A non-zero value of addr is taken to be a sug-
     gestion of a process address near which the mapping should be placed.  When
     the system selects a value for pa, it will never place a mapping at address
     0, nor will it replace any extant mapping, nor map  into  areas  considered
     part of the potential data or stack "segments".

     The  MAP_NORESERVE  option  specifies  that no swap space be reserved for a
     mapping.  Without this flag, the creation of a writable MAP_PRIVATE mapping
     reserves swap space equal to the size of the mapping; when the  mapping  is
     written  into, the reserved space is employed to hold private copies of the
     data.  A write into a MAP_NORESERVE mapping produces results  which  depend
     on  the  current  availability  of  swap  space in the system.  If space is
     available, the write succeeds and a private copy of  the  written	page  is
     created; if space is not available, the write fails and a SIGBUS or SIGSEGV
     signal is delivered to the writing process.  MAP_NORESERVE mappings are in-
     herited across fork(); at the time of the fork(), swap space is reserved in
     the  child for all private pages that currently exist in the parent; there-
     after the child's mapping behaves as described above.

     The off argument is constrained to be aligned and sized  according  to  the
     value  returned  by  sysconf(3C) when passed _SC_PAGESIZE or _SC_PAGE_SIZE.
     When MAP_FIXED is specified, the addr argument must also  meet  these  con-
     straints.	 The system performs mapping operations over whole pages.  Thus,
     while the len argument *need not meet a size or alignment	constraint,  the
     system  will  include, in any mapping operation, any partial page specified
     by the range [pa, pa + len).

     The system will always zero-fill any partial page at the end of an  object.
     Further,  the system will never write out any modified portions of the last
     page of an object which are beyond its end.  References to whole pages fol-
     lowing the end of an object will result in the  delivery  of  a  SIGBUS  or
     SIGSEGV  signal.  SIGBUS signals may also be delivered on various file sys-
     tem conditions, including quota exceeded errors.

     The mmap() function adds an extra reference to the file associated with the
     file descriptor fildes which is not removed by  a	subsequent  close(2)  on
     that  file  descriptor.   This  reference is removed when there are no more
     mappings to the file.

     The st_atime field of the mapped file may be marked for update at any  time
     between  the mmap() call and the corresponding munmap(2) call.  The initial
     read or write reference to a mapped region will cause the	file's	st_atime
     field to be marked for update if it has not already been marked for update.

     The  st_ctime  and st_mtime fields of a file that is mapped with MAP_SHARED
     and PROT_WRITE, will be marked for update at some point in the interval be-
     tween a write reference to the mapped region and the next call to msync(3C)
     with MS_ASYNC or MS_SYNC for that portion of the file by any  process.   If
     there  is	no  such call, these fields may be marked for update at any time
     after a write reference if the underlying file is modified as a result.

     If the process calls mlockall(3C)	with  the  MCL_FUTURE  flag,  the  pages
     mapped  by  all  future  calls to mmap() will be locked in memory.  In this
     case, if not enough memory could be locked, mmap() fails and sets errno  to
     EAGAIN.

RETURN VALUES
     On  success,  mmap()  returns  the  address at which the mapping was placed
     (pa).  On failure it returns MAP_FAILED and sets errno to indicate  an  er-
     ror.

ERRORS
     The mmap() function will fail if:

     EACCES	    The  fildes file descriptor is not open for read, regardless
		    of the protection specified, or fildes is not open for write
		    and PROT_WRITE was specified for a MAP_SHARED type mapping.

     EAGAIN	    The mapping could not be locked in memory.

		    There was insufficient room to reserve swap  space	for  the
		    mapping.

		    The  file  to  be mapped is already locked using advisory or
		    mandatory record locking.  See fcntl(2).

     EBADF	    The fildes file descriptor is not open.

     EINVAL	    The arguments addr (if MAP_FIXED was specified) or	off  are
		    not multiples of the page size as returned by sysconf().

		    The  field	in  flags  is  invalid	(neither  MAP_PRIVATE or
		    MAP_SHARED).

		    The argument len has a value less than or equal to 0.

     EMFILE	    The number of mapped regions would exceed an implementation-
		    dependent limit (per process or per system).

     ENODEV	    The fildes argument refers to an object for which mmap()  is
		    meaningless, such as a terminal.

     ENOMEM	    The MAP_FIXED option was specified and the range [addr, addr
		    +  len)  exceeds  that  allowed  for  the address space of a
		    process.

		    The MAP_FIXED option was not specified and there is insuffi-
		    cient room in the address space to effect the mapping.

		    The composite size of len plus the lengths of  all	previous
		    mmappings exceeds RLIMIT_VMEM (see getrlimit(2)).

     ENXIO	    The  range	[off, off + len) is illegal for mmapping to this
		    device.

     EOVERFLOW	    The file is a regular file and the value of off plus len ex-
		    ceeds the offset maximum establish in the open file descrip-
		    tion associated with fildes.

USAGE
     The mmap() function allows access to resources using address space  manipu-
     lations  instead of the read/write interface.  Once a file is mapped, all a
     process has to do to access it is use the data at the address to which  the
     object was mapped.
     Consider the following pseudo-code:

	    fildes = open(...)
	    lseek(fildes, offset, whence)
	    read(fildes, buf, len)
	    /* use data in buf */

     The following is a rewrite using mmap():

	    fildes = open(...)
	    address = mmap((caddr_t) 0, len, (PROT_READ | PROT_WRITE),
			   MAP_PRIVATE, fildes, offset)
	    /* use data at address */

     The mmap() function has an explicit 64-bit equivalent.  See interface64(5).

SEE ALSO
     close(2), exec(2), fcntl(2), fork(2), getrlimit(2), mprotect(2), munmap(2),
     shmat(2),	lockf(3C),  mlockall(3C), msync(3C), plock(3C), sysconf(3C), in-
     terface64(5)

				   16 Jun 1997				 mmap(2)

home | help