]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/mmap.2
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / man / man2 / mmap.2
CommitLineData
9bccf70c
A
1.\" $NetBSD: mmap.2,v 1.5 1995/06/24 10:48:59 cgd Exp $
2.\"
3.\" Copyright (c) 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)mmap.2 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993
37.Dt MMAP 2
38.Os BSD 4
39.Sh NAME
40.Nm mmap
41.Nd map files or devices into memory
42.Sh SYNOPSIS
9bccf70c
A
43.Fd #include <sys/mman.h>
44.Ft void *
2d21ac55
A
45.Fo mmap
46.Fa "void *addr"
47.Fa "size_t len"
48.Fa "int prot"
49.Fa "int flags"
50.Fa "int fildes"
51.Fa "off_t offset"
52.Fc
9bccf70c
A
53.Sh DESCRIPTION
54The
55.Nm mmap
56function causes the pages starting at
57.Fa addr
58and continuing for at most
59.Fa len
60bytes to be mapped from the object described by
2d21ac55 61.Fa fildes ,
9bccf70c
A
62starting at byte offset
63.Fa offset .
64If
65.Fa offset
66or
67.Fa len
68is not a multiple of the pagesize, the mapped region may extend past the
69specified range.
70.Pp
71If
72.Fa addr
73is non-zero, it is used as a hint to the system.
74(As a convenience to the system, the actual address of the region may differ
75from the address supplied.)
76If
77.Fa addr
78is zero, an address will be selected by the system.
79The actual starting address of the region is returned.
80A successful
81.Fa mmap
82deletes any previous mapping in the allocated address range.
83.Pp
84The protections (region accessibility) are specified in the
85.Fa prot
86argument by
87.Em or Ns 'ing
88the following values:
89.Pp
90.Bl -tag -width MAP_FIXEDX
91.It Dv PROT_EXEC
92Pages may be executed.
93.It Dv PROT_READ
94Pages may be read.
95.It Dv PROT_WRITE
96Pages may be written.
97.El
98.Pp
99The
100.Fa flags
2d21ac55
A
101parameter specifies the type of the mapped object, mapping options,
102and whether modifications made to the mapped copy of the page
103are private to the process (copy-on-write)
104or are to be shared with other references.
105Sharing, mapping type, and options are specified in the
9bccf70c
A
106.Fa flags
107argument by
108.Em or Ns 'ing
109the following values:
110.Pp
2d21ac55 111.Bl -tag -width MAP_HASSEMAPHOREX
9bccf70c
A
112.It Dv MAP_ANON
113Map anonymous memory not associated with any specific file.
2d21ac55 114Mac OS X specific: the file descriptor used for creating
9bccf70c 115.Dv MAP_ANON
2d21ac55
A
116regions can be used to pass some Mach VM flags, and can
117be specified as \-1 if no such flags are associated with
118the region. Mach VM flags are defined in
119<mach/vm_statistics.h> and the ones that currently apply
120to
121.Nm mmap
122are:
123.Pp
124VM_FLAGS_PURGABLE to create Mach purgable (i.e. volatile) memory
125VM_MAKE_TAG(tag) to associate an 8-bit tag with the region
126.Pp
127<mach/vm_statistics.h> defines some preset tags (with a VM_MEMORY_ prefix).
128Users are encouraged to use tags between 240 and 255.
129Tags are used by tools such as vmmap(1) to help identify specific memory regions.
9bccf70c
A
130.It Dv MAP_FILE
131Mapped from a regular file or character-special device memory. (This is
132the default mapping type, and need not be specified.)
133.It Dv MAP_FIXED
134Do not permit the system to select a different address than the one
135specified.
136If the specified address cannot be used,
137.Nm mmap
138will fail.
139If MAP_FIXED is specified,
140.Fa addr
141must be a multiple of the pagesize.
142Use of this option is discouraged.
143.It Dv MAP_HASSEMAPHORE
144Notify the kernel that the region may contain semaphores and that special
145handling may be necessary.
9bccf70c 146.It Dv MAP_PRIVATE
2d21ac55 147Modifications are private (copy-on-write).
9bccf70c
A
148.It Dv MAP_SHARED
149Modifications are shared.
2d21ac55
A
150.It Dv MAP_NOCACHE
151Pages in this mapping are not retained in the kernel's memory cache.
152If the system runs low on memory, pages in MAP_NOCACHE mappings will be among
153the first to be reclaimed.
154This flag is intended for mappings that have little locality and
155provides a hint to the kernel that pages in this mapping are unlikely to be needed
156again in the near future.
9bccf70c
A
157.El
158.Pp
2d21ac55
A
159Conforming applications must specify either MAP_PRIVATE or MAP_SHARED.
160.Pp
9bccf70c
A
161The
162.Xr close 2
163function does not unmap pages, see
164.Xr munmap 2
165for further information.
166.Pp
167The current design does not allow a process to specify the location of
168swap space.
169In the future we may define an additional mapping type,
170.Dv MAP_SWAP ,
171in which
172the file descriptor argument specifies a file or device to which swapping
173should be done.
174.Sh RETURN VALUES
175Upon successful completion,
176.Nm mmap
177returns a pointer to the mapped region.
178Otherwise, a value of -1 is returned and
179.Va errno
180is set to indicate the error.
181.Sh ERRORS
182.Fn Mmap
183will fail if:
184.Bl -tag -width Er
2d21ac55
A
185.\" ===========
186.It Bq Er EACCES
187.Fa Fildes
188is not open for reading.
189.\" ===========
9bccf70c 190.It Bq Er EACCES
9bccf70c
A
191The flags
192.Dv PROT_WRITE
193and
194.Dv MAP_SHARED
2d21ac55 195are specified as part of the
9bccf70c
A
196.Fa flags
197and
198.Fa prot
199parameters and
2d21ac55
A
200.Fa fildes
201is not open for writing.
202.\" ===========
9bccf70c 203.It Bq Er EBADF
2d21ac55
A
204.Fa fildes
205is not a valid file descriptor for an open file.
9bccf70c
A
206.It Bq Er EINVAL
207.Dv MAP_FIXED
2d21ac55 208is specified and the
9bccf70c 209.I addr
2d21ac55
A
210parameter is not page aligned.
211.\" ===========
212.It Bq Er EINVAL
213.Fa fildes
214does not reference a regular or character special file.
215.\" ===========
216.It Bq Er EINVAL
217.Fa flags
218does not include either MAP_PRIVATE or MAP_SHARED.
219.\" ===========
220.It Bq Er EINVAL
221.Fa len
222is not greater than zero.
223.\" ===========
224.It Bq Er EINVAL
225.Fa offset
226is not a multiple of the page size,
227as returned by
228.Xr sysconf 3 .
229.\" ===========
230.It Bq Er EMFILE
231The limit on mapped regions (per process or system) is exceeded.
232.\" ===========
233.It Bq Er ENODEV
234The file type for
235.Fa fildes
236is not supported for mapping.
237.\" ===========
238.It Bq Er ENOMEM
239.Dv MAP_FIXED
240is specified and the address range specified
241exceeds the address space limit for the process.
242.\" ===========
9bccf70c
A
243.It Bq Er ENOMEM
244.Dv MAP_FIXED
2d21ac55 245is specified and the address specified by the
9bccf70c 246.Fa addr
2d21ac55
A
247parameter isn't available.
248.\" ===========
249.It Bq Er ENOMEM
9bccf70c 250.Dv MAP_ANON
2d21ac55
A
251is specified and insufficient memory is available.
252.\" ===========
253.It Bq Er ENXIO
254Addresses in the specified range are invalid for fildes.
255.\" ===========
256.It Bq Er EOVERFLOW
257Addresses in the specified range exceed the maximum offset
258set for fildes.
259.El
260.Sh LEGACY SYNOPSIS
261.Fd #include <sys/types.h>
262.Fd #include <sys/mman.h>
263.Pp
264The include file
265.In sys/types.h
266is necessary.
267.Sh COMPATIBILITY
268.Fn mmap
269now returns with
270.Va errno
271set to EINVAL in places that historically succeeded.
272The rules have changed as follows:
273.Bl -bullet
274.It
275The
276.Fa flags
277parameter must specify either MAP_PRIVATE or MAP_SHARED.
278.It
279The
280.Fa size
281parameter must not be 0.
282.It
283The
284.Fa off
285parameter must be a multiple of pagesize,
286as returned by
287.Fn sysconf .
9bccf70c
A
288.El
289.Sh "SEE ALSO"
290.Xr getpagesize 2 ,
9bccf70c
A
291.Xr madvise 2 ,
292.Xr mincore 2 ,
2d21ac55
A
293.Xr mlock 2 ,
294.Xr mprotect 2 ,
295.Xr msync 2 ,
296.Xr munmap 2 ,
297.Xr sysconf 3 ,
298.Xr compat 5