\r
.align 8\r
xPortStartScheduler:\r
- lw sp, pxCurrentTCB /* Load pxCurrentTCB. */\r
- lw sp, 0( sp ) /* Read sp from first TCB member. */\r
- lw x1, 0( sp )\r
- lw x5, 1 * WORD_SIZE( sp ) /* t0 */\r
- lw x6, 2 * WORD_SIZE( sp ) /* t1 */\r
- lw x7, 3 * WORD_SIZE( sp ) /* t2 */\r
- lw x8, 4 * WORD_SIZE( sp ) /* s0/fp */\r
- lw x9, 5 * WORD_SIZE( sp ) /* s1 */\r
- lw x10, 6 * WORD_SIZE( sp ) /* a0 */\r
- lw x11, 7 * WORD_SIZE( sp ) /* a1 */\r
- lw x12, 8 * WORD_SIZE( sp ) /* a2 */\r
- lw x13, 9 * WORD_SIZE( sp ) /* a3 */\r
- lw x14, 10 * WORD_SIZE( sp ) /* a4 */\r
- lw x15, 11 * WORD_SIZE( sp ) /* a5 */\r
- lw x16, 12 * WORD_SIZE( sp ) /* a6 */\r
- lw x17, 13 * WORD_SIZE( sp ) /* a7 */\r
- lw x18, 14 * WORD_SIZE( sp ) /* s2 */\r
- lw x19, 15 * WORD_SIZE( sp ) /* s3 */\r
- lw x20, 16 * WORD_SIZE( sp ) /* s4 */\r
- lw x21, 17 * WORD_SIZE( sp ) /* s5 */\r
- lw x22, 18 * WORD_SIZE( sp ) /* s6 */\r
- lw x23, 19 * WORD_SIZE( sp ) /* s7 */\r
- lw x24, 20 * WORD_SIZE( sp ) /* s8 */\r
- lw x25, 21 * WORD_SIZE( sp ) /* s9 */\r
- lw x26, 22 * WORD_SIZE( sp ) /* s10 */\r
- lw x27, 23 * WORD_SIZE( sp ) /* s11 */\r
- lw x28, 24 * WORD_SIZE( sp ) /* t3 */\r
- lw x29, 25 * WORD_SIZE( sp ) /* t4 */\r
- lw x30, 26 * WORD_SIZE( sp ) /* t5 */\r
- lw x31, 27 * WORD_SIZE( sp ) /* t6 */\r
- addi sp, sp, CONTEXT_SIZE\r
- csrs mie, 8 /* Enable soft interrupt. */\r
- csrs mstatus, 8 /* Enable interrupts. */\r
- ret\r
+ lw sp, pxCurrentTCB /* Load pxCurrentTCB. */\r
+ lw sp, 0( sp ) /* Read sp from first TCB member. */\r
+\r
+ lw x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */\r
+ lw x5, 2 * WORD_SIZE( sp ) /* t0 */\r
+ lw x6, 3 * WORD_SIZE( sp ) /* t1 */\r
+ lw x7, 4 * WORD_SIZE( sp ) /* t2 */\r
+ lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */\r
+ lw x9, 6 * WORD_SIZE( sp ) /* s1 */\r
+ lw x10, 7 * WORD_SIZE( sp ) /* a0 */\r
+ lw x11, 8 * WORD_SIZE( sp ) /* a1 */\r
+ lw x12, 9 * WORD_SIZE( sp ) /* a2 */\r
+ lw x13, 10 * WORD_SIZE( sp ) /* a3 */\r
+ lw x14, 11 * WORD_SIZE( sp ) /* a4 */\r
+ lw x15, 12 * WORD_SIZE( sp ) /* a5 */\r
+ lw x16, 13 * WORD_SIZE( sp ) /* a6 */\r
+ lw x17, 14 * WORD_SIZE( sp ) /* a7 */\r
+ lw x18, 15 * WORD_SIZE( sp ) /* s2 */\r
+ lw x19, 16 * WORD_SIZE( sp ) /* s3 */\r
+ lw x20, 17 * WORD_SIZE( sp ) /* s4 */\r
+ lw x21, 18 * WORD_SIZE( sp ) /* s5 */\r
+ lw x22, 19 * WORD_SIZE( sp ) /* s6 */\r
+ lw x23, 20 * WORD_SIZE( sp ) /* s7 */\r
+ lw x24, 21 * WORD_SIZE( sp ) /* s8 */\r
+ lw x25, 22 * WORD_SIZE( sp ) /* s9 */\r
+ lw x26, 23 * WORD_SIZE( sp ) /* s10 */\r
+ lw x27, 24 * WORD_SIZE( sp ) /* s11 */\r
+ lw x28, 25 * WORD_SIZE( sp ) /* t3 */\r
+ lw x29, 26 * WORD_SIZE( sp ) /* t4 */\r
+ lw x30, 27 * WORD_SIZE( sp ) /* t5 */\r
+ lw x31, 28 * WORD_SIZE( sp ) /* t6 */\r
+ addi sp, sp, CONTEXT_SIZE\r
+ csrs mstatus, 8 /* Enable machine interrupts. */\r
+ csrs mie, 8 /* Enable soft interrupt. */\r
+ ret\r
\r
/*-----------------------------------------------------------*/\r
\r
.align 8\r
vPortTrapHandler:\r
addi sp, sp, -CONTEXT_SIZE\r
- sw x1, 0( sp )\r
- sw x5, 1 * WORD_SIZE( sp )\r
- sw x6, 2 * WORD_SIZE( sp )\r
- sw x7, 3 * WORD_SIZE( sp )\r
- sw x8, 4 * WORD_SIZE( sp )\r
- sw x9, 5 * WORD_SIZE( sp )\r
- sw x10, 6 * WORD_SIZE( sp )\r
- sw x11, 7 * WORD_SIZE( sp )\r
- sw x12, 8 * WORD_SIZE( sp )\r
- sw x13, 9 * WORD_SIZE( sp )\r
- sw x14, 10 * WORD_SIZE( sp )\r
- sw x15, 11 * WORD_SIZE( sp )\r
- sw x16, 12 * WORD_SIZE( sp )\r
- sw x17, 13 * WORD_SIZE( sp )\r
- sw x18, 14 * WORD_SIZE( sp )\r
- sw x19, 15 * WORD_SIZE( sp )\r
- sw x20, 16 * WORD_SIZE( sp )\r
- sw x21, 17 * WORD_SIZE( sp )\r
- sw x22, 18 * WORD_SIZE( sp )\r
- sw x23, 19 * WORD_SIZE( sp )\r
- sw x24, 20 * WORD_SIZE( sp )\r
- sw x25, 21 * WORD_SIZE( sp )\r
- sw x26, 22 * WORD_SIZE( sp )\r
- sw x27, 23 * WORD_SIZE( sp )\r
- sw x28, 24 * WORD_SIZE( sp )\r
- sw x29, 25 * WORD_SIZE( sp )\r
- sw x30, 26 * WORD_SIZE( sp )\r
- sw x31, 27 * WORD_SIZE( sp )\r
+ sw x1, 1( sp )\r
+ sw x5, 2 * WORD_SIZE( sp )\r
+ sw x6, 3 * WORD_SIZE( sp )\r
+ sw x7, 4 * WORD_SIZE( sp )\r
+ sw x8, 5 * WORD_SIZE( sp )\r
+ sw x9, 6 * WORD_SIZE( sp )\r
+ sw x10, 7 * WORD_SIZE( sp )\r
+ sw x11, 8 * WORD_SIZE( sp )\r
+ sw x12, 9 * WORD_SIZE( sp )\r
+ sw x13, 10 * WORD_SIZE( sp )\r
+ sw x14, 11 * WORD_SIZE( sp )\r
+ sw x15, 12 * WORD_SIZE( sp )\r
+ sw x16, 13 * WORD_SIZE( sp )\r
+ sw x17, 14 * WORD_SIZE( sp )\r
+ sw x18, 15 * WORD_SIZE( sp )\r
+ sw x19, 16 * WORD_SIZE( sp )\r
+ sw x20, 17 * WORD_SIZE( sp )\r
+ sw x21, 18 * WORD_SIZE( sp )\r
+ sw x22, 19 * WORD_SIZE( sp )\r
+ sw x23, 20 * WORD_SIZE( sp )\r
+ sw x24, 21 * WORD_SIZE( sp )\r
+ sw x25, 22 * WORD_SIZE( sp )\r
+ sw x26, 23 * WORD_SIZE( sp )\r
+ sw x27, 24 * WORD_SIZE( sp )\r
+ sw x28, 25 * WORD_SIZE( sp )\r
+ sw x29, 26 * WORD_SIZE( sp )\r
+ sw x30, 27 * WORD_SIZE( sp )\r
+ sw x31, 28 * WORD_SIZE( sp )\r
+\r
+ /* Save exception return address. */\r
+ csrr t0, mepc\r
+ sw t0, 0( sp )\r
+\r
lw t0, pxCurrentTCB /* Load pxCurrentTCB. */\r
sw sp, 0( t0 ) /* Write sp from first TCB member. */\r
\r
- csrr t0, mepc\r
- sw t0, 31 * WORD_SIZE( sp )\r
-\r
csrr a0, mcause\r
csrr a1, mepc\r
mv a2, sp\r
li t0, 0x00001800 /* MSTATUS MPP */\r
csrs mstatus, t0\r
\r
- /* Cut and past restore code from xPortStartScheduler - can be made a macro\r
- but that makes debugging harder. */\r
- lw sp, pxCurrentTCB /* Load pxCurrentTCB. */\r
- lw sp, 0( sp ) /* Read sp from first TCB member. */\r
- lw x1, 0( sp )\r
- lw x5, 1 * WORD_SIZE( sp ) /* t0 */\r
- lw x6, 2 * WORD_SIZE( sp ) /* t1 */\r
- lw x7, 3 * WORD_SIZE( sp ) /* t2 */\r
- lw x8, 4 * WORD_SIZE( sp ) /* s0/fp */\r
- lw x9, 5 * WORD_SIZE( sp ) /* s1 */\r
- lw x10, 6 * WORD_SIZE( sp ) /* a0 */\r
- lw x11, 7 * WORD_SIZE( sp ) /* a1 */\r
- lw x12, 8 * WORD_SIZE( sp ) /* a2 */\r
- lw x13, 9 * WORD_SIZE( sp ) /* a3 */\r
- lw x14, 10 * WORD_SIZE( sp ) /* a4 */\r
- lw x15, 11 * WORD_SIZE( sp ) /* a5 */\r
- lw x16, 12 * WORD_SIZE( sp ) /* a6 */\r
- lw x17, 13 * WORD_SIZE( sp ) /* a7 */\r
- lw x18, 14 * WORD_SIZE( sp ) /* s2 */\r
- lw x19, 15 * WORD_SIZE( sp ) /* s3 */\r
- lw x20, 16 * WORD_SIZE( sp ) /* s4 */\r
- lw x21, 17 * WORD_SIZE( sp ) /* s5 */\r
- lw x22, 18 * WORD_SIZE( sp ) /* s6 */\r
- lw x23, 19 * WORD_SIZE( sp ) /* s7 */\r
- lw x24, 20 * WORD_SIZE( sp ) /* s8 */\r
- lw x25, 21 * WORD_SIZE( sp ) /* s9 */\r
- lw x26, 22 * WORD_SIZE( sp ) /* s10 */\r
- lw x27, 23 * WORD_SIZE( sp ) /* s11 */\r
- lw x28, 24 * WORD_SIZE( sp ) /* t3 */\r
- lw x29, 25 * WORD_SIZE( sp ) /* t4 */\r
- lw x30, 26 * WORD_SIZE( sp ) /* t5 */\r
- lw x31, 27 * WORD_SIZE( sp ) /* t6 */\r
- addi sp, sp, CONTEXT_SIZE\r
- mret\r
+ lw sp, pxCurrentTCB /* Load pxCurrentTCB. */\r
+ lw sp, 0( sp ) /* Read sp from first TCB member. */\r
+\r
+ /* Load mret with the address of the first task. */\r
+ lw t0, 0( sp )\r
+ csrw mepc, t0\r
+\r
+ lw x1, 1( sp )\r
+ lw x5, 2 * WORD_SIZE( sp ) /* t0 */\r
+ lw x6, 3 * WORD_SIZE( sp ) /* t1 */\r
+ lw x7, 4 * WORD_SIZE( sp ) /* t2 */\r
+ lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */\r
+ lw x9, 6 * WORD_SIZE( sp ) /* s1 */\r
+ lw x10, 7 * WORD_SIZE( sp ) /* a0 */\r
+ lw x11, 8 * WORD_SIZE( sp ) /* a1 */\r
+ lw x12, 9 * WORD_SIZE( sp ) /* a2 */\r
+ lw x13, 10 * WORD_SIZE( sp ) /* a3 */\r
+ lw x14, 11 * WORD_SIZE( sp ) /* a4 */\r
+ lw x15, 12 * WORD_SIZE( sp ) /* a5 */\r
+ lw x16, 13 * WORD_SIZE( sp ) /* a6 */\r
+ lw x17, 14 * WORD_SIZE( sp ) /* a7 */\r
+ lw x18, 15 * WORD_SIZE( sp ) /* s2 */\r
+ lw x19, 16 * WORD_SIZE( sp ) /* s3 */\r
+ lw x20, 17 * WORD_SIZE( sp ) /* s4 */\r
+ lw x21, 18 * WORD_SIZE( sp ) /* s5 */\r
+ lw x22, 19 * WORD_SIZE( sp ) /* s6 */\r
+ lw x23, 20 * WORD_SIZE( sp ) /* s7 */\r
+ lw x24, 21 * WORD_SIZE( sp ) /* s8 */\r
+ lw x25, 22 * WORD_SIZE( sp ) /* s9 */\r
+ lw x26, 23 * WORD_SIZE( sp ) /* s10 */\r
+ lw x27, 24 * WORD_SIZE( sp ) /* s11 */\r
+ lw x28, 25 * WORD_SIZE( sp ) /* t3 */\r
+ lw x29, 26 * WORD_SIZE( sp ) /* t4 */\r
+ lw x30, 27 * WORD_SIZE( sp ) /* t5 */\r
+ lw x31, 28 * WORD_SIZE( sp ) /* t6 */\r
+ addi sp, sp, CONTEXT_SIZE\r
+\r
+ mret\r
+\r
+\r