1// Copyright 2019 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(__x86_64__)
16
17#define MARL_BUILD_ASM 1
18#include "osfiber_asm_x64.h"
19
20// void marl_fiber_swap(marl_fiber_context* from, const marl_fiber_context* to)
21// rdi: from
22// rsi: to
23.text
24.global MARL_ASM_SYMBOL(marl_fiber_swap)
25.align 4
26MARL_ASM_SYMBOL(marl_fiber_swap):
27
28 // Save context 'from'
29
30 // Store callee-preserved registers
31 movq %rbx, MARL_REG_RBX(%rdi)
32 movq %rbp, MARL_REG_RBP(%rdi)
33 movq %r12, MARL_REG_R12(%rdi)
34 movq %r13, MARL_REG_R13(%rdi)
35 movq %r14, MARL_REG_R14(%rdi)
36 movq %r15, MARL_REG_R15(%rdi)
37
38 movq (%rsp), %rcx /* call stores the return address on the stack before jumping */
39 movq %rcx, MARL_REG_RIP(%rdi)
40 leaq 8(%rsp), %rcx /* skip the pushed return address */
41 movq %rcx, MARL_REG_RSP(%rdi)
42
43 // Load context 'to'
44 movq %rsi, %r8
45
46 // Load callee-preserved registers
47 movq MARL_REG_RBX(%r8), %rbx
48 movq MARL_REG_RBP(%r8), %rbp
49 movq MARL_REG_R12(%r8), %r12
50 movq MARL_REG_R13(%r8), %r13
51 movq MARL_REG_R14(%r8), %r14
52 movq MARL_REG_R15(%r8), %r15
53
54 // Load first two call parameters
55 movq MARL_REG_RDI(%r8), %rdi
56 movq MARL_REG_RSI(%r8), %rsi
57
58 // Load stack pointer
59 movq MARL_REG_RSP(%r8), %rsp
60
61 // Load instruction pointer, and jump
62 movq MARL_REG_RIP(%r8), %rcx
63 jmp *%rcx
64
65#endif // defined(__x86_64__)
66