|
| 1 | +.section .text.tinygo_startTask,"ax",@progbits |
| 2 | +.global tinygo_startTask |
| 3 | +.type tinygo_startTask, %function |
| 4 | +tinygo_startTask: |
| 5 | + // Small assembly stub for starting a goroutine. This already runs on the |
| 6 | + // new stack, control reaches this function after returning from the initial |
| 7 | + // tinygo_swapTask below (the retw.n instruction). |
| 8 | + // |
| 9 | + // The stack was set up in such a way that it looks as if this function was |
| 10 | + // paused using tinygo_swapTask by setting up the parent register window and |
| 11 | + // return pointer as a call4 instruction - except such a call never took |
| 12 | + // place. Instead, the stack pointer is switched to the new stack after all |
| 13 | + // live-but-invisible registers have been flushed to the stack. This means |
| 14 | + // that all registers as present in tinygo_swapTask are moved four up (a2 in |
| 15 | + // tinygo_swapTask is a6 in this function). We don't use any of those |
| 16 | + // registers however. Instead, the retw.n instruction will load them through |
| 17 | + // an underflow exception from the stack which means we get a0-a3 as defined |
| 18 | + // in task_stack_esp32.go. |
| 19 | + |
| 20 | + // Branch to the "goroutine start" function. The first (and only) parameter |
| 21 | + // is stored in a2, but has to be moved to a6 to make it appear as a2 in the |
| 22 | + // goroutine start function (due to changing the register window by four |
| 23 | + // with callx4). |
| 24 | + mov.n a6, a2 |
| 25 | + callx4 a3 |
| 26 | + |
| 27 | + // After return, exit this goroutine. This call never returns. |
| 28 | + call4 tinygo_pause |
| 29 | + |
| 30 | +.section .text.tinygo_swapTask,"ax",@progbits |
| 31 | +.global tinygo_swapTask |
| 32 | +.type tinygo_swapTask, %function |
| 33 | +tinygo_swapTask: |
| 34 | + // This function gets the following parameters: |
| 35 | + // a2 = newStack uintptr |
| 36 | + // a3 = oldStack *uintptr |
| 37 | + |
| 38 | + // Reserve 32 bytes on the stack. It really needs to be 32 bytes, with 16 |
| 39 | + // extra at the bottom to adhere to the ABI. |
| 40 | + entry sp, 32 |
| 41 | + |
| 42 | + // Disable interrupts while flushing registers. This is necessary because |
| 43 | + // interrupts might want to use the stack pointer (at a2) which will be some |
| 44 | + // arbitrary register while registers are flushed. |
| 45 | + rsil a4, 3 // XCHAL_EXCM_LEVEL |
| 46 | + |
| 47 | + // Flush all unsaved registers to the stack. |
| 48 | + // This trick has been borrowed from the Zephyr project: |
| 49 | + // https://github.com/zephyrproject-rtos/zephyr/blob/d79b003758/arch/xtensa/include/xtensa-asm2-s.h#L17 |
| 50 | + and a12, a12, a12 |
| 51 | + rotw 3 |
| 52 | + and a12, a12, a12 |
| 53 | + rotw 3 |
| 54 | + and a12, a12, a12 |
| 55 | + rotw 3 |
| 56 | + and a12, a12, a12 |
| 57 | + rotw 3 |
| 58 | + and a12, a12, a12 |
| 59 | + rotw 4 |
| 60 | + |
| 61 | + // Restore interrupts. |
| 62 | + wsr.ps a4 |
| 63 | + |
| 64 | + // At this point, the following is true: |
| 65 | + // WindowStart == 1 << WindowBase |
| 66 | + // Therefore, we don't need to do this manually. |
| 67 | + // It also means that the stack pointer can now be safely modified. |
| 68 | + |
| 69 | + // Save a0, which stores the return address and the parent register window |
| 70 | + // in the upper two bits. |
| 71 | + s32i.n a0, sp, 0 |
| 72 | + |
| 73 | + // Save the current stack pointer in oldStack. |
| 74 | + s32i.n sp, a3, 0 |
| 75 | + |
| 76 | + // Switch to the new stack pointer (newStack). |
| 77 | + mov.n sp, a2 |
| 78 | + |
| 79 | + // Load a0, which is the previous return addres from before the previous |
| 80 | + // switch or the constructed return address to tinygo_startTask. This |
| 81 | + // register also stores the parent register window. |
| 82 | + l32i.n a0, sp, 0 |
| 83 | + |
| 84 | + // Return into the new stack. This instruction will trigger a window |
| 85 | + // underflow, reloading the saved registers from the stack. |
| 86 | + retw.n |
0 commit comments