1MMAP(2)                    Linux Programmer's Manual                   MMAP(2)
2
3
4

NAME

6       mmap, munmap - map or unmap files or devices into memory
7

SYNOPSIS

9       #include <sys/mman.h>
10
11       void *mmap(void *start, size_t length, int prot, int flags,
12                  int fd, off_t offset);
13
14       int munmap(void *start, size_t length);
15

DESCRIPTION

17       mmap()  creates a new mapping in the virtual address space of the call‐
18       ing process.  The starting address for the new mapping is specified  in
19       start.  The length argument specifies the length of the mapping.
20
21       If  start is NULL, then the kernel chooses the address at which to cre‐
22       ate the mapping; this is the most portable method  of  creating  a  new
23       mapping.   If  start  is  not  NULL, then the kernel takes it as a hint
24       about where to place the mapping; on Linux, the mapping will be created
25       at  the  next  higher page boundary.  The address of the new mapping is
26       returned as the result of the call.
27
28       The contents of a file mapping (as opposed to an anonymous mapping; see
29       MAP_ANONYMOUS  below),  are  initialised using length bytes starting at
30       offset offset in the file (or other object) referred  to  by  the  file
31       descriptor  fd.  offset must be a multiple of the page size as returned
32       by sysconf(_SC_PAGE_SIZE).
33
34       The prot argument describes the desired memory protection of  the  map‐
35       ping  (and  must  not  conflict with the open mode of the file).  It is
36       either PROT_NONE or the bitwise OR of one  or  more  of  the  following
37       flags:
38
39       PROT_EXEC  Pages may be executed.
40
41       PROT_READ  Pages may be read.
42
43       PROT_WRITE Pages may be written.
44
45       PROT_NONE  Pages may not be accessed.
46
47       The  flags argument determines whether updates to the mapping are visi‐
48       ble to other processes mapping the same region, and whether updates are
49       caried through to the underlying file.  This behaviour is determined by
50       including exactly one of the following values in flags:
51
52       MAP_SHARED Share this mapping.  Updates to the mapping are  visible  to
53                  other  processes that map this file, and are carried through
54                  to the underlying  file.   The  file  may  not  actually  be
55                  updated until msync(2) or munmap(2) is called.
56
57       MAP_PRIVATE
58                  Create a private copy-on-write mapping.  Updates to the map‐
59                  ping are not visible to other  processes  mapping  the  same
60                  file,  and  are  not carried through to the underlying file.
61                  It is unspecified whether changes made to the file after the
62                  mmap() call are visible in the mapped region.
63
64       Both of these flags are described in POSIX.1-2001.
65
66       In addition, zero or more of the following values can be ORed in flags:
67
68       MAP_FIXED
69              Don't  interpret  start  as a hint: place the mapping at exactly
70              that address.  start must be a multiple of the  page  size.   If
71              the  memory  region specified by start and len overlaps pages of
72              any existing mapping(s), then the overlapped part of the  exist‐
73              ing mapping(s) will be discarded.  If the specified address can‐
74              not be used,  mmap()  will  fail.   Because  requiring  a  fixed
75              address  for  a mapping is less portable, the use of this option
76              is discouraged.
77
78       MAP_DENYWRITE
79              This flag is ignored.  (Long ago, it signalled that attempts  to
80              write  to  the  underlying  file should fail with ETXTBUSY.  But
81              this was a source of denial-of-service attacks.)
82
83       MAP_EXECUTABLE
84              This flag is ignored.
85
86       MAP_NORESERVE
87              Do not reserve swap space for this mapping.  When swap space  is
88              reserved,  one  has  the guarantee that it is possible to modify
89              the mapping.  When swap space is  not  reserved  one  might  get
90              SIGSEGV  upon  a  write if no physical memory is available.  See
91              also the discussion of the  file  /proc/sys/vm/overcommit_memory
92              in  proc(5).   In  kernels before 2.6, this flag only had effect
93              for private writable mappings.
94
95       MAP_LOCKED (since Linux 2.5.37)
96              Lock the pages of the mapped region into memory in the manner of
97              mlock().  This flag is ignored in older kernels.
98
99       MAP_GROWSDOWN
100              Used  for stacks.  Indicates to the kernel virtual memory system
101              that the mapping should extend downwards in memory.
102
103       MAP_ANON
104              Synonym for MAP_ANONYMOUS.  Deprecated.
105
106       MAP_ANONYMOUS
107              The mapping is not backed by any file;  its  contents  are  ini‐
108              tialised to zero.  The fd and offset arguments are ignored; how‐
109              ever, some implementations require fd to be -1 if  MAP_ANONYMOUS
110              (or  MAP_ANON)  is  specified,  and portable applications should
111              ensure this.  The  use  of  MAP_ANONYMOUS  in  conjunction  with
112              MAP_SHARED is only supported on Linux since kernel 2.4.
113
114       MAP_FILE
115              Compatibility flag. Ignored.
116
117       MAP_32BIT
118              Put the mapping into the first 2GB of the process address space.
119              Ignored when MAP_FIXED is set.  This flag is currently only sup‐
120              ported on x86-64 for 64bit programs.
121
122       MAP_POPULATE (since Linux 2.5.46)
123              Populate  (prefault) page tables for a file mapping, by perform‐
124              ing read-ahead on the file.  Later accesses to the mapping  will
125              not be blocked by page faults.
126
127       MAP_NONBLOCK (since Linux 2.5.46)
128              Only meaningful in conjunction with MAP_POPULATE.  Don't perform
129              read-ahead: only create page tables entries for pages  that  are
130              already present in RAM.
131
132       Of  the above flags, only MAP_FIXED is specified in POSIX.1-2001.  How‐
133       ever,  most  systems  also  support  MAP_ANONYMOUS  (or   its   synonym
134       MAP_ANON).
135
136       Some systems document the additional flags MAP_AUTOGROW, MAP_AUTORESRV,
137       MAP_COPY, and MAP_LOCAL.
138
139       Memory mapped by mmap() is preserved  across  fork(2),  with  the  same
140       attributes.
141
142       A  file is mapped in multiples of the page size. For a file that is not
143       a multiple of the page  size,  the  remaining  memory  is  zeroed  when
144       mapped,  and writes to that region are not written out to the file. The
145       effect of changing the size of the underlying file of a mapping on  the
146       pages  that  correspond  to  added  or  removed  regions of the file is
147       unspecified.
148
149       The munmap() system call deletes the mappings for the specified address
150       range,  and  causes further references to addresses within the range to
151       generate invalid memory references.  The region is  also  automatically
152       unmapped  when  the  process is terminated.  On the other hand, closing
153       the file descriptor does not unmap the region.
154
155       If MAP_FIXED is specified, start must be a multiple of the  page  size.
156       In  all  other  cases start address is rounded up to the next page size
157       boundary.  All pages containing a  part  of  the  indicated  range  are
158       unmapped,  and  subsequent  references  to  these  pages  will generate
159       SIGSEGV. It is not an error if the indicated range does not contain any
160       mapped pages.
161
162       For file-backed mappings, the st_atime field for the mapped file may be
163       updated at any time between the mmap() and the corresponding unmapping;
164       the  first  reference  to a mapped page will update the field if it has
165       not been already.
166
167       The st_ctime and st_mtime field for a file mapped with  PROT_WRITE  and
168       MAP_SHARED  will  be  updated  after  a write to the mapped region, and
169       before a subsequent msync() with the MS_SYNC or MS_ASYNC flag,  if  one
170       occurs.
171

RETURN VALUE

173       On success, mmap() returns a pointer to the mapped area.  On error, the
174       value MAP_FAILED (that is, (void *) -1) is returned, and errno  is  set
175       appropriately.   On  success,  munmap()  returns  0, on failure -1, and
176       errno is set (probably to EINVAL).
177

NOTES

179       It is architecture dependent whether  PROT_READ  implies  PROT_EXEC  or
180       not.   Portable  programs should always set PROT_EXEC if they intend to
181       execute code in the new mapping.
182

ERRORS

184       EACCES A file descriptor refers to a non-regular file.  Or  MAP_PRIVATE
185              was  requested,  but  fd is not open for reading.  Or MAP_SHARED
186              was requested and PROT_WRITE is set,  but  fd  is  not  open  in
187              read/write (O_RDWR) mode.  Or PROT_WRITE is set, but the file is
188              append-only.
189
190       EAGAIN The file has been locked, or too much  memory  has  been  locked
191              (see setrlimit(2)).
192
193       EBADF  fd  is  not  a  valid file descriptor (and MAP_ANONYMOUS was not
194              set).
195
196       EINVAL We don't like start, length,  or  offset  (e.g.,  they  are  too
197              large, or not aligned on a page boundary).
198
199       EINVAL (since Linux 2.6.12), length was 0.
200
201       EINVAL flags  contained neither MAP_PRIVATE or MAP_SHARED, or contained
202              both of these values.
203
204       ENFILE The system limit on the total number  of  open  files  has  been
205              reached.
206
207       ENODEV The underlying filesystem of the specified file does not support
208              memory mapping.
209
210       ENOMEM No memory is available, or the process's maximum number of  map‐
211              pings would have been exceeded.
212
213       EPERM  The prot argument asks for PROT_EXEC but the mapped area belongs
214              to a file on a filesystem that was mounted no-exec.
215
216       ETXTBSY
217              MAP_DENYWRITE was set but the object specified by fd is open for
218              writing.
219
220       Use of a mapped region can result in these signals:
221
222       SIGSEGV
223              Attempted write into a region mapped as read-only.
224
225       SIGBUS Attempted access to a portion of the buffer that does not corre‐
226              spond to the file (for example, beyond  the  end  of  the  file,
227              including  the  case  where  another  process  has truncated the
228              file).
229

AVAILABILITY

231       On POSIX systems on which mmap(), msync() and munmap()  are  available,
232       _POSIX_MAPPED_FILES is defined in <unistd.h> to a value greater than 0.
233       (See also sysconf(3).)
234

CONFORMING TO

236       SVr4, 4.4BSD, POSIX.1-2001.
237

BUGS

239       On Linux there are no  guarantees  like  those  suggested  above  under
240       MAP_NORESERVE.   By  default,  any  process can be killed at any moment
241       when the system runs out of memory.
242
243       In kernels before 2.6.7, the MAP_POPULATE flag only has effect if  prot
244       is specified as PROT_NONE.
245
246       SUSv3  specifies  that  mmap() should fail if length is 0.  However, in
247       kernels before 2.6.12, mmap() succeeded in this case:  no  mapping  was
248       created and the call returned start.  Since kernel 2.6.12, mmap() fails
249       with the error EINVAL for this case.
250

SEE ALSO

252       getpagesize(2), mincore(2), mlock(2),  mmap2(2),  mremap(2),  msync(2),
253       remap_file_pages(2), setrlimit(2), shm_open(3)
254       B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.
255
256
257
258Linux 2.6.19                      2006-12-04                           MMAP(2)
Impressum