This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Control not returning after Context Switch

Hi,

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
_context_switch:
    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

    .endfunc

 

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

 

Regards,

Ajeesh

Parents
  • Hi daith,

    Thanks for the reply.
    My original implementation had "sub lr,lr, #4" at the very beginning and the"bx lr" at the end. Even then the execution does not return to the call. It goes directly to the next task. Is context switch suppose to happen like this? Or should it wait till the scheduler gets finished?

    Regards,
    Ajeesh

Reply
  • Hi daith,

    Thanks for the reply.
    My original implementation had "sub lr,lr, #4" at the very beginning and the"bx lr" at the end. Even then the execution does not return to the call. It goes directly to the next task. Is context switch suppose to happen like this? Or should it wait till the scheduler gets finished?

    Regards,
    Ajeesh

Children
  • I thought this was supposed to be the interrupt function that saved the context of one task and loaded the context for another. So it would return to continue the new task. I don't quite see what you were expecting it to do otherwise. If you want a system function to call then it should set an event to interrupt at user level into a routine like this unless you only have programmed task switching.