如何在基于ARMv8A TF-A的裸机环境中启用MMU

问题描述

我最近在运行于TF-A之上的ARMv8A裸机测试环境中的较低EL(EL1 / EL0)中启用了MMU。打开MMU要求创建转换表,该表是高度格式化的数据集,很难从头开始创建。事实证明,TF-A库具有一组功能非常齐全的功能,可简化针对任何翻译制度的这些表的创建并启用MMU。但是我花了一些时间才能达到所需的顺序。因此,我要问这个问题,并用自己的答案跟进。我希望这个问题对于搜索相同信息的人们来说很容易解决,它将使他们迅速朝正确的方向前进,并希望节省一些时间。

这对于希望使用TF-A库创建自定义用途的MMU表的开发人员而言非常有用。对于该主题,有ARM提供的文档非常有用。此答案旨在作为起点,指出执行任务所需的所有概念。

函数和宏名称在将来的TF-A版本中可能会更改,但是我希望以前的概念保持不变。

解决方法

请在下面找到使用TF-A库创建所需的转换表并启用MMU的步骤。

必填概念-

  1. 要记住的主要概念是TF-A库将给定翻译体制的每组翻译表都保存为“上下文”。

  2. 该软件应使用TF-A创建多个上下文。

  3. amp-atf / docs 文件夹的 xlat-tables-lib-v2-design.rst 文档中有关于此主题的文档。 TF-A版本。

  4. amp-atf / lib / xlat_tables_v2 文件夹包含的代码显示了与翻译表相关的库函数的示例用法

  5. 在TF-A中为翻译表相关的动作定义了两种结构- xlat_ctx_t mmap_region_t 。这些实际上是typedef,它们分别根据 struct xlat_ctx struct mmap_region 创建类型(在 amp-atf / include / lib / xlat_tables / xlat_tables_v2_helpers.h中定义 amp-atf / include / lib / xlat_tables / xlat_tables_v2.h )。

  6. struct xlat_ctx 被创建为包含翻译上下文(即任何一种翻译体制)的信息。当然,每个上下文都应包含多个MMU区域映射。创建 struct mmap_region 来包含每个此类区域的信息

  7. 该库具有函数和宏,这些函数和宏抽象了这些结构对象的创建,填充和使用。因此,需要考虑以正确的顺序从库中调用正确的函数来使用这些结构。

  8. 一个名为 tf 的翻译上下文已经被TF-A代码库本身创建和使用。 amp-atf / lib / xlat_tables_v2 文件夹中的代码适用于此上下文。

其他概念-

  1. 静态和动态映射-该库将内存中转换表的创建与核心寄存器的编程分开,以指向这些表。在创建转换表之前,将MMU区域添加到内存的过程称为 static 映射。此外,该库还支持在创建转换表后 将MMU区域添加到内存中。这称为动态映射。

  2. xlat表库有两个版本- xlat_tables xlat_tables_v2 。版本2支持动态映射,并且更加灵活。总体而言,这两个版本目前仍是TF-A代码库的活动部分。

软件流程-(4个步骤)

  1. 在内存中创建新的翻译上下文(新的struct xlat_ctx对象)

    - A macro REGISTER_XLAT_CONTEXT is defined for this
    - arguments - a name for the new context,max. number of mmap regions to be statically allocated for,max. number of xlat tables,virtual address range and physical address range. Usually,these would be defined in the platform-specific include files for the overall project. 
    - Lets assume that the name *name1* is passed to this macro. 
    - A *struct xlat_ctx" object with the name - *name1_xlat_ctx* will be created by this macro
    
  2. 将所需的MMU区域添加到上下文(新的结构mmap_region对象)

    - Create a *struct mmap_region* object out of the basic memory addresses and ranges
    - A macro MAP_REGION is defined for this
    - eg. *mmap_region_t tmp = MAP_REGION(base_pa_addr,base_va_addr,region_size,attributes)*
    - The attributes specify all the important qualities of the region like RO/RW,device/normal etc. There are macros for these values already available in TF-A (inside *amp-atf/include/lib/xlat_tables/xlat_tables_v2.h*). Multiple attributes can be ORed together and passed as the argument
    - After the region is created,it is to be added to the translation context through the *mmap_add_region_ctx* function
    - eg. mmap_add_region_ctx(&name1_xlat_ctx,&tmp);
    - Multiple calls to MAP_REGION and mmap_add_region_ctx would be required in general to map all the required regions.
    
  3. 确保已设置新上下文的翻译体制

    - this is stored in the variable *xlat_regime* of the xlat_ctx_t object
    - there are macros already defined for all the regimes
    - eg. name1_xlat_ctx.xlat_regime = EL1_EL0_REGIME;
    
  4. 使用以上信息,并在另一内存区域中以所需格式创建转换表

    - The function init_xlat_tables_ctx() is available for this
    eg. init_xlat_tables_ctx(&name1_xlat_ctx)
    
  5. 执行TLB无效并编程核心寄存器(MAIR0 / 1,TTBR0 / 1_ELx,SCTLR_ELx等)以指向这些新创建的转换表并打开MMU

    - There are different functions for each EL available in the library for this - 
    - *enable_mmu_el3(attributes),enable_mmu_el2(attributes),enable_mmu_el1(attributes)*
    - The attributes here refer to the attributes of the newly created translation table - where it is placed in memory and whether it is shareable across multiple PEs in a multicore system. 
    - eg. enable_mmu_el1(XLAT_TABLE_NSH);
    

对!通过上述步骤,应该可以在MMU打开的情况下开始执行代码并在所需的EL中执行数据。

我希望这个答案能为那些想使用TF-A库创建新的翻译表以供其自定义使用的人们提供一定的先机。请让我知道是否可以将其他信息添加到此答案中,以使其更有用。

谢谢!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...