|
| 1 | +# |
| 2 | +# Context switch for ARM64/macOS (aarch64). (Position-independent code.) |
| 3 | +# Jafar Al-Gharaibeh, Nov 2016. |
| 4 | +# Ian McLinden, Feb 2025. |
| 5 | +# |
| 6 | +# |
| 7 | + |
| 8 | +.data |
| 9 | +.ERR: .ascii "new_context() returned in coswitch" |
| 10 | + |
| 11 | +.text |
| 12 | + .p2align 4 ; 16 byte word boundary |
| 13 | + .file "rswitch.s" |
| 14 | + .text |
| 15 | + .globl _coswitch, _syserr |
| 16 | + |
| 17 | +_coswitch: |
| 18 | + # _coswitch(old_cstate, new_cstate, first) |
| 19 | + # |
| 20 | + # x0 old_cstate |
| 21 | + # x1 new_cstate |
| 22 | + # x2 first (equals 0 if first activation) |
| 23 | + # |
| 24 | + # fp is x29 |
| 25 | + # lr is x30 , in arm32 r14 |
| 26 | + # sp , in arm32 r13 |
| 27 | + # pc , in arm32 r15 |
| 28 | + # |
| 29 | + # args : x0-x7 |
| 30 | + # r8 indirect location, c++? |
| 31 | + # Scratch : x9-x15 |
| 32 | + # Don't touch : x18 (platform register - reserved by Rosetta) |
| 33 | + # Callee-saved : x19-x28 (must be restored if used) |
| 34 | + # Must save : xr30, x31 |
| 35 | + # Others : ? |
| 36 | + # |
| 37 | + # Old stack pointer -> old_cstate[0] |
| 38 | + # Old link register -> old_cstate[1] |
| 39 | + mov x9, sp |
| 40 | + str x9, [x0] |
| 41 | + mov x9, x30 |
| 42 | + str x9, [x0, #8] |
| 43 | + # frame pointer |
| 44 | + str x29, [x0, #16] |
| 45 | + |
| 46 | + str x19, [x0, #24] |
| 47 | + str x20, [x0, #32] |
| 48 | + str x21, [x0, #40] |
| 49 | + str x22, [x0, #48] |
| 50 | + str x23, [x0, #56] |
| 51 | + str x24, [x0, #64] |
| 52 | + str x25, [x0, #72] |
| 53 | + str x26, [x0, #80] |
| 54 | + str x27, [x0, #88] |
| 55 | + str x28, [x0, #96] |
| 56 | + |
| 57 | + # Otherwise load the new state |
| 58 | + # new_cstate[0] -> new stack pointer |
| 59 | + # new_cstate[1] -> new link register |
| 60 | + ldr x9, [x1] |
| 61 | + mov sp, x9 |
| 62 | + ldr x9, [x1, #8] |
| 63 | + mov x30, x9 |
| 64 | + # frame pointer |
| 65 | + ldr x29, [x1, #16] |
| 66 | + |
| 67 | + ldr x19, [x1, #24] |
| 68 | + ldr x20, [x1, #32] |
| 69 | + ldr x21, [x1, #40] |
| 70 | + ldr x22, [x1, #48] |
| 71 | + ldr x23, [x1, #56] |
| 72 | + ldr x24, [x1, #64] |
| 73 | + ldr x25, [x1, #72] |
| 74 | + ldr x26, [x1, #80] |
| 75 | + ldr x27, [x1, #88] |
| 76 | + ldr x28, [x1, #96] |
| 77 | + |
| 78 | + |
| 79 | + # If this the first activation |
| 80 | + # skip to call new_context() |
| 81 | + cmp x2, #0 |
| 82 | + beq .L1 |
| 83 | + |
| 84 | + # Ready to return |
| 85 | + ret |
| 86 | + |
| 87 | +.L1: |
| 88 | + # Executed only once, first==0 |
| 89 | + # |
| 90 | + # Call new_context((int) 0, (ptr) 0) |
| 91 | + # set the arguments x0 and x1 to zeros |
| 92 | + mov x0, #0 |
| 93 | + mov x1, #0 |
| 94 | + bl _new_context |
| 95 | + # we should never get here, if we do call syserr(...) |
| 96 | + adrp x0, .ERR@PAGE |
| 97 | + add x0, x0, .ERR@PAGEOFF |
| 98 | + bl _syserr |
0 commit comments