]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mmap.2
ffix
[thirdparty/man-pages.git] / man2 / mmap.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (C) 1996 Andries Brouwer (aeb@cwi.nl)
4.\"
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
13.\"
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
21.\"
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
24.\"
25.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
26.\" Modified 2000-03-25 by Jim Van Zandt <jrv@vanzandt.mv.com>
27.\" Modified 2001-10-04 by John Levon <moz@compsoc.man.ac.uk>
28.\" Modified 2003-02-02 by Andi Kleen <ak@muc.de>
305a0578 29.\" Modified 2003-05-21 by Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 30.\" MAP_LOCKED works from 2.5.37
305a0578 31.\" Modified 2004-06-17 by Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 32.\" Modified 2004-09-11 by aeb
1a956089
MK
33.\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr>
34.\" Modified 2004-12-08, mtk, formatting tidy-ups
fea681da 35.\"
1a956089 36.TH MMAP 2 2004-12-08 "Linux 2.6.9" "Linux Programmer's Manual"
fea681da
MK
37.SH NAME
38mmap, munmap \- map or unmap files or devices into memory
39.SH SYNOPSIS
e0037472 40.nf
fea681da
MK
41.B #include <sys/mman.h>
42.sp
e0037472
MK
43.BI "void *mmap(void *" start ", size_t " length ", int " prot ", int " flags ,
44.BI " int " fd ", off_t " offset );
fea681da
MK
45.sp
46.BI "int munmap(void *" start ", size_t " length );
e0037472 47.fi
fea681da
MK
48.SH DESCRIPTION
49The
1a956089 50.BR mmap ()
fea681da
MK
51function asks to map
52.I length
53bytes starting at offset
54.I offset
55from the file (or other object) specified by the file descriptor
56.I fd
57into memory, preferably at address
58.IR start .
59This latter address is a hint only, and is usually specified as 0.
60The actual place where the object is mapped is returned by
1a956089 61.BR mmap (),
fea681da
MK
62and is never 0.
63.LP
64The
65.I prot
66argument describes the desired memory protection (and must not
67conflict with the open mode of the file). It is either
68.B PROT_NONE
69or is the bitwise OR of one or more of the other PROT_* flags.
70.TP 1.1i
71.B PROT_EXEC
72Pages may be executed.
73.TP
74.B PROT_READ
75Pages may be read.
76.TP
77.B PROT_WRITE
78Pages may be written.
79.TP
80.B PROT_NONE
81Pages may not be accessed.
82.LP
83The
84.I flags
85parameter specifies the type of the mapped object, mapping options and
86whether modifications made to the mapped copy of the page are private to
87the process or are to be shared with other references. It has bits
88.TP 1.1i
89.B MAP_FIXED
90Do not select a different address than the one specified.
1a956089 91If the memory region specified by
8478ee02 92.I start
1a956089 93and
8478ee02 94.I len
1a956089
MK
95overlaps pages of any existing mapping(s), then the overlapped
96part of the existing mapping(s) will be discarded.
fea681da 97If the specified address cannot be used,
1a956089
MK
98.BR mmap ()
99will fail.
100If MAP_FIXED is specified,
fea681da
MK
101.I start
102must be a multiple of the pagesize. Use of this option is discouraged.
103.TP
104.B MAP_SHARED
105Share this mapping with all other processes that map this object.
106Storing to the region is equivalent to writing to the file.
107The file may not actually be updated until
108.BR msync (2)
109or
110.BR munmap (2)
111are called.
112.TP
113.B MAP_PRIVATE
114Create a private copy-on-write mapping.
115Stores to the region do not affect the original file.
116It is unspecified whether changes made to the file after the
1a956089 117.BR mmap ()
fea681da
MK
118call are visible in the mapped region.
119.LP
120You must specify exactly one of MAP_SHARED and MAP_PRIVATE.
121.LP
122The above three flags are described in POSIX.1b (formerly POSIX.4) and SUSv2.
123Linux also knows about the following non-standard flags:
124.TP
125.B MAP_DENYWRITE
126This flag is ignored.
127.\" Introduced in 1.1.36, removed in 1.3.24.
128(Long ago, it signalled that attempts to write to the underlying file
129should fail with ETXTBUSY. But this was a source of denial-of-service attacks.)
130.TP
131.B MAP_EXECUTABLE
132This flag is ignored.
133.\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link.
134.\" (Long ago, it signalled that the underlying file is an executable.
135.\" However, that information was not really used anywhere.)
136.\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of
137.\" MAP_DENYWRITE?
138.TP
139.B MAP_NORESERVE
4006dcac
MK
140Do not reserve swap space for this mapping.
141When swap space is reserved, one has the guarantee
142that it is possible to modify the mapping.
143When swap space is not reserved one might get SIGSEGV upon a write
144if no physical memory is available.
145See also the discussion of the file
146.I /proc/sys/vm/overcommit_memory
147in
148.BR proc (5).
149In kernels before 2.6, this flag only had effect for
150private writable mappings.
fea681da
MK
151.TP
152.BR MAP_LOCKED " (since Linux 2.5.37)"
153Lock the pages of the mapped region into memory in the manner of
1a956089 154.BR mlock () .
fea681da
MK
155This flag is ignored in older kernels.
156.\" If set, the mapped pages will not be swapped out.
157.TP
158.B MAP_GROWSDOWN
159Used for stacks. Indicates to the kernel VM system that the mapping
160should extend downwards in memory.
161.TP
162.B MAP_ANONYMOUS
163The mapping is not backed by any file; the
164.I fd
165and
166.I offset
74f3f90b
MK
167arguments are ignored.
168The use of this flag in conjunction with MAP_SHARED
169is only supported on Linux since kernel 2.4.
fea681da
MK
170.TP
171.B MAP_ANON
172Alias for MAP_ANONYMOUS. Deprecated.
173.TP
174.B MAP_FILE
175Compatibility flag. Ignored.
176.TP
177.B MAP_32BIT
178Put the mapping into the first 2GB of the process address space.
179Ignored when
180.I MAP_FIXED
181is set. This flag is currently only supported on x86-64 for 64bit programs.
182.TP
183.BR MAP_POPULATE " (since Linux 2.5.46)"
184Populate (prefault) pagetables.
185.TP
186.BR MAP_NONBLOCK " (since Linux 2.5.46)"
a28e26a1 187Do not block on I/O.
fea681da
MK
188.LP
189Some systems document the additional flags MAP_AUTOGROW, MAP_AUTORESRV,
190MAP_COPY, and MAP_LOCAL.
191.LP
192.I fd
74f3f90b
MK
193should be a valid file descriptor, unless MAP_ANONYMOUS is set.
194If MAP_ANONYMOUS is set, then
195.I fd
196is ignored on Linux.
197However, some implementations require
198.I fd
199to be \-1 if MAP_ANONYMOUS (or MAP_ANON) is specified,
200and portable applications should ensure this.
fea681da
MK
201.LP
202.I offset
203should be a multiple of the page size as returned by
204.BR getpagesize (2).
205.LP
206Memory mapped by
1a956089 207.BR mmap ()
fea681da
MK
208is preserved across
209.BR fork (2),
210with the same attributes.
211.LP
212A file is mapped in multiples of the page size. For a file that is not
213a multiple of the page size, the remaining memory is zeroed when mapped,
214and writes to that region are not written out to the file. The effect of
215changing the size of the underlying file of a mapping on the pages that
216correspond to added or removed regions of the file is unspecified.
217
218The
1a956089 219.BR munmap ()
fea681da
MK
220system call deletes the mappings for the specified address range, and
221causes further references to addresses within the range to generate
222invalid memory references. The region is also automatically unmapped
223when the process is terminated. On the other hand, closing the file
224descriptor does not unmap the region.
225.LP
226The address
227.I start
228must be a multiple of the page size. All pages containing a part
229of the indicated range are unmapped, and subsequent references
230to these pages will generate SIGSEGV. It is not an error if the
231indicated range does not contain any mapped pages.
232
233For file-backed mappings, the
8478ee02 234.I st_atime
fea681da 235field for the mapped file may be updated at any time between the
1a956089 236.BR mmap ()
fea681da
MK
237and the corresponding unmapping; the first reference to a mapped
238page will update the field if it has not been already.
239.LP
240The
8478ee02 241.I st_ctime
fea681da 242and
8478ee02 243.I st_mtime
fea681da
MK
244field for a file mapped with PROT_WRITE and MAP_SHARED will be updated after
245a write to the mapped region, and before a subsequent
1a956089 246.BR msync ()
fea681da
MK
247with the MS_SYNC or MS_ASYNC flag, if one occurs.
248.SH "RETURN VALUE"
249On success,
1a956089 250.BR mmap ()
fea681da
MK
251returns a pointer to the mapped area.
252On error, the value
253.B MAP_FAILED
254(that is, (void *) \-1) is returned, and
255.I errno
256is set appropriately.
257On success,
1a956089 258.BR munmap ()
fea681da
MK
259returns 0, on failure \-1, and
260.I errno
261is set (probably to EINVAL).
262.SH NOTES
263It is architecture dependent whether
264.I PROT_READ
265includes
266.I PROT_EXEC
267or not. Portable programs should always set
268.I PROT_EXEC
269if they intend to execute code in the new mapping.
270.SH ERRORS
271.TP
272.B EACCES
273A file descriptor refers to a non-regular file.
274Or MAP_PRIVATE was requested, but
275.I fd
276is not open for reading.
277Or MAP_SHARED was requested and PROT_WRITE is set, but
278.I fd
279is not open in read/write (O_RDWR) mode.
280Or PROT_WRITE is set, but the file is append-only.
281.TP
282.B EAGAIN
83cd3686
MK
283The file has been locked, or too much memory has been locked (see
284.BR setrlimit (2)).
fea681da
MK
285.TP
286.B EBADF
287.I fd
288is not a valid file descriptor (and MAP_ANONYMOUS was not set).
289.TP
290.B EINVAL
291We don't like
292.I start
293or
294.I length
295or
296.IR offset .
297(E.g., they are too large, or not aligned on a PAGESIZE boundary.)
298.\" jbl - not sure this actually happens ? see generic_file_mmap
24b1a012
MK
299.\" mtk: Before 2.6.12, a length of 0 was permitted: mmap() did
300.\" not create mapping, but just returned 'start'; since 2.6.12,
301.\" a length of 0 yields EINVAL (as required by SUSv3).
fea681da
MK
302.TP
303.B ENFILE
304.\" This is for shared anonymous segments
305.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
306The system limit on the total number of open files has been reached.
307.\" .TP
308.\" .B ENOEXEC
309.\" A file could not be mapped for reading.
310.TP
311.B ENODEV
312The underlying filesystem of the specified file does not support
313memory mapping.
314.TP
315.B ENOMEM
316No memory is available, or the process's maximum number of mappings would
317have been exceeded.
318.TP
319.B EPERM
320The
321.I prot
322argument asks for
323.B PROT_EXEC
324but the mapped area belongs to a file on a filesystem that
325was mounted no-exec.
326.\" (Since 2.4.25 / 2.6.0.)
327.TP
328.B ETXTBSY
329MAP_DENYWRITE was set but the object specified by
330.I fd
331is open for writing.
332.LP
333Use of a mapped region can result in these signals:
334.TP
335.B SIGSEGV
1e321034 336Attempted write into a region mapped as read-only.
fea681da
MK
337.TP
338.B SIGBUS
339Attempted access to a portion of the buffer that does not correspond
340to the file (for example, beyond the end of the file, including the
341case where another process has truncated the file).
342.SH AVAILABILITY
343On POSIX systems on which
1a956089
MK
344.BR mmap (),
345.BR msync ()
fea681da 346and
1a956089 347.BR munmap ()
fea681da
MK
348are available,
349.B _POSIX_MAPPED_FILES
350is defined in <unistd.h> to a value greater than 0. (See also
351.BR sysconf (3).)
352.\" POSIX 1003.1-2001: It shall be defined to -1 or 0 or 200112L.
353.\" -1: unavailable, 0: ask using sysconf().
354.\" glibc defines it to 1.
355.SH "CONFORMING TO"
356SVr4, POSIX.1b (formerly POSIX.4), 4.4BSD, SUSv2.
357SVr4 documents additional error codes ENXIO and ENODEV.
358SUSv2 documents additional error codes EMFILE and EOVERFLOW.
359.SH BUGS
dbc53ca8
MK
360On Linux there are no guarantees like those suggested above under
361.BR MAP_NORESERVE .
362By default, any process can be killed
fea681da 363at any moment when the system runs out of memory.
dbc53ca8
MK
364
365In kernels before 2.6.7, the
366.B MAP_POPULATE
367flag only has effect if
368.I prot
369is specified as
370.BR PROT_NONE .
fea681da
MK
371.SH "SEE ALSO"
372.BR getpagesize (2),
f75c3a3b 373.BR mincore (2),
fea681da
MK
374.BR mlock (2),
375.BR mmap2 (2),
376.BR mremap (2),
377.BR msync (2),
931e4e25 378.BR remap_file_pages (2),
83cd3686 379.BR setrlimit (2),
424a8950 380.BR shm_open (3)
fea681da
MK
381.br
382B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.
383.\"
384.\" Repeat after me: private read-only mappings are 100% equivalent to
385.\" shared read-only mappings. No ifs, buts, or maybes. -- Linus