From 4b5910efc19843387c8fc5b2c0ff4d5ebcfdd561 Mon Sep 17 00:00:00 2001 From: yintao Date: Mon, 7 Aug 2023 21:04:48 +0800 Subject: [PATCH] nuttx/sim: simlulator rptun powerdown Signed-off-by: yintao --- arch/sim/src/nuttx-names.in | 2 ++ arch/sim/src/sim/posix/sim_hostmisc.c | 39 ++++++++++++++++++++++++ arch/sim/src/sim/sim_internal.h | 4 +++ arch/sim/src/sim/sim_rptun.c | 44 +++++++++++++++------------ 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index 0edb3d27e4..bb4e201295 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -87,6 +87,7 @@ NXSYMBOLS(opendir) NXSYMBOLS(perror) NXSYMBOLS(poll) NXSYMBOLS(posix_memalign) +NXSYMBOLS(posix_spawn) NXSYMBOLS(pthread_attr_init) NXSYMBOLS(pthread_attr_setstack) NXSYMBOLS(pthread_attr_destroy) @@ -149,3 +150,4 @@ NXSYMBOLS(usleep) NXSYMBOLS(utimensat) NXSYMBOLS(write) NXSYMBOLS(writev) +NXSYMBOLS(waitpid) diff --git a/arch/sim/src/sim/posix/sim_hostmisc.c b/arch/sim/src/sim/posix/sim_hostmisc.c index c6023330fa..8be18a05d7 100644 --- a/arch/sim/src/sim/posix/sim_hostmisc.c +++ b/arch/sim/src/sim/posix/sim_hostmisc.c @@ -22,11 +22,15 @@ * Included Files ****************************************************************************/ +#include #include #include +#include #include #include #include +#include +#include #include "sim_internal.h" @@ -138,3 +142,38 @@ void host_init_cwd(void) chdir(path); } #endif + +/**************************************************************************** + * Name: host_posix_spawn + ****************************************************************************/ + +pid_t host_posix_spawn(const char *path, + char *const argv[], char *const envp[]) +{ + int ret; + pid_t pid; + char *default_argv[] = + { + NULL + }; + + if (!argv) + { + argv = default_argv; + } + + ret = posix_spawn(&pid, path, NULL, NULL, argv, envp); + return ret > 0 ? -ret : pid; +} + +/**************************************************************************** + * Name: host_wait + ****************************************************************************/ + +int host_waitpid(pid_t pid) +{ + int status; + + pid = waitpid(pid, &status, 0); + return pid < 0 ? -errno : status; +} diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index a300c5b504..ef78bb891b 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -168,6 +168,10 @@ int host_backtrace(void** array, int size); void host_init_cwd(void); #endif +pid_t host_posix_spawn(const char *path, + char *const argv[], char *const envp[]); +int host_waitpid(pid_t pid); + /* sim_hostmemory.c *********************************************************/ void *host_allocheap(size_t sz); diff --git a/arch/sim/src/sim/sim_rptun.c b/arch/sim/src/sim/sim_rptun.c index 021f2e0459..df35dae7cb 100644 --- a/arch/sim/src/sim/sim_rptun.c +++ b/arch/sim/src/sim/sim_rptun.c @@ -33,7 +33,7 @@ * Pre-processor Definitions ****************************************************************************/ -#define SIM_RPTUN_RESET 0x1 +#define SIM_RPTUN_STOP 0x1 #define SIM_RPTUN_PANIC 0x2 #define SIM_RPTUN_MASK 0xffff #define SIM_RPTUN_SHIFT 16 @@ -65,6 +65,7 @@ struct sim_rptun_dev_s struct simple_addrenv_s addrenv[2]; char cpuname[RPMSG_NAME_SIZE + 1]; char shmemname[RPMSG_NAME_SIZE + 1]; + pid_t pid; /* Work queue for transmit */ @@ -174,11 +175,32 @@ static bool sim_rptun_is_master(struct rptun_dev_s *dev) static int sim_rptun_start(struct rptun_dev_s *dev) { + struct sim_rptun_dev_s *priv = container_of(dev, + struct sim_rptun_dev_s, rptun); + pid_t pid; + + pid = host_posix_spawn(sim_rptun_get_cpuname(dev), NULL, NULL); + if (pid < 0) + { + return pid; + } + + priv->pid = pid; return 0; } static int sim_rptun_stop(struct rptun_dev_s *dev) { + struct sim_rptun_dev_s *priv = container_of(dev, + struct sim_rptun_dev_s, rptun); + + priv->shmem->cmdm = SIM_RPTUN_STOP << SIM_RPTUN_SHIFT; + + host_waitpid(priv->pid); + + host_freeshmem(priv->shmem); + priv->shmem = NULL; + return 0; } @@ -211,23 +233,6 @@ static int sim_rptun_register_callback(struct rptun_dev_s *dev, return 0; } -static void sim_rptun_reset(struct rptun_dev_s *dev, int value) -{ - struct sim_rptun_dev_s *priv = container_of(dev, - struct sim_rptun_dev_s, rptun); - - DEBUGASSERT((value & ~SIM_RPTUN_MASK) == 0); - - if (priv->master) - { - priv->shmem->cmdm = value | (SIM_RPTUN_RESET << SIM_RPTUN_SHIFT); - } - else - { - priv->shmem->cmds = value | (SIM_RPTUN_RESET << SIM_RPTUN_SHIFT); - } -} - static void sim_rptun_panic(struct rptun_dev_s *dev) { struct sim_rptun_dev_s *priv = container_of(dev, @@ -249,7 +254,7 @@ static void sim_rptun_check_cmd(struct sim_rptun_dev_s *priv) switch ((cmd >> SIM_RPTUN_SHIFT) & SIM_RPTUN_MASK) { - case SIM_RPTUN_RESET: + case SIM_RPTUN_STOP: host_abort(cmd & SIM_RPTUN_MASK); break; @@ -309,7 +314,6 @@ static const struct rptun_ops_s g_sim_rptun_ops = .stop = sim_rptun_stop, .notify = sim_rptun_notify, .register_callback = sim_rptun_register_callback, - .reset = sim_rptun_reset, .panic = sim_rptun_panic, };