I'm in the end of my pre-emptive OS (minimalist) on an ARM926 but I encounter a difficulty.

Today, I'm able to start the OS, switch in user mode (with its own stask) and get back to the OS mode (SVC) after an interrupt on the timer.

But today I'm trying to return in the user function with the context before the timer interrupt.

What I've done :
My init task function :

```
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;
}

```

The activate 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

```

Its call :

```
activate(task[0].sp);
print_uart0("Kernel gets back control ! \n");
print_uart0("Load the next task ! \n");
activate(task[0].sp);

```

What I do in my 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

```

The savcontext function :

```
int i = 0;
//char printable = 0x00;
/* UPDATE THE STACK TASK */
for ( i = 0 ; i <= 13 ; i++ ){
    task[0].sp[i] = *(ptr+i);
}

```

But my problem is when I call for the second time activate(task[0].sp), it branches anywhere and my main program is restarted.

Where am I wrong ?

Regards, VincentB