Skip to content

Commit 00a655a

Browse files
committed
[Kernel] Add ARCH_CPU_STACK_GROWS_UPWARD option
1 parent c7a85df commit 00a655a

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

components/finsh/cmd.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,28 @@ static long _list_thread(struct rt_list_node *list)
101101
else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
102102
else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
103103

104+
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
105+
ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size;
106+
while (*ptr == '#')ptr --;
107+
108+
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
109+
((rt_uint32_t)thread->sp - (rt_uint32_t)thread->stack_addr),
110+
thread->stack_size,
111+
((rt_uint32_t)ptr - (rt_uint32_t)thread->stack_addr) * 100 / thread->stack_size,
112+
thread->remaining_tick,
113+
thread->error);
114+
#else
104115
ptr = (rt_uint8_t *)thread->stack_addr;
105116
while (*ptr == '#')ptr ++;
106117

107118
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
108-
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
119+
(thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
109120
thread->stack_size,
110-
(thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100
121+
(thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t) ptr) * 100
111122
/ thread->stack_size,
112123
thread->remaining_tick,
113124
thread->error);
125+
#endif
114126
}
115127

116128
return 0;

libcpu/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ config ARCH_IA32
8888

8989
config ARCH_HOST_SIMULATOR
9090
bool
91+
92+
config ARCH_CPU_STACK_GROWS_UPWARD
93+
bool
94+
default n

src/scheduler.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread)
9191
level = rt_hw_interrupt_disable();
9292
while (level);
9393
}
94+
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
95+
else if ((rt_uint32_t)thread->sp > ((rt_uint32_t)thread->stack_addr + thread->stack_size))
96+
{
97+
rt_kprintf("warning: %s stack is close to the top of stack address.\n",
98+
thread->name);
99+
}
100+
#else
94101
else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
95102
{
96-
rt_kprintf("warning: %s stack is close to end of stack address.\n",
103+
rt_kprintf("warning: %s stack is close to the bottom of stack address.\n",
97104
thread->name);
98105
}
106+
#endif
99107
}
100108
#endif
101109

src/thread.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,15 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
136136

137137
/* init thread stack */
138138
rt_memset(thread->stack_addr, '#', thread->stack_size);
139+
#ifdef ARCH_CPU_STACK_GROWS_UPWARD
140+
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
141+
(void *)((char *)thread->stack_addr),
142+
(void *)rt_thread_exit);
143+
#else
139144
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
140145
(void *)((char *)thread->stack_addr + thread->stack_size - 4),
141146
(void *)rt_thread_exit);
147+
#endif
142148

143149
/* priority init */
144150
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);

0 commit comments

Comments
 (0)