summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoryangjie11 <[email protected]>2018-11-28 19:02:11 +0800
committerGitHub <[email protected]>2018-11-28 19:02:11 +0800
commit1a7f95f92b84b0a3acd6243cb2a658b4f9b6f1ac (patch)
tree3b90c3e4b7ced83ce239ae8df020926eaf74d7de /src
parent4a43d3cdae1b731dcf51d3531048c51fd4142516 (diff)
parentd4469fe7d83d08913d096dc6d75a750dd854c0cc (diff)
Merge pull request #1 from RT-Thread/master
test
Diffstat (limited to 'src')
-rw-r--r--src/Kconfig31
-rw-r--r--src/clock.c27
-rw-r--r--src/components.c36
-rw-r--r--src/cpu.c91
-rw-r--r--src/device.c18
-rw-r--r--src/idle.c102
-rw-r--r--src/ipc.c31
-rw-r--r--src/irq.c31
-rw-r--r--src/kservice.c100
-rw-r--r--src/mem.c72
-rw-r--r--src/memheap.c26
-rw-r--r--src/mempool.c18
-rw-r--r--src/module.c1289
-rw-r--r--src/module.h277
-rw-r--r--src/object.c69
-rw-r--r--src/scheduler.c696
-rw-r--r--src/signal.c48
-rw-r--r--src/slab.c22
-rw-r--r--src/thread.c122
-rw-r--r--src/timer.c46
20 files changed, 1044 insertions, 2108 deletions
diff --git a/src/Kconfig b/src/Kconfig
index f08a90f66b..9ae655c670 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -8,6 +8,22 @@ config RT_NAME_MAX
Each kernel object, such as thread, timer, semaphore etc, has a name,
the RT_NAME_MAX is the maximal size of this object name.
+config RT_USING_SMP
+ bool "Enable SMP(Symmetric multiprocessing)"
+ default n
+ help
+ This option should be selected by machines which have an SMP-
+ capable CPU.
+ The only effect of this option is to make the SMP-related
+ options available to the user for configuration.
+
+config RT_CPUS_NR
+ int "Number of CPUs"
+ default 2
+ depends on RT_USING_SMP
+ help
+ Number of CPUs in the system
+
config RT_ALIGN_SIZE
int "Alignment size for CPU architecture data access"
default 4
@@ -51,11 +67,16 @@ config RT_USING_OVERFLOW_CHECK
config RT_USING_HOOK
bool "Enable system hook"
default y
+ select RT_USING_IDLE_HOOK
help
Enable the hook function when system running, such as idle thread hook,
thread context switch etc.
- if RT_USING_HOOK
+config RT_USING_IDLE_HOOK
+ bool "Enable IDLE Task hook"
+ default y if RT_USING_HOOK
+
+ if RT_USING_IDLE_HOOK
config RT_IDEL_HOOK_LIST_SIZE
int "The max size of idel hook list"
default 4
@@ -295,8 +316,10 @@ menu "Kernel Device Object"
endmenu
-config RT_USING_MODULE
- bool "The dynamic module feature"
- default n
+config RT_VER_NUM
+ hex
+ default 0x40000
+ help
+ RT-Thread version number
endmenu
diff --git a/src/clock.c b/src/clock.c
index 734c0d5014..1bedb4e06a 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -1,21 +1,7 @@
/*
- * File : clock.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -26,12 +12,17 @@
* 2010-05-20 Bernard fix the tick exceeds the maximum limits
* 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
* 2011-06-26 Bernard add rt_tick_set function.
+ * 2018-11-22 Jesven add per cpu tick
*/
#include <rthw.h>
#include <rtthread.h>
+#ifdef RT_USING_SMP
+#define rt_tick rt_cpu_index(0)->tick
+#else
static rt_tick_t rt_tick = 0;
+#endif
extern void rt_timer_check(void);
@@ -85,7 +76,11 @@ void rt_tick_increase(void)
struct rt_thread *thread;
/* increase the global tick */
+#ifdef RT_USING_SMP
+ rt_cpu_self()->tick ++;
+#else
++ rt_tick;
+#endif
/* check time slice */
thread = rt_thread_self();
diff --git a/src/components.c b/src/components.c
index 818e570b3c..d77ef8300f 100644
--- a/src/components.c
+++ b/src/components.c
@@ -1,21 +1,7 @@
/*
- * File : init.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2012 - 2015, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -28,6 +14,7 @@
* 2015-05-04 Bernard Rename it to components.c because compiling issue
* in some IDEs.
* 2015-07-29 Arda.Fu Add support to use RT_USING_USER_MAIN with IAR
+ * 2018-11-22 Jesven Add secondary cpu boot up
*/
#include <rthw.h>
@@ -58,7 +45,7 @@
*
* rti_end --> 6.end
*
- * These automatically initializaiton, the driver or component initial function must
+ * These automatically initialization, the driver or component initial function must
* be defined with:
* INIT_BOARD_EXPORT(fn);
* INIT_DEVICE_EXPORT(fn);
@@ -123,7 +110,7 @@ void rt_components_init(void)
int result;
const struct rt_init_desc *desc;
- rt_kprintf("do components intialization.\n");
+ rt_kprintf("do components initialization.\n");
for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
{
rt_kprintf("initialize %s", desc->fn_name);
@@ -146,7 +133,7 @@ void rt_application_init(void);
void rt_hw_board_init(void);
int rtthread_startup(void);
-#if defined (__CC_ARM)
+#if defined(__CC_ARM) || defined(__CLANG_ARM)
extern int $Super$$main(void);
/* re-define main function */
int $Sub$$main(void)
@@ -194,8 +181,11 @@ void main_thread_entry(void *parameter)
/* RT-Thread components initialization */
rt_components_init();
+#ifdef RT_USING_SMP
+ rt_hw_secondary_cpu_up();
+#endif
/* invoke system main function */
-#if defined (__CC_ARM)
+#if defined(__CC_ARM) || defined(__CLANG_ARM)
$Super$$main(); /* for ARMCC. */
#elif defined(__ICCARM__) || defined(__GNUC__)
main();
@@ -229,7 +219,7 @@ int rtthread_startup(void)
{
rt_hw_interrupt_disable();
- /* board level initalization
+ /* board level initialization
* NOTE: please initialize heap inside board initialization.
*/
rt_hw_board_init();
@@ -257,6 +247,10 @@ int rtthread_startup(void)
/* idle thread initialization */
rt_thread_idle_init();
+#ifdef RT_USING_SMP
+ rt_hw_spin_lock(&_cpus_lock);
+#endif /*RT_USING_SMP*/
+
/* start scheduler */
rt_system_scheduler_start();
diff --git a/src/cpu.c b/src/cpu.c
new file mode 100644
index 0000000000..ef6dce1787
--- /dev/null
+++ b/src/cpu.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-10-30 Bernard The first version
+ */
+
+#include <rtthread.h>
+#include <rthw.h>
+
+#ifdef RT_USING_SMP
+
+static struct rt_cpu rt_cpus[RT_CPUS_NR];
+rt_hw_spinlock_t _cpus_lock;
+
+/**
+ * This fucntion will return current cpu.
+ */
+struct rt_cpu *rt_cpu_self(void)
+{
+ return &rt_cpus[rt_hw_cpu_id()];
+}
+
+struct rt_cpu *rt_cpu_index(int index)
+{
+ return &rt_cpus[index];
+}
+
+/**
+ * This function will lock all cpus's scheduler and disable local irq.
+ */
+rt_base_t rt_cpus_lock(void)
+{
+ rt_base_t level;
+ struct rt_cpu* pcpu;
+
+ level = rt_hw_local_irq_disable();
+
+ pcpu = rt_cpu_self();
+ if (pcpu->current_thread != RT_NULL)
+ {
+ if (pcpu->current_thread->cpus_lock_nest++ == 0)
+ {
+ pcpu->current_thread->scheduler_lock_nest++;
+ rt_hw_spin_lock(&_cpus_lock);
+ }
+ }
+ return level;
+}
+RTM_EXPORT(rt_cpus_lock);
+
+/**
+ * This function will restore all cpus's scheduler and restore local irq.
+ */
+void rt_cpus_unlock(rt_base_t level)
+{
+ struct rt_cpu* pcpu = rt_cpu_self();
+
+ if (pcpu->current_thread != RT_NULL)
+ {
+ if (--pcpu->current_thread->cpus_lock_nest == 0)
+ {
+ pcpu->current_thread->scheduler_lock_nest--;
+ rt_hw_spin_unlock(&_cpus_lock);
+ }
+ }
+ rt_hw_local_irq_enable(level);
+}
+RTM_EXPORT(rt_cpus_unlock);
+
+/**
+ * This function is invoked by scheduler.
+ * It will restore the lock state to whatever the thread's counter expects.
+ * If target thread not locked the cpus then unlock the cpus lock.
+ */
+void rt_cpus_lock_status_restore(struct rt_thread *thread)
+{
+ struct rt_cpu* pcpu = rt_cpu_self();
+
+ pcpu->current_thread = thread;
+ if (!thread->cpus_lock_nest)
+ {
+ rt_hw_spin_unlock(&_cpus_lock);
+ }
+}
+RTM_EXPORT(rt_post_switch);
+
+#endif
diff --git a/src/device.c b/src/device.c
index 0b662ae620..51fc6176a6 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1,21 +1,7 @@
/*
- * File : device.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
diff --git a/src/idle.c b/src/idle.c
index d6c2f98a49..d02b199f32 100644
--- a/src/idle.c
+++ b/src/idle.c
@@ -1,21 +1,7 @@
/*
- * File : idle.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -27,11 +13,17 @@
* 2016-08-09 ArdaFu add method to get the handler of the idle thread.
* 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
* 2018-07-14 armink add idle hook list
+ * 2018-11-22 Jesven add per cpu idle task
+ * combine the code of primary and secondary cpu
*/
#include <rthw.h>
#include <rtthread.h>
+#ifdef RT_USING_MODULE
+#include <dlmodule.h>
+#endif
+
#if defined (RT_USING_HOOK)
#ifndef RT_USING_IDLE_HOOK
#define RT_USING_IDLE_HOOK
@@ -46,16 +38,21 @@
#endif
#endif
-static struct rt_thread idle;
-ALIGN(RT_ALIGN_SIZE)
-static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
+#ifdef RT_USING_SMP
+#define _CPUS_NR RT_CPUS_NR
+#else
+#define _CPUS_NR 1
+#endif
extern rt_list_t rt_thread_defunct;
-#ifdef RT_USING_IDLE_HOOK
+static struct rt_thread idle[_CPUS_NR];
+ALIGN(RT_ALIGN_SIZE)
+static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];
+#ifdef RT_USING_IDLE_HOOK
#ifndef RT_IDEL_HOOK_LIST_SIZE
-#define RT_IDEL_HOOK_LIST_SIZE 4
+#define RT_IDEL_HOOK_LIST_SIZE 4
#endif
static void (*idle_hook_list[RT_IDEL_HOOK_LIST_SIZE])();
@@ -158,7 +155,7 @@ void rt_thread_idle_excute(void)
rt_base_t lock;
rt_thread_t thread;
#ifdef RT_USING_MODULE
- rt_module_t module = RT_NULL;
+ struct rt_dlmodule *module = RT_NULL;
#endif
RT_DEBUG_NOT_IN_INTERRUPT;
@@ -173,14 +170,10 @@ void rt_thread_idle_excute(void)
struct rt_thread,
tlist);
#ifdef RT_USING_MODULE
- /* get thread's parent module */
- module = (rt_module_t)thread->module_id;
-
- /* if the thread is module's main thread */
- if (module != RT_NULL && module->module_thread == thread)
+ module = (struct rt_dlmodule*)thread->module_id;
+ if (module)
{
- /* detach module's main thread */
- module->module_thread = RT_NULL;
+ dlmodule_destroy(module);
}
#endif
/* remove defunct thread */
@@ -235,14 +228,21 @@ void rt_thread_idle_excute(void)
static void rt_thread_idle_entry(void *parameter)
{
-#ifdef RT_USING_IDLE_HOOK
- rt_size_t i;
+#ifdef RT_USING_SMP
+ if (rt_hw_cpu_id() != 0)
+ {
+ while (1)
+ {
+ rt_hw_secondary_cpu_idle_exec();
+ }
+ }
#endif
while (1)
{
-
#ifdef RT_USING_IDLE_HOOK
+ rt_size_t i;
+
for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
{
if (idle_hook_list[i] != RT_NULL)
@@ -265,18 +265,26 @@ static void rt_thread_idle_entry(void *parameter)
*/
void rt_thread_idle_init(void)
{
- /* initialize thread */
- rt_thread_init(&idle,
- "tidle",
- rt_thread_idle_entry,
- RT_NULL,
- &rt_thread_stack[0],
- sizeof(rt_thread_stack),
- RT_THREAD_PRIORITY_MAX - 1,
- 32);
+ int i;
+ char tidle_name[RT_NAME_MAX];
- /* startup */
- rt_thread_startup(&idle);
+ for (i = 0; i < _CPUS_NR; i++)
+ {
+ rt_sprintf(tidle_name, "tidle%d", i);
+ rt_thread_init(&idle[i],
+ tidle_name,
+ rt_thread_idle_entry,
+ RT_NULL,
+ &rt_thread_stack[i][0],
+ sizeof(rt_thread_stack[i]),
+ RT_THREAD_PRIORITY_MAX - 1,
+ 32);
+#ifdef RT_USING_SMP
+ rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
+#endif
+ /* startup */
+ rt_thread_startup(&idle[i]);
+ }
}
/**
@@ -287,5 +295,11 @@ void rt_thread_idle_init(void)
*/
rt_thread_t rt_thread_idle_gethandler(void)
{
- return (rt_thread_t)(&idle);
+#ifdef RT_USING_SMP
+ register int id = rt_hw_cpu_id();
+#else
+ register int id = 0;
+#endif
+
+ return (rt_thread_t)(&idle[id]);
}
diff --git a/src/ipc.c b/src/ipc.c
index b1a5dbe402..7f603cfc4d 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -1,21 +1,7 @@
/*
- * File : ipc.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -46,6 +32,7 @@
* 2010-11-10 Bernard add IPC reset command implementation.
* 2011-12-18 Bernard add more parameter checking in message queue
* 2013-09-14 Grissiom add an option check in rt_event_recv
+ * 2018-10-02 Bernard add 64bit support for mailbox
*/
#include <rtthread.h>
@@ -500,10 +487,10 @@ rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)
if (cmd == RT_IPC_CMD_RESET)
{
- rt_uint32_t value;
+ rt_ubase_t value;
/* get value */
- value = (rt_uint32_t)arg;
+ value = (rt_ubase_t)arg;
/* disable interrupt */
level = rt_hw_interrupt_disable();
@@ -1387,7 +1374,7 @@ rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
/* init mailbox */
mb->size = size;
- mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t));
+ mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
if (mb->msg_pool == RT_NULL)
{
/* delete mailbox object */
@@ -1450,7 +1437,7 @@ RTM_EXPORT(rt_mb_delete);
* @return the error code
*/
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
- rt_uint32_t value,
+ rt_ubase_t value,
rt_int32_t timeout)
{
struct rt_thread *thread;
@@ -1581,7 +1568,7 @@ RTM_EXPORT(rt_mb_send_wait);
*
* @return the error code
*/
-rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
+rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)
{
return rt_mb_send_wait(mb, value, 0);
}
@@ -1597,7 +1584,7 @@ RTM_EXPORT(rt_mb_send);
*
* @return the error code
*/
-rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
+rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
{
struct rt_thread *thread;
register rt_ubase_t temp;
diff --git a/src/irq.c b/src/irq.c
index 9b933e3960..ee03204819 100644
--- a/src/irq.c
+++ b/src/irq.c
@@ -1,27 +1,14 @@
/*
- * File : irq.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2006-02-24 Bernard first version
* 2006-05-03 Bernard add IRQ_DEBUG
* 2016-08-09 ArdaFu add interrupt enter and leave hook.
+ * 2018-11-22 Jesven rt_interrupt_get_nest function add disable irq
*/
#include <rthw.h>
@@ -62,7 +49,11 @@ void rt_interrupt_leave_sethook(void (*hook)(void))
/**@{*/
+#ifdef RT_USING_SMP
+#define rt_interrupt_nest rt_cpu_self()->irq_nest
+#else
volatile rt_uint8_t rt_interrupt_nest;
+#endif
/**
* This function will be invoked by BSP, when enter interrupt service routine
@@ -116,7 +107,13 @@ RTM_EXPORT(rt_interrupt_leave);
*/
rt_uint8_t rt_interrupt_get_nest(void)
{
- return rt_interrupt_nest;
+ rt_uint8_t ret;
+ rt_base_t level;
+
+ level = rt_hw_interrupt_disable();
+ ret = rt_interrupt_nest;
+ rt_hw_interrupt_enable(level);
+ return ret;
}
RTM_EXPORT(rt_interrupt_get_nest);
diff --git a/src/kservice.c b/src/kservice.c
index fa82ccdd80..c938f4110a 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -1,21 +1,7 @@
/*
- * File : kservice.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -36,6 +22,10 @@
#include <rtthread.h>
#include <rthw.h>
+#ifdef RT_USING_MODULE
+#include <dlmodule.h>
+#endif
+
/* use precision */
#define RT_PRINTF_PRECISION
@@ -143,20 +133,21 @@ void *rt_memset(void *s, int c, rt_ubase_t count)
return s;
#else
-#define LBLOCKSIZE (sizeof(rt_int32_t))
-#define UNALIGNED(X) ((rt_int32_t)X & (LBLOCKSIZE - 1))
+#define LBLOCKSIZE (sizeof(long))
+#define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
- int i;
+ unsigned int i;
char *m = (char *)s;
- rt_uint32_t buffer;
- rt_uint32_t *aligned_addr;
- rt_uint32_t d = c & 0xff;
+ unsigned long buffer;
+ unsigned long *aligned_addr;
+ unsigned int d = c & 0xff; /* To avoid sign extension, copy C to an
+ unsigned variable. */
if (!TOO_SMALL(count) && !UNALIGNED(s))
{
/* If we get this far, we know that n is large and m is word-aligned. */
- aligned_addr = (rt_uint32_t *)s;
+ aligned_addr = (unsigned long *)s;
/* Store D into each char sized location in BUFFER so that
* we can set large blocks quickly.
@@ -236,25 +227,24 @@ void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
return dst;
#else
-#define UNALIGNED(X, Y) \
- (((rt_int32_t)X & (sizeof(rt_int32_t) - 1)) | \
- ((rt_int32_t)Y & (sizeof(rt_int32_t) - 1)))
-#define BIGBLOCKSIZE (sizeof(rt_int32_t) << 2)
-#define LITTLEBLOCKSIZE (sizeof(rt_int32_t))
+#define UNALIGNED(X, Y) \
+ (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
+#define BIGBLOCKSIZE (sizeof (long) << 2)
+#define LITTLEBLOCKSIZE (sizeof (long))
#define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
char *dst_ptr = (char *)dst;
char *src_ptr = (char *)src;
- rt_int32_t *aligned_dst;
- rt_int32_t *aligned_src;
+ long *aligned_dst;
+ long *aligned_src;
int len = count;
/* If the size is small, or either SRC or DST is unaligned,
then punt into the byte copy loop. This should be rare. */
if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
{
- aligned_dst = (rt_int32_t *)dst_ptr;
- aligned_src = (rt_int32_t *)src_ptr;
+ aligned_dst = (long *)dst_ptr;
+ aligned_src = (long *)src_ptr;
/* Copy 4X long words at a time if possible. */
while (len >= BIGBLOCKSIZE)
@@ -531,7 +521,7 @@ char *rt_strdup(const char *s)
return tmp;
}
RTM_EXPORT(rt_strdup);
-#ifdef __CC_ARM
+#if defined(__CC_ARM) || defined(__CLANG_ARM)
char *strdup(const char *s) __attribute__((alias("rt_strdup")));
#endif
#endif
@@ -552,20 +542,20 @@ RTM_EXPORT(rt_show_version);
/* private function */
#define isdigit(c) ((unsigned)((c) - '0') < 10)
-rt_inline rt_int32_t divide(rt_int32_t *n, rt_int32_t base)
+rt_inline int divide(long *n, int base)
{
- rt_int32_t res;
+ int res;
/* optimized for processor which does not support divide instructions. */
if (base == 10)
{
- res = ((rt_uint32_t) * n) % 10U;
- *n = ((rt_uint32_t) * n) / 10U;
+ res = (int)(((unsigned long)*n) % 10U);
+ *n = (long)(((unsigned long)*n) / 10U);
}
else
{
- res = ((rt_uint32_t) * n) % 16U;
- *n = ((rt_uint32_t) * n) / 16U;
+ res = (int)(((unsigned long)*n) % 16U);
+ *n = (long)(((unsigned long)*n) / 16U);
}
return res;
@@ -1199,31 +1189,36 @@ RTM_EXPORT(rt_kprintf);
*/
void *rt_malloc_align(rt_size_t size, rt_size_t align)
{
- void *align_ptr;
void *ptr;
+ void *align_ptr;
+ int uintptr_size;
rt_size_t align_size;
- /* align the alignment size to 4 byte */
- align = ((align + 0x03) & ~0x03);
+ /* sizeof pointer */
+ uintptr_size = sizeof(void*);
+ uintptr_size -= 1;
+
+ /* align the alignment size to uintptr size byte */
+ align = ((align + uintptr_size) & ~uintptr_size);
/* get total aligned size */
- align_size = ((size + 0x03) & ~0x03) + align;
+ align_size = ((size + uintptr_size) & ~uintptr_size) + align;
/* allocate memory block from heap */
ptr = rt_malloc(align_size);
if (ptr != RT_NULL)
{
/* the allocated memory block is aligned */
- if (((rt_uint32_t)ptr & (align - 1)) == 0)
+ if (((rt_ubase_t)ptr & (align - 1)) == 0)
{
- align_ptr = (void *)((rt_uint32_t)ptr + align);
+ align_ptr = (void *)((rt_ubase_t)ptr + align);
}
else
{
- align_ptr = (void *)(((rt_uint32_t)ptr + (align - 1)) & ~(align - 1));
+ align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
}
/* set the pointer before alignment pointer to the real pointer */
- *((rt_uint32_t *)((rt_uint32_t)align_ptr - sizeof(void *))) = (rt_uint32_t)ptr;
+ *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
ptr = align_ptr;
}
@@ -1242,7 +1237,7 @@ void rt_free_align(void *ptr)
{
void *real_ptr;
- real_ptr = (void *) * (rt_uint32_t *)((rt_uint32_t)ptr - sizeof(void *));
+ real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
rt_free(real_ptr);
}
RTM_EXPORT(rt_free_align);
@@ -1323,13 +1318,10 @@ void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
if (rt_assert_hook == RT_NULL)
{
#ifdef RT_USING_MODULE
- if (rt_module_self() != RT_NULL)
+ if (dlmodule_self())
{
- /* unload assertion module */
- rt_module_unload(rt_module_self());
-
- /* re-schedule */
- rt_schedule();
+ /* close assertion module */
+ dlmodule_exit(-1);
}
else
#endif
diff --git a/src/mem.c b/src/mem.c
index 324a5f8f64..ca5329404e 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -1,21 +1,7 @@
/*
- * File : mem.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -25,6 +11,7 @@
* 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
* 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer.
* 2017-07-14 armink fix rt_realloc issue when new size is 0
+ * 2018-10-02 Bernard Add 64bit support
*/
/*
@@ -111,12 +98,19 @@ struct heap_mem
/* magic and used flag */
rt_uint16_t magic;
rt_uint16_t used;
+#ifdef ARCH_CPU_64BIT
+ rt_uint32_t resv;
+#endif
rt_size_t next, prev;
#ifdef RT_USING_MEMTRACE
+#ifdef ARCH_CPU_64BIT
+ rt_uint8_t thread[8];
+#else
rt_uint8_t thread[4]; /* thread name */
#endif
+#endif
};
/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
@@ -125,7 +119,12 @@ static rt_uint8_t *heap_ptr;
/** the last entry, always unused! */
static struct heap_mem *heap_end;
+#ifdef ARCH_CPU_64BIT
+#define MIN_SIZE 24
+#else
#define MIN_SIZE 12
+#endif
+
#define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
#define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE)
@@ -205,8 +204,8 @@ static void plug_holes(struct heap_mem *mem)
void rt_system_heap_init(void *begin_addr, void *end_addr)
{
struct heap_mem *mem;
- rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
- rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
+ rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
+ rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
RT_DEBUG_NOT_IN_INTERRUPT;
@@ -220,7 +219,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr)
else
{
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
- (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
+ (rt_ubase_t)begin_addr, (rt_ubase_t)end_addr);
return;
}
@@ -229,7 +228,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr)
heap_ptr = (rt_uint8_t *)begin_align;
RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
- (rt_uint32_t)heap_ptr, mem_size_aligned));
+ (rt_ubase_t)heap_ptr, mem_size_aligned));
/* initialize the start of the heap */
mem = (struct heap_mem *)heap_ptr;
@@ -275,11 +274,11 @@ void *rt_malloc(rt_size_t size)
rt_size_t ptr, ptr2;
struct heap_mem *mem, *mem2;
- RT_DEBUG_NOT_IN_INTERRUPT;
-
if (size == 0)
return RT_NULL;
+ RT_DEBUG_NOT_IN_INTERRUPT;
+
if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
size, RT_ALIGN(size, RT_ALIGN_SIZE)));
@@ -388,14 +387,14 @@ void *rt_malloc(rt_size_t size)
}
rt_sem_release(&heap_sem);
- RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
- RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
- RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
+ RT_ASSERT((rt_ubase_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_ubase_t)heap_end);
+ RT_ASSERT((rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
+ RT_ASSERT((((rt_ubase_t)mem) & (RT_ALIGN_SIZE - 1)) == 0);
RT_DEBUG_LOG(RT_DEBUG_MEM,
("allocate memory at 0x%x, size: %d\n",
- (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
- (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
+ (rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
+ (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
RT_OBJECT_HOOK_CALL(rt_malloc_hook,
(((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
@@ -527,8 +526,6 @@ void *rt_calloc(rt_size_t count, rt_size_t size)
{
void *p;
- RT_DEBUG_NOT_IN_INTERRUPT;
-
/* allocate 'count' objects of size 'size' */
p = rt_malloc(count * size);
@@ -550,11 +547,12 @@ void rt_free(void *rmem)
{
struct heap_mem *mem;
- RT_DEBUG_NOT_IN_INTERRUPT;
-
if (rmem == RT_NULL)
return;
- RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
+
+ RT_DEBUG_NOT_IN_INTERRUPT;
+
+ RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr &&
(rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);
@@ -573,8 +571,8 @@ void rt_free(void *rmem)
RT_DEBUG_LOG(RT_DEBUG_MEM,
("release memory 0x%x, size: %d\n",
- (rt_uint32_t)rmem,
- (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
+ (rt_ubase_t)rmem,
+ (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
/* protect the heap from concurrent access */
@@ -639,12 +637,12 @@ FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
int memcheck(void)
{
int position;
- rt_uint32_t level;
+ rt_ubase_t level;
struct heap_mem *mem;
level = rt_hw_interrupt_disable();
for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next])
{
- position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr;
+ position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr;
if (position < 0) goto __exit;
if (position > mem_size_aligned) goto __exit;
if (mem->magic != HEAP_MAGIC) goto __exit;
@@ -679,7 +677,7 @@ int memtrace(int argc, char **argv)
rt_kprintf("\n--memory item information --\n");
for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next])
{
- int position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr;
+ int position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr;
int size;
rt_kprintf("[0x%08x - ", mem);
diff --git a/src/memheap.c b/src/memheap.c
index de637f2685..eb5d710029 100644
--- a/src/memheap.c
+++ b/src/memheap.c
@@ -1,21 +1,11 @@
/*
- * File : memheap.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2012, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/*
+ * File : memheap.c
*
* Change Logs:
* Date Author Notes
@@ -60,7 +50,7 @@
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
const char *name,
void *start_addr,
- rt_uint32_t size)
+ rt_size_t size)
{
struct rt_memheap_item *item;
@@ -145,7 +135,7 @@ rt_err_t rt_memheap_detach(struct rt_memheap *heap)
}
RTM_EXPORT(rt_memheap_detach);
-void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size)
+void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
{
rt_err_t result;
rt_uint32_t free_size;
diff --git a/src/mempool.c b/src/mempool.c
index cd98e8c14e..2184586aba 100644
--- a/src/mempool.c
+++ b/src/mempool.c
@@ -1,21 +1,7 @@
/*
- * File : mempool.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
diff --git a/src/module.c b/src/module.c
deleted file mode 100644
index 89ab7735bd..0000000000
--- a/src/module.c
+++ /dev/null
@@ -1,1289 +0,0 @@
-/*
- * File : module.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date Author Notes
- * 2010-01-09 Bernard first version
- * 2010-04-09 yi.qiu implement based on first version
- * 2010-10-23 yi.qiu implement module memory allocator
- * 2011-05-25 yi.qiu implement module hook function
- * 2011-06-23 yi.qiu rewrite module memory allocator
- * 2012-11-23 Bernard using RT_DEBUG_LOG instead of rt_kprintf.
- * 2012-11-28 Bernard remove rt_current_module and user
- * can use rt_module_unload to remove a module.
- * 2017-08-20 parai support intel 386 machine
- */
-
-#include <rthw.h>
-#include <rtthread.h>
-#include <rtm.h>
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-#endif
-
-#ifdef RT_USING_MODULE
-#include "module.h"
-
-#define elf_module ((Elf32_Ehdr *)module_ptr)
-#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
-#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
-
-#define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
-#define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
-#define IS_REL(s) (s.sh_type == SHT_REL)
-#define IS_RELA(s) (s.sh_type == SHT_RELA)
-#define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
-#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
-#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
-
-#ifdef RT_USING_MODULE_STKSZ
-#undef RT_USING_MODULE_STKSZ
-#endif
-
-#ifndef RT_USING_MODULE_STKSZ
-#define RT_USING_MODULE_STKSZ (4096 * 2)
-#endif
-
-#ifndef RT_USING_MODULE_PRIO
-#define RT_USING_MODULE_PRIO (RT_THREAD_PRIORITY_MAX - 2)
-#endif
-
-static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
-static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
-
-#if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
- #pragma section="RTMSymTab"
-#endif
-
-/**
- * @ingroup SystemInit
- *
- * This function will initialize system module
- */
-int rt_system_module_init(void)
-{
-#if defined(__GNUC__) && !defined(__CC_ARM)
- extern int __rtmsymtab_start;
- extern int __rtmsymtab_end;
-
- _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
- _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
-#elif defined (__CC_ARM)
- extern int RTMSymTab$$Base;
- extern int RTMSymTab$$Limit;
-
- _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
- _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
-#elif defined (__IAR_SYSTEMS_ICC__)
- _rt_module_symtab_begin = __section_begin("RTMSymTab");
- _rt_module_symtab_end = __section_end("RTMSymTab");
-#endif
-
- return 0;
-}
-INIT_COMPONENT_EXPORT(rt_system_module_init);
-
-#ifdef RT_USING_FINSH
-void list_symbol(void)
-{
- /* find in kernel symbol table */
- struct rt_module_symtab *index;
-
- for (index = _rt_module_symtab_begin;
- index != _rt_module_symtab_end;
- index ++)
- {
- rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
- }
-
- return ;
-}
-FINSH_FUNCTION_EXPORT(list_symbol, list symbol for module);
-MSH_CMD_EXPORT(list_symbol, list symbol for module);
-#endif
-
-static rt_uint32_t rt_module_symbol_find(const char *sym_str)
-{
- /* find in kernel symbol table */
- struct rt_module_symtab *index;
-
- for (index = _rt_module_symtab_begin;
- index != _rt_module_symtab_end;
- index ++)
- {
- if (rt_strcmp(index->name, sym_str) == 0)
- return (rt_uint32_t)index->addr;
- }
-
- return 0;
-}
-
-/**
- * This function will return self module object
- *
- * @return the self module object
- */
-rt_module_t rt_module_self(void)
-{
- rt_thread_t tid;
-
- tid = rt_thread_self();
- if (tid == RT_NULL)
- return RT_NULL;
-
- /* return current module */
- return (rt_module_t)tid->module_id;
-}
-RTM_EXPORT(rt_module_self);
-
-static int rt_module_arm_relocate(struct rt_module *module,
- Elf32_Rel *rel,
- Elf32_Addr sym_val)
-{
- Elf32_Addr *where, tmp;
- Elf32_Sword addend, offset;
- rt_uint32_t upper, lower, sign, j1, j2;
-
- where = (Elf32_Addr *)((rt_uint8_t *)module->module_space
- + rel->r_offset
- - module->vstart_addr);
- switch (ELF32_R_TYPE(rel->r_info))
- {
- case R_ARM_NONE:
- break;
- case R_ARM_ABS32:
- *where += (Elf32_Addr)sym_val;
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n",
- where, *where));
- break;
- case R_ARM_PC24:
- case R_ARM_PLT32:
- case R_ARM_CALL:
- case R_ARM_JUMP24:
- addend = *where & 0x00ffffff;
- if (addend & 0x00800000)
- addend |= 0xff000000;
- tmp = sym_val - (Elf32_Addr)where + (addend << 2);
- tmp >>= 2;
- *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n",
- where, *where));
- break;
- case R_ARM_REL32:
- *where += sym_val - (Elf32_Addr)where;
- RT_DEBUG_LOG(RT_DEBUG_MODULE,
- ("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
- where, *where, sym_val, rel->r_offset));
- break;
- case R_ARM_V4BX:
- *where &= 0xf000000f;
- *where |= 0x01a0f000;
- break;
-#ifdef MODULE_USING_386
- case R_386_GLOB_DAT:
- case R_386_JUMP_SLOT:
-#endif
- case R_ARM_GLOB_DAT:
- case R_ARM_JUMP_SLOT:
- *where = (Elf32_Addr)sym_val;
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n",
- where, *where, sym_val));
- break;
-#if 0 /* To do */
- case R_ARM_GOT_BREL:
- temp = (Elf32_Addr)sym_val;
- *where = (Elf32_Addr)&temp;
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n",
- where, *where, sym_val));
- break;
-#endif
-#ifdef MODULE_USING_386
- case R_386_RELATIVE:
-#endif
- case R_ARM_RELATIVE:
- *where = (Elf32_Addr)sym_val + *where;
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n",
- where, *where, sym_val));
- break;
- case R_ARM_THM_CALL:
- case R_ARM_THM_JUMP24:
- upper = *(rt_uint16_t *)where;
- lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
-
- sign = (upper >> 10) & 1;
- j1 = (lower >> 13) & 1;
- j2 = (lower >> 11) & 1;
- offset = (sign << 24) |
- ((~(j1 ^ sign) & 1) << 23) |
- ((~(j2 ^ sign) & 1) << 22) |
- ((upper & 0x03ff) << 12) |
- ((lower & 0x07ff) << 1);
- if (offset & 0x01000000)
- offset -= 0x02000000;
- offset += sym_val - (Elf32_Addr)where;
-
- if (!(offset & 1) ||
- offset <= (rt_int32_t)0xff000000 ||
- offset >= (rt_int32_t)0x01000000)
- {
- rt_kprintf("Module: Only Thumb addresses allowed\n");
-
- return -1;
- }
-
- sign = (offset >> 24) & 1;
- j1 = sign ^ (~(offset >> 23) & 1);
- j2 = sign ^ (~(offset >> 22) & 1);
- *(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
- (sign << 10) |
- ((offset >> 12) & 0x03ff));
- *(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
- (j1 << 13) | (j2 << 11) |
- ((offset >> 1) & 0x07ff));
- upper = *(rt_uint16_t *)where;
- lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
- break;
- default:
- return -1;
- }
-
- return 0;
-}
-
-#ifdef RT_USING_HOOK
-static void (*rt_module_load_hook)(rt_module_t module);
-static void (*rt_module_unload_hook)(rt_module_t module);
-
-/**
- * @addtogroup Hook
- */
-
-/**@{*/
-
-/**
- * This function will set a hook function, which will be invoked when module
- * be loaded to system.
- *
- * @param hook the hook function
- */
-void rt_module_load_sethook(void (*hook)(rt_module_t module))
-{
- rt_module_load_hook = hook;
-}
-
-/**
- * This function will set a hook function, which will be invoked when module
- * be unloaded from system.
- *
- * @param hook the hook function
- */
-void rt_module_unload_sethook(void (*hook)(rt_module_t module))
-{
- rt_module_unload_hook = hook;
-}
-
-/**@}*/
-#endif
-
-static struct rt_module *_load_shared_object(const char *name,
- void *module_ptr)
-{
- rt_module_t module = RT_NULL;
- rt_bool_t linked = RT_FALSE;
- rt_uint32_t index, module_size = 0;
- Elf32_Addr vstart_addr, vend_addr;
- rt_bool_t has_vstart;
-
- RT_ASSERT(module_ptr != RT_NULL);
-
- if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
- {
- /* rtmlinker finished */
- linked = RT_TRUE;
- }
-
- /* get the ELF image size */
- has_vstart = RT_FALSE;
- vstart_addr = vend_addr = RT_NULL;
- for (index = 0; index < elf_module->e_phnum; index++)
- {
- if (phdr[index].p_type != PT_LOAD)
- continue;
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("LOAD segment: %d, 0x%p, 0x%08x\n",
- index, phdr[index].p_vaddr, phdr[index].p_memsz));
-
- if (phdr[index].p_memsz < phdr[index].p_filesz)
- {
- rt_kprintf("invalid elf: segment %d: p_memsz: %d, p_filesz: %d\n",
- index, phdr[index].p_memsz, phdr[index].p_filesz);
- return RT_NULL;
- }
- if (!has_vstart)
- {
- vstart_addr = phdr[index].p_vaddr;
- vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
- has_vstart = RT_TRUE;
- if (vend_addr < vstart_addr)
- {
- rt_kprintf("invalid elf: segment %d: p_vaddr: %d, p_memsz: %d\n",
- index, phdr[index].p_vaddr, phdr[index].p_memsz);
- return RT_NULL;
- }
- }
- else
- {
- if (phdr[index].p_vaddr < vend_addr)
- {
- rt_kprintf("invalid elf: segment should be sorted and not overlapped\n");
- return RT_NULL;
- }
- if (phdr[index].p_vaddr > vend_addr + 16)
- {
- /* There should not be too much padding in the object files. */
- rt_kprintf("warning: too much padding before segment %d\n", index);
- }
-
- vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
- if (vend_addr < phdr[index].p_vaddr)
- {
- rt_kprintf("invalid elf: "
- "segment %d address overflow\n", index);
- return RT_NULL;
- }
- }
- }
-
- module_size = vend_addr - vstart_addr;
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("module size: %d, vstart_addr: 0x%p\n",
- module_size, vstart_addr));
-
- if (module_size == 0)
- {
- rt_kprintf("Module: size error\n");
-
- return RT_NULL;
- }
-
- /* allocate module */
- module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
- name);
- if (!module)
- return RT_NULL;
-
- module->vstart_addr = vstart_addr;
-
- module->nref = 0;
-
- /* allocate module space */
- module->module_space = rt_malloc(module_size);
- if (module->module_space == RT_NULL)
- {
- rt_kprintf("Module: allocate space failed.\n");
- rt_object_delete(&(module->parent));
-
- return RT_NULL;
- }
-
- /* zero all space */
- rt_memset(module->module_space, 0, module_size);
-
- for (index = 0; index < elf_module->e_phnum; index++)
- {
- if (phdr[index].p_type == PT_LOAD)
- {
- rt_memcpy(module->module_space + phdr[index].p_vaddr - vstart_addr,
- (rt_uint8_t *)elf_module + phdr[index].p_offset,
- phdr[index].p_filesz);
- }
- }
-
- /* set module entry */
- module->module_entry = module->module_space
- + elf_module->e_entry - vstart_addr;
-
- /* handle relocation section */
- for (index = 0; index < elf_module->e_shnum; index ++)
- {
- rt_uint32_t i, nr_reloc;
- Elf32_Sym *symtab;
- Elf32_Rel *rel;
- rt_uint8_t *strtab;
- static rt_bool_t unsolved = RT_FALSE;
-
- if (!IS_REL(shdr[index]))
- continue;
-
- /* get relocate item */
- rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
-
- /* locate .rel.plt and .rel.dyn section */
- symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
- shdr[shdr[index].sh_link].sh_offset);
- strtab = (rt_uint8_t *)module_ptr +
- shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
- nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
-
- /* relocate every items */
- for (i = 0; i < nr_reloc; i ++)
- {
- Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
- strtab + sym->st_name,
- sym->st_shndx));
-
- if ((sym->st_shndx != SHT_NULL) ||
- (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
-#ifdef MODULE_USING_386
- || ( (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) && (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) )
-#endif
- )
- {
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)(module->module_space
- + sym->st_value
- - vstart_addr));
- }
- else if (!linked)
- {
- Elf32_Addr addr;
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
- strtab + sym->st_name));
-
- /* need to resolve symbol in kernel symbol table */
- addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
- if (addr == 0)
- {
- rt_kprintf("Module: can't find %s in kernel symbol table\n",
- strtab + sym->st_name);
- unsolved = RT_TRUE;
- }
- else
- rt_module_arm_relocate(module, rel, addr);
- }
- rel ++;
- }
-
- if (unsolved)
- {
- rt_object_delete(&(module->parent));
-
- return RT_NULL;
- }
- }
-
- /* construct module symbol table */
- for (index = 0; index < elf_module->e_shnum; index ++)
- {
- /* find .dynsym section */
- rt_uint8_t *shstrab;
- shstrab = (rt_uint8_t *)module_ptr +
- shdr[elf_module->e_shstrndx].sh_offset;
- if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
- break;
- }
-
- /* found .dynsym section */
- if (index != elf_module->e_shnum)
- {
- int i, count = 0;
- Elf32_Sym *symtab = RT_NULL;
- rt_uint8_t *strtab = RT_NULL;
-
- symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
- strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
-
- for (i = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++)
- {
- if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
- (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
- count ++;
- }
-
- module->symtab = (struct rt_module_symtab *)rt_malloc
- (count * sizeof(struct rt_module_symtab));
- module->nsym = count;
- for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++)
- {
- rt_size_t length;
-
- if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
- (ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
- continue;
-
- length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
-
- module->symtab[count].addr =
- (void *)(module->module_space + symtab[i].st_value - module->vstart_addr);
- module->symtab[count].name = rt_malloc(length);
- rt_memset((void *)module->symtab[count].name, 0, length);
- rt_memcpy((void *)module->symtab[count].name,
- strtab + symtab[i].st_name,
- length);
- count ++;
- }
- }
-
- return module;
-}
-
-static struct rt_module* _load_relocated_object(const char *name,
- void *module_ptr)
-{
- rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
- rt_uint32_t module_addr = 0, module_size = 0;
- struct rt_module *module = RT_NULL;
- rt_uint8_t *ptr, *strtab, *shstrab;
-
- /* get the ELF image size */
- for (index = 0; index < elf_module->e_shnum; index ++)
- {
- /* text */
- if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
- {
- module_size += shdr[index].sh_size;
- module_addr = shdr[index].sh_addr;
- }
- /* rodata */
- if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
- {
- module_size += shdr[index].sh_size;
- }
- /* data */
- if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
- {
- module_size += shdr[index].sh_size;
- }
- /* bss */
- if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
- {
- module_size += shdr[index].sh_size;
- }
- }
-
- /* no text, data and bss on image */
- if (module_size == 0)
- return RT_NULL;
-
- /* allocate module */
- module = (struct rt_module *)
- rt_object_allocate(RT_Object_Class_Module, (const char *)name);
- if (module == RT_NULL)
- return RT_NULL;
-
- module->vstart_addr = 0;
-
- /* allocate module space */
- module->module_space = rt_malloc(module_size);
- if (module->module_space == RT_NULL)
- {
- rt_kprintf("Module: allocate space failed.\n");
- rt_object_delete(&(module->parent));
-
- return RT_NULL;
- }
-
- /* zero all space */
- ptr = module->module_space;
- rt_memset(ptr, 0, module_size);
-
- /* load text and data section */
- for (index = 0; index < elf_module->e_shnum; index ++)
- {
- /* load text section */
- if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
- {
- rt_memcpy(ptr,
- (rt_uint8_t *)elf_module + shdr[index].sh_offset,
- shdr[index].sh_size);
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load text 0x%x, size %d\n",
- ptr, shdr[index].sh_size));
- ptr += shdr[index].sh_size;
- }
-
- /* load rodata section */
- if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
- {
- rt_memcpy(ptr,
- (rt_uint8_t *)elf_module + shdr[index].sh_offset,
- shdr[index].sh_size);
- rodata_addr = (rt_uint32_t)ptr;
- RT_DEBUG_LOG(RT_DEBUG_MODULE,
- ("load rodata 0x%x, size %d, rodata 0x%x\n",
- ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
- ptr += shdr[index].sh_size;
- }
-
- /* load data section */
- if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
- {
- rt_memcpy(ptr,
- (rt_uint8_t *)elf_module + shdr[index].sh_offset,
- shdr[index].sh_size);
- data_addr = (rt_uint32_t)ptr;
- RT_DEBUG_LOG(RT_DEBUG_MODULE,
- ("load data 0x%x, size %d, data 0x%x\n",
- ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
- ptr += shdr[index].sh_size;
- }
-
- /* load bss section */
- if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
- {
- rt_memset(ptr, 0, shdr[index].sh_size);
- bss_addr = (rt_uint32_t)ptr;
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load bss 0x%x, size %d,\n",
- ptr, shdr[index].sh_size));
- }
- }
-
- /* set module entry */
- module->module_entry =
- (rt_uint8_t *)module->module_space + elf_module->e_entry - module_addr;
-
- /* handle relocation section */
- for (index = 0; index < elf_module->e_shnum; index ++)
- {
- rt_uint32_t i, nr_reloc;
- Elf32_Sym *symtab;
- Elf32_Rel *rel;
-
- if (!IS_REL(shdr[index]))
- continue;
-
- /* get relocate item */
- rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
-
- /* locate .dynsym and .dynstr */
- symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
- shdr[shdr[index].sh_link].sh_offset);
- strtab = (rt_uint8_t *)module_ptr +
- shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
- shstrab = (rt_uint8_t *)module_ptr +
- shdr[elf_module->e_shstrndx].sh_offset;
- nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
-
- /* relocate every items */
- for (i = 0; i < nr_reloc; i ++)
- {
- Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
- strtab + sym->st_name));
-
- if (sym->st_shndx != STN_UNDEF)
- {
- if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
- (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
- {
- if (rt_strncmp((const char *)(shstrab +
- shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
- {
- /* relocate rodata section */
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rodata\n"));
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)(rodata_addr + sym->st_value));
- }
- else if (rt_strncmp((const char *)
- (shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
- {
- /* relocate bss section */
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("bss\n"));
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)bss_addr + sym->st_value);
- }
- else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
- ELF_DATA, 6) == 0)
- {
- /* relocate data section */
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("data\n"));
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)data_addr + sym->st_value);
- }
- }
- else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
- {
- /* relocate function */
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)((rt_uint8_t *)
- module->module_space
- - module_addr
- + sym->st_value));
- }
- }
- else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
- {
- /* relocate function */
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)((rt_uint8_t *)
- module->module_space
- - module_addr
- + sym->st_value));
- }
- else
- {
- Elf32_Addr addr;
-
- if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
- {
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
- strtab + sym->st_name));
-
- /* need to resolve symbol in kernel symbol table */
- addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
- if (addr != (Elf32_Addr)RT_NULL)
- {
- rt_module_arm_relocate(module, rel, addr);
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("symbol addr 0x%x\n",
- addr));
- }
- else
- rt_kprintf("Module: can't find %s in kernel symbol table\n",
- strtab + sym->st_name);
- }
- else
- {
- rt_module_arm_relocate(module, rel,
- (Elf32_Addr)((rt_uint8_t *)
- module->module_space
- - module_addr
- + sym->st_value));
- }
- }
- rel ++;
- }
- }
-
- return module;
-}
-
-#define RT_MODULE_ARG_MAX 8
-static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
-{
- int argc = 0;
- char *ptr = cmd;
-
- while ((ptr - cmd) < length)
- {
- /* strip bank and tab */
- while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
- *ptr++ = '\0';
- /* check whether it's the end of line */
- if ((ptr - cmd) >= length) break;
-
- /* handle string with quote */
- if (*ptr == '"')
- {
- argv[argc++] = ++ptr;
-
- /* skip this string */
- while (*ptr != '"' && (ptr - cmd) < length)
- if (*ptr ++ == '\\') ptr ++;
- if ((ptr - cmd) >= length) break;
-
- /* skip '"' */
- *ptr ++ = '\0';
- }
- else
- {
- argv[argc++] = ptr;
- while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
- ptr ++;
- }
-
- if (argc >= RT_MODULE_ARG_MAX) break;
- }
-
- return argc;
-}
-
-/* module main thread entry */
-static void module_main_entry(void *parameter)
-{
- int argc;
- char *argv[RT_MODULE_ARG_MAX];
- typedef int (*main_func_t)(int argc, char **argv);
-
- rt_module_t module = (rt_module_t) parameter;
- if (module == RT_NULL)
- return;
-
- if (module->module_cmd_line == RT_NULL && module->module_cmd_size != 0)
- /* malloc for module_cmd_line failed. */
- return;
-
- /* FIXME: we should run some C++ initialize code before jump into the
- * entry. */
-
- if (module->module_cmd_line == RT_NULL)
- {
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("run bare entry: 0x%p\n",
- module->module_entry));
- ((main_func_t)module->module_entry)(0, RT_NULL);
- return;
- }
-
- rt_memset(argv, 0x00, sizeof(argv));
- argc = _rt_module_split_arg((char *)module->module_cmd_line,
- module->module_cmd_size, argv);
- if (argc == 0)
- return;
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("run main entry: 0x%p with %s\n",
- module->module_entry,
- module->module_cmd_line));
- /* do the main function */
- ((main_func_t)module->module_entry)(argc, argv);
- return;
-}
-
-/**
- * This function will load a module with a main function from memory and create a
- * main thread for it
- *
- * @param name the name of module, which shall be unique
- * @param module_ptr the memory address of module image
- * @argc the count of argument
- * @argd the argument data, which should be a
- *
- * @return the module object
- */
-rt_module_t rt_module_do_main(const char *name,
- void *module_ptr,
- const char *cmd_line,
- int line_size)
-{
- rt_module_t module;
-
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_load: %s\n", name));
-
- /* check ELF header */
- if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
- rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
- {
- rt_kprintf("Module: magic error\n");
-
- return RT_NULL;
- }
-
- /* check ELF class */
- if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
- {
- rt_kprintf("Module: ELF class error\n");
-
- return RT_NULL;
- }
-
- if (elf_module->e_type == ET_REL)
- {
- module = _load_relocated_object(name, module_ptr);
- }
- else if (elf_module->e_type == ET_DYN)
- {
- module = _load_shared_object(name, module_ptr);
- }
- else
- {
- rt_kprintf("Module: unsupported elf type\n");
-
- return RT_NULL;
- }
-
- if (module == RT_NULL)
- return RT_NULL;
-
- if (line_size && cmd_line)
- {
- /* set module argument */
- module->module_cmd_line = (rt_uint8_t *)rt_malloc(line_size + 1);
- if (module->module_cmd_line)
- {
- rt_memcpy(module->module_cmd_line, cmd_line, line_size);
- module->module_cmd_line[line_size] = '\0';
- }
- module->module_cmd_size = line_size;
- }
- else
- {
- /* initialize an empty command */
- module->module_cmd_line = RT_NULL;
- module->module_cmd_size = 0;
- }
-
- /* increase module reference count */
- module->nref ++;
-
- if (elf_module->e_entry != 0)
- {
- /* create module thread */
- module->module_thread = rt_thread_create(name,
- module_main_entry, module,
- RT_USING_MODULE_STKSZ,
- RT_USING_MODULE_PRIO, 10);
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n",
- module->module_entry));
-
- /* set module id */
- module->module_thread->module_id = (void *)module;
- module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
-
- /* startup module thread */
- rt_thread_startup(module->module_thread);
- }
- else
- {
- /* without entry point */
- module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
- }
-
-#ifdef RT_USING_HOOK
- if (rt_module_load_hook != RT_NULL)
- {
- rt_module_load_hook(module);
- }
-#endif
-
- return module;
-}
-
-/**
- * This function will load a module from memory and create a thread for it
- *
- * @param name the name of module, which shall be unique
- * @param module_ptr the memory address of module image
- *
- * @return the module object
- */
-rt_module_t rt_module_load(const char *name, void *module_ptr)
-{
- return rt_module_do_main(name, module_ptr, RT_NULL, 0);
-}
-
-#ifdef RT_USING_DFS
-#include <dfs_posix.h>
-
-static char *_module_name(const char *path)
-{
- const char *first, *end, *ptr;
- char *name;
- int size;
-
- ptr = (char *)path;
- first = ptr;
- end = path + rt_strlen(path);
-
- while (*ptr != '\0')
- {
- if (*ptr == '/')
- first = ptr + 1;
- if (*ptr == '.')
- end = ptr - 1;
-
- ptr ++;
- }
-
- size = end - first + 1;
- name = rt_malloc(size);
- rt_strncpy(name, first, size);
- name[size] = '\0';
-
- return name;
-}
-
-/**
- * This function will load a module from a file
- *
- * @param path the full path of application module
- *
- * @return the module object
- */
-rt_module_t rt_module_open(const char *path)
-{
- int fd, length;
- struct rt_module *module;
- struct stat s;
- char *buffer, *offset_ptr;
- char *name;
-
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* check parameters */
- RT_ASSERT(path != RT_NULL);
-
- if (stat(path, &s) != 0)
- {
- rt_kprintf("Module: access %s failed\n", path);
-
- return RT_NULL;
- }
- buffer = (char *)rt_malloc(s.st_size);
- if (buffer == RT_NULL)
- {
- rt_kprintf("Module: out of memory\n");
-
- return RT_NULL;
- }
-
- offset_ptr = buffer;
- fd = open(path, O_RDONLY, 0);
- if (fd < 0)
- {
- rt_kprintf("Module: open %s failed\n", path);
- rt_free(buffer);
-
- return RT_NULL;
- }
-
- do
- {
- length = read(fd, offset_ptr, 4096);
- if (length > 0)
- {
- offset_ptr += length;
- }
- }while (length > 0);
-
- /* close fd */
- close(fd);
-
- if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
- {
- rt_kprintf("Module: read file failed\n");
- rt_free(buffer);
-
- return RT_NULL;
- }
-
- name = _module_name(path);
- module = rt_module_load(name, (void *)buffer);
- rt_free(buffer);
- rt_free(name);
-
- return module;
-}
-
-/**
- * This function will do a excutable program with main function and parameters.
- *
- * @param path the full path of application module
- * @param cmd_line the command line of program
- * @param size the size of command line of program
- *
- * @return the module object
- */
-rt_module_t rt_module_exec_cmd(const char *path, const char *cmd_line, int size)
-{
- struct stat s;
- int fd, length;
- char *name, *buffer, *offset_ptr;
- struct rt_module *module = RT_NULL;
-
- name = buffer = RT_NULL;
-
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* check parameters */
- RT_ASSERT(path != RT_NULL);
-
- /* get file size */
- if (stat(path, &s) != 0)
- {
- rt_kprintf("Module: access %s failed\n", path);
- goto __exit;
- }
-
- /* allocate buffer to save program */
- offset_ptr = buffer = (char *)rt_malloc(s.st_size);
- if (buffer == RT_NULL)
- {
- rt_kprintf("Module: out of memory\n");
- goto __exit;
- }
-
- fd = open(path, O_RDONLY, 0);
- if (fd < 0)
- {
- rt_kprintf("Module: open %s failed\n", path);
- goto __exit;
- }
-
- do
- {
- length = read(fd, offset_ptr, 4096);
- if (length > 0)
- {
- offset_ptr += length;
- }
- }while (length > 0);
- /* close fd */
- close(fd);
-
- if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
- {
- rt_kprintf("Module: read file failed\n");
- goto __exit;
- }
-
- /* get module */
- name = _module_name(path);
- /* execute module */
- module = rt_module_do_main(name, (void *)buffer, cmd_line, size);
-
-__exit:
- rt_free(buffer);
- rt_free(name);
-
- return module;
-}
-
-#if defined(RT_USING_FINSH)
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
-#endif
-
-#endif
-
-/**
- * This function will destroy a module and release its resource.
- *
- * @param module the module to be destroyed.
- *
- * @return the operation status, RT_EOK on OK; -RT_ERROR on error
- */
-rt_err_t rt_module_destroy(rt_module_t module)
-{
- int i;
-
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* check parameter */
- RT_ASSERT(module != RT_NULL);
- RT_ASSERT(module->nref == 0);
- RT_ASSERT(rt_object_get_type(&module->parent) == RT_Object_Class_Module);
-
- RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_destroy: %8.*s\n",
- RT_NAME_MAX, module->parent.name));
-
- /* module has entry point */
- if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
- {
- /* delete command line */
- if (module->module_cmd_line != RT_NULL)
- {
- rt_free(module->module_cmd_line);
- }
- }
-
- /* release module space memory */
- rt_free(module->module_space);
-
- /* release module symbol table */
- for (i = 0; i < module->nsym; i ++)
- {
- rt_free((void *)module->symtab[i].name);
- }
- if (module->symtab != RT_NULL)
- rt_free(module->symtab);
-
- /* delete module object */
- rt_object_delete((rt_object_t)module);
-
- return RT_EOK;
-}
-
-/**
- * This function will unload a module from memory and release resources
- *
- * @param module the module to be unloaded
- *
- * @return the operation status, RT_EOK on OK; -RT_ERROR on error
- */
-rt_err_t rt_module_unload(rt_module_t module)
-{
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* check parameter */
- if (module == RT_NULL)
- return -RT_ERROR;
-
- rt_enter_critical();
- /* invoke module cleanup */
- rt_exit_critical();
-
-#ifdef RT_USING_HOOK
- if (rt_module_unload_hook != RT_NULL)
- {
- rt_module_unload_hook(module);
- }
-#endif
-
- return RT_EOK;
-}
-
-/**
- * This function will find the specified module.
- *
- * @param name the name of module finding
- *
- * @return the module
- */
-rt_module_t rt_module_find(const char *name)
-{
- struct rt_object_information *information;
- struct rt_object *object;
- struct rt_list_node *node;
-
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* enter critical */
- rt_enter_critical();
-
- /* try to find device object */
- information = rt_object_get_information(RT_Object_Class_Module);
- RT_ASSERT(information != RT_NULL);
- for (node = information->object_list.next;
- node != &(information->object_list);
- node = node->next)
- {
- object = rt_list_entry(node, struct rt_object, list);
- if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
- {
- /* leave critical */
- rt_exit_critical();
-
- return (rt_module_t)object;
- }
- }
-
- /* leave critical */
- rt_exit_critical();
-
- /* not found */
- return RT_NULL;
-}
-RTM_EXPORT(rt_module_find);
-
-#endif
diff --git a/src/module.h b/src/module.h
deleted file mode 100644
index 90d4dc9524..0000000000
--- a/src/module.h
+++ /dev/null
@@ -1,277 +0,0 @@
-/*
- * File : module.h
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date Author Notes
- * 2010-01-09 Bernard first version
- * 2010-04-09 yi.qiu implement based on first version
- */
-
-#ifndef __MODULE_H__
-#define __MODULE_H__
-
-#include <rtdef.h>
-
-typedef rt_uint8_t Elf_Byte;
-
-typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */
-typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */
-typedef rt_int32_t Elf32_Sword; /* Signed large integer */
-typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */
-typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
-
-/* e_ident[] magic number */
-#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
-#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
-#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
-#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
-#define RTMMAG "\177RTM" /* magic */
-#define ELFMAG "\177ELF" /* magic */
-#define SELFMAG 4 /* size of magic */
-
-#define EI_CLASS 4 /* file class */
-#define EI_NIDENT 16 /* Size of e_ident[] */
-
-/* e_ident[] file class */
-#define ELFCLASSNONE 0 /* invalid */
-#define ELFCLASS32 1 /* 32-bit objs */
-#define ELFCLASS64 2 /* 64-bit objs */
-#define ELFCLASSNUM 3 /* number of classes */
-
-/* e_ident[] data encoding */
-#define ELFDATANONE 0 /* invalid */
-#define ELFDATA2LSB 1 /* Little-Endian */
-#define ELFDATA2MSB 2 /* Big-Endian */
-#define ELFDATANUM 3 /* number of data encode defines */
-
-/* e_ident */
-#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
- (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
- (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
- (ehdr).e_ident[EI_MAG3] == ELFMAG3)
-
-#define ET_NONE 0 /* No file type */
-#define ET_REL 1 /* Relocatable file */
-#define ET_EXEC 2 /* Executable file */
-#define ET_DYN 3 /* Shared object file */
-#define ET_CORE 4 /* Core file */
-
-/* ELF Header */
-typedef struct elfhdr
-{
- unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
- Elf32_Half e_type; /* object file type */
- Elf32_Half e_machine; /* machine */
- Elf32_Word e_version; /* object file version */
- Elf32_Addr e_entry; /* virtual entry point */
- Elf32_Off e_phoff; /* program header table offset */
- Elf32_Off e_shoff; /* section header table offset */
- Elf32_Word e_flags; /* processor-specific flags */
- Elf32_Half e_ehsize; /* ELF header size */
- Elf32_Half e_phentsize; /* program header entry size */
- Elf32_Half e_phnum; /* number of program header entries */
- Elf32_Half e_shentsize; /* section header entry size */
- Elf32_Half e_shnum; /* number of section header entries */
- Elf32_Half e_shstrndx; /* section header table's "section
- header string table" entry offset */
-} Elf32_Ehdr;
-
-/* Section Header */
-typedef struct
-{
- Elf32_Word sh_name; /* name - index into section header
- string table section */
- Elf32_Word sh_type; /* type */
- Elf32_Word sh_flags; /* flags */
- Elf32_Addr sh_addr; /* address */
- Elf32_Off sh_offset; /* file offset */
- Elf32_Word sh_size; /* section size */
- Elf32_Word sh_link; /* section header table index link */
- Elf32_Word sh_info; /* extra information */
- Elf32_Word sh_addralign; /* address alignment */
- Elf32_Word sh_entsize; /* section entry size */
-} Elf32_Shdr;
-
-/* Section names */
-#define ELF_BSS ".bss" /* uninitialized data */
-#define ELF_DATA ".data" /* initialized data */
-#define ELF_DEBUG ".debug" /* debug */
-#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
-#define ELF_DYNSTR ".dynstr" /* dynamic string table */
-#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
-#define ELF_FINI ".fini" /* termination code */
-#define ELF_GOT ".got" /* global offset table */
-#define ELF_HASH ".hash" /* symbol hash table */
-#define ELF_INIT ".init" /* initialization code */
-#define ELF_REL_DATA ".rel.data" /* relocation data */
-#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
-#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
-#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
-#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
-#define ELF_REL_TEXT ".rel.text" /* relocation code */
-#define ELF_RODATA ".rodata" /* read-only data */
-#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
-#define ELF_STRTAB ".strtab" /* string table */
-#define ELF_SYMTAB ".symtab" /* symbol table */
-#define ELF_TEXT ".text" /* code */
-#define ELF_RTMSYMTAB "RTMSymTab"
-
-/* Symbol Table Entry */
-typedef struct elf32_sym
-{
- Elf32_Word st_name; /* name - index into string table */
- Elf32_Addr st_value; /* symbol value */
- Elf32_Word st_size; /* symbol size */
- unsigned char st_info; /* type and binding */
- unsigned char st_other; /* 0 - no defined meaning */
- Elf32_Half st_shndx; /* section header index */
-} Elf32_Sym;
-
-#define STB_LOCAL 0 /* BIND */
-#define STB_GLOBAL 1
-#define STB_WEAK 2
-#define STB_NUM 3
-
-#define STB_LOPROC 13 /* processor specific range */
-#define STB_HIPROC 15
-
-#define STT_NOTYPE 0 /* symbol type is unspecified */
-#define STT_OBJECT 1 /* data object */
-#define STT_FUNC 2 /* code object */
-#define STT_SECTION 3 /* symbol identifies an ELF section */
-#define STT_FILE 4 /* symbol's name is file name */
-#define STT_COMMON 5 /* common data object */
-#define STT_TLS 6 /* thread-local data object */
-#define STT_NUM 7 /* # defined types in generic range */
-#define STT_LOOS 10 /* OS specific range */
-#define STT_HIOS 12
-#define STT_LOPROC 13 /* processor specific range */
-#define STT_HIPROC 15
-
-#define STN_UNDEF 0 /* undefined */
-
-#define ELF_ST_BIND(info) ((info) >> 4)
-#define ELF_ST_TYPE(info) ((info) & 0xf)
-#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
-
-/* Relocation entry with implicit addend */
-typedef struct
-{
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
-} Elf32_Rel;
-
-/* Relocation entry with explicit addend */
-typedef struct
-{
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
- Elf32_Sword r_addend;
-} Elf32_Rela;
-
-/* Extract relocation info - r_info */
-#define ELF32_R_SYM(i) ((i) >> 8)
-#define ELF32_R_TYPE(i) ((unsigned char) (i))
-#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
-
-/*
- * Relocation type for arm
- */
-#define R_ARM_NONE 0
-#define R_ARM_PC24 1
-#define R_ARM_ABS32 2
-#define R_ARM_REL32 3
-#define R_ARM_THM_CALL 10
-#define R_ARM_GLOB_DAT 21
-#define R_ARM_JUMP_SLOT 22
-#define R_ARM_RELATIVE 23
-#define R_ARM_GOT_BREL 26
-#define R_ARM_PLT32 27
-#define R_ARM_CALL 28
-#define R_ARM_JUMP24 29
-#define R_ARM_THM_JUMP24 30
-#define R_ARM_V4BX 40
-
-/*
- * Relocation type for arm
- */
-#define R_386_GLOB_DAT 6
-#define R_386_JUMP_SLOT 7
-#define R_386_RELATIVE 8
-
-/* Program Header */
-typedef struct
-{
- Elf32_Word p_type; /* segment type */
- Elf32_Off p_offset; /* segment offset */
- Elf32_Addr p_vaddr; /* virtual address of segment */
- Elf32_Addr p_paddr; /* physical address - ignored? */
- Elf32_Word p_filesz; /* number of bytes in file for seg. */
- Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
- Elf32_Word p_flags; /* flags */
- Elf32_Word p_align; /* memory alignment */
-} Elf32_Phdr;
-
-/* p_type */
-#define PT_NULL 0
-#define PT_LOAD 1
-#define PT_DYNAMIC 2
-#define PT_INTERP 3
-#define PT_NOTE 4
-#define PT_SHLIB 5
-#define PT_PHDR 6
-#define PT_TLS 7
-#define PT_NUM 8
-#define PT_LOOS 0x60000000
-#define PT_HIOS 0x6fffffff
-#define PT_LOPROC 0x70000000
-#define PT_HIPROC 0x7fffffff
-
-/* p_flags */
-#define PF_X 1
-#define PF_W 2
-#define PF_R 4
-
-/* sh_type */
-#define SHT_NULL 0 /* inactive */
-#define SHT_PROGBITS 1 /* program defined information */
-#define SHT_SYMTAB 2 /* symbol table section */
-#define SHT_STRTAB 3 /* string table section */
-#define SHT_RELA 4 /* relocation section with addends*/
-#define SHT_HASH 5 /* symbol hash table section */
-#define SHT_DYNAMIC 6 /* dynamic section */
-#define SHT_NOTE 7 /* note section */
-#define SHT_NOBITS 8 /* no space section */
-#define SHT_REL 9 /* relocation section without addends */
-#define SHT_SHLIB 10 /* reserved - purpose unknown */
-#define SHT_DYNSYM 11 /* dynamic symbol table section */
-#define SHT_NUM 12 /* number of section types */
-#define SHT_LOPROC 0x70000000 /* reserved range for processor */
-#define SHT_HIPROC 0x7fffffff /* specific section header types */
-#define SHT_LOUSER 0x80000000 /* reserved range for application */
-#define SHT_HIUSER 0xffffffff /* specific indexes */
-
-/* Section Attribute Flags - sh_flags */
-#define SHF_WRITE 0x1 /* Writable */
-#define SHF_ALLOC 0x2 /* occupies memory */
-#define SHF_EXECINSTR 0x4 /* executable */
-#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
-/* specific section attributes */
-
-#endif
diff --git a/src/object.c b/src/object.c
index b09af9ddee..70b9a5c04e 100644
--- a/src/object.c
+++ b/src/object.c
@@ -1,21 +1,7 @@
/*
- * File : object.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -32,6 +18,10 @@
#include <rtthread.h>
#include <rthw.h>
+#ifdef RT_USING_MODULE
+#include <dlmodule.h>
+#endif
+
/*
* define object_info for the number of rt_object_container items.
*/
@@ -111,7 +101,7 @@ static struct rt_object_information rt_object_container[RT_Object_Info_Unknown]
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
/* initialize object container - module */
- {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_module)},
+ {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
#endif
};
@@ -251,6 +241,9 @@ void rt_object_init(struct rt_object *object,
{
register rt_base_t temp;
struct rt_object_information *information;
+#ifdef RT_USING_MODULE
+ struct rt_dlmodule *module = dlmodule_self();
+#endif
/* get object information */
information = rt_object_get_information(type);
@@ -269,8 +262,18 @@ void rt_object_init(struct rt_object *object,
/* lock interrupt */
temp = rt_hw_interrupt_disable();
- /* insert object into information object list */
- rt_list_insert_after(&(information->object_list), &(object->list));
+#ifdef RT_USING_MODULE
+ if (module)
+ {
+ rt_list_insert_after(&(module->object_list), &(object->list));
+ object->module_id = (void *)module;
+ }
+ else
+#endif
+ {
+ /* insert object into information object list */
+ rt_list_insert_after(&(information->object_list), &(object->list));
+ }
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
@@ -318,6 +321,9 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
struct rt_object *object;
register rt_base_t temp;
struct rt_object_information *information;
+#ifdef RT_USING_MODULE
+ struct rt_dlmodule *module = dlmodule_self();
+#endif
RT_DEBUG_NOT_IN_INTERRUPT;
@@ -332,6 +338,9 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
return RT_NULL;
}
+ /* clean memory data of object */
+ rt_memset(object, 0x0, information->object_size);
+
/* initialize object's parameters */
/* set object type */
@@ -340,14 +349,6 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
/* set object flag */
object->flag = 0;
-#ifdef RT_USING_MODULE
- if (rt_module_self() != RT_NULL)
- {
- object->flag |= RT_OBJECT_FLAG_MODULE;
- }
- object->module_id = (void *)rt_module_self();
-#endif
-
/* copy name */
rt_strncpy(object->name, name, RT_NAME_MAX);
@@ -356,8 +357,18 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
/* lock interrupt */
temp = rt_hw_interrupt_disable();
- /* insert object into information object list */
- rt_list_insert_after(&(information->object_list), &(object->list));
+#ifdef RT_USING_MODULE
+ if (module)
+ {
+ rt_list_insert_after(&(module->object_list), &(object->list));
+ object->module_id = (void *)module;
+ }
+ else
+#endif
+ {
+ /* insert object into information object list */
+ rt_list_insert_after(&(information->object_list), &(object->list));
+ }
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
diff --git a/src/scheduler.c b/src/scheduler.c
index 1be1816bc2..e177bde6b7 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -1,21 +1,7 @@
/*
- * File : scheduler.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -35,28 +21,36 @@
* 2010-12-13 Bernard add defunct list initialization even if not use heap.
* 2011-05-10 Bernard clean scheduler debug log.
* 2013-12-21 Grissiom add rt_critical_level
+ * 2018-11-22 Jesven remove the current task from ready queue
+ * add per cpu ready queue
+ * add _get_highest_priority_thread to find highest priority task
+ * rt_schedule_insert_thread won't insert current task to ready queue
+ * in smp version, rt_hw_context_switch_interrupt maybe switch to
+ * new task directly
+ *
*/
#include <rtthread.h>
#include <rthw.h>
-static rt_int16_t rt_scheduler_lock_nest;
-extern volatile rt_uint8_t rt_interrupt_nest;
+#ifdef RT_USING_SMP
+rt_hw_spinlock_t _rt_critical_lock;
+#endif /*RT_USING_SMP*/
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
-struct rt_thread *rt_current_thread;
-
-rt_uint8_t rt_current_priority;
-
+rt_uint32_t rt_thread_ready_priority_group;
#if RT_THREAD_PRIORITY_MAX > 32
/* Maximum priority level, 256 */
-rt_uint32_t rt_thread_ready_priority_group;
rt_uint8_t rt_thread_ready_table[32];
-#else
-/* Maximum priority level, 32 */
-rt_uint32_t rt_thread_ready_priority_group;
#endif
+#ifndef RT_USING_SMP
+extern volatile rt_uint8_t rt_interrupt_nest;
+static rt_int16_t rt_scheduler_lock_nest;
+struct rt_thread *rt_current_thread;
+rt_uint8_t rt_current_priority;
+#endif /*RT_USING_SMP*/
+
rt_list_t rt_thread_defunct;
#ifdef RT_USING_HOOK
@@ -89,11 +83,11 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread)
RT_ASSERT(thread != RT_NULL);
if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
- (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
- (rt_uint32_t)thread->sp >
- (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
+ (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
+ (rt_ubase_t)thread->sp >
+ (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
{
- rt_uint32_t level;
+ rt_ubase_t level;
rt_kprintf("thread:%s stack overflow\n", thread->name);
#ifdef RT_USING_FINSH
@@ -105,7 +99,7 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread)
level = rt_hw_interrupt_disable();
while (level);
}
- else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
+ else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
{
rt_kprintf("warning: %s stack is close to end of stack address.\n",
thread->name);
@@ -113,15 +107,93 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread)
}
#endif
+/*
+ * get the highest priority thread in ready queue
+ */
+#ifdef RT_USING_SMP
+static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio)
+{
+ register struct rt_thread *highest_priority_thread;
+ register rt_ubase_t highest_ready_priority, local_highest_ready_priority;
+ struct rt_cpu* pcpu = rt_cpu_self();
+
+#if RT_THREAD_PRIORITY_MAX > 32
+ register rt_ubase_t number;
+
+ if (rt_thread_ready_priority_group == 0 && pcpu->priority_group == 0)
+ {
+ *highest_prio = pcpu->current_thread->current_priority;
+ /* only local IDLE is readly */
+ return pcpu->current_thread;
+ }
+
+ number = __rt_ffs(rt_thread_ready_priority_group) - 1;
+ highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
+ number = __rt_ffs(pcpu->priority_group) - 1;
+ local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;
+#else
+ highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
+ local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1;
+#endif
+
+ /* get highest ready priority thread */
+ if (highest_ready_priority < local_highest_ready_priority)
+ {
+ *highest_prio = highest_ready_priority;
+ highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
+ struct rt_thread,
+ tlist);
+ }
+ else
+ {
+ *highest_prio = local_highest_ready_priority;
+ highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next,
+ struct rt_thread,
+ tlist);
+ }
+
+ return highest_priority_thread;
+}
+#else
+static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio)
+{
+ register struct rt_thread *highest_priority_thread;
+ register rt_ubase_t highest_ready_priority;
+
+#if RT_THREAD_PRIORITY_MAX > 32
+ register rt_ubase_t number;
+
+ number = __rt_ffs(rt_thread_ready_priority_group) - 1;
+ highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
+#else
+ highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
+#endif
+
+ /* get highest ready priority thread */
+ highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
+ struct rt_thread,
+ tlist);
+
+ *highest_prio = highest_ready_priority;
+
+ return highest_priority_thread;
+}
+#endif
+
/**
* @ingroup SystemInit
* This function will initialize the system scheduler
*/
void rt_system_scheduler_init(void)
{
+#ifdef RT_USING_SMP
+ int cpu;
+#endif /*RT_USING_SMP*/
register rt_base_t offset;
+#ifndef RT_USING_SMP
rt_scheduler_lock_nest = 0;
+#endif /*RT_USING_SMP*/
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
RT_THREAD_PRIORITY_MAX));
@@ -130,9 +202,25 @@ void rt_system_scheduler_init(void)
{
rt_list_init(&rt_thread_priority_table[offset]);
}
+#ifdef RT_USING_SMP
+ for (cpu = 0; cpu < RT_CPUS_NR; cpu++)
+ {
+ struct rt_cpu *pcpu = rt_cpu_index(cpu);
+ for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
+ {
+ rt_list_init(&pcpu->priority_table[offset]);
+ }
+
+ pcpu->irq_switch_flag = 0;
+ pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;
+ pcpu->current_thread = RT_NULL;
+ pcpu->priority_group = 0;
- rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
- rt_current_thread = RT_NULL;
+#if RT_THREAD_PRIORITY_MAX > 32
+ rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));
+#endif
+ }
+#endif /*RT_USING_SMP*/
/* initialize ready priority group */
rt_thread_ready_priority_group = 0;
@@ -154,26 +242,24 @@ void rt_system_scheduler_init(void)
void rt_system_scheduler_start(void)
{
register struct rt_thread *to_thread;
- register rt_ubase_t highest_ready_priority;
+ rt_ubase_t highest_ready_priority;
-#if RT_THREAD_PRIORITY_MAX > 32
- register rt_ubase_t number;
+ to_thread = _get_highest_priority_thread(&highest_ready_priority);
- number = __rt_ffs(rt_thread_ready_priority_group) - 1;
- highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
+#ifdef RT_USING_SMP
+ to_thread->oncpu = rt_hw_cpu_id();
#else
- highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
-#endif
-
- /* get switch to thread */
- to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
- struct rt_thread,
- tlist);
-
rt_current_thread = to_thread;
+#endif /*RT_USING_SMP*/
+
+ rt_schedule_remove_thread(to_thread);
/* switch to new thread */
+#ifdef RT_USING_SMP
+ rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp, to_thread);
+#else
rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
+#endif /*RT_USING_SMP*/
/* never come back */
}
@@ -184,6 +270,114 @@ void rt_system_scheduler_start(void)
/**@{*/
+
+#ifdef RT_USING_SMP
+/**
+ * This function will handle IPI interrupt and do a scheduling in system;
+ *
+ * @param vector, the number of IPI interrupt for system scheduling
+ * @param param, use RT_NULL
+ *
+ * NOTE: this function should be invoke or register as ISR in BSP.
+ */
+void rt_scheduler_ipi_handler(int vector, void *param)
+{
+ rt_schedule();
+}
+
+/**
+ * This function will perform one scheduling. It will select one thread
+ * with the highest priority level in global ready queue or local ready queue,
+ * then switch to it.
+ */
+void rt_schedule(void)
+{
+ rt_base_t level;
+ struct rt_thread *to_thread;
+ struct rt_thread *current_thread;
+ struct rt_cpu *pcpu;
+ int cpu_id;
+
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
+
+ cpu_id = rt_hw_cpu_id();
+ pcpu = rt_cpu_index(cpu_id);
+ current_thread = pcpu->current_thread;
+
+ /* whether do switch in interrupt */
+ if (pcpu->irq_nest)
+ {
+ pcpu->irq_switch_flag = 1;
+ }
+ else if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */
+ {
+ rt_ubase_t highest_ready_priority;
+
+ if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
+ {
+ to_thread = _get_highest_priority_thread(&highest_ready_priority);
+ current_thread->oncpu = RT_CPU_DETACHED;
+ if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
+ {
+ if (current_thread->current_priority < highest_ready_priority)
+ {
+ to_thread = current_thread;
+ }
+ else
+ {
+ rt_schedule_insert_thread(current_thread);
+ }
+ }
+ to_thread->oncpu = cpu_id;
+ if (to_thread != current_thread)
+ {
+ /* if the destination thread is not the same as current thread */
+ pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
+
+ RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
+
+ rt_schedule_remove_thread(to_thread);
+
+ /* switch to new thread */
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
+ ("[%d]switch to priority#%d "
+ "thread:%.*s(sp:0x%08x), "
+ "from thread:%.*s(sp: 0x%08x)\n",
+ pcpu->irq_nest, highest_ready_priority,
+ RT_NAME_MAX, to_thread->name, to_thread->sp,
+ RT_NAME_MAX, current_thread->name, current_thread->sp));
+
+#ifdef RT_USING_OVERFLOW_CHECK
+ _rt_scheduler_stack_check(to_thread);
+#endif
+
+ {
+ extern void rt_thread_handle_sig(rt_bool_t clean_state);
+
+ rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
+ (rt_ubase_t)&to_thread->sp, to_thread);
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+
+#ifdef RT_USING_SIGNALS
+ /* check signal status */
+ rt_thread_handle_sig(RT_TRUE);
+#endif
+ goto __exit;
+ }
+ }
+ }
+ }
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+
+__exit:
+ return ;
+}
+#else
/**
* This function will perform one schedule. It will select one thread
* with the highest priority level, then switch to it.
@@ -200,81 +394,169 @@ void rt_schedule(void)
/* check the scheduler is enabled or not */
if (rt_scheduler_lock_nest == 0)
{
- register rt_ubase_t highest_ready_priority;
+ rt_ubase_t highest_ready_priority;
-#if RT_THREAD_PRIORITY_MAX <= 32
- highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
-#else
- register rt_ubase_t number;
+ if (rt_thread_ready_priority_group != 0)
+ {
+ int need_insert_from_thread = 0;
- number = __rt_ffs(rt_thread_ready_priority_group) - 1;
- highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
-#endif
+ to_thread = _get_highest_priority_thread(&highest_ready_priority);
- /* get switch to thread */
- to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
- struct rt_thread,
- tlist);
+ if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
+ {
+ if (rt_current_thread->current_priority < highest_ready_priority)
+ {
+ to_thread = rt_current_thread;
+ }
+ else
+ {
+ need_insert_from_thread = 1;
+ }
+ }
- /* if the destination thread is not the same as current thread */
- if (to_thread != rt_current_thread)
- {
- rt_current_priority = (rt_uint8_t)highest_ready_priority;
- from_thread = rt_current_thread;
- rt_current_thread = to_thread;
+ if (to_thread != rt_current_thread)
+ {
+ /* if the destination thread is not the same as current thread */
+ rt_current_priority = (rt_uint8_t)highest_ready_priority;
+ from_thread = rt_current_thread;
+ rt_current_thread = to_thread;
+
+ RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
- RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
+ if (need_insert_from_thread)
+ {
+ rt_schedule_insert_thread(from_thread);
+ }
- /* switch to new thread */
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("[%d]switch to priority#%d "
- "thread:%.*s(sp:0x%p), "
- "from thread:%.*s(sp: 0x%p)\n",
- rt_interrupt_nest, highest_ready_priority,
- RT_NAME_MAX, to_thread->name, to_thread->sp,
- RT_NAME_MAX, from_thread->name, from_thread->sp));
+ rt_schedule_remove_thread(to_thread);
+
+ /* switch to new thread */
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
+ ("[%d]switch to priority#%d "
+ "thread:%.*s(sp:0x%p), "
+ "from thread:%.*s(sp: 0x%p)\n",
+ rt_interrupt_nest, highest_ready_priority,
+ RT_NAME_MAX, to_thread->name, to_thread->sp,
+ RT_NAME_MAX, from_thread->name, from_thread->sp));
#ifdef RT_USING_OVERFLOW_CHECK
- _rt_scheduler_stack_check(to_thread);
+ _rt_scheduler_stack_check(to_thread);
#endif
- if (rt_interrupt_nest == 0)
- {
- extern void rt_thread_handle_sig(rt_bool_t clean_state);
+ if (rt_interrupt_nest == 0)
+ {
+ extern void rt_thread_handle_sig(rt_bool_t clean_state);
- rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
- (rt_uint32_t)&to_thread->sp);
+ rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
+ (rt_ubase_t)&to_thread->sp);
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
#ifdef RT_USING_SIGNALS
- /* check signal status */
- rt_thread_handle_sig(RT_TRUE);
+ /* check signal status */
+ rt_thread_handle_sig(RT_TRUE);
#endif
+ goto __exit;
+ }
+ else
+ {
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
+
+ rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
+ (rt_ubase_t)&to_thread->sp);
+ }
}
else
{
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
-
- rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
- (rt_uint32_t)&to_thread->sp);
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
+ rt_schedule_remove_thread(rt_current_thread);
}
}
- else
- {
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- }
}
- else
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+
+__exit:
+ return;
+}
+#endif /*RT_USING_SMP*/
+
+/**
+ * This function checks if a scheduling is needed after IRQ context. If yes,
+ * it will select one thread with the highest priority level, and then switch
+ * to it.
+ */
+#ifdef RT_USING_SMP
+void rt_scheduler_do_irq_switch(void *context)
+{
+ int cpu_id;
+ rt_base_t level;
+ struct rt_cpu* pcpu;
+ struct rt_thread *to_thread;
+ struct rt_thread *current_thread;
+
+ level = rt_hw_interrupt_disable();
+
+ cpu_id = rt_hw_cpu_id();
+ pcpu = rt_cpu_index(cpu_id);
+ current_thread = pcpu->current_thread;
+
+ if (pcpu->irq_switch_flag == 0)
{
- /* enable interrupt */
rt_hw_interrupt_enable(level);
+ return;
}
+
+ if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)
+ {
+ rt_ubase_t highest_ready_priority;
+
+ /* clear irq switch flag */
+ pcpu->irq_switch_flag = 0;
+
+ if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
+ {
+ to_thread = _get_highest_priority_thread(&highest_ready_priority);
+ current_thread->oncpu = RT_CPU_DETACHED;
+ if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
+ {
+ if (current_thread->current_priority < highest_ready_priority)
+ {
+ to_thread = current_thread;
+ }
+ else
+ {
+ rt_schedule_insert_thread(current_thread);
+ }
+ }
+ to_thread->oncpu = cpu_id;
+ if (to_thread != current_thread)
+ {
+ /* if the destination thread is not the same as current thread */
+
+ pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
+
+ RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
+
+ rt_schedule_remove_thread(to_thread);
+
+#ifdef RT_USING_OVERFLOW_CHECK
+ _rt_scheduler_stack_check(to_thread);
+#endif
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
+
+ current_thread->cpus_lock_nest--;
+ current_thread->scheduler_lock_nest--;
+
+ rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
+ (rt_ubase_t)&to_thread->sp, to_thread);
+ }
+ }
+ }
+ rt_hw_interrupt_enable(level);
}
+#endif /*RT_USING_SMP*/
/*
* This function will insert a thread to system ready queue. The state of
@@ -283,6 +565,70 @@ void rt_schedule(void)
* @param thread the thread to be inserted
* @note Please do not invoke this function in user application.
*/
+#ifdef RT_USING_SMP
+void rt_schedule_insert_thread(struct rt_thread *thread)
+{
+ int cpu_id;
+ int bind_cpu;
+ rt_uint32_t cpu_mask;
+ register rt_base_t level;
+
+ RT_ASSERT(thread != RT_NULL);
+
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
+
+ /* change stat */
+ thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
+
+ if (thread->oncpu != RT_CPU_DETACHED)
+ {
+ goto __exit;
+ }
+
+ cpu_id = rt_hw_cpu_id();
+ bind_cpu = thread->bind_cpu ;
+
+ /* insert thread to ready list */
+ if (bind_cpu == RT_CPUS_NR)
+ {
+#if RT_THREAD_PRIORITY_MAX > 32
+ rt_thread_ready_table[thread->number] |= thread->high_mask;
+#endif
+ rt_thread_ready_priority_group |= thread->number_mask;
+
+ rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
+ &(thread->tlist));
+ cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
+ rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask);
+ }
+ else
+ {
+ struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
+
+#if RT_THREAD_PRIORITY_MAX > 32
+ pcpu->ready_table[thread->number] |= thread->high_mask;
+#endif
+ pcpu->priority_group |= thread->number_mask;
+
+ rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
+ &(thread->tlist));
+
+ if (cpu_id != bind_cpu)
+ {
+ cpu_mask = 1 << bind_cpu;
+ rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask);
+ }
+ }
+
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
+ RT_NAME_MAX, thread->name, thread->current_priority));
+
+__exit:
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+}
+#else
void rt_schedule_insert_thread(struct rt_thread *thread)
{
register rt_base_t temp;
@@ -295,32 +641,29 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
/* change stat */
thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
+ if (thread == rt_current_thread)
+ {
+ goto __exit;
+ }
+
/* insert thread to ready list */
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
&(thread->tlist));
- /* set priority mask */
-#if RT_THREAD_PRIORITY_MAX <= 32
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
RT_NAME_MAX, thread->name, thread->current_priority));
-#else
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("insert thread[%.*s], the priority: %d 0x%x %d\n",
- RT_NAME_MAX,
- thread->name,
- thread->number,
- thread->number_mask,
- thread->high_mask));
-#endif
+ /* set priority mask */
#if RT_THREAD_PRIORITY_MAX > 32
rt_thread_ready_table[thread->number] |= thread->high_mask;
#endif
rt_thread_ready_priority_group |= thread->number_mask;
+__exit:
/* enable interrupt */
rt_hw_interrupt_enable(temp);
}
+#endif /*RT_USING_SMP*/
/*
* This function will remove a thread from system ready queue.
@@ -329,28 +672,71 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
*
* @note Please do not invoke this function in user application.
*/
+#ifdef RT_USING_SMP
void rt_schedule_remove_thread(struct rt_thread *thread)
{
- register rt_base_t temp;
+ register rt_base_t level;
RT_ASSERT(thread != RT_NULL);
/* disable interrupt */
- temp = rt_hw_interrupt_disable();
+ level = rt_hw_interrupt_disable();
-#if RT_THREAD_PRIORITY_MAX <= 32
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
RT_NAME_MAX, thread->name,
thread->current_priority));
+
+ /* remove thread from ready list */
+ rt_list_remove(&(thread->tlist));
+ if (thread->bind_cpu == RT_CPUS_NR)
+ {
+ if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
+ {
+#if RT_THREAD_PRIORITY_MAX > 32
+ rt_thread_ready_table[thread->number] &= ~thread->high_mask;
+ if (rt_thread_ready_table[thread->number] == 0)
+ {
+ rt_thread_ready_priority_group &= ~thread->number_mask;
+ }
+#else
+ rt_thread_ready_priority_group &= ~thread->number_mask;
+#endif
+ }
+ }
+ else
+ {
+ struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
+
+ if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
+ {
+#if RT_THREAD_PRIORITY_MAX > 32
+ pcpu->ready_table[thread->number] &= ~thread->high_mask;
+ if (rt_thread_ready_table[thread->number] == 0)
+ {
+ pcpu->priority_group &= ~thread->number_mask;
+ }
#else
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("remove thread[%.*s], the priority: %d 0x%x %d\n",
- RT_NAME_MAX,
- thread->name,
- thread->number,
- thread->number_mask,
- thread->high_mask));
+ pcpu->priority_group &= ~thread->number_mask;
#endif
+ }
+ }
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+}
+#else
+void rt_schedule_remove_thread(struct rt_thread *thread)
+{
+ register rt_base_t level;
+
+ RT_ASSERT(thread != RT_NULL);
+
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
+
+ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
+ RT_NAME_MAX, thread->name,
+ thread->current_priority));
/* remove thread from ready list */
rt_list_remove(&(thread->tlist));
@@ -368,12 +754,41 @@ void rt_schedule_remove_thread(struct rt_thread *thread)
}
/* enable interrupt */
- rt_hw_interrupt_enable(temp);
+ rt_hw_interrupt_enable(level);
}
+#endif /*RT_USING_SMP*/
/**
* This function will lock the thread scheduler.
*/
+#ifdef RT_USING_SMP
+void rt_enter_critical(void)
+{
+ register rt_base_t level;
+ struct rt_thread *current_thread;
+
+ /* disable interrupt */
+ level = rt_hw_local_irq_disable();
+
+ current_thread = rt_cpu_self()->current_thread;
+ /*
+ * the maximal number of nest is RT_UINT16_MAX, which is big
+ * enough and does not check here
+ */
+
+ /* lock scheduler for all cpus */
+ if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest)
+ {
+ rt_hw_spin_lock(&_rt_critical_lock);
+ }
+
+ /* lock scheduler for local cpu */
+ current_thread->scheduler_lock_nest ++;
+
+ /* enable interrupt */
+ rt_hw_local_irq_enable(level);
+}
+#else
void rt_enter_critical(void)
{
register rt_base_t level;
@@ -390,11 +805,45 @@ void rt_enter_critical(void)
/* enable interrupt */
rt_hw_interrupt_enable(level);
}
+#endif /*RT_USING_SMP*/
RTM_EXPORT(rt_enter_critical);
/**
* This function will unlock the thread scheduler.
*/
+#ifdef RT_USING_SMP
+void rt_exit_critical(void)
+{
+ register rt_base_t level;
+ struct rt_thread *current_thread;
+
+ /* disable interrupt */
+ level = rt_hw_local_irq_disable();
+
+ current_thread = rt_cpu_self()->current_thread;
+
+ current_thread->scheduler_lock_nest --;
+
+ if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest)
+ {
+ rt_hw_spin_unlock(&_rt_critical_lock);
+ }
+
+ if (current_thread->scheduler_lock_nest <= 0)
+ {
+ current_thread->scheduler_lock_nest = 0;
+ /* enable interrupt */
+ rt_hw_local_irq_enable(level);
+
+ rt_schedule();
+ }
+ else
+ {
+ /* enable interrupt */
+ rt_hw_local_irq_enable(level);
+ }
+}
+#else
void rt_exit_critical(void)
{
register rt_base_t level;
@@ -418,6 +867,7 @@ void rt_exit_critical(void)
rt_hw_interrupt_enable(level);
}
}
+#endif /*RT_USING_SMP*/
RTM_EXPORT(rt_exit_critical);
/**
@@ -427,8 +877,14 @@ RTM_EXPORT(rt_exit_critical);
*/
rt_uint16_t rt_critical_level(void)
{
- return rt_scheduler_lock_nest;
+#ifdef RT_USING_SMP
+ struct rt_thread *current_thread = rt_cpu_self()->current_thread;
+
+ return current_thread->scheduler_lock_nest;
+#else
+ return rt_scheduler_lock_nest;
+#endif /*RT_USING_SMP*/
}
RTM_EXPORT(rt_critical_level);
-/**@}*/
+/**@}*/
diff --git a/src/signal.c b/src/signal.c
index 5b93c774df..05632b2c4a 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -1,25 +1,13 @@
/*
- * File : signal.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/5 Bernard the first version
+ * 2018/09/17 Jesven fix: in _signal_deliver RT_THREAD_STAT_MASK to RT_THREAD_STAT_SIGNAL_MASK
+ * 2018/11/22 Jesven in smp version rt_hw_context_switch_to add a param
*/
#include <stdint.h>
@@ -55,7 +43,7 @@ void rt_thread_handle_sig(rt_bool_t clean_state);
static void _signal_default_handler(int signo)
{
- dbg_log(DBG_INFO, "handled signo[%d] with default action.\n", signo);
+ LOG_I("handled signo[%d] with default action.", signo);
return ;
}
@@ -74,10 +62,14 @@ static void _signal_entry(void *parameter)
tid->sp = tid->sig_ret;
tid->sig_ret = RT_NULL;
- dbg_log(DBG_LOG, "switch back to: 0x%08x\n", tid->sp);
+ LOG_D("switch back to: 0x%08x\n", tid->sp);
tid->stat &= ~RT_THREAD_STAT_SIGNAL;
- rt_hw_context_switch_to((rt_uint32_t) & (tid->sp));
+#ifdef RT_USING_SMP
+ rt_hw_context_switch_to((rt_ubase_t)&(tid->sp), tid);
+#else
+ rt_hw_context_switch_to((rt_ubase_t)&(tid->sp));
+#endif /*RT_USING_SMP*/
}
/*
@@ -122,7 +114,7 @@ static void _signal_deliver(rt_thread_t tid)
/* do signal action in self thread context */
rt_thread_handle_sig(RT_TRUE);
}
- else if (!((tid->stat & RT_THREAD_STAT_MASK) & RT_THREAD_STAT_SIGNAL))
+ else if (!((tid->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL))
{
/* add signal state */
tid->stat |= RT_THREAD_STAT_SIGNAL;
@@ -133,7 +125,7 @@ static void _signal_deliver(rt_thread_t tid)
(void *)((char *)tid->sig_ret - 32), RT_NULL);
rt_hw_interrupt_enable(level);
- dbg_log(DBG_LOG, "signal stack pointer @ 0x%08x\n", tid->sp);
+ LOG_D("signal stack pointer @ 0x%08x", tid->sp);
/* re-schedule */
rt_schedule();
@@ -147,7 +139,7 @@ static void _signal_deliver(rt_thread_t tid)
rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler)
{
- rt_sighandler_t old;
+ rt_sighandler_t old = RT_NULL;
rt_thread_t tid = rt_thread_self();
if (!sig_valid(signo)) return SIG_ERR;
@@ -282,7 +274,7 @@ __done:
{
*si = si_node->si;
- dbg_log(DBG_LOG, "sigwait: %d sig raised!\n", signo);
+ LOG_D("sigwait: %d sig raised!", signo);
if (si_prev) si_prev->list.next = si_node->list.next;
else tid->si_list = si_node->list.next;
@@ -334,7 +326,7 @@ void rt_thread_handle_sig(rt_bool_t clean_state)
handler = tid->sig_vectors[signo];
rt_hw_interrupt_enable(level);
- dbg_log(DBG_LOG, "handle signal: %d, handler 0x%08x\n", signo, handler);
+ LOG_D("handle signal: %d, handler 0x%08x", signo, handler);
if (handler) handler(signo);
level = rt_hw_interrupt_disable();
@@ -392,7 +384,7 @@ void rt_thread_free_sig(rt_thread_t tid)
struct rt_slist_node *node;
struct siginfo_node *si_node;
- dbg_log(DBG_LOG, "free signal info list\n");
+ LOG_D("free signal info list");
node = &(si_list->list);
do
{
@@ -418,7 +410,7 @@ int rt_thread_kill(rt_thread_t tid, int sig)
RT_ASSERT(tid != RT_NULL);
if (!sig_valid(sig)) return -RT_EINVAL;
- dbg_log(DBG_INFO, "send signal: %d\n", sig);
+ LOG_I("send signal: %d", sig);
si.si_signo = sig;
si.si_code = SI_USER;
si.si_value.sival_ptr = RT_NULL;
@@ -476,7 +468,7 @@ int rt_thread_kill(rt_thread_t tid, int sig)
}
else
{
- dbg_log(DBG_ERROR, "The allocation of signal info node failed.\n");
+ LOG_E("The allocation of signal info node failed.");
}
/* deliver signal to this thread */
@@ -490,7 +482,7 @@ int rt_system_signal_init(void)
_rt_siginfo_pool = rt_mp_create("signal", RT_SIG_INFO_MAX, sizeof(struct siginfo_node));
if (_rt_siginfo_pool == RT_NULL)
{
- dbg_log(DBG_ERROR, "create memory pool for signal info failed.\n");
+ LOG_E("create memory pool for signal info failed.");
RT_ASSERT(0);
}
diff --git a/src/slab.c b/src/slab.c
index 16c68a2408..bf26c84cca 100644
--- a/src/slab.c
+++ b/src/slab.c
@@ -1,21 +1,11 @@
/*
- * File : slab.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/*
+ * File : slab.c
*
* Change Logs:
* Date Author Notes
diff --git a/src/thread.c b/src/thread.c
index b3d91b30d0..df9f151834 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -1,21 +1,7 @@
/*
- * File : thread.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -37,18 +23,17 @@
* 2012-12-29 Bernard fixed compiling warning.
* 2016-08-09 ArdaFu add thread suspend and resume hook.
* 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
- bug when thread has not startup.
+ * bug when thread has not startup.
+ * 2018-11-22 Jesven yield is same to rt_schedule
+ * add support for tasks bound to cpu
*/
-#include <rtthread.h>
#include <rthw.h>
+#include <rtthread.h>
-extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
-extern struct rt_thread *rt_current_thread;
extern rt_list_t rt_thread_defunct;
#ifdef RT_USING_HOOK
-
static void (*rt_thread_suspend_hook)(rt_thread_t thread);
static void (*rt_thread_resume_hook) (rt_thread_t thread);
static void (*rt_thread_inited_hook) (rt_thread_t thread);
@@ -98,7 +83,7 @@ void rt_thread_exit(void)
register rt_base_t level;
/* get current thread */
- thread = rt_current_thread;
+ thread = rt_thread_self();
/* disable interrupt */
level = rt_hw_interrupt_disable();
@@ -150,9 +135,15 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
/* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size);
+#ifdef ARCH_CPU_STACK_GROWS_UPWARD
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
- (void *)((char *)thread->stack_addr + thread->stack_size - 4),
+ (void *)((char *)thread->stack_addr),
(void *)rt_thread_exit);
+#else
+ thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
+ (void *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
+ (void *)rt_thread_exit);
+#endif
/* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
@@ -173,6 +164,16 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
thread->error = RT_EOK;
thread->stat = RT_THREAD_INIT;
+#ifdef RT_USING_SMP
+ /* not bind on any cpu */
+ thread->bind_cpu = RT_CPUS_NR;
+ thread->oncpu = RT_CPU_DETACHED;
+
+ /* lock init */
+ thread->scheduler_lock_nest = 0;
+ thread->cpus_lock_nest = 0;
+#endif /*RT_USING_SMP*/
+
/* initialize cleanup function and user data */
thread->cleanup = 0;
thread->user_data = 0;
@@ -185,10 +186,6 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
0,
RT_TIMER_FLAG_ONE_SHOT);
-#ifdef RT_USING_MODULE
- thread->module_id = RT_NULL;
-#endif
-
/* initialize signal */
#ifdef RT_USING_SIGNALS
thread->sig_mask = 0x00;
@@ -263,7 +260,19 @@ RTM_EXPORT(rt_thread_init);
*/
rt_thread_t rt_thread_self(void)
{
+#ifdef RT_USING_SMP
+ rt_base_t lock;
+ rt_thread_t self;
+
+ lock = rt_hw_local_irq_disable();
+ self = rt_cpu_self()->current_thread;
+ rt_hw_local_irq_enable(lock);
+ return self;
+#else
+ extern rt_thread_t rt_current_thread;
+
return rt_current_thread;
+#endif
}
RTM_EXPORT(rt_thread_self);
@@ -461,36 +470,7 @@ RTM_EXPORT(rt_thread_delete);
*/
rt_err_t rt_thread_yield(void)
{
- register rt_base_t level;
- struct rt_thread *thread;
-
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
-
- /* set to current thread */
- thread = rt_current_thread;
-
- /* if the thread stat is READY and on ready queue list */
- if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
- thread->tlist.next != thread->tlist.prev)
- {
- /* remove thread from thread list */
- rt_list_remove(&(thread->tlist));
-
- /* put thread to end of ready queue */
- rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
- &(thread->tlist));
-
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
-
- rt_schedule();
-
- return RT_EOK;
- }
-
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
+ rt_schedule();
return RT_EOK;
}
@@ -508,13 +488,14 @@ rt_err_t rt_thread_sleep(rt_tick_t tick)
register rt_base_t temp;
struct rt_thread *thread;
- /* disable interrupt */
- temp = rt_hw_interrupt_disable();
/* set to current thread */
- thread = rt_current_thread;
+ thread = rt_thread_self();
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
+ /* disable interrupt */
+ temp = rt_hw_interrupt_disable();
+
/* suspend thread */
rt_thread_suspend(thread);
@@ -571,7 +552,8 @@ RTM_EXPORT(rt_thread_mdelay);
* @param cmd the control command, which includes
* RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
* RT_THREAD_CTRL_STARTUP for starting a thread;
- * RT_THREAD_CTRL_CLOSE for delete a thread.
+ * RT_THREAD_CTRL_CLOSE for delete a thread;
+ * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
* @param arg the argument of control command
*
* @return RT_EOK
@@ -637,6 +619,22 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
return rt_thread_delete(thread);
#endif
+#ifdef RT_USING_SMP
+ case RT_THREAD_CTRL_BIND_CPU:
+ {
+ rt_uint8_t cpu;
+
+ if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
+ {
+ /* we only support bind cpu before started phase. */
+ return RT_ERROR;
+ }
+
+ cpu = (rt_uint8_t)(size_t)arg;
+ thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
+ break;
+ }
+#endif /*RT_USING_SMP*/
default:
break;
}
@@ -673,6 +671,8 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
return -RT_ERROR;
}
+ RT_ASSERT(thread == rt_thread_self());
+
/* disable interrupt */
temp = rt_hw_interrupt_disable();
diff --git a/src/timer.c b/src/timer.c
index 042826f41b..fd938065db 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -1,21 +1,7 @@
/*
- * File : timer.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ * Copyright (c) 2006-2018, RT-Thread Development Team
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
@@ -57,7 +43,8 @@ static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
#ifdef RT_USING_HOOK
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
-static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
+static void (*rt_timer_enter_hook)(struct rt_timer *timer);
+static void (*rt_timer_exit_hook)(struct rt_timer *timer);
/**
* @addtogroup Hook
@@ -66,14 +53,25 @@ static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
/**@{*/
/**
- * This function will set a hook function, which will be invoked when timer
- * is timeout.
+ * This function will set a hook function, which will be invoked when enter
+ * timer timeout callback function.
+ *
+ * @param hook the hook function
+ */
+void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
+{
+ rt_timer_enter_hook = hook;
+}
+
+/**
+ * This function will set a hook function, which will be invoked when exit
+ * timer timeout callback function.
*
* @param hook the hook function
*/
-void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
+void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
{
- rt_timer_timeout_hook = hook;
+ rt_timer_exit_hook = hook;
}
/**@}*/
@@ -517,7 +515,7 @@ void rt_timer_check(void)
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
{
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
+ RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
/* remove timer from timer list firstly */
_rt_timer_remove(t);
@@ -528,6 +526,7 @@ void rt_timer_check(void)
/* re-get tick */
current_tick = rt_tick_get();
+ RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
@@ -592,7 +591,7 @@ void rt_soft_timer_check(void)
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
{
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
+ RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
/* move node to the next */
n = n->next;
@@ -608,6 +607,7 @@ void rt_soft_timer_check(void)
/* re-get tick */
current_tick = rt_tick_get();
+ RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
/* lock scheduler */