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