diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index 06170ecae25..addcf7d48e8 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -79,12 +79,9 @@ VPATH = sim DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH))) HOSTCFLAGS += ${shell $(INCDIR) "$(CC)" $(ARCH_SRCDIR)} -HOSTSRCS = up_hosttime.c - -ifeq ($(CONFIG_LIBC_MODLIB),y) - HOSTSRCS += up_hostmemory.c -else ifeq ($(CONFIG_BINFMT_LOADABLE),y) - HOSTSRCS += up_hostmemory.c +HOSTSRCS = up_hostmemory.c up_hosttime.c +ifneq ($(CONFIG_HOST_MACOS),y) + STDLIBS += -lrt endif ifeq ($(CONFIG_STACK_COLORATION),y) @@ -201,10 +198,6 @@ endif ifeq ($(CONFIG_RPTUN),y) CSRCS += up_rptun.c - HOSTSRCS += up_shmem.c -ifneq ($(CONFIG_HOST_MACOS),y) - STDLIBS += -lrt -endif endif ifeq ($(CONFIG_FS_HOSTFS),y) diff --git a/arch/sim/src/sim/up_hostmemory.c b/arch/sim/src/sim/up_hostmemory.c index 6c4345bb93c..0950fd5a917 100644 --- a/arch/sim/src/sim/up_hostmemory.c +++ b/arch/sim/src/sim/up_hostmemory.c @@ -22,10 +22,12 @@ * Included Files ****************************************************************************/ -#include +#include +#include +#include -#include -#include +#include +#include /**************************************************************************** * Public Functions @@ -47,9 +49,67 @@ void *host_alloc_heap(size_t sz) MAP_ANON | MAP_PRIVATE, -1, 0); if (p == MAP_FAILED) { - perror("Failed to allocate heap with mmap"); - exit(EXIT_FAILURE); + return NULL; } return p; } + +void *host_alloc_shmem(const char *name, size_t size, int master) +{ + void *mem; + int oflag; + int ret; + int fd; + + oflag = O_RDWR; + if (master) + { + oflag |= O_CREAT | O_TRUNC; + } + + while (1) + { + fd = shm_open(name, oflag, S_IRUSR | S_IWUSR); + if (fd >= 0) + { + if (!master) + { + /* Avoid the second slave instance open successfully */ + + shm_unlink(name); + } + break; + } + + if (master || errno != ENOENT) + { + return NULL; + } + + /* Master isn't ready, sleep and try again */ + + usleep(1000); + } + + ret = ftruncate(fd, size); + if (ret < 0) + { + close(fd); + return NULL; + } + + mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + close(fd); /* Don't need keep fd any more once the memory get mapped */ + if (mem == MAP_FAILED) + { + return NULL; + } + + return mem; +} + +void host_free_shmem(void *mem) +{ + munmap(mem, 0); +} diff --git a/arch/sim/src/sim/up_internal.h b/arch/sim/src/sim/up_internal.h index de35b8cc19f..ef90ae9d1a5 100644 --- a/arch/sim/src/sim/up_internal.h +++ b/arch/sim/src/sim/up_internal.h @@ -227,6 +227,8 @@ void up_longjmp(xcpt_reg_t *jb, int val) noreturn_function; /* up_hostmemory.c **********************************************************/ void *host_alloc_heap(size_t sz); +void *host_alloc_shmem(const char *name, size_t size, int master); +void host_free_shmem(void *mem); /* up_hosttime.c ************************************************************/ @@ -377,18 +379,11 @@ void netdriver_setmtu(int mtu); void netdriver_loop(void); #endif -#ifdef CONFIG_RPTUN - -/* up_shmem.c ***************************************************************/ - -void *shmem_open(const char *name, size_t size, int master); -void shmem_close(void *mem); - /* up_rptun.c ***************************************************************/ +#ifdef CONFIG_RPTUN int up_rptun_init(void); void up_rptun_loop(void); - #endif #ifdef CONFIG_SIM_SPIFLASH diff --git a/arch/sim/src/sim/up_rptun.c b/arch/sim/src/sim/up_rptun.c index 98098b0824d..c908722798c 100644 --- a/arch/sim/src/sim/up_rptun.c +++ b/arch/sim/src/sim/up_rptun.c @@ -192,9 +192,9 @@ int up_rptun_init(void) { int ret; - g_dev.shmem = shmem_open("rptun-shmem", - sizeof(*g_dev.shmem), - CONFIG_SIM_RPTUN_MASTER); + g_dev.shmem = host_alloc_shmem("rptun-shmem", + sizeof(*g_dev.shmem), + CONFIG_SIM_RPTUN_MASTER); if (g_dev.shmem == NULL) { return -ENOMEM; @@ -244,7 +244,7 @@ int up_rptun_init(void) ret = rptun_initialize(&g_dev.rptun); if (ret < 0) { - shmem_close(g_dev.shmem); + host_free_shmem(g_dev.shmem); return ret; } diff --git a/arch/sim/src/sim/up_shmem.c b/arch/sim/src/sim/up_shmem.c deleted file mode 100644 index f86d8168b36..00000000000 --- a/arch/sim/src/sim/up_shmem.c +++ /dev/null @@ -1,110 +0,0 @@ -/**************************************************************************** - * arch/sim/src/sim/up_shmem.c - * - * Copyright (C) 2019 Xiaomi Inc. All rights reserved. - * Author: Chao An - * - * 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 - ****************************************************************************/ - -#define _GNU_SOURCE 1 - -#include -#include -#include - -#include -#include - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -void *shmem_open(const char *name, size_t size, int master) -{ - void *mem; - int oflag; - int ret; - int fd; - - oflag = O_RDWR; - if (master) - { - oflag |= O_CREAT | O_TRUNC; - } - - while (1) - { - fd = shm_open(name, oflag, S_IRUSR | S_IWUSR); - if (fd >= 0) - { - if (!master) - { - /* Avoid the second slave instance open successfully */ - - shm_unlink(name); - } - break; - } - - if (master || errno != ENOENT) - { - return NULL; - } - - /* Master isn't ready, sleep and try again */ - - usleep(1000); - } - - ret = ftruncate(fd, size); - if (ret < 0) - { - close(fd); - return NULL; - } - - mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - close(fd); /* Don't need keep fd any more once the memory get mapped */ - if (mem == MAP_FAILED) - { - return NULL; - } - - return mem; -} - -void shmem_close(void *mem) -{ - munmap(mem, 0); -}