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