xref: /freebsd/lib/libsys/mmap.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd August 14, 2023
29*8269e767SBrooks Davis.Dt MMAP 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm mmap
33*8269e767SBrooks Davis.Nd allocate memory, or map files or devices into memory
34*8269e767SBrooks Davis.Sh LIBRARY
35*8269e767SBrooks Davis.Lb libc
36*8269e767SBrooks Davis.Sh SYNOPSIS
37*8269e767SBrooks Davis.In sys/mman.h
38*8269e767SBrooks Davis.Ft void *
39*8269e767SBrooks Davis.Fn mmap "void *addr" "size_t len" "int prot" "int flags" "int fd" "off_t offset"
40*8269e767SBrooks Davis.Sh DESCRIPTION
41*8269e767SBrooks DavisThe
42*8269e767SBrooks Davis.Fn mmap
43*8269e767SBrooks Davissystem call causes the pages starting at
44*8269e767SBrooks Davis.Fa addr
45*8269e767SBrooks Davisand continuing for at most
46*8269e767SBrooks Davis.Fa len
47*8269e767SBrooks Davisbytes to be mapped from the object described by
48*8269e767SBrooks Davis.Fa fd ,
49*8269e767SBrooks Davisstarting at byte offset
50*8269e767SBrooks Davis.Fa offset .
51*8269e767SBrooks DavisIf
52*8269e767SBrooks Davis.Fa len
53*8269e767SBrooks Davisis not a multiple of the page size, the mapped region may extend past the
54*8269e767SBrooks Davisspecified range.
55*8269e767SBrooks DavisAny such extension beyond the end of the mapped object will be zero-filled.
56*8269e767SBrooks Davis.Pp
57*8269e767SBrooks DavisIf
58*8269e767SBrooks Davis.Fa fd
59*8269e767SBrooks Davisreferences a regular file or a shared memory object, the range of
60*8269e767SBrooks Davisbytes starting at
61*8269e767SBrooks Davis.Fa offset
62*8269e767SBrooks Davisand continuing for
63*8269e767SBrooks Davis.Fa len
64*8269e767SBrooks Davisbytes must be legitimate for the possible (not necessarily
65*8269e767SBrooks Daviscurrent) offsets in the object.
66*8269e767SBrooks DavisIn particular, the
67*8269e767SBrooks Davis.Fa offset
68*8269e767SBrooks Davisvalue cannot be negative.
69*8269e767SBrooks DavisIf the object is truncated and the process later accesses a page that
70*8269e767SBrooks Davisis wholly within the truncated region, the access is aborted and a
71*8269e767SBrooks Davis.Dv SIGBUS
72*8269e767SBrooks Davissignal is delivered to the process.
73*8269e767SBrooks Davis.Pp
74*8269e767SBrooks DavisIf
75*8269e767SBrooks Davis.Fa fd
76*8269e767SBrooks Davisreferences a device file, the interpretation of the
77*8269e767SBrooks Davis.Fa offset
78*8269e767SBrooks Davisvalue is device specific and defined by the device driver.
79*8269e767SBrooks DavisThe virtual memory subsystem does not impose any restrictions on the
80*8269e767SBrooks Davis.Fa offset
81*8269e767SBrooks Davisvalue in this case, passing it unchanged to the driver.
82*8269e767SBrooks Davis.Pp
83*8269e767SBrooks DavisIf
84*8269e767SBrooks Davis.Fa addr
85*8269e767SBrooks Davisis non-zero, it is used as a hint to the system.
86*8269e767SBrooks Davis(As a convenience to the system, the actual address of the region may differ
87*8269e767SBrooks Davisfrom the address supplied.)
88*8269e767SBrooks DavisIf
89*8269e767SBrooks Davis.Fa addr
90*8269e767SBrooks Davisis zero, an address will be selected by the system.
91*8269e767SBrooks DavisThe actual starting address of the region is returned.
92*8269e767SBrooks DavisA successful
93*8269e767SBrooks Davis.Fa mmap
94*8269e767SBrooks Davisdeletes any previous mapping in the allocated address range.
95*8269e767SBrooks Davis.Pp
96*8269e767SBrooks DavisThe protections (region accessibility) are specified in the
97*8269e767SBrooks Davis.Fa prot
98*8269e767SBrooks Davisargument by
99*8269e767SBrooks Davis.Em or Ns 'ing
100*8269e767SBrooks Davisthe following values:
101*8269e767SBrooks Davis.Pp
102*8269e767SBrooks Davis.Bl -tag -width PROT_WRITE -compact
103*8269e767SBrooks Davis.It Dv PROT_NONE
104*8269e767SBrooks DavisPages may not be accessed.
105*8269e767SBrooks Davis.It Dv PROT_READ
106*8269e767SBrooks DavisPages may be read.
107*8269e767SBrooks Davis.It Dv PROT_WRITE
108*8269e767SBrooks DavisPages may be written.
109*8269e767SBrooks Davis.It Dv PROT_EXEC
110*8269e767SBrooks DavisPages may be executed.
111*8269e767SBrooks Davis.El
112*8269e767SBrooks Davis.Pp
113*8269e767SBrooks DavisIn addition to these protection flags,
114*8269e767SBrooks Davis.Fx
115*8269e767SBrooks Davisprovides the ability to set the maximum protection of a region allocated by
116*8269e767SBrooks Davis.Nm
117*8269e767SBrooks Davisand later altered by
118*8269e767SBrooks Davis.Xr mprotect 2 .
119*8269e767SBrooks DavisThis is accomplished by
120*8269e767SBrooks Davis.Em or Ns 'ing
121*8269e767SBrooks Davisone or more
122*8269e767SBrooks Davis.Dv PROT_
123*8269e767SBrooks Davisvalues wrapped in the
124*8269e767SBrooks Davis.Dv PROT_MAX()
125*8269e767SBrooks Davismacro into the
126*8269e767SBrooks Davis.Fa prot
127*8269e767SBrooks Davisargument.
128*8269e767SBrooks Davis.Pp
129*8269e767SBrooks DavisThe
130*8269e767SBrooks Davis.Fa flags
131*8269e767SBrooks Davisargument specifies the type of the mapped object, mapping options and
132*8269e767SBrooks Daviswhether modifications made to the mapped copy of the page are private
133*8269e767SBrooks Davisto the process or are to be shared with other references.
134*8269e767SBrooks DavisSharing, mapping type and options are specified in the
135*8269e767SBrooks Davis.Fa flags
136*8269e767SBrooks Davisargument by
137*8269e767SBrooks Davis.Em or Ns 'ing
138*8269e767SBrooks Davisthe following values:
139*8269e767SBrooks Davis.Bl -tag -width MAP_PREFAULT_READ
140*8269e767SBrooks Davis.It Dv MAP_32BIT
141*8269e767SBrooks DavisRequest a region in the first 2GB of the current process's address space.
142*8269e767SBrooks DavisIf a suitable region cannot be found,
143*8269e767SBrooks Davis.Fn mmap
144*8269e767SBrooks Daviswill fail.
145*8269e767SBrooks Davis.It Dv MAP_ALIGNED Ns Pq Fa n
146*8269e767SBrooks DavisAlign the region on a requested boundary.
147*8269e767SBrooks DavisIf a suitable region cannot be found,
148*8269e767SBrooks Davis.Fn mmap
149*8269e767SBrooks Daviswill fail.
150*8269e767SBrooks DavisThe
151*8269e767SBrooks Davis.Fa n
152*8269e767SBrooks Davisargument specifies the binary logarithm of the desired alignment.
153*8269e767SBrooks Davis.It Dv MAP_ALIGNED_SUPER
154*8269e767SBrooks DavisAlign the region to maximize the potential use of large
155*8269e767SBrooks Davis.Pq Dq super
156*8269e767SBrooks Davispages.
157*8269e767SBrooks DavisIf a suitable region cannot be found,
158*8269e767SBrooks Davis.Fn mmap
159*8269e767SBrooks Daviswill fail.
160*8269e767SBrooks DavisThe system will choose a suitable page size based on the size of
161*8269e767SBrooks Davismapping.
162*8269e767SBrooks DavisThe page size used as well as the alignment of the region may both be
163*8269e767SBrooks Davisaffected by properties of the file being mapped.
164*8269e767SBrooks DavisIn particular,
165*8269e767SBrooks Davisthe physical address of existing pages of a file may require a specific
166*8269e767SBrooks Davisalignment.
167*8269e767SBrooks DavisThe region is not guaranteed to be aligned on any specific boundary.
168*8269e767SBrooks Davis.It Dv MAP_ANON
169*8269e767SBrooks DavisMap anonymous memory not associated with any specific file.
170*8269e767SBrooks DavisThe file descriptor used for creating
171*8269e767SBrooks Davis.Dv MAP_ANON
172*8269e767SBrooks Davismust be \-1.
173*8269e767SBrooks DavisThe
174*8269e767SBrooks Davis.Fa offset
175*8269e767SBrooks Davisargument must be 0.
176*8269e767SBrooks Davis.\".It Dv MAP_FILE
177*8269e767SBrooks Davis.\"Mapped from a regular file or character-special device memory.
178*8269e767SBrooks Davis.It Dv MAP_ANONYMOUS
179*8269e767SBrooks DavisThis flag is identical to
180*8269e767SBrooks Davis.Dv MAP_ANON
181*8269e767SBrooks Davisand is provided for compatibility.
182*8269e767SBrooks Davis.It Dv MAP_EXCL
183*8269e767SBrooks DavisThis flag can only be used in combination with
184*8269e767SBrooks Davis.Dv MAP_FIXED .
185*8269e767SBrooks DavisPlease see the definition of
186*8269e767SBrooks Davis.Dv MAP_FIXED
187*8269e767SBrooks Davisfor the description of its effect.
188*8269e767SBrooks Davis.It Dv MAP_FIXED
189*8269e767SBrooks DavisDo not permit the system to select a different address than the one
190*8269e767SBrooks Davisspecified.
191*8269e767SBrooks DavisIf the specified address cannot be used,
192*8269e767SBrooks Davis.Fn mmap
193*8269e767SBrooks Daviswill fail.
194*8269e767SBrooks DavisIf
195*8269e767SBrooks Davis.Dv MAP_FIXED
196*8269e767SBrooks Davisis specified,
197*8269e767SBrooks Davis.Fa addr
198*8269e767SBrooks Davismust be a multiple of the page size.
199*8269e767SBrooks DavisIf
200*8269e767SBrooks Davis.Dv MAP_EXCL
201*8269e767SBrooks Davisis not specified, a successful
202*8269e767SBrooks Davis.Dv MAP_FIXED
203*8269e767SBrooks Davisrequest replaces any previous mappings for the process'
204*8269e767SBrooks Davispages in the range from
205*8269e767SBrooks Davis.Fa addr
206*8269e767SBrooks Davisto
207*8269e767SBrooks Davis.Fa addr
208*8269e767SBrooks Davis+
209*8269e767SBrooks Davis.Fa len .
210*8269e767SBrooks DavisIn contrast, if
211*8269e767SBrooks Davis.Dv MAP_EXCL
212*8269e767SBrooks Davisis specified, the request will fail if a mapping
213*8269e767SBrooks Davisalready exists within the range.
214*8269e767SBrooks Davis.It Dv MAP_GUARD
215*8269e767SBrooks DavisInstead of a mapping, create a guard of the specified size.
216*8269e767SBrooks DavisGuards allow a process to create reservations in its address space,
217*8269e767SBrooks Daviswhich can later be replaced by actual mappings.
218*8269e767SBrooks Davis.Pp
219*8269e767SBrooks Davis.Fa mmap
220*8269e767SBrooks Daviswill not create mappings in the address range of a guard unless
221*8269e767SBrooks Davisthe request specifies
222*8269e767SBrooks Davis.Dv MAP_FIXED .
223*8269e767SBrooks DavisGuards can be destroyed with
224*8269e767SBrooks Davis.Xr munmap 2 .
225*8269e767SBrooks DavisAny memory access by a thread to the guarded range results
226*8269e767SBrooks Davisin the delivery of a
227*8269e767SBrooks Davis.Dv SIGSEGV
228*8269e767SBrooks Davissignal to that thread.
229*8269e767SBrooks Davis.It Dv MAP_NOCORE
230*8269e767SBrooks DavisRegion is not included in a core file.
231*8269e767SBrooks Davis.It Dv MAP_NOSYNC
232*8269e767SBrooks DavisCauses data dirtied via this VM map to be flushed to physical media
233*8269e767SBrooks Davisonly when necessary (usually by the pager) rather than gratuitously.
234*8269e767SBrooks DavisTypically this prevents the update daemons from flushing pages dirtied
235*8269e767SBrooks Davisthrough such maps and thus allows efficient sharing of memory across
236*8269e767SBrooks Davisunassociated processes using a file-backed shared memory map.
237*8269e767SBrooks DavisWithout
238*8269e767SBrooks Davisthis option any VM pages you dirty may be flushed to disk every so often
239*8269e767SBrooks Davis(every 30-60 seconds usually) which can create performance problems if you
240*8269e767SBrooks Davisdo not need that to occur (such as when you are using shared file-backed
241*8269e767SBrooks Davismmap regions for IPC purposes).
242*8269e767SBrooks DavisDirty data will be flushed automatically when all mappings of an object are
243*8269e767SBrooks Davisremoved and all descriptors referencing the object are closed.
244*8269e767SBrooks DavisNote that VM/file system coherency is
245*8269e767SBrooks Davismaintained whether you use
246*8269e767SBrooks Davis.Dv MAP_NOSYNC
247*8269e767SBrooks Davisor not.
248*8269e767SBrooks DavisThis option is not portable
249*8269e767SBrooks Davisacross
250*8269e767SBrooks Davis.Ux
251*8269e767SBrooks Davisplatforms (yet), though some may implement the same behavior
252*8269e767SBrooks Davisby default.
253*8269e767SBrooks Davis.Pp
254*8269e767SBrooks Davis.Em WARNING !
255*8269e767SBrooks DavisExtending a file with
256*8269e767SBrooks Davis.Xr ftruncate 2 ,
257*8269e767SBrooks Davisthus creating a big hole, and then filling the hole by modifying a shared
258*8269e767SBrooks Davis.Fn mmap
259*8269e767SBrooks Daviscan lead to severe file fragmentation.
260*8269e767SBrooks DavisIn order to avoid such fragmentation you should always pre-allocate the
261*8269e767SBrooks Davisfile's backing store by
262*8269e767SBrooks Davis.Fn write Ns ing
263*8269e767SBrooks Daviszero's into the newly extended area prior to modifying the area via your
264*8269e767SBrooks Davis.Fn mmap .
265*8269e767SBrooks DavisThe fragmentation problem is especially sensitive to
266*8269e767SBrooks Davis.Dv MAP_NOSYNC
267*8269e767SBrooks Davispages, because pages may be flushed to disk in a totally random order.
268*8269e767SBrooks Davis.Pp
269*8269e767SBrooks DavisThe same applies when using
270*8269e767SBrooks Davis.Dv MAP_NOSYNC
271*8269e767SBrooks Davisto implement a file-based shared memory store.
272*8269e767SBrooks DavisIt is recommended that you create the backing store by
273*8269e767SBrooks Davis.Fn write Ns ing
274*8269e767SBrooks Daviszero's to the backing file rather than
275*8269e767SBrooks Davis.Fn ftruncate Ns ing
276*8269e767SBrooks Davisit.
277*8269e767SBrooks DavisYou can test file fragmentation by observing the KB/t (kilobytes per
278*8269e767SBrooks Davistransfer) results from an
279*8269e767SBrooks Davis.Dq Li iostat 1
280*8269e767SBrooks Daviswhile reading a large file sequentially, e.g.,\& using
281*8269e767SBrooks Davis.Dq Li dd if=filename of=/dev/null bs=32k .
282*8269e767SBrooks Davis.Pp
283*8269e767SBrooks DavisThe
284*8269e767SBrooks Davis.Xr fsync 2
285*8269e767SBrooks Davissystem call will flush all dirty data and metadata associated with a file,
286*8269e767SBrooks Davisincluding dirty NOSYNC VM data, to physical media.
287*8269e767SBrooks DavisThe
288*8269e767SBrooks Davis.Xr sync 8
289*8269e767SBrooks Daviscommand and
290*8269e767SBrooks Davis.Xr sync 2
291*8269e767SBrooks Davissystem call generally do not flush dirty NOSYNC VM data.
292*8269e767SBrooks DavisThe
293*8269e767SBrooks Davis.Xr msync 2
294*8269e767SBrooks Davissystem call is usually not needed since
295*8269e767SBrooks Davis.Bx
296*8269e767SBrooks Davisimplements a coherent file system buffer cache.
297*8269e767SBrooks DavisHowever, it may be
298*8269e767SBrooks Davisused to associate dirty VM pages with file system buffers and thus cause
299*8269e767SBrooks Davisthem to be flushed to physical media sooner rather than later.
300*8269e767SBrooks Davis.It Dv MAP_PREFAULT_READ
301*8269e767SBrooks DavisImmediately update the calling process's lowest-level virtual address
302*8269e767SBrooks Davistranslation structures, such as its page table, so that every memory
303*8269e767SBrooks Davisresident page within the region is mapped for read access.
304*8269e767SBrooks DavisOrdinarily these structures are updated lazily.
305*8269e767SBrooks DavisThe effect of this option is to eliminate any soft faults that would
306*8269e767SBrooks Davisotherwise occur on the initial read accesses to the region.
307*8269e767SBrooks DavisAlthough this option does not preclude
308*8269e767SBrooks Davis.Fa prot
309*8269e767SBrooks Davisfrom including
310*8269e767SBrooks Davis.Dv PROT_WRITE ,
311*8269e767SBrooks Davisit does not eliminate soft faults on the initial write accesses to the
312*8269e767SBrooks Davisregion.
313*8269e767SBrooks Davis.It Dv MAP_PRIVATE
314*8269e767SBrooks DavisModifications are private.
315*8269e767SBrooks Davis.It Dv MAP_SHARED
316*8269e767SBrooks DavisModifications are shared.
317*8269e767SBrooks Davis.It Dv MAP_STACK
318*8269e767SBrooks DavisCreates both a mapped region that grows downward on demand and an
319*8269e767SBrooks Davisadjoining guard that both reserves address space for the mapped region
320*8269e767SBrooks Davisto grow into and limits the mapped region's growth.
321*8269e767SBrooks DavisTogether, the mapped region and the guard occupy
322*8269e767SBrooks Davis.Fa len
323*8269e767SBrooks Davisbytes of the address space.
324*8269e767SBrooks DavisThe guard starts at the returned address, and the mapped region ends at
325*8269e767SBrooks Davisthe returned address plus
326*8269e767SBrooks Davis.Fa len
327*8269e767SBrooks Davisbytes.
328*8269e767SBrooks DavisUpon access to the guard, the mapped region automatically grows in size,
329*8269e767SBrooks Davisand the guard shrinks by an equal amount.
330*8269e767SBrooks DavisEssentially, the boundary between the guard and the mapped region moves
331*8269e767SBrooks Davisdownward so that the access falls within the enlarged mapped region.
332*8269e767SBrooks DavisHowever, the guard will never shrink to less than the number of pages
333*8269e767SBrooks Davisspecified by the sysctl
334*8269e767SBrooks Davis.Dv security.bsd.stack_guard_page ,
335*8269e767SBrooks Davisthereby ensuring that a gap for detecting stack overflow always exists
336*8269e767SBrooks Davisbetween the downward growing mapped region and the closest mapped region
337*8269e767SBrooks Davisbeneath it.
338*8269e767SBrooks Davis.Pp
339*8269e767SBrooks Davis.Dv MAP_STACK
340*8269e767SBrooks Davisimplies
341*8269e767SBrooks Davis.Dv MAP_ANON
342*8269e767SBrooks Davisand
343*8269e767SBrooks Davis.Fa offset
344*8269e767SBrooks Davisof 0.
345*8269e767SBrooks DavisThe
346*8269e767SBrooks Davis.Fa fd
347*8269e767SBrooks Davisargument
348*8269e767SBrooks Davismust be -1 and
349*8269e767SBrooks Davis.Fa prot
350*8269e767SBrooks Davismust include at least
351*8269e767SBrooks Davis.Dv PROT_READ
352*8269e767SBrooks Davisand
353*8269e767SBrooks Davis.Dv PROT_WRITE .
354*8269e767SBrooks DavisThe size of the guard, in pages, is specified by sysctl
355*8269e767SBrooks Davis.Dv security.bsd.stack_guard_page .
356*8269e767SBrooks Davis.El
357*8269e767SBrooks Davis.Pp
358*8269e767SBrooks DavisThe
359*8269e767SBrooks Davis.Xr close 2
360*8269e767SBrooks Davissystem call does not unmap pages, see
361*8269e767SBrooks Davis.Xr munmap 2
362*8269e767SBrooks Davisfor further information.
363*8269e767SBrooks Davis.Sh NOTES
364*8269e767SBrooks DavisAlthough this implementation does not impose any alignment restrictions on
365*8269e767SBrooks Davisthe
366*8269e767SBrooks Davis.Fa offset
367*8269e767SBrooks Davisargument, a portable program must only use page-aligned values.
368*8269e767SBrooks Davis.Pp
369*8269e767SBrooks DavisLarge page mappings require that the pages backing an object be
370*8269e767SBrooks Davisaligned in matching blocks in both the virtual address space and RAM.
371*8269e767SBrooks DavisThe system will automatically attempt to use large page mappings when
372*8269e767SBrooks Davismapping an object that is already backed by large pages in RAM by
373*8269e767SBrooks Davisaligning the mapping request in the virtual address space to match the
374*8269e767SBrooks Davisalignment of the large physical pages.
375*8269e767SBrooks DavisThe system may also use large page mappings when mapping portions of an
376*8269e767SBrooks Davisobject that are not yet backed by pages in RAM.
377*8269e767SBrooks DavisThe
378*8269e767SBrooks Davis.Dv MAP_ALIGNED_SUPER
379*8269e767SBrooks Davisflag is an optimization that will align the mapping request to the
380*8269e767SBrooks Davissize of a large page similar to
381*8269e767SBrooks Davis.Dv MAP_ALIGNED ,
382*8269e767SBrooks Davisexcept that the system will override this alignment if an object already
383*8269e767SBrooks Davisuses large pages so that the mapping will be consistent with the existing
384*8269e767SBrooks Davislarge pages.
385*8269e767SBrooks DavisThis flag is mostly useful for maximizing the use of large pages on the
386*8269e767SBrooks Davisfirst mapping of objects that do not yet have pages present in RAM.
387*8269e767SBrooks Davis.Sh RETURN VALUES
388*8269e767SBrooks DavisUpon successful completion,
389*8269e767SBrooks Davis.Fn mmap
390*8269e767SBrooks Davisreturns a pointer to the mapped region.
391*8269e767SBrooks DavisOtherwise, a value of
392*8269e767SBrooks Davis.Dv MAP_FAILED
393*8269e767SBrooks Davisis returned and
394*8269e767SBrooks Davis.Va errno
395*8269e767SBrooks Davisis set to indicate the error.
396*8269e767SBrooks Davis.Sh ERRORS
397*8269e767SBrooks DavisThe
398*8269e767SBrooks Davis.Fn mmap
399*8269e767SBrooks Davissystem call
400*8269e767SBrooks Daviswill fail if:
401*8269e767SBrooks Davis.Bl -tag -width Er
402*8269e767SBrooks Davis.It Bq Er EACCES
403*8269e767SBrooks DavisThe flag
404*8269e767SBrooks Davis.Dv PROT_READ
405*8269e767SBrooks Daviswas specified as part of the
406*8269e767SBrooks Davis.Fa prot
407*8269e767SBrooks Davisargument and
408*8269e767SBrooks Davis.Fa fd
409*8269e767SBrooks Daviswas not open for reading.
410*8269e767SBrooks DavisThe flags
411*8269e767SBrooks Davis.Dv MAP_SHARED
412*8269e767SBrooks Davisand
413*8269e767SBrooks Davis.Dv PROT_WRITE
414*8269e767SBrooks Daviswere specified as part of the
415*8269e767SBrooks Davis.Fa flags
416*8269e767SBrooks Davisand
417*8269e767SBrooks Davis.Fa prot
418*8269e767SBrooks Davisargument and
419*8269e767SBrooks Davis.Fa fd
420*8269e767SBrooks Daviswas not open for writing.
421*8269e767SBrooks Davis.It Bq Er EBADF
422*8269e767SBrooks DavisThe
423*8269e767SBrooks Davis.Fa fd
424*8269e767SBrooks Davisargument
425*8269e767SBrooks Davisis not a valid open file descriptor.
426*8269e767SBrooks Davis.It Bq Er EINVAL
427*8269e767SBrooks DavisAn invalid (negative) value was passed in the
428*8269e767SBrooks Davis.Fa offset
429*8269e767SBrooks Davisargument, when
430*8269e767SBrooks Davis.Fa fd
431*8269e767SBrooks Davisreferenced a regular file or shared memory.
432*8269e767SBrooks Davis.It Bq Er EINVAL
433*8269e767SBrooks DavisAn invalid value was passed in the
434*8269e767SBrooks Davis.Fa prot
435*8269e767SBrooks Davisargument.
436*8269e767SBrooks Davis.It Bq Er EINVAL
437*8269e767SBrooks DavisAn undefined option was set in the
438*8269e767SBrooks Davis.Fa flags
439*8269e767SBrooks Davisargument.
440*8269e767SBrooks Davis.It Bq Er EINVAL
441*8269e767SBrooks DavisBoth
442*8269e767SBrooks Davis.Dv MAP_PRIVATE
443*8269e767SBrooks Davisand
444*8269e767SBrooks Davis.Dv MAP_SHARED
445*8269e767SBrooks Daviswere specified.
446*8269e767SBrooks Davis.It Bq Er EINVAL
447*8269e767SBrooks DavisNone of
448*8269e767SBrooks Davis.Dv MAP_ANON ,
449*8269e767SBrooks Davis.Dv MAP_GUARD ,
450*8269e767SBrooks Davis.Dv MAP_PRIVATE ,
451*8269e767SBrooks Davis.Dv MAP_SHARED ,
452*8269e767SBrooks Davisor
453*8269e767SBrooks Davis.Dv MAP_STACK
454*8269e767SBrooks Daviswas specified.
455*8269e767SBrooks DavisAt least one of these flags must be included.
456*8269e767SBrooks Davis.It Bq Er EINVAL
457*8269e767SBrooks Davis.Dv MAP_STACK
458*8269e767SBrooks Daviswas specified and
459*8269e767SBrooks Davis.Va len
460*8269e767SBrooks Davisis less than or equal to the guard size.
461*8269e767SBrooks Davis.It Bq Er EINVAL
462*8269e767SBrooks Davis.Dv MAP_FIXED
463*8269e767SBrooks Daviswas specified and the
464*8269e767SBrooks Davis.Fa addr
465*8269e767SBrooks Davisargument was not page aligned, or part of the desired address space
466*8269e767SBrooks Davisresides out of the valid address space for a user process.
467*8269e767SBrooks Davis.It Bq Er EINVAL
468*8269e767SBrooks DavisBoth
469*8269e767SBrooks Davis.Dv MAP_FIXED
470*8269e767SBrooks Davisand
471*8269e767SBrooks Davis.Dv MAP_32BIT
472*8269e767SBrooks Daviswere specified and part of the desired address space resides outside
473*8269e767SBrooks Davisof the first 2GB of user address space.
474*8269e767SBrooks Davis.It Bq Er EINVAL
475*8269e767SBrooks DavisThe
476*8269e767SBrooks Davis.Fa len
477*8269e767SBrooks Davisargument
478*8269e767SBrooks Daviswas equal to zero.
479*8269e767SBrooks Davis.It Bq Er EINVAL
480*8269e767SBrooks Davis.Dv MAP_ALIGNED
481*8269e767SBrooks Daviswas specified and the desired alignment was either larger than the
482*8269e767SBrooks Davisvirtual address size of the machine or smaller than a page.
483*8269e767SBrooks Davis.It Bq Er EINVAL
484*8269e767SBrooks Davis.Dv MAP_ANON
485*8269e767SBrooks Daviswas specified and the
486*8269e767SBrooks Davis.Fa fd
487*8269e767SBrooks Davisargument was not -1.
488*8269e767SBrooks Davis.It Bq Er EINVAL
489*8269e767SBrooks Davis.Dv MAP_ANON
490*8269e767SBrooks Daviswas specified and the
491*8269e767SBrooks Davis.Fa offset
492*8269e767SBrooks Davisargument was not 0.
493*8269e767SBrooks Davis.It Bq Er EINVAL
494*8269e767SBrooks DavisBoth
495*8269e767SBrooks Davis.Dv MAP_FIXED
496*8269e767SBrooks Davisand
497*8269e767SBrooks Davis.Dv MAP_EXCL
498*8269e767SBrooks Daviswere specified, but the requested region is already used by a mapping.
499*8269e767SBrooks Davis.It Bq Er EINVAL
500*8269e767SBrooks Davis.Dv MAP_EXCL
501*8269e767SBrooks Daviswas specified, but
502*8269e767SBrooks Davis.Dv MAP_FIXED
503*8269e767SBrooks Daviswas not.
504*8269e767SBrooks Davis.It Bq Er EINVAL
505*8269e767SBrooks Davis.Dv MAP_GUARD
506*8269e767SBrooks Daviswas specified, but the
507*8269e767SBrooks Davis.Fa offset
508*8269e767SBrooks Davisargument was not zero, the
509*8269e767SBrooks Davis.Fa fd
510*8269e767SBrooks Davisargument was not -1, or the
511*8269e767SBrooks Davis.Fa prot
512*8269e767SBrooks Davisargument was not
513*8269e767SBrooks Davis.Dv PROT_NONE .
514*8269e767SBrooks Davis.It Bq Er EINVAL
515*8269e767SBrooks Davis.Dv MAP_GUARD
516*8269e767SBrooks Daviswas specified together with one of the flags
517*8269e767SBrooks Davis.Dv MAP_ANON ,
518*8269e767SBrooks Davis.Dv MAP_PREFAULT ,
519*8269e767SBrooks Davis.Dv MAP_PREFAULT_READ ,
520*8269e767SBrooks Davis.Dv MAP_PRIVATE ,
521*8269e767SBrooks Davis.Dv MAP_SHARED ,
522*8269e767SBrooks Davis.Dv MAP_STACK .
523*8269e767SBrooks Davis.It Bq Er ENODEV
524*8269e767SBrooks Davis.Dv MAP_ANON
525*8269e767SBrooks Davishas not been specified and
526*8269e767SBrooks Davis.Fa fd
527*8269e767SBrooks Davisdid not reference a regular or character special file.
528*8269e767SBrooks Davis.It Bq Er ENOMEM
529*8269e767SBrooks Davis.Dv MAP_FIXED
530*8269e767SBrooks Daviswas specified and the
531*8269e767SBrooks Davis.Fa addr
532*8269e767SBrooks Davisargument was not available.
533*8269e767SBrooks Davis.Dv MAP_ANON
534*8269e767SBrooks Daviswas specified and insufficient memory was available.
535*8269e767SBrooks Davis.It Bq Er ENOTSUP
536*8269e767SBrooks DavisThe
537*8269e767SBrooks Davis.Fa prot
538*8269e767SBrooks Davisargument contains protections which are not a subset of the specified
539*8269e767SBrooks Davismaximum protections.
540*8269e767SBrooks Davis.El
541*8269e767SBrooks Davis.Sh SEE ALSO
542*8269e767SBrooks Davis.Xr madvise 2 ,
543*8269e767SBrooks Davis.Xr mincore 2 ,
544*8269e767SBrooks Davis.Xr minherit 2 ,
545*8269e767SBrooks Davis.Xr mlock 2 ,
546*8269e767SBrooks Davis.Xr mprotect 2 ,
547*8269e767SBrooks Davis.Xr msync 2 ,
548*8269e767SBrooks Davis.Xr munlock 2 ,
549*8269e767SBrooks Davis.Xr munmap 2 ,
550*8269e767SBrooks Davis.Xr getpagesize 3 ,
551*8269e767SBrooks Davis.Xr getpagesizes 3
552*8269e767SBrooks Davis.Sh HISTORY
553*8269e767SBrooks DavisThe
554*8269e767SBrooks Davis.Nm
555*8269e767SBrooks Davissystem call was first documented in
556*8269e767SBrooks Davis.Bx 4.2
557*8269e767SBrooks Davisand implemented in
558*8269e767SBrooks Davis.Bx 4.4 .
559*8269e767SBrooks Davis.\" XXX: lots of missing history of FreeBSD additions.
560*8269e767SBrooks Davis.Pp
561*8269e767SBrooks DavisThe
562*8269e767SBrooks Davis.Dv PROT_MAX
563*8269e767SBrooks Davisfunctionality was introduced in
564*8269e767SBrooks Davis.Fx 13 .
565