SIM: Add simulation of a multi-CPU processor

This commit is contained in:
Gregory Nutt 2016-02-10 14:58:21 -06:00
parent 5e91bf9a03
commit 52f88b0625
5 changed files with 272 additions and 2 deletions

View File

@ -65,6 +65,10 @@ ifeq ($(CONFIG_SCHED_TICKLESS),y)
CSRCS += up_tickless.c
endif
ifeq ($(CONFIG_SMP),y)
HOSTSRCS += up_simsmp.c
endif
ifeq ($(CONFIG_DEV_CONSOLE),y)
CSRCS += up_uartwait.c
HOSTSRCS += up_simuart.c

View File

@ -28,6 +28,13 @@ nanosleep NXnanosleep
poll NXpoll
printf NXprintf
pthread_create NXpthread_create
pthread_mutex_init NXpthread_mutex_init
pthread_mutex_lock NXpthread_mutex_lock
pthread_mutex_unlock NXpthread_mutex_unlock
pthread_mutex_destroy NXpthread_mutex_destroy
pthread_key_create NXpthread_key_create
pthread_setspecific NXpthread_setspecific
pthread_getspecific NXpthread_getspecific
read NXread
readdir NXreaddir
realloc NXrealloc

View File

@ -1,7 +1,7 @@
/****************************************************************************
* arch/sim/src/up_head.c
*
* Copyright (C) 2007-2009, 2011-2113 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2011-2013, 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -73,11 +73,17 @@ static int g_exitcode = EXIT_SUCCESS;
int main(int argc, char **argv, char **envp)
{
#ifdef CONFIG_SMP
/* In the SMP case, configure the main thread as CPU 0 */
sim_cpu0initialize();
#endif
#ifdef CONFIG_PM
/* Power management should be initialized early in the (simulated) boot
* sequence.
*/
#ifdef CONFIG_PM
pm_initialize();
#endif

View File

@ -210,6 +210,12 @@ extern volatile int g_uart_data_available;
int up_setjmp(xcpt_reg_t *jb);
void up_longjmp(xcpt_reg_t *jb, int val) noreturn_function;
/* up_simsmp.c ************************************************************/
#ifdef CONFIG_SMP
int sim_cpu0initialize(void);
#endif
/* up_tickless.c **********************************************************/
#ifdef CONFIG_SCHED_TICKLESS

247
arch/sim/src/up_simsmp.c Normal file
View File

@ -0,0 +1,247 @@
/****************************************************************************
* arch/sim/src/up_simsmp.c
*
* Copyright (C) 2016 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 <pthread.h>
#include <semaphore.h>
/****************************************************************************
* Private Types
****************************************************************************/
typedef int (*main_t)(int argc, char **argv);
struct sim_cpuinfo_s
{
int cpu; /* CPU number */
pthread_mutex_t mutex; /* For synchronization */
main_t idletask; /* IDLE task entry point */
};
/****************************************************************************
* Private Data
****************************************************************************/
static pthread_key_t g_cpukey;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: sim_idle_trampoline
*
* Description:
* This is a pthread task entry point. A (host) pthread is used to
* simulate a CPU. Multiple pthreads is a good analog to tasks running on
* multiple CPUs
*
* This function is simply a wrapper that sets the pthread specific data
* that presents the CPU number and then calls into the IDLE task entry
* point.
*
* Input Parameters:
* arg - Stanard pthread argument
*
* Returned Value:
* This function does not return
*
****************************************************************************/
static void *sim_idle_trampoline(void *arg)
{
struct sim_cpuinfo_s *cpuinfo = (struct sim_cpuinfo_s *)arg;
int ret;
/* Set the CPU number zero for the CPU thread */
ret = pthread_setspecific(g_cpukey, (const void *)((uintptr_t)cpuinfo->cpu));
if (ret != 0)
{
return NULL;
}
/* Give control to the IDLE task */
(void)cpuinfo->idletask(0, (char **)0);
/* The IDLE task will not return. This is just to keep the compiler happy */
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sim_cpu0initialize
*
* Description:
* Create the pthread-specific data key and set the indication of CPU0
* the the main thread.
*
* Input Parameters:
* None
*
* Returned Value:
* An integer index in the range of 0 through (CONFIG_SMP_NCPUS-1) that
* corresponds to the currently executing CPU.
*
****************************************************************************/
int sim_cpu0initialize(void)
{
int ret;
/* Create the pthread key */
ret = pthread_key_create(&g_cpukey, NULL);
if (ret != 0)
{
return -ret;
}
/* Set the CPU number zero for the main thread */
ret = pthread_setspecific(g_cpukey, (const void *)0);
if (ret != 0)
{
return -ret;
}
return 0;
}
/****************************************************************************
* Name: up_cpundx
*
* Description:
* Return an index in the range of 0 through (CONFIG_SMP_NCPUS-1) that
* corresponds to the currently executing CPU.
*
* Input Parameters:
* None
*
* Returned Value:
* An integer index in the range of 0 through (CONFIG_SMP_NCPUS-1) that
* corresponds to the currently executing CPU.
*
****************************************************************************/
int up_cpundx(void)
{
void *value = pthread_getspecific(g_cpukey);
return (int)((uintptr_t)value);
}
/****************************************************************************
* Name: up_cpustart
*
* Description:
* In an SMP configution, only one CPU is initially active (CPU 0). System
* initialization occurs on that single thread. At the completion of the
* initialization of the OS, just before beginning normal multitasking,
* the additional CPUs would be started by calling this function.
*
* Each CPU is provided the entry point to is IDLE task when started. The
* OS initialization logic calls this function repeatedly until each CPU
* has been started.
*
* Input Parameters:
* cpu - The index of the CPU being started. This will be a numeric
* value in the range of from one to (CONFIG_SMP_NCPUS-1). (CPU
* 0 is already active)
* idletask - The entry point to the IDLE task.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
int up_cpustart(int cpu, main_t idletask)
{
struct sim_cpuinfo_s cpuinfo;
pthread_t thread;
int ret;
/* Initialize the CPU info */
cpuinfo.cpu = cpu;
cpuinfo.idletask = idletask;
ret = pthread_mutex_init(&cpuinfo.mutex, NULL);
if (ret != 0)
{
return -ret;
}
/* Lock the mutex */
ret = pthread_mutex_lock(&cpuinfo.mutex);
if (ret != 0)
{
ret = -ret;
goto errout_with_mutex;
}
/* Start the CPU emulation thread */
ret = pthread_create(&thread, NULL, sim_idle_trampoline, &cpuinfo);
if (ret != 0)
{
ret = -ret;
goto errout_with_lock;
}
/* Try to lock the mutex again. This will block until the pthread unlocks
* the mutex.
*/
ret = pthread_mutex_lock(&cpuinfo.mutex);
if (ret != 0)
{
ret = -ret;
}
errout_with_lock:
(void)pthread_mutex_unlock(&cpuinfo.mutex);
errout_with_mutex:
(void)pthread_mutex_destroy(&cpuinfo.mutex);
return ret;
}