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(__i386__) 16 17#define MARL_BUILD_ASM 1 18#include "osfiber_asm_x86.h" 19 20// void marl_fiber_swap(marl_fiber_context* from, const marl_fiber_context* to) 21// esp+4: from 22// esp+8: to 23.text 24.global marl_fiber_swap 25.align 4 26marl_fiber_swap: 27 // Save context 'from' 28 movl 4(%esp), %eax 29 30 // Store callee-preserved registers 31 movl %ebx, MARL_REG_EBX(%eax) 32 movl %ebp, MARL_REG_EBP(%eax) 33 movl %esi, MARL_REG_ESI(%eax) 34 movl %edi, MARL_REG_EDI(%eax) 35 36 movl (%esp), %ecx /* call stores the return address on the stack before jumping */ 37 movl %ecx, MARL_REG_EIP(%eax) 38 lea 4(%esp), %ecx /* skip the pushed return address */ 39 movl %ecx, MARL_REG_ESP(%eax) 40 41 // Load context 'to' 42 movl 8(%esp), %ecx 43 44 // Load callee-preserved registers 45 movl MARL_REG_EBX(%ecx), %ebx 46 movl MARL_REG_EBP(%ecx), %ebp 47 movl MARL_REG_ESI(%ecx), %esi 48 movl MARL_REG_EDI(%ecx), %edi 49 50 // Load stack pointer 51 movl MARL_REG_ESP(%ecx), %esp 52 53 // Load instruction pointer, and jump 54 movl MARL_REG_EIP(%ecx), %ecx 55 jmp *%ecx 56 57#endif // defined(__i386__) 58