当前位置:   article > 正文

ATF 页表构建代码分析_atf code分析

atf code分析

以mt8173为例 

  1. /*******************************************************************************
  2. * Macro generating the code for the function setting up the pagetables as per
  3. * the platform memory map & initialize the mmu, for the given exception level
  4. ******************************************************************************/
  5. #define DEFINE_CONFIGURE_MMU_EL(_el) \
  6. void plat_configure_mmu_el ## _el(unsigned long total_base, \
  7. unsigned long total_size, \
  8. unsigned long ro_start, \
  9. unsigned long ro_limit, \
  10. unsigned long coh_start, \
  11. unsigned long coh_limit) \
  12. { \
  13. mmap_add_region(total_base, total_base, \
  14. total_size, \
  15. MT_MEMORY | MT_RW | MT_SECURE); \
  16. mmap_add_region(ro_start, ro_start, \
  17. ro_limit - ro_start, \
  18. MT_MEMORY | MT_RO | MT_SECURE); \
  19. mmap_add_region(coh_start, coh_start, \
  20. coh_limit - coh_start, \
  21. MT_DEVICE | MT_RW | MT_SECURE); \
  22. mmap_add(plat_mmap); \
  23. init_xlat_tables(); \
  24. \
  25. enable_mmu_el ## _el(0); \
  26. }
  27. /* Define EL3 variants of the function initialising the MMU */
  28. DEFINE_CONFIGURE_MMU_EL(3)
  29. DEFINE_ENABLE_MMU_EL(3,TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),tlbialle3) //定义el3 mmu函数

 

 mmap region 内存添加到系统中

  1. void mmap_add_region(unsigned long base_pa, unsigned long base_va,
  2. unsigned long size, unsigned attr)
  3. {
  4. mmap_region_t *mm = mmap;
  5. mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
  6. unsigned long pa_end = base_pa + size - 1;
  7. unsigned long va_end = base_va + size - 1;
  8. assert(IS_PAGE_ALIGNED(base_pa));
  9. assert(IS_PAGE_ALIGNED(base_va));
  10. assert(IS_PAGE_ALIGNED(size));
  11. if (!size)
  12. return;
  13. /* Find correct place in mmap to insert new region */
  14. while (mm->base_va < base_va && mm->size)//从开始遍历,查看需要插入到哪个位置,base_va是需要插入的va
  15. ++mm;
  16. //mm->base_va >= base_va 则退出循环
  17. /* Make room for new region by moving other regions up by one place */
  18. memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);//从mm数组元素开始到最后,整体向后移动1个单位
  19. /* Check we haven't lost the empty sentinal from the end of the array */
  20. assert(mm_last->size == 0);
  21. mm->base_pa = base_pa;//insert new value
  22. mm->base_va = base_va;
  23. mm->size = size;
  24. mm->attr = attr;
  25. if (pa_end > max_pa)
  26. max_pa = pa_end;
  27. if (va_end > max_va)
  28. max_va = va_end;
  29. }
  30. void mmap_add(const mmap_region_t *mm)
  31. {
  32. while (mm->size) {
  33. mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
  34. ++mm;
  35. }
  36. }

初始化页表,使用了递归操作

  1. static mmap_region_t *init_xlation_table(mmap_region_t *mm,
  2. unsigned long base_va,
  3. unsigned long *table, unsigned level)
  4. {
  5. unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
  6. XLAT_TABLE_ENTRIES_SHIFT;
  7. unsigned level_size = 1 << level_size_shift;
  8. unsigned long level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
  9. assert(level <= 3);
  10. debug_print("New xlat table:\n");
  11. do {
  12. unsigned long desc = UNSET_DESC;//0xffffffff
  13. if (mm->base_va + mm->size <= base_va) {//若此时mm映射过了,跳入下一个mm,base_va一直在递增
  14. /* Area now after the region so skip it */
  15. ++mm;
  16. continue;
  17. }
  18. debug_print(" %010lx %8lx " + 6 - 2 * level, base_va,
  19. level_size);
  20. if (mm->base_va >= base_va + level_size) {//说明在base_va的后面,先设置成invalid
  21. /* Next region is after area so nothing to map yet */
  22. desc = INVALID_DESC;
  23. } else if (mm->base_va <= base_va && mm->base_va + mm->size >=
  24. base_va + level_size) {//若需要映射区域可以覆盖此level一级大小,则不需要下一级
  25. /* Next region covers all of area */
  26. int attr = mmap_region_attr(mm, base_va, level_size);//获取映射属性
  27. if (attr >= 0)
  28. desc = mmap_desc(attr,
  29. base_va - mm->base_va + mm->base_pa,
  30. level);
  31. }
  32. /* else Next region only partially covers area, so need */
  33. if (desc == UNSET_DESC) {//其他情况都需要下一级页表,主要包括2M区域,mm的小于2M的区域则走这个地方
  34. /* Area not covered by a region so need finer table */
  35. unsigned long *new_table = xlat_tables[next_xlat++];
  36. assert(next_xlat <= MAX_XLAT_TABLES);
  37. desc = TABLE_DESC | (unsigned long)new_table;
  38. /* Recurse to fill in new table */
  39. mm = init_xlation_table(mm, base_va,
  40. new_table, level+1);//递归调用,填充下一级表格
  41. }
  42. debug_print("\n");
  43. *table++ = desc;//表描述符赋值
  44. base_va += level_size;// 虚拟地址增加
  45. } while (mm->size && (base_va & level_index_mask));//base_va & level_index_mask为0时结束递归代表则返回到向上一个level函数
  46. return mm;
  47. }

使能mmu 重要寄存器(system register)配置代码

  1. /*******************************************************************************
  2. * Macro generating the code for the function enabling the MMU in the given
  3. * exception level, assuming that the pagetables have already been created.
  4. *
  5. * _el: Exception level at which the function will run
  6. * _tcr_extra: Extra bits to set in the TCR register. This mask will
  7. * be OR'ed with the default TCR value.
  8. * _tlbi_fct: Function to invalidate the TLBs at the current
  9. * exception level
  10. ******************************************************************************/
  11. #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
  12. void enable_mmu_el##_el(uint32_t flags) \
  13. { \
  14. uint64_t mair, tcr, ttbr; \
  15. uint32_t sctlr; \
  16. \
  17. assert(IS_IN_EL(_el)); \
  18. assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
  19. \
  20. /* Set attributes in the right indices of the MAIR */ \
  21. mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \ //attr1
  22. mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
  23. ATTR_IWBWA_OWBWA_NTR_INDEX); \ //attr0 inner writeback,write alloc,outer writeback,write alloc,
  24. write_mair_el##_el(mair); \
  25. \
  26. /* Invalidate TLBs at the current exception level */ \
  27. _tlbi_fct(); \
  28. \
  29. /* Set TCR bits as well. */ \
  30. /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
  31. tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
  32. TCR_RGN_INNER_WBA | \
  33. (64 - __builtin_ctzl(ADDR_SPACE_SIZE)); \
  34. tcr |= _tcr_extra; \
  35. write_tcr_el##_el(tcr); \ //-----------tcr 写入
  36. \
  37. /* Set TTBR bits as well */ \
  38. ttbr = (uint64_t) l1_xlation_table; \
  39. write_ttbr0_el##_el(ttbr); \ //-----页表基地址写入
  40. \
  41. /* Ensure all translation table writes have drained */ \
  42. /* into memory, the TLB invalidation is complete, */ \
  43. /* and translation register writes are committed */ \
  44. /* before enabling the MMU */ \
  45. dsb(); \
  46. isb(); \
  47. \
  48. sctlr = read_sctlr_el##_el(); \
  49. sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \
  50. \
  51. if (flags & DISABLE_DCACHE) \
  52. sctlr &= ~SCTLR_C_BIT; \
  53. else \
  54. sctlr |= SCTLR_C_BIT; \
  55. \
  56. write_sctlr_el##_el(sctlr); \ //----------sctlr_el3写入
  57. \
  58. /* Ensure the MMU enable takes effect immediately */ \
  59. isb(); \
  60. }
  61. /* Define EL1 and EL3 variants of the function enabling the MMU */
  62. DEFINE_ENABLE_MMU_EL(1,(tcr_ps_bits << TCR_EL1_IPS_SHIFT),tlbivmalle1) //定义el1 mmu函数
  63. DEFINE_ENABLE_MMU_EL(3,TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),tlbialle3) //定义el3 mmu函数

重要的block or page 描述符赋值代码

  1. static unsigned long mmap_desc(unsigned attr, unsigned long addr_pa,
  2. unsigned level)
  3. {
  4. unsigned long desc = addr_pa;
  5. desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;//bit[1:0]
  6. desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0; //NS, bit[5]
  7. desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);//AP[2:1]---> bits[7:6]
  8. desc |= LOWER_ATTRS(ACCESS_FLAG);//bit10
  9. if (attr & MT_MEMORY) {
  10. desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);//attr0@mair_el3
  11. if (attr & MT_RW)
  12. desc |= UPPER_ATTRS(XN);//读写区域不能执行指令 ,bit[54] xn
  13. } else {
  14. desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);//attr1@mair_el3,device memory outer shareable
  15. desc |= UPPER_ATTRS(XN);//no instruction execute
  16. }
  17. debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
  18. debug_print(attr & MT_RW ? "-RW" : "-RO");
  19. debug_print(attr & MT_NS ? "-NS" : "-S");
  20. return desc;
  21. }

代码调用总结如下图

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号