三、MMU使用实例:地址映射
开启MMU,并初始化MMU,并将NAND Flash的初始4K处的虚拟地址0x00000000映射到物理地址0x00000000到处。以方便初始化代码运行。
再将虚拟地址0xA0000000映射到物理地址0x56000000(GPBCON的开始地址),当操作0xA0000000时
效果等同于操作0x56000000。实例中通过其来操作LED。
最后将虚拟地址0xB0000000-0xB3FFFFFF映射到物理地址0x30000000-0x33FFFFFF,即将物理地址0x30000000-0x33FFFFFF处的内存映射。在连接程序时,将二部分代码的运行地址指定为0xB0004000,即在内存
中运行。
这 个程序只使用一级页表,以段的方式进行地址映射,32位CPU虚拟地址空间达到4G,一级页表使用4096个描述符来表示4G空间(每个描述符对应 1MB),每个描述符占4字节,所以一级页表占16KB。这个程序使用SDRAM的开始16KB存放一级页表,所以剩下的内存开始地址就为 0x30004000,这个地址最终会对应虚拟地址0xB0004000(所以代码运行地址为0xB0004000)。
程序分为两部分:第一部分的实时地址和运行地址都为0,用来初始化SDRAM,然后复制第二部分的代码到SDRAM中(存放在虚拟地址0xB0004000处即物理地址 0x30004000处)、设置页表、启动MMU,最后跳到SDRAM中的地址0xB0004000处运行 led_on。
四、源代码分析
mem.lds//代码段分布
- SECTIONS{
- first 0x00000000 : {start.o mmu.o}
- second 0xB0004000 : AT(2048){ led_on.o}
- }
start.S
- .equ MEM_CTL_BASE, 0X48000000
- .equ SDRAM_BASE, 0x30000000
- .text
- .global _start
- _start:
- bl disable_watch_dog
- bl memsetup
- bl copy_code_to_sdram
- ldr sp,=4096
- bl init_MMU_tlb
- bl mmu_init
- ldr sp,=0xB4000000
- ldr pc,=0xB0004000
- halt_loop:
- b halt_loop
- /*
- *turn off watchdog
- */
- disable_watch_dog:
- mov r1, #0x53000000
- mov r2, #0x0
- str r2, [r1]
- mov pc, lr
- copy_code_to_sdram:
- mov r1,#2048
- ldr r2,=SDRAM_BASE+0x4000
- mov r3,#4*1024
- 1:
- ldr r4,[r1],#4
- str r4,[r2],#4
- cmp r1,r3
- bne 1b
- mov pc,lr
-
- memsetup:
- /* memory control configuration */
- /* make r0 relative the current location so that it */
- /* reads SMRDATA out of FLASH rather than memory ! */
- mov r1,#MEM_CTL_BASE
- adr r2,mem_cfg_val
- add r3,r1,#52
- 1:
- ldr r4, [r2], #4
- str r4, [r1], #4
- cmp r1, r3
- bne 1b
- mov pc,lr
- .align 4
- /* the literal pools origin */
- mem_cfg_val:
- .long 0x22011110
- .long 0x00000700
- .long 0x00000700
- .long 0x00000700
- .long 0x00000700
- .long 0x00000700
- .long 0x00000700 @BANK5
- .long 0x00018005
- .long 0x00018005
- .long 0x00ac07a3 @REFRESH
- .long 0x000000B1
- .long 0x00000030
- .long 0x00000030
mmu.c
- #define MEM_START 0x30000000
- #define MEM_END 0x34000000
- #define PAGE_SIZE 0x00100000 //页表 MVA[31:20] 1M
- #define MMU_TBL_BASE ( unsigned long *) 0x30000000 //Level 1 page table start address, total 0x800 = 2^12
- /*
- * 用于段描述符的一些宏定义
- */
- #define MMU_FULL_ACCESS (3 << 10) /* 访问权限 */
- #define MMU_DOMAIN (0 << 5) /* 属于哪个域 */
- #define MMU_SPECIAL (1 << 4) /* 必须是1 */
- #define MMU_CACHEABLE (1 << 3) /* cacheable */
- #define MMU_BUFFERABLE (1 << 2) /* bufferable 寄存器地址映射一般不要buffer */
- #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)
- /*
- *init MMU page table
- */
- void init_MMU_tlb(void)
- {
- unsigned long vm_addr,phy_addr;
- unsigned long *tb_base = MMU_TBL_BASE;
- /*the physical address of start code is 0 ,runing address of program also is 0. so virtual address is 0, if you also want to tun program in sdram. */
- vm_addr = 0;
- phy_addr = 0;
- *(tb_base+(vm_addr>>20)) = (phy_addr & 0xFFF00000) | MMU_SECDESC_WB;
-
- /* GPIO0x56000000 map into 0xA0000000. so you can operate 0xA00000000 as GPIO0x56000000 */
- vm_addr = 0xA0000000;
- phy_addr = 0x56000000;
- *(tb_base+(vm_addr>>20)) = (phy_addr & 0xFFF00000) | MMU_SECDESC;
- /* SDRAM map into 0xB0000000. so you can operate 0xB00000000 as SDRAM */
- vm_addr = 0xB0000000;
- for (phy_addr = MEM_START; phy_addr< MEM_END; phy_addr+=PAGE_SIZE)
- {
- *(tb_base+(vm_addr>>20)) = (phy_addr & 0xFFF00000) | MMU_SECDESC_WB;
- vm_addr +=PAGE_SIZE;
- }
-
-
- }
- /*
- * 启动MMU
- */
- void mmu_init(void)
- {
- unsigned long ttb = 0x30000000;//物理内存起始地址,页表存放在内存开始处共16K
- __asm__(
- "mov r0, #0\n"
- "mcr p15, 0, r0, c7, c7, 0\n" /* 使无效ICaches和DCaches */
-
- "mcr p15, 0, r0, c7, c10, 4\n" /* drain write buffer on v4 */
- "mcr p15, 0, r0, c8, c7, 0\n" /* 使无效指令、数据TLB */
-
- "mov r4, %0\n" /* r4 = 页表基址 */
- "mcr p15, 0, r4, c2, c0, 0\n" /* 设置页表基址寄存器 */
-
- "mvn r0, #0\n"
- "mcr p15, 0, r0, c3, c0, 0\n" /* 域访问控制寄存器设为0xFFFFFFFF,
- * 不进行权限检查
- */
- /*
- * 对于控制寄存器,先读出其值,在这基础上修改感兴趣的位,
- * 然后再写入
- */
- "mrc p15, 0, r0, c1, c0, 0\n" /* 读出控制寄存器的值 */
-
- /* 控制寄存器的低16位含义为:.RVI ..RS B... .CAM
- * R : 表示换出Cache中的条目时使用的算法,
- * 0 = Random replacement;1 = Round robin replacement
- * V : 表示异常向量表所在的位置,
- * 0 = Low addresses = 0x00000000;1 = High addresses = 0xFFFF0000
- * I : 0 = 关闭ICaches;1 = 开启ICaches
- * R、S : 用来与页表中的描述符一起确定内存的访问权限
- * B : 0 = CPU为小字节序;1 = CPU为大字节序
- * C : 0 = 关闭DCaches;1 = 开启DCaches
- * A : 0 = 数据访问时不进行地址对齐检查;1 = 数据访问时进行地址对齐检查
- * M : 0 = 关闭MMU;1 = 开启MMU
- */
-
- /*
- * 先清除不需要的位,往下若需要则重新设置它们
- */
- /* .RVI ..RS B... .CAM */
- "bic r0, r0, #0x3000\n" /* ..11 .... .... .... 清除V、I位 */
- "bic r0, r0, #0x0300\n" /* .... ..11 .... .... 清除R、S位 */
- "bic r0, r0, #0x0087\n" /* .... .... 1... .111 清除B/C/A/M */
- /*
- * 设置需要的位
- */
- "orr r0, r0, #0x0002\n" /* .... .... .... ..1. 开启对齐检查 */
- "orr r0, r0, #0x0004\n" /* .... .... .... .1.. 开启DCaches */
- "orr r0, r0, #0x1000\n" /* ...1 .... .... .... 开启ICaches */
- "orr r0, r0, #0x0001\n" /* .... .... .... ...1 使能MMU */
-
- "mcr p15, 0, r0, c1, c0, 0\n" /* 将修改的值写入控制寄存器 */
- : /* 无输出 */
- : "r" (ttb) );
- }
led_on.c
- /*
- * leds.c: 循环点亮4个LED
- * 属于第二部分程序,此时MMU已开启,使用虚拟地址
- */
- #define GPBCON (*(volatile unsigned long *)0xA0000010) // 物理地址0x56000010
- #define GPBDAT (*(volatile unsigned long *)0xA0000014) // 物理地址0x56000014
- #define GPB5_out (1<<(5*2))
- #define GPB6_out (1<<(6*2))
- #define GPB7_out (1<<(7*2))
- #define GPB8_out (1<<(8*2))
- #define GPGCON (*(volatile unsigned long *) 0xA0000060)
- #define GPGDAT (*(volatile unsigned long *) 0xA0000064)
- #define GPGUP (*(volatile unsigned long *) 0xA0000068)
- #define GPX_up 0x00000000
- #define GPG0_in ~(3<<(0*2))
- #define GPG3_in ~(3<<(3*2))
- #define GPG5_in ~(3<<(5*2))
- #define GPG6_in ~(3<<(6*2))
- /*
- * wait函数加上“static inline”是有原因的,
- * 这样可以使得编译leds.c时,wait嵌入main中,编译结果中只有main一个函数。
- * 于是在连接时,main函数的地址就是由连接文件指定的运行时装载地址。
- * 而连接文件mmu.lds中,指定了leds.o的运行时装载地址为0xB4004000,
- * 这样,head.S中的“ldr pc, =0xB4004000”就是跳去执行main函数。
- */
- static inline void wait(unsigned long dly)
- {
- for(; dly > 0; dly--);
- }
- int main(void)
- {
- unsigned long dwDat;
-
- // 将LED1-4对应的GPB5/6/7/8四个引脚设为输出
- GPBCON = GPB5_out|GPB6_out|GPB7_out|GPB8_out;
- GPGCON = (GPG0_in & GPG3_in & GPG5_in & GPG6_in);
- GPGUP = GPX_up;
- while(1){
-
- dwDat=GPGDAT;
-
- if(dwDat & (1<<0))
- GPBDAT |=(1<<5);
- else
- GPBDAT &=~(1<<5);
- if(dwDat & (1<<3))
- GPBDAT |=(1<<6);
- else
- GPBDAT &=~(1<<6);
- if(dwDat & (1<<5))
- GPBDAT |=(1<<7);
- else
- GPBDAT &=~(1<<7);
- if(dwDat & (1<<6))
- GPBDAT |=(1<<8);
- else
- GPBDAT &=~(1<<8);
-
- }
- return 0;
- }
Makefile
- objs := start.o mmu.o led_on.o
- mmu.bin : $(objs)
- arm-linux-ld -Tmmu.lds -o mmu_elf $^
- arm-linux-objcopy -O binary -S mmu_elf $@
- arm-linux-objdump -D -m arm mmu_elf > mmu.dis
-
- %.o:%.c
- arm-linux-gcc -Wall -O2 -c -o $@ $<
- %.o:%.S
- arm-linux-gcc -Wall -O2 -c -o $@ $<
- clean:
- rm -f mmu.bin mmu_elf mmu.dis *.o
以下为能直接运行的二进制文件,下载到Nand Flash 的 Black0 直接以Nand Flash运行.
mmu.rar
阅读(1821) | 评论(0) | 转发(0) |