1.\" $NetBSD: mmap.2,v 1.52 2018/05/02 16:00:20 christos 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 May 2, 2018 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.Fa mmap 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_FILE 172Mapped from a regular file or character-special device memory. 173Read accesses beyond the end of of the file or device but less 174than the current page size will be zero-filled. 175Write accesses beyond the end of the file or device but less 176than the current page size will not affect the file or device. 177References beyond the end of file that are beyond the current 178page size will result in the delivery of 179.Dv SIGBUS 180signal. 181.It Dv MAP_FIXED 182Do not permit the system to select a different address than the one 183specified. 184If the specified address cannot be used, 185.Nm mmap 186will fail. 187If MAP_FIXED is specified, 188.Fa addr 189must be a multiple of the pagesize. 190Use of this option is discouraged. 191.It Dv MAP_HASSEMAPHORE 192Notify the kernel that the region may contain semaphores and that special 193handling may be necessary. 194.It Dv MAP_INHERIT 195Permit regions to be inherited across 196.Xr execve 2 197system calls. 198.It Dv MAP_NORESERVE 199Only reserve address space, but do not reserve swap space or any other 200resources for this mapping. 201Access to the address space is not guaranteed and may result in a segmentation 202violation. 203Unimplemented. 204.It Dv MAP_REMAPDUP 205Only valid for 206.Xr mremap 2 . 207.It Dv MAP_RENAME 208Assign the referenced private pages to the file descriptor provided. 209Unimplemented. 210.It Dv MAP_STACK 211Allocate a memory segment that can be used either for a process or thread stack. 212This currently has no effect, but its use is reserved for architectures 213that might require special treatment of that address space. 214Unimplemented. 215.It Dv MAP_TRYFIXED 216Attempt to use the address 217.Fa addr 218even if it falls within the normally protected process data or 219text segment memory regions. 220If the requested region of memory 221is actually present in the memory map, a different address will 222be selected as if 223.Dv MAP_TRYFIXED 224had not been specified. 225If 226.Fa addr 227is 228.Fa NULL , 229this flag is ignored and the system will select a mapping address. 230.It Dv MAP_WIRED 231Lock the mapped region into memory as with 232.Xr mlock 2 . 233.It Dv MAP_PRIVATE 234Modifications made by this process are private, however modifications made by 235other processes using 236.Dv MAP_SHARED 237will be seen. 238.It Dv MAP_SHARED 239Modifications are shared. 240.El 241.Pp 242The 243.Xr close 2 244function does not unmap pages, see 245.Xr munmap 2 246for further information. 247.Pp 248The current design does not allow a process to specify the location of 249swap space. 250In the future we may define an additional mapping type, 251.Dv MAP_SWAP , 252in which 253the file descriptor argument specifies a file or device to which swapping 254should be done. 255.Pp 256If 257.Dv MAP_FIXED 258is not specified, the system will attempt to place the mapping in an 259unused portion of the address space chosen to minimize possible 260collision between mapped regions and the heap. 261.Sh RETURN VALUES 262Upon successful completion, 263.Nm mmap 264returns a pointer to the mapped region. 265Otherwise, a value of 266.Dv MAP_FAILED 267is returned and 268.Va errno 269is set to indicate the error. 270The symbol 271.Dv MAP_FAILED 272is defined in the header 273.Ao Pa sys/mman.h Ac . 274No successful return from 275.Fn mmap 276will return the value 277.Dv MAP_FAILED . 278.Sh ERRORS 279.Fn mmap 280will fail if: 281.Bl -tag -width Er 282.It Bq Er EACCES 283The flag 284.Dv PROT_READ 285was specified as part of the 286.Fa prot 287parameter and 288.Fa fd 289was not open for reading. 290.Pp 291The flags 292.Dv MAP_SHARED 293and 294.Dv PROT_WRITE 295were specified as part of the 296.Fa flags 297and 298.Fa prot 299parameters and 300.Fa fd 301was not open for writing. 302.Pp 303PAX mprotect restrictions prohibit the requested protection. 304.It Bq Er EBADF 305.Fa fd 306is not a valid open file descriptor. 307.It Bq Er EINVAL 308.\"One of 309.\".Dv MAP_ANON 310.\"or 311.\".Dv MAP_FILE 312.\"was not specified as part of the 313.\".Fa flags 314.\"parameter. 315.Dv MAP_FIXED 316was specified and the 317.Fa addr 318parameter was not page aligned or was outside of the 319valid address range for a process. 320.Pp 321.Dv MAP_ANON was specified and 322.Fa fd 323was not \-1. 324.It Bq Er ENODEV 325.Fa fd 326did not reference a regular or character special file. 327.It Bq Er ENOMEM 328.Dv MAP_FIXED 329was specified and the 330.Fa addr 331parameter wasn't available. 332.Pp 333.Dv MAP_ANON 334was specified and insufficient memory was available. 335.It Bq Er EOVERFLOW 336.Fa fd 337references a regular file and the value of 338.Fa offset 339plus 340.Fa len 341would exceed the offset maximum established in its open file description. 342.El 343.Sh SEE ALSO 344.Xr madvise 2 , 345.Xr mincore 2 , 346.Xr mlock 2 , 347.Xr mprotect 2 , 348.Xr mremap 2 , 349.Xr msync 2 , 350.Xr munmap 2 , 351.Xr getpagesize 3 , 352.Xr sysconf 3 353.Sh STANDARDS 354The 355.Fn mmap 356function conforms to 357.St -p1003.1b-93 . 358.Sh HISTORY 359The 360.Fn mmap 361interface was first designed in 362.Bx 4.2 . 363