Merge pull request #1968 from RT-Thread/feature_stack_grows

[Kernel] Add ARCH_CPU_STACK_GROWS_UPWARD option
This commit is contained in:
Bernard Xiong 2018-11-11 17:07:57 +08:00 committed by GitHub
commit cb1af3616d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 4 deletions

View File

@ -101,16 +101,28 @@ static long _list_thread(struct rt_list_node *list)
else if (stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close "); else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size;
while (*ptr == '#')ptr --;
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
((rt_uint32_t)thread->sp - (rt_uint32_t)thread->stack_addr),
thread->stack_size,
((rt_uint32_t)ptr - (rt_uint32_t)thread->stack_addr) * 100 / thread->stack_size,
thread->remaining_tick,
thread->error);
#else
ptr = (rt_uint8_t *)thread->stack_addr; ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#')ptr ++; while (*ptr == '#')ptr ++;
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), (thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
thread->stack_size, thread->stack_size,
(thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100 (thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t) ptr) * 100
/ thread->stack_size, / thread->stack_size,
thread->remaining_tick, thread->remaining_tick,
thread->error); thread->error);
#endif
} }
return 0; return 0;

View File

@ -88,3 +88,7 @@ config ARCH_IA32
config ARCH_HOST_SIMULATOR config ARCH_HOST_SIMULATOR
bool bool
config ARCH_CPU_STACK_GROWS_UPWARD
bool
default n

View File

@ -91,11 +91,19 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread)
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
while (level); while (level);
} }
else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
else if ((rt_uint32_t)thread->sp > ((rt_uint32_t)thread->stack_addr + thread->stack_size))
{ {
rt_kprintf("warning: %s stack is close to end of stack address.\n", rt_kprintf("warning: %s stack is close to the top of stack address.\n",
thread->name); thread->name);
} }
#else
else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
{
rt_kprintf("warning: %s stack is close to the bottom of stack address.\n",
thread->name);
}
#endif
} }
#endif #endif

View File

@ -136,9 +136,15 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
/* init thread stack */ /* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size); 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),
(void *)rt_thread_exit);
#else
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, 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 + thread->stack_size - 4),
(void *)rt_thread_exit); (void *)rt_thread_exit);
#endif
/* priority init */ /* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);