Add shared memory initializatin logic

This commit is contained in:
Gregory Nutt 2014-09-23 08:46:31 -06:00
parent 242d5f2068
commit a73a3ef99f
12 changed files with 381 additions and 13 deletions

View File

@ -9085,11 +9085,6 @@ int shmget(key_t key, size_t size, int shmflg);
Upon creation, the data structure associated with the new shared memory identifier will be initialized as follows:
</p>
<ul>
<li>
<p>
The values of <code>shm_perm.cuid</code>, <code>shm_perm.uid</code>, <code>shm_perm.cgid</code>, and <code>shm_perm.gid</code> are set equal to the effective user ID and effective group ID, respectively, of the calling process.
</p>
</li>
<li>
<p>
The low-order nine bits of <code>shm_perm.mode</code> are set equal to the low-order nine bits of <code>shmflg</code>.
@ -9162,6 +9157,17 @@ int shmget(key_t key, size_t size, int shmflg);
A shared memory identifier is to be created, but the system-imposed limit on the maximum number of allowed shared memory identifiers system-wide would be exceeded.
</li>
</ul>
<p>
<b>POSIX Deviations</b>
<p>
<ul>
<li>
<p>
The values of <code>shm_perm.cuid</code>, <code>shm_perm.uid</code>, <code>shm_perm.cgid</code>, and <code>shm_perm.gid</code> should be set equal to the effective user ID and effective group ID, respectively, of the calling process.
The NuttX <code>ipc_perm</code> structure, however, does not support these fields because user and group IDs are not yet supported by NuttX.
</p>
</li>
</ul>
<h3><a name="shmat">2.12.2 <code>shmat</code></a></h3>
<p>

View File

@ -458,6 +458,13 @@ config DEBUG_MM
---help---
Enable memory management debug SYSLOG output (disabled by default)
config DEBUG_SHM
bool "Shared Memory Debug Output"
default n
depends on MM_SHM
---help---
Enable shared memory management debug SYSLOG output (disabled by default)
config DEBUG_NET
bool "Network Debug Output"
default n

View File

@ -200,7 +200,7 @@ config ARCH_HEAP_VBASE
The virtual address of the beginning of the heap region.
config ARCH_SHM_VBASE
hex "Virtual heap base"
hex "Shared memory base"
depends on MM_SHM
---help---
The virtual address of the beginning of the shared memory region.

123
include/nuttx/shm.h Normal file
View File

@ -0,0 +1,123 @@
/****************************************************************************
* include/nuttx/shm.h
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_SHM_H
#define __INCLUDE_NUTTX_SHM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#ifdef CONFIG_MM_SHM
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_ARCH_ADDRENV
# error CONFIG_ARCH_ADDRENV must be selected with CONFIG_MM_SHM
#endif
#ifndef CONFIG_BUILD_KERNEL
# error CONFIG_BUILD_KERNEL must be selected with CONFIG_MM_SHM
#endif
#ifndef CONFIG_GRAN
# error CONFIG_GRAN must be selected with CONFIG_MM_SHM
#endif
#ifdef CONFIG_GRAN_SINGLE
# error CONFIG_GRAN_SINGLE must NOT be selected with CONFIG_MM_SHM
#endif
#ifndef CONFIG_MM_PGALLOC
# error CONFIG_MM_PGALLOC must be selected with CONFIG_MM_SHM
#endif
/* Debug */
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG_SHM
# define shmdbg(format, ...) dbg(format, ##__VA_ARGS__)
# define shmvdbg(format, ...) vdbg(format, ##__VA_ARGS__)
# else
# define shmdbg(format, ...) mdbg(format, ##__VA_ARGS__)
# define shmvdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
# endif
#else
# ifdef CONFIG_DEBUG_SHM
# define shmdbg dbg
# define shmvdbg vdbg
# else
# define shmdbg (void)
# define shmvdbg (void)
# endif
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: shm_initialize
*
* Description:
* Perform one time, start-up initialization of the shared memor logic.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void shm_initialize(void);
#endif /* CONFIG_MM_SHM */
#endif /* __INCLUDE_NUTTX_SHM_H */

View File

@ -89,10 +89,12 @@ extern "C"
struct ipc_perm
{
#if 0 /* User and group IDs not yet supported by NuttX */
uid_t uid; /* Owner's user ID */
gid_t gid; /* Owner's group ID */
uid_t cuid; /* Creator's user ID */
gid_t cgid; /* Creator's group ID */
#endif
mode_t mode; /* Read/write permission */
};

View File

@ -49,6 +49,7 @@
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
/* Definitions required by POSIX */
#define SHM_RDONLY 0x01 /* Attach read-only (else read-write) */
#define SHM_RND 0x02 /* Round attach address to SHMLBA */

View File

@ -33,8 +33,8 @@
*
****************************************************************************/
#ifndef __MM_MM_GRAN_MM_GRAHN_H
#define __MM_MM_GRAN_MM_GRAHN_H
#ifndef __MM_MM_GRAN_MM_GRAN_H
#define __MM_MM_GRAN_MM_GRAN_H
/****************************************************************************
* Included Files
@ -83,7 +83,7 @@
* Public Types
****************************************************************************/
/* This structure represents the state of on granule allocation */
/* This structure represents the state of one granule allocation */
struct gran_s
{
@ -148,4 +148,4 @@ void gran_leave_critical(FAR struct gran_s *priv);
void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
unsigned int ngranules);
#endif /* __MM_MM_GRAN_MM_GRAHN_H */
#endif /* __MM_MM_GRAN_MM_GRAN_H */

View File

@ -36,6 +36,7 @@
# Shared memory allocator
ifeq ($(CONFIG_MM_SHM),y)
CSRCS += shm_initialize.c
CSRCS += shmat.c shmctl.c shmdt.c shmget.c
# Add the shared memory directory to the build

106
mm/shm/shm.h Normal file
View File

@ -0,0 +1,106 @@
/****************************************************************************
* mm/shm/shm.h
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __MM_SHM_SHM_H
#define __MM_SHM_SHM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdint.h>
#include <semaphore.h>
#include <nuttx/addrenv.h>
#ifdef CONFIG_MM_SHM
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* IPC_PRIVATE is the only value for the the SHM key that is guaranteed to
* be invalid.
*/
#define SHM_INVALID_KEY IPC_PRIVATE
/****************************************************************************
* Public Types
****************************************************************************/
/* This structure represents the state of one shared memory region
* allocation. Cast compatible with struct shmid_ds.
*/
struct shm_region_s
{
struct shmid_ds sr_ds; /* Region info */
key_t sr_key; /* Lookup key. IPC_PRIVATE means unused */
sem_t sr_sem; /* Manages exclusive access to this region */
/* List of physical pages allocated for this memory region */
uintptr_t sr_pages[CONFIG_ARCH_SHM_NPAGES];
};
/* This structure represents the set of all shared memory regions.
* Access to the region
*/
struct shm_info_s
{
sem_t si_sem; /* Manages exclusive access to the region list */
struct shm_region_s si_region[CONFIG_ARCH_SHM_MAXREGIONS];
};
/****************************************************************************
* Public Data
****************************************************************************/
/* State of the all shared memory */
extern struct shm_info_s g_shminfo;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#endif /* CONFIG_MM_SHM */
#endif /* __MM_SHM_SHM_H */

111
mm/shm/shm_initialize.c Executable file
View File

@ -0,0 +1,111 @@
/****************************************************************************
* mm/shm/shm_initialize.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include "shm/shm.h"
#ifdef CONFIG_MM_SHM
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* State of the all shared memory */
struct shm_info_s g_shminfo;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: shm_initialize
*
* Description:
* Perform one time, start-up initialization of the shared memor logic.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void shm_initialize(void)
{
#if SHM_INVALID_KEY != 0
FAR struct shm_region_s *region;
int i;
#endif
/* Initialize the shared memory region list */
sem_init(&g_shminfo.si_sem, 0, 1);
#if SHM_INVALID_KEY != 0
/* Initialize each shared memory region */
for (i = 0; i < CONFIG_ARCH_SHM_NPAGES; i++)
{
region = &g_shminfo.si_region[i];
/* Markk the key invalid for each region. The invalid key is an
* indication that the region is not in use.
*/
region->sr_key = SHM_INVALID_KEY;
}
#endif
}
#endif /* CONFIG_MM_SHM */

View File

@ -83,9 +83,6 @@
* Upon creation, the data structure associated with the new shared memory
* identifier will be initialized as follows:
*
* - The values of shm_perm.cuid, shm_perm.uid, shm_perm.cgid, and
* shm_perm.gid are set equal to the effective user ID and effective
* group ID, respectively, of the calling process.
* - The low-order nine bits of shm_perm.mode are set equal to the low-
* order nine bits of shmflg.
* - The value of shm_segsz is set equal to the value of size.
@ -136,6 +133,13 @@
* limit on the maximum number of allowed shared memory identifiers
* system-wide would be exceeded.
*
* POSIX Deviations:
* - The values of shm_perm.cuid, shm_perm.uid, shm_perm.cgid, and
* shm_perm.gid should be set equal to the effective user ID and
* effective group ID, respectively, of the calling process.
* The NuttX ipc_perm structure, however, does not support these
* fields because user and group IDs are not yet supported by NuttX.
*
****************************************************************************/
int shmget(key_t key, size_t size, int shmflg)

View File

@ -49,6 +49,7 @@
#include <nuttx/net/net.h>
#include <nuttx/lib.h>
#include <nuttx/mm.h>
#include <nuttx/shm.h>
#include <nuttx/kmalloc.h>
#include <nuttx/init.h>
@ -485,6 +486,12 @@ void os_start(void)
up_initialize();
#ifdef CONFIG_MM_SHM
/* Initialize shared memory support */
shm_initialize();
#endif
/* Initialize the C libraries. This is done last because the libraries
* may depend on the above.
*/