#include "regs.h"
void enable_mmu(unsigned long ttb);
void init_ttb(unsigned long *ttb_base);
void mmap(unsigned long *ttb_base, unsigned long va, unsigned long pa);
void memset(char *buf, char ch, int size);
void memcpy(char *dst, char *src, int size);
void do_swi(unsigned long regs[]);
void (*printf)(char *, ...) = 0x43e11434;
void main(void)
{
unsigned long vector_base = 0xffff0000;
unsigned long tt_base = 0x73000000;
unsigned long *pdo_swi = 0x75000000;
extern unsigned long vectors_start, vectors_end;
memset(tt_base, 0x00, 16 * 1024);
mmap(tt_base, vector_base, 0x70000000);
enable_mmu(tt_base);
memcpy(vector_base, vectors_start, 0x100);
*pdo_swi = do_swi;
__asm__ __volatile__ (
"mov r0, r0\n"
"swi 0x7777\n"
"mov r1, r1\n"
);
}
void do_swi(unsigned long regs[])
{
unsigned long *instr = regs[13] - 4;
printf("swi: 0x%x\n", *instr & 0xffffff);
}
void enable_mmu(unsigned long ttb)
{
unsigned long c1_flags;
init_ttb(ttb);
c1_flags = 1 | (1 << 3) | ( 1 << 11) | ( 1 << 13) | (1 << 28);
__asm__ __volatile__ (
"mvn r0, #0 \n"
"mcr p15, 0, r0, c3, c0, 0\n"
"mcr p15, 0, %1, c2, c0, 0\n" //configure ttb
"mrc p15, 0, r0, c1, c0, 0\n"
"orr %0, r0, %0\n"
"mcr p15, 0, %0, c1, c0, 0\n" //enable mmu
:
: "r" (c1_flags), "r" (ttb)
: "r0"
);
}
void init_ttb(unsigned long *ttb_base)
{
unsigned long va, pa;
for (va = 0x00000000; va < 0x10000000; va += 0x100000) { //Others
pa = va;
ttb_base[ va >> 20] = (pa & 0xfff00000) | 2;
}
for (va = 0x10000000; va < 0x14000000; va += 0x100000) { //SFR
pa = va;
ttb_base[ va >> 20] = (pa & 0xfff00000) | 2;
}
for (va = 0x40000000; va < 0x80000000; va += 0x100000) { //DRAM
pa = va;
ttb_base[ va >> 20] = (pa & 0xfff00000) | 2;
}
}
void mmap(unsigned long *ttb_base, unsigned long va, unsigned long pa)
{
ttb_base[ va >> 20] = (pa & 0xfff00000) | 2;
}
void memset(char *buf, char ch, int size)
{
int i;
for (i = 0; i < size; i ++)
buf[i] = ch;
}
void memcpy(char *dst, char *src, int size)
{
int i;
for (i = 0; i < size; i ++)
dst[i] = src[i];
}
__asm__ (
"vectors:\n"
"b reset\n"
"b und\n"
"b swi\n"
"b pre_abt\n"
"b dat_abt\n"
".word 0\n"
"b irq\n"
"b fiq\n"
"reset:\n"
"und:\n"
"mov sp, #0x74000000\n"
"stmfd sp!, {r0-r12, lr}\n"
"mov r0, sp\n"
"mov r3, #0x74000000\n"
"ldr r3, [r3]\n"
"blx r3\n"
"mov sp, #0x74000000\n"
"ldmea sp, {r0-r12, pc}^\n"
"swi:\n"
"stmfd sp!, {r0-r12, lr}\n"
"mov r0, sp\n"
"mov r3, #0x75000000\n"
"ldr r3, [r3]\n"
"blx r3\n"
"ldmfd sp, {r0-r12, pc}^\n"
"pre_abt:\n"
"dat_abt:\n"
"fiq:\n"
"irq:\n"
"mov sp, #0x75000000\n"
"sub lr, lr, #4 \n"
"stmfd sp!, {r0-r12, lr}\n"
"mov r0, sp\n"
"mov r3, #0x75000000\n"
"ldr r3, [r3]\n"
"blx r3\n"
"mov sp, #0x75000000\n"
"ldmea sp, {r0-r12, pc}^\n"
"EOV:\n"
"vectors_start:\n"
".word vectors\n"
"vectors_end:\n"
".word EOV\n"
);
===================================================================
Makefile文件:
default:
arm-linux-gcc -c test.c -o test.o
arm-linux-ld -Ttext=0x70003000 test.o -o test
arm-linux-objcopy -O binary test test.bin
clean:
rm -f test.o test test.bin *~
===================================================================
烧写测试参考:“Tiny4412从SD卡启动u-boot用linux的DNW烧写裸板程序”
说明:
①系统上电复位后CPU就处于管理模式(svc),执行0地址处的b reset跳转到reset:处继续运行,把相关硬件初始化以后会清掉cpsr后5位并把第四位置1,进入user mode设置栈后运行于用户态(usr),即处理器启动时首先进入管理员模式(svc),此后进入除用户模式之外的其他模式,主要完成各模式的堆栈设置,最后进入用户模式,运行用户程序;当发生swi软中断以后cpu进入svc模式。
②swi软中断主要用于usr模式(应用程序通常运行于usr模式)切换到svc模式下。在arm的7种模式当中(已经不止7种了)usr模式是唯一一个非特权模式,其他都是特权模式,比如fiq、und等都是特权模式,他们之间的切换直接更改cpsr寄存器的低5位的模式位或者真的发生fiq、und等异常的时候就可以达到切换的目的;而usr模式不是特权模式没有办法更改cpsr寄存器的低5位进行切换,想切换到特权模式只能调用swi指令,swi指令会帮助它进入到svc模式。
③如果原来是svc模式,发生未定义指令异常后进入und(Undefined)模式,这时候要重新设置sp栈指针;如果执行swi指令的时候已经处于svc模式,那么发生swi的软中断之后仍然还是svc模式,这个时候就不用再去设置sp栈指针了(在tiny4412异常实验中因为运行的uboot,已经处于svc模式,故要注意sp指针)。
④只有处理swi和und异常的时候lr指向下一条指令,其他的异常发生的时候lr都是指向下两条指令;ARM上的每条指令长度都是32位即4个字节;swi指令也是32位且其后面跟的value值占该指令的低24位,所以在程序里可以得到swi指令的value值,具体如下:
unsigned long *pdo_swi = 0x75000000;
*pdo_swi = do_swi; //先把中断处理函数do_swi地址放在0x75000000
在发生swi异常的时候程序会自动跳到异常向量入口:"b swi\n"
接着跳转到swi处执行:
"swi:\n"
"stmfd sp!, {r0-r12, lr}\n"
//保护现场,把usr模式下的相关寄存器入栈,
//存放顺序是先存放lr、r12....r0,最终sp指向r0的地址
"mov r0, sp\n" //把上一步中指向usr模式下r0地址的sp传给r0寄存器
"mov r3, #0x75000000\n"
"ldr r3, [r3]\n"
"blx r3\n" //调用中断处理函数do_swi,参数放在r0中
//regs[0] == r0
//regs[1] == r1
//.....
//regs[12] == r12
//regs[13] == lr
void do_swi(unsigned long regs[]) //regs指向usr模式下r0地址
{
//按照入栈顺序regs[13]为usr模式下lr值,即发生swi异常时下一条指令的地址
//regs[13] - 4 = lr - 4 ; 即上一条指令的地址也就是swi异常的地址
unsigned long *instr = regs[13] - 4;
//根据ARM指令是32位的,swi指令也是32位且其后面跟的value值占该指令的低24位得到value值
printf("swi: 0x%x\n", *instr & 0xffffff);
}
上一篇:Tiny4412的MMU映射代码示例
下一篇:Tiny4412的MMU解析
推荐阅读最新更新时间:2024-03-16 16:16