Skip to content

Commit

Permalink
Update boost asm (#5291)
Browse files Browse the repository at this point in the history
* update boost asm

* Fix error
  • Loading branch information
NathanFreeman authored Apr 10, 2024
1 parent e824c86 commit ed3af5b
Show file tree
Hide file tree
Showing 24 changed files with 950 additions and 692 deletions.
7 changes: 7 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ EOF
[mips64*], [SW_CPU="mips64"],
[mips*], [SW_CPU="mips32"],
[riscv64*], [SW_CPU="riscv64"],
[loongarch64*], [SW_CPU="loongarch64"],
[
SW_USE_ASM_CONTEXT="no"
]
Expand Down Expand Up @@ -1160,6 +1161,12 @@ EOF
else
SW_USE_ASM_CONTEXT="no"
fi
elif test "$SW_CPU" = "loongarch64"; then
if test "$SW_OS" = "LINUX"; then
SW_CONTEXT_ASM_FILE="loongarch64_sysv_elf_gas.S"
else
SW_USE_ASM_CONTEXT="no"
fi
else
SW_USE_ASM_CONTEXT="no"
fi
Expand Down
20 changes: 18 additions & 2 deletions include/swoole_asm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ SW_EXTERN_C_BEGIN

typedef void *fcontext_t;

struct transfer_t {
fcontext_t fctx;
void * data;
};

#ifdef __GNUC__
#define SW_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
#else
Expand All @@ -41,8 +46,19 @@ typedef void *fcontext_t;
#define SW_INDIRECT_RETURN
#endif

intptr_t swoole_jump_fcontext(fcontext_t *ofc, fcontext_t nfc, intptr_t vp, bool preserve_fpu = false);
SW_INDIRECT_RETURN fcontext_t swoole_make_fcontext(void *sp, size_t size, void (*fn)(intptr_t));
#undef SWOOLE_CONTEXT_CALLDECL
#if (defined(i386) || defined(__i386__) || defined(__i386) \
|| defined(__i486__) || defined(__i586__) || defined(__i686__) \
|| defined(__X86__) || defined(_X86_) || defined(__THW_INTEL__) \
|| defined(__I86__) || defined(__INTEL__) || defined(__IA32__) \
|| defined(_M_IX86) || defined(_I86_)) && defined(BOOST_WINDOWS)
# define SWOOLE_CONTEXT_CALLDECL __cdecl
#else
# define SWOOLE_CONTEXT_CALLDECL
#endif

transfer_t SWOOLE_CONTEXT_CALLDECL swoole_jump_fcontext(fcontext_t const to, void * vp);
fcontext_t SWOOLE_CONTEXT_CALLDECL swoole_make_fcontext(void *stack, size_t stack_size, void (* fn)(transfer_t));

SW_EXTERN_C_END

Expand Down
5 changes: 5 additions & 0 deletions include/swoole_coroutine_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
typedef ucontext_t coroutine_context_t;
#elif defined(USE_ASM_CONTEXT)
typedef fcontext_t coroutine_context_t;
typedef transfer_t coroutine_transfer_t;
#endif

typedef std::function<void(void *)> CoroutineFunc;
Expand Down Expand Up @@ -89,7 +90,11 @@ class Context {
void *private_data_;
bool end_;

#if USE_UCONTEXT
static void context_func(void *arg);
#else
static void context_func(transfer_t arg);
#endif
};

} // namespace coroutine
Expand Down
17 changes: 13 additions & 4 deletions src/coroutine/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Context::Context(size_t stack_size, CoroutineFunc fn, void *private_data)
ctx_.uc_link = nullptr;
makecontext(&ctx_, (void (*)(void)) & context_func, 1, this);
#else
ctx_ = swoole_make_fcontext(sp, stack_size_, (void (*)(intptr_t)) & context_func);
ctx_ = swoole_make_fcontext(sp, stack_size_, (void (*)(transfer_t)) & context_func);
swap_ctx_ = nullptr;
#endif

Expand Down Expand Up @@ -123,7 +123,8 @@ bool Context::swap_in() {
#if USE_UCONTEXT
return 0 == swapcontext(&swap_ctx_, &ctx_);
#else
swoole_jump_fcontext(&swap_ctx_, ctx_, (intptr_t) this, true);
coroutine_transfer_t transfer_data = swoole_jump_fcontext(ctx_, (void *) this);
ctx_ = transfer_data.fctx;
return true;
#endif
}
Expand All @@ -132,13 +133,21 @@ bool Context::swap_out() {
#if USE_UCONTEXT
return 0 == swapcontext(&ctx_, &swap_ctx_);
#else
swoole_jump_fcontext(&ctx_, swap_ctx_, (intptr_t) this, true);
coroutine_transfer_t transfer_data = swoole_jump_fcontext(swap_ctx_, (void *) this);
swap_ctx_ = transfer_data.fctx;
return true;
#endif
}

void Context::context_func(void *arg) {
void Context::context_func(
#if USE_UCONTEXT
void *arg) {
auto *_this = (Context *) arg;
#else
transfer_t arg) {
auto *_this = (Context *) arg.data;
_this->swap_ctx_ = arg.fctx;
#endif
_this->fn_(_this->private_data_);
_this->end_ = true;
_this->swap_out();
Expand Down
3 changes: 3 additions & 0 deletions thirdparty/boost/asm/combined.S
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#elif defined(__arm64__)
#include "make_arm64_aapcs_elf_gas.S"
#include "jump_arm64_aapcs_elf_gas.S"
#elif defined(__loongarch64)
#include "make_loongarch64_sysv_elf_gas.S"
#include "jump_loongarch64_sysv_elf_gas.S"
#else
#error "No arch's"
#endif
Expand Down
37 changes: 9 additions & 28 deletions thirdparty/boost/asm/jump_arm64_aapcs_elf_gas.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Edward Nevill 2015
Copyright Edward Nevill + Oliver Kowalke 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
Expand Down Expand Up @@ -51,7 +51,7 @@
* *
*******************************************************/

.cpu generic+fp+simd
.file "jump_arm64_aapcs_elf_gas.S"
.text
.align 2
.global swoole_jump_fcontext
Expand All @@ -60,23 +60,12 @@ swoole_jump_fcontext:
# prepare stack for GP + FPU
sub sp, sp, #0xb0

# Because gcc may save integer registers in fp registers across a
# function call we cannot skip saving the fp registers.
#
# Do not reinstate this test unless you fully understand what you
# are doing.
#
# # test if fpu env should be preserved
# cmp w3, #0
# b.eq 1f

# save d8 - d15
stp d8, d9, [sp, #0x00]
stp d10, d11, [sp, #0x10]
stp d12, d13, [sp, #0x20]
stp d14, d15, [sp, #0x30]

1:
# save x19-x30
stp x19, x20, [sp, #0x40]
stp x21, x22, [sp, #0x50]
Expand All @@ -88,25 +77,18 @@ swoole_jump_fcontext:
# save LR as PC
str x30, [sp, #0xa0]

# store RSP (pointing to context-data) in first argument (x0).
# STR cannot have sp as a target register
# store RSP (pointing to context-data) in X0
mov x4, sp
str x4, [x0]

# restore RSP (pointing to context-data) from A2 (x1)
mov sp, x1

# # test if fpu env should be preserved
# cmp w3, #0
# b.eq 2f
# restore RSP (pointing to context-data) from X1
mov sp, x0

# load d8 - d15
ldp d8, d9, [sp, #0x00]
ldp d10, d11, [sp, #0x10]
ldp d12, d13, [sp, #0x20]
ldp d14, d15, [sp, #0x30]

2:
# load x19-x30
ldp x19, x20, [sp, #0x40]
ldp x21, x22, [sp, #0x50]
Expand All @@ -115,9 +97,10 @@ swoole_jump_fcontext:
ldp x27, x28, [sp, #0x80]
ldp x29, x30, [sp, #0x90]

# use third arg as return value after jump
# and as first arg in context function
mov x0, x2
# return transfer_t from jump
# pass transfer_t as first arg in context function
# X0 == FCTX, X1 == DATA
mov x0, x4

# load pc
ldr x4, [sp, #0xa0]
Expand All @@ -127,7 +110,5 @@ swoole_jump_fcontext:

ret x4
.size swoole_jump_fcontext,.-swoole_jump_fcontext
#ifndef __NetBSD__
# Mark that we don't need executable stack.
.section .note.GNU-stack,"",%progbits
#endif
37 changes: 13 additions & 24 deletions thirdparty/boost/asm/jump_arm64_aapcs_macho_gas.S
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
Copyright Edward Nevill + Oliver Kowalke 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
/*******************************************************
* *
* ------------------------------------------------- *
Expand Down Expand Up @@ -52,20 +58,12 @@ _swoole_jump_fcontext:
; prepare stack for GP + FPU
sub sp, sp, #0xb0

#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
; test if fpu env should be preserved
cmp w3, #0
b.eq 1f

; save d8 - d15
stp d8, d9, [sp, #0x00]
stp d10, d11, [sp, #0x10]
stp d12, d13, [sp, #0x20]
stp d14, d15, [sp, #0x30]

1:
#endif

; save x19-x30
stp x19, x20, [sp, #0x40]
stp x21, x22, [sp, #0x50]
Expand All @@ -77,28 +75,18 @@ _swoole_jump_fcontext:
; save LR as PC
str lr, [sp, #0xa0]

; store RSP (pointing to context-data) in first argument (x0).
; STR cannot have sp as a target register
; store RSP (pointing to context-data) in X0
mov x4, sp
str x4, [x0]

; restore RSP (pointing to context-data) from A2 (x1)
mov sp, x1

#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
; test if fpu env should be preserved
cmp w3, #0
b.eq 2f
; restore RSP (pointing to context-data) from X1
mov sp, x0

; load d8 - d15
ldp d8, d9, [sp, #0x00]
ldp d10, d11, [sp, #0x10]
ldp d12, d13, [sp, #0x20]
ldp d14, d15, [sp, #0x30]

2:
#endif

; load x19-x30
ldp x19, x20, [sp, #0x40]
ldp x21, x22, [sp, #0x50]
Expand All @@ -107,9 +95,10 @@ _swoole_jump_fcontext:
ldp x27, x28, [sp, #0x80]
ldp fp, lr, [sp, #0x90]

; use third arg as return value after jump
; and as first arg in context function
mov x0, x2
; return transfer_t from jump
; pass transfer_t as first arg in context function
; X0 == FCTX, X1 == DATA
mov x0, x4

; load pc
ldr x4, [sp, #0xa0]
Expand Down
Loading

0 comments on commit ed3af5b

Please sign in to comment.