-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fiber] Add x64 implementation on MacOS
- Loading branch information
Showing
7 changed files
with
203 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
This file is a part of SORT(Simple Open Ray Tracing), an open-source cross | ||
platform physically based renderer. | ||
Copyright (c) 2011-2023 by Jiayin Cao - All rights reserved. | ||
SORT is a free software written for educational purpose. Anyone can distribute | ||
or modify it under the the terms of the GNU General Public License Version 3 as | ||
published by the Free Software Foundation. However, there is NO warranty that | ||
all components are functional in a perfect manner. Without even the implied | ||
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU General Public License along with | ||
this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include "core/define.h" | ||
|
||
#if defined(SORT_X64_TARGET) && (defined(SORT_IN_MAC) || defined(SORT_IN_LINUX)) | ||
|
||
#include "fiber_impl_asm_x64.h" | ||
|
||
// fiber entry function | ||
static inline void fiber_entry(void (*target)(void*), void* arg) { | ||
target(arg); | ||
} | ||
|
||
// setup a new fiber | ||
void create_fiber(struct FiberContext* context, | ||
void* stack, | ||
uint32_t stackSize, | ||
void (*target)(void*), | ||
void* arg) { | ||
uintptr_t* stack_top = (uintptr_t*)((uint8_t*)(stack) + stackSize); | ||
context->rip = (uintptr_t)&fiber_entry; | ||
context->rdi = (uintptr_t)target; | ||
context->rsi = (uintptr_t)arg; | ||
context->rsp = (uintptr_t)&stack_top[-3]; | ||
stack_top[-2] = 0; // No return target. | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
This file is a part of SORT(Simple Open Ray Tracing), an open-source cross | ||
platform physically based renderer. | ||
Copyright (c) 2011-2023 by Jiayin Cao - All rights reserved. | ||
SORT is a free software written for educational purpose. Anyone can distribute | ||
or modify it under the the terms of the GNU General Public License Version 3 as | ||
published by the Free Software Foundation. However, there is NO warranty that | ||
all components are functional in a perfect manner. Without even the implied | ||
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU General Public License along with | ||
this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
|
||
|
||
#if defined(__x86_64__) | ||
|
||
#include "fiber_impl_asm_x64_shared.h" | ||
|
||
// void marl_fiber_swap(marl_fiber_context* from, const marl_fiber_context* to) | ||
// rdi: from | ||
// rsi: to | ||
.text | ||
.global ASM_ENTRY_WRAPPER(switch_fiber) | ||
.align 4 | ||
ASM_ENTRY_WRAPPER(switch_fiber): | ||
|
||
// Save context 'from' | ||
|
||
// Store callee-preserved registers | ||
movq %rbx, FIBER_REG_RBX(%rdi) | ||
movq %rbp, FIBER_REG_RBP(%rdi) | ||
movq %r12, FIBER_REG_R12(%rdi) | ||
movq %r13, FIBER_REG_R13(%rdi) | ||
movq %r14, FIBER_REG_R14(%rdi) | ||
movq %r15, FIBER_REG_R15(%rdi) | ||
|
||
movq (%rsp), %rcx /* call stores the return address on the stack before jumping */ | ||
movq %rcx, FIBER_REG_RIP(%rdi) | ||
leaq 8(%rsp), %rcx /* skip the pushed return address */ | ||
movq %rcx, FIBER_REG_RSP(%rdi) | ||
|
||
// Load context 'to' | ||
movq %rsi, %r8 | ||
|
||
// Load callee-preserved registers | ||
movq FIBER_REG_RBX(%r8), %rbx | ||
movq FIBER_REG_RBP(%r8), %rbp | ||
movq FIBER_REG_R12(%r8), %r12 | ||
movq FIBER_REG_R13(%r8), %r13 | ||
movq FIBER_REG_R14(%r8), %r14 | ||
movq FIBER_REG_R15(%r8), %r15 | ||
|
||
// Load first two call parameters | ||
movq FIBER_REG_RDI(%r8), %rdi | ||
movq FIBER_REG_RSI(%r8), %rsi | ||
|
||
// Load stack pointer | ||
movq FIBER_REG_RSP(%r8), %rsp | ||
|
||
// Load instruction pointer, and jump | ||
movq FIBER_REG_RIP(%r8), %rcx | ||
jmp *%rcx | ||
|
||
#endif // defined(__x86_64__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
This file is a part of SORT(Simple Open Ray Tracing), an open-source cross | ||
platform physically based renderer. | ||
Copyright (c) 2011-2023 by Jiayin Cao - All rights reserved. | ||
SORT is a free software written for educational purpose. Anyone can distribute | ||
or modify it under the the terms of the GNU General Public License Version 3 as | ||
published by the Free Software Foundation. However, there is NO warranty that | ||
all components are functional in a perfect manner. Without even the implied | ||
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU General Public License along with | ||
this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
|
||
// This file simply defines some macros to be shared by assembly language and C code | ||
|
||
#include "core/define.h" | ||
|
||
#define FIBER_REG_RBX 0x00 | ||
#define FIBER_REG_RBP 0x08 | ||
#define FIBER_REG_R12 0x10 | ||
#define FIBER_REG_R13 0x18 | ||
#define FIBER_REG_R14 0x20 | ||
#define FIBER_REG_R15 0x28 | ||
#define FIBER_REG_RDI 0x30 | ||
#define FIBER_REG_RSI 0x38 | ||
#define FIBER_REG_RSP 0x40 | ||
#define FIBER_REG_RIP 0x48 | ||
|
||
#if defined(SORT_IN_MAC) | ||
#define ASM_ENTRY_WRAPPER(x) _##x | ||
#else | ||
#define ASM_ENTRY_WRAPPER(x) x | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters