修复栈泄露的bug

This commit is contained in:
root
2023-08-27 21:25:09 +08:00
parent 363c2da50c
commit f8ac961264
14 changed files with 145 additions and 113 deletions

View File

@@ -1,38 +1,38 @@
/** /**
****************************************************************************** ******************************************************************************
* @file Project/STM32F2xx_StdPeriph_Template/stm32f2xx_it.c * @file Project/STM32F2xx_StdPeriph_Template/stm32f2xx_it.c
* @author MCD Application Team * @author MCD Application Team
* @version V1.1.0 * @version V1.1.0
* @date 13-April-2012 * @date 13-April-2012
* @brief Main Interrupt Service Routines. * @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and * This file provides template for all exceptions handler and
* peripherals interrupt service routine. * peripherals interrupt service routine.
****************************************************************************** ******************************************************************************
* @attention * @attention
* *
* <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2> * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
* *
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License. * You may not use this file except in compliance with the License.
* You may obtain a copy of the License at: * You may obtain a copy of the License at:
* *
* http://www.st.com/software_license_agreement_liberty_v2 * http://www.st.com/software_license_agreement_liberty_v2
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
****************************************************************************** ******************************************************************************
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "stm32f2xx_it.h" #include "stm32f2xx_it.h"
/** @addtogroup Template_Project /** @addtogroup Template_Project
* @{ * @{
*/ */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
@@ -46,19 +46,19 @@
/******************************************************************************/ /******************************************************************************/
/** /**
* @brief This function handles NMI exception. * @brief This function handles NMI exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void NMI_Handler(void) void NMI_Handler(void)
{ {
} }
/** /**
* @brief This function handles Hard Fault exception. * @brief This function handles Hard Fault exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void HardFault_Handler(void) void HardFault_Handler(void)
{ {
/* Go to infinite loop when Hard Fault exception occurs */ /* Go to infinite loop when Hard Fault exception occurs */
@@ -68,10 +68,10 @@ void HardFault_Handler(void)
} }
/** /**
* @brief This function handles Memory Manage exception. * @brief This function handles Memory Manage exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void MemManage_Handler(void) void MemManage_Handler(void)
{ {
/* Go to infinite loop when Memory Manage exception occurs */ /* Go to infinite loop when Memory Manage exception occurs */
@@ -81,10 +81,10 @@ void MemManage_Handler(void)
} }
/** /**
* @brief This function handles Bus Fault exception. * @brief This function handles Bus Fault exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void BusFault_Handler(void) void BusFault_Handler(void)
{ {
/* Go to infinite loop when Bus Fault exception occurs */ /* Go to infinite loop when Bus Fault exception occurs */
@@ -94,10 +94,10 @@ void BusFault_Handler(void)
} }
/** /**
* @brief This function handles Usage Fault exception. * @brief This function handles Usage Fault exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void UsageFault_Handler(void) void UsageFault_Handler(void)
{ {
/* Go to infinite loop when Usage Fault exception occurs */ /* Go to infinite loop when Usage Fault exception occurs */
@@ -107,37 +107,37 @@ void UsageFault_Handler(void)
} }
/** /**
* @brief This function handles SVCall exception. * @brief This function handles SVCall exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void SVC_Handler(void) void SVC_Handler(void)
{ {
} }
/** /**
* @brief This function handles Debug Monitor exception. * @brief This function handles Debug Monitor exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void DebugMon_Handler(void) void DebugMon_Handler(void)
{ {
} }
/** /**
* @brief This function handles PendSVC exception. * @brief This function handles PendSVC exception.
* @param None * @param None
* @retval None * @retval None
*/ */
void PendSV_Handler(void) void PendSV_Handler(void)
{ {
} }
/** /**
* @brief This function handles SysTick Handler. * @brief This function handles SysTick Handler.
* @param None * @param None
* @retval None * @retval None
*/ */
void SysTick_Handler(void) void SysTick_Handler(void)
{ {
// TimingDelay_Decrement(); // TimingDelay_Decrement();
@@ -151,17 +151,16 @@ void SysTick_Handler(void)
/******************************************************************************/ /******************************************************************************/
/** /**
* @brief This function handles PPP interrupt request. * @brief This function handles PPP interrupt request.
* @param None * @param None
* @retval None * @retval None
*/ */
/*void PPP_IRQHandler(void) /*void PPP_IRQHandler(void)
{ {
}*/ }*/
/** /**
* @} * @}
*/ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@@ -44,5 +44,6 @@ void arch_init(void)
mpu_init(); mpu_init();
SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk; SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk;
SCB->SHCSR |= SCB_SHCSR_BUSFAULTENA_Msk; SCB->SHCSR |= SCB_SHCSR_BUSFAULTENA_Msk;
((uint8_t *)(0xE000E008))[0] |= 0x6;
} }
INIT_LOW_HARD(arch_init); INIT_LOW_HARD(arch_init);

View File

@@ -30,13 +30,13 @@ PendSV_Handler:
// //
TST LR,#4 TST LR,#4
ITE EQ ITTEE EQ
MSREQ MSP, r3 MSREQ MSP, r3
MOVEQ r1,r3
MSRNE PSP, r3 MSRNE PSP, r3
MOVNE r0,r3
thread_sche: thread_sche:
MOV r0, r3 // r0:psp r1:msp r2:(0msp 1 psp)
MOV r1, r2
// r0:sp r1:(0msp 1 psp)
LDR.w r3,=schde_to LDR.w r3,=schde_to
BLX r3 BLX r3
// r1 psp r2 msp // r1 psp r2 msp
@@ -76,8 +76,10 @@ super_mode: //更新下msp的值
MOV R0,#0 MOV R0,#0
MSR CONTROL, R0 MSR CONTROL, R0
to_ret: to_ret:
CPSIE I
push {r0-r3, lr} push {r0-r3, lr}
bl mpu_switch_to bl mpu_switch_to
pop {r0-r3, lr} pop {r0-r3, lr}
CPSIE I
DSB
ISB
BX LR BX LR

View File

@@ -115,6 +115,14 @@ void MemManage_Handler(void)
} }
} }
} }
else
{
printk("semgement fault.\n");
/*TODO:杀死进程*/
while (1)
{
}
}
/* Go to infinite loop when Memory Manage exception occurs */ /* Go to infinite loop when Memory Manage exception occurs */
} }
@@ -141,7 +149,30 @@ void BusFault_Handler(void)
void UsageFault_Handler(void) void UsageFault_Handler(void)
{ {
printk("%s\n", __FUNCTION__); printk("%s\n", __FUNCTION__);
if (SCB->CFSR & (1 << 16))
{
printk("未定义指令错误\n");
}
if (SCB->CFSR & (1 << 17))
{
printk("试图切换到错误的状态\n");
}
if (SCB->CFSR & (1 << 18))
{
printk("试图执行EXC_RETURN错误的异常\n");
}
if (SCB->CFSR & (1 << 19))
{
printk("试图执行协处理器指令\n");
}
if (SCB->CFSR & (1 << 24))
{
printk("产生了非对其访问错误\n");
}
if (SCB->CFSR & (1 << 25))
{
printk("除零错误\n");
}
/* Go to infinite loop when Usage Fault exception occurs */ /* Go to infinite loop when Usage Fault exception occurs */
while (1) while (1)
{ {

View File

@@ -23,7 +23,6 @@ syscall_exit:
.type syscall_ret_user, %function .type syscall_ret_user, %function
syscall_ret_user: syscall_ret_user:
// //
@ mov r3,r0
//msp //msp
mrs r0, msp mrs r0, msp
@@ -31,8 +30,6 @@ syscall_ret_user:
ldmfd r0!,{r4-r11} ldmfd r0!,{r4-r11}
msr msp,r0 msr msp,r0
pop {r0,r1,r2,lr} pop {r0,r1,r2,lr}
//
@ str r3, [r0, #0]
msr CONTROL, r1 msr CONTROL, r1
bx lr bx lr

View File

@@ -32,5 +32,5 @@ enum region_rights
region_info_t *mm_space_alloc_pt_region(mm_space_t *m_space); region_info_t *mm_space_alloc_pt_region(mm_space_t *m_space);
void mm_space_free_pt_region(mm_space_t *m_space, region_info_t *ri); void mm_space_free_pt_region(mm_space_t *m_space, region_info_t *ri);
void mm_space_init(mm_space_t *mm_space); void mm_space_init(mm_space_t *mm_space, int is_knl);
bool_t mm_space_add(mm_space_t *m_space, umword_t addr, umword_t size, uint8_t attrs); bool_t mm_space_add(mm_space_t *m_space, umword_t addr, umword_t size, uint8_t attrs);

View File

@@ -17,5 +17,5 @@ typedef struct task
} task_t; } task_t;
task_t *thread_get_current_task(void); task_t *thread_get_current_task(void);
void task_init(task_t *task, ram_limit_t *ram); void task_init(task_t *task, ram_limit_t *ram, int is_knl);
task_t *task_create(ram_limit_t *lim); task_t *task_create(ram_limit_t *lim, int is_knl);

View File

@@ -13,7 +13,7 @@
#include "util.h" #include "util.h"
#include "arch.h" #include "arch.h"
#define THREAD_BLOCK_SIZE 0x200 //!< 线程块大小,栈在块的顶部 #define THREAD_BLOCK_SIZE 0x400 //!< 线程块大小,栈在块的顶部
enum thread_state enum thread_state
{ {
@@ -75,7 +75,7 @@ static inline pf_t *thread_get_current_pf(void)
return thread_get_pf(thread_get_current()); return thread_get_pf(thread_get_current());
} }
void thread_init(thread_t *th); void thread_init(thread_t *th);
void thread_set_exc_regs(thread_t *th, umword_t pc); void thread_set_exc_regs(thread_t *th, umword_t pc, umword_t sp);
thread_t *thread_create(ram_limit_t *ram); thread_t *thread_create(ram_limit_t *ram);
void thread_bind(thread_t *th, kobject_t *tk); void thread_bind(thread_t *th, kobject_t *tk);

View File

@@ -5,7 +5,7 @@
#include "mpu.h" #include "mpu.h"
#include "assert.h" #include "assert.h"
void mm_space_init(mm_space_t *mm_space) void mm_space_init(mm_space_t *mm_space, int is_knl)
{ {
region_info_t *regi_info; region_info_t *regi_info;
@@ -13,10 +13,12 @@ void mm_space_init(mm_space_t *mm_space)
{ {
mm_space->pt_regions[i].region_inx = -1; mm_space->pt_regions[i].region_inx = -1;
} }
if (!is_knl)
regi_info = mm_space_alloc_pt_region(mm_space); {
assert(regi_info); regi_info = mm_space_alloc_pt_region(mm_space);
mm_pages_init(&mm_space->mm_pages, regi_info); assert(regi_info);
mm_pages_init(&mm_space->mm_pages, regi_info);
}
} }
region_info_t *mm_space_alloc_pt_region(mm_space_t *m_space) region_info_t *mm_space_alloc_pt_region(mm_space_t *m_space)
{ {

View File

@@ -81,7 +81,7 @@ sched_t *scheduler_next(void)
return next_sch; return next_sch;
} }
sp_info_t *schde_to(void *sp, umword_t sp_type) sp_info_t *schde_to(void *usp, void *ksp, umword_t sp_type)
{ {
scheduler_t *sche = scheduler_get_current(); scheduler_t *sche = scheduler_get_current();
@@ -89,14 +89,8 @@ sp_info_t *schde_to(void *sp, umword_t sp_type)
sched_t *next = sche->cur_sche; sched_t *next = sche->cur_sche;
thread_t *next_th = container_of(next, thread_t, sche); thread_t *next_th = container_of(next, thread_t, sche);
if (!sp_type) cur_th->sp.knl_sp = ksp;
{ cur_th->sp.user_sp = usp;
cur_th->sp.knl_sp = sp;
}
else
{
cur_th->sp.user_sp = sp;
}
cur_th->sp.sp_type = sp_type; cur_th->sp.sp_type = sp_type;
return &next_th->sp; return &next_th->sp;
} }

View File

@@ -6,6 +6,7 @@
enum task_op_code enum task_op_code
{ {
TASK_OBJ_MAP, TASK_OBJ_MAP,
TASK_OBJ_UNMAP,
}; };
static msg_tag_t task_syscall_func(entry_frame_t *f) static msg_tag_t task_syscall_func(entry_frame_t *f)
@@ -22,7 +23,8 @@ static msg_tag_t task_syscall_func(entry_frame_t *f)
case TASK_OBJ_MAP: case TASK_OBJ_MAP:
/* code */ /* code */
break; break;
case TASK_OBJ_UNMAP:
break;
default: default:
break; break;
} }
@@ -30,18 +32,18 @@ static msg_tag_t task_syscall_func(entry_frame_t *f)
return tag; return tag;
} }
void task_init(task_t *task, ram_limit_t *ram) void task_init(task_t *task, ram_limit_t *ram, int is_knl)
{ {
assert(task); assert(task);
assert(ram); assert(ram);
kobject_init(&task->kobj); kobject_init(&task->kobj);
obj_space_init(&task->obj_space, ram); obj_space_init(&task->obj_space, ram);
mm_space_init(&task->mm_space); mm_space_init(&task->mm_space, is_knl);
task->lim = ram; task->lim = ram;
} }
task_t *task_create(ram_limit_t *lim) task_t *task_create(ram_limit_t *lim, int is_knl)
{ {
task_t *tk = mm_limit_alloc(lim, sizeof(task_t)); task_t *tk = mm_limit_alloc(lim, sizeof(task_t));
@@ -49,7 +51,7 @@ task_t *task_create(ram_limit_t *lim)
{ {
return NULL; return NULL;
} }
task_init(tk, lim); task_init(tk, lim, is_knl);
return tk; return tk;
} }
@@ -67,7 +69,7 @@ task_t *task_create(ram_limit_t *lim)
static kobject_t *task_create_func(ram_limit_t *lim, umword_t arg0, umword_t arg1, static kobject_t *task_create_func(ram_limit_t *lim, umword_t arg0, umword_t arg1,
umword_t arg2, umword_t arg3) umword_t arg2, umword_t arg3)
{ {
task_t *tk = task_create(lim); task_t *tk = task_create(lim, FALSE);
return &tk->kobj; return &tk->kobj;
} }

View File

@@ -36,11 +36,15 @@ void thread_init(thread_t *th)
* @param pc * @param pc
* @param ip * @param ip
*/ */
void thread_set_exc_regs(thread_t *th, umword_t pc) void thread_set_exc_regs(thread_t *th, umword_t pc, umword_t sp)
{ {
thread_t *cur_th = thread_get_current();
pf_t *th_pf = thread_get_pf(th); pf_t *th_pf = thread_get_pf(th);
th_pf->pf_s.pc = pc; th_pf->pf_s.pc = pc;
cur_th->sp.knl_sp = ((char *)cur_th + THREAD_BLOCK_SIZE - 8);
cur_th->sp.user_sp = (void *)((sp - 8) & ~7UL);
cur_th->sp.sp_type = 1;
} }
/** /**
* @brief 线程绑定到task * @brief 线程绑定到task

View File

@@ -27,14 +27,14 @@ void thread_exit(void)
void thread_user_pf_set(thread_t *cur_th, void *pc, void *user_sp, void *ram) void thread_user_pf_set(thread_t *cur_th, void *pc, void *user_sp, void *ram)
{ {
pf_t *cur_pf = (pf_t *)user_sp - 1; // thread_get_pf(cur_th); pf_t *cur_pf = (pf_t *)((addr_t)(user_sp - 8) & ~0x7UL) - 1; // thread_get_pf(cur_th);
cur_pf->pf_s.xpsr = 0x01000000L; cur_pf->pf_s.xpsr = 0x01000000L;
cur_pf->pf_s.lr = (umword_t)thread_exit; //!< 线程退出时调用的函数 cur_pf->pf_s.lr = (umword_t)thread_exit; //!< 线程退出时调用的函数
cur_pf->pf_s.pc = (umword_t)pc | 0x1; cur_pf->pf_s.pc = (umword_t)pc | 0x1;
cur_pf->rg1[5] = (umword_t)ram; cur_pf->rg1[5] = (umword_t)ram;
cur_th->sp.knl_sp = (char *)cur_th + THREAD_BLOCK_SIZE; cur_th->sp.knl_sp = ((char *)cur_th + THREAD_BLOCK_SIZE - 8);
cur_th->sp.user_sp = cur_pf; cur_th->sp.user_sp = cur_pf;
cur_th->sp.sp_type = 1; cur_th->sp.sp_type = 1;
// arch_set_user_sp((umword_t)cur_pf); // arch_set_user_sp((umword_t)cur_pf);

View File

@@ -32,7 +32,7 @@ static void knl_init_1(void)
thread_t *cur_th = thread_get_current(); thread_t *cur_th = thread_get_current();
thread_init(cur_th); thread_init(cur_th);
task_init(&knl_task, &root_factory_get()->limit); task_init(&knl_task, &root_factory_get()->limit, TRUE);
thread_bind(cur_th, &knl_task.kobj); thread_bind(cur_th, &knl_task.kobj);
thread_ready(cur_th, FALSE); thread_ready(cur_th, FALSE);
@@ -49,7 +49,7 @@ static void knl_init_2(void)
{ {
thread_t *init_thread = thread_create(&root_factory_get()->limit); thread_t *init_thread = thread_create(&root_factory_get()->limit);
assert(init_thread); assert(init_thread);
task_t *init_task = task_create(&root_factory_get()->limit); task_t *init_task = task_create(&root_factory_get()->limit, FALSE);
assert(init_task); assert(init_task);
app_info_t *app = app_info_get((void *)(KNL_TEXT + INIT_OFFSET)); app_info_t *app = app_info_get((void *)(KNL_TEXT + INIT_OFFSET));
@@ -112,6 +112,6 @@ void start_kernel(void)
thread_sched(); thread_sched();
cli(); cli();
while (1) while (1);
; // printk(".");
} }