From acc66c547999ce444b79efccd67625a1af347d5d Mon Sep 17 00:00:00 2001 From: tangzz98 <54998890+tangzz98@users.noreply.github.com> Date: Mon, 30 Oct 2023 08:24:55 -0400 Subject: 实现MPU抽象层 (#8080) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为RT-Thread设计MPU抽象层,支持ARMV7-M,ARMV8-M架构,让用户使用MPU检测栈溢出等内存问题,实现线程内存隔离 - 在components/mp目录下提供通用的API,libcpu目录下提供各处理器架构的具体实现 - 在STM32U575 NUCLEO, STM32H75 NUCLEO开发板测试通过 --- src/idle.c | 10 ++++++++++ src/scheduler_mp.c | 10 ++++++++++ src/scheduler_up.c | 10 ++++++++++ src/thread.c | 7 +++++++ 4 files changed, 37 insertions(+) (limited to 'src') diff --git a/src/idle.c b/src/idle.c index ca5064e346..524b45d3b3 100644 --- a/src/idle.c +++ b/src/idle.c @@ -233,11 +233,21 @@ static void rt_defunct_execute(void) } #ifdef RT_USING_HEAP +#ifdef RT_USING_MEM_PROTECTION + if (thread->mem_regions != RT_NULL) + { + RT_KERNEL_FREE(thread->mem_regions); + } +#endif /* if need free, delete it */ if (object_is_systemobject == RT_FALSE) { /* release thread's stack */ +#ifdef RT_USING_HW_STACK_GUARD + RT_KERNEL_FREE(thread->stack_buf); +#else RT_KERNEL_FREE(thread->stack_addr); +#endif /* delete thread object */ rt_object_delete((rt_object_t)thread); } diff --git a/src/scheduler_mp.c b/src/scheduler_mp.c index 656457aad6..ff3a4f7a11 100644 --- a/src/scheduler_mp.c +++ b/src/scheduler_mp.c @@ -107,6 +107,7 @@ static void _scheduler_stack_check(struct rt_thread *thread) #endif /* not defined ARCH_MM_MMU */ #endif /* RT_USING_SMART */ +#ifndef RT_USING_HW_STACK_GUARD #ifdef ARCH_CPU_STACK_GROWS_UPWARD if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' || #else @@ -124,14 +125,23 @@ static void _scheduler_stack_check(struct rt_thread *thread) rt_spin_lock(&_spinlock); while (level); } +#endif #ifdef ARCH_CPU_STACK_GROWS_UPWARD +#ifndef RT_USING_HW_STACK_GUARD else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) +#else + if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) +#endif { rt_kprintf("warning: %s stack is close to the top of stack address.\n", thread->parent.name); } #else +#ifndef RT_USING_HW_STACK_GUARD else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) +#else + if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) +#endif { rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->parent.name); diff --git a/src/scheduler_up.c b/src/scheduler_up.c index ef8c36dc88..d8b13a5620 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -110,6 +110,7 @@ static void _scheduler_stack_check(struct rt_thread *thread) #endif /* not defined ARCH_MM_MMU */ #endif /* RT_USING_SMART */ +#ifndef RT_USING_HW_STACK_GUARD #ifdef ARCH_CPU_STACK_GROWS_UPWARD if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' || #else @@ -126,14 +127,23 @@ static void _scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } +#endif #ifdef ARCH_CPU_STACK_GROWS_UPWARD +#ifndef RT_USING_HW_STACK_GUARD else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) +#else + if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) +#endif { rt_kprintf("warning: %s stack is close to the top of stack address.\n", thread->parent.name); } #else +#ifndef RT_USING_HW_STACK_GUARD else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) +#else + if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) +#endif { rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->parent.name); diff --git a/src/thread.c b/src/thread.c index 21c6230192..cd14a15d21 100644 --- a/src/thread.c +++ b/src/thread.c @@ -157,6 +157,10 @@ static rt_err_t _thread_init(struct rt_thread *thread, rt_list_init(&(thread->tlist)); rt_list_init(&(thread->tlist_schedule)); +#ifdef RT_USING_MEM_PROTECTION + thread->mem_regions = RT_NULL; +#endif + #ifdef RT_USING_SMART thread->wakeup.func = RT_NULL; #endif @@ -170,6 +174,9 @@ static rt_err_t _thread_init(struct rt_thread *thread, /* init thread stack */ rt_memset(thread->stack_addr, '#', thread->stack_size); +#ifdef RT_USING_HW_STACK_GUARD + rt_hw_stack_guard_init(thread); +#endif #ifdef ARCH_CPU_STACK_GROWS_UPWARD thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (void *)((char *)thread->stack_addr), -- cgit v1.3.1