I am porting some embedded system to mpc8306 and switch my tasks in the function handling timer interrupt. I know that the basic concept of task switching is to save the values of all the registers working for current task and reload the values of the registers for another task. Since all the register should be saved for current task to the task's stack, I think the meaning of the first several statements in the timer interrupt function should be like "lwz r1,(address of current task stack) ". Thus, my question is: how can I save the value of "r1" since it is written before I get the stack address ?
Maybe you can share more pseudo code to analyze for the specific question...
My assembly code in timer interrupt handler is like the following:
vTimerInteruptHandler:
mtspr 272,r1
lwz r1, (currentStack)------------get the address of current task's stack
stw r4, XR4(r1) --------------save r4 to the stack
mflr r4,
stw r4, XLR(r1)--------------save lr to the stack
lis r4, vPortISR@h
ori r4, r4, vPortISR@l
mtlr r4
blr
rfi
vPortISR:
lwz r4, XLR(r1)
mtlr r4
lwz r4, XR4(r1)
portSAVE_CONTEXT
# call the c interrupt handler
lis r4, vPortTickIntHandler@h--------------------vPortTickHandler is to switch task, which makes "currentStack" point to the next task's stack
ori r4, r4, vPortTickIntHandler@l
mtlr r4
blrl
portRESTORE_CONTEXT
rfi
.end
.macro portSAVE_CONTEXT
stw r0, XR0(r1)
stw r2, XR2(r1)
stw r3, XR3(r1)
stw r4, XR4(r1)
stw r5, XR5(r1)
stw r6, XR6(r1)
stw r7, XR7(r1)
stw r8, XR8(r1)
stw r9, XR9(r1)
stw r10, XR10(r1)
stw r11, XR11(r1)
stw r12, XR12(r1)
stw r13, XR13(r1)
stw r14, XR14(r1)
stw r15, XR15(r1)
stw r16, XR16(r1)
stw r17, XR17(r1)
stw r18, XR18(r1)
stw r19, XR19(r1)
stw r20, XR20(r1)
stw r21, XR21(r1)
stw r22, XR22(r1)
stw r23, XR23(r1)
stw r24, XR24(r1)
stw r25, XR25(r1)
stw r26, XR26(r1)
stw r27, XR27(r1)
stw r28, XR28(r1)
stw r29, XR29(r1)
stw r30, XR30(r1)
stw r31, XR31(r1)
mfmsr r0
stw r0, XMSR(r1)
mfspr r0, SRR0
stw r0, XSRR0(r1)
mfspr r0, SRR1
stw r0, XSRR1(r1)
mflr r0
stw r0, XLR(r1)
mfcr r0
stw r0, XCR(r1)
mfctr r0
stw r0, XCTR(r1)
mfxer r0
stw r0, XXER(r1)
mfmsr r0
stw r0, XMSR(r1)
mfspr r0,272
stw r0,XR1(r1)
.endm
.macro portRESTORE_CONTEXT
lwz r1, (currentStack)
lwz r0, XSRR0(r1)
mtspr SRR0,r0
lwz r0, XSRR1(r1)
mtspr SRR1,r0
lwz r0, XLR(r1)
mtlr r0
lwz r0, XCR(r1)
mtcrf 0xff,r0
lwz r0, XCTR(r1)
mtctr r0
lwz r0, XXER(r1)
mtxer r0
lwz r0, XR0(r1)
lwz r2, XR2(r1)
lwz r3, XR3(r1)
lwz r4, XR4(r1)
lwz r5, XR5(r1)
lwz r6, XR6(r1)
lwz r7, XR7(r1)
lwz r8, XR8(r1)
lwz r9, XR9(r1)
lwz r10, XR10(r1)
lwz r11, XR11(r1)
lwz r12, XR12(r1)
lwz r13, XR13(r1)
lwz r14, XR14(r1)
lwz r15, XR15(r1)
lwz r16, XR16(r1)
lwz r17, XR17(r1)
lwz r18, XR18(r1)
lwz r19, XR19(r1)
lwz r20, XR20(r1)
lwz r21, XR21(r1)
lwz r22, XR22(r1)
lwz r23, XR23(r1)
lwz r24, XR24(r1)
lwz r25, XR25(r1)
lwz r26, XR26(r1)
lwz r27, XR27(r1)
lwz r28, XR28(r1)
lwz r29, XR29(r1)
lwz r30, XR30(r1)
lwz r31, XR31(r1)
lwz r1, 0(r1)
.endm
Why "lwz r1, 0(r1)" rather than "lwz r1, XR1(r1)"?
Thank you for your help and sorry for not making my code clear. XR1 equals to 0 in my context. I just want to make sure if I saved all the resgisters that I should.
This question seems to be more about state saving for exception entry rather than switching tasks, but the usual approach is to use a SPRG as temporary storage.
I have used SPRG0 according to your advice and the situation looks like better, but it is still not working. I saved the following registers for state saving in timer interrupt handler:
r0 to r31, srr0,srr1, msr,lr, ctr, cr, xer.
I wonder if there are any other registers I should save ?