@@ -110,8 +110,21 @@ CONFIG_RT_USING_PIN=y
|
||||
CONFIG_CPU_TYPE="stm32f4"
|
||||
CONFIG_RTT_DIR="./"
|
||||
CONFIG_ARCH="cortex-m4"
|
||||
CONFIG_FLOAT_TYPE="hard"
|
||||
# CONFIG_FLOAT_TYPE="hard"
|
||||
CONFIG_BOARD_NAME="STM32F407VET6"
|
||||
CONFIG_BUDDY_SLAB=n
|
||||
CONFIG_SMP=n
|
||||
CONFIG_CPU=1
|
||||
CONFIG_THREAD_BLOCK_SIZE=0x400
|
||||
CONFIG_PAGE_SHIFT=9
|
||||
CONFIG_MMU=n
|
||||
CONFIG_KNL_TEST=n
|
||||
CONFIG_ELF_LAUNCH=n
|
||||
CONFIG_THREAD_MSG_BUG_LEN=128
|
||||
CONFIG_THREAD_IPC_MSG_LEN=96
|
||||
CONFIG_THREAD_MAP_BUF_LEN=16
|
||||
CONFIG_MSG_BUF_VADDR=0xE0000000
|
||||
CONFIG_BOOT_FS_VADDR=0xE0001000
|
||||
CONFIG_MPU_PAGE_FAULT_SUPPORT=y
|
||||
CONFIG_MPU_PAGE_NUM=64
|
||||
CONFIG_MPU_PAGE_FAULT_REGIONS_NUM=2
|
||||
@@ -86,64 +86,33 @@ void MemManage_Handler(void)
|
||||
{
|
||||
bool_t is_knl = is_run_knl();
|
||||
addr_t fault_addr = (addr_t)(SCB->MMFAR);
|
||||
addr_t bus_addr = (addr_t)(SCB->BFAR);
|
||||
task_t *cur_task = thread_get_current_task();
|
||||
umword_t status;
|
||||
|
||||
// printk("%s\n", __FUNCTION__);
|
||||
if (SCB->CFSR & 0x1)
|
||||
if ((SCB->CFSR & 0x1) || (SCB->CFSR & 0x2))
|
||||
{
|
||||
printk("instr 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 2)
|
||||
{
|
||||
printk("data 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 128)
|
||||
{
|
||||
fault_addr = (addr_t)(SCB->MMFAR);
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
// printk("instr || data 0x%x access is error.\n", fault_addr);
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, mem_addr:0x%lx bus_addr:0x%lx semgement fault.\n",
|
||||
thread_get_current_task(), fault_addr, bus_addr);
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
}
|
||||
if ((SCB->CFSR & 8))
|
||||
{
|
||||
printk("出栈错误\n");
|
||||
// fault_addr = arch_get_user_sp();
|
||||
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
printk("push stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 16)
|
||||
{
|
||||
printk("压栈错误\n");
|
||||
// fault_addr = arch_get_user_sp();
|
||||
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
printk("pop stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 32)
|
||||
{
|
||||
printk("浮点惰性压栈错误\n");
|
||||
}
|
||||
|
||||
end:
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, semgement fault.\n", thread_get_current_task());
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
printk("Floating point lazy stack error.\n");
|
||||
}
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,7 +49,14 @@ void mpu_switch_to_task(struct task *tk)
|
||||
region_info_t *ri = &tk->mm_space.mem_vma.pt_regions[i];
|
||||
if (ri->region_inx >= 0)
|
||||
{
|
||||
ARM_MPU_SetRegionEx(ri->region_inx, ri->rbar, ri->rasr);
|
||||
if (ri->rbar == 0 && ri->rasr == 0)
|
||||
{
|
||||
ARM_MPU_ClrRegion(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
ARM_MPU_SetRegionEx(ri->region_inx, ri->rbar, ri->rasr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -39,48 +39,33 @@ void MemManage_Handler(void)
|
||||
{
|
||||
bool_t is_knl = is_run_knl();
|
||||
addr_t fault_addr = (addr_t)(SCB->MMFAR);
|
||||
addr_t bus_addr = (addr_t)(SCB->BFAR);
|
||||
task_t *cur_task = thread_get_current_task();
|
||||
umword_t status;
|
||||
|
||||
// printk("%s\n", __FUNCTION__);
|
||||
if (SCB->CFSR & 0x1)
|
||||
if ((SCB->CFSR & 0x1) || (SCB->CFSR & 0x2))
|
||||
{
|
||||
printk("instr 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 2)
|
||||
{
|
||||
printk("data 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 128)
|
||||
{
|
||||
fault_addr = (addr_t)(SCB->MMFAR);
|
||||
|
||||
goto end;
|
||||
// printk("instr || data 0x%x access is error.\n", fault_addr);
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, mem_addr:0x%lx bus_addr:0x%lx semgement fault.\n",
|
||||
thread_get_current_task(), fault_addr, bus_addr);
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
}
|
||||
if ((SCB->CFSR & 8))
|
||||
{
|
||||
printk("出栈错误\n");
|
||||
|
||||
goto end;
|
||||
printk("push stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 16)
|
||||
{
|
||||
printk("压栈错误\n");
|
||||
|
||||
goto end;
|
||||
printk("pop stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 32)
|
||||
{
|
||||
printk("浮点惰性压栈错误\n");
|
||||
}
|
||||
|
||||
end:
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, semgement fault.\n", thread_get_current_task());
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
printk("Floating point lazy stack error.\n");
|
||||
}
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,64 +82,33 @@ void MemManage_Handler(void)
|
||||
{
|
||||
bool_t is_knl = is_run_knl();
|
||||
addr_t fault_addr = (addr_t)(SCB->MMFAR);
|
||||
addr_t bus_addr = (addr_t)(SCB->BFAR);
|
||||
task_t *cur_task = thread_get_current_task();
|
||||
umword_t status;
|
||||
|
||||
// printk("%s\n", __FUNCTION__);
|
||||
if (SCB->CFSR & 0x1)
|
||||
if ((SCB->CFSR & 0x1) || (SCB->CFSR & 0x2))
|
||||
{
|
||||
printk("instr 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 2)
|
||||
{
|
||||
printk("data 0x%x access is error.\n", fault_addr);
|
||||
}
|
||||
if (SCB->CFSR & 128)
|
||||
{
|
||||
fault_addr = (addr_t)(SCB->MMFAR);
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
// printk("instr || data 0x%x access is error.\n", fault_addr);
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, mem_addr:0x%lx bus_addr:0x%lx semgement fault.\n",
|
||||
thread_get_current_task(), fault_addr, bus_addr);
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
}
|
||||
if ((SCB->CFSR & 8))
|
||||
{
|
||||
printk("出栈错误\n");
|
||||
// fault_addr = arch_get_user_sp();
|
||||
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
printk("push stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 16)
|
||||
{
|
||||
printk("压栈错误\n");
|
||||
// fault_addr = arch_get_user_sp();
|
||||
|
||||
// if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
// {
|
||||
// goto end;
|
||||
// }
|
||||
// return;
|
||||
goto end;
|
||||
printk("pop stack is error.\n");
|
||||
}
|
||||
if (SCB->CFSR & 32)
|
||||
{
|
||||
printk("浮点惰性压栈错误\n");
|
||||
}
|
||||
|
||||
end:
|
||||
if (task_vma_page_fault(&(thread_get_current_task()->mm_space.mem_vma),
|
||||
ALIGN_DOWN(fault_addr, PAGE_SIZE), NULL) < 0)
|
||||
{
|
||||
printk("task:0x%x, semgement fault.\n", thread_get_current_task());
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
printk("Floating point lazy stack error.\n");
|
||||
}
|
||||
task_knl_kill(thread_get_current(), is_knl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,7 +42,21 @@ bool_t is_rw_access(task_t *tg_task, void *addr, size_t size, bool_t ignore_null
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_MPU_PAGE_FAULT_SUPPORT)
|
||||
task_vma_t *vma = &tg_task->mm_space.mem_vma;
|
||||
for (int i = 0; i < MPU_PAGE_NUM; i++)
|
||||
{
|
||||
/*TODO:加锁*/
|
||||
if (vma->mem_pages[i] == NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (vma->mem_pages[i] == (void *)ALIGN_DOWN((addr_t)addr, PAGE_SIZE) && (vma->mem_pages_attrs[i] & VPAGE_PROT_RW))
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return FALSE;
|
||||
#else
|
||||
return TRUE;
|
||||
|
||||
@@ -150,6 +150,7 @@ int task_vma_alloc(task_vma_t *task_vma, vma_addr_t vaddr, size_t size,
|
||||
ret = -EINVAL;
|
||||
goto err_end;
|
||||
}
|
||||
mword_t status = spinlock_lock(&task_vma->lock);
|
||||
|
||||
if (paddr == 0)
|
||||
{
|
||||
@@ -189,6 +190,8 @@ int task_vma_alloc(task_vma_t *task_vma, vma_addr_t vaddr, size_t size,
|
||||
if (alloc_cn == 0)
|
||||
{
|
||||
task_vma->mem_pages_alloc_size[i] = size;
|
||||
} else {
|
||||
task_vma->mem_pages_alloc_size[i] = 0;
|
||||
}
|
||||
alloc_cn++;
|
||||
if (alloc_cn * PAGE_SIZE >= size)
|
||||
@@ -246,6 +249,7 @@ err_end:
|
||||
mm_limit_free_align(pt_task->lim, (void *)vma_addr, mem_align_size);
|
||||
}
|
||||
end:
|
||||
spinlock_set(&task_vma->lock, status);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
@@ -281,6 +285,8 @@ static int task_vma_free_inner(task_vma_t *task_vma, vaddr_t vaddr, size_t size,
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
mword_t status = spinlock_lock(&task_vma->lock);
|
||||
|
||||
pt_task = container_of(
|
||||
container_of(task_vma, mm_space_t, mem_vma),
|
||||
task_t,
|
||||
@@ -346,6 +352,7 @@ end:
|
||||
{
|
||||
mpu_switch_to_task(pt_task);
|
||||
}
|
||||
spinlock_set(&task_vma->lock, status);
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
@@ -391,11 +398,14 @@ int task_vma_page_fault(task_vma_t *task_vma, vaddr_t addr, void *paddr)
|
||||
assert((addr & (PAGE_SIZE - 1)) == 0);
|
||||
task_t *pt_task;
|
||||
assert(task_vma);
|
||||
int ret = -ENOENT;
|
||||
|
||||
pt_task = container_of(
|
||||
container_of(task_vma, mm_space_t, mem_vma),
|
||||
task_t,
|
||||
mm_space);
|
||||
mword_t status = spinlock_lock(&task_vma->lock);
|
||||
|
||||
#if IS_ENABLED(MPU_PAGE_FAULT_SUPPORT) //!< 缺页模拟
|
||||
for (int i = 0; i < MPU_PAGE_NUM; i++)
|
||||
{
|
||||
@@ -413,6 +423,7 @@ int task_vma_page_fault(task_vma_t *task_vma, vaddr_t addr, void *paddr)
|
||||
task_vma->mem_pages_pt_regions[sel_region]->start_addr = addr;
|
||||
task_vma->mem_pages_pt_regions[sel_region]->size = PAGE_SIZE;
|
||||
task_vma->pt_regions_sel++;
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -420,7 +431,8 @@ int task_vma_page_fault(task_vma_t *task_vma, vaddr_t addr, void *paddr)
|
||||
{
|
||||
mpu_switch_to_task(pt_task);
|
||||
}
|
||||
return 0;
|
||||
spinlock_set(&task_vma->lock, status);
|
||||
return ret;
|
||||
#else
|
||||
return -ENOSYS;
|
||||
#endif
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
# export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
# export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7e-m+fp/hard
|
||||
|
||||
export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7e-m+fp/hard
|
||||
# export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
# export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7e-m+fp/hard
|
||||
|
||||
export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
|
||||
export BOARD=STM32F407VET6
|
||||
export CROSS_COMPILE_NAME=arm-none-eabi-
|
||||
|
||||
@@ -16,7 +16,7 @@ enum vpage_prot_attrs
|
||||
};
|
||||
|
||||
#define VMA_ADDR_RESV 0x1 //!< flags 保留内存
|
||||
#define VMA_ADDR_PAGE_FAULT_SIM 0x2 //!< page fault模拟
|
||||
#define VMA_ADDR_PAGE_FAULT_SIM 0x2 //!< page fault模拟,使用mpu模拟,该内存不能被栈使用
|
||||
|
||||
typedef union vma_addr
|
||||
{
|
||||
|
||||
@@ -124,7 +124,53 @@ static void vmm_press_block_sim_page_fault_test(CuTest *cu)
|
||||
#undef TEST_MM_SIZE
|
||||
#undef TEST_MEM_CN
|
||||
}
|
||||
#include <u_thread_util.h>
|
||||
#include <u_sleep.h>
|
||||
#include <u_log.h>
|
||||
static obj_handler_t th_hd;
|
||||
static void mem_sim_thread(void)
|
||||
{
|
||||
int i = 0;
|
||||
uint8_t data[512];
|
||||
while (1)
|
||||
{
|
||||
memset(data, 0, 512);
|
||||
ulog_write_str(LOG_PROT, "i am live.\n");
|
||||
u_sleep_ms(1000);
|
||||
i++;
|
||||
if (i >= 40)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
u_sleep_ms(100000);
|
||||
}
|
||||
}
|
||||
static void vmm_press_block_sim_thread_test(CuTest *cu)
|
||||
{
|
||||
msg_tag_t tag;
|
||||
addr_t addr;
|
||||
int ret;
|
||||
tag = u_vmam_alloc(VMA_PROT, vma_addr_create(VPAGE_PROT_RWX, VMA_ADDR_PAGE_FAULT_SIM, 0), PAGE_SIZE * 4, 0, &addr);
|
||||
CuAssert(cu, "vmam alloc failed.\n", msg_tag_get_val(tag) >= 0);
|
||||
addr_t msg_addr;
|
||||
tag = u_vmam_alloc(VMA_PROT, vma_addr_create(VPAGE_PROT_RWX, VMA_ADDR_PAGE_FAULT_SIM, 0), PAGE_SIZE, 0, &msg_addr);
|
||||
CuAssert(cu, "vmam alloc failed.\n", msg_tag_get_val(tag) >= 0);
|
||||
memset((void *)addr, 0, PAGE_SIZE * 4);
|
||||
memset((void *)msg_addr, 0, PAGE_SIZE);
|
||||
printf("sim_addr:0x%lx, msg_addr:0x%lx\n", addr, msg_addr);
|
||||
ret = u_thread_create(&th_hd, (void *)(addr + (PAGE_SIZE * 4) - sizeof(void *) * 2), (void *)msg_addr, mem_sim_thread);
|
||||
CuAssert(cu, "u_thread_create failed.\n", ret >= 0);
|
||||
u_thread_run(th_hd, 2);
|
||||
while (1)
|
||||
{
|
||||
u_sleep_ms(10000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static CuSuite suite;
|
||||
CuSuite *vmm_test_suite(void)
|
||||
{
|
||||
@@ -137,6 +183,9 @@ CuSuite *vmm_test_suite(void)
|
||||
SUITE_ADD_TEST(&suite, vmm_press_block_test);
|
||||
#if IS_ENABLED(CONFIG_MPU_PAGE_FAULT_SUPPORT)
|
||||
SUITE_ADD_TEST(&suite, vmm_press_block_sim_page_fault_test);
|
||||
// mpu模拟缺页的内存不能用作栈内存,这是硬件限制导致的!
|
||||
// SUITE_ADD_TEST(&suite, vmm_press_block_sim_thread_test);
|
||||
#endif
|
||||
|
||||
return &suite;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user