Armv8-A Memory management

本文介绍Armv8-A的内存管理。内存管理指的是在系统中,内存访问是如何实现的。程序员

使用内存管理机制,可让每一个应用之间的内存地址分离,即sandbox application,也可让多个在物理内存上碎片化的地址造成虚拟地址空间一个连续的地址,同时可让程序员编程更为方便。编程

image-20200802152342377

虚拟地址到物理地址的转换经过mapping的方式来进行,其关键为Translation tables,存储在memory中,而且被OS或者hypervisor来管理。缓存

Memory Management Unit(MMU)

虚拟地址到物理地址的translation由MMU来执行,其包含两个部分:架构

  1. The table walk unit, which contains logic that reads the translation tables from memory.
  2. Translation Lookaside Buffers (TLBs), which cache recently used translations ,是被映射的地址,而不是地址对应的内容。

当一个虚拟地址来临的时候,MMU先是查找TLBs,看是否有cached translation,若是没有,那么table walk unit就memory中读取对应的table entry或者table entries(大块内存可能就须要多个entry)app

image-20200802154658528

Table entry

translation tabls经过把整个虚拟地址空间划分为多个大小相同的block/page,而后每一个page对应一个entry。less

image-20200802164144863

Table lookup

对于一个单一层级的查找来讲,当对某个虚拟地址来查找table的时候,会把虚拟地址分城两部分,高位的用来定位对应的entry,而低位的则用来表示该地址在查找到的该block中的偏置大小。ide

image-20200802162320897

Multilevel translation

在实践使用中,通常使用的是分级table。每一级的访问都像前面介绍的table lookup同样访问。性能

在armv8中,最多能够分为4级,每级分别被标记为0-3. 这种分级方案支持larger blocks和 smaller blocks:spa

  • larger blocks:相比smaller blocks,它只要更少层级的读取翻译就能够获得对应的物理地址。同时,在TLB中的缓存效率也更高
  • small block:能够提供对内存分配fine-grain的控制。可是在TLB中的缓存效率较低,由于须要更多层级的读取翻译才能够获得物理地址

image-20200802171131536

OS必须作好large block和small block之间的平衡,以达到效率和物理内存使用灵活上的平衡,从而达到最优的性能。Note:在这种状况下,处理器并不知道这次翻译对应的block大小,它须要经过table walk的方式来获得。操作系统

Address spaces in Armv8-A

image-20200802190815094

在arm中,同时存在几个独立的虚拟地址空间,好比OS的、Hypervisor的以及Secore Monitor的。每一个虚拟地址空间都有他们本身的setting和tables。从这个图上咱们能够看到,OS的虚拟地址实际上是通过了两层translation的,先是由OS翻译成IPAs,而后再又hypervisor翻译成真正的物理地址,这两次翻译虽然在表的格式方面可能有所差别,可是大致是基本是一致的。以下图所示:

image-20200802192245574

Address sizes

虽然Armv8-A是64位的架构,可是并非意味着全部的地址都是64位的

Size of virtual addresses

image-20200802193015118

从这个图也能够看出,对于EL0/EL1,kernel space和user space的地址空间是分开的,kernel的位于高地址,user位于低地址。kernel space和user space都有各自的translation table。TCR_ELx registers中表示的TnSZ值用来控制虚拟地址空间:

image-20200802200714262

全部的armv8-a架构都支持48位的虚拟地址,52位的是可选支持。好比上图的中的0x0000_0000_0000_0000~0x000F_FFFF_FFFF_FFFF表示的就是一个52位的地址空间

Size of physical address

size of物理地址是有实现决定的,最大支持52bits。若是你在table entry中指定一个

Adress Space Identifiers - Tagging translations with the owning process

对于现代操做系统,多个同时运行的进程看起来都是有着相同的虚拟地址的。那么从MMU的角度来说,它怎么区分一个相同的虚拟地址是来自哪一个进程呢?同时,理想化的来说,咱们但愿不一样的进程所对应的TLB是互不冲突的,这就不会致使TLB的的刷新以及上下文切换。解决这个问题的手段就是使用Address Space Identifiers (ASIDs)

对于EL0/EL1,即OS,的虚拟地址,translations table entry的属性字段中的nG位用来标记global或者non-global。kernel对应的是global,也即意味着对于全部的进程都是共用的,而non-global的ranslation则与对应的进程相关。对于non-global的,则匹配TLB中的ASID和当前translations提供的ASID,会选择匹配了的来进行处理。

image-20200802204452682

Virtual Machine Identifiers - Tagging translations with the owning VM

对于不一样的VM,和ASID同样,使用Virtual Machine Identifier (VMID) 来进行tag。这里讲的VM就是EL0 EL1 EL2 EL3这些。

Common not Private

有这样一个问题,如今CPU都是多核的,同一个ASID和VMID在不一样的process上有相同的含义吗?

对Armv8.0-A 版本,答案是否认的。没有要求对于多个processor之间要求ASID和VMID含义一致,即相同的ASID在不一样的processor可能表示不一样的进程。也即意味着,一个processor建立的TLB不能被另外一个processor使用。

可是,在实际中,咱们更倾向于他们是跨processor通用的,所以从Armv8.2-A 开始引入了Common not Private (CnP)bit in the Translation Table Base Register (TTBR) ,若是CnP位被set,那么就意味着能够通用。

Controlling address translation

Translation table format

image-20200802210800346

为何对于level3没有Next-level Table Address?由于前面也提到过,最多允许四级地址。也即3就是最后一级了,必须输出硬件地址了,即Output Block Address

为何level0没有Output Block Address?由于level0表示的范围太大了,直接让其表示blocks没有意义。

为何第一个和第三个的descriptor是同样的?由于这样允许recursive tables,让他们相互之间能够point back。This is useful because it makes it easy to calculate the virtual address of a particular page table entry so that it can be updated .

Translation granule

translation granule,即翻译粒度,指的是最小可described的block memory大小。Armv8-A 支持4KB, 16KB, and 64KB 。具体是由ID_AA64MMFR0_EL1 所指定。

image-20200802213039482

There are restrictions on using 52-bit addresses. When the selected granule is 4KB or 16KB, the maximum virtual address region size is 48 bits. Similarly, output physical addresses are limited to 48 bits. It is only when the 64KB granule is used that the full 52 bits can be used 。

The starting level of address translation

image-20200802213502181

这张表说明的是,当选择4KB的 granule时,各个entry level所利用的地址bits。假如你设置TCR_ELx.T0SZ为32,那么虚拟地址空间就只有\(64-TOSZ=32\)位,那么对应上表,你就能够发现,已经不须要level0了,直接从level1开始就能够彻底表示整个虚拟地址空间了。而若是虚拟地址空间只有30位,那么只须要从level2开始就能够完整表示整个地址空间了。也即,虚拟地址空间越小,那么所需的level也就越小。

Registers that control address translation

地址翻译是经过以下这些寄存器来配合控制的:

  • SCTLR_ELx
    • M - Enable Memory Management Unit (MMU).
    • C - Enable for data and unified caches
    • EE - Endianness of translation table walks.
  • TTBR0_ELx and TTBR1_ELx
    • BADDR - Physical address (PA) (or intermediate physical address, IPA, for EL0/EL1) of start of translation table.
    • ASID - The Address Space Identifier for Non-Global translations.
  • TCR_ELx
    • PS/IPS - Size of PA or IPA space, the maximum output addresssize.
    • TnSZ - Size of address space covered by table.
    • TGn - Granule size.
    • SH/IRGN/ORGN - Cacheability and shareability to be used by MMU table walks.
    • TBIn - Disabling of table walks to a specific table.
  • MAIR_ELx
    • Attr - Controls the Type and cacheability in Stage 1 tables.

MMU disabled

若是MMU被禁用的话,那么全部地址都是flat-mapped,即输入等于输出。

Translation Lookaside Buffer maintenance

TLB缓存的最近使用的translations,以即可以获得translation结果而不用去读tables。注意:TLB换成的translations,即从虚拟地址到硬件地址的直接映射关系,而不是translation tables。我的认为有三个缘由:一是这样效率最高,直接读结果;二是entry有多个level,换成entry的话,会须要更多的空间;三是entry可能会随着配置寄存器的更改而有不一样的含义,这也会给缓存的重利用形成更多负担。

这些状况下,TLB不会对其进行缓存:

• A translation fault (unmapped address).
• An address size fault (address outside of range).
• An access flag fault.

当你首次mapping一个地址的时候,并不须要issue a TLB invalidate ,而当有下列行为的时候,必须issue a TLB invalidate :

  • Unmap an address .Take an address that was previously valid or mapped and mark it as faulting.
  • Change the mapping of an address. Change the output address or any of the attributes. For example, change an address from read-only to read-write permissions.
  • Change the way the tables are interpreted. This is less common. But, for example, if the granule size was changed, then the interpretation of the tables also changes. Therefore, a TLB invalidate would be necessary.

对应的指令为TLBI <type><level>{IS|OS} {, <xt>}

Address translation instructions

Address Translation (AT) 能够用来查询特定地址的translation结果,翻译结果和地址属性,保存在Physical Address Register, PAR_EL1 。同时,AT指令能够查询指定regime的结果,好比EL2能够查询某个EL1地址的结果,但不能反过来。由于EL2比EL1拥有更高的特权。

Check your knowledge

  • What is the difference between a stage and a level in address translation?

    stage表示的是从输入到输出的两个阶段。第一个阶段是从虚拟地址VA到中间物理地址IPA,阶段二是从IPA到物理地址PA。只有EL1/EL0的才有两个stage。

    level表示的给定stage中不一样级别的tables,从大范围往小范围级级缩小。

  • What is the maximum size of a physical address?
    这是implementation决定的,最大为52bit

  • Which register field controls the size of the virtual address space?
    TCR_ELx.TnSZ, or VTCR_EL2.T0SZ for Stage 2

  • What is a translation granule, and what are the supported sizes?
    表示的是最小的内存描叙分割单元,支持4,16,64KB三种

  • What does the TLBI ALLE3 do?
    刷新全部EL3的虚拟地址空间对应的TLB entries

  • How are addresses mapped when the MMU is disabled?
    采用flat mapped,即输出等于输入

  • What is an ASID and when does a TLB entry include an ASID?
    ASID表示的当前地址对应的哪一个application。. Non-Global mappings (nG=1)对应的TLBs使用ASID进行标记。

  • MMU和DMA?
    As well as the Memory Management Unit (MMU) in the processor, it is increasingly common to have MMUs for non-processor masters, such as Direct Memory Access (DMA) engines. These are referred to as SMMUs (System MMUs) in Arm systems, and elsewhere as IOMMU.

参考

内容来自https://developer.arm.com/architectures/learn-the-architecture/memory-management

相关文章
相关标签/搜索