]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/mmap.2
xnu-2782.1.97.tar.gz
[apple/xnu.git] / bsd / man / man2 / mmap.2
CommitLineData
9bccf70c
A
1.\" Copyright (c) 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
9bccf70c
A
12.\" 4. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
b0d623f7
A
28.\" @(#)mmap.2 8.4 (Berkeley) 5/11/95
29.\" $FreeBSD: src/lib/libc/sys/mmap.2,v 1.56 2007/01/09 00:28:15 imp Exp $
9bccf70c 30.\"
b0d623f7 31.Dd April 21, 2006
9bccf70c 32.Dt MMAP 2
b0d623f7 33.Os
9bccf70c
A
34.Sh NAME
35.Nm mmap
b0d623f7
A
36.Nd allocate memory, or map files or devices into memory
37.Sh LIBRARY
38.Lb libc
9bccf70c 39.Sh SYNOPSIS
b0d623f7 40.In sys/mman.h
9bccf70c 41.Ft void *
b0d623f7 42.Fn mmap "void *addr" "size_t len" "int prot" "int flags" "int fd" "off_t offset"
9bccf70c
A
43.Sh DESCRIPTION
44The
b0d623f7
A
45.Fn mmap
46system call causes the pages starting at
9bccf70c
A
47.Fa addr
48and continuing for at most
49.Fa len
50bytes to be mapped from the object described by
b0d623f7 51.Fa fd ,
9bccf70c
A
52starting at byte offset
53.Fa offset .
54If
55.Fa offset
56or
57.Fa len
58is not a multiple of the pagesize, the mapped region may extend past the
59specified range.
b0d623f7 60Any extension beyond the end of the mapped object will be zero-filled.
9bccf70c 61.Pp
b0d623f7
A
62The
63.Fa addr
64argument is used by the system to determine the starting address of the mapping,
65and its interpretation is dependent on the setting of the MAP_FIXED flag.
66If MAP_FIXED is specified in
67.Fa flags ,
68the system will try to place the mapping at the specified address,
69possibly removing a
70mapping that already exists at that location.
71If MAP_FIXED is not specified,
72then the system will attempt to use the range of addresses starting at
9bccf70c 73.Fa addr
b0d623f7
A
74if they do not overlap any existing mappings,
75including memory allocated by malloc(3) and other such allocators.
76Otherwise,
77the system will choose an alternate address for the mapping (using an implementation
78dependent algorithm)
79that does not overlap any existing
80mappings.
81In other words,
82without MAP_FIXED the system will attempt to find an empty location in the address space if the specified address
83range has already been mapped by something else.
9bccf70c
A
84If
85.Fa addr
b0d623f7
A
86is zero and MAP_FIXED is not specified,
87then an address will be selected by the system so as not to overlap
88any existing mappings in the address space.
89In all cases,
90the actual starting address of the region is returned.
91If MAP_FIXED is specified,
92a successful
9bccf70c
A
93.Fa mmap
94deletes any previous mapping in the allocated address range.
b0d623f7 95Previous mappings are never deleted if MAP_FIXED is not specified.
9bccf70c
A
96.Pp
97The protections (region accessibility) are specified in the
98.Fa prot
99argument by
100.Em or Ns 'ing
101the following values:
102.Pp
b0d623f7
A
103.Bl -tag -width PROT_WRITE -compact
104.It Dv PROT_NONE
105Pages may not be accessed.
9bccf70c
A
106.It Dv PROT_READ
107Pages may be read.
108.It Dv PROT_WRITE
109Pages may be written.
b0d623f7
A
110.It Dv PROT_EXEC
111Pages may be executed.
9bccf70c
A
112.El
113.Pp
b0d623f7
A
114Note that, due to hardware limitations, on some platforms PROT_WRITE may
115imply PROT_READ, and PROT_READ may imply PROT_EXEC. Portable programs
116should not rely on these flags being separately enforcable.
117.Pp
9bccf70c
A
118The
119.Fa flags
b0d623f7
A
120argument specifies the type of the mapped object, mapping options and
121whether modifications made to the mapped copy of the page are private
122to the process (copy-on-write) or are to be shared with other references.
123Sharing, mapping type and options are specified in the
9bccf70c
A
124.Fa flags
125argument by
126.Em or Ns 'ing
127the following values:
b0d623f7 128.Bl -tag -width MAP_HASSEMAPHORE
9bccf70c
A
129.It Dv MAP_ANON
130Map anonymous memory not associated with any specific file.
b0d623f7
A
131The
132.Fa offset
133argument is ignored.
2d21ac55 134Mac OS X specific: the file descriptor used for creating
9bccf70c 135.Dv MAP_ANON
2d21ac55
A
136regions can be used to pass some Mach VM flags, and can
137be specified as \-1 if no such flags are associated with
138the region. Mach VM flags are defined in
139<mach/vm_statistics.h> and the ones that currently apply
140to
141.Nm mmap
142are:
143.Pp
144VM_FLAGS_PURGABLE to create Mach purgable (i.e. volatile) memory
2d21ac55 145.Pp
b0d623f7
A
146VM_MAKE_TAG(tag) to associate an 8-bit tag with the region
147.br
2d21ac55
A
148<mach/vm_statistics.h> defines some preset tags (with a VM_MEMORY_ prefix).
149Users are encouraged to use tags between 240 and 255.
150Tags are used by tools such as vmmap(1) to help identify specific memory regions.
6d2010ae
A
151.Pp
152VM_FLAGS_SUPERPAGE_SIZE_* to use superpages for the allocation.
153See <mach/vm_statistics.h> for supported architectures and sizes (or use
154VM_FLAGS_SUPERPAGE_SIZE_ANY to have the kernel choose a size).
155The specified size must be divisible by the superpage size (except for
156VM_FLAGS_SUPERPAGE_SIZE_ANY), and if you use MAP_FIXED, the specified address
157must be properly aligned. If the system cannot satisfy the request with superpages,
158the call will fail. Note that currently, superpages are always wired and not
159inherited by children of the process.
9bccf70c 160.It Dv MAP_FILE
b0d623f7 161Mapped from a regular file. (This is
9bccf70c
A
162the default mapping type, and need not be specified.)
163.It Dv MAP_FIXED
164Do not permit the system to select a different address than the one
165specified.
166If the specified address cannot be used,
b0d623f7 167.Fn mmap
9bccf70c 168will fail.
b0d623f7
A
169If
170.Dv MAP_FIXED
171is specified,
9bccf70c
A
172.Fa addr
173must be a multiple of the pagesize.
b0d623f7
A
174If a
175.Dv MAP_FIXED
176request is successful, the mapping established by
177.Fn mmap
178replaces any previous mappings for the process' pages in the range from
179.Fa addr
180to
181.Fa addr
182+
183.Fa len .
9bccf70c
A
184Use of this option is discouraged.
185.It Dv MAP_HASSEMAPHORE
186Notify the kernel that the region may contain semaphores and that special
187handling may be necessary.
9bccf70c 188.It Dv MAP_PRIVATE
2d21ac55 189Modifications are private (copy-on-write).
9bccf70c
A
190.It Dv MAP_SHARED
191Modifications are shared.
2d21ac55
A
192.It Dv MAP_NOCACHE
193Pages in this mapping are not retained in the kernel's memory cache.
194If the system runs low on memory, pages in MAP_NOCACHE mappings will be among
195the first to be reclaimed.
196This flag is intended for mappings that have little locality and
197provides a hint to the kernel that pages in this mapping are unlikely to be needed
198again in the near future.
9bccf70c
A
199.El
200.Pp
2d21ac55
A
201Conforming applications must specify either MAP_PRIVATE or MAP_SHARED.
202.Pp
b0d623f7 203The
9bccf70c 204.Xr close 2
b0d623f7 205system call does not unmap pages, see
9bccf70c
A
206.Xr munmap 2
207for further information.
208.Pp
209The current design does not allow a process to specify the location of
210swap space.
211In the future we may define an additional mapping type,
212.Dv MAP_SWAP ,
213in which
214the file descriptor argument specifies a file or device to which swapping
215should be done.
216.Sh RETURN VALUES
217Upon successful completion,
b0d623f7 218.Fn mmap
9bccf70c 219returns a pointer to the mapped region.
b0d623f7
A
220Otherwise, a value of
221.Dv MAP_FAILED
222is returned and
9bccf70c
A
223.Va errno
224is set to indicate the error.
225.Sh ERRORS
b0d623f7
A
226The
227.Fn mmap
228system call
9bccf70c
A
229will fail if:
230.Bl -tag -width Er
231.It Bq Er EACCES
b0d623f7
A
232The flag
233.Dv PROT_READ
234was specified as part of the
235.Fa prot
236argument and
237.Fa fd
238was not open for reading.
9bccf70c 239The flags
9bccf70c 240.Dv MAP_SHARED
b0d623f7
A
241and
242.Dv PROT_WRITE
243were specified as part of the
9bccf70c
A
244.Fa flags
245and
246.Fa prot
b0d623f7
A
247argument and
248.Fa fd
249was not open for writing.
9bccf70c 250.It Bq Er EBADF
b0d623f7
A
251The
252.Fa fd
253argument
254is not a valid open file descriptor.
9bccf70c
A
255.It Bq Er EINVAL
256.Dv MAP_FIXED
b0d623f7
A
257was specified and the
258.Fa addr
259argument was not page aligned, or part of the desired address space
260resides out of the valid address space for a user process.
2d21ac55
A
261.It Bq Er EINVAL
262.Fa flags
263does not include either MAP_PRIVATE or MAP_SHARED.
2d21ac55 264.It Bq Er EINVAL
b0d623f7 265The
2d21ac55 266.Fa len
b0d623f7
A
267argument
268was negative.
2d21ac55 269.It Bq Er EINVAL
b0d623f7 270The
2d21ac55 271.Fa offset
b0d623f7
A
272argument
273was not page-aligned based on the page size as returned by getpagesize(3).
2d21ac55 274.It Bq Er ENODEV
b0d623f7
A
275.Dv MAP_ANON
276has not been specified and the file
277.Fa fd
278refers to does not support mapping.
9bccf70c
A
279.It Bq Er ENOMEM
280.Dv MAP_FIXED
b0d623f7 281was specified and the
9bccf70c 282.Fa addr
b0d623f7
A
283argument was not available.
284.Dv MAP_FIXED
285was specified and the address range specified exceeds the address space
286limit for the process.
9bccf70c 287.Dv MAP_ANON
b0d623f7 288was specified and insufficient memory was available.
2d21ac55 289.It Bq Er ENXIO
b0d623f7
A
290Addresses in the specified range are invalid for
291.Fa fd .
2d21ac55
A
292.It Bq Er EOVERFLOW
293Addresses in the specified range exceed the maximum offset
b0d623f7
A
294set for
295.Fa fd .
2d21ac55
A
296.El
297.Sh LEGACY SYNOPSIS
298.Fd #include <sys/types.h>
299.Fd #include <sys/mman.h>
300.Pp
301The include file
302.In sys/types.h
303is necessary.
304.Sh COMPATIBILITY
305.Fn mmap
306now returns with
307.Va errno
308set to EINVAL in places that historically succeeded.
309The rules have changed as follows:
310.Bl -bullet
311.It
312The
313.Fa flags
314parameter must specify either MAP_PRIVATE or MAP_SHARED.
315.It
316The
317.Fa size
318parameter must not be 0.
319.It
320The
321.Fa off
322parameter must be a multiple of pagesize,
323as returned by
324.Fn sysconf .
9bccf70c 325.El
b0d623f7 326.Sh SEE ALSO
9bccf70c
A
327.Xr madvise 2 ,
328.Xr mincore 2 ,
b0d623f7 329.Xr minherit 2 ,
2d21ac55
A
330.Xr mlock 2 ,
331.Xr mprotect 2 ,
332.Xr msync 2 ,
b0d623f7 333.Xr munlock 2 ,
2d21ac55 334.Xr munmap 2 ,
b0d623f7
A
335.Xr shmat 2 ,
336.Xr getpagesize 3