xref: /netbsd-src/lib/libc/sys/mmap.2 (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1.\"	$NetBSD: mmap.2,v 1.31 2003/04/16 13:34:54 wiz 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_PRIVATE
163Modifications made by this process are private, however modifications made by
164other processes using
165.Dv MAP_SHARED
166will be seen.
167.It Dv MAP_SHARED
168Modifications are shared.
169.It Dv MAP_COPY
170Modifications are private, including other processes.
171.El
172.Pp
173The
174.Xr close 2
175function does not unmap pages, see
176.Xr munmap 2
177for further information.
178.Pp
179The current design does not allow a process to specify the location of
180swap space.
181In the future we may define an additional mapping type,
182.Dv MAP_SWAP ,
183in which
184the file descriptor argument specifies a file or device to which swapping
185should be done.
186.Pp
187If
188.Dv MAP_FIXED
189is not specified, the system will attempt to place the mapping in an
190unused portion of the address space chosen to minimize possible
191collision between mapped regions and the heap.
192.Sh RETURN VALUES
193Upon successful completion,
194.Nm mmap
195returns a pointer to the mapped region.
196Otherwise, a value of
197.Dv MAP_FAILED
198is returned and
199.Va errno
200is set to indicate the error.
201The symbol
202.Dv MAP_FAILED
203is defined in the header
204.Ao Pa sys/mman.h Ac .
205No successful return from
206.Fn mmap
207will return the value
208.Dv MAP_FAILED .
209.Sh ERRORS
210.Fn mmap
211will fail if:
212.Bl -tag -width Er
213.It Bq Er EACCES
214The flag
215.Dv PROT_READ
216was specified as part of the
217.Fa prot
218parameter and
219.Fa fd
220was not open for reading.
221The flags
222.Dv MAP_SHARED
223and
224.Dv PROT_WRITE
225were specified as part of the
226.Fa flags
227and
228.Fa prot
229parameters and
230.Fa fd
231was not open for writing.
232.It Bq Er EBADF
233.Fa fd
234is not a valid open file descriptor.
235.It Bq Er EINVAL
236.\"One of
237.\".Dv MAP_ANON
238.\"or
239.\".Dv MAP_FILE
240.\"was not specified as part of the
241.\".Fa flags
242.\"parameter.
243.Dv MAP_FIXED
244was specified and the
245.Fa addr
246parameter was not page aligned or was outside of the
247valid address range for a process.
248.Dv MAP_ANON was specified and
249.Fa fd
250was not \-1.
251.Fa len
252was less than zero.
253.It Bq Er ENODEV
254.Fa fd
255did not reference a regular or character special file.
256.It Bq Er ENOMEM
257.Dv MAP_FIXED
258was specified and the
259.Fa addr
260parameter wasn't available.
261.Dv MAP_ANON
262was specified and insufficient memory was available.
263.It Bq Er EOVERFLOW
264.Fa fd
265references a regular file and the value of
266.Fa offset
267plus
268.Fa len
269would exceed the offset maximum established in its open file description.
270.El
271.Sh SEE ALSO
272.Xr madvise 2 ,
273.Xr mincore 2 ,
274.Xr mlock 2 ,
275.Xr mprotect 2 ,
276.Xr msync 2 ,
277.Xr munmap 2 ,
278.Xr getpagesize 3 ,
279.Xr sysconf 3
280.Sh BUGS
281The
282.Dv MAP_COPY
283flag is not implemented.
284The current
285.Dv MAP_COPY
286semantics are the same as those of the
287.Dv MAP_PRIVATE
288flag.
289