【轉(zhuǎn)】mini2440裸機(jī)開啟MMU實(shí)驗(yàn)
.text
.global _start
_start:
??? ldr sp, =4096?????????????????????? @ 設(shè)置棧指針,以下都是C函數(shù),調(diào)用前需要設(shè)好棧
??? bl disable_watch_dog?????????????? @ 關(guān)閉WATCHDOG,否則CPU會(huì)不斷重啟
??? bl memsetup??????????????????????? @ 設(shè)置存儲(chǔ)控制器以使用SDRAM
??? bl copy_2th_to_sdram?????????????? @ 將第二部分代碼復(fù)制到SDRAM
??? bl create_page_table?????????????? @ 設(shè)置頁(yè)表
??? bl mmu_init??????????????????????? @ 啟動(dòng)MMU
??? ldr sp, =0xB4000000???????????????? @ 重設(shè)棧指針,指向SDRAM頂端(使用虛擬地址)
??? ldr pc, =0xB0004000???????????????? @ 跳到SDRAM中繼續(xù)執(zhí)行第二部分代碼
halt_loop:
??? b?? halt_loop
這段匯編,可以看出,整一個(gè)設(shè)置流程,其中,設(shè)置頁(yè)表和啟動(dòng)MMU是重點(diǎn)
設(shè)置頁(yè)表
/*
* 設(shè)置頁(yè)表
*/
void create_page_table(void)
{
/*?
* 用于段描述符的一些宏定義
*/?
#define MMU_FULL_ACCESS???? (3 << 10)?? /* 訪問權(quán)限 */
#define MMU_DOMAIN????????? (0 << 5)??? /* 屬于哪個(gè)域 */
#define MMU_SPECIAL???????? (1 << 4)??? /* 必須是1 */
#define MMU_CACHEABLE?????? (1 << 3)??? /* cacheable */
#define MMU_BUFFERABLE????? (1 << 2)??? /* bufferable */
#define MMU_SECTION???????? (2)???????? /* 表示這是段描述符 */
#define MMU_SECDESC???????? (MMU_FULL_ACCESS | MMU_DOMAIN | MMU_SPECIAL |
???????????????????????????? MMU_SECTION)
#define MMU_SECDESC_WB????? (MMU_FULL_ACCESS | MMU_DOMAIN | MMU_SPECIAL |
???????????????????????????? MMU_CACHEABLE | MMU_BUFFERABLE | MMU_SECTION)
#define MMU_SECTION_SIZE??? 0x00100000
??? unsigned long virtuladdr, physicaladdr;
??? unsigned long *mmu_tlb_base = (unsigned long *)0x30000000;
????
??? /*
???? * Steppingstone的起始物理地址為0,第一部分程序的起始運(yùn)行地址也是0,
???? * 為了在開啟MMU后仍能運(yùn)行第一部分的程序,
???? * 將0~1M的虛擬地址映射到同樣的物理地址
???? */
??? virtuladdr = 0;
??? physicaladdr = 0;
??? *(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
??????????????????????????????????????????? MMU_SECDESC_WB;
??? /*
???? * 0x56000000是GPIO寄存器的起始物理地址,
???? * GPBCON和GPBDAT這兩個(gè)寄存器的物理地址0x56000010、0x56000014,
???? * 為了在第二部分程序中能以地址0xA0000010、0xA0000014來(lái)操作GPBCON、GPBDAT,
???? * 把從0xA0000000開始的1M虛擬地址空間映射到從0x56000000開始的1M物理地址空間
???? */
??? virtuladdr = 0xA0000000;
??? physicaladdr = 0x56000000;
??? *(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
??????????????????????????????????????????? MMU_SECDESC;
??? /*
???? * SDRAM的物理地址范圍是0x30000000~0x33FFFFFF,
???? * 將虛擬地址0xB0000000~0xB3FFFFFF映射到物理地址0x30000000~0x33FFFFFF上,
???? * 總共64M,涉及64個(gè)段描述符
???? */
??? virtuladdr = 0xB0000000;
??? physicaladdr = 0x30000000;
??? while (virtuladdr < 0xB4000000)
??? {
??????? *(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
??????????????????????????????????????????????? MMU_SECDESC_WB;
??????? virtuladdr += 0x100000;
??????? physicaladdr += 0x100000;
??? }
}
這里通過(guò) *(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) | MMU_SECDESC_WB;
虛擬地址部分向右移動(dòng)20位,物理地址部分保存在[31:20]中,并設(shè)置相應(yīng)的權(quán)限,巧妙地設(shè)置了頁(yè)表。另外需要注意的是這里 virtuladdr += 0x100000; physicaladdr += 0x100000; 因?yàn)橹皇褂肹31:20]所以增加1 也等于0x100000。
/*
* 啟動(dòng)MMU
*/
void mmu_init(void)
{
??? unsigned long ttb = 0x30000000;
__asm__(
??? "mov??? r0, #0n"
??? "mcr??? p15, 0, r0, c7, c7, 0n"??? /* 使無(wú)效ICaches和DCaches */
????
??? "mcr??? p15, 0, r0, c7, c10, 4n"?? /* drain write buffer on v4 */
??? "mcr??? p15, 0, r0, c8, c7, 0n"??? /* 使無(wú)效指令、數(shù)據(jù)TLB */
????
??? "mov??? r4, %0n"?????????????????? /* r4 = 頁(yè)表基址 */
??? "mcr??? p15, 0, r4, c2, c0, 0n"??? /* 設(shè)置頁(yè)表基址寄存器 */
????
??? "mvn??? r0, #0n"???????????????????
??? "mcr??? p15, 0, r0, c3, c0, 0n"??? /* 域訪問控制寄存器設(shè)為0xFFFFFFFF,
???????????????????????????????????????? * 不進(jìn)行權(quán)限檢查?
???????????????????????????????????????? */????
??? /*?
???? * 對(duì)于控制寄存器,先讀出其值,在這基礎(chǔ)上修改感興趣的位,
???? * 然后再寫入
???? */
??? "mrc??? p15, 0, r0, c1, c0, 0n"??? /* 讀出控制寄存器的值 */
????
??? /* 控制寄存器的低16位含義為:.RVI ..RS B... .CAM
???? * R : 表示換出Cache中的條目時(shí)使用的算法,
???? *???? 0 = Random replacement;1 = Round robin replacement
???? * V : 表示異常向量表所在的位置,
???? *???? 0 = Low addresses = 0x00000000;1 = High addresses = 0xFFFF0000
???? * I : 0 = 關(guān)閉ICaches;1 = 開啟ICaches
???? * R、S : 用來(lái)與頁(yè)表中的描述符一起確定內(nèi)存的訪問權(quán)限
???? * B : 0 = CPU為小字節(jié)序;1 = CPU為大字節(jié)序
???? * C : 0 = 關(guān)閉DCaches;1 = 開啟DCaches
???? * A : 0 = 數(shù)據(jù)訪問時(shí)不進(jìn)行地址對(duì)齊檢查;1 = 數(shù)據(jù)訪問時(shí)進(jìn)行地址對(duì)齊檢查
???? * M : 0 = 關(guān)閉MMU;1 = 開啟MMU
???? */
????
??? /*?
???? * 先清除不需要的位,往下若需要?jiǎng)t重新設(shè)置它們????
???? */
??????????????????????????????????????? /* .RVI ..RS B... .CAM */?
??? "bic??? r0, r0, #0x3000n"????????? /* ..11 .... .... .... 清除V、I位 */
??? "bic??? r0, r0, #0x0300n"????????? /* .... ..11 .... .... 清除R、S位 */
??? "bic??? r0, r0, #0x0087n"????????? /* .... .... 1... .111 清除B/C/A/M */
??? /*
???? * 設(shè)置需要的位
???? */
??? "orr??? r0, r0, #0x0002n"????????? /* .... .... .... ..1. 開啟對(duì)齊檢查 */
??? "orr??? r0, r0, #0x0004n"????????? /* .... .... .... .1.. 開啟DCaches */
??? "orr??? r0, r0, #0x1000n"????????? /* ...1 .... .... .... 開啟ICaches */
??? "orr??? r0, r0, #0x0001n"????????? /* .... .... .... ...1 使能MMU */
????
??? "mcr??? p15, 0, r0, c1, c0, 0n"??? /* 將修改的值寫入控制寄存器 */
??? : /* 無(wú)輸出 */
??? : "r" (ttb) );
}