| 1 |
/* LoongArch support code for fibers and multithreading. |
| 2 |
Copyright (C) 2023-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 |
* Performs a context switch. |
| 29 |
* |
| 30 |
* $a0 - void** - ptr to old stack pointer |
| 31 |
* $a1 - void* - new stack pointer |
| 32 |
* |
| 33 |
*/ |
| 34 |
|
| 35 |
#if defined(__loongarch_lp64) |
| 36 |
# define GPR_L ld.d |
| 37 |
# define GPR_S st.d |
| 38 |
# define SZ_GPR 8 |
| 39 |
# define ADDSP(si) addi.d $sp, $sp, si |
| 40 |
#elif defined(__loongarch64_ilp32) |
| 41 |
# define GPR_L ld.w |
| 42 |
# define GPR_S st.w |
| 43 |
# define SZ_GPR 4 |
| 44 |
# define ADDSP(si) addi.w $sp, $sp, si |
| 45 |
#else |
| 46 |
# error Unsupported GPR size (must be 64-bit or 32-bit). |
| 47 |
#endif |
| 48 |
|
| 49 |
#if defined(__loongarch_double_float) |
| 50 |
# define FPR_L fld.d |
| 51 |
# define FPR_S fst.d |
| 52 |
# define SZ_FPR 8 |
| 53 |
#elif defined(__loongarch_single_float) |
| 54 |
# define FPR_L fld.s |
| 55 |
# define FPR_S fst.s |
| 56 |
# define SZ_FPR 4 |
| 57 |
#else |
| 58 |
# define SZ_FPR 0 |
| 59 |
#endif |
| 60 |
|
| 61 |
.text |
| 62 |
.align 2 |
| 63 |
.global fiber_switchContext |
| 64 |
.type fiber_switchContext, @function |
| 65 |
fiber_switchContext: |
| 66 |
.cfi_startproc |
| 67 |
ADDSP(-11 * SZ_GPR) |
| 68 |
|
| 69 |
// fp regs and return address are stored below the stack |
| 70 |
// because we don't want the GC to scan them. |
| 71 |
|
| 72 |
// return address (r1) |
| 73 |
GPR_S $r1, $sp, -SZ_GPR |
| 74 |
|
| 75 |
#if SZ_FPR != 0 |
| 76 |
// callee-saved scratch FPRs (f24-f31) |
| 77 |
FPR_S $f24, $sp, -SZ_GPR-1*SZ_FPR |
| 78 |
FPR_S $f25, $sp, -SZ_GPR-2*SZ_FPR |
| 79 |
FPR_S $f26, $sp, -SZ_GPR-3*SZ_FPR |
| 80 |
FPR_S $f27, $sp, -SZ_GPR-4*SZ_FPR |
| 81 |
FPR_S $f28, $sp, -SZ_GPR-5*SZ_FPR |
| 82 |
FPR_S $f29, $sp, -SZ_GPR-6*SZ_FPR |
| 83 |
FPR_S $f30, $sp, -SZ_GPR-7*SZ_FPR |
| 84 |
FPR_S $f31, $sp, -SZ_GPR-8*SZ_FPR |
| 85 |
#endif |
| 86 |
|
| 87 |
// callee-saved GPRs (r21, fp (r22), r23-r31) |
| 88 |
GPR_S $r21, $sp, 0*SZ_GPR |
| 89 |
GPR_S $fp, $sp, 1*SZ_GPR |
| 90 |
GPR_S $s0, $sp, 2*SZ_GPR |
| 91 |
GPR_S $s1, $sp, 3*SZ_GPR |
| 92 |
GPR_S $s2, $sp, 4*SZ_GPR |
| 93 |
GPR_S $s3, $sp, 5*SZ_GPR |
| 94 |
GPR_S $s4, $sp, 6*SZ_GPR |
| 95 |
GPR_S $s5, $sp, 7*SZ_GPR |
| 96 |
GPR_S $s6, $sp, 8*SZ_GPR |
| 97 |
GPR_S $s7, $sp, 9*SZ_GPR |
| 98 |
GPR_S $s8, $sp, 10*SZ_GPR |
| 99 |
|
| 100 |
// swap stack pointer |
| 101 |
GPR_S $sp, $a0, 0 |
| 102 |
move $sp, $a1 |
| 103 |
|
| 104 |
GPR_L $r1, $sp, -SZ_GPR |
| 105 |
|
| 106 |
#if SZ_FPR != 0 |
| 107 |
FPR_L $f24, $sp, -SZ_GPR-1*SZ_FPR |
| 108 |
FPR_L $f25, $sp, -SZ_GPR-2*SZ_FPR |
| 109 |
FPR_L $f26, $sp, -SZ_GPR-3*SZ_FPR |
| 110 |
FPR_L $f27, $sp, -SZ_GPR-4*SZ_FPR |
| 111 |
FPR_L $f28, $sp, -SZ_GPR-5*SZ_FPR |
| 112 |
FPR_L $f29, $sp, -SZ_GPR-6*SZ_FPR |
| 113 |
FPR_L $f30, $sp, -SZ_GPR-7*SZ_FPR |
| 114 |
FPR_L $f31, $sp, -SZ_GPR-8*SZ_FPR |
| 115 |
#endif |
| 116 |
|
| 117 |
GPR_L $r21, $sp, 0*SZ_GPR |
| 118 |
GPR_L $fp, $sp, 1*SZ_GPR |
| 119 |
GPR_L $s0, $sp, 2*SZ_GPR |
| 120 |
GPR_L $s1, $sp, 3*SZ_GPR |
| 121 |
GPR_L $s2, $sp, 4*SZ_GPR |
| 122 |
GPR_L $s3, $sp, 5*SZ_GPR |
| 123 |
GPR_L $s4, $sp, 6*SZ_GPR |
| 124 |
GPR_L $s5, $sp, 7*SZ_GPR |
| 125 |
GPR_L $s6, $sp, 8*SZ_GPR |
| 126 |
GPR_L $s7, $sp, 9*SZ_GPR |
| 127 |
GPR_L $s8, $sp, 10*SZ_GPR |
| 128 |
|
| 129 |
ADDSP(11 * SZ_GPR) |
| 130 |
|
| 131 |
jr $r1 // return |
| 132 |
.cfi_endproc |
| 133 |
.size fiber_switchContext,.-fiber_switchContext |