1// Copyright 2022 The Marl Authors.
2//
3// Licensed under the Apache License. Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#if defined(__loongarch_lp64)
16
17#define MARL_BUILD_ASM 1
18#include "osfiber_asm_loongarch64.h"
19
20// void marl_fiber_swap(marl_fiber_context* from, const marl_fiber_context* to)
21// a0: from
22// a1: to
23.text
24.global marl_fiber_swap
25.align 4
26marl_fiber_swap:
27
28 // Save context 'from'
29
30 // Store callee-preserved registers
31 st.d $s0, $a0, MARL_REG_s0
32 st.d $s1, $a0, MARL_REG_s1
33 st.d $s2, $a0, MARL_REG_s2
34 st.d $s3, $a0, MARL_REG_s3
35 st.d $s4, $a0, MARL_REG_s4
36 st.d $s5, $a0, MARL_REG_s5
37 st.d $s6, $a0, MARL_REG_s6
38 st.d $s7, $a0, MARL_REG_s7
39 st.d $s8, $a0, MARL_REG_s8
40
41 fst.d $fs0, $a0, MARL_REG_fs0
42 fst.d $fs1, $a0, MARL_REG_fs1
43 fst.d $fs2, $a0, MARL_REG_fs2
44 fst.d $fs3, $a0, MARL_REG_fs3
45 fst.d $fs4, $a0, MARL_REG_fs4
46 fst.d $fs5, $a0, MARL_REG_fs5
47 fst.d $fs6, $a0, MARL_REG_fs6
48 fst.d $fs7, $a0, MARL_REG_fs7
49
50 st.d $ra, $a0, MARL_REG_ra
51 st.d $sp, $a0, MARL_REG_sp
52 st.d $fp, $a0, MARL_REG_fp
53
54 // Recover callee-preserved registers
55 ld.d $s0, $a1, MARL_REG_s0
56 ld.d $s1, $a1, MARL_REG_s1
57 ld.d $s2, $a1, MARL_REG_s2
58 ld.d $s3, $a1, MARL_REG_s3
59 ld.d $s4, $a1, MARL_REG_s4
60 ld.d $s5, $a1, MARL_REG_s5
61 ld.d $s6, $a1, MARL_REG_s6
62 ld.d $s7, $a1, MARL_REG_s7
63 ld.d $s8, $a1, MARL_REG_s8
64
65 fld.d $fs0, $a1, MARL_REG_fs0
66 fld.d $fs1, $a1, MARL_REG_fs1
67 fld.d $fs2, $a1, MARL_REG_fs2
68 fld.d $fs3, $a1, MARL_REG_fs3
69 fld.d $fs4, $a1, MARL_REG_fs4
70 fld.d $fs5, $a1, MARL_REG_fs5
71 fld.d $fs6, $a1, MARL_REG_fs6
72 fld.d $fs7, $a1, MARL_REG_fs7
73
74 ld.d $ra, $a1, MARL_REG_ra
75 ld.d $sp, $a1, MARL_REG_sp
76 ld.d $fp, $a1, MARL_REG_fp
77
78 // Recover arguments
79 ld.d $a0, $a1, MARL_REG_a0
80 ld.d $a1, $a1, MARL_REG_a1
81
82 jr $ra // Jump to the trampoline
83
84#endif // defined(__loongarch_lp64)
85