【linux】arm mm内存管理

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

邮箱:tangzhongp@163.comlinux

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

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

 

 

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

 

  arm mmu硬件原理

clip_image001

 

clip_image002

 

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

 

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位能够索引1Msectionui

该数组元素的内容是:this

clip_image004

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

 

转换成程序语言就是:.net

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

相关文章
相关标签/搜索