内存管理单元(MMU),它负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查。现代的多用用户多进程操作系统通过MMU使得各个用户进程都拥有自己独立的地址空间:地址映射功能使得各进程拥有“看起来"一样的地址空间,而内存访问权限的检查可以保护每个进程所用的内存不被其他进程破坏。
源码:
head.s
- @*************************************************************************
- @ 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
init.c
-
-
-
-
-
- #define WTCON (*(volatile unsigned long *)0x53000000)
-
- #define MEM_CTL_BASE 0x48000000
-
-
-
-
- void disable_watch_dog(void)
- {
- WTCON = 0;
- }
-
-
-
- void memsetup(void)
- {
-
- unsigned long const mem_cfg_val[]={ 0x22011110,
- 0x00000700,
- 0x00000700,
- 0x00000700,
- 0x00000700,
- 0x00000700,
- 0x00000700,
- 0x00018005,
- 0x00018005,
- 0x008C07A3,
- 0x000000B1,
- 0x00000030,
- 0x00000030,
- };
- int i = 0;
- volatile unsigned long *p = (volatile unsigned long *)MEM_CTL_BASE;
- for(; i < 13; i++)
- p[i] = mem_cfg_val[i];
- }
-
-
-
- void copy_2th_to_sdram(void)
- {
- unsigned int *pdwSrc = (unsigned int *)2048;
- unsigned int *pdwDest = (unsigned int *)0x30004000;
-
- while (pdwSrc < (unsigned int *)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) /* 必须是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;
-
-
-
-
-
-
- virtuladdr = 0;
- physicaladdr = 0;
- *(mmu_tlb_base + (virtuladdr >> 20)) = (physicaladdr & 0xFFF00000) | \
- MMU_SECDESC_WB;
-
-
-
-
-
-
- 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;
- physicaladdr += 0x100000;
- }
- }
-
-
-
- void mmu_init(void)
- {
- unsigned long ttb = 0x30000000;
- __asm__(
- "mov r0, #0\n"
- "mcr p15, 0, r0, c7, c7, 0\n"
-
- "mcr p15, 0, r0, c7, c10, 4\n"
- "mcr p15, 0, r0, c8, c7, 0\n"
-
- "mov r4, %0\n"
- "mcr p15, 0, r4, c2, c0, 0\n"
-
- "mvn r0, #0\n"
- "mcr p15, 0, r0, c3, c0, 0\n"
-
-
-
-
-
-
- "mrc p15, 0, r0, c1, c0, 0\n"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- "bic r0, r0, #0x3000\n"
- "bic r0, r0, #0x0300\n"
- "bic r0, r0, #0x0087\n"
-
-
-
- "orr r0, r0, #0x0002\n"
- "orr r0, r0, #0x0004\n"
- "orr r0, r0, #0x1000\n"
- "orr r0, r0, #0x0001\n"
-
- "mcr p15, 0, r0, c1, c0, 0\n"
- :
- : "r" (ttb) );
- }