OSDN Git Service

(split) LDP man-pages の original/ を v3.25 に更新。
[linuxjm/LDP_man-pages.git] / original / man2 / mmap.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 1996 Andries Brouwer <aeb@cwi.nl>
4 .\" and Copyright (C) 2006, 2007 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
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>
30 .\" Modified 2003-05-21 by Michael Kerrisk <mtk.manpages@gmail.com>
31 .\"     MAP_LOCKED works from 2.5.37
32 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Modified 2004-09-11 by aeb
34 .\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr>
35 .\" Modified 2004-12-08, mtk, formatting tidy-ups
36 .\" Modified 2006-12-04, mtk, various parts rewritten
37 .\" 2007-07-10, mtk, Added an example program.
38 .\" 2008-11-18, mtk, document MAP_STACK
39 .\"
40 .TH MMAP 2 2010-06-20 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 mmap, munmap \- map or unmap files or devices into memory
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/mman.h>
46 .sp
47 .BI "void *mmap(void *" addr ", size_t " length \
48 ", int " prot ", int " flags ,
49 .BI "           int " fd ", off_t " offset );
50 .BI "int munmap(void *" addr ", size_t " length );
51 .fi
52 .SH DESCRIPTION
53 .BR mmap ()
54 creates a new mapping in the virtual address space of
55 the calling process.
56 The starting address for the new mapping is specified in
57 .IR addr .
58 The
59 .I length
60 argument specifies the length of the mapping.
61
62 If
63 .I addr
64 is NULL,
65 then the kernel chooses the address at which to create the mapping;
66 this is the most portable method of creating a new mapping.
67 If
68 .I addr
69 is not NULL,
70 then the kernel takes it as a hint about where to place the mapping;
71 on Linux, the mapping will be created at a nearby page boundary.
72 .\" Before Linux 2.6.24, the address was rounded up to the next page
73 .\" boundary; since 2.6.24, it is rounded down!
74 The address of the new mapping is returned as the result of the call.
75
76 The contents of a file mapping (as opposed to an anonymous mapping; see
77 .B MAP_ANONYMOUS
78 below), are initialized using
79 .I length
80 bytes starting at offset
81 .I offset
82 in the file (or other object) referred to by the file descriptor
83 .IR fd .
84 .I offset
85 must be a multiple of the page size as returned by
86 .IR sysconf(_SC_PAGE_SIZE) .
87 .LP
88 The
89 .I prot
90 argument describes the desired memory protection of the mapping
91 (and must not conflict with the open mode of the file).
92 It is either
93 .B PROT_NONE
94 or the bitwise OR of one or more of the following flags:
95 .TP 1.1i
96 .B PROT_EXEC
97 Pages may be executed.
98 .TP
99 .B PROT_READ
100 Pages may be read.
101 .TP
102 .B PROT_WRITE
103 Pages may be written.
104 .TP
105 .B PROT_NONE
106 Pages may not be accessed.
107 .LP
108 The
109 .I flags
110 argument determines whether updates to the mapping
111 are visible to other processes mapping the same region,
112 and whether updates are carried through to the underlying file.
113 This behavior is determined by including exactly one
114 of the following values in
115 .IR flags :
116 .TP 1.1i
117 .B MAP_SHARED
118 Share this mapping.
119 Updates to the mapping are visible to other processes that map this file,
120 and are carried through to the underlying file.
121 The file may not actually be updated until
122 .BR msync (2)
123 or
124 .BR munmap ()
125 is called.
126 .TP
127 .B MAP_PRIVATE
128 Create a private copy-on-write mapping.
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.
132 It is unspecified whether changes made to the file after the
133 .BR mmap ()
134 call are visible in the mapped region.
135 .LP
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 :
140 .TP
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.
143 This flag is only supported on x86-64, for 64-bit programs.
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
152 .B MAP_32BIT
153 flag is ignored when
154 .B MAP_FIXED
155 is set.
156 .TP
157 .B MAP_ANON
158 Synonym for
159 .BR MAP_ANONYMOUS .
160 Deprecated.
161 .TP
162 .B MAP_ANONYMOUS
163 The mapping is not backed by any file;
164 its contents are initialized to zero.
165 The
166 .I fd
167 and
168 .I offset
169 arguments are ignored;
170 however, some implementations require
171 .I fd
172 to be \-1 if
173 .B MAP_ANONYMOUS
174 (or
175 .BR MAP_ANON )
176 is specified,
177 and portable applications should ensure this.
178 The use of
179 .B MAP_ANONYMOUS
180 in conjunction with
181 .B MAP_SHARED
182 is only supported on Linux since kernel 2.4.
183 .TP
184 .B MAP_DENYWRITE
185 This flag is ignored.
186 .\" Introduced in 1.1.36, removed in 1.3.24.
187 (Long ago, it signaled that attempts to write to the underlying file
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.
195 .\" (Long ago, it signaled that the underlying file is an executable.
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
200 .B MAP_FILE
201 Compatibility flag.
202 Ignored.
203 .\" On some systems, this was required as the opposite of
204 .\" MAP_ANONYMOUS -- mtk, 1 May 2007
205 .TP
206 .B MAP_FIXED
207 Don't interpret
208 .I addr
209 as a hint: place the mapping at exactly that address.
210 .I addr
211 must be a multiple of the page size.
212 If the memory region specified by
213 .I addr
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.
223 .TP
224 .B MAP_GROWSDOWN
225 Used for stacks.
226 Indicates to the kernel virtual memory system that the mapping
227 should extend downwards in memory.
228 .TP
229 .BR MAP_HUGETLB " (since Linux 2.6.32)"
230 Allocate the mapping using "huge pages."
231 See the kernel source file
232 .I Documentation/vm/hugetlbpage.txt
233 for further information.
234 .TP
235 .BR MAP_LOCKED " (since Linux 2.5.37)"
236 Lock the pages of the mapped region into memory in the manner of
237 .BR mlock (2).
238 This flag is ignored in older kernels.
239 .\" If set, the mapped pages will not be swapped out.
240 .TP
241 .BR MAP_NONBLOCK " (since Linux 2.5.46)"
242 Only meaningful in conjunction with
243 .BR MAP_POPULATE .
244 Don't perform read-ahead:
245 only create page tables entries for pages
246 that are already present in RAM.
247 Since Linux 2.6.23, this flag causes
248 .BR MAP_POPULATE
249 to do nothing.
250 One day the combination of
251 .BR MAP_POPULATE
252 and
253 .BR MAP_NONBLOCK
254 may be reimplemented.
255 .TP
256 .B MAP_NORESERVE
257 Do not reserve swap space for this mapping.
258 When swap space is reserved, one has the guarantee
259 that it is possible to modify the mapping.
260 When swap space is not reserved one might get
261 .B SIGSEGV
262 upon a write
263 if no physical memory is available.
264 See also the discussion of the file
265 .I /proc/sys/vm/overcommit_memory
266 in
267 .BR proc (5).
268 In kernels before 2.6, this flag only had effect for
269 private writable mappings.
270 .TP
271 .BR MAP_POPULATE " (since Linux 2.5.46)"
272 Populate (prefault) page tables for a mapping.
273 For a file mapping, this causes read-ahead on the file.
274 Later accesses to the mapping will not be blocked by page faults.
275 .BR MAP_POPULATE
276 is only supported for private mappings since Linux 2.6.23.
277 .TP
278 .BR MAP_STACK " (since Linux 2.6.27)"
279 Allocate the mapping at an address suitable for a process
280 or thread stack.
281 This flag is currently a no-op,
282 but is used in the glibc threading implementation so that
283 if some architectures require special treatment for stack allocations,
284 support can later be transparently implemented for glibc.
285 .\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08
286 .\" commit cd98a04a59e2f94fa64d5bf1e26498d27427d5e7
287 .\" http://thread.gmane.org/gmane.linux.kernel/720412
288 .\" "pthread_create() slow for many threads; also time to revisit 64b
289 .\"  context switch optimization?"
290 .TP
291 .BR MAP_UNINITIALIZED " (since Linux 2.6.33)"
292 Don't clear anonymous pages.
293 This flag is intended to improve performance on embedded devices.
294 This flag is only honored if the kernel was configured with the
295 .B CONFIG_MMAP_ALLOW_UNINITIALIZED
296 option.
297 Because of the security implications,
298 that option is normally enabled only on embedded devices
299 (i.e., devices where one has complete control of the contents of user memory).
300 .LP
301 Of the above flags, only
302 .B MAP_FIXED
303 is specified in POSIX.1-2001.
304 However, most systems also support
305 .B MAP_ANONYMOUS
306 (or its synonym
307 .BR MAP_ANON ).
308 .LP
309 Some systems document the additional flags
310 .BR MAP_AUTOGROW ,
311 .BR MAP_AUTORESRV ,
312 .BR MAP_COPY ,
313 and
314 .BR MAP_LOCAL .
315 .LP
316 Memory mapped by
317 .BR mmap ()
318 is preserved across
319 .BR fork (2),
320 with the same attributes.
321 .LP
322 A file is mapped in multiples of the page size.
323 For a file that is not
324 a multiple of the page size, the remaining memory is zeroed when mapped,
325 and writes to that region are not written out to the file.
326 The effect of
327 changing the size of the underlying file of a mapping on the pages that
328 correspond to added or removed regions of the file is unspecified.
329 .SS munmap()
330 The
331 .BR munmap ()
332 system call deletes the mappings for the specified address range, and
333 causes further references to addresses within the range to generate
334 invalid memory references.
335 The region is also automatically unmapped
336 when the process is terminated.
337 On the other hand, closing the file
338 descriptor does not unmap the region.
339 .LP
340 The address
341 .I addr
342 must be a multiple of the page size.
343 All pages containing a part
344 of the indicated range are unmapped, and subsequent references
345 to these pages will generate
346 .BR SIGSEGV .
347 It is not an error if the
348 indicated range does not contain any mapped pages.
349 .SS Timestamps changes for file-backed mappings
350 For file-backed mappings, the
351 .I st_atime
352 field for the mapped file may be updated at any time between the
353 .BR mmap ()
354 and the corresponding unmapping; the first reference to a mapped
355 page will update the field if it has not been already.
356 .LP
357 The
358 .I st_ctime
359 and
360 .I st_mtime
361 field for a file mapped with
362 .B PROT_WRITE
363 and
364 .B MAP_SHARED
365 will be updated after
366 a write to the mapped region, and before a subsequent
367 .BR msync (2)
368 with the
369 .B MS_SYNC
370 or
371 .B MS_ASYNC
372 flag, if one occurs.
373 .SH "RETURN VALUE"
374 On success,
375 .BR mmap ()
376 returns a pointer to the mapped area.
377 On error, the value
378 .B MAP_FAILED
379 (that is,
380 .IR "(void\ *)\ \-1" )
381 is returned, and
382 .I errno
383 is set appropriately.
384 On success,
385 .BR munmap ()
386 returns 0, on failure \-1, and
387 .I errno
388 is set (probably to
389 .BR EINVAL ).
390 .SH ERRORS
391 .TP
392 .B EACCES
393 A file descriptor refers to a non-regular file.
394 Or
395 .B MAP_PRIVATE
396 was requested, but
397 .I fd
398 is not open for reading.
399 Or
400 .B MAP_SHARED
401 was requested and
402 .B PROT_WRITE
403 is set, but
404 .I fd
405 is not open in read/write
406 .RB ( O_RDWR )
407 mode.
408 Or
409 .B PROT_WRITE
410 is set, but the file is append-only.
411 .TP
412 .B EAGAIN
413 The file has been locked, or too much memory has been locked (see
414 .BR setrlimit (2)).
415 .TP
416 .B EBADF
417 .I fd
418 is not a valid file descriptor (and
419 .B MAP_ANONYMOUS
420 was not set).
421 .TP
422 .B EINVAL
423 We don't like
424 .IR addr ,
425 .IR length ,
426 or
427 .I offset
428 (e.g., they are too large, or not aligned on a page boundary).
429 .TP
430 .B EINVAL
431 (since Linux 2.6.12)
432 .I length
433 was 0.
434 .TP
435 .B EINVAL
436 .I flags
437 contained neither
438 .B MAP_PRIVATE
439 or
440 .BR MAP_SHARED ,
441 or contained both of these values.
442 .TP
443 .B ENFILE
444 .\" This is for shared anonymous segments
445 .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
446 The system limit on the total number of open files has been reached.
447 .\" .TP
448 .\" .B ENOEXEC
449 .\" A file could not be mapped for reading.
450 .TP
451 .B ENODEV
452 The underlying file system of the specified file does not support
453 memory mapping.
454 .TP
455 .B ENOMEM
456 No memory is available, or the process's maximum number of mappings would
457 have been exceeded.
458 .TP
459 .B EPERM
460 The
461 .I prot
462 argument asks for
463 .B PROT_EXEC
464 but the mapped area belongs to a file on a file system that
465 was mounted no-exec.
466 .\" (Since 2.4.25 / 2.6.0.)
467 .TP
468 .B ETXTBSY
469 .B MAP_DENYWRITE
470 was set but the object specified by
471 .I fd
472 is open for writing.
473 .LP
474 Use of a mapped region can result in these signals:
475 .TP
476 .B SIGSEGV
477 Attempted write into a region mapped as read-only.
478 .TP
479 .B SIGBUS
480 Attempted access to a portion of the buffer that does not correspond
481 to the file (for example, beyond the end of the file, including the
482 case where another process has truncated the file).
483 .SH "CONFORMING TO"
484 SVr4, 4.4BSD, POSIX.1-2001.
485 .\" SVr4 documents additional error codes ENXIO and ENODEV.
486 .\" SUSv2 documents additional error codes EMFILE and EOVERFLOW.
487 .SH AVAILABILITY
488 On POSIX systems on which
489 .BR mmap (),
490 .BR msync (2)
491 and
492 .BR munmap ()
493 are available,
494 .B _POSIX_MAPPED_FILES
495 is defined in \fI<unistd.h>\fP to a value greater than 0.
496 (See also
497 .BR sysconf (3).)
498 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
499 .\" -1: unavailable, 0: ask using sysconf().
500 .\" glibc defines it to 1.
501 .SH NOTES
502 Since kernel 2.4, this system call has been superseded by
503 .BR mmap2 (2).
504 Nowadays,
505 .\" Since around glibc 2.1/2.2, depending on the platform.
506 the glibc
507 .BR mmap ()
508 wrapper function invokes
509 .BR mmap2 (2)
510 with a suitably adjusted value for
511 .IR offset .
512
513 On some hardware architectures (e.g., i386),
514 .B PROT_WRITE
515 implies
516 .BR PROT_READ .
517 It is architecture dependent whether
518 .B PROT_READ
519 implies
520 .B PROT_EXEC
521 or not.
522 Portable programs should always set
523 .B PROT_EXEC
524 if they intend to execute code in the new mapping.
525
526 The portable way to create a mapping is to specify
527 .I addr
528 as 0 (NULL), and omit
529 .B MAP_FIXED
530 from
531 .IR flags .
532 In this case, the system chooses the address for the mapping;
533 the address is chosen so as not to conflict with any existing mapping,
534 and will not be 0.
535 If the
536 .B MAP_FIXED
537 flag is specified, and
538 .I addr
539 is 0 (NULL), then the mapped address will be 0 (NULL).
540 .SH BUGS
541 On Linux there are no guarantees like those suggested above under
542 .BR MAP_NORESERVE .
543 By default, any process can be killed
544 at any moment when the system runs out of memory.
545
546 In kernels before 2.6.7, the
547 .B MAP_POPULATE
548 flag only has effect if
549 .I prot
550 is specified as
551 .BR PROT_NONE .
552
553 SUSv3 specifies that
554 .BR mmap ()
555 should fail if
556 .I length
557 is 0.
558 However, in kernels before 2.6.12,
559 .BR mmap ()
560 succeeded in this case: no mapping was created and the call returned
561 .IR addr .
562 Since kernel 2.6.12,
563 .BR mmap ()
564 fails with the error
565 .B EINVAL
566 for this case.
567 .SH EXAMPLE
568 .\" FIXME . Add an example here that uses an anonymous shared region for
569 .\" IPC between parent and child.
570 .PP
571 The following program prints part of the file specified in
572 its first command-line argument to standard output.
573 The range of bytes to be printed is specified via offset and length
574 values in the second and third command-line arguments.
575 The program creates a memory mapping of the required
576 pages of the file and then uses
577 .BR write (2)
578 to output the desired bytes.
579 .nf
580
581 #include <sys/mman.h>
582 #include <sys/stat.h>
583 #include <fcntl.h>
584 #include <stdio.h>
585 #include <stdlib.h>
586 #include <unistd.h>
587
588 #define handle_error(msg) \\
589     do { perror(msg); exit(EXIT_FAILURE); } while (0)
590
591 int
592 main(int argc, char *argv[])
593 {
594     char *addr;
595     int fd;
596     struct stat sb;
597     off_t offset, pa_offset;
598     size_t length;
599     ssize_t s;
600
601     if (argc < 3 || argc > 4) {
602         fprintf(stderr, "%s file offset [length]\\n", argv[0]);
603         exit(EXIT_FAILURE);
604     }
605
606     fd = open(argv[1], O_RDONLY);
607     if (fd == \-1)
608         handle_error("open");
609
610     if (fstat(fd, &sb) == \-1)           /* To obtain file size */
611         handle_error("fstat");
612
613     offset = atoi(argv[2]);
614     pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1);
615         /* offset for mmap() must be page aligned */
616
617     if (offset >= sb.st_size) {
618         fprintf(stderr, "offset is past end of file\\n");
619         exit(EXIT_FAILURE);
620     }
621
622     if (argc == 4) {
623         length = atoi(argv[3]);
624         if (offset + length > sb.st_size)
625             length = sb.st_size \- offset;
626                 /* Can\(aqt display bytes past end of file */
627
628     } else {    /* No length arg ==> display to end of file */
629         length = sb.st_size \- offset;
630     }
631
632     addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
633                 MAP_PRIVATE, fd, pa_offset);
634     if (addr == MAP_FAILED)
635         handle_error("mmap");
636
637     s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
638     if (s != length) {
639         if (s == \-1)
640             handle_error("write");
641
642         fprintf(stderr, "partial write");
643         exit(EXIT_FAILURE);
644     }
645
646     exit(EXIT_SUCCESS);
647 } /* main */
648 .fi
649 .SH "SEE ALSO"
650 .BR getpagesize (2),
651 .BR mincore (2),
652 .BR mlock (2),
653 .BR mmap2 (2),
654 .BR mprotect (2),
655 .BR mremap (2),
656 .BR msync (2),
657 .BR remap_file_pages (2),
658 .BR setrlimit (2),
659 .BR shmat (2),
660 .BR shm_open (3),
661 .BR shm_overview (7)
662 .br
663 B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.
664 .\"
665 .\" Repeat after me: private read-only mappings are 100% equivalent to
666 .\" shared read-only mappings. No ifs, buts, or maybes. -- Linus