1/* 2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 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 * http://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 16#include "runtime/fibers/arch/asm_macros.h" 17#include "runtime/fibers/arch/amd64/context_layout.h" 18 19#define GPR_O(reg) FCTX_GPR_OFFSET_BYTES_ ## reg 20#define FP_O(reg) FCTX_FP_OFFSET_BYTES_ ## reg 21#include "runtime/fibers/arch/amd64/helpers.S" 22 23/** 24 * arguments: 25 * rdi: uint8_t* ctx_memory_from 26 * rsi: uint8_t* ctx_memory_to 27 */ 28FUNCTION_START(SwitchContext) 29 CFI_STARTPROC 30 /*** save current context ***/ 31 STORE_CONTEXT ctx_baseaddr_reg=%rdi, tmp_reg=%rcx 32 33 /*** restore next context ***/ 34 // fp: env 35 leaq FP_O(FPENV)(%rsi), %rcx 36 // NB: unmasks FP exceptions 37 fldenv (%rcx) 38 // fp: csr 39 ldmxcsr FP_O(MXCSR)(%rsi) 40 41 // gp: callee-saved 42 CFI_DEF_CFA(%rsi, 0) 43 CFI_OFFSET(%rbx, GPR_O(RBX)) 44 movq GPR_O(RBX)(%rsi), %rbx 45 CFI_OFFSET(%rbp, GPR_O(RBP)) 46 movq GPR_O(RBP)(%rsi), %rbp 47 CFI_OFFSET(%r12, GPR_O(R12)) 48 movq GPR_O(R12)(%rsi), %r12 49 CFI_OFFSET(%r13, GPR_O(R13)) 50 movq GPR_O(R13)(%rsi), %r13 51 CFI_OFFSET(%r14, GPR_O(R14)) 52 movq GPR_O(R14)(%rsi), %r14 53 CFI_OFFSET(%r15, GPR_O(R15)) 54 movq GPR_O(R15)(%rsi), %r15 55 // gp: the arg register 56 CFI_OFFSET(%rdi, GPR_O(RDI)) 57 movq GPR_O(RDI)(%rsi), %rdi 58 // gp: stack pointer 59 CFI_OFFSET(%rsp, GPR_O(RSP)) 60 movq GPR_O(RSP)(%rsi), %rsp 61 // push next_context->RIP to restore it on return from SwitchContext 62 CFI_OFFSET(%rip, GPR_O(RIP)) 63 movq GPR_O(RIP)(%rsi), %rcx 64 pushq %rcx 65 66 CFI_ENDPROC 67 CFI_STARTPROC 68 xorq %rax, %rax 69 ret 70 CFI_ENDPROC 71FUNCTION_END(SwitchContext) 72 73#undef GPR_O 74#undef FP_O 75 76// we don't need executable stack. 77.section .note.GNU-stack,"",%progbits