1.\" $OpenBSD: mmap.2,v 1.38 2011/04/11 17:46:19 tedu Exp $ 2.\" $NetBSD: mmap.2,v 1.5 1995/06/24 10:48:59 cgd Exp $ 3.\" 4.\" Copyright (c) 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)mmap.2 8.1 (Berkeley) 6/4/93 32.\" 33.Dd $Mdocdate: April 11 2011 $ 34.Dt MMAP 2 35.Os 36.Sh NAME 37.Nm mmap 38.Nd map files or devices into memory 39.Sh SYNOPSIS 40.Fd #include <sys/types.h> 41.Fd #include <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 offset 57or 58.Fa len 59is not a multiple of the pagesize, the mapped region may extend past the 60specified range. 61.Pp 62If 63.Fa addr 64is non-zero, it is used as a hint to the system. 65(As a convenience to the system, the actual address of the region may differ 66from the address supplied.) 67If 68.Fa addr 69is zero, an address will be selected by the system. 70The actual starting address of the region is returned. 71A successful 72.Fa mmap 73deletes any previous mapping in the allocated address range. 74.Pp 75The protections (region accessibility) are specified in the 76.Fa prot 77argument by OR'ing the following values: 78.Pp 79.Bl -tag -width "PROT_WRITE " -offset indent -compact 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 87No permissions. 88.El 89.Pp 90The 91.Fa flags 92parameter specifies the type of the mapped object, mapping options, and 93whether modifications made to the mapped copy of the page are private 94to the process or are to be shared with other references. 95Sharing, mapping type, and options are specified in the 96.Fa flags 97argument by OR'ing the following values: 98.Bl -tag -width MAP_FIXEDX 99.It Dv MAP_ANON 100Map anonymous memory not associated with any specific file. 101The file descriptor used for creating 102.Dv MAP_ANON 103must currently be \-1 indicating no name is associated with the 104region. 105.It Dv MAP_FILE 106Mapped from a regular file or character-special device memory. 107(This is the default mapping type, and need not be specified.) 108.It Dv MAP_FIXED 109Do not permit the system to select a different address than the one 110specified. 111If the specified address cannot be used, 112.Nm mmap 113will fail. 114If 115.Dv MAP_FIXED 116is specified, 117.Fa addr 118must be a multiple of the pagesize. 119Use of this option is discouraged. 120.It Dv MAP_HASSEMAPHORE 121Notify the kernel that the region may contain semaphores and that special 122handling may be necessary. 123.It Dv MAP_INHERIT 124Permit regions to be inherited across 125.Xr exec 3 126system calls. 127.It Dv MAP_PRIVATE 128Modifications are private. 129.It Dv MAP_SHARED 130Modifications are shared. 131.It Dv MAP_TRYFIXED 132Attempt to use the hint provided by 133.Fa addr 134even if the kernel would normally prefer a different address, but do not 135fail if the address is not available. 136This option is provided for compatibility with other operating systems 137and its use in 138.Ox 139is discouraged. 140.It Dv MAP_COPY 141Modifications are private and, unlike 142.Dv MAP_PRIVATE , 143modifications made by others are not visible. 144This option is deprecated, shouldn't be used and behaves just like 145.Dv MAP_PRIVATE 146in the current implementation. 147.El 148.Pp 149The 150.Xr close 2 151function does not unmap pages; see 152.Xr munmap 2 153for further information. 154.Pp 155The current design does not allow a process to specify the location of 156swap space. 157In the future we may define an additional mapping type, 158.Dv MAP_SWAP , 159in which 160the file descriptor argument specifies a file or device to which swapping 161should be done. 162.Sh RETURN VALUES 163Upon successful completion, 164.Nm mmap 165returns a pointer to the mapped region. 166Otherwise, a value of 167.Dv MAP_FAILED 168is returned and 169.Va errno 170is set to indicate the error. 171The symbol 172.Dv MAP_FAILED 173is defined in the header 174.Ao Pa sys/mman.h Ac . 175No successful return from 176.Fn mmap 177will return the value 178.Dv MAP_FAILED . 179.Sh ERRORS 180.Fn mmap 181will fail if: 182.Bl -tag -width Er 183.It Bq Er EACCES 184The flag 185.Dv PROT_READ 186was specified as part of the 187.Fa prot 188parameter and 189.Fa fd 190was not open for reading. 191The flags 192.Dv MAP_SHARED 193and 194.Dv PROT_WRITE 195were specified as part 196of the 197.Fa flags 198and 199.Fa prot 200parameters and 201.Fa fd 202was not open for writing. 203.It Bq Er EBADF 204.Fa fd 205is not a valid open file descriptor. 206.It Bq Er EINVAL 207.Dv MAP_FIXED 208was specified and the 209.Fa addr 210parameter was not page aligned or 211.Fa addr 212and 213.Fa size 214specify a region that would extend beyond the end of the address space. 215.Fa fd 216did not reference a regular or character special file. 217.It Bq Er ENOMEM 218.Dv MAP_FIXED 219was specified and the 220.Fa addr 221parameter wasn't available. 222.Dv MAP_ANON 223was specified and insufficient memory was available. 224.El 225.Sh SEE ALSO 226.Xr madvise 2 , 227.Xr mincore 2 , 228.Xr mlock 2 , 229.Xr mprotect 2 , 230.Xr mquery 2 , 231.Xr msync 2 , 232.Xr munmap 2 , 233.Xr getpagesize 3 234.Sh BUGS 235Due to a limitation of the current vm system (see 236.Xr uvm 9 ) , 237mapping descriptors 238.Dv PROT_WRITE 239without also specifying 240.Dv PROT_READ 241is useless 242(results in a segmentation fault when first accessing the mapping). 243This means that such descriptors must be opened with 244.Dv O_RDWR , 245which requires both read and write permissions on the underlying 246object. 247