2022-07-20 01:33 AM
Hi There, i'm working on project to reduce blocking delay and i already use STM module for system time (system ticks) and preemtive of four thread using eTimer. But here i try to update a system where during delay it execute another thread immediately by do simple context switching. I already try try to save and load the register for context switching. but when the previous thread is load, some of the register are different.
What is the importance register and as i notice the stack register is offset of r0.
here is my example of context switching but fail to switch:
asm (".global _port_switch");
asm ("_port_switch:");
asm volatile ("wrteei 0" : : : "memory");
//Store register information into argument 1
// asm ("subi %sp, %sp, 80");
// asm ("stw %r0, 144(%r3)");
asm ("subi %sp, %sp, 64");
asm ("lwz %r0, 4(%sp");
asm ("stw %r0, 144(%r3)");
asm ("lwz %r0, 8(%sp");
asm ("stw %r0, 148(%r3)");
asm ("lwz %r0, 12(%sp");
asm ("stw %r0, 152(%r3)");
asm ("lwz %r0, 16(%sp");
asm ("stw %r0, 156(%r3)");
asm ("lwz %r0, 20(%sp");
asm ("stw %r0, 160(%r3)");
asm ("lwz %r0, 24(%sp");
asm ("stw %r0, 164(%r3)");
asm ("lwz %r0, 28(%sp");
asm ("stw %r0, 168(%r3)");
asm ("lwz %r0, 32(%sp");
asm ("stw %r0, 172(%r3)");
asm ("lwz %r0, 36(%sp");
asm ("stw %r0, 176(%r3)");
asm ("lwz %r0, 40(%sp");
asm ("stw %r0, 180(%r3)");
asm ("lwz %r0, 44(%sp");
asm ("stw %r0, 184(%r3)");
asm ("lwz %r0, 48(%sp");
asm ("stw %r0, 188(%r3)");
asm ("lwz %r0, 52(%sp");
asm ("stw %r0, 192(%r3)");
asm ("lwz %r0, 56(%sp");
asm ("stw %r0, 196(%r3)");
asm ("lwz %r0, 60(%sp");
asm ("stw %r0, 200(%r3)");
asm ("lwz %r0, 64(%sp");
asm ("stw %r0, 204(%r3)");
asm ("stw %r0, 24(%r3)");
asm ("mfSRR0 %r0");
asm ("stw %r0, 0(%r3)");
asm ("mfSRR1 %r0");
asm ("stw %r0, 4(%r3)");
asm ("mfCR %r0");
asm ("stw %r0, 8(%r3)");
asm ("mfLR %r0");
asm ("stw %r0, 12(%r3)");
asm ("mfCTR %r0");
asm ("stw %r0, 16(%r3)");
asm ("mfXER %r0");
asm ("stw %r0, 20(%r3)");
// asm ("stw %r1, 148(%r3)");
// asm ("stw %r2, 152(%r3)");
asm ("stw %r1, 28(%r3)");
asm ("stw %r2, 32(%r3)");
// asm ("stw %r3, 28(%r3)"); //not store r1(sp), r2, r3(argument 1), r4(argument 2)
// asm ("stw %r4, 32(%r3)");
asm ("stw %r5, 36(%r3)");
asm ("stw %r6, 40(%r3)");
asm ("stw %r7, 44(%r3)");
asm ("stw %r8, 48(%r3)");
asm ("stw %r9, 52(%r3)");
asm ("stw %r10, 56(%r3)");
asm ("stw %r11, 60(%r3)");
asm ("stw %r12, 64(%r3)");
asm ("stw %r13, 68(%r3)");
asm ("stw %r14, 72(%r3)");
asm ("stw %r15, 76(%r3)");
asm ("stw %r16, 80(%r3)");
asm ("stw %r17, 84(%r3)");
asm ("stw %r18, 88(%r3)");
asm ("stw %r19, 92(%r3)");
asm ("stw %r20, 96(%r3)");
asm ("stw %r21, 100(%r3)");
asm ("stw %r22, 104(%r3)");
asm ("stw %r23, 108(%r3)");
asm ("stw %r24, 112(%r3)");
asm ("stw %r25, 116(%r3)");
asm ("stw %r26, 120(%r3)");
asm ("stw %r27, 124(%r3)");
asm ("stw %r28, 128(%r3)");
asm ("stw %r29, 132(%r3)");
asm ("stw %r30, 136(%r3)");
asm ("stw %r31, 140(%r3)");
// asm ("addi %sp, %sp, 80");
//Load register information from argument 2
asm ("lwz %r31, 140(%r4");
asm ("lwz %r30, 136(%r4");
asm ("lwz %r29, 132(%r4");
asm ("lwz %r28, 128(%r4");
asm ("lwz %r27, 124(%r4");
asm ("lwz %r26, 120(%r4");
asm ("lwz %r25, 116(%r4");
asm ("lwz %r24, 112(%r4");
asm ("lwz %r23, 108(%r4");
asm ("lwz %r22, 104(%r4");
asm ("lwz %r21, 100(%r4");
asm ("lwz %r20, 96(%r4");
asm ("lwz %r19, 92(%r4");
asm ("lwz %r18, 88(%r4");
asm ("lwz %r17, 84(%r4");
asm ("lwz %r16, 80(%r4");
asm ("lwz %r15, 76(%r4");
asm ("lwz %r14, 72(%r4");
asm ("lwz %r13, 68(%r4");
asm ("lwz %r12, 64(%r4");
asm ("lwz %r11, 60(%r4");
asm ("lwz %r10, 56(%r4");
asm ("lwz %r9, 52(%r4");
asm ("lwz %r8, 48(%r4");
asm ("lwz %r7, 44(%r4");
asm ("lwz %r6, 40(%r4");
asm ("lwz %r5, 36(%r4");
// asm ("lwz %r4, 32(%r4");
// asm ("lwz %r3, 28(%r4");
asm ("stw %r2, 32(%r3)");
asm ("stw %r1, 28(%r3)");
asm ("lwz %r0, 20(%r4");
asm ("mtXER %r0");
asm ("lwz %r0, 16(%r4");
asm ("mtCTR %r0");
asm ("lwz %r0, 12(%r4");
asm ("mtLR %r0");
asm ("lwz %r0, 8(%r4");
asm ("mtCR %r0");
asm ("lwz %r0, 4(%r4");
asm ("mtSRR1 %r0");
asm ("lwz %r0, 0(%r4");
asm ("mtSRR0 %r0");
asm ("lwz %r0, 24(%r4");
asm ("lwz %r0, 204(%r4)");
asm ("stw %r0, 64(%sp");
asm ("lwz %r0, 200(%r4)");
asm ("stw %r0, 60(%sp");
asm ("lwz %r0, 196(%r4)");
asm ("stw %r0, 56(%sp");
asm ("lwz %r0, 192(%r4)");
asm ("stw %r0, 52(%sp");
asm ("lwz %r0, 188(%r4)");
asm ("stw %r0, 48(%sp");
asm ("lwz %r0, 184(%r4)");
asm ("stw %r0, 44(%sp");
asm ("lwz %r0, 180(%r4)");
asm ("stw %r0, 40(%sp");
asm ("lwz %r0, 176(%r4)");
asm ("stw %r0, 36(%sp");
asm ("lwz %r0, 172(%r4)");
asm ("stw %r0, 32(%sp");
asm ("lwz %r0, 168(%r4)");
asm ("stw %r0, 28(%sp");
asm ("lwz %r0, 164(%r4)");
asm ("stw %r0, 24(%sp");
asm ("lwz %r0, 160(%r4)");
asm ("stw %r0, 20(%sp");
asm ("lwz %r0, 156(%r4)");
asm ("stw %r0, 16(%sp");
asm ("lwz %r0, 152(%r4)");
asm ("stw %r0, 12(%sp");
asm ("lwz %r0, 148(%r4)");
asm ("stw %r0, 8(%sp");
asm ("lwz %r0, 144(%r4)");
asm ("stw %r0, 4(%sp");
asm ("addi %sp, %sp, 64");
asm volatile("wrteei 1" : : : "memory");
asm ("blr"); //blr (Branch to Link Register) not rfi (return from interrupt)
i'm really appreciate the help cause i'm already dont't know what to do.
sorry for bad english.