In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to rt-thread in the stack and stack analysis, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
rt-thread stack pushing and unstacking 1. Description
Main want to analyze rt-thread in the stack and push the stack related operations. Thus better grasps the thread switch and the thread recovery related knowledge.
2. usage scenarios
First of all, we need to understand under what circumstances do we need to push and pull stacks? One way of thinking about this problem is that when a program does something all the time, it does it sequentially, without any interference. But when an interrupt arrives, the logic must prioritize the interrupt. So what do you need to do at this time?
Perhaps this example is a bit divorced from reality. To put it bluntly, it means that a person is focused on completing one thing. At this time, it should be carried out smoothly. But what do you do when you have something more urgent to deal with before it's done?
For one person:
(1)Keep things unfinished, keep current progress
(2)Empty your mind and focus on more important things
(3)Return to the scene of unfinished business, and continue to complete unfinished business
The human brain works like this, and the logic of the chip needs to be executed like this. We know how the chip knows the state of the current program, except for several important registers, sp(program pointer register), general register Rx, and LR link register. With this information, you can know the current running state of the program, this is the program's scene.
For armv7, registers can be divided into the following categories:
armasm_pge1464343210583
In rt-thread operating systems, there are two places involved in stack pushing and stack pulling operations: the first is the entry of interrupts and the exit after interrupt processing is completed, and the second is thread switching.
3. A brief analysis of the initialization of the rt-thread stack
For/bsp/qemu-vexpress-a9, the first line of code to execute rtt when the system powers up is in the/libcpu/arm/cortex-a/start_gcc.S file.
Then execute the_reset function, which is written by the assembly function. Because there is no stack space in the early stage, the code needs to be completed by assembly instructions.
Then allocate stack space and so on. Perform the rest of the logic to rtt. I won't repeat it here. The main analysis here is the initialization of threads.
Each thread needs to allocate stack space at initialization time.
rt_thread_create/rt_thread_init --> _rt_thread_init --> rt_hw_stack_init
The final call to the/libcpu/arm/cortex-a/stack.c file.
rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
rt_uint8_t *stack_addr, void *texit)
{
rt_uint32_t *stk;
stack_addr += sizeof(rt_uint32_t);
stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
stk = (rt_uint32_t *)stack_addr;
*(--stk) = (rt_uint32_t)tentry; /* entry point */
*(--stk) = (rt_uint32_t)texit; /* lr */
*(--stk) = 0xdeadbeef; /* r12 */
*(--stk) = 0xdeadbeef; /* r11 */
*(--stk) = 0xdeadbeef; /* r10 */
*(--stk) = 0xdeadbeef; /* r9 */
*(--stk) = 0xdeadbeef; /* r8 */
*(--stk) = 0xdeadbeef; /* r7 */
*(--stk) = 0xdeadbeef; /* r6 */
*(--stk) = 0xdeadbeef; /* r5 */
*(--stk) = 0xdeadbeef; /* r4 */
*(--stk) = 0xdeadbeef; /* r3 */
*(--stk) = 0xdeadbeef; /* r2 */
*(--stk) = 0xdeadbeef; /* r1 */
*(--stk) = (rt_uint32_t)parameter; /* r0 : argument */
/* cpsr */
if ((rt_uint32_t)tentry & 0x01)
*(--stk) = SVCMODE | 0x20; /* thumb mode */
else
*(--stk) = SVCMODE; /* arm mode */
#ifdef RT_USING_LWP
*(--stk) = 0; /* user lr */
*(--stk) = 0; /* user sp*/
#endif
#ifdef RT_USING_FPU
*(--stk) = 0; /* not use fpu*/
#endif
/* return task's current stack address */
return (rt_uint8_t *)stk;
}
When initializing threads, each thread has a stack space. This stack space not only stores parameter variables, but also stores the scene needed for thread execution at the first address of the stack address. And each thread has a separate stack memory, which is at the entrance to the stack.
These registers need to be fetched when a thread switch occurs
.globl rt_thread_switch_interrupt_flag
.globl rt_interrupt_from_thread
.globl rt_interrupt_to_thread
.globl rt_hw_context_switch_interrupt
rt_hw_context_switch_interrupt:
#ifdef RT_USING_SMP
/* r0 :svc_mod context
* r1 :addr of from_thread's sp
* r2 :addr of to_thread's sp
* r3 :to_thread's tcb
*/
str r0, [r1]
ldr sp, [r2]
mov r0, r3
bl rt_cpus_lock_status_restore
b rt_hw_context_switch_exit
Execute to rt_hw_context_switch_exit function
.global rt_hw_context_switch_exit
rt_hw_context_switch_exit:
#ifdef RT_USING_SMP
#ifdef RT_USING_SIGNALS
mov r0, sp
cps #Mode_IRQ
bl rt_signal_check
cps #Mode_SVC
mov sp, r0
#endif
#endif
#ifdef RT_USING_FPU
/* fpu context */
ldmfd sp!, {r6}
vmsr fpexc, r6
tst r6, #(1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.