]>
Commit | Line | Data |
---|---|---|
a62f5121 | 1 | .\" Copyright (C) 1996 Andries Brouwer <aeb@cwi.nl> |
c11b1abf | 2 | .\" and Copyright (C) 2006, 2007 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 3 | .\" |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
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. | |
c13182ef | 13 | .\" |
fea681da MK |
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. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
fea681da MK |
25 | .\" |
26 | .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com> | |
27 | .\" Modified 2000-03-25 by Jim Van Zandt <jrv@vanzandt.mv.com> | |
28 | .\" Modified 2001-10-04 by John Levon <moz@compsoc.man.ac.uk> | |
29 | .\" Modified 2003-02-02 by Andi Kleen <ak@muc.de> | |
c11b1abf | 30 | .\" Modified 2003-05-21 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 31 | .\" MAP_LOCKED works from 2.5.37 |
c11b1abf | 32 | .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 33 | .\" Modified 2004-09-11 by aeb |
1a956089 MK |
34 | .\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr> |
35 | .\" Modified 2004-12-08, mtk, formatting tidy-ups | |
a62f5121 | 36 | .\" Modified 2006-12-04, mtk, various parts rewritten |
74fa61b7 | 37 | .\" 2007-07-10, mtk, Added an example program. |
e6205b0c | 38 | .\" 2008-11-18, mtk, document MAP_STACK |
fea681da | 39 | .\" |
4b8c67d9 | 40 | .TH MMAP 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
fea681da MK |
41 | .SH NAME |
42 | mmap, munmap \- map or unmap files or devices into memory | |
43 | .SH SYNOPSIS | |
e0037472 | 44 | .nf |
fea681da | 45 | .B #include <sys/mman.h> |
68e4db0a | 46 | .PP |
14f5ae6d | 47 | .BI "void *mmap(void *" addr ", size_t " length \ |
a62f5121 | 48 | ", int " prot ", int " flags , |
e0037472 | 49 | .BI " int " fd ", off_t " offset ); |
14f5ae6d | 50 | .BI "int munmap(void *" addr ", size_t " length ); |
e0037472 | 51 | .fi |
dbfe9c70 | 52 | .PP |
45e97e2a | 53 | See NOTES for information on feature test macro requirements. |
fea681da | 54 | .SH DESCRIPTION |
1a956089 | 55 | .BR mmap () |
c13182ef | 56 | creates a new mapping in the virtual address space of |
5e8cde2f MK |
57 | the calling process. |
58 | The starting address for the new mapping is specified in | |
14f5ae6d | 59 | .IR addr . |
5e8cde2f MK |
60 | The |
61 | .I length | |
62 | argument specifies the length of the mapping. | |
efeece04 | 63 | .PP |
5e8cde2f | 64 | If |
14f5ae6d | 65 | .I addr |
5e8cde2f MK |
66 | is NULL, |
67 | then the kernel chooses the address at which to create the mapping; | |
68 | this is the most portable method of creating a new mapping. | |
c13182ef | 69 | If |
14f5ae6d | 70 | .I addr |
5e8cde2f MK |
71 | is not NULL, |
72 | then the kernel takes it as a hint about where to place the mapping; | |
6aa7db0a MK |
73 | on Linux, the mapping will be created at a nearby page boundary. |
74 | .\" Before Linux 2.6.24, the address was rounded up to the next page | |
29328361 | 75 | .\" boundary; since 2.6.24, it is rounded down! |
5e8cde2f | 76 | The address of the new mapping is returned as the result of the call. |
efeece04 | 77 | .PP |
5e8cde2f MK |
78 | The contents of a file mapping (as opposed to an anonymous mapping; see |
79 | .B MAP_ANONYMOUS | |
d9bfdb9c | 80 | below), are initialized using |
fea681da MK |
81 | .I length |
82 | bytes starting at offset | |
83 | .I offset | |
5e8cde2f MK |
84 | in the file (or other object) referred to by the file descriptor |
85 | .IR fd . | |
a62f5121 MK |
86 | .I offset |
87 | must be a multiple of the page size as returned by | |
88 | .IR sysconf(_SC_PAGE_SIZE) . | |
dd3568a1 | 89 | .PP |
fea681da MK |
90 | The |
91 | .I prot | |
c13182ef | 92 | argument describes the desired memory protection of the mapping |
5e8cde2f MK |
93 | (and must not conflict with the open mode of the file). |
94 | It is either | |
fea681da | 95 | .B PROT_NONE |
a62f5121 | 96 | or the bitwise OR of one or more of the following flags: |
fea681da MK |
97 | .TP 1.1i |
98 | .B PROT_EXEC | |
99 | Pages may be executed. | |
100 | .TP | |
101 | .B PROT_READ | |
102 | Pages may be read. | |
103 | .TP | |
104 | .B PROT_WRITE | |
105 | Pages may be written. | |
106 | .TP | |
107 | .B PROT_NONE | |
108 | Pages may not be accessed. | |
dd3568a1 | 109 | .PP |
fea681da MK |
110 | The |
111 | .I flags | |
a62f5121 MK |
112 | argument determines whether updates to the mapping |
113 | are visible to other processes mapping the same region, | |
ba7cb080 | 114 | and whether updates are carried through to the underlying file. |
d9bfdb9c | 115 | This behavior is determined by including exactly one |
a62f5121 | 116 | of the following values in |
5e8cde2f | 117 | .IR flags : |
ca90e95a | 118 | .TP |
fea681da | 119 | .B MAP_SHARED |
c13182ef | 120 | Share this mapping. |
bf525e90 MK |
121 | Updates to the mapping are visible to other processes mapping the same region, |
122 | and (in the case of file-backed mappings) | |
123 | are carried through to the underlying file. | |
72e8bdae MK |
124 | (To precisely control when updates are carried through |
125 | to the underlying file requires the use of | |
126 | .BR msync (2).) | |
fea681da MK |
127 | .TP |
128 | .B MAP_PRIVATE | |
129 | Create a private copy-on-write mapping. | |
a62f5121 MK |
130 | Updates to the mapping are not visible to other processes |
131 | mapping the same file, and are not carried through to | |
132 | the underlying file. | |
fea681da | 133 | It is unspecified whether changes made to the file after the |
1a956089 | 134 | .BR mmap () |
fea681da | 135 | call are visible in the mapped region. |
dd3568a1 | 136 | .PP |
78cdbef7 | 137 | Both of these flags are described in POSIX.1-2001 and POSIX.1-2008. |
efeece04 | 138 | .PP |
a62f5121 MK |
139 | In addition, zero or more of the following values can be ORed in |
140 | .IR flags : | |
fea681da | 141 | .TP |
c368e7ca MK |
142 | .BR MAP_32BIT " (since Linux 2.4.20, 2.6)" |
143 | Put the mapping into the first 2 Gigabytes of the process address space. | |
33a0ccb2 | 144 | This flag is supported only on x86-64, for 64-bit programs. |
c368e7ca | 145 | It was added to allow thread stacks to be allocated somewhere |
ee8655b5 | 146 | in the first 2\ GB of memory, |
c368e7ca MK |
147 | so as to improve context-switch performance on some early |
148 | 64-bit processors. | |
149 | .\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08 | |
150 | Modern x86-64 processors no longer have this performance problem, | |
151 | so use of this flag is not required on those systems. | |
152 | The | |
83314009 | 153 | .B MAP_32BIT |
c368e7ca | 154 | flag is ignored when |
a62f5121 | 155 | .B MAP_FIXED |
83314009 | 156 | is set. |
fea681da | 157 | .TP |
a62f5121 | 158 | .B MAP_ANON |
c13182ef MK |
159 | Synonym for |
160 | .BR MAP_ANONYMOUS . | |
a62f5121 MK |
161 | Deprecated. |
162 | .TP | |
fea681da | 163 | .B MAP_ANONYMOUS |
5e8cde2f | 164 | The mapping is not backed by any file; |
d9bfdb9c | 165 | its contents are initialized to zero. |
5e8cde2f | 166 | The |
fea681da | 167 | .I fd |
423cb9f7 | 168 | argument is ignored; |
c13182ef | 169 | however, some implementations require |
a62f5121 | 170 | .I fd |
c13182ef MK |
171 | to be \-1 if |
172 | .B MAP_ANONYMOUS | |
173 | (or | |
174 | .BR MAP_ANON ) | |
a62f5121 MK |
175 | is specified, |
176 | and portable applications should ensure this. | |
423cb9f7 JH |
177 | The |
178 | .I offset | |
179 | argument should be zero. | |
180 | .\" See the pgoff overflow check in do_mmap(). | |
181 | .\" See the offset check in sys_mmap in arch/x86/kernel/sys_x86_64.c. | |
c13182ef | 182 | The use of |
a62f5121 | 183 | .B MAP_ANONYMOUS |
c13182ef | 184 | in conjunction with |
51ffcca0 | 185 | .B MAP_SHARED |
33a0ccb2 | 186 | is supported on Linux only since kernel 2.4. |
fea681da | 187 | .TP |
83314009 MK |
188 | .B MAP_DENYWRITE |
189 | This flag is ignored. | |
190 | .\" Introduced in 1.1.36, removed in 1.3.24. | |
d9bfdb9c | 191 | (Long ago, it signaled that attempts to write to the underlying file |
83314009 MK |
192 | should fail with |
193 | .BR ETXTBUSY . | |
194 | But this was a source of denial-of-service attacks.) | |
195 | .TP | |
196 | .B MAP_EXECUTABLE | |
197 | This flag is ignored. | |
198 | .\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link. | |
d9bfdb9c | 199 | .\" (Long ago, it signaled that the underlying file is an executable. |
83314009 MK |
200 | .\" However, that information was not really used anywhere.) |
201 | .\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of | |
202 | .\" MAP_DENYWRITE? | |
203 | .TP | |
fea681da | 204 | .B MAP_FILE |
c13182ef MK |
205 | Compatibility flag. |
206 | Ignored. | |
988db661 | 207 | .\" On some systems, this was required as the opposite of |
83314009 | 208 | .\" MAP_ANONYMOUS -- mtk, 1 May 2007 |
fea681da | 209 | .TP |
51ffcca0 | 210 | .B MAP_FIXED |
83314009 | 211 | Don't interpret |
14f5ae6d | 212 | .I addr |
83314009 | 213 | as a hint: place the mapping at exactly that address. |
14f5ae6d | 214 | .I addr |
83314009 MK |
215 | must be a multiple of the page size. |
216 | If the memory region specified by | |
14f5ae6d | 217 | .I addr |
83314009 MK |
218 | and |
219 | .I len | |
220 | overlaps pages of any existing mapping(s), then the overlapped | |
221 | part of the existing mapping(s) will be discarded. | |
222 | If the specified address cannot be used, | |
223 | .BR mmap () | |
224 | will fail. | |
225 | Because requiring a fixed address for a mapping is less portable, | |
226 | the use of this option is discouraged. | |
fea681da | 227 | .TP |
83314009 | 228 | .B MAP_GROWSDOWN |
86f12eb0 MK |
229 | This flag is used for stacks. |
230 | It indicates to the kernel virtual memory system that the mapping | |
5fab2e7c | 231 | should extend downward in memory. |
176b1a76 MK |
232 | The return address is one page lower than the memory area that is |
233 | actually created in the process's virtual address space. | |
234 | Touching an address in the "guard" page below the mapping will cause | |
235 | the mapping to grow by a page. | |
236 | This growth can be repeated until the mapping grows to within a | |
237 | page of the high end of the next lower mapping, | |
238 | at which point touching the "guard" page will result in a | |
239 | .B SIGSEGV | |
240 | signal. | |
83314009 | 241 | .TP |
76a34baa MK |
242 | .BR MAP_HUGETLB " (since Linux 2.6.32)" |
243 | Allocate the mapping using "huge pages." | |
66a9882e | 244 | See the Linux kernel source file |
76a34baa | 245 | .I Documentation/vm/hugetlbpage.txt |
f1461fe1 | 246 | for further information, as well as NOTES, below. |
76a34baa | 247 | .TP |
5d2038b6 MK |
248 | .BR MAP_HUGE_2MB ", " MAP_HUGE_1GB " (since Linux 3.8)" |
249 | .\" See https://lwn.net/Articles/533499/ | |
250 | Used in conjunction with | |
251 | .B MAP_HUGETLB | |
c4b7e5ac | 252 | to select alternative hugetlb page sizes (respectively, 2\ MB and 1\ GB) |
5d2038b6 | 253 | on systems that support multiple hugetlb page sizes. |
efeece04 | 254 | .IP |
5d2038b6 MK |
255 | More generally, the desired huge page size can be configured by encoding |
256 | the base-2 logarithm of the desired page size in the six bits at the offset | |
257 | .BR MAP_HUGE_SHIFT . | |
258 | (A value of zero in this bit field provides the default huge page size; | |
259 | the default huge page size can be discovered vie the | |
260 | .I Hugepagesize | |
261 | field exposed by | |
262 | .IR /proc/meminfo .) | |
263 | Thus, the above two constants are defined as: | |
efeece04 | 264 | .IP |
5d2038b6 | 265 | .in +4n |
b8302363 | 266 | .EX |
5d2038b6 MK |
267 | #define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT) |
268 | #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT) | |
b8302363 | 269 | .EE |
e646a1ba | 270 | .in |
efeece04 | 271 | .IP |
5d2038b6 MK |
272 | The range of huge page sizes that are supported by the system |
273 | can be discovered by listing the subdirectories in | |
274 | .IR /sys/kernel/mm/hugepages . | |
275 | .TP | |
83314009 | 276 | .BR MAP_LOCKED " (since Linux 2.5.37)" |
7e3786bc | 277 | Mark the mmaped region to be locked in the same way as |
74d32233 | 278 | .BR mlock (2). |
7e3786bc MH |
279 | This implementation will try to populate (prefault) the whole range but |
280 | the mmap call doesn't fail with | |
281 | .B ENOMEM | |
911f1c7a MK |
282 | if this fails. |
283 | Therefore major faults might happen later on. | |
284 | So the semantic is not as strong as | |
7e3786bc | 285 | .BR mlock (2). |
911f1c7a | 286 | One should use |
bf7bc8b8 | 287 | .BR mmap () |
911f1c7a | 288 | plus |
7e3786bc | 289 | .BR mlock (2) |
911f1c7a MK |
290 | when major faults are not acceptable after the initialization of the mapping. |
291 | The | |
292 | .BR MAP_LOCKED | |
293 | flag is ignored in older kernels. | |
83314009 | 294 | .\" If set, the mapped pages will not be swapped out. |
fea681da MK |
295 | .TP |
296 | .BR MAP_NONBLOCK " (since Linux 2.5.46)" | |
3f06ade3 | 297 | This flag is meaningful only in conjunction with |
51ffcca0 | 298 | .BR MAP_POPULATE . |
c13182ef | 299 | Don't perform read-ahead: |
33a0ccb2 | 300 | create page tables entries only for pages |
51ffcca0 | 301 | that are already present in RAM. |
7c40de08 | 302 | Since Linux 2.6.23, this flag causes |
f38fa944 MK |
303 | .BR MAP_POPULATE |
304 | to do nothing. | |
487c2f05 | 305 | One day, the combination of |
f38fa944 MK |
306 | .BR MAP_POPULATE |
307 | and | |
308 | .BR MAP_NONBLOCK | |
3b777aff | 309 | may be reimplemented. |
83314009 MK |
310 | .TP |
311 | .B MAP_NORESERVE | |
312 | Do not reserve swap space for this mapping. | |
313 | When swap space is reserved, one has the guarantee | |
314 | that it is possible to modify the mapping. | |
8bd58774 MK |
315 | When swap space is not reserved one might get |
316 | .B SIGSEGV | |
317 | upon a write | |
83314009 MK |
318 | if no physical memory is available. |
319 | See also the discussion of the file | |
320 | .I /proc/sys/vm/overcommit_memory | |
321 | in | |
322 | .BR proc (5). | |
33a0ccb2 | 323 | In kernels before 2.6, this flag had effect only for |
83314009 MK |
324 | private writable mappings. |
325 | .TP | |
326 | .BR MAP_POPULATE " (since Linux 2.5.46)" | |
f38fa944 MK |
327 | Populate (prefault) page tables for a mapping. |
328 | For a file mapping, this causes read-ahead on the file. | |
bbebbb6d | 329 | This will help to reduce blocking on page faults later. |
f38fa944 | 330 | .BR MAP_POPULATE |
33a0ccb2 | 331 | is supported for private mappings only since Linux 2.6.23. |
e6205b0c MK |
332 | .TP |
333 | .BR MAP_STACK " (since Linux 2.6.27)" | |
334 | Allocate the mapping at an address suitable for a process | |
335 | or thread stack. | |
336 | This flag is currently a no-op, | |
337 | but is used in the glibc threading implementation so that | |
338 | if some architectures require special treatment for stack allocations, | |
339 | support can later be transparently implemented for glibc. | |
67b59ff5 | 340 | .\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08 |
e6205b0c MK |
341 | .\" commit cd98a04a59e2f94fa64d5bf1e26498d27427d5e7 |
342 | .\" http://thread.gmane.org/gmane.linux.kernel/720412 | |
343 | .\" "pthread_create() slow for many threads; also time to revisit 64b | |
344 | .\" context switch optimization?" | |
12062404 MK |
345 | .TP |
346 | .BR MAP_UNINITIALIZED " (since Linux 2.6.33)" | |
347 | Don't clear anonymous pages. | |
348 | This flag is intended to improve performance on embedded devices. | |
33a0ccb2 | 349 | This flag is honored only if the kernel was configured with the |
12062404 MK |
350 | .B CONFIG_MMAP_ALLOW_UNINITIALIZED |
351 | option. | |
352 | Because of the security implications, | |
353 | that option is normally enabled only on embedded devices | |
354 | (i.e., devices where one has complete control of the contents of user memory). | |
dd3568a1 | 355 | .PP |
7c7adcbe MK |
356 | Of the above flags, only |
357 | .B MAP_FIXED | |
78cdbef7 | 358 | is specified in POSIX.1-2001 and POSIX.1-2008. |
7c7adcbe MK |
359 | However, most systems also support |
360 | .B MAP_ANONYMOUS | |
361 | (or its synonym | |
362 | .BR MAP_ANON ). | |
f5f41651 MK |
363 | .\" FIXME . for later review when Issue 8 is one day released... |
364 | .\" POSIX may add MAP_ANON in the future | |
365 | .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8 | |
366 | .\" http://austingroupbugs.net/view.php?id=850 | |
dd3568a1 | 367 | .PP |
fea681da | 368 | Memory mapped by |
1a956089 | 369 | .BR mmap () |
fea681da MK |
370 | is preserved across |
371 | .BR fork (2), | |
372 | with the same attributes. | |
dd3568a1 | 373 | .PP |
c13182ef MK |
374 | A file is mapped in multiples of the page size. |
375 | For a file that is not | |
fea681da | 376 | a multiple of the page size, the remaining memory is zeroed when mapped, |
c13182ef MK |
377 | and writes to that region are not written out to the file. |
378 | The effect of | |
fea681da MK |
379 | changing the size of the underlying file of a mapping on the pages that |
380 | correspond to added or removed regions of the file is unspecified. | |
de5f7e28 | 381 | .SS munmap() |
fea681da | 382 | The |
1a956089 | 383 | .BR munmap () |
fea681da MK |
384 | system call deletes the mappings for the specified address range, and |
385 | causes further references to addresses within the range to generate | |
c13182ef MK |
386 | invalid memory references. |
387 | The region is also automatically unmapped | |
388 | when the process is terminated. | |
389 | On the other hand, closing the file | |
fea681da | 390 | descriptor does not unmap the region. |
dd3568a1 | 391 | .PP |
fea681da | 392 | The address |
14f5ae6d | 393 | .I addr |
0e824bcb MK |
394 | must be a multiple of the page size (but |
395 | .I length | |
396 | need not be). | |
c13182ef | 397 | All pages containing a part |
fea681da | 398 | of the indicated range are unmapped, and subsequent references |
8bd58774 MK |
399 | to these pages will generate |
400 | .BR SIGSEGV . | |
c13182ef | 401 | It is not an error if the |
fea681da | 402 | indicated range does not contain any mapped pages. |
47297adb | 403 | .SH RETURN VALUE |
fea681da | 404 | On success, |
1a956089 | 405 | .BR mmap () |
fea681da MK |
406 | returns a pointer to the mapped area. |
407 | On error, the value | |
408 | .B MAP_FAILED | |
c13182ef | 409 | (that is, |
009df872 | 410 | .IR "(void\ *)\ \-1" ) |
5e8cde2f | 411 | is returned, and |
fea681da | 412 | .I errno |
80691a91 | 413 | is set to indicate the cause of the error. |
efeece04 | 414 | .PP |
fea681da | 415 | On success, |
1a956089 | 416 | .BR munmap () |
80691a91 MK |
417 | returns 0. |
418 | On failure, it returns \-1, and | |
fea681da | 419 | .I errno |
80691a91 | 420 | is set to indicate the cause of the error (probably to |
51ffcca0 | 421 | .BR EINVAL ). |
fea681da MK |
422 | .SH ERRORS |
423 | .TP | |
424 | .B EACCES | |
425 | A file descriptor refers to a non-regular file. | |
5e7c71f6 | 426 | Or a file mapping was requested, but |
fea681da MK |
427 | .I fd |
428 | is not open for reading. | |
c13182ef MK |
429 | Or |
430 | .B MAP_SHARED | |
431 | was requested and | |
432 | .B PROT_WRITE | |
51ffcca0 | 433 | is set, but |
fea681da | 434 | .I fd |
682edefb MK |
435 | is not open in read/write |
436 | .RB ( O_RDWR ) | |
437 | mode. | |
c13182ef MK |
438 | Or |
439 | .B PROT_WRITE | |
51ffcca0 | 440 | is set, but the file is append-only. |
fea681da MK |
441 | .TP |
442 | .B EAGAIN | |
83cd3686 MK |
443 | The file has been locked, or too much memory has been locked (see |
444 | .BR setrlimit (2)). | |
fea681da MK |
445 | .TP |
446 | .B EBADF | |
447 | .I fd | |
c13182ef | 448 | is not a valid file descriptor (and |
51ffcca0 MK |
449 | .B MAP_ANONYMOUS |
450 | was not set). | |
fea681da MK |
451 | .TP |
452 | .B EINVAL | |
453 | We don't like | |
14f5ae6d | 454 | .IR addr , |
62a04c81 | 455 | .IR length , |
fea681da | 456 | or |
0daa9e92 | 457 | .I offset |
62a04c81 MK |
458 | (e.g., they are too large, or not aligned on a page boundary). |
459 | .TP | |
460 | .B EINVAL | |
f99fc197 | 461 | (since Linux 2.6.12) |
fea681da | 462 | .I length |
62a04c81 MK |
463 | was 0. |
464 | .TP | |
465 | .B EINVAL | |
466 | .I flags | |
467 | contained neither | |
468 | .B MAP_PRIVATE | |
fea681da | 469 | or |
62a04c81 MK |
470 | .BR MAP_SHARED , |
471 | or contained both of these values. | |
fea681da MK |
472 | .TP |
473 | .B ENFILE | |
474 | .\" This is for shared anonymous segments | |
475 | .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp() | |
e258766b | 476 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
477 | .\" .TP |
478 | .\" .B ENOEXEC | |
479 | .\" A file could not be mapped for reading. | |
480 | .TP | |
481 | .B ENODEV | |
9ee4a2b6 | 482 | The underlying filesystem of the specified file does not support |
fea681da MK |
483 | memory mapping. |
484 | .TP | |
485 | .B ENOMEM | |
74309bed MK |
486 | No memory is available. |
487 | .TP | |
488 | .B ENOMEM | |
489 | The process's maximum number of mappings would have been exceeded. | |
c0b89788 | 490 | This error can also occur for |
3804d39d | 491 | .BR munmap (), |
c0b89788 MK |
492 | when unmapping a region in the middle of an existing mapping, |
493 | since this results in two smaller mappings on either side of | |
494 | the region being unmapped. | |
fea681da | 495 | .TP |
c87d084b JG |
496 | .B ENOMEM |
497 | (since Linux 4.7) | |
498 | The process's | |
499 | .B RLIMIT_DATA | |
500 | limit, described in | |
501 | .BR getrlimit (2), | |
502 | would have been exceeded. | |
503 | .TP | |
038f5175 MK |
504 | .B EOVERFLOW |
505 | On 32-bit architecture together with the large file extension | |
506 | (i.e., using 64-bit | |
507 | .IR off_t ): | |
508 | the number of pages used for | |
509 | .I length | |
510 | plus number of pages used for | |
511 | .I offset | |
512 | would overflow | |
513 | .I "unsigned long" | |
514 | (32 bits). | |
515 | .TP | |
fea681da MK |
516 | .B EPERM |
517 | The | |
518 | .I prot | |
519 | argument asks for | |
520 | .B PROT_EXEC | |
9ee4a2b6 | 521 | but the mapped area belongs to a file on a filesystem that |
fea681da MK |
522 | was mounted no-exec. |
523 | .\" (Since 2.4.25 / 2.6.0.) | |
524 | .TP | |
fbab10e5 MK |
525 | .B EPERM |
526 | The operation was prevented by a file seal; see | |
527 | .BR fcntl (2). | |
528 | .TP | |
fea681da | 529 | .B ETXTBSY |
c13182ef | 530 | .B MAP_DENYWRITE |
51ffcca0 | 531 | was set but the object specified by |
fea681da MK |
532 | .I fd |
533 | is open for writing. | |
dd3568a1 | 534 | .PP |
fea681da MK |
535 | Use of a mapped region can result in these signals: |
536 | .TP | |
537 | .B SIGSEGV | |
1e321034 | 538 | Attempted write into a region mapped as read-only. |
fea681da MK |
539 | .TP |
540 | .B SIGBUS | |
541 | Attempted access to a portion of the buffer that does not correspond | |
542 | to the file (for example, beyond the end of the file, including the | |
543 | case where another process has truncated the file). | |
8fddf95a MS |
544 | .SH ATTRIBUTES |
545 | For an explanation of the terms used in this section, see | |
546 | .BR attributes (7). | |
547 | .TS | |
548 | allbox; | |
549 | lbw18 lb lb | |
550 | l l l. | |
551 | Interface Attribute Value | |
552 | T{ | |
553 | .BR mmap (), | |
554 | .BR munmap () | |
555 | T} Thread safety MT-Safe | |
556 | .TE | |
47297adb | 557 | .SH CONFORMING TO |
78cdbef7 | 558 | POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD. |
2b2581ee MK |
559 | .\" SVr4 documents additional error codes ENXIO and ENODEV. |
560 | .\" SUSv2 documents additional error codes EMFILE and EOVERFLOW. | |
fea681da MK |
561 | .SH AVAILABILITY |
562 | On POSIX systems on which | |
1a956089 | 563 | .BR mmap (), |
9af134cd | 564 | .BR msync (2), |
fea681da | 565 | and |
1a956089 | 566 | .BR munmap () |
fea681da MK |
567 | are available, |
568 | .B _POSIX_MAPPED_FILES | |
6387216b MK |
569 | is defined in \fI<unistd.h>\fP to a value greater than 0. |
570 | (See also | |
fea681da | 571 | .BR sysconf (3).) |
97c1eac8 | 572 | .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L. |
fea681da MK |
573 | .\" -1: unavailable, 0: ask using sysconf(). |
574 | .\" glibc defines it to 1. | |
a1d5f77c | 575 | .SH NOTES |
34ccb744 | 576 | On some hardware architectures (e.g., i386), |
0daa9e92 | 577 | .B PROT_WRITE |
f3edaabb MK |
578 | implies |
579 | .BR PROT_READ . | |
a1d5f77c MK |
580 | It is architecture dependent whether |
581 | .B PROT_READ | |
582 | implies | |
583 | .B PROT_EXEC | |
584 | or not. | |
585 | Portable programs should always set | |
586 | .B PROT_EXEC | |
587 | if they intend to execute code in the new mapping. | |
efeece04 | 588 | .PP |
80d17cfa MK |
589 | The portable way to create a mapping is to specify |
590 | .I addr | |
591 | as 0 (NULL), and omit | |
592 | .B MAP_FIXED | |
593 | from | |
594 | .IR flags . | |
595 | In this case, the system chooses the address for the mapping; | |
46cdb997 | 596 | the address is chosen so as not to conflict with any existing mapping, |
80d17cfa MK |
597 | and will not be 0. |
598 | If the | |
599 | .B MAP_FIXED | |
600 | flag is specified, and | |
601 | .I addr | |
491cd2f0 | 602 | is 0 (NULL), then the mapped address will be 0 (NULL). |
efeece04 | 603 | .PP |
45e97e2a MK |
604 | Certain |
605 | .I flags | |
f8619b6a MK |
606 | constants are defined only if suitable feature test macros are defined |
607 | (possibly by default): | |
608 | .BR _DEFAULT_SOURCE | |
609 | with glibc 2.19 or later; | |
610 | or | |
45e97e2a MK |
611 | .BR _BSD_SOURCE |
612 | or | |
613 | .BR _SVID_SOURCE | |
f8619b6a | 614 | in glibc 2.19 and earlier. |
50efeef8 | 615 | (Employing |
45e97e2a MK |
616 | .BR _GNU_SOURCE |
617 | also suffices, | |
618 | and requiring that macro specifically would have been more logical, | |
76c637e1 | 619 | since these flags are all Linux-specific.) |
45e97e2a MK |
620 | The relevant flags are: |
621 | .BR MAP_32BIT , | |
622 | .BR MAP_ANONYMOUS | |
623 | (and the synonym | |
624 | .BR MAP_ANON ), | |
625 | .BR MAP_DENYWRITE , | |
626 | .BR MAP_EXECUTABLE , | |
627 | .BR MAP_FILE , | |
628 | .BR MAP_GROWSDOWN , | |
629 | .BR MAP_HUGETLB , | |
630 | .BR MAP_LOCKED , | |
631 | .BR MAP_NONBLOCK , | |
632 | .BR MAP_NORESERVE , | |
633 | .BR MAP_POPULATE , | |
634 | and | |
635 | .BR MAP_STACK . | |
efeece04 | 636 | .PP |
3bd859bf MK |
637 | An application can determine which pages of a mapping are |
638 | currently resident in the buffer/page cache using | |
639 | .BR mincore (2). | |
35c189fb | 640 | .\" |
0f319769 MK |
641 | .SS Timestamps changes for file-backed mappings |
642 | For file-backed mappings, the | |
643 | .I st_atime | |
644 | field for the mapped file may be updated at any time between the | |
645 | .BR mmap () | |
646 | and the corresponding unmapping; the first reference to a mapped | |
647 | page will update the field if it has not been already. | |
dd3568a1 | 648 | .PP |
0f319769 MK |
649 | The |
650 | .I st_ctime | |
651 | and | |
652 | .I st_mtime | |
653 | field for a file mapped with | |
654 | .B PROT_WRITE | |
655 | and | |
656 | .B MAP_SHARED | |
657 | will be updated after | |
658 | a write to the mapped region, and before a subsequent | |
659 | .BR msync (2) | |
660 | with the | |
661 | .B MS_SYNC | |
662 | or | |
663 | .B MS_ASYNC | |
664 | flag, if one occurs. | |
665 | .\" | |
f1461fe1 MK |
666 | .SS Huge page (Huge TLB) mappings |
667 | For mappings that employ huge pages, the requirements for the arguments of | |
668 | .BR mmap () | |
669 | and | |
670 | .BR munmap () | |
671 | differ somewhat from the requirements for mappings | |
672 | that use the native system page size. | |
efeece04 | 673 | .PP |
f1461fe1 MK |
674 | For |
675 | .BR mmap (), | |
676 | .I offset | |
677 | must be a multiple of the underlying huge page size. | |
678 | The system automatically aligns | |
679 | .I length | |
680 | to be a multiple of the underlying huge page size. | |
efeece04 | 681 | .PP |
f1461fe1 MK |
682 | For |
683 | .BR munmap (), | |
684 | .I addr | |
685 | and | |
686 | .I length | |
687 | must both be a multiple of the underlying huge page size. | |
688 | .\" | |
0722a578 | 689 | .SS C library/kernel differences |
35c189fb MK |
690 | This page describes the interface provided by the glibc |
691 | .BR mmap () | |
692 | wrapper function. | |
693 | Originally, this function invoked a system call of the same name. | |
694 | Since kernel 2.4, that system call has been superseded by | |
695 | .BR mmap2 (2), | |
696 | and nowadays | |
697 | .\" Since around glibc 2.1/2.2, depending on the platform. | |
698 | the glibc | |
699 | .BR mmap () | |
700 | wrapper function invokes | |
701 | .BR mmap2 (2) | |
702 | with a suitably adjusted value for | |
703 | .IR offset . | |
fea681da | 704 | .SH BUGS |
329ad271 | 705 | On Linux, there are no guarantees like those suggested above under |
c13182ef | 706 | .BR MAP_NORESERVE . |
dbc53ca8 | 707 | By default, any process can be killed |
fea681da | 708 | at any moment when the system runs out of memory. |
efeece04 | 709 | .PP |
dbc53ca8 MK |
710 | In kernels before 2.6.7, the |
711 | .B MAP_POPULATE | |
33a0ccb2 | 712 | flag has effect only if |
dbc53ca8 MK |
713 | .I prot |
714 | is specified as | |
715 | .BR PROT_NONE . | |
efeece04 | 716 | .PP |
c13182ef | 717 | SUSv3 specifies that |
c8f3e580 MK |
718 | .BR mmap () |
719 | should fail if | |
720 | .I length | |
721 | is 0. | |
722 | However, in kernels before 2.6.12, | |
723 | .BR mmap () | |
724 | succeeded in this case: no mapping was created and the call returned | |
14f5ae6d | 725 | .IR addr . |
c8f3e580 MK |
726 | Since kernel 2.6.12, |
727 | .BR mmap () | |
728 | fails with the error | |
729 | .B EINVAL | |
730 | for this case. | |
efeece04 | 731 | .PP |
a780f17b MK |
732 | POSIX specifies that the system shall always |
733 | zero fill any partial page at the end | |
b072a788 | 734 | of the object and that system will never write any modification of the |
a780f17b MK |
735 | object beyond its end. |
736 | On Linux, when you write data to such partial page after the end | |
b072a788 | 737 | of the object, the data stays in the page cache even after the file |
a780f17b MK |
738 | is closed and unmapped |
739 | and even though the data is never written to the file itself, | |
740 | subsequent mappings may see the modified content. | |
741 | In some cases, this could be fixed by calling | |
742 | .BR msync (2) | |
743 | before the unmap takes place; | |
4e07c70f MK |
744 | however, this doesn't work on |
745 | .BR tmpfs (5) | |
b758a50a | 746 | (for example, when using the POSIX shared memory interface documented in |
a780f17b | 747 | .BR shm_overview (7)). |
74fa61b7 | 748 | .SH EXAMPLE |
2e001ad4 MK |
749 | .\" FIXME . Add an example here that uses an anonymous shared region for |
750 | .\" IPC between parent and child. | |
74fa61b7 MK |
751 | .PP |
752 | The following program prints part of the file specified in | |
753 | its first command-line argument to standard output. | |
754 | The range of bytes to be printed is specified via offset and length | |
755 | values in the second and third command-line arguments. | |
756 | The program creates a memory mapping of the required | |
757 | pages of the file and then uses | |
758 | .BR write (2) | |
759 | to output the desired bytes. | |
f30b7415 | 760 | .SS Program source |
e7d0bb47 | 761 | .EX |
74fa61b7 MK |
762 | #include <sys/mman.h> |
763 | #include <sys/stat.h> | |
764 | #include <fcntl.h> | |
765 | #include <stdio.h> | |
766 | #include <stdlib.h> | |
767 | #include <unistd.h> | |
768 | ||
4407d3d8 MK |
769 | #define handle_error(msg) \\ |
770 | do { perror(msg); exit(EXIT_FAILURE); } while (0) | |
771 | ||
74fa61b7 MK |
772 | int |
773 | main(int argc, char *argv[]) | |
774 | { | |
775 | char *addr; | |
776 | int fd; | |
777 | struct stat sb; | |
778 | off_t offset, pa_offset; | |
779 | size_t length; | |
780 | ssize_t s; | |
781 | ||
fbbfa7ce | 782 | if (argc < 3 || argc > 4) { |
74fa61b7 MK |
783 | fprintf(stderr, "%s file offset [length]\\n", argv[0]); |
784 | exit(EXIT_FAILURE); | |
785 | } | |
786 | ||
787 | fd = open(argv[1], O_RDONLY); | |
4407d3d8 | 788 | if (fd == \-1) |
8568021d | 789 | handle_error("open"); |
74fa61b7 | 790 | |
4407d3d8 MK |
791 | if (fstat(fd, &sb) == \-1) /* To obtain file size */ |
792 | handle_error("fstat"); | |
74fa61b7 MK |
793 | |
794 | offset = atoi(argv[2]); | |
795 | pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1); | |
796 | /* offset for mmap() must be page aligned */ | |
797 | ||
798 | if (offset >= sb.st_size) { | |
799 | fprintf(stderr, "offset is past end of file\\n"); | |
800 | exit(EXIT_FAILURE); | |
801 | } | |
802 | ||
803 | if (argc == 4) { | |
804 | length = atoi(argv[3]); | |
805 | if (offset + length > sb.st_size) | |
806 | length = sb.st_size \- offset; | |
f81fb444 | 807 | /* Can\(aqt display bytes past end of file */ |
5b6adad1 | 808 | |
74fa61b7 MK |
809 | } else { /* No length arg ==> display to end of file */ |
810 | length = sb.st_size \- offset; | |
811 | } | |
812 | ||
813 | addr = mmap(NULL, length + offset \- pa_offset, PROT_READ, | |
814 | MAP_PRIVATE, fd, pa_offset); | |
4407d3d8 MK |
815 | if (addr == MAP_FAILED) |
816 | handle_error("mmap"); | |
74fa61b7 MK |
817 | |
818 | s = write(STDOUT_FILENO, addr + offset \- pa_offset, length); | |
819 | if (s != length) { | |
820 | if (s == \-1) | |
4407d3d8 MK |
821 | handle_error("write"); |
822 | ||
823 | fprintf(stderr, "partial write"); | |
74fa61b7 MK |
824 | exit(EXIT_FAILURE); |
825 | } | |
826 | ||
40142309 MK |
827 | munmap(addr, length + offset \- pa_offset); |
828 | close(fd); | |
829 | ||
74fa61b7 | 830 | exit(EXIT_SUCCESS); |
c54ed37e | 831 | } |
e7d0bb47 | 832 | .EE |
47297adb | 833 | .SH SEE ALSO |
fea681da | 834 | .BR getpagesize (2), |
c4d76cd9 | 835 | .BR memfd_create (2), |
f75c3a3b | 836 | .BR mincore (2), |
fea681da MK |
837 | .BR mlock (2), |
838 | .BR mmap2 (2), | |
54504ac3 | 839 | .BR mprotect (2), |
fea681da MK |
840 | .BR mremap (2), |
841 | .BR msync (2), | |
931e4e25 | 842 | .BR remap_file_pages (2), |
83cd3686 | 843 | .BR setrlimit (2), |
7921f13b | 844 | .BR shmat (2), |
13acca70 | 845 | .BR userfaultfd (2), |
f93af9c6 MK |
846 | .BR shm_open (3), |
847 | .BR shm_overview (7) | |
efeece04 | 848 | .PP |
0bf14b87 MK |
849 | The descriptions of the following files in |
850 | .BR proc (5): | |
851 | .IR /proc/[pid]/maps , | |
852 | .IR /proc/[pid]/map_files , | |
853 | and | |
854 | .IR /proc/[pid]/smaps . | |
efeece04 | 855 | .PP |
d2fdb1e3 | 856 | B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128\(en129 and 389\(en391. |
fea681da MK |
857 | .\" |
858 | .\" Repeat after me: private read-only mappings are 100% equivalent to | |
859 | .\" shared read-only mappings. No ifs, buts, or maybes. -- Linus |