xref: /netbsd-src/lib/libc/sys/mmap.2 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: mmap.2,v 1.37 2004/10/04 18:14:48 perry 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. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"	@(#)mmap.2	8.4 (Berkeley) 5/11/95
31.\"
32.Dd October 6, 2003
33.Dt MMAP 2
34.Os
35.Sh NAME
36.Nm mmap
37.Nd map files or devices into memory
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/mman.h
42.Ft void *
43.Fn mmap "void *addr" "size_t len" "int prot" "int flags" "int fd" "off_t offset"
44.Sh DESCRIPTION
45The
46.Nm mmap
47function causes the pages starting at
48.Fa addr
49and continuing for at most
50.Fa len
51bytes to be mapped from the object described by
52.Fa fd ,
53starting at byte offset
54.Fa offset .
55If
56.Fa len
57is not a multiple of the pagesize, the mapped region may extend past the
58specified range.
59Any such extension beyond the end of the mapped object will be zero-filled.
60.Pp
61If
62.Fa addr
63is non-zero, it is used as a hint to the system.
64(As a convenience to the system, the actual address of the region may differ
65from the address supplied.)
66If
67.Fa addr
68is zero, an address will be selected by the system.
69The actual starting address of the region is returned.
70A successful
71.Fa mmap
72deletes any previous mapping in the allocated address range.
73.Pp
74The protections (region accessibility) are specified in the
75.Fa prot
76argument by
77.Em or Ns 'ing
78the following values:
79.Pp
80.Bl -tag -width PROT_WRITEXX
81.It Dv PROT_EXEC
82Pages may be executed.
83.It Dv PROT_READ
84Pages may be read.
85.It Dv PROT_WRITE
86Pages may be written.
87.It Dv PROT_NONE
88Pages may not be accessed.
89.El
90.Pp
91.Bf -symbolic
92Note that, due to hardware limitations, on some platforms
93.Dv PROT_WRITE
94may imply
95.Dv PROT_READ ,
96and
97.Dv PROT_READ
98may imply
99.Dv PROT_EXEC .
100Portable programs should not rely on these flags being separately
101enforceable.
102.Ef
103.Pp
104The
105.Fa flags
106parameter specifies the type of the mapped object, mapping options and
107whether modifications made to the mapped copy of the page are private
108to the process or are to be shared with other references.
109Note that either
110.Dv MAP_SHARED ,
111.Dv MAP_PRIVATE
112or
113.Dv MAP_COPY
114must be specified.
115Sharing, mapping type and options are specified in the
116.Fa flags
117argument by
118.Em or Ns 'ing
119the following values:
120.Pp
121.Bl -tag -width MAP_HASSEMAPHOREXX
122.It Dv MAP_ALIGNED(n)
123Request that the allocation be aligned to the given boundary.
124The parameter
125.Ar n
126should be the base 2 logarithm of the desired alignment (e.g., to
127request alignment to 16K, use 14 as the value for n).
128The alignment must be equal to or greater than the platform's page
129size as returned by
130.Xr sysconf 3
131with the
132.Dv _SC_PAGESIZE
133request.
134.It Dv MAP_ANON
135Map anonymous memory not associated with any specific file.
136The file descriptor is not used for creating
137.Dv MAP_ANON
138regions, and must be specified as \-1.
139The mapped memory will be zero filled.
140.It Dv MAP_FILE
141Mapped from a regular file or character-special device memory.
142.It Dv MAP_FIXED
143Do not permit the system to select a different address than the one
144specified.
145If the specified address cannot be used,
146.Nm mmap
147will fail.
148If MAP_FIXED is specified,
149.Fa addr
150must be a multiple of the pagesize.
151Use of this option is discouraged.
152.It Dv MAP_HASSEMAPHORE
153Notify the kernel that the region may contain semaphores and that special
154handling may be necessary.
155.It Dv MAP_INHERIT
156Permit regions to be inherited across
157.Xr execve 2
158system calls.
159.It Dv MAP_TRYFIXED
160Attempt to use the address
161.Fa addr
162even if it falls within the normally protected process data or
163text segment memory regions.  If the requested region of memory
164is actually present in the memory map, a different address will
165be selected as if MAP_TRYFIXED had not been specified.  If
166.Fa addr
167is
168.Fa NULL ,
169this flag is ignored and the system will select a mapping address.
170.It Dv MAP_WIRED
171Lock the mapped region into memory as with
172.Xr mlock 2 .
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