【linux】arm mm内存管理

欢迎转载,转载时请保留作者信息,谢谢。

邮箱:tangzhongp@163.com

博客园地址:http://www.cnblogs.com/embedded-tzp

Csdn博客地址:http://blog.csdn.net/xiayulewa

 

 

这篇文章写的很好:Linux中的内存管理   http://blog.chinaunix.net/uid-26611383-id-3761754.html

 

  arm mmu硬件原理

clip_image001

 

clip_image002

 

由上图,arm分四种模式,section,大小页+ 极小页,  section模式简单,也能说明mmu本质,其它模式只是用了多级数组索引而已,本质是一样的,详细可以阅读arm920t的技术文档,此处仅说section模式。

 

clip_image003

 

 

虚拟地址到物理地址转换是由arm硬件自动管理的,上面这图的Translation table是存在内存中的,是一个数组,数组的首地址即TTB(Translation table base), 其值被赋值给协处理器CP15Register 2, translation table base (TTB) register, 该数组是被Indexed by modified virtual address bits [31:20],共12位索引,数组元素个数有2 ^ 12 = 4096个,每个MVA12位可以索引1Msection

该数组元素的内容是:

clip_image004

因此程序要做的工作主要为在内存中申请该数组(全局,栈,堆都行),然后将该数组赋值,再将该数组首地址给CP15TTB寄存器,内存管理从宏观来说就是这么简单。

 

转换成程序语言就是:

static __global unsigned long  mmu_translation_table[4096] = {}; // 2^12

 

CP15.R2 = &mmu_translation_table[0]; //TTB 

 

由此可见,mmu_translation_table会额外占用4096 * 4(sizeof(unsigned long)) = 16KB的空间。

 

mmu_translation_table[0]mmu_translation_table[1]...等又该如何初始化和赋值呢?

 

clip_image004[1]

 

上图的Section base本质上是物理地址,占31:20位。

可以定义成一个结构体, 假设为小端:

typedef  struct {

    u32  pfn : 12; // Section base address, page frame number

    u32  reserve0: 8;

    u32  AP: 2; // access permissions

    u32  reserve1: 1;

    u32  domain: 4; // Specify one of the 16 possible domains (held in the domain accesscontrol register) that contain the primary access controls

    u32  reserve2: 1;

    u32  C: 1; 

    u32  B: 1; // These bits (C and B) indicatewhether the area of memory mapped by this section is treated as write-back cachable, write-through cachable, noncached buffered, or noncached nonbuffered

    u32  des_type: 2; //section,大小页+ 极小页

} mmu_translation_table_element_t;

 

 

根据上述定义,可以重新定义mmu table

static __global mmu_translation_table_element_t  mmu_translation_table[4096] = {0}; // 2^12

CP15.R2TTB = &mmu_translation_table[0]; (自然语言)

 

公式

综上所述,物理地址与虚拟地址的关系明确为:

 

mmu_translation_table[VA >> 20] = PA & ((~0) << 20) + 权限控制位(共20位)

 

可见,mmu_translation_table12位由物理地址的高12位组成,低20位为该物理段的权限控制。

 

PA =  mmu_translation_table[VA >> 20] & ((~0) << 20) + VA & (1 << 20 - 1);

 

上述公式的解释为:虚拟地址的高12位作为下标去索引mmu_translation_table,索引到的内容只取高12位,得到了物理地址的高位地址,再与虚拟地址的低20位组合,则获得了真实的物理地址。

 

  linux mmu

 

内核地址空间划分:

 

clip_image005

 

src\arch\arm\kernel\vmlinux.lds中,定义 . = 0xC0000000 + 0x00008000, 因此linux kernel 代码的虚拟地址从0xC0008000处开始。

而在u-boot加载内核代码时,有tftp 0x30008000 uImage,因此linux kernel的物理地址从0x30008000开始。

 

PA(0x30008000) ↔ VA(0xC0008000)

 

clip_image007

 

posted on 2015-04-22 15:32  embedded_linux  阅读(1208)  评论(0编辑  收藏  举报