Системный вызов linux mmap

Системный вызов linux mmap

If Fa addr is non-zero, it is used as a hint to the system. (As a convenience to the system, the actual address of the region may differ from the address supplied.) If Fa addr is zero, an address will be selected by the system. The actual starting address of the region is returned. A successful Fa mmap deletes any previous mapping in the allocated address range.

The protections (region accessibility) are specified in the Fa prot argument by or ‘ing the following values:

PROT_NONE Pages may not be accessed. PROT_READ Pages may be read. PROT_WRITE Pages may be written. PROT_EXEC Pages may be executed.

The Fa flags argument specifies the type of the mapped object, mapping options and whether modifications made to the mapped copy of the page are private to the process or are to be shared with other references. Sharing, mapping type and options are specified in the Fa flags argument by or ‘ing the following values:

MAP_ANON Map anonymous memory not associated with any specific file. The file descriptor used for creating MAP_ANON must be -1. The Fa offset argument is ignored. MAP_FIXED Do not permit the system to select a different address than the one specified. If the specified address cannot be used, mmap ();
will fail. If MAP_FIXED is specified, Fa addr must be a multiple of the pagesize. If a MAP_FIXED request is successful, the mapping established by mmap ();
replaces any previous mappings for the process’ pages in the range from Fa addr to Fa addr + Fa len . Use of this option is discouraged. MAP_HASSEMAPHORE Notify the kernel that the region may contain semaphores and that special handling may be necessary. MAP_INHERIT This flag never operated as advertised and is no longer supported. Please refer to minherit(2) for further information. MAP_NOCORE Region is not included in a core file. MAP_NOSYNC Causes data dirtied via this VM map to be flushed to physical media only when necessary (usually by the pager) rather than gratuitously. Typically this prevents the update daemons from flushing pages dirtied through such maps and thus allows efficient sharing of memory across unassociated processes using a file-backed shared memory map. Without this option any VM pages you dirty may be flushed to disk every so often (every 30-60 seconds usually) which can create performance problems if you do not need that to occur (such as when you are using shared file-backed mmap regions for IPC purposes). Note that VM/file system coherency is maintained whether you use MAP_NOSYNC or not. This option is not portable across UNIX platforms (yet), though some may implement the same behavior by default.

WARNING Extending a file with ftruncate(2), thus creating a big hole, and then filling the hole by modifying a shared mmap ();
can lead to severe file fragmentation. In order to avoid such fragmentation you should always pre-allocate the file’s backing store by write (Ns ing);
zero’s into the newly extended area prior to modifying the area via your mmap (.);
The fragmentation problem is especially sensitive to MAP_NOSYNC pages, because pages may be flushed to disk in a totally random order.

The same applies when using MAP_NOSYNC to implement a file-based shared memory store. It is recommended that you create the backing store by write (Ns ing);
zero’s to the backing file rather than ftruncate (Ns ing);
it. You can test file fragmentation by observing the KB/t (kilobytes per transfer) results from an « iostat 1 » while reading a large file sequentially, e.g. using « dd if=filename of=/dev/null bs=32k »

The fsync(2) system call will flush all dirty data and metadata associated with a file, including dirty NOSYNC VM data, to physical media. The sync(8) command and sync(2) system call generally do not flush dirty NOSYNC VM data. The msync(2) system call is obsolete since BSD implements a coherent file system buffer cache. However, it may be used to associate dirty VM pages with file system buffers and thus cause them to be flushed to physical media sooner rather than later. MAP_PRIVATE Modifications are private. MAP_SHARED Modifications are shared. MAP_STACK MAP_STACK implies MAP_ANON and Fa offset of 0. The Fa fd argument must be -1 and Fa prot must include at least PROT_READ and PROT_WRITE This option creates a memory region that grows to at most Fa len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus Fa len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.

Читайте также:  Эмулятор андроид для windows рейтинг

The close(2) system call does not unmap pages, see munmap(2) for further information.

The current design does not allow a process to specify the location of swap space. In the future we may define an additional mapping type, MAP_SWAP in which the file descriptor argument specifies a file or device to which swapping should be done.

RETURN VALUES


ERRORS

Bq Er EACCES The flag PROT_READ was specified as part of the Fa prot argument and Fa fd was not open for reading. The flags MAP_SHARED and PROT_WRITE were specified as part of the Fa flags and Fa prot argument and Fa fd was not open for writing. Bq Er EBADF The Fa fd argument is not a valid open file descriptor. Bq Er EINVAL MAP_FIXED was specified and the Fa addr argument was not page aligned, or part of the desired address space resides out of the valid address space for a user process. Bq Er EINVAL The Fa len argument was negative. Bq Er EINVAL MAP_ANON was specified and the Fa fd argument was not -1. The Fa offset argument was not page-aligned. (See Sx BUGS below.) Bq Er ENODEV MAP_ANON has not been specified and Fa fd did not reference a regular or character special file. Bq Er ENOMEM MAP_FIXED was specified and the Fa addr argument was not available. MAP_ANON was specified and insufficient memory was available. The system has reached the per-process mmap limit specified in the vm.max_proc_mmap sysctl.

SEE ALSO

The previous documented limit of 2GB was a documentation bug. That limit has not existed since Fx 2.2 .

Note that an attempt to mmap ();
zero bytes has no effect and succeeds, while an attempt to munmap ();
zero bytes will return Bq Er EINVAL .

Источник

Системный вызов linux mmap

If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the mapping will be created at the next higher page boundary. The address of the new mapping is returned as the result of the call.

The contents of a file mapping (as opposed to an anonymous mapping; see MAP_ANONYMOUS below), are initialized using length bytes starting at offset offset in the file (or other object) referred to by the file descriptor fd . offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE) .

The prot argument describes the desired memory protection of the mapping (and must not conflict with the open mode of the file). It is either PROT_NONE or the bitwise OR of one or more of the following flags: PROT_EXEC Pages may be executed. PROT_READ Pages may be read. PROT_WRITE Pages may be written. PROT_NONE Pages may not be accessed.

The flags argument determines whether updates to the mapping are visible to other processes mapping the same region, and whether updates are carried through to the underlying file. This behavior is determined by including exactly one of the following values in flags : MAP_SHARED Share this mapping. Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file. The file may not actually be updated until msync (2) or munmap () is called. MAP_PRIVATE Create a private copy-on-write mapping. Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file. It is unspecified whether changes made to the file after the mmap () call are visible in the mapped region.

Both of these flags are described in POSIX.1-2001.

In addition, zero or more of the following values can be ORed in flags : MAP_32BIT (since Linux 2.4.20, 2.6) Put the mapping into the first 2 Gigabytes of the process address space. This flag is only supported on x86-64, for 64-bit programs. It was added to allow thread stacks to be allocated somewhere in the first 2GB of memory, so as to improve context-switch performance on some early 64-bit processors. Modern x86-64 processors no longer have this performance problem, so use of this flag is not required on those systems. The MAP_32BIT flag is ignored when MAP_FIXED is set. MAP_ANON Synonym for MAP_ANONYMOUS . Deprecated. MAP_ANONYMOUS The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON ) is specified, and portable applications should ensure this. The use of MAP_ANONYMOUS in conjunction with MAP_SHARED is only supported on Linux since kernel 2.4. MAP_DENYWRITE This flag is ignored. (Long ago, it signaled that attempts to write to the underlying file should fail with ETXTBUSY . But this was a source of denial-of-service attacks.) MAP_EXECUTABLE This flag is ignored. MAP_FILE Compatibility flag. Ignored. MAP_FIXED Don’t interpret addr as a hint: place the mapping at exactly that address. addr must be a multiple of the page size. If the memory region specified by addr and len overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded. If the specified address cannot be used, mmap () will fail. Because requiring a fixed address for a mapping is less portable, the use of this option is discouraged. MAP_GROWSDOWN Used for stacks. Indicates to the kernel virtual memory system that the mapping should extend downwards in memory. MAP_LOCKED (since Linux 2.5.37) Lock the pages of the mapped region into memory in the manner of mlock (2). This flag is ignored in older kernels. MAP_NONBLOCK (since Linux 2.5.46) Only meaningful in conjunction with MAP_POPULATE . Don’t perform read-ahead: only create page tables entries for pages that are already present in RAM. Since Linux 2.6.23, this flag causes MAP_POPULATE to do nothing. One day the combination of MAP_POPULATE and MAP_NONBLOCK may be re-implemented. MAP_NORESERVE Do not reserve swap space for this mapping. When swap space is reserved, one has the guarantee that it is possible to modify the mapping. When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available. See also the discussion of the file /proc/sys/vm/overcommit_memory in proc (5). In kernels before 2.6, this flag only had effect for private writable mappings. MAP_POPULATE (since Linux 2.5.46) Populate (prefault) page tables for a mapping. For a file mapping, this causes read-ahead on the file. Later accesses to the mapping will not be blocked by page faults. MAP_POPULATE is only supported for private mappings since Linux 2.6.23.

Читайте также:  Find all files in folder linux

Of the above flags, only MAP_FIXED is specified in POSIX.1-2001. However, most systems also support MAP_ANONYMOUS (or its synonym MAP_ANON ). MAP_STACK (since Linux 2.6.27) Allocate the mapping at an address suitable for a process or thread stack. This flag is currently a no-op, but is used in the glibc threading implementation so that if some architectures require special treatment for stack allocations, support can later be transparently implemented for glibc.

Some systems document the additional flags MAP_AUTOGROW , MAP_AUTORESRV , MAP_COPY , and MAP_LOCAL .

Memory mapped by mmap () is preserved across fork (2), with the same attributes.

A file is mapped in multiples of the page size. For a file that is not a multiple of the page size, the remaining memory is zeroed when mapped, and writes to that region are not written out to the file. The effect of changing the size of the underlying file of a mapping on the pages that correspond to added or removed regions of the file is unspecified.

munmap()

The address addr must be a multiple of the page size. All pages containing a part of the indicated range are unmapped, and subsequent references to these pages will generate SIGSEGV . It is not an error if the indicated range does not contain any mapped pages.

Timestamps changes for file-backed mappings

The st_ctime and st_mtime field for a file mapped with PROT_WRITE and MAP_SHARED will be updated after a write to the mapped region, and before a subsequent msync (2) with the MS_SYNC or MS_ASYNC flag, if one occurs.

RETURN VALUE


ERRORS

Use of a mapped region can result in these signals: SIGSEGV Attempted write into a region mapped as read-only. SIGBUS Attempted access to a portion of the buffer that does not correspond to the file (for example, beyond the end of the file, including the case where another process has truncated the file).

CONFORMING TO


AVAILABILITY


NOTES

On some hardware architectures (e.g., i386), PROT_WRITE implies PROT_READ . It is architecture dependent whether PROT_READ implies PROT_EXEC or not. Portable programs should always set PROT_EXEC if they intend to execute code in the new mapping.

The portable way to create a mapping is to specify addr as 0 (NULL), and omit MAP_FIXED from flags . In this case, the system chooses the address for the mapping; the address is chosen so as not to conflict with any existing mapping, and will not be 0. If the MAP_FIXED flag is specified, and addr is 0 (NULL), then the mapped address will be 0 (NULL).

In kernels before 2.6.7, the MAP_POPULATE flag only has effect if prot is specified as PROT_NONE .

SUSv3 specifies that mmap () should fail if length is 0. However, in kernels before 2.6.12, mmap () succeeded in this case: no mapping was created and the call returned addr . Since kernel 2.6.12, mmap () fails with the error EINVAL for this case.

EXAMPLE

The following program prints part of the file specified in its first command-line argument to standard output. The range of bytes to be printed is specified via offset and length values in the second and third command-line arguments. The program creates a memory mapping of the required pages of the file and then uses write (2) to output the desired bytes.

Читайте также:  Usb keyboard driver linux

Источник

Системный вызов linux mmap

void * mmap(void * start , size_t length , int prot , int flags , int fd , off_t offset );

int munmap(void * start , size_t length );

ОПИСАНИЕ

Аргумент prot описывает желаемый режим защиты памяти (он не должен конфликтовать с режимом открытия файла). Оно является либо PROT_NONE либо побитовым ИЛИ одного или нескольких флагов PROT_*. PROT_EXEC (данные в страницах могут исполняться); PROT_READ (данные можно читать); PROT_WRITE (в эту область можно записывать информацию); PROT_NONE (доступ к этой области памяти запрещен).

Параметр flags задает тип отражаемого объекта, опции отражения и указывает, принадлежат ли отраженные данные только этому процессу или их могут читать другие. Он состоит из комбинации следующих битов: MAP_FIXED Не использовать другой адрес, если адрес задан в параметрах функции. Если заданный адрес не может быть использован, то функция mmap вернет сообщение об ошибке. Если используется MAP_FIXED, то start должен быть пропорционален размеру страницы. Использование этой опции не рекомендуется. MAP_SHARED Разделить использование этого отражения с другими процессами, отражающими тот же объект. Запись информации в эту область памяти будет эквивалентна записи в файл. Файл может не обновляться до вызова функций msync (2) или munmap (2) . MAP_PRIVATE Создать неразделяемое отражение с механизмом copy-on-write. Запись в эту область памяти не влияет на файл. Не определено, являются или нет изменения в файле после вызова mmap видимыми в отраженном диапазоне.

Вы должны задать либо MAP_SHARED, либо MAP_PRIVATE.

Эти три флага описаны в POSIX.1b (бывшем POSIX.4) and SUSv2. В Linux также анализируются следующие нестандартные флаги: MAP_DENYWRITE Этот флаг игнорируется. (Раньше он обозначал, что попытки записи в подчиненные файлы должны завершаться с кодом ошибки ETXTBUSY. Но это стало основой для атак типа ‘отказ-в-доступе’ — ‘denial-of-service’.) MAP_EXECUTABLE Этот флаг игнорируется. MAP_NORESERVE (Используется вместе с MAP_PRIVATE.) Не выделяет страницы пространства подкачки для этого отображения. Если пространство подкачки выделяется, то это частное пространство копирования-при-записи может быть изменено. Если оно не выделено, то можно получить SIGSEGV при записи и отсутствии доступной памяти. MAP_LOCKED (Linux 2.5.37 и выше) Блокировать страницу или размеченную область в памяти так, как это делает mlock() . Этот флаг игнорируется в старых ядрах. MAP_GROWSDOWN Используется для стеков. Для VM системы ядра обозначает, что отображение должно распространяться вниз по памяти. MAP_ANONYMOUS Отображение не резервируется ни в каком файле; аргументы fd и offset игнорируются. Этот флаг вместе с MAP_SHARED реализован с Linux 2.4. MAP_ANON Псевдоним для MAP_ANONYMOUS. Не используется. MAP_FILE Флаг совместимости. Игнорируется.

MAP_32BIT Поместить размещение в первые 2Гб адресного рпостранства процесса. Игнорируется, если указано MAP_FIXED . Этот флаг сейчас поддерживается только на x86-64 для 64-битных программ.

Некоторые системы документируют дополнительные флаги MAP_AUTOGROW, MAP_AUTORESRV, MAP_COPY и MAP_LOCAL.

fd должно быть корректным описателем файла, если только не установлено MAP_ANONYMOUS, так как в этом случае аргумент игнорируется.

offset должен быть пропорционален размеру страницы, получаемому при помощи функции getpagesize (2).

Memory mapped by mmap is preserved across fork (2), with the same attributes.

A file is mapped in multiples of the page size. For a file that is not a multiple of the page size, the remaining memory is zeroed when mapped, and writes to that region are not written out to the file. The effect of changing the size of the underlying file of a mapping on the pages that correspond to added or removed regions of the file is unspecified. Системный вызов munmap удаляет все отражения из заданной области памяти, после чего все ссылки на данную область будут вызывать ошибку «неправильное обращение к памяти» (invalid memory reference). Отражение удаляется автоматически при завершении процесса. С другой стороны, закрытие файла не приведет к снятию отражения.

Адрес start должно быть кратен размеру страницы. Все страницы, содержащие часть указанного диапазона, не отображены, и последующие ссылки на эти страницы будут генерировать SIGSEGV. Это не будет являться ошибкой, если указанный диапазон не содержит отображенных страниц. Для отображений ‘файл-бэкэнд’ поле st_atime отображаемого файла может быть обновлено в любой момент между mmap() и соответствующим снятием отображения; первое обращение к отображенной странице обновит поле, если оно до этого уже не было обновлено.

Поля st_ctime и st_mtime файла, отображенного по PROT_WRITE и MAP_SHARED, будут обновлены после записи в отображенний диапазон, и до вызова последующего msync() с флагом MS_SYNC или MS_ASYNC, если такой случится.

ВОЗВРАЩАЕМЫЕ ЗНАЧЕНИЯ


ЗАМЕЧАНИЯ


НАЙДЕННЫЕ ОШИБКИ

Использование отражаемой области памяти может привести к следующим сигналам: SIGSEGV (попытка записи в область памяти, заданную mmap как область для чтения); SIGBUS (попытка доступа к части буфера, которая не является файлом; например, она может находиться за пределами файла. Подобной является ситуация, когда другой процесс уменьшает длину файла).

Источник

Оцените статью