我在 ARM926 上的抢先操作系统(极简主义)结束了,但我遇到了困难。
今天,我能够启动操作系统,切换到用户模式(使用它自己的任务)并在定时器中断后返回操作系统模式(SVC)。
但是今天我试图在定时器中断之前返回带有上下文的用户函数。
我做了什么:我的初始化任务功能:
init_task(taskstruct * task, unsigned int* stack, void (*function)(void) ){
stack += STACK_SIZE;// - 16; /* End of stack, minus what we're about to push */
//stack[0] = 0x10; /* User mode, interrupts on */
//stack[1] = (unsigned int)function;
task->sp = stack;
task->sp[0] = task->registers[0]; // r0
task->sp[1] = task->registers[1];// = task->sp[3];//0; // r1
task->sp[2] = task->registers[2];// = task->sp[4];//0; // r2
task->sp[3] = task->registers[3];// = task->sp[5];//0; // r3
task->sp[4] = task->registers[4];// = task->sp[6];//0; // r4
task->sp[5] = task->registers[5];// = task->sp[7];//0; // r5
task->sp[6] = task->registers[6];// = task->sp[8];//0; // r6
task->sp[7] = task->registers[7];// = task->sp[9];//0; // r7
task->sp[8] = task->registers[8];// = task->sp[10];//0; // r8
task->sp[9] = task->registers[9];// = task->sp[11];//0; // r9
task->sp[10] = task->registers[10];// = task->sp[12];//0; // r10
task->sp[11] = task->registers[11];// = task->sp[13];//0; // r11
task->sp[12] = task->registers[12];// = task->sp[14];//0; // r12
task->sp[13] = (unsigned int)function;
}
激活功能:
.global activate
activate:
/* Save kernel state */
STMFD sp!,{r1-r12,lr}
NOP
msr CPSR_c, SYS_MODE /* Sys mode with IRQ enabled and FIQ disabled*/
mov sp, r0 /* MOVE TO THE STACK USER
/* LOAD THE TASK'S CONTEXT */
mov ip, r0
LDMFD sp!, {r0-ip,lr}
NOP
mov pc, lr
它的调用:
activate(task[0].sp);
print_uart0("Kernel gets back control ! \n");
print_uart0("Load the next task ! \n");
activate(task[0].sp);
我在 irq_handler 中做了什么:
irq_handler:
/* Save the return value */
SUB ip,lr,#4
BL event_irq_handler
/* Save the user task context */
MSR CPSR_c, #INT_OFF|SYS_MODE
MOV lr, ip
STMFD sp!,{r0-ip,lr}
NOP
MOV r0, sp
BL saveTaskContext
/* Load kernel state */
MSR CPSR_c,SVC_MODE
LDMFD sp!,{r1-r12,pc}
NOP
savcontext 函数:
int i = 0;
//char printable = 0x00;
/* UPDATE THE STACK TASK */
for ( i = 0 ; i <= 13 ; i++ ){
task[0].sp[i] = *(ptr+i);
}
但是我的问题是当我第二次调用activate(task[0].sp) 时,它会在任何地方分支并且我的主程序会重新启动。
我哪里错了?
问候,文森特B