MMU使用实例:地址映射
这个实例将开启MMU,并将虚拟地址0xA0000000-0xA0100000映射到物理地址0x56000000-0x56100000(GPBCON物理地址为0x56000010,GPBDAT物理地址为0x56000014),来驱动LED。
将虚拟地址0xB0000000-0xB3FFFFFF映射到物理地址0x30000000-0x33FFFFFF,在连接程序时,将一部分代码的运行地址指定为0xB0004000.
这个程序只使用一级页表,以段的方式进行地址映射,32位CPU虚拟地址空间达到4G,一级页表使用4096个描述符来表示4G空间(每个描述符对应1MB),每个描述符占4字节,所以一级页表占16KB。这个程序使用SDRAM的开始16KB存放一级页表,所以剩下的内存开始地址就为0x30004000,这个地址最终会对应虚拟地址0xB0004000(所以代码运行地址为0xB0004000)
程序分为两部分:第一部分的运行地址为0,它用来初始化SDRAM,复制第二部分的代码到SDRAM中(存放在0x30004000)、设置页表、启动MMU,最后跳到SDRAM中(地址0xB0004000),第二部分运行地址设为0xB0004000,用来驱动LED
先看连接文件mmu.lds
SECTIONS {
firtst 0x00000000 : { head.o init.o }
second 0xB0004000 : AT(2048) { leds.o }
}
程序分两个段:first和second。first由head.o和init.o组成,加载和运行地址都是0,second由leds.o组成,加载地址为2048,重定位地址为0xB0004000。
@*************************************************************************
@ File:head.S
@ 功能:设置SDRAM,将第二部分代码复制到SDRAM,设置页表,启动MMU,
@ 然后跳到SDRAM继续执行
@*************************************************************************
.text
.global _start
_start:
ldr sp, =4096 @ 设置栈指针,以下都是C函数,调用前需要设好栈
bl disable_watch_dog @ 关闭WATCHDOG,否则CPU会不断重启
bl memsetup @ 设置存储控制器以使用SDRAM
bl copy_2th_to_sdram @ 将第二部分代码复制到SDRAM
bl create_page_table @ 设置页表
bl mmu_init @ 启动MMU,启动以后下面代码都用虚拟地址
ldr sp, =0xB4000000 @ 重设栈指针,指向SDRAM顶端(使用虚拟地址)
ldr pc, =0xB0004000 @ 跳到SDRAM中继续执行第二部分代码
halt_loop:
b halt_loop
#define WTCON (*(volatile unsigned long *)0x53000000)
#define MEM_CTL_BASE 0x48000000
void disable_watch_dog(void)
{
WTCON = 0; // 关闭WATCHDOG很简单,往这个寄存器写0即可
}
void memsetup(void)
{
unsigned long const mem_cfg_val[]={ 0x22011110, //BWSCON
0x00000700, //BANKCON0
0x00000700, //BANKCON1
0x00000700, //BANKCON2
0x00000700, //BANKCON3
0x00000700, //BANKCON4
0x00000700, //BANKCON5
0x00018005, //BANKCON6
0x00018005, //BANKCON7
0x008C07A3, //REFRESH
0x000000B1, //BANKSIZE
0x00000030, //MRSRB6
0x00000030, //MRSRB7
};
int i = 0;
volatile unsigned long *p = (volatile unsigned long *)MEM_CTL_BASE;
for(; i < 13; i++)
p[i] = mem_cfg_val[i]; //循环复制13个寄存器到内存控制器基址
}
void copy_2th_to_sdram(void)
{
unsigned int *pdwSrc = (unsigned int *)2048; //第二段代码加载地址2048
unsigned int *pdwDest = (unsigned int *)0x30004000; //0x30004000前放页表
while (pdwSrc < (unsigned int *)4096) //4kb最大4096
{
*pdwDest = *pdwSrc;
pdwDest++;
pdwSrc++;
}
}
void create_page_table(void)
{
#define MMU_FULL_ACCESS (3 << 10)
#define MMU_DOMAIN (0 << 5)
#define MMU_SPECIAL (1 << 4)
#define MMU_CACHEABLE (1 << 3)
#define MMU_BUFFERABLE (1 << 2)
#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;
virtuladdr = 0;
physicaladdr = 0;
//虚拟地址[31:20]用于索引一级页表,找到它对应的描述符,对应于(virtualaddr>>20)
//段描述符中[31:20]保存段的物理地址,对应(physicaladdr & 0xFFF00000)
*(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
MMU_SECDESC_WB;
[page]
virtuladdr = 0xA0000000;
physicaladdr = 0x56000000;
*(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
MMU_SECDESC;
virtuladdr = 0xB0000000;
physicaladdr = 0x30000000;
while (virtuladdr < 0xB4000000)
{
*(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) |
MMU_SECDESC_WB;
virtuladdr += 0x100000; //右移20位就是1
physicaladdr += 0x100000; //右移20位就是1
}
}
void mmu_init(void)
{
unsigned long ttb = 0x30000000;
__asm__(
"mov r0, #0
"
"mcr p15, 0, r0, c7, c7, 0
"
"mcr p15, 0, r0, c7, c10, 4
"
"mcr p15, 0, r0, c8, c7, 0
"
"mov r4, %0
"
"mcr p15, 0, r4, c2, c0, 0
"
"mvn r0, #0
"
"mcr p15, 0, r0, c3, c0, 0
"
"mrc p15, 0, r0, c1, c0, 0
"
"bic r0, r0, #0x3000
"
"bic r0, r0, #0x0300
"
"bic r0, r0, #0x0087
"
"orr r0, r0, #0x0002
"
"orr r0, r0, #0x0004
"
"orr r0, r0, #0x1000
"
"orr r0, r0, #0x0001
"
"mcr p15, 0, r0, c1, c0, 0
"
:
: "r" (ttb) );
}
#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))
static inline void wait(unsigned long dly)
{
for(; dly > 0; dly--);
}
int main(void)
{
unsigned long i = 0;
// 将LED1-4对应的GPB5/6/7/8四个引脚设为输出
GPBCON = GPB5_out|GPB6_out|GPB7_out|GPB8_out;
while(1){
wait(30000);
GPBDAT = (~(i<<5)); // 根据i的值,点亮LED1-4
if(++i == 16)
i = 0;
}
return 0;
}
最后是Makefile
objs := head.o init.o leds.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