armv8 mmu 内存页表属性

armv8中的页表项

D5.3 VMSAv8-64 translation table format descriptors

In general, a descriptor is one of: 页表项的4种类型

• An invalid or fault entry. 有效的或者无效的页表项

• A table entry, that points to the next-level translation table. 指向下一级的页表项

• A block entry, that defines the memory properties for the access. 块页表项

• A reserved format. 保留的页表项

Bit[1] of the descriptor indicates the descriptor type, and bit[0] indicates whether the descriptor is valid. bit1表示页表项的类型,bit0表示这个页表项有效还是无效

介绍L0/L1/L2页表项的格式

D5.3.1 VMSAv8-64 translation table level 0, level 1, and level 2 descriptor formats

上图展示了L0/L1/L2页表的三种类型。

Descriptor encodings, Armv8 level 0, level 1, and level 2 formats

Descriptor bit[0] identifies whether the descriptor is valid, and is 1 for a valid descriptor. If a lookup returns an

invalid descriptor, the associated input address is unmapped, and any attempt to access it generates a Translation fault.

Descriptor bit[1] identifies the descriptor type, and is encoded as:

0, Block

The descriptor gives the base address of a block of memory, and the attributes for that memory

region.

1, Table

The descriptor gives the address of the next level of translation table, and for a stage 1 translation,

some attributes for that translation.

The other fields in the valid descriptors are:

Block descriptor

Gives the base address and attributes of a block of memory, as follows:

4KB translation granule

• For a level 1 Block descriptor, bits[47:30] are bits[47:30] of the output address.

This output address specifies a 1GB block of memory.

• For a level 2 descriptor, bits[47:21] are bits[47:21] of the output address.This

output address specifies a 2MB block of memory.

16KB translation granule

For a level 2 Block descriptor, bits[47:25] are bits[47:25] of the output address.This

output address specifies a 32MB block of memory.

64KB translation granule

For a level 1 Block descriptor:

• If ARMv8.2-LPA is implemented, bits[15:12] are bits[51:48] of the output

address and bits[47:42] are bits [47:42] of the output address. This output address

specifies a 4TB block of memory.

• If ARMv8.2-LPA is not implemented, there is no level 1 Block descriptor.

For a level 2 Block descriptor:

• If ARMv8.2-LPA is implemented, bits[15:12] are bits[51:48] of the output

address, and bits[47:29] are bits [47:29] of the output address.This output address

specifies a 512MB block of memory.

• If ARMv8.2-LPA is not implemented, bits[47:29] are bits[47:29] of the output

address.This output address specifies a 512MB block of memory.

In Armv8.0, bits[63:52, 11:2] provide attributes for the target memory block. When

ARMv8.1-TTHM is implemented, bits[63:51, 11:2] provide the attributes for the target memory

block. For more information, see Memory attribute fields in the VMSAv8-64 translation table format

descriptors on page D5-2599.

Note

• In Armv8.0, the position and contents of bits[63:52, 11:2] are identical to bits[63:52, 11:2] in

the Page descriptors.

• When ARMv8.1-TTHM is implemented, the position and contents of bits[63:51, 11:2] are

identical to bits[63:51, 11:2] in the Page descriptors

上诉描述了block类型的页表,在不同粒度下一个block 的位宽以及能覆盖的内存大小

Table descriptor

Gives the translation table address for the next-level lookup, as follows:

4KB translation granule

  • Bits[47:12] are bits[47:12] of the address of the required next-level table, which is:

For a level 0 Table descriptor, the address of a level 1 table.

For a level 1 Table descriptor, the address of a level 2 table.

For a level 2 Table descriptor, the address of a level 3 table.

  • Bits[11:0] of the table address are zero.

16KB translation granule

  • Bits[47:14] are bits[47:14] of the address of the required next-level table, which is:

For a level 0 Table descriptor, the address of a level 1 table.

For a level 1 Table descriptor, the address of a level 2 table.

For a level 2 Table descriptor, the address of a level 3 table.

• Bits[13:0] of the table address are zero.

64KB translation granule

  •  Bits[47:16] are bits[47:16] of the address of the required next-level table, which is:

For a level 1 Table descriptor, the address of a level 2 table.

For a level 2 Table descriptor, the address of a level 3 table.

When ARMv8.2-LPA is implemented, bits[15:12] are bits[51:48] of the required

next-level table.

  • Bits[15:0] of the table address are zero.

For a stage 1 translation only, bits[63:59] provide attributes for the next-level lookup, see Memory

attribute fields in the VMSAv8-64 translation table format descriptors on page D5-2599.

If the translation table defines either the Secure or Non-secure EL1&0, when EL2 is enabled, stage 1 translations,

then the output address in the descriptor is the IPA of the target block or table. Otherwise, it is the PA of the target

block or table.

table类型的页表,在不同粒度下一个table类型指向下一级页表的地址位宽。

L3的页表格式

level 3就是linux内核中的PTE(page table entry)页表

D5.3.2 Armv8 translation table level 3 descriptor formats

 

从上图可以看出armv8架构支持5种 L3页表格式

Descriptor bit[0] identifies whether the descriptor is valid, and is 1 for a valid descriptor. If a lookup returns an

invalid descriptor, the associated input address is unmapped, and any attempt to access it generates a Translation

fault.

Descriptor bit[1] identifies the descriptor type, and is encoded as:

  • 0, Reserved, invalid

    • Behaves identically to encodings with bit[0] set to 0.

    • This encoding must not be used in level 3 translation tables.

  • 1, Page          Gives the address and attributes of a 4KB, 16KB, or 64KB page of memory.

At this level, the only valid format is the Page descriptor. The other fields in the Page descriptor are:.....

内存属性

D5.3.3 Memory attribute fields in the VMSAv8-64 translation table format descriptors

下一级页表的属性

Next-level attributes in stage 1 VMSAv8-64 Table descriptors

In a Table descriptor for a stage 1 translation, bits[63:59] of the descriptor define the attributes for the next-level

translation table access, and bits[58:51] are IGNORED:

These attributes are:(2599页)

name

describe

描述

NSTable, bit[63]

For memory accesses from Secure state, specifies the Security state for subsequent levels of lookup,

see Hierarchical control of Secure or Non-secure memory accesses on page D5-2605.

For memory accesses from Non-secure state, including all accesses in the EL2 or EL2&0 translation

regime, this bit is RES0 and is ignored by the PE.

APTable, bits[62:61]

Access permissions limit for subsequent levels of lookup, see Hierarchical control of data access

permissions on page D5-2611.

APTable[0] is RES0:

• In the EL2 translation regime.

• In the EL3 translation regime.

From Armv8.1, when ARMv8.1-HPD is implemented, this field can be disabled. When the value

of TCR_ELx.HPD{0} or TCR_ELx.HPD1 is 1:

• The value of the corresponding APTable field is IGNORED by hardware, allowing the field to

be used by software.

The behavior of the system is as if the value of the corresponding APTable field is 0.

UXNTable or XNTable, bit[60]

XN limit for subsequent levels of lookup, see Hierarchical control of instruction fetching on

page D5-2616.

The naming of this field depends on whether stage 1 of the translation regime can support two VA

ranges:

Stage 1 can support two VA ranges

This field is UXNTable, and determines whether execution at EL0 of instructions

fetched from the region identified at a lower level of lookup permitted.

Note

PXNTable is the equivalent control of execution at a higher Exception level.

Stage 1 supports only one VA range

This field is XNTable.

From Armv8.1, when ARMv8.1-HPD is implemented, this field can be disabled. When the value

of TCR_ELx.HPD{0} or TCR_ELx.HPD1 is 1:

• The value of the corresponding UXNTable field is IGNORED by hardware, allowing the field

to be used by software.

• The behavior of the system is as if the value of the corresponding UXNTable field is 0.

Note

From Armv8.3, if EL2 is enabled in the current Security state, in the EL1 translation regime, when

the value of HCR_EL2.{NV, NV1} == {1, 1}, bit[60] holds PXNTable, see Additional behaviors

when HCR_EL2.NV == 1 and HCR_EL2.NV1 == 1 on page D5-2645.

PXNTable, bit[59]

PXN limit for subsequent levels of lookup, see Hierarchical control of instruction fetching on

page D5-2616.

This field is valid only for a stage 1 translation that can support two VA ranges. It is RES0 for stage 1

translations that can support only one VA range.

From Armv8.1, when ARMv8.1-HPD is implemented, this field can be disabled. When the value

of TCR_ELx.HPD{0} or TCR_EL1.HPD1 is 1:

• The value of the corresponding PXNTable field is IGNORED by hardware, allowing the field

to be used by software.

• The behavior of the system is as if the value of the corresponding PXNTable field is 0.

Note

From Armv8.3, if EL2 is enabled in the current Security state, in the EL1&0 translation regime,

when the value of HCR_EL2.{NV, NV1} == {1, 1}, bit[59] is RES0, see Additional behaviors when

HCR_EL2.NV == 1 and HCR_EL2.NV1 == 1 on page D5-2645.

block或page的页表项的属性

分成高位和低位属性

Attribute fields in stage 1 VMSAv8-64 Block and Page descriptors

In Block and Page descriptors, the memory attributes are split into an upper block and a lower block, as shown for a stage 1 translation:

Attribute fields for VMSAv8-64 stage 1 Block and Page descriptors

名称

比特位

描述

AttrIndx[2:0]

Bit[4:2]

MAIR_ELn寄存器用来表示内存的属性,比如设备内存(Device Memory)、普通内存等。软件可以设置8个不同内存属性。常见的内存属性有:

  • 0:DEVICE_nGnRnE

  • 1:DEVICE_nGnRE

  • 2:DEVICE_GRE

  • 3:NORMAL_NC

  • 4:NORMAL

  • 5:NORMAL_WT

AttrIndex用来索引不同的内存属性

NS

Bit[5]

非安全比特位(Non-secure)。当处于安全模式时用来指定访问的内存地址是安全映射的还是非安全映射的

AP[2:1]

Bit[7:6]

数据访问权限比特位。

AP[1]:表示该内存允许用户权限(EL0)还是更高权限的特权异常等级(EL1)来访问。在Linux内核中使用PTE_USER宏来表示可以在用户态访问该页面。

  • 1:表示可以被EL0以及更高特权的异常等级访问

  • 0:表示不能被EL0访问,但是可以被EL1访问。

AP[2]: 只读权限还是可读可写权限。在Linux内核中使用PTE_RDONLY宏来表示该比特位。

  • 1:表示只读

  • 0:表示可读可写

SH

Bit[9:8]

内存缓存共享属性。在Linux内核中使用PTE_SHARED宏来表示该比特位。

  • 00:没有共享

  • 01:保留

  • 10:Outer Shareable

  • 11:Inner Shareable

AF

Bit[10]

访问比特位(access flag)。Linux内核使用PTE_AF宏来表示该比特位。

当第一次访问页面时硬件会自动设置这个访问比特位。

nG

Bit[11]

非全局比特位(the not global bit)。Linux内核使用PTE_NG宏来表示该比特位。

这个比特位用于TLB管理。TLB的表项分成全局的和进程持有的。当设置该比特位时表示这个页面对应的TLB表项是进程持有的。

nT

Bit[16]

块类型(Block)的页表项(block translation entry)

DBM

Bit[51]

脏比特位(Dirty Bit Modifier)。Linux内核使用PTE_DBM宏来表示该比特位

Contiguous

Bit[52]

表示当前页表项处在一个连续物理页面集合中,可以使用一个单一的TLB表项进行优化。

Linux内核使用PTE_CONT宏来表示该比特位。

PXN

Bit[53]

The Privileged execute-never field,表示该页面在特权模式下不能执行。

Linux内核使用PTE_PXN宏来表示该比特位。

XN/UXN

Bit[54]

The Execute-never or Unprivileged execute-never field. XN表示该页面在任何模式下都不能执行。UXN表示该页面在用户模式下不能执行。linux内核使用PTE_UXN宏来表示

PBHA

Bit[62:59]

Page-based Hardware Attributes bits页面相关的硬件属性。

<think>嗯,用户之前问了关于内核空间能否访问用户空间地址的问题,现在又转向了ARMv8架构中的普通内存和设备内存。我需要先确定用户的具体需求是什么。可能用户在进行底层开发,或者是在学习操作系统、驱动开发相关的知识。他们可能想知道在ARMv8架构下这两种内存类型的区别,以及如何正确使用它们。 首先,我得回忆一下ARMv8内存类型分类。ARMv8内存分为普通内存(Normal Memory)和设备内存(Device Memory)。普通内存用于常规的数据和代码存储,而设备内存用于与外设寄存器通信。这两者在访问属性、顺序性、副作用等方面有很大不同。 用户可能想知道为什么要有这样的区分,以及在实际编程中需要注意什么。比如,访问设备内存时可能需要使用特定的内存屏障,或者避免编译器优化,使用volatile关键字。另外,设备内存的访问属性(如nGnRnE、nGnRE、GRE等)对程序行为的影响也是关键点。 接下来要考虑用户的背景。他们可能是嵌入式开发者,或是学习操作系统的学生。如果是开发者,可能需要具体的代码示例和实际应用中的注意事项;如果是学生,可能需要更基础的概念解释。但根据之前的提问,用户似乎对内核和硬件机制有较深兴趣,因此需要详细的技术细节。 用户可能没有直接问到的,但潜在需要了解的是内存类型在缓存策略上的差异。比如,普通内存通常会被缓存,而设备内存通常不被缓存,这会影响性能和一致性。此外,内存屏障的使用在不同内存类型中的必要性也需要解释清楚。 需要确保回答结构清晰,分点说明两者的区别、属性、访问方式及实际应用。同时,要使用中文,避免专业术语过于密集,必要时举例说明。比如,在访问设备内存时,使用volatile指针防止编译器优化,或者使用DSB指令来确保内存操作的顺序性。 另外,用户可能对ARMv8内存属性寄存器(MAIR)配置感兴趣,但这个问题可能超出当前问题的范围,可以适当提及但不深入。重点是普通内存和设备内存的核心区别和实际影响。 最后,总结部分要强调正确使用这两种内存的重要性,避免因错误配置导致的问题,如数据损坏、设备故障或性能下降。确保用户理解为什么需要区分这两种内存类型,以及如何在开发中正确应用这些知识。</think>### **ARMv8 普通内存与设备内存详解** 在 ARMv8 架构中,内存根据用途和访问特性被划分为 **普通内存(Normal Memory)** 和 **设备内存(Device Memory)**。这一分类直接影响 CPU 对内存的访问行为、缓存策略和内存屏障要求。以下是两者的核心区别和实际应用解析: --- ### **1. 普通内存(Normal Memory)** #### **(1) 定义与用途** - **用途**:存储程序代码、堆栈、动态数据等常规内容。 - **特点**:允许 **推测访问**(Speculative Access)和 **乱序执行**(Out-of-Order Execution),支持硬件缓存。 #### **(2) 关键属性** - **访问顺序性**: 默认允许 CPU 和编译器对内存操作进行重排序(除非通过内存屏障显式限制)。 - **缓存策略**: 通常配置为 **Write-Back Cacheable**(写回缓存),通过 L1/L2 缓存加速访问。 - **副作用**: 访问普通内存不会触发硬件外设行为(如修改寄存器状态)。 #### **(3) 示例场景** - 读写全局变量、分配堆内存、加载可执行代码。 --- ### **2. 设备内存(Device Memory)** #### **(1) 定义与用途** - **用途**:映射硬件外设寄存器(如 GPIO、UART、DMA 控制器等),用于与设备交互。 - **特点**:访问可能伴随 **硬件副作用**(如触发中断、修改设备状态),通常 **禁止缓存**。 #### **(2) 关键属性** - **访问顺序性**: 严格遵循程序顺序(No Reordering),每次访问必须按代码顺序执行。 - **缓存策略**: 强制配置为 **Non-Cacheable**(不可缓存),直接操作物理内存。 - **副作用**: 读写操作可能直接改变外设状态(如向 UART 数据寄存器写入一个字节会触发串口发送)。 #### **(3) 子类型(ARMv8 内存属性)** 设备内存进一步分为三种类型,通过 `MAIR_ELx`(Memory Attribute Indirection Register)配置: 1. **Device-nGnRnE**(严格设备内存) - 禁止重排序(No reordering)、禁止合并(No gathering)、**错误提前报告**(Early Write Acknowledgement)。 - 用于关键外设(如中断控制器),确保每次访问原子性。 2. **Device-nGnRE** - 禁止重排序和合并,但允许错误延迟报告。 - 常见于普通外设寄存器(如 GPIO)。 3. **Device-GRE** - 允许有限重排序(Gather-Reorder- Early Ack),适用于高吞吐设备(如 DMA 缓冲区)。 --- ### **3. 核心区别对比** | **特性** | **普通内存** | **设备内存** | |-----------------------|---------------------------|---------------------------| | **访问顺序性** | 允许乱序/推测执行 | 严格按程序顺序执行 | | **缓存策略** | 通常可缓存(Write-Back) | 不可缓存 | | **硬件副作用** | 无 | 可能触发设备行为 | | **内存屏障要求** | 需显式屏障控制多核一致性 | 每次访问隐式保序 | | **典型应用** | 代码、数据 | 外设寄存器、MMIO 区域 | --- ### **4. 编程实践与注意事项** #### **(1) 访问设备内存的代码规范** - **使用 `volatile` 关键字**:防止编译器优化(如消除“冗余”读写): ```c volatile uint32_t *reg = (volatile uint32_t *)0x1C010000; *reg = 0x55; // 确保写入实际发生 ``` - **避免缓存干扰**:通过 MMU配置设备内存为 `Non-Cacheable`。 - **显式内存屏障**:即使设备内存隐式保序,跨普通/设备内存混合访问时仍需屏障: ```c // 写入设备寄存器后,确保后续普通内存操作不重排到前面 *device_reg = cmd; DSB(); // 数据同步屏障 ``` #### **(2) 配置示例(ARMv8 MMU)** - **普通内存属性**: ```c // MAIR_EL1 索引 0:Normal Write-Back Cacheable mair = MAIR_ATTR(0, MT_NORMAL_WB); ``` - **设备内存属性**: ```c // MAIR_EL1 索引 1:Device-nGnRE mair |= MAIR_ATTR(1, MT_DEVICE_nGnRE); ``` - **页项设置**: ```c // 映射外设寄存器区域(0x1C000000~0x1C0FFFFF)为设备内存 set_pte(addr, attr_index=1, AP=RW, S2AP=Unprivileged); ``` --- ### **5. 常见问题与调试** #### **(1) 错误配置的后果** - **普通内存误设为设备内存**: 导致缓存不一致(如 DMA 直接修改内存后,CPU 读取缓存旧值)。 - **设备内存误设为普通内存**: 缓存导致外设寄存器读写丢失(如写入被缓存,未实际到达设备)。 #### **(2) 调试技巧** - **检查页属性**:通过 `MMU 转储工具` 确认内存类型和缓存配置。 - **使用逻辑分析仪**:捕获总线信号,验证设备寄存器的实际读写时序。 - **内核日志**:关注 `Alignment Fault` 或 `Permission Fault` 异常(可能因属性不匹配触发)。 --- ### **6. 总结** - **普通内存**:用于常规数据,支持缓存和乱序访问,依赖显式屏障保证一致性。 - **设备内存**:用于外设交互,不可缓存且严格保序,需通过 `volatile` 和屏障确保可靠性。 - **设计原则**:通过 MMU 正确配置内存类型,是避免硬件异常、数据竞争的关键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byd yes

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值