summaryrefslogtreecommitdiff
path: root/coroutine/win64
diff options
context:
space:
mode:
Diffstat (limited to 'coroutine/win64')
-rw-r--r--coroutine/win64/Context.S77
-rw-r--r--coroutine/win64/Context.asm54
-rw-r--r--coroutine/win64/Context.h79
3 files changed, 169 insertions, 41 deletions
diff --git a/coroutine/win64/Context.S b/coroutine/win64/Context.S
new file mode 100644
index 0000000000..e0ee38e006
--- /dev/null
+++ b/coroutine/win64/Context.S
@@ -0,0 +1,77 @@
+##
+## This file is part of the "Coroutine" project and released under the MIT License.
+##
+## Created by Samuel Williams on 4/11/2018.
+## Copyright, 2018, by Samuel Williams.
+##
+
+.text
+
+.globl coroutine_transfer
+coroutine_transfer:
+ # Save the thread information block:
+ pushq %gs:8
+ pushq %gs:16
+
+ # Save caller registers:
+ pushq %rbp
+ pushq %rbx
+ pushq %rdi
+ pushq %rsi
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+
+ movaps %xmm15, -168(%rsp)
+ movaps %xmm14, -152(%rsp)
+ movaps %xmm13, -136(%rsp)
+ movaps %xmm12, -120(%rsp)
+ movaps %xmm11, -104(%rsp)
+ movaps %xmm10, -88(%rsp)
+ movaps %xmm9, -72(%rsp)
+ movaps %xmm8, -56(%rsp)
+ movaps %xmm7, -40(%rsp)
+ movaps %xmm6, -24(%rsp)
+
+ # Save caller stack pointer:
+ mov %rsp, (%rcx)
+
+ # Restore callee stack pointer:
+ mov (%rdx), %rsp
+
+ movaps -24(%rsp), %xmm6
+ movaps -40(%rsp), %xmm7
+ movaps -56(%rsp), %xmm8
+ movaps -72(%rsp), %xmm9
+ movaps -88(%rsp), %xmm10
+ movaps -104(%rsp), %xmm11
+ movaps -120(%rsp), %xmm12
+ movaps -136(%rsp), %xmm13
+ movaps -152(%rsp), %xmm14
+ movaps -168(%rsp), %xmm15
+
+ # Restore callee stack:
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rsi
+ popq %rdi
+ popq %rbx
+ popq %rbp
+
+ # Restore the thread information block:
+ popq %gs:16
+ popq %gs:8
+
+ # Put the first argument into the return value:
+ mov %rcx, %rax
+
+ # We pop the return address and jump to it:
+ ret
+
+.globl coroutine_trampoline
+coroutine_trampoline:
+ # Do not remove this. This forces 16-byte alignment when entering the coroutine.
+ ret
diff --git a/coroutine/win64/Context.asm b/coroutine/win64/Context.asm
index 6f64547d80..8c4dea1c93 100644
--- a/coroutine/win64/Context.asm
+++ b/coroutine/win64/Context.asm
@@ -2,12 +2,17 @@
;; This file is part of the "Coroutine" project and released under the MIT License.
;;
;; Created by Samuel Williams on 10/5/2018.
-;; Copyright, 2018, by Samuel Williams. All rights reserved.
+;; Copyright, 2018, by Samuel Williams.
;;
.code
coroutine_transfer proc
+ ; Save the thread information block:
+ push qword ptr gs:[8]
+ push qword ptr gs:[16]
+
+ ; Save caller registers:
push rbp
push rbx
push rdi
@@ -16,14 +21,36 @@ coroutine_transfer proc
push r13
push r14
push r15
-
- ; Save caller stack pointer
- mov [rcx], rsp
-
- ; Restore callee stack pointer
+
+ movaps [rsp - 24], xmm6
+ movaps [rsp - 40], xmm7
+ movaps [rsp - 56], xmm8
+ movaps [rsp - 72], xmm9
+ movaps [rsp - 88], xmm10
+ movaps [rsp - 104], xmm11
+ movaps [rsp - 120], xmm12
+ movaps [rsp - 136], xmm13
+ movaps [rsp - 152], xmm14
+ movaps [rsp - 168], xmm15
+
+ ; Save caller stack pointer:
+ mov [rcx], rsp
+
+ ; Restore callee stack pointer:
mov rsp, [rdx]
- ; Restore callee stack
+ movaps xmm15, [rsp - 168]
+ movaps xmm14, [rsp - 152]
+ movaps xmm13, [rsp - 136]
+ movaps xmm12, [rsp - 120]
+ movaps xmm11, [rsp - 104]
+ movaps xmm10, [rsp - 88]
+ movaps xmm9, [rsp - 72]
+ movaps xmm8, [rsp - 56]
+ movaps xmm7, [rsp - 40]
+ movaps xmm6, [rsp - 24]
+
+ ; Restore callee stack:
pop r15
pop r14
pop r13
@@ -33,11 +60,20 @@ coroutine_transfer proc
pop rbx
pop rbp
- ; Put the first argument into the return value
+ ; Restore the thread information block:
+ pop qword ptr gs:[16]
+ pop qword ptr gs:[8]
+
+ ; Put the first argument into the return value:
mov rax, rcx
- ; We pop the return address and jump to it
+ ; We pop the return address and jump to it:
ret
coroutine_transfer endp
+coroutine_trampoline proc
+ ; Do not remove this. This forces 16-byte alignment when entering the coroutine.
+ ret
+coroutine_trampoline endp
+
end
diff --git a/coroutine/win64/Context.h b/coroutine/win64/Context.h
index 53b185a9ef..23b21b5c1c 100644
--- a/coroutine/win64/Context.h
+++ b/coroutine/win64/Context.h
@@ -1,61 +1,76 @@
+#ifndef COROUTINE_WIN64_CONTEXT_H
+#define COROUTINE_WIN64_CONTEXT_H 1
+
/*
* This file is part of the "Coroutine" project and released under the MIT License.
*
* Created by Samuel Williams on 10/5/2018.
- * Copyright, 2018, by Samuel Williams. All rights reserved.
+ * Copyright, 2018, by Samuel Williams.
*/
#pragma once
#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
-#define COROUTINE void
+#define COROUTINE __declspec(noreturn) void
+#define COROUTINE_DECL void
-const size_t COROUTINE_REGISTERS = 8;
+enum {
+ COROUTINE_REGISTERS = 8,
+ COROUTINE_XMM_REGISTERS = 1+10*2,
+};
struct coroutine_context
{
- void **stack_pointer;
+ void **stack_pointer;
+ void *argument;
};
-typedef void(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
+
+void coroutine_trampoline(void);
-inline void coroutine_initialize(
- coroutine_context *context,
- coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
+
+static inline void coroutine_initialize(
+ struct coroutine_context *context,
+ coroutine_start start,
+ void *stack,
+ size_t size
) {
- context->stack_pointer = (void**)stack_pointer;
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
- /* Windows Thread Information Block */
- *--context->stack_pointer = 0;
- *--context->stack_pointer = stack_pointer;
- *--context->stack_pointer = (void*)stack_size;
+ /* Win64 ABI requires space for arguments */
+ context->stack_pointer -= 4;
- *--context->stack_pointer = (void*)start;
+ /* Return address */
+ *--context->stack_pointer = 0;
+ *--context->stack_pointer = (void*)(uintptr_t)start;
+ *--context->stack_pointer = (void*)coroutine_trampoline;
- context->stack_pointer -= COROUTINE_REGISTERS;
- memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+ /* Windows Thread Information Block */
+ /* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
+ *--context->stack_pointer = (void*)top; /* gs:[0x08] */
+ *--context->stack_pointer = (void*)stack; /* gs:[0x10] */
+
+ context->stack_pointer -= COROUTINE_REGISTERS;
+ memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
+ memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
}
-#if __cplusplus
-}
-#endif \ No newline at end of file
+#endif /* COROUTINE_WIN64_CONTEXT_H */