Skip to content

Commit

Permalink
Merge pull request #8 from oli-obk/fiber
Browse files Browse the repository at this point in the history
Use fibers on windows
  • Loading branch information
alexcrichton authored Dec 10, 2018
2 parents 0f116f3 + 2c3befa commit 6e5d422
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 178 deletions.
14 changes: 12 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ doctest = false
test = false

[dependencies]
cfg-if = "0.1"
libc = "0.2"
cfg-if = "0.1.6"
libc = "0.2.45"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.6"
features = [
'memoryapi',
'winbase',
'fibersapi',
'processthreadsapi',
'minwindef',
]

[build-dependencies]
cc = "1.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A stack-growth library for Rust. Enables annotating fixed points in programs
where the stack may want to grow larger. Spills over to the heap if the stack
has it its limit.
has hit its limit.

This library is intended on helping implement recursive algorithms.

Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
cfg.define("APPLE", None);
} else if target.contains("windows") {
cfg.define("WINDOWS", None);
cfg.file("src/arch/windows.c");
} else {
panic!("\n\nusing currently unsupported target triple with \
stacker: {}\n\n", target);
Expand Down
21 changes: 10 additions & 11 deletions src/arch/i686.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

.text

GLOBAL(__stacker_black_box):
ret

GLOBAL(__stacker_stack_pointer):
mov %esp, %eax
ret

#if defined(WINDOWS)
GLOBAL(__stacker_get_tib_32):
mov %fs:0x18, %eax
ret
#endif

GLOBAL(__stacker_switch_stacks):
// CFI instructions tells the unwinder how to unwind this function
// This enables unwinding through our extended stacks and also
// backtrackes
.cfi_startproc
push %ebp
.cfi_def_cfa_offset 8 // restore esp by adding 8
.cfi_offset ebp, -8 // restore ebp from the stack
mov %esp, %ebp
mov 8(%ebp), %esp // switch to our new stack
.cfi_def_cfa_register ebp // restore esp from ebp
mov 16(%ebp), %esp // switch to our new stack
mov 12(%ebp), %eax // load function we're going to call
push 16(%ebp) // push argument to first function
push 8(%ebp) // push argument to first function
call *%eax // call our function pointer
mov %ebp, %esp // restore the old stack pointer
pop %ebp
ret
.cfi_endproc
23 changes: 0 additions & 23 deletions src/arch/i686.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,9 @@
.MODEL FLAT, C
.CODE

__stacker_black_box PROC
RET
__stacker_black_box ENDP

__stacker_stack_pointer PROC
MOV EAX, ESP
RET
__stacker_stack_pointer ENDP

__stacker_get_tib_32 PROC
ASSUME FS:NOTHING
MOV EAX, FS:[24]
ASSUME FS:ERROR
RET
__stacker_get_tib_32 ENDP

__stacker_switch_stacks PROC
PUSH EBP
MOV EBP, ESP
MOV ESP, [EBP + 8] ; switch stacks
MOV EAX, [EBP + 12] ; load the function we're going to call
PUSH [EBP + 16] ; push the argument to this function
CALL EAX ; call the next function
MOV ESP, EBP ; restore the old stack pointer
POP EBP
RET
__stacker_switch_stacks ENDP

END
5 changes: 5 additions & 0 deletions src/arch/windows.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <windows.h>

PVOID __stacker_get_current_fiber() {
return GetCurrentFiber();
}
32 changes: 10 additions & 22 deletions src/arch/x86_64.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,23 @@

.text

GLOBAL(__stacker_black_box):
ret

GLOBAL(__stacker_stack_pointer):
movq %rsp, %rax
ret

#if defined(WINDOWS)
#define ARG1 %rcx
#define ARG2 %rdx
#define ARG3 %r8
#else
#define ARG1 %rdi
#define ARG2 %rsi
#define ARG3 %rdx
#endif

#if defined(WINDOWS)
GLOBAL(__stacker_get_tib_64):
mov %gs:0x30, %rax
ret
#endif

GLOBAL(__stacker_switch_stacks):
// CFI instructions tells the unwinder how to unwind this function
// This enables unwinding through our extended stacks and also
// backtrackes
.cfi_startproc
push %rbp
.cfi_def_cfa_offset 16 // restore rsp by adding 16
.cfi_offset rbp, -16 // restore rbp from the stack
mov %rsp, %rbp
mov ARG1, %rsp // switch to our new stack
mov ARG3, ARG1 // move the data pointer to the first argument
call *ARG2 // call our function pointer
.cfi_def_cfa_register rbp // restore rsp from rbp
mov %rdx, %rsp // switch to our new stack
call *%rsi // call our function pointer, data argument in %rdi
mov %rbp, %rsp // restore the old stack pointer
pop %rbp
ret
.cfi_endproc
17 changes: 1 addition & 16 deletions src/arch/x86_64.asm
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
_text SEGMENT

__stacker_black_box PROC
RET
__stacker_black_box ENDP

__stacker_stack_pointer PROC
MOV RAX, RSP
RET
__stacker_stack_pointer ENDP

__stacker_switch_stacks PROC
PUSH RBP
MOV RBP, RSP
MOV RSP, RCX ; switch to our new stack
MOV RCX, R8 ; move the data pointer to the first argument
CALL RDX ; call our function pointer
MOV RSP, RBP ; restore the old stack pointer
POP RBP
RET
__stacker_switch_stacks ENDP

END
END
Loading

0 comments on commit 6e5d422

Please sign in to comment.