赞
踩
以mt8173为例
- /*******************************************************************************
- * Macro generating the code for the function setting up the pagetables as per
- * the platform memory map & initialize the mmu, for the given exception level
- ******************************************************************************/
- #define DEFINE_CONFIGURE_MMU_EL(_el) \
- void plat_configure_mmu_el ## _el(unsigned long total_base, \
- unsigned long total_size, \
- unsigned long ro_start, \
- unsigned long ro_limit, \
- unsigned long coh_start, \
- unsigned long coh_limit) \
- { \
- mmap_add_region(total_base, total_base, \
- total_size, \
- MT_MEMORY | MT_RW | MT_SECURE); \
- mmap_add_region(ro_start, ro_start, \
- ro_limit - ro_start, \
- MT_MEMORY | MT_RO | MT_SECURE); \
- mmap_add_region(coh_start, coh_start, \
- coh_limit - coh_start, \
- MT_DEVICE | MT_RW | MT_SECURE); \
- mmap_add(plat_mmap); \
- init_xlat_tables(); \
- \
- enable_mmu_el ## _el(0); \
- }
-
- /* Define EL3 variants of the function initialising the MMU */
- DEFINE_CONFIGURE_MMU_EL(3)
-
- DEFINE_ENABLE_MMU_EL(3,TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),tlbialle3) //定义el3 mmu函数
mmap region 内存添加到系统中
- void mmap_add_region(unsigned long base_pa, unsigned long base_va,
- unsigned long size, unsigned attr)
- {
- mmap_region_t *mm = mmap;
- mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
- unsigned long pa_end = base_pa + size - 1;
- unsigned long va_end = base_va + size - 1;
-
- assert(IS_PAGE_ALIGNED(base_pa));
- assert(IS_PAGE_ALIGNED(base_va));
- assert(IS_PAGE_ALIGNED(size));
-
- if (!size)
- return;
-
- /* Find correct place in mmap to insert new region */
- while (mm->base_va < base_va && mm->size)//从开始遍历,查看需要插入到哪个位置,base_va是需要插入的va
- ++mm;
- //mm->base_va >= base_va 则退出循环
- /* Make room for new region by moving other regions up by one place */
- memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);//从mm数组元素开始到最后,整体向后移动1个单位
-
- /* Check we haven't lost the empty sentinal from the end of the array */
- assert(mm_last->size == 0);
-
- mm->base_pa = base_pa;//insert new value
- mm->base_va = base_va;
- mm->size = size;
- mm->attr = attr;
-
- if (pa_end > max_pa)
- max_pa = pa_end;
- if (va_end > max_va)
- max_va = va_end;
- }
-
- void mmap_add(const mmap_region_t *mm)
- {
- while (mm->size) {
- mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
- ++mm;
- }
- }
初始化页表,使用了递归操作
- static mmap_region_t *init_xlation_table(mmap_region_t *mm,
- unsigned long base_va,
- unsigned long *table, unsigned level)
- {
- unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
- XLAT_TABLE_ENTRIES_SHIFT;
- unsigned level_size = 1 << level_size_shift;
- unsigned long level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
-
- assert(level <= 3);
-
- debug_print("New xlat table:\n");
-
- do {
- unsigned long desc = UNSET_DESC;//0xffffffff
-
- if (mm->base_va + mm->size <= base_va) {//若此时mm映射过了,跳入下一个mm,base_va一直在递增
- /* Area now after the region so skip it */
- ++mm;
- continue;
- }
-
- debug_print(" %010lx %8lx " + 6 - 2 * level, base_va,
- level_size);
-
- if (mm->base_va >= base_va + level_size) {//说明在base_va的后面,先设置成invalid
- /* Next region is after area so nothing to map yet */
- desc = INVALID_DESC;
- } else if (mm->base_va <= base_va && mm->base_va + mm->size >=
- base_va + level_size) {//若需要映射区域可以覆盖此level一级大小,则不需要下一级
- /* Next region covers all of area */
- int attr = mmap_region_attr(mm, base_va, level_size);//获取映射属性
- if (attr >= 0)
- desc = mmap_desc(attr,
- base_va - mm->base_va + mm->base_pa,
- level);
- }
- /* else Next region only partially covers area, so need */
-
- if (desc == UNSET_DESC) {//其他情况都需要下一级页表,主要包括2M区域,mm的小于2M的区域则走这个地方
- /* Area not covered by a region so need finer table */
- unsigned long *new_table = xlat_tables[next_xlat++];
- assert(next_xlat <= MAX_XLAT_TABLES);
- desc = TABLE_DESC | (unsigned long)new_table;
-
- /* Recurse to fill in new table */
- mm = init_xlation_table(mm, base_va,
- new_table, level+1);//递归调用,填充下一级表格
- }
-
- debug_print("\n");
-
- *table++ = desc;//表描述符赋值
- base_va += level_size;// 虚拟地址增加
- } while (mm->size && (base_va & level_index_mask));//base_va & level_index_mask为0时结束递归代表则返回到向上一个level函数
-
- return mm;
- }
使能mmu 重要寄存器(system register)配置代码
- /*******************************************************************************
- * Macro generating the code for the function enabling the MMU in the given
- * exception level, assuming that the pagetables have already been created.
- *
- * _el: Exception level at which the function will run
- * _tcr_extra: Extra bits to set in the TCR register. This mask will
- * be OR'ed with the default TCR value.
- * _tlbi_fct: Function to invalidate the TLBs at the current
- * exception level
- ******************************************************************************/
- #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
- void enable_mmu_el##_el(uint32_t flags) \
- { \
- uint64_t mair, tcr, ttbr; \
- uint32_t sctlr; \
- \
- assert(IS_IN_EL(_el)); \
- assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
- \
- /* Set attributes in the right indices of the MAIR */ \
- mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \ //attr1
- mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
- ATTR_IWBWA_OWBWA_NTR_INDEX); \ //attr0 inner writeback,write alloc,outer writeback,write alloc,
- write_mair_el##_el(mair); \
- \
- /* Invalidate TLBs at the current exception level */ \
- _tlbi_fct(); \
- \
- /* Set TCR bits as well. */ \
- /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
- tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
- TCR_RGN_INNER_WBA | \
- (64 - __builtin_ctzl(ADDR_SPACE_SIZE)); \
- tcr |= _tcr_extra; \
- write_tcr_el##_el(tcr); \ //-----------tcr 写入
- \
- /* Set TTBR bits as well */ \
- ttbr = (uint64_t) l1_xlation_table; \
- write_ttbr0_el##_el(ttbr); \ //-----页表基地址写入
- \
- /* Ensure all translation table writes have drained */ \
- /* into memory, the TLB invalidation is complete, */ \
- /* and translation register writes are committed */ \
- /* before enabling the MMU */ \
- dsb(); \
- isb(); \
- \
- sctlr = read_sctlr_el##_el(); \
- sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \
- \
- if (flags & DISABLE_DCACHE) \
- sctlr &= ~SCTLR_C_BIT; \
- else \
- sctlr |= SCTLR_C_BIT; \
- \
- write_sctlr_el##_el(sctlr); \ //----------sctlr_el3写入
- \
- /* Ensure the MMU enable takes effect immediately */ \
- isb(); \
- }
-
- /* Define EL1 and EL3 variants of the function enabling the MMU */
- DEFINE_ENABLE_MMU_EL(1,(tcr_ps_bits << TCR_EL1_IPS_SHIFT),tlbivmalle1) //定义el1 mmu函数
- DEFINE_ENABLE_MMU_EL(3,TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),tlbialle3) //定义el3 mmu函数
重要的block or page 描述符赋值代码
- static unsigned long mmap_desc(unsigned attr, unsigned long addr_pa,
- unsigned level)
- {
- unsigned long desc = addr_pa;
-
- desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;//bit[1:0]
-
- desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0; //NS, bit[5]
-
- desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);//AP[2:1]---> bits[7:6]
-
- desc |= LOWER_ATTRS(ACCESS_FLAG);//bit10
-
- if (attr & MT_MEMORY) {
- desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);//attr0@mair_el3
- if (attr & MT_RW)
- desc |= UPPER_ATTRS(XN);//读写区域不能执行指令 ,bit[54] xn
- } else {
- desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);//attr1@mair_el3,device memory outer shareable
- desc |= UPPER_ATTRS(XN);//no instruction execute
- }
-
- debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
- debug_print(attr & MT_RW ? "-RW" : "-RO");
- debug_print(attr & MT_NS ? "-NS" : "-S");
-
- return desc;
- }
代码调用总结如下图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。