xref: /netbsd-src/lib/libc/sys/mmap.2 (revision 3fe138c1461e710931a50b66f08982c5f52c371f)
1.\"	$NetBSD: mmap.2,v 1.10 1997/12/15 04:00:08 mrg 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.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993
37.Dt MMAP 2
38.Os BSD 4
39.Sh NAME
40.Nm mmap
41.Nd map files or devices into memory
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <sys/mman.h>
45.Ft void *
46.Fn mmap "void *addr" "size_t len" "int prot" "int flags" "int fd" "off_t offset"
47.Sh DESCRIPTION
48The
49.Nm mmap
50function causes the pages starting at
51.Fa addr
52and continuing for at most
53.Fa len
54bytes to be mapped from the object described by
55.Fa fd ,
56starting at byte offset
57.Fa offset .
58If
59.Fa offset
60or
61.Fa len
62is not a multiple of the pagesize, the mapped region may extend past the
63specified range.
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.El
92.Pp
93The
94.Fa flags
95parameter specifies the type of the mapped object, mapping options and
96whether modifications made to the mapped copy of the page are private
97to the process or are to be shared with other references.
98Note that either
99.Dv MAP_SHARED
100or
101.Dv MAP_PRIVATE
102must be specified.
103Sharing, mapping type and options are specified in the
104.Fa flags
105argument by
106.Em or Ns 'ing
107the following values:
108.Pp
109.Bl -tag -width MAP_FIXEDX
110.It Dv MAP_ANON
111Map anonymous memory not associated with any specific file.
112The file descriptor used for creating
113.Dv MAP_ANON
114regions is used only for
115naming, and may be specified as \-1 if no name is associated with the
116region.
117.It Dv MAP_FILE
118Mapped from a regular file or character-special device memory.
119.It Dv MAP_FIXED
120Do not permit the system to select a different address than the one
121specified.
122If the specified address cannot be used,
123.Nm mmap
124will fail.
125If MAP_FIXED is specified,
126.Fa addr
127must be a multiple of the pagesize.
128Use of this option is discouraged.
129.It Dv MAP_HASSEMAPHORE
130Notify the kernel that the region may contain semaphores and that special
131handling may be necessary.
132.It Dv MAP_INHERIT
133Permit regions to be inherited across
134.Xr execve 2
135system calls.
136.It Dv MAP_PRIVATE
137Modifications are private.
138.It Dv MAP_SHARED
139Modifications are shared.
140.El
141.Pp
142The
143.Xr close 2
144function does not unmap pages, see
145.Xr munmap 2
146for further information.
147.Pp
148The current design does not allow a process to specify the location of
149swap space.
150In the future we may define an additional mapping type,
151.Dv MAP_SWAP ,
152in which
153the file descriptor argument specifies a file or device to which swapping
154should be done.
155.Sh RETURN VALUES
156Upon successful completion,
157.Nm mmap
158returns a pointer to the mapped region.
159Otherwise, a value of -1 is returned and
160.Va errno
161is set to indicate the error.
162.Sh ERRORS
163.Fn Mmap
164will fail if:
165.Bl -tag -width Er
166.It Bq Er EACCES
167The flag
168.Dv PROT_READ
169was specified as part of the
170.Fa prot
171parameter and
172.Fa fd
173was not open for reading.
174The flags
175.Dv PROT_WRITE and
176.Dv MAP_SHARED
177were specified as part
178of the
179.Fa flags
180and
181.Fa prot
182parameters and
183.Fa fd
184was not open for writing.
185.It Bq Er EBADF
186.Fa Fd
187is not a valid open file descriptor.
188.It Bq Er EINVAL
189.Dv MAP_FIXED
190was specified and the
191.I addr
192parameter was not page aligned.
193.Fa Fd
194did not reference a regular or character special file.
195.It Bq Er ENOMEM
196.Dv MAP_FIXED
197was specified and the
198.Fa addr
199parameter wasn't available.
200.Dv MAP_ANON
201was specified and insufficient memory was available.
202.Sh "SEE ALSO"
203.Xr madvise 2 ,
204.Xr mincore 2 ,
205.Xr mlock 2 ,
206.Xr mprotect 2 ,
207.Xr msync 2 ,
208.Xr munmap 2 ,
209.Xr getpagesize 3
210