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