xref: /openbsd/lib/libc/sys/mmap.2 (revision 17df1aa7)
1.\"	$OpenBSD: mmap.2,v 1.37 2008/06/30 05:46:46 otto Exp $
2.\"	$NetBSD: mmap.2,v 1.5 1995/06/24 10:48:59 cgd Exp $
3.\"
4.\" Copyright (c) 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"	@(#)mmap.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: June 30 2008 $
34.Dt MMAP 2
35.Os
36.Sh NAME
37.Nm mmap
38.Nd map files or devices into memory
39.Sh SYNOPSIS
40.Fd #include <sys/types.h>
41.Fd #include <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 offset
57or
58.Fa len
59is not a multiple of the pagesize, the mapped region may extend past the
60specified range.
61.Pp
62If
63.Fa addr
64is non-zero, it is used as a hint to the system.
65(As a convenience to the system, the actual address of the region may differ
66from the address supplied.)
67If
68.Fa addr
69is zero, an address will be selected by the system.
70The actual starting address of the region is returned.
71A successful
72.Fa mmap
73deletes any previous mapping in the allocated address range.
74.Pp
75The protections (region accessibility) are specified in the
76.Fa prot
77argument by OR'ing the following values:
78.Pp
79.Bl -tag -width "PROT_WRITE  " -offset indent -compact
80.It Dv PROT_EXEC
81Pages may be executed.
82.It Dv PROT_READ
83Pages may be read.
84.It Dv PROT_WRITE
85Pages may be written.
86.It Dv PROT_NONE
87No permissions.
88.El
89.Pp
90The
91.Fa flags
92parameter specifies the type of the mapped object, mapping options, and
93whether modifications made to the mapped copy of the page are private
94to the process or are to be shared with other references.
95Sharing, mapping type, and options are specified in the
96.Fa flags
97argument by OR'ing the following values:
98.Bl -tag -width MAP_FIXEDX
99.It Dv MAP_ANON
100Map anonymous memory not associated with any specific file.
101The file descriptor used for creating
102.Dv MAP_ANON
103must currently be \-1 indicating no name is associated with the
104region.
105.It Dv MAP_FILE
106Mapped from a regular file or character-special device memory.
107(This is the default mapping type, and need not be specified.)
108.It Dv MAP_FIXED
109Do not permit the system to select a different address than the one
110specified.
111If the specified address cannot be used,
112.Nm mmap
113will fail.
114If
115.Dv MAP_FIXED
116is specified,
117.Fa addr
118must be a multiple of the pagesize.
119Use of this option is discouraged.
120.It Dv MAP_HASSEMAPHORE
121Notify the kernel that the region may contain semaphores and that special
122handling may be necessary.
123.It Dv MAP_INHERIT
124Permit regions to be inherited across
125.Xr exec 3
126system calls.
127.It Dv MAP_PRIVATE
128Modifications are private.
129.It Dv MAP_SHARED
130Modifications are shared.
131.It Dv MAP_COPY
132Modifications are private and, unlike
133.Dv MAP_PRIVATE ,
134modifications made by others are not visible.
135This option is deprecated, shouldn't be used and behaves just like
136.Dv MAP_PRIVATE
137in the current implementation.
138.El
139.Pp
140The
141.Xr close 2
142function does not unmap pages; see
143.Xr munmap 2
144for further information.
145.Pp
146The current design does not allow a process to specify the location of
147swap space.
148In the future we may define an additional mapping type,
149.Dv MAP_SWAP ,
150in which
151the file descriptor argument specifies a file or device to which swapping
152should be done.
153.Sh RETURN VALUES
154Upon successful completion,
155.Nm mmap
156returns a pointer to the mapped region.
157Otherwise, a value of
158.Dv MAP_FAILED
159is returned and
160.Va errno
161is set to indicate the error.
162The symbol
163.Dv MAP_FAILED
164is defined in the header
165.Ao Pa sys/mman.h Ac .
166No successful return from
167.Fn mmap
168will return the value
169.Dv MAP_FAILED .
170.Sh ERRORS
171.Fn mmap
172will fail if:
173.Bl -tag -width Er
174.It Bq Er EACCES
175The flag
176.Dv PROT_READ
177was specified as part of the
178.Fa prot
179parameter and
180.Fa fd
181was not open for reading.
182The flags
183.Dv MAP_SHARED
184and
185.Dv PROT_WRITE
186were specified as part
187of the
188.Fa flags
189and
190.Fa prot
191parameters and
192.Fa fd
193was not open for writing.
194.It Bq Er EBADF
195.Fa fd
196is not a valid open file descriptor.
197.It Bq Er EINVAL
198.Dv MAP_FIXED
199was specified and the
200.Fa addr
201parameter was not page aligned or
202.Fa addr
203and
204.Fa size
205specify a region that would extend beyond the end of the address space.
206.Fa fd
207did not reference a regular or character special file.
208.It Bq Er ENOMEM
209.Dv MAP_FIXED
210was specified and the
211.Fa addr
212parameter wasn't available.
213.Dv MAP_ANON
214was specified and insufficient memory was available.
215.El
216.Sh SEE ALSO
217.Xr madvise 2 ,
218.Xr mincore 2 ,
219.Xr mlock 2 ,
220.Xr mprotect 2 ,
221.Xr mquery 2 ,
222.Xr msync 2 ,
223.Xr munmap 2 ,
224.Xr getpagesize 3
225.Sh BUGS
226Due to a limitation of the current vm system (see
227.Xr uvm 9 ) ,
228mapping descriptors
229.Dv PROT_WRITE
230without also specifying
231.Dv PROT_READ
232is useless
233(results in a segmentation fault when first accessing the mapping).
234This means that such descriptors must be opened with
235.Dv O_RDWR ,
236which requires both read and write permissions on the underlying
237object.
238