1// Copyright 2021 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(__riscv) && __riscv_xlen == 64 16 17#define MARL_BUILD_ASM 1 18#include "osfiber_asm_rv64.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 sd s0, MARL_REG_s0(a0) 32 sd s1, MARL_REG_s1(a0) 33 sd s2, MARL_REG_s2(a0) 34 sd s3, MARL_REG_s3(a0) 35 sd s4, MARL_REG_s4(a0) 36 sd s5, MARL_REG_s5(a0) 37 sd s6, MARL_REG_s6(a0) 38 sd s7, MARL_REG_s7(a0) 39 sd s8, MARL_REG_s8(a0) 40 sd s9, MARL_REG_s9(a0) 41 sd s10, MARL_REG_s10(a0) 42 sd s11, MARL_REG_s11(a0) 43 44 fsd fs0, MARL_REG_fs0(a0) 45 fsd fs1, MARL_REG_fs1(a0) 46 fsd fs2, MARL_REG_fs2(a0) 47 fsd fs3, MARL_REG_fs3(a0) 48 fsd fs4, MARL_REG_fs4(a0) 49 fsd fs5, MARL_REG_fs5(a0) 50 fsd fs6, MARL_REG_fs6(a0) 51 fsd fs7, MARL_REG_fs7(a0) 52 fsd fs8, MARL_REG_fs8(a0) 53 fsd fs9, MARL_REG_fs9(a0) 54 fsd fs10, MARL_REG_fs10(a0) 55 fsd fs11, MARL_REG_fs11(a0) 56 57 sd sp, MARL_REG_sp(a0) 58 // On RISC-V ra is caller-saved 59 // but we need ra to jump to the trampoline 60 sd ra, MARL_REG_ra(a0) 61 62 move t0, a1 // Store a1 in temporary register 63 64 // Recover callee-preserved registers 65 ld s0, MARL_REG_s0(t0) 66 ld s1, MARL_REG_s1(t0) 67 ld s2, MARL_REG_s2(t0) 68 ld s3, MARL_REG_s3(t0) 69 ld s4, MARL_REG_s4(t0) 70 ld s5, MARL_REG_s5(t0) 71 ld s6, MARL_REG_s6(t0) 72 ld s7, MARL_REG_s7(t0) 73 ld s8, MARL_REG_s8(t0) 74 ld s9, MARL_REG_s9(t0) 75 ld s10, MARL_REG_s10(t0) 76 ld s11, MARL_REG_s11(t0) 77 78 fld fs0, MARL_REG_fs0(t0) 79 fld fs1, MARL_REG_fs1(t0) 80 fld fs2, MARL_REG_fs2(t0) 81 fld fs3, MARL_REG_fs3(t0) 82 fld fs4, MARL_REG_fs4(t0) 83 fld fs5, MARL_REG_fs5(t0) 84 fld fs6, MARL_REG_fs6(t0) 85 fld fs7, MARL_REG_fs7(t0) 86 fld fs8, MARL_REG_fs8(t0) 87 fld fs9, MARL_REG_fs9(t0) 88 fld fs10, MARL_REG_fs10(t0) 89 fld fs11, MARL_REG_fs11(t0) 90 91 ld sp, MARL_REG_sp(t0) 92 ld ra, MARL_REG_ra(t0) 93 94 // Recover arguments 95 ld a0, MARL_REG_a0(t0) 96 ld a1, MARL_REG_a1(t0) 97 98 jr ra // Jump to the trampoline 99 100#endif // defined(__riscv) && __riscv_xlen == 64 101