diff --git a/Documentation/NuttxUserGuide.html b/Documentation/NuttxUserGuide.html index 480c7daf7b..ee5d45ae17 100644 --- a/Documentation/NuttxUserGuide.html +++ b/Documentation/NuttxUserGuide.html @@ -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:

+

+ POSIX Deviations +

+

2.12.2 shmat

diff --git a/Kconfig b/Kconfig index a2b7004091..c285096bd1 100644 --- a/Kconfig +++ b/Kconfig @@ -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 diff --git a/arch/Kconfig b/arch/Kconfig index c586595a94..2c024d0b88 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -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. diff --git a/include/nuttx/shm.h b/include/nuttx/shm.h new file mode 100644 index 0000000000..baff3df9e4 --- /dev/null +++ b/include/nuttx/shm.h @@ -0,0 +1,123 @@ +/**************************************************************************** + * include/nuttx/shm.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#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 */ diff --git a/include/sys/ipc.h b/include/sys/ipc.h index a1340dc248..42cf5b1efd 100644 --- a/include/sys/ipc.h +++ b/include/sys/ipc.h @@ -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 */ }; diff --git a/include/sys/shm.h b/include/sys/shm.h index c59b3e143a..73ecbca988 100644 --- a/include/sys/shm.h +++ b/include/sys/shm.h @@ -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 */ diff --git a/mm/mm_gran/mm_gran.h b/mm/mm_gran/mm_gran.h index 2156f96efb..cb9316ae42 100644 --- a/mm/mm_gran/mm_gran.h +++ b/mm/mm_gran/mm_gran.h @@ -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 */ diff --git a/mm/shm/Make.defs b/mm/shm/Make.defs index c0fb12a81d..f02e5864f2 100644 --- a/mm/shm/Make.defs +++ b/mm/shm/Make.defs @@ -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 diff --git a/mm/shm/shm.h b/mm/shm/shm.h new file mode 100644 index 0000000000..0a05dce291 --- /dev/null +++ b/mm/shm/shm.h @@ -0,0 +1,106 @@ +/**************************************************************************** + * mm/shm/shm.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include + +#include + +#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 */ diff --git a/mm/shm/shm_initialize.c b/mm/shm/shm_initialize.c new file mode 100755 index 0000000000..1da2d4e2ec --- /dev/null +++ b/mm/shm/shm_initialize.c @@ -0,0 +1,111 @@ +/**************************************************************************** + * mm/shm/shm_initialize.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#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 */ diff --git a/mm/shm/shmget.c b/mm/shm/shmget.c index f5e718b4a8..3df1dd7639 100755 --- a/mm/shm/shmget.c +++ b/mm/shm/shmget.c @@ -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) diff --git a/sched/init/os_start.c b/sched/init/os_start.c index 62f150c92f..91902b82ee 100644 --- a/sched/init/os_start.c +++ b/sched/init/os_start.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -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. */