summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/mmap.c
diff options
context:
space:
mode:
authorGedare Bloom <gedare@rtems.org>2017-03-15 14:31:00 -0400
committerGedare Bloom <gedare@rtems.org>2017-05-05 10:34:08 -0400
commit87de70a2984cece87db94f4b445589c4e24e5c77 (patch)
tree945d211b09e3b7714a2bba6d06ceae7a601d4116 /cpukit/posix/src/mmap.c
parentposix: Add mmap/unmap support for mapping files. (diff)
downloadrtems-87de70a2984cece87db94f4b445589c4e24e5c77.tar.bz2
posix/mman: add mmap support for shm objects
Update #2859.
Diffstat (limited to 'cpukit/posix/src/mmap.c')
-rw-r--r--cpukit/posix/src/mmap.c219
1 files changed, 165 insertions, 54 deletions
diff --git a/cpukit/posix/src/mmap.c b/cpukit/posix/src/mmap.c
index c05a65e078..9f555ade92 100644
--- a/cpukit/posix/src/mmap.c
+++ b/cpukit/posix/src/mmap.c
@@ -1,5 +1,10 @@
+/**
+ * @file
+ */
+
/*
* Copyright (c) 2012 Chris Johns (chrisj@rtems.org)
+ * Copyright (c) 2017 Gedare Bloom (gedare@rtems.org)
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
@@ -19,7 +24,8 @@
#include "rtems/libio_.h"
-#include <rtems/posix/mmanimpl.h>
+#include <rtems/posix/mmanimpl.h>
+#include <rtems/posix/shmimpl.h>
#define RTEMS_MUTEX_ATTRIBS \
(RTEMS_PRIORITY | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
@@ -49,11 +55,14 @@ bool mmap_mappings_lock_create(
* the libio lock is locked and we then test the mapping lock again. If not
* present we create the mapping lock then release libio lock.
*/
+ /* FIXME: double-checked locking anti-pattern. */
if ( mmap_mappings_lock == 0 ) {
rtems_status_code sc = RTEMS_SUCCESSFUL;
rtems_chain_initialize_empty( &mmap_mappings );
rtems_semaphore_obtain( rtems_libio_semaphore,
RTEMS_WAIT, RTEMS_NO_TIMEOUT );
+ /* FIXME: account for semaphore in confdefs, or maybe just use the
+ * rtems_libio_semaphore? */
if ( mmap_mappings_lock == 0 )
sc = rtems_semaphore_create( rtems_build_name( 'M', 'M', 'A', 'P' ),
1,
@@ -98,19 +107,45 @@ bool mmap_mappings_lock_release(
return true;
}
+/* Helper function only gets called for mmap mappings of shared memory objects
+ * with the MAP_SHARED flag.
+ */
+static void *shm_mmap( rtems_libio_t *iop, size_t len, int prot, off_t off)
+{
+ POSIX_Shm_Control *shm = iop_to_shm( iop );
+ void *m;
+
+ _Objects_Allocator_lock();
+
+ m = (*shm->shm_object.ops->object_mmap)( &shm->shm_object, len, prot, off);
+ if ( m != NULL ) {
+ /* Keep a reference in the shared memory to prevent its removal. */
+ ++shm->reference_count;
+
+ /* Update atime */
+ _POSIX_Shm_Update_atime(shm);
+ }
+
+ _Objects_Allocator_unlock();
+
+ return m;
+}
+
void *mmap(
void *addr, size_t len, int prot, int flags, int fildes, off_t off
)
{
- struct stat sb;
- mmap_mapping* mapping;
- ssize_t r;
-
- /*
- * Clear errno.
- */
+ struct stat sb;
+ mmap_mapping *mapping;
+ ssize_t r;
+ rtems_libio_t *iop;
+ bool map_fixed;
+
+ map_fixed = (flags & MAP_FIXED) == MAP_FIXED;
+
+ /* Clear errno. */
errno = 0;
-
+
/*
* Get a stat of the file to get the dev + inode number and to make sure the
* fd is ok. The normal libio calls cannot be used because we need to return
@@ -122,109 +157,185 @@ void *mmap(
return MAP_FAILED;
}
+ /* fstat ensures we have a good file descriptor. Hold on to iop. */
+ iop = rtems_libio_iop( fildes );
+
if ( len == 0 ) {
errno = EINVAL;
return MAP_FAILED;
}
- /*
- * Check the type of file we have and make sure it is supported.
- */
+ /* Check the type of file we have and make sure it is supported. */
if ( S_ISDIR( sb.st_mode ) || S_ISLNK( sb.st_mode )) {
errno = ENODEV;
return MAP_FAILED;
}
-
+
/*
* We can provide read, write and execute because the memory in RTEMS does
* not normally have protections but we cannot hide access to memory.
*/
if ( prot == PROT_NONE ) {
+ errno = ENOTSUP;
+ return MAP_FAILED;
+ }
+
+ /* Either MAP_SHARED or MAP_PRIVATE must be defined, but not both */
+ if ( (flags & MAP_SHARED) == MAP_SHARED ) {
+ if ( (flags & MAP_PRIVATE) == MAP_PRIVATE ) {
+ errno = EINVAL;
+ return MAP_FAILED;
+ }
+ } else if ( (flags & MAP_PRIVATE != MAP_PRIVATE) ) {
errno = EINVAL;
return MAP_FAILED;
}
-
+
/*
- * Check to see if the mapping is valid for the file.
+ * We can not normally provide restriction of write access. Reject any
+ * attempt to map without write permission, since we are not able to
+ * prevent a write from succeeding.
*/
+ if ( PROT_WRITE != (prot & PROT_WRITE) ) {
+ errno = ENOTSUP;
+ return MAP_FAILED;
+ }
+
+ /* Check to see if the mapping is valid for a regular file. */
if ( S_ISREG( sb.st_mode )
+ /* FIXME: Should this be using strict inequality (>) comparisons? It would
+ * be valid to map a region exactly equal to the st_size, e.g. see below. */
&& (( off >= sb.st_size ) || (( off + len ) >= sb.st_size ))) {
errno = EOVERFLOW;
return MAP_FAILED;
}
/*
- * Obtain the mmap lock. Sets errno on failure.
+ * Check to see if the mapping is valid for other file/object types.
+ * Does this satisfy for devices?
*/
- if ( !mmap_mappings_lock_obtain( ) )
+ if ( sb.st_size < off + len ) {
+ errno = ENXIO;
return MAP_FAILED;
+ }
- if (( flags & MAP_FIXED ) == MAP_FIXED ) {
- rtems_chain_node* node = rtems_chain_first (&mmap_mappings);
- while ( !rtems_chain_is_tail( &mmap_mappings, node )) {
- /*
- * If the map is fixed see if this address is already mapped. At this
- * point in time if there is an overlap in the mappings we return an
- * error.
- */
- mapping = (mmap_mapping*) node;
- if ( ( addr >= mapping->addr ) &&
- ( addr < ( mapping->addr + mapping->len )) ) {
- mmap_mappings_lock_release( );
- errno = ENXIO;
- return MAP_FAILED;
- }
- node = rtems_chain_next( node );
+ /* Do not seek on character devices, pipes, sockets, or memory objects. */
+ if ( S_ISREG( sb.st_mode ) || S_ISBLK( sb.st_mode ) ) {
+ if ( lseek( fildes, off, SEEK_SET ) < 0 ) {
+ return MAP_FAILED;
}
}
+ /* Create the mapping */
mapping = malloc( sizeof( mmap_mapping ));
if ( !mapping ) {
- mmap_mappings_lock_release( );
errno = ENOMEM;
return NULL;
}
-
memset( mapping, 0, sizeof( mmap_mapping ));
-
mapping->len = len;
mapping->flags = flags;
-
- if (( flags & MAP_FIXED ) != MAP_FIXED ) {
+ mapping->iop = iop;
+
+ /*
+ * HACK: We should have a better generic way to distinguish between
+ * shm objects and other mmap'd files. We need to know at munmap time
+ * if the mapping is to a shared memory object in order to refcnt shms.
+ * We could do this by providing mmap in the file operations if needed.
+ */
+ if ( S_ISREG( sb.st_mode ) || S_ISBLK( sb.st_mode ) ||
+ S_ISCHR( sb.st_mode ) || S_ISFIFO( sb.st_mode ) ||
+ S_ISSOCK( sb.st_mode ) ) {
+ mapping->is_shared_shm = false;
+ } else {
+ mapping->is_shared_shm = true;
+ }
+
+ /*
+ * MAP_SHARED currently is only supported for shared memory objects.
+ */
+ if ( (MAP_SHARED == (flags & MAP_SHARED)) && (mapping->is_shared_shm == false) ) {
+ free( mapping );
+ errno = ENOTSUP;
+ return MAP_FAILED;
+ }
+
+ if ( map_fixed ) {
+ mapping->addr = addr;
+ } else if ( MAP_PRIVATE == (flags & MAP_PRIVATE) ) {
+ /* private mappings of shared memory do not need special treatment. */
+ mapping->is_shared_shm = false;
mapping->addr = malloc( len );
if ( !mapping->addr ) {
- mmap_mappings_lock_release( );
free( mapping );
errno = ENOMEM;
return MAP_FAILED;
}
+ }
- /*
- * Do not seek on character devices, pipes or sockets.
- */
- if ( S_ISREG( sb.st_mode ) || S_ISBLK( sb.st_mode ) ) {
- if ( lseek( fildes, off, SEEK_SET ) < 0 ) {
- mmap_mappings_lock_release( );
- free( mapping->addr );
+ /* MAP_FIXED is not supported for shared memory objects with MAP_SHARED. */
+ if ( map_fixed && mapping->is_shared_shm ) {
+ free( mapping );
+ errno = ENOTSUP;
+ return MAP_FAILED;
+ }
+
+ /* Lock access to mmap_mappings. Sets errno on failure. */
+ if ( !mmap_mappings_lock_obtain( ) )
+ return MAP_FAILED;
+
+ if ( map_fixed ) {
+ rtems_chain_node* node = rtems_chain_first (&mmap_mappings);
+ while ( !rtems_chain_is_tail( &mmap_mappings, node )) {
+ /*
+ * If the map is fixed see if this address is already mapped. At this
+ * point in time if there is an overlap in the mappings we return an
+ * error. POSIX allows us to also return successfully by unmapping
+ * the overlapping prior mappings.
+ */
+ mapping = (mmap_mapping*) node;
+ if ( ( addr >= mapping->addr ) &&
+ ( addr < ( mapping->addr + mapping->len )) ) {
free( mapping );
+ mmap_mappings_lock_release( );
+ errno = ENXIO;
return MAP_FAILED;
}
+ node = rtems_chain_next( node );
}
}
- r = read( fildes, mapping->addr, len );
+ /* Populate the data */
+ if ( MAP_PRIVATE == (flags & MAP_PRIVATE) ) {
+ /*
+ * Use read() for private mappings. This updates atime as needed.
+ * Changes to the underlying object will NOT be reflected in the mapping.
+ * The underlying object can be removed while the mapping exists.
+ */
+ r = read( fildes, mapping->addr, len );
- if ( r != len ) {
- mmap_mappings_lock_release( );
- free( mapping->addr );
- free( mapping );
- errno = ENXIO;
- return MAP_FAILED;
+ if ( r != len ) {
+ mmap_mappings_lock_release( );
+ if ( !map_fixed ) {
+ free( mapping->addr );
+ }
+ free( mapping );
+ errno = ENXIO;
+ return MAP_FAILED;
+ }
+ } else if ( MAP_SHARED == (flags & MAP_SHARED) ) {
+ /* Currently only shm objects can be MAP_SHARED. */
+ mapping->addr = shm_mmap(iop, len, prot, off);
}
+ /* add an extra reference to the file associated with fildes that
+ * is not removed by a subsequent close(). This reference shall be removed
+ * when there are no more mappings to the file. */
+ rtems_libio_increment_mapping_refcnt(iop);
+
rtems_chain_append( &mmap_mappings, &mapping->node );
mmap_mappings_lock_release( );
-
+
return mapping->addr;
}