xref: /netbsd-src/lib/libc/sys/mmap.2 (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1.\"	$NetBSD: mmap.2,v 1.36 2004/03/24 16:14:36 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. 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.
139.It Dv MAP_FILE
140Mapped from a regular file or character-special device memory.
141.It Dv MAP_FIXED
142Do not permit the system to select a different address than the one
143specified.
144If the specified address cannot be used,
145.Nm mmap
146will fail.
147If MAP_FIXED is specified,
148.Fa addr
149must be a multiple of the pagesize.
150Use of this option is discouraged.
151.It Dv MAP_HASSEMAPHORE
152Notify the kernel that the region may contain semaphores and that special
153handling may be necessary.
154.It Dv MAP_INHERIT
155Permit regions to be inherited across
156.Xr execve 2
157system calls.
158.It Dv MAP_TRYFIXED
159Attempt to use the address
160.Fa addr
161even if it falls within the normally protected process data or
162text segment memory regions.  If the requested region of memory
163is actually present in the memory map, a different address will
164be selected as if MAP_TRYFIXED had not been specified.  If
165.Fa addr
166is
167.Fa NULL ,
168this flag is ignored and the system will select a mapping address.
169.It Dv MAP_WIRED
170Lock the mapped region into memory as with
171.Xr mlock 2 .
172.It Dv MAP_PRIVATE
173Modifications made by this process are private, however modifications made by
174other processes using
175.Dv MAP_SHARED
176will be seen.
177.It Dv MAP_SHARED
178Modifications are shared.
179.It Dv MAP_COPY
180Modifications are private, including other processes.
181.El
182.Pp
183The
184.Xr close 2
185function does not unmap pages, see
186.Xr munmap 2
187for further information.
188.Pp
189The current design does not allow a process to specify the location of
190swap space.
191In the future we may define an additional mapping type,
192.Dv MAP_SWAP ,
193in which
194the file descriptor argument specifies a file or device to which swapping
195should be done.
196.Pp
197If
198.Dv MAP_FIXED
199is not specified, the system will attempt to place the mapping in an
200unused portion of the address space chosen to minimize possible
201collision between mapped regions and the heap.
202.Sh RETURN VALUES
203Upon successful completion,
204.Nm mmap
205returns a pointer to the mapped region.
206Otherwise, a value of
207.Dv MAP_FAILED
208is returned and
209.Va errno
210is set to indicate the error.
211The symbol
212.Dv MAP_FAILED
213is defined in the header
214.Ao Pa sys/mman.h Ac .
215No successful return from
216.Fn mmap
217will return the value
218.Dv MAP_FAILED .
219.Sh ERRORS
220.Fn mmap
221will fail if:
222.Bl -tag -width Er
223.It Bq Er EACCES
224The flag
225.Dv PROT_READ
226was specified as part of the
227.Fa prot
228parameter and
229.Fa fd
230was not open for reading.
231The flags
232.Dv MAP_SHARED
233and
234.Dv PROT_WRITE
235were specified as part of the
236.Fa flags
237and
238.Fa prot
239parameters and
240.Fa fd
241was not open for writing.
242.It Bq Er EBADF
243.Fa fd
244is not a valid open file descriptor.
245.It Bq Er EINVAL
246.\"One of
247.\".Dv MAP_ANON
248.\"or
249.\".Dv MAP_FILE
250.\"was not specified as part of the
251.\".Fa flags
252.\"parameter.
253.Dv MAP_FIXED
254was specified and the
255.Fa addr
256parameter was not page aligned or was outside of the
257valid address range for a process.
258.Dv MAP_ANON was specified and
259.Fa fd
260was not \-1.
261.Fa len
262was less than zero.
263.It Bq Er ENODEV
264.Fa fd
265did not reference a regular or character special file.
266.It Bq Er ENOMEM
267.Dv MAP_FIXED
268was specified and the
269.Fa addr
270parameter wasn't available.
271.Dv MAP_ANON
272was specified and insufficient memory was available.
273.It Bq Er EOVERFLOW
274.Fa fd
275references a regular file and the value of
276.Fa offset
277plus
278.Fa len
279would exceed the offset maximum established in its open file description.
280.El
281.Sh SEE ALSO
282.Xr madvise 2 ,
283.Xr mincore 2 ,
284.Xr mlock 2 ,
285.Xr mprotect 2 ,
286.Xr msync 2 ,
287.Xr munmap 2 ,
288.Xr getpagesize 3 ,
289.Xr sysconf 3
290.Sh BUGS
291The
292.Dv MAP_COPY
293flag is not implemented.
294The current
295.Dv MAP_COPY
296semantics are the same as those of the
297.Dv MAP_PRIVATE
298flag.
299