ver 0.3

前言
本来是要写虚拟化系列的文章,而虚拟化技术又是和硬件密切相关的,于是就依托ARM体系架构相继写了几篇虚拟化基础性的文章。等写到内存的时候,发现内存又太过重要,不是写一篇综述级别的文章就能讲清楚的,但是在检讨内存管理的原理时,又觉得Cache太过重要,不写清楚Cache,总觉得内存的事情讲不透,于是就先写了Cache相关系列文章。现在Cache系列的文章暂时告一段落,我们可以开始进入内存的世界,也就离我们码农更近了一些,离CPU远了一些,不过没关系,大家还是要密切配合才能工作,码农平时写得代码以及代码中操作的数据还是要经过内存传递给CPU,由CPU自己处理或者统一调配给总线上的其他功能单元或者外设继续处理,然后实现我们的业务需求,比如可以看到高清画质的大片。基于以上的背景,我们研究内存管理的基本原理就显得更加有必要,可以帮助我们在研究内核的源码中一些核心的数据结构、用户空间内存优化、以及性能调优时候提供理论和方法论的依据。本篇我们先来介绍,ARM体系内存管理的核心单元MMU,也算是我们ARM内存系列文章的绪论。
正文
前面我们介绍了ARM Cache子系统的相关的知识,我们了解到Cache因为特殊的电路结构,作为PE-Core执行单元最亲近的存储组织,通过备份主存中的数据,可以极大提高CPU的工作效率。Cache的结构和容量决定了其在工作的时候也涉及到Cache内部寻址的过程,也可以说是一个映射的过程,就是说通过主存的虚拟地址如何找到主存副本所在的Cache Line,主要有3种方式:VIVT、VIPT、PIPT。简单归纳一下,串联主存、CPU、Cache的线索就是虚拟地址,而在背后驱动这个虚拟地址的功能单元其实就是MMU,内存管理单元。从总线架构的视角看过去,MMU所在位置如图1-1所示。

通过图1-1,ARM体系下每一个PE-Core(Master-1中的功能单元)都有一个MMU,负责处理与内存有关的事物。当然,有些情况也要做一下说明:
(1) MMU在ARM体系下是一个相对独立的功能单元,可以通过配置禁止使用,那么此时MMU的功能就需要通过编程的方式让CPU吸收,此时CPU的性能影响是非常大的。
(2) MMU主要是和CPU以及Cache打交道,但是如果处理的内存被配置为不可Cache,那么此时MMU也会配合PE-Core绕过Cache直接操作内存(Master 5中的功能单元)。
(3) 内存管理的行文中,如果不做特殊说明,那么“内存”、“外存”、“主存”都是同样含义,专指图1-1 Master-5中的存储单元。
1. MMU
1.1 MMU Overview
首先,我们看一下ARM手册中中对MMU的描述:
Memory Management Unit (MMU)
Provides detailed control of the part of a memory system that provides a single stage of address translation. Most of the control is provided using translation tables that are held in memory, and define the attributes of different regions of the physical memory map.
When memory is accessed, the Memory Management Unit (MMU) controls address translation, memory access permissions, memory attribute determination, and memory attribute checking.
The MMU uses the most significant bits of the Virtual Address to index entries in a translation table and establish which block is being accessed. The MMU translates the Virtual Addresses of code and data to the Physical Addresses in the actual system. The translation is carried out automatically in hardware and is transparent to the application. In addition to address translation, the MMU controls memory access permissions, memory ordering, and cache policies for each region of memory.
Armv8-A uses a Virtual Memory system where the addresses used by code (virtual addresses) are translated into physical addresses which are used by the memory system. This translation is performed by


1470

被折叠的 条评论
为什么被折叠?



