xref: /netbsd-src/lib/libc/sys/mmap.2 (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1.\"	$NetBSD: mmap.2,v 1.32 2003/06/23 21:32:37 christos 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.4 (Berkeley) 5/11/95
35.\"
36.Dd April 10, 2003
37.Dt MMAP 2
38.Os
39.Sh NAME
40.Nm mmap
41.Nd map files or devices into memory
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In sys/mman.h
46.Ft void *
47.Fn mmap "void *addr" "size_t len" "int prot" "int flags" "int fd" "off_t offset"
48.Sh DESCRIPTION
49The
50.Nm mmap
51function causes the pages starting at
52.Fa addr
53and continuing for at most
54.Fa len
55bytes to be mapped from the object described by
56.Fa fd ,
57starting at byte offset
58.Fa offset .
59If
60.Fa len
61is not a multiple of the pagesize, the mapped region may extend past the
62specified range.
63Any such extension beyond the end of the mapped object will be zero-filled.
64.Pp
65If
66.Fa addr
67is non-zero, it is used as a hint to the system.
68(As a convenience to the system, the actual address of the region may differ
69from the address supplied.)
70If
71.Fa addr
72is zero, an address will be selected by the system.
73The actual starting address of the region is returned.
74A successful
75.Fa mmap
76deletes any previous mapping in the allocated address range.
77.Pp
78The protections (region accessibility) are specified in the
79.Fa prot
80argument by
81.Em or Ns 'ing
82the following values:
83.Pp
84.Bl -tag -width MAP_FIXEDX
85.It Dv PROT_EXEC
86Pages may be executed.
87.It Dv PROT_READ
88Pages may be read.
89.It Dv PROT_WRITE
90Pages may be written.
91.It Dv PROT_NONE
92Pages may not be accessed.
93.El
94.Pp
95.Bf -symbolic
96Note that, due to hardware limitations, on some platforms
97.Dv PROT_WRITE
98may imply
99.Dv PROT_READ ,
100and
101.Dv PROT_READ
102may imply
103.Dv PROT_EXEC .
104Portable programs should not rely on these flags being separately
105enforceable.
106.Ef
107.Pp
108The
109.Fa flags
110parameter specifies the type of the mapped object, mapping options and
111whether modifications made to the mapped copy of the page are private
112to the process or are to be shared with other references.
113Note that either
114.Dv MAP_SHARED ,
115.Dv MAP_PRIVATE
116or
117.Dv MAP_COPY
118must be specified.
119Sharing, mapping type and options are specified in the
120.Fa flags
121argument by
122.Em or Ns 'ing
123the following values:
124.Pp
125.Bl -tag -width MAP_FIXEDX
126.It Dv MAP_ALIGNED(n)
127Request that the allocation be aligned to the given boundary.
128The parameter
129.Ar n
130should be the base 2 logarithm of the desired alignment (e.g., to
131request alignment to 16K, use 14 as the value for n).
132The alignment must be equal to or greater than the platform's page
133size as returned by
134.Xr sysconf 3
135with the
136.Dv _SC_PAGESIZE
137request.
138.It Dv MAP_ANON
139Map anonymous memory not associated with any specific file.
140The file descriptor is not used for creating
141.Dv MAP_ANON
142regions, and must be specified as \-1.
143.It Dv MAP_FILE
144Mapped from a regular file or character-special device memory.
145.It Dv MAP_FIXED
146Do not permit the system to select a different address than the one
147specified.
148If the specified address cannot be used,
149.Nm mmap
150will fail.
151If MAP_FIXED is specified,
152.Fa addr
153must be a multiple of the pagesize.
154Use of this option is discouraged.
155.It Dv MAP_HASSEMAPHORE
156Notify the kernel that the region may contain semaphores and that special
157handling may be necessary.
158.It Dv MAP_INHERIT
159Permit regions to be inherited across
160.Xr execve 2
161system calls.
162.It Dv MAP_TRYFIXED
163Attempt to use the address
164.Fa addr
165even if it falls within the normally protected process data or
166text segment memory regions.  If the requested region of memory
167is actually present in the memory map, a different address will
168be selected as if MAP_TRYFIXED had not been specified.  If
169.Fa addr
170is
171.Fa NULL ,
172this flag is ignored and the system will select a mapping address.
173.It Dv MAP_PRIVATE
174Modifications made by this process are private, however modifications made by
175other processes using
176.Dv MAP_SHARED
177will be seen.
178.It Dv MAP_SHARED
179Modifications are shared.
180.It Dv MAP_COPY
181Modifications are private, including other processes.
182.El
183.Pp
184The
185.Xr close 2
186function does not unmap pages, see
187.Xr munmap 2
188for further information.
189.Pp
190The current design does not allow a process to specify the location of
191swap space.
192In the future we may define an additional mapping type,
193.Dv MAP_SWAP ,
194in which
195the file descriptor argument specifies a file or device to which swapping
196should be done.
197.Pp
198If
199.Dv MAP_FIXED
200is not specified, the system will attempt to place the mapping in an
201unused portion of the address space chosen to minimize possible
202collision between mapped regions and the heap.
203.Sh RETURN VALUES
204Upon successful completion,
205.Nm mmap
206returns a pointer to the mapped region.
207Otherwise, a value of
208.Dv MAP_FAILED
209is returned and
210.Va errno
211is set to indicate the error.
212The symbol
213.Dv MAP_FAILED
214is defined in the header
215.Ao Pa sys/mman.h Ac .
216No successful return from
217.Fn mmap
218will return the value
219.Dv MAP_FAILED .
220.Sh ERRORS
221.Fn mmap
222will fail if:
223.Bl -tag -width Er
224.It Bq Er EACCES
225The flag
226.Dv PROT_READ
227was specified as part of the
228.Fa prot
229parameter and
230.Fa fd
231was not open for reading.
232The flags
233.Dv MAP_SHARED
234and
235.Dv PROT_WRITE
236were specified as part of the
237.Fa flags
238and
239.Fa prot
240parameters and
241.Fa fd
242was not open for writing.
243.It Bq Er EBADF
244.Fa fd
245is not a valid open file descriptor.
246.It Bq Er EINVAL
247.\"One of
248.\".Dv MAP_ANON
249.\"or
250.\".Dv MAP_FILE
251.\"was not specified as part of the
252.\".Fa flags
253.\"parameter.
254.Dv MAP_FIXED
255was specified and the
256.Fa addr
257parameter was not page aligned or was outside of the
258valid address range for a process.
259.Dv MAP_ANON was specified and
260.Fa fd
261was not \-1.
262.Fa len
263was less than zero.
264.It Bq Er ENODEV
265.Fa fd
266did not reference a regular or character special file.
267.It Bq Er ENOMEM
268.Dv MAP_FIXED
269was specified and the
270.Fa addr
271parameter wasn't available.
272.Dv MAP_ANON
273was specified and insufficient memory was available.
274.It Bq Er EOVERFLOW
275.Fa fd
276references a regular file and the value of
277.Fa offset
278plus
279.Fa len
280would exceed the offset maximum established in its open file description.
281.El
282.Sh SEE ALSO
283.Xr madvise 2 ,
284.Xr mincore 2 ,
285.Xr mlock 2 ,
286.Xr mprotect 2 ,
287.Xr msync 2 ,
288.Xr munmap 2 ,
289.Xr getpagesize 3 ,
290.Xr sysconf 3
291.Sh BUGS
292The
293.Dv MAP_COPY
294flag is not implemented.
295The current
296.Dv MAP_COPY
297semantics are the same as those of the
298.Dv MAP_PRIVATE
299flag.
300