git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3192 35acf78f-673a-0410-8e92-d51de3d6d3f4

This commit is contained in:
gdisirio 2011-08-07 09:00:12 +00:00
parent 0c276798fd
commit 075ff711f1
15 changed files with 95 additions and 57 deletions

View File

@ -23,7 +23,6 @@
*/ */
__main_stack_size__ = 0x0400; __main_stack_size__ = 0x0400;
__process_stack_size__ = 0x0400; __process_stack_size__ = 0x0400;
__stacks_total_size__ = __main_stack_size__ + __process_stack_size__;
MEMORY MEMORY
{ {
@ -98,6 +97,21 @@ SECTIONS
_etext = .; _etext = .;
_textdata = _etext; _textdata = _etext;
.stacks :
{
. = ALIGN(8);
__main_stack_base__ = .;
. += __main_stack_size__;
. = ALIGN(8);
__main_stack_end__ = .;
__process_stack_base__ = .;
__main_thread_stack_base__ = .;
. += __process_stack_size__;
. = ALIGN(8);
__process_stack_end__ = .;
__main_thread_stack_end__ = .;
} > ram
.data : .data :
{ {
PROVIDE(_data = .); PROVIDE(_data = .);
@ -127,4 +141,4 @@ PROVIDE(end = .);
_end = .; _end = .;
__heap_base__ = _end; __heap_base__ = _end;
__heap_end__ = __ram_end__ - __stacks_total_size__; __heap_end__ = __ram_end__;

View File

@ -395,7 +395,7 @@
* @p panic_msg variable set to @p NULL. * @p panic_msg variable set to @p NULL.
*/ */
#if !defined(CH_DBG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__) #if !defined(CH_DBG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__)
#define CH_DBG_ENABLE_STACK_CHECK FALSE #define CH_DBG_ENABLE_STACK_CHECK TRUE
#endif #endif
/** /**

View File

@ -76,12 +76,12 @@ typedef struct {
initialized to zero. */ initialized to zero. */
struct context r_ctx; /**< @brief Not used, present because struct context r_ctx; /**< @brief Not used, present because
offsets. */ offsets. */
#if CH_USE_REGISTRY #if CH_USE_REGISTRY || defined(__DOXYGEN__)
Thread *r_newer; /**< @brief Newer registry element. */ Thread *r_newer; /**< @brief Newer registry element. */
Thread *r_older; /**< @brief Older registry element. */ Thread *r_older; /**< @brief Older registry element. */
#endif #endif
/* End of the fields shared with the Thread structure.*/ /* End of the fields shared with the Thread structure.*/
#if CH_TIME_QUANTUM > 0 #if (CH_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
cnt_t r_preempt; /**< @brief Round robin counter. */ cnt_t r_preempt; /**< @brief Round robin counter. */
#endif #endif
Thread *r_current; /**< @brief The currently running Thread *r_current; /**< @brief The currently running

View File

@ -44,16 +44,22 @@ struct Thread {
/* End of the fields shared with the ThreadsQueue structure. */ /* End of the fields shared with the ThreadsQueue structure. */
tprio_t p_prio; /**< @brief Thread priority. */ tprio_t p_prio; /**< @brief Thread priority. */
struct context p_ctx; /**< @brief Processor context. */ struct context p_ctx; /**< @brief Processor context. */
#if CH_USE_REGISTRY #if CH_USE_REGISTRY || defined(__DOXYGEN__)
Thread *p_newer; /**< @brief Newer registry element. */ Thread *p_newer; /**< @brief Newer registry element. */
Thread *p_older; /**< @brief Older registry element. */ Thread *p_older; /**< @brief Older registry element. */
#endif #endif
/* End of the fields shared with the ReadyList structure. */ /* End of the fields shared with the ReadyList structure. */
#if CH_USE_REGISTRY #if CH_USE_REGISTRY || defined(__DOXYGEN__)
/** /**
* @brief Thread name or @p NULL. * @brief Thread name or @p NULL.
*/ */
const char *p_name; const char *p_name;
#endif
#if CH_DBG_ENABLE_STACK_CHECK || defined(__DOXYGEN__)
/**
* @brief Thread stack boundary.
*/
stkalign_t *p_stklimit;
#endif #endif
/** /**
* @brief Current thread state. * @brief Current thread state.
@ -63,19 +69,19 @@ struct Thread {
* @brief Various thread flags. * @brief Various thread flags.
*/ */
tmode_t p_flags; tmode_t p_flags;
#if CH_USE_DYNAMIC #if CH_USE_DYNAMIC || defined(__DOXYGEN__)
/** /**
* @brief References to this thread. * @brief References to this thread.
*/ */
trefs_t p_refs; trefs_t p_refs;
#endif #endif
#if CH_USE_NESTED_LOCKS #if CH_USE_NESTED_LOCKS || defined(__DOXYGEN__)
/** /**
* @brief Number of nested locks. * @brief Number of nested locks.
*/ */
cnt_t p_locks; cnt_t p_locks;
#endif #endif
#if CH_DBG_THREADS_PROFILING #if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
/** /**
* @brief Thread consumed time in ticks. * @brief Thread consumed time in ticks.
* @note This field can overflow. * @note This field can overflow.
@ -109,22 +115,22 @@ struct Thread {
* states. * states.
*/ */
void *wtobjp; void *wtobjp;
#if CH_USE_EVENTS #if CH_USE_EVENTS || defined(__DOXYGEN__)
/** /**
* @brief Enabled events mask. * @brief Enabled events mask.
* @note This field is only valied while the thread is in the * @note This field is only valid while the thread is in the
* @p THD_STATE_WTOREVT or @p THD_STATE_WTANDEVT states. * @p THD_STATE_WTOREVT or @p THD_STATE_WTANDEVT states.
*/ */
eventmask_t ewmask; eventmask_t ewmask;
#endif #endif
} p_u; } p_u;
#if CH_USE_WAITEXIT #if CH_USE_WAITEXIT || defined(__DOXYGEN__)
/** /**
* @brief Termination waiting list. * @brief Termination waiting list.
*/ */
ThreadsList p_waiting; ThreadsList p_waiting;
#endif #endif
#if CH_USE_MESSAGES #if CH_USE_MESSAGES || defined(__DOXYGEN__)
/** /**
* @brief Messages queue. * @brief Messages queue.
*/ */
@ -134,13 +140,13 @@ struct Thread {
*/ */
msg_t p_msg; msg_t p_msg;
#endif #endif
#if CH_USE_EVENTS #if CH_USE_EVENTS || defined(__DOXYGEN__)
/** /**
* @brief Pending events mask. * @brief Pending events mask.
*/ */
eventmask_t p_epending; eventmask_t p_epending;
#endif #endif
#if CH_USE_MUTEXES #if CH_USE_MUTEXES || defined(__DOXYGEN__)
/** /**
* @brief List of the mutexes owned by this thread. * @brief List of the mutexes owned by this thread.
* @note The list is terminated by a @p NULL in this field. * @note The list is terminated by a @p NULL in this field.
@ -151,7 +157,7 @@ struct Thread {
*/ */
tprio_t p_realprio; tprio_t p_realprio;
#endif #endif
#if CH_USE_DYNAMIC && CH_USE_MEMPOOLS #if (CH_USE_DYNAMIC && CH_USE_MEMPOOLS) || defined(__DOXYGEN__)
/** /**
* @brief Memory Pool where the thread workspace is returned. * @brief Memory Pool where the thread workspace is returned.
*/ */

View File

@ -77,6 +77,9 @@ void _idle_thread(void *p) {
*/ */
void chSysInit(void) { void chSysInit(void) {
static Thread mainthread; static Thread mainthread;
#if CH_DBG_ENABLE_STACK_CHECK
extern stkalign_t __main_thread_stack_base__;
#endif
port_init(); port_init();
_scheduler_init(); _scheduler_init();
@ -94,6 +97,9 @@ void chSysInit(void) {
/* Now this instructions flow becomes the main thread.*/ /* Now this instructions flow becomes the main thread.*/
setcurrp(_thread_init(&mainthread, NORMALPRIO)); setcurrp(_thread_init(&mainthread, NORMALPRIO));
currp->p_state = THD_STATE_CURRENT; currp->p_state = THD_STATE_CURRENT;
#if CH_DBG_ENABLE_STACK_CHECK
currp->p_stklimit = &__main_thread_stack_base__;
#endif
chSysEnable(); chSysEnable();
chRegSetThreadName("main"); chRegSetThreadName("main");

View File

@ -99,6 +99,9 @@ Thread *_thread_init(Thread *tp, tprio_t prio) {
#if CH_USE_MESSAGES #if CH_USE_MESSAGES
queue_init(&tp->p_msgqueue); queue_init(&tp->p_msgqueue);
#endif #endif
#if CH_DBG_ENABLE_STACK_CHECK
tp->p_stklimit = (stkalign_t *)(tp + 1);
#endif
#if defined(THREAD_EXT_INIT_HOOK) #if defined(THREAD_EXT_INIT_HOOK)
THREAD_EXT_INIT_HOOK(tp); THREAD_EXT_INIT_HOOK(tp);
#endif #endif

View File

@ -31,7 +31,7 @@
#include "ch.h" #include "ch.h"
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
extern void __ram_end__(void); extern void __main_stack_end__(void);
extern void ResetHandler(void); extern void ResetHandler(void);
extern void NMIVector(void); extern void NMIVector(void);
extern void HardFaultVector(void); extern void HardFaultVector(void);
@ -88,7 +88,7 @@ extern void VectorBC(void);
__attribute__ ((section("vectors"))) __attribute__ ((section("vectors")))
#endif #endif
void (*_vectors[])(void) = { void (*_vectors[])(void) = {
__ram_end__, ResetHandler, NMIVector, HardFaultVector, __main_stack_end__, ResetHandler, NMIVector, HardFaultVector,
MemManageVector, BusFaultVector, UsageFaultVector, Vector1C, MemManageVector, BusFaultVector, UsageFaultVector, Vector1C,
Vector20, Vector24, Vector28, SVCallVector, Vector20, Vector24, Vector28, SVCallVector,
DebugMonitorVector, Vector34, PendSVVector, SysTickVector, DebugMonitorVector, Vector34, PendSVVector, SysTickVector,

View File

@ -31,7 +31,7 @@
#include "ch.h" #include "ch.h"
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
extern void __ram_end__(void); extern void __main_stack_end__(void);
extern void ResetHandler(void); extern void ResetHandler(void);
extern void NMIVector(void); extern void NMIVector(void);
extern void HardFaultVector(void); extern void HardFaultVector(void);
@ -112,7 +112,7 @@ extern void Vector11C(void);
__attribute__ ((section("vectors"))) __attribute__ ((section("vectors")))
#endif #endif
void (*_vectors[])(void) = { void (*_vectors[])(void) = {
__ram_end__, ResetHandler, NMIVector, HardFaultVector, __main_stack_end__, ResetHandler, NMIVector, HardFaultVector,
MemManageVector, BusFaultVector, UsageFaultVector, Vector1C, MemManageVector, BusFaultVector, UsageFaultVector, Vector1C,
Vector20, Vector24, Vector28, SVCallVector, Vector20, Vector24, Vector28, SVCallVector,
DebugMonitorVector, Vector34, PendSVVector, SysTickVector, DebugMonitorVector, Vector34, PendSVVector, SysTickVector,

View File

@ -9,3 +9,6 @@ PORTASM =
PORTINC = ${CHIBIOS}/os/ports/GCC/ARMCMx \ PORTINC = ${CHIBIOS}/os/ports/GCC/ARMCMx \
${CHIBIOS}/os/ports/GCC/ARMCMx/STM32 ${CHIBIOS}/os/ports/GCC/ARMCMx/STM32
PORTLD = ${CHIBIOS}/os/ports/GCC/ARMCMx/STM32/ld

View File

@ -38,7 +38,7 @@
#endif #endif
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
extern void __ram_end__(void); extern void __main_stack_end__(void);
extern void ResetHandler(void); extern void ResetHandler(void);
extern void NMIVector(void); extern void NMIVector(void);
extern void HardFaultVector(void); extern void HardFaultVector(void);
@ -138,7 +138,7 @@ extern void Vector14C(void);
__attribute__ ((section("vectors"))) __attribute__ ((section("vectors")))
#endif #endif
void (*_vectors[])(void) = { void (*_vectors[])(void) = {
__ram_end__, ResetHandler, NMIVector, HardFaultVector, __main_stack_end__, ResetHandler, NMIVector, HardFaultVector,
MemManageVector, BusFaultVector, UsageFaultVector, Vector1C, MemManageVector, BusFaultVector, UsageFaultVector, Vector1C,
Vector20, Vector24, Vector28, SVCallVector, Vector20, Vector24, Vector28, SVCallVector,
DebugMonitorVector, Vector34, PendSVVector, SysTickVector, DebugMonitorVector, Vector34, PendSVVector, SysTickVector,

View File

@ -243,7 +243,7 @@ struct intctx {
#else #else
#define port_switch(ntp, otp) { \ #define port_switch(ntp, otp) { \
register struct intctx *r13 asm ("r13"); \ register struct intctx *r13 asm ("r13"); \
if ((void *)(r13 - 1) < (void *)(otp + 1)) \ if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \
chDbgPanic("stack overflow"); \ chDbgPanic("stack overflow"); \
_port_switch(ntp, otp); \ _port_switch(ntp, otp); \
} }

View File

@ -166,15 +166,7 @@ void _port_switch_from_isr(void) {
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
__attribute__((naked)) __attribute__((naked))
#endif #endif
void port_switch(Thread *ntp, Thread *otp) { void _port_switch(Thread *ntp, Thread *otp) {
#if CH_DBG_ENABLE_STACK_CHECK
/* Stack overflow check, if enabled.*/
register struct intctx *r13 asm ("r13");
if ((void *)(r13 - 1) < (void *)(otp + 1))
asm volatile ("movs r0, #0 \n\t"
"b chDbgPanic");
#endif /* CH_DBG_ENABLE_STACK_CHECK */
PUSH_CONTEXT(); PUSH_CONTEXT();

View File

@ -314,11 +314,32 @@ struct intctx {
#define port_wait_for_interrupt() #define port_wait_for_interrupt()
#endif #endif
/**
* @brief Performs a context switch between two threads.
* @details This is the most critical code in any port, this function
* is responsible for the context switch between 2 threads.
* @note The implementation of this code affects <b>directly</b> the context
* switch performance so optimize here as much as you can.
*
* @param[in] ntp the thread to be switched in
* @param[in] otp the thread to be switched out
*/
#if !defined(CH_DBG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__)
#define port_switch(ntp, otp) _port_switch(ntp, otp)
#else
#define port_switch(ntp, otp) { \
register struct intctx *r13 asm ("r13"); \
if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \
chDbgPanic("stack overflow"); \
_port_switch(ntp, otp); \
}
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void port_halt(void); void port_halt(void);
void port_switch(Thread *ntp, Thread *otp); void _port_switch(Thread *ntp, Thread *otp);
void _port_irq_epilogue(void); void _port_irq_epilogue(void);
void _port_switch_from_isr(void); void _port_switch_from_isr(void);
void _port_thread_start(void); void _port_thread_start(void);

View File

@ -73,28 +73,11 @@ typedef funcp_t * funcpp_t;
#define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
/** /**
* @brief Ram end. * @brief Main thread stack initial position.
* @details This symbol must be exported by the linker script and represents * @details This symbol must be exported by the linker script and represents
* the location after the last RAM location. * the main thread stack initial position.
*/ */
extern uint8_t __ram_end__; extern uint8_t __process_stack_end__;
/**
* @brief Main stack size.
* @details This symbol must be exported by the linker script and represents
* the main stack size.
* @note The main stack is the stack where interrupts and exceptions are
* processed.
*/
extern uint8_t __main_stack_size__;
/**
* @brief Process stack size.
* @details This symbol must be exported by the linker script and represents
* the process stack size.
* @note The process stack is the stack used by the @p main() function.
*/
extern uint8_t __process_stack_size__;
/** /**
* @brief ROM image of the data segment start. * @brief ROM image of the data segment start.
@ -206,7 +189,7 @@ void ResetHandler(void) {
main stack is assumed to be allocated starting from @p __ram_end__ main stack is assumed to be allocated starting from @p __ram_end__
extending downward.*/ extending downward.*/
asm volatile ("cpsid i"); asm volatile ("cpsid i");
psp = SYMVAL(__ram_end__) - SYMVAL(__main_stack_size__); psp = SYMVAL(__process_stack_end__);
asm volatile ("msr PSP, %0" : : "r" (psp)); asm volatile ("msr PSP, %0" : : "r" (psp));
ctl = CRT0_CONTROL_INIT; ctl = CRT0_CONTROL_INIT;

View File

@ -87,6 +87,15 @@
(backported to 2.2.4). (backported to 2.2.4).
- FIX: Fixed timeout problem in the lwIP interface layer (bug 3302420) - FIX: Fixed timeout problem in the lwIP interface layer (bug 3302420)
(backported to 2.2.4). (backported to 2.2.4).
- NEW: Improved stack checking and reorganized memory map for the Cortex-Mx
demos. Now stacks are allocated at the start of the RAM, an overflow of the
exception stack now triggers an exception (it could went unnoticed before).
The process stack is organized to be checked on context switch like other
threads. Now all threads have an explicit stack boundary pointer.
(to be completed, most demos have to be edited)
(documentation to be updated)
(change to be ported to IAR and Keil ports)
(change to be ported to ARM and other ports)
- NEW: Added debug plugin for Eclipse under ./tools/eclipse (backported to - NEW: Added debug plugin for Eclipse under ./tools/eclipse (backported to
2.2.7). 2.2.7).
- NEW: The debug macros chDbgCheck() and chDbgAssert() now can be externally - NEW: The debug macros chDbgCheck() and chDbgAssert() now can be externally
@ -96,6 +105,7 @@
- NEW: Added provisional support for STM32L1xx and STM32F2xx. Because of this - NEW: Added provisional support for STM32L1xx and STM32F2xx. Because of this
some directories related to the STM32 have been renamed, your makefiles may some directories related to the STM32 have been renamed, your makefiles may
require adjustments. require adjustments.
(change to be ported to IAR and Keil build files)
- NEW: Added a custom rule to the various rules.mk files, now it is possible - NEW: Added a custom rule to the various rules.mk files, now it is possible
to add an user rule into the Makefiles. to add an user rule into the Makefiles.
- NEW: Improvements to the trace buffer, now it stores a full thread pointer - NEW: Improvements to the trace buffer, now it stores a full thread pointer
@ -450,7 +460,7 @@
(backported to 2.0.6). (backported to 2.0.6).
- FIX: Incorrect AT91SAM7X initialization, thanks Leszek (bug 3075354) - FIX: Incorrect AT91SAM7X initialization, thanks Leszek (bug 3075354)
(backported to 2.0.5). (backported to 2.0.5).
- FIX: Fixed race condition in function chSchGoSleepTimeoutS(), thanks Balázs - FIX: Fixed race condition in function chSchGoSleepTimeoutS(), thanks Bal<EFBFBD>zs
(bug 3074984)(backported to 2.0.5). (bug 3074984)(backported to 2.0.5).
- FIX: Fixed race condition in threads creation (bug 3069854)(backported - FIX: Fixed race condition in threads creation (bug 3069854)(backported
to 2.0.5). to 2.0.5).