From fc83627b14536d0b13b2dd751d09cecbea9db378 Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 29 Sep 2020 02:06:41 +0200 Subject: move thread init into arch/ --- lib/arch/x86_64/init_stack.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/arch/x86_64/init_stack.cc (limited to 'lib/arch/x86_64/init_stack.cc') diff --git a/lib/arch/x86_64/init_stack.cc b/lib/arch/x86_64/init_stack.cc new file mode 100644 index 0000000..748fb56 --- /dev/null +++ b/lib/arch/x86_64/init_stack.cc @@ -0,0 +1,33 @@ +#include +#include // uintN_t + +extern "C" void thread_create(); + +void* init_stack(void* stack_ptr, void (*entry)(void*), void* ctx) { + static_assert(sizeof(uint64_t) == sizeof(std::uintptr_t), "Pointer must be 64bit!"); + + // Setup initial stack frame which will be popped when yielding + // first time into the thread. + // Basic idea is to yield into Thread::entry() function which will + // then call the user function. + + uint64_t* stack = static_cast(stack_ptr); + // Arguments for `thread_create`. + *(--stack) = reinterpret_cast(ctx); + *(--stack) = reinterpret_cast(entry); + + // Yield epilogue. + *(--stack) = reinterpret_cast(thread_create); // Return address + *(--stack) = static_cast(0); // rbp + + // Callee saved registers. + *(--stack) = static_cast(0); // rbx + *(--stack) = reinterpret_cast(static_cast(stack_ptr) - 4); // rbp + *(--stack) = static_cast(0); // r12 + *(--stack) = static_cast(0); // r13 + *(--stack) = static_cast(0); // r14 + *(--stack) = static_cast(0); // r15 + + assert((reinterpret_cast(stack) & 0xf) == 0); // 16byte aligned + return static_cast(stack); +} -- cgit v1.2.3