Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: Add experimental fibers #42

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
08c4ece
Initial executions draft
zetanumbers Sep 20, 2024
8c29fd0
Implement executions
zetanumbers Sep 23, 2024
09051b5
fix inline asm typo
zetanumbers Sep 23, 2024
8764fbd
Add execution example
zetanumbers Sep 23, 2024
48c1827
Use a single `fiber_switch` function
zetanumbers Sep 25, 2024
f10d21c
fix: `Execution` implementation
zetanumbers Sep 26, 2024
e07f501
change example's strings and names
zetanumbers Sep 26, 2024
5422922
Make execution thread and unwind unsafe
zetanumbers Sep 26, 2024
4b10cd9
docs: Document Execution's constructors
zetanumbers Oct 1, 2024
44f2274
rename `Execution` into a `Fiber`
zetanumbers Oct 2, 2024
064e6d4
Expand fiber's documentation
zetanumbers Oct 3, 2024
74edac1
rename fiber source and example files
zetanumbers Oct 10, 2024
6b9a2dd
rename execution to fiber in the fiber example source code
zetanumbers Oct 10, 2024
4021b9f
Add comments to the code
zetanumbers Oct 10, 2024
8caeddb
declare fiber function ABIs
zetanumbers Oct 10, 2024
8bafe87
Add `Fiber::switch_in_place` method
zetanumbers Oct 22, 2024
eb50d76
Add fiber benchmark
zetanumbers Oct 22, 2024
43cbd29
optimize fiber_switch with inline assembly
zetanumbers Oct 22, 2024
0f0f997
Further optimize fiber_switch
zetanumbers Oct 22, 2024
78a2286
Initial aarch64 implementation
zetanumbers Oct 24, 2024
238eee6
fix aarch64 implementation
zetanumbers Oct 28, 2024
a0cd97f
Add risc-v support
zetanumbers Oct 29, 2024
f59d07c
Copy and modify some coroutine tests for fibers
zetanumbers Nov 2, 2024
98f7da3
Fix fiber backtrace test
zetanumbers Nov 2, 2024
7d4334e
simplify fiber API
zetanumbers Nov 2, 2024
c7aa183
remove RecursiveFiber macro and instead use F shorthand
zetanumbers Nov 7, 2024
d1a1f24
Add support for unwinding from `Fiber::switch` call
zetanumbers Nov 7, 2024
f02216b
Add `fiber_unchecked` constructor
zetanumbers Nov 7, 2024
67da390
remove x28 register clobber copy-paste typo on risc-v
zetanumbers Nov 8, 2024
0c114c2
Fix risc-v segfault bug
zetanumbers Nov 8, 2024
bc26ead
Simplify fiber_switch function declaration
zetanumbers Nov 11, 2024
22b5814
Add from_raw and into_raw methods to Fiber
zetanumbers Dec 4, 2024
eee8403
update fiber documentation
zetanumbers Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add risc-v support
  • Loading branch information
zetanumbers committed Oct 29, 2024
commit a0cd97fbbb19d96d04d702cefb7f4c6970f6aad9
61 changes: 60 additions & 1 deletion src/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@
//! ```

use core::arch::{asm, global_asm};
use core::{ffi, ptr};

use super::{allocate_obj_on_stack, push};
use crate::stack::{Stack, StackPointer};
use crate::unwind::{
asm_may_unwind_root, asm_may_unwind_yield, cfi_reset_args_size_root, cfi_reset_args_size_yield,
InitialFunc, StackCallFunc, TrapHandler,
InitialFunc, StackCallFunc, SwitchFiberFunc, TrapHandler,
};
use crate::util::EncodedValue;

Expand Down Expand Up @@ -255,6 +256,64 @@ extern "C" {
fn stack_call_trampoline(arg: *mut u8, sp: StackPointer, f: StackCallFunc);
}

#[inline]
pub unsafe fn fiber_init_stack(stack: &impl Stack) -> StackPointer {
unsafe extern "C" fn entry(
sp: StackPointer,
arg: EncodedValue,
// There could be no output buffer on a fresh fiber.
// Assuming first `f` function never returns on this fiber.
_obj: *mut ffi::c_void,
f: SwitchFiberFunc,
) {
f(sp, arg, ptr::null_mut())
}

let mut sp = stack.base().get();

// Put the entry function pointer
push(&mut sp, Some(entry as StackWord));

StackPointer::new_unchecked(sp)
}

#[inline]
pub unsafe fn fiber_switch(
stack_ptr: StackPointer,
mut arg: EncodedValue,
ret: *mut ffi::c_void,
mut f: SwitchFiberFunc,
) {
let mut sp = stack_ptr.get();
asm!(
addi!("sp", "sp", -3),
s!("s0", 2, "sp"),
s!("s1", 1, "sp"),
"lla ra, 2f",
s!("ra", 0, "sp"),
l!("ra", 0, "a0"),
"mv t1, sp",
addi!("sp", "a0", 1),
"mv a0, t1",
"ret",
"2:",
l!("s1", 1, "sp"),
l!("s0", 2, "sp"),
addi!("sp", "sp", 2),

inlateout("a0") sp, inlateout("a1") arg, inlateout("a3") f,
lateout("s2") _, lateout("s3") _, lateout("s4") _, lateout("s5") _,
lateout("s6") _, lateout("s7") _, lateout("s8") _, lateout("s9") _,
lateout("s10") _, lateout("s11") _,
lateout("fs0") _, lateout("fs1") _, lateout("fs2") _, lateout("fs3") _,
lateout("fs4") _, lateout("fs5") _, lateout("fs6") _, lateout("fs7") _,
lateout("fs8") _, lateout("fs9") _, lateout("fs10") _, lateout("fs11") _,
lateout("x28") _,
clobber_abi("C"),
);
f(StackPointer::new_unchecked(sp), arg, ret)
}

#[inline]
pub unsafe fn init_stack<T>(stack: &impl Stack, func: InitialFunc<T>, obj: T) -> StackPointer {
let mut sp = stack.base().get();
Expand Down