完善不支持mpu的处理器支持,更加省内存

This commit is contained in:
zhangzheng
2024-01-28 14:57:12 +08:00
parent e3aecf4506
commit 931bb451b9
15 changed files with 92 additions and 23 deletions

2
.vscode/launch.json vendored
View File

@@ -48,7 +48,7 @@
// "miDebuggerPath": "/opt/homebrew/bin/arm-none-eabi-gdb",
"miDebuggerPath": "/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gdb",
// "miDebuggerPath": "/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gdb",
"miDebuggerServerAddress": "127.0.0.1:2331",
"miDebuggerServerAddress": "127.0.0.1:3333",
"MIMode": "gdb",
"setupCommands": [
{

View File

@@ -10,7 +10,7 @@ CONFIG_KNL_DATA_SIZE=0x10000
CONFIG_KNL_OFFSET=0x2000
CONFIG_INIT_TASK_OFFSET=0x10000
CONFIG_BOOTFS_OFFSET=0x22000
CONFIG_MK_MPU_CFG=y
CONFIG_MK_MPU_CFG=n
CONFIG_FT_ADDR_NR=16
CONFIG_SYS_SCHE_HZ=1000
CONFIG_USER_ISR_START_NO=16

View File

@@ -4,13 +4,13 @@
* Created on: ATShining
* Author: Administrator
*/
#include "types.h"
#include "mpu.h"
#include <mk_sys.h>
#include <mpu_armv7.h>
#include "thread.h"
#include "task.h"
#if CONFIG_MK_MPU_CFG
static volatile umword_t *MPUCR = (umword_t *)0xE000ED94;
void mpu_init(void)
@@ -64,3 +64,11 @@ void mpu_switch_to(void)
mpu_switch_to_task(tk);
}
#else
void mpu_switch_to(void)
{
}
void mpu_switch_to_task(struct task *tk)
{
}
#endif

View File

@@ -12,7 +12,6 @@
#include <mpu_armv8.h>
#include "thread.h"
#include "task.h"
static volatile umword_t *MPUCR = (umword_t *)0xE000ED94;
void mpu_init(void)
{
@@ -31,8 +30,9 @@ void mpu_calc_regs(region_info_t *region, umword_t addr, umword_t ffs_val, uint8
assert((addr & (MPU_ALIGN_SIZE - 1)) == 0);
assert((ffs_val & (MPU_ALIGN_SIZE - 1)) == 0);
region->rbar = ARM_MPU_RBAR(addr, ARM_MPU_SH_NON,
attrs == REGION_RO ? 1 : 0, 1, 1);
0, 0, attrs == REGION_RWX ? 0 : 1);
region->rasr = ARM_MPU_RLAR(addr + ffs_val, region->region_inx);
ARM_MPU_SetMemAttr(region->region_inx, ARM_MPU_ATTR_NON_CACHEABLE);
}
void mpu_region_set(int inx, umword_t rbar, umword_t rasr)
{

View File

@@ -3,8 +3,10 @@
#include "types.h"
#include "mm_page.h"
#include <assert.h>
typedef struct region_info
{
#if CONFIG_MK_MPU_CFG
umword_t start_addr; //!< 内存申请的开始地址
umword_t block_start_addr; //!< 块申请的开始地址
umword_t block_size; //!< 保护的块大小
@@ -13,22 +15,25 @@ typedef struct region_info
umword_t rasr; //!< mpu保护寄存器信息
int16_t region_inx;
uint8_t region; //!< 区域禁止信息
#endif
} region_info_t;
typedef struct mm_space
{
#if CONFIG_MK_MPU_CFG
region_info_t pt_regions[CONFIG_REGION_NUM]; //!< mpu内存保护块
#endif
// mm_pages_t mm_pages; //!< 模拟分页内存
void *mm_block; //!< task 的私有内存块
size_t mm_block_size; //!< 私有内存块的大小
} mm_space_t;
enum region_rights
{
REGION_PRIV = 1,
REGION_RO = 2,
REGION_RWX = 3,
};
#if CONFIG_MK_MPU_CFG
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);
@@ -36,7 +41,19 @@ void mm_space_free_pt_region(mm_space_t *m_space, region_info_t *ri);
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);
void mm_space_del(mm_space_t *m_space, umword_t addr);
#else
static inline void mm_space_init(mm_space_t *mm_space, int is_knl)
{
}
static inline bool_t mm_space_add(mm_space_t *m_space, umword_t addr, umword_t size, uint8_t attrs)
{
return TRUE;
}
static inline void mm_space_del(mm_space_t *m_space, umword_t addr)
{
}
#endif
static inline void mm_space_set_ram_block(mm_space_t *mm_space, void *mem, size_t size)
{
mm_space->mm_block = mem;
@@ -48,4 +65,4 @@ static inline void mm_space_get_ram_block(mm_space_t *mm_space, void **mem, size
assert(size);
*mem = mm_space->mm_block;
*size = mm_space->mm_block_size;
}
}

View File

@@ -2,6 +2,7 @@
#include "mm_space.h"
#include "task.h"
#if CONFIG_MK_MPU_CFG
#if CONFIG_MPU_VERSION == 2
#define MPU_ALIGN_SIZE 32
#endif
@@ -14,4 +15,28 @@ void mpu_region_clr(int inx);
void mpu_calc_regs(region_info_t *region, umword_t addr, umword_t ffs_val,
uint8_t attrs, uint8_t regions_bits);
void mpu_switch_to(void);
void mpu_switch_to_task(struct task *tk);
void mpu_switch_to_task(struct task *tk);
#else
static inline void mpu_init(void)
{
}
static inline void mpu_disable(void)
{
}
static inline void mpu_enable(void)
{
}
static inline void mpu_calc_regs(region_info_t *region, umword_t addr, umword_t ffs_val,
uint8_t attrs, uint8_t regions_bits)
{
}
static inline void mpu_region_set(int inx, umword_t rbar, umword_t rasr)
{
}
static inline void mpu_region_clr(int inx)
{
}
void mpu_switch_to(void);
void mpu_switch_to_task(struct task *tk);
#endif

View File

@@ -15,6 +15,8 @@
#include "util.h"
#include "mpu.h"
#include "printk.h"
#if CONFIG_MK_MPU_CFG
static bool_t mpu_calc(
mm_space_t *ms,
umword_t mem_start_addr,
@@ -204,3 +206,11 @@ again_alloc:
return ram;
#endif
}
#else
void *mpu_ram_alloc(mm_space_t *ms, ram_limit_t *r_limit, size_t ram_size)
{
void *ram = mm_limit_alloc(r_limit, ram_size);
return ram;
}
#endif

View File

@@ -84,6 +84,7 @@ static void mm_man_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_t
break;
case MM_ALIGN_ALLOC:
{
#if CONFIG_MK_MPU_CFG
region_info_t *regi_info = mm_space_alloc_pt_region(&cur_task->mm_space);
if (regi_info)
@@ -112,6 +113,9 @@ static void mm_man_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_t
{
tag = msg_tag_init4(0, 0, 0, -ENOMEM);
}
#else
tag = msg_tag_init4(0, 0, 0, -ENOSYS);
#endif
}
break;
case MM_MOD_ATTRS:

View File

@@ -16,6 +16,7 @@
#include "mm_page.h"
#include "mm_space.h"
#include "mpu.h"
#if CONFIG_MK_MPU_CFG
static mm_entry_t *mm_pages_entry_alloc(mm_pages_t *mm, addr_t new_addr)
{
for (int i = 0; i < PAGE_NR; i++)
@@ -127,4 +128,5 @@ void *mm_page_alloc_fault(mm_pages_t *mm, addr_t addr)
}
return NULL;
}
}
#endif

View File

@@ -13,21 +13,13 @@
#include "mm_space.h"
#include "mpu.h"
#include "assert.h"
#if CONFIG_MK_MPU_CFG
void mm_space_init(mm_space_t *mm_space, int is_knl)
{
// region_info_t *regi_info;
for (int i = 0; i < CONFIG_REGION_NUM; i++)
{
mm_space->pt_regions[i].region_inx = -1;
}
if (!is_knl)
{
// regi_info = mm_space_alloc_pt_region(mm_space);
// 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)
{
@@ -90,3 +82,4 @@ void mm_space_del(mm_space_t *m_space, umword_t addr)
}
}
}
#endif

View File

@@ -235,13 +235,16 @@ static void share_mem_init(share_mem_t *sm, umword_t max)
*/
static share_mem_t *share_mem_create(ram_limit_t *lim, size_t max)
{
int align_size = max;
#if CONFIG_MK_MPU_CFG
#if CONFIG_MPU_VERSION == 1
if (max < PAGE_SIZE || !is_power_of_2(max))
{
//!< 大小必须是2的整数倍
return NULL;
}
align_size = max;
#elif CONFIG_MPU_VERSION == 2
if (max < MPU_ALIGN_SIZE || (max & (MPU_ALIGN_SIZE - 1)))
{
@@ -249,15 +252,18 @@ static share_mem_t *share_mem_create(ram_limit_t *lim, size_t max)
return NULL;
}
max += MPU_ALIGN_SIZE;
align_size = MPU_ALIGN_SIZE;
#endif
#else
align_size = sizeof(void *);
#endif
share_mem_t *mem = mm_limit_alloc(lim, sizeof(share_mem_t));
if (!mem)
{
return NULL;
}
mem->mem = mm_limit_alloc_align(lim, max, CONFIG_MPU_VERSION == 2 ? MPU_ALIGN_SIZE : max);
mem->mem = mm_limit_alloc_align(lim, max, align_size);
if (!mem->mem)
{
mm_limit_free(lim, mem);

View File

@@ -313,7 +313,11 @@ static void task_release_stage2(kobject_t *kobj)
// task_t *cur_tk = thread_get_current_task();
obj_space_release(&tk->obj_space, tk->lim);
#if CONFIG_MK_MPU_CFG
mm_limit_free_align(tk->lim, tk->mm_space.mm_block, tk->mm_space.mm_block_size);
#else
mm_limit_free(tk->lim, tk->mm_space.mm_block);
#endif
mm_limit_free(tk->lim, tk);
// if (cur_tk == tk)
// {

View File

@@ -2,7 +2,7 @@ ENTRY(Reset_Handler)
MEMORY
{
RAM (arw) : ORIGIN = 0x20000000, LENGTH = 0x10000
FLASH (arx) : ORIGIN = 0x00000000 + 0x2000, LENGTH = 0x10000 - 0x2000
FLASH (arx) : ORIGIN = 0x8000000 + 0x2000, LENGTH = 0x10000 - 0x2000
}
SECTIONS
{

View File

@@ -36,6 +36,7 @@
static void test(void)
{
#if 0
mpu_test();
sharea_mem_test();
printf_test();
ulog_test();
@@ -45,7 +46,6 @@ static void test(void)
mm_test();
app_test();
mpu_test();
thread_press_test();
kobj_create_press_test();
u_sleep_ms(1000);

View File

@@ -5,7 +5,7 @@ void mpu_test(void)
#if 1
// mpu保护测试
int *test = ((int *)0x8000000);
int *test = ((int *)80000000);
*test = 1;
printf("test is %d\n", *test);
#endif