AnsweredAssumed Answered

Control not returning after Context Switch

Question asked by ajmalali on Feb 15, 2017
Latest reply on Feb 16, 2017 by Carlos_Musich


I am using I.mx6q Sabre-sd board. I am trying to write my own custom OS. During the context switching process, the context switch happens accurately but the control doesn't return to the exact position. ie. when the context has to be switched, i call a function that is written in assembly. After the assembly has been executed the context gets switched but the control doesn't reach where the assembly function was actually called. But the next task is being executed accurately. What might be the problem. The assembly code i am using is,

.global _context_switch
.func _context_switch
//save required registers
    STMFD sp!,{r14}
    STMFD sp!,{r0-r12,r14}
    MRS r0,cpsr
    STMFD sp!,{r0}
    LDR r0,__userStackPtr2Ptr
    LDR r1,[r0]
    STR sp,[r1]
    LDR r0,__newUserStackPtr2Ptr
    LDR r12,[r0]
    LDR sp ,[r12]
    LDMFD sp!,{r0}
    ORR r0,r0,#0xc0
    MSR cpsr,r0
    BIC r0,r0,#0xc0
    MSR spsr,r0
    LDR sp,[r12]
    ADD sp,sp,#4
    LDMFD sp!,{r0-r12,lr}
    LDMFD sp!,{pc}^
// sub lr, lr, #4;
// bx lr




Is this correct? Am i missing some steps ? I am not good with assembly. Please help.