| 1 |
/* AArch64 support code for fibers and multithreading. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#include "../common/threadasm.S" |
| 26 |
|
| 27 |
/** |
| 28 |
* preserve/restore AAPCS64 registers |
| 29 |
* x19-x28 5.1.1 64-bit callee saved |
| 30 |
* x29 fp, or possibly callee saved reg - depends on platform choice 5.2.3) |
| 31 |
* x30 lr |
| 32 |
* d8-d15 5.1.2 says callee only must save bottom 64-bits (the "d" regs) |
| 33 |
* |
| 34 |
* saved regs on stack will look like: |
| 35 |
* 19: x19 |
| 36 |
* 18: x20 |
| 37 |
* ... |
| 38 |
* 10: x28 |
| 39 |
* 9: x29 (fp) <-- oldp / *newp save stack top |
| 40 |
* 8: x30 (lr) |
| 41 |
* 7: d8 |
| 42 |
* ... |
| 43 |
* 0: d15 <-- sp |
| 44 |
*/ |
| 45 |
.text |
| 46 |
.global CSYM(fiber_switchContext) |
| 47 |
#ifndef __APPLE__ |
| 48 |
.type CSYM(fiber_switchContext), %function |
| 49 |
#endif |
| 50 |
.align 4 |
| 51 |
CSYM(fiber_switchContext): |
| 52 |
.cfi_startproc |
| 53 |
stp d15, d14, [sp, #-20*8]! |
| 54 |
stp d13, d12, [sp, #2*8] |
| 55 |
stp d11, d10, [sp, #4*8] |
| 56 |
stp d9, d8, [sp, #6*8] |
| 57 |
stp x30, x29, [sp, #8*8] // lr, fp |
| 58 |
stp x28, x27, [sp, #10*8] |
| 59 |
stp x26, x25, [sp, #12*8] |
| 60 |
stp x24, x23, [sp, #14*8] |
| 61 |
stp x22, x21, [sp, #16*8] |
| 62 |
stp x20, x19, [sp, #18*8] |
| 63 |
|
| 64 |
// oldp is set above saved lr (x30) to hide it and float regs |
| 65 |
// from GC |
| 66 |
add x19, sp, #9*8 |
| 67 |
str x19, [x0] // *oldp tstack |
| 68 |
sub sp, x1, #9*8 // switch to newp sp |
| 69 |
|
| 70 |
ldp x20, x19, [sp, #18*8] |
| 71 |
ldp x22, x21, [sp, #16*8] |
| 72 |
ldp x24, x23, [sp, #14*8] |
| 73 |
ldp x26, x25, [sp, #12*8] |
| 74 |
ldp x28, x27, [sp, #10*8] |
| 75 |
ldp x30, x29, [sp, #8*8] // lr, fp |
| 76 |
ldp d9, d8, [sp, #6*8] |
| 77 |
ldp d11, d10, [sp, #4*8] |
| 78 |
ldp d13, d12, [sp, #2*8] |
| 79 |
ldp d15, d14, [sp], #20*8 |
| 80 |
ret |
| 81 |
.cfi_endproc |
| 82 |
#ifndef __APPLE__ |
| 83 |
.size CSYM(fiber_switchContext),.-CSYM(fiber_switchContext) |
| 84 |
#endif |
| 85 |
/** |
| 86 |
* When generating any kind of backtrace (gdb, exception handling) for |
| 87 |
* a function called in a Fiber, we need to tell the unwinder to stop |
| 88 |
* at our Fiber main entry point, i.e. we need to mark the bottom of |
| 89 |
* the call stack. This can be done by clearing the link register lr |
| 90 |
* prior to calling fiber_entryPoint (i.e. in fiber_switchContext) or |
| 91 |
* using a .cfi_undefined directive for the link register in the |
| 92 |
* Fiber entry point. cfi_undefined seems to yield better results in gdb. |
| 93 |
* Unfortunately we can't place it into fiber_entryPoint using inline |
| 94 |
* asm, so we use this trampoline instead. |
| 95 |
*/ |
| 96 |
.text |
| 97 |
.global CSYM(fiber_trampoline) |
| 98 |
.p2align 2 |
| 99 |
#ifndef __APPLE__ |
| 100 |
.type CSYM(fiber_trampoline), %function |
| 101 |
#endif |
| 102 |
CSYM(fiber_trampoline): |
| 103 |
.cfi_startproc |
| 104 |
.cfi_undefined x30 |
| 105 |
// fiber_entryPoint never returns |
| 106 |
bl CSYM(fiber_entryPoint) |
| 107 |
.cfi_endproc |
| 108 |
#ifndef __APPLE__ |
| 109 |
.size CSYM(fiber_trampoline),.-CSYM(fiber_trampoline) |
| 110 |
#endif |