xref: /netbsd-src/lib/libc/sys/mmap.2 (revision 82d56013d7b633d116a93943de88e08335357a7c)
1.\"	$NetBSD: mmap.2,v 1.55 2019/09/08 17:24:49 sevan 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 September 8, 2019
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.Nm
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.Bl -tag -width PROT_WRITEXX -offset indent
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
87Placeholder when requesting no access permission.
88.El
89.Pp
90As a
91.Nx
92extension, the
93.Dv PROT_MPROTECT
94macro can be used to request additional permissions for later use with
95.Fn mprotect 2 .
96For example
97.Dv PROT_MPROTECT(PROT_READ)
98requests that future
99.Dv PROT_READ
100mappings are allowed and can be enabled using
101.Xr mprotect 2 ,
102but does not currently grant read mappings to the returned memory segment.
103This is necessary for switching pages between writable and executable
104when PaX MPROTECT restrictions are in place.
105See
106.Xr mremap 2
107for a sample use case.
108.Pp
109.Bf -symbolic
110Note that, due to hardware limitations, on some platforms
111.Dv PROT_WRITE
112may imply
113.Dv PROT_READ ,
114and
115.Dv PROT_READ
116may imply
117.Dv PROT_EXEC .
118Portable programs should not rely on these flags being separately
119enforceable.
120.Ef
121.Pp
122The
123.Fa flags
124parameter specifies the type of the mapped object, mapping options and
125whether modifications made to the mapped copy of the page are private
126to the process or are to be shared with other references.
127Note that either
128.Dv MAP_SHARED
129or
130.Dv MAP_PRIVATE
131must be specified.
132Sharing, mapping type and options are specified in the
133.Fa flags
134argument by
135.Em OR Ns 'ing
136the following values:
137.Bl -tag -width MAP_HASSEMAPHOREXX -offset indent
138.It Dv MAP_ALIGNED(n)
139Request that the allocation be aligned to the given boundary.
140The parameter
141.Ar n
142should be the base 2 logarithm of the desired alignment (e.g., to
143request alignment to 16K, use 14 as the value for n).
144The alignment must be equal to or greater than the platform's page
145size as returned by
146.Xr sysconf 3
147with the
148.Dv _SC_PAGESIZE
149request.
150The following constants are defined for convenience:
151.Bl -bullet -compact -offset indent
152.It
153.Dv MAP_ALIGNMENT_64KB
154.It
155.Dv MAP_ALIGNMENT_16MB
156.It
157.Dv MAP_ALIGNMENT_4GB
158.It
159.Dv MAP_ALIGNMENT_1TB
160.It
161.Dv MAP_ALIGNMENT_256TB
162.It
163.Dv MAP_ALIGNMENT_64PB
164.El
165.It Dv MAP_ANON
166Map anonymous memory not associated with any specific file.
167The file descriptor is not used for creating
168.Dv MAP_ANON
169regions, and must be specified as \-1.
170The mapped memory will be zero filled.
171.It Dv MAP_ANONYMOUS
172Synonymous with
173.Dv MAP_ANON .
174.It Dv MAP_FILE
175Mapped from a regular file or character-special device memory.
176Read accesses beyond the end of of the file or device but less
177than the current page size will be zero-filled.
178Write accesses beyond the end of the file or device but less
179than the current page size will not affect the file or device.
180References beyond the end of file that are beyond the current
181page size will result in the delivery of
182.Dv SIGBUS
183signal.
184.It Dv MAP_FIXED
185Do not permit the system to select a different address than the one
186specified.
187If the specified address cannot be used,
188.Nm mmap
189will fail.
190If
191.Dv MAP_FIXED
192is specified,
193.Fa addr
194must be a multiple of the pagesize.
195Use of this option is discouraged.
196.It Dv MAP_HASSEMAPHORE
197Notify the kernel that the region may contain semaphores and that special
198handling may be necessary.
199.It Dv MAP_INHERIT
200Permit regions to be inherited across
201.Xr execve 2
202system calls.
203.It Dv MAP_NORESERVE
204Only reserve address space, but do not reserve swap space or any other
205resources for this mapping.
206Access to the address space is not guaranteed and may result in a segmentation
207violation.
208Unimplemented.
209.It Dv MAP_PRIVATE
210Modifications made by this process are private, however modifications made by
211other processes using
212.Dv MAP_SHARED
213will be seen.
214.It Dv MAP_REMAPDUP
215Only valid for
216.Xr mremap 2 .
217.It Dv MAP_RENAME
218Assign the referenced private pages to the file descriptor provided.
219Unimplemented.
220.It Dv MAP_SHARED
221Modifications are shared.
222.It Dv MAP_STACK
223Allocate a memory segment that can be used either for a process or thread stack.
224This currently has no effect, but its use is reserved for architectures
225that might require special treatment of that address space.
226Unimplemented.
227.It Dv MAP_TRYFIXED
228Attempt to use the address
229.Fa addr
230even if it falls within the normally protected process data or
231text segment memory regions.
232If the requested region of memory
233is actually present in the memory map, a different address will
234be selected as if
235.Dv MAP_TRYFIXED
236had not been specified.
237If
238.Fa addr
239is
240.Dv NULL ,
241this flag is ignored and the system will select a mapping address.
242.It Dv MAP_WIRED
243Lock the mapped region into memory as with
244.Xr mlock 2 .
245.El
246.Pp
247The
248.Xr close 2
249function does not unmap pages, see
250.Xr munmap 2
251for further information.
252.Pp
253The current design does not allow a process to specify the location of
254swap space.
255In the future we may define an additional mapping type,
256.Dv MAP_SWAP ,
257in which
258the file descriptor argument specifies a file or device to which swapping
259should be done.
260.Pp
261If
262.Dv MAP_FIXED
263is not specified, the system will attempt to place the mapping in an
264unused portion of the address space chosen to minimize possible
265collision between mapped regions and the heap.
266.Sh RETURN VALUES
267Upon successful completion,
268.Nm mmap
269returns a pointer to the mapped region.
270Otherwise, a value of
271.Dv MAP_FAILED
272is returned and
273.Va errno
274is set to indicate the error.
275The symbol
276.Dv MAP_FAILED
277is defined in the header
278.In sys/mman.h .
279No successful return from
280.Fn mmap
281will return the value
282.Dv MAP_FAILED .
283.Sh ERRORS
284.Fn mmap
285will fail if:
286.Bl -tag -width Er
287.It Bq Er EACCES
288The flag
289.Dv PROT_READ
290was specified as part of the
291.Fa prot
292parameter and
293.Fa fd
294was not open for reading.
295.Pp
296The flags
297.Dv MAP_SHARED
298and
299.Dv PROT_WRITE
300were specified as part of the
301.Fa flags
302and
303.Fa prot
304parameters and
305.Fa fd
306was not open for writing.
307.Pp
308PaX mprotect restrictions prohibit the requested protection.
309.It Bq Er EBADF
310.Fa fd
311is not a valid open file descriptor.
312.It Bq Er EINVAL
313.\"One of
314.\".Dv MAP_ANON
315.\"or
316.\".Dv MAP_FILE
317.\"was not specified as part of the
318.\".Fa flags
319.\"parameter.
320.Dv MAP_FIXED
321was specified and the
322.Fa addr
323parameter was not page aligned or was outside of the
324valid address range for a process.
325.Pp
326.Dv MAP_ANON
327was specified and
328.Fa fd
329was not \-1.
330.It Bq Er ENODEV
331.Fa fd
332did not reference a regular or character special file.
333.It Bq Er ENOMEM
334.Dv MAP_FIXED
335was specified and the
336.Fa addr
337parameter wasn't available.
338.Pp
339.Dv MAP_ANON
340was specified and insufficient memory was available.
341.It Bq Er EOVERFLOW
342.Fa fd
343references a regular file and the value of
344.Fa offset
345plus
346.Fa len
347would exceed the offset maximum established in its open file description.
348.El
349.Sh SEE ALSO
350.Xr madvise 2 ,
351.Xr mincore 2 ,
352.Xr mlock 2 ,
353.Xr mprotect 2 ,
354.Xr mremap 2 ,
355.Xr msync 2 ,
356.Xr munmap 2 ,
357.Xr getpagesize 3 ,
358.Xr sysconf 3
359.Sh STANDARDS
360The
361.Fn mmap
362function conforms to
363.St -p1003.1b-93 .
364.Sh HISTORY
365The
366.Fn mmap
367interface was first designed in
368.Bx 4.2 .
369