diff options
Diffstat (limited to 'lib/arch/x86_64')
-rw-r--r-- | lib/arch/x86_64/README.md | 9 | ||||
-rw-r--r-- | lib/arch/x86_64/asm.h | 4 | ||||
-rw-r--r-- | lib/arch/x86_64/thread_create.s | 20 | ||||
-rw-r--r-- | lib/arch/x86_64/yield.s | 44 |
4 files changed, 77 insertions, 0 deletions
diff --git a/lib/arch/x86_64/README.md b/lib/arch/x86_64/README.md new file mode 100644 index 0000000..f573843 --- /dev/null +++ b/lib/arch/x86_64/README.md @@ -0,0 +1,9 @@ +# SystemV AMD64 ABI + +- Integer/pointer arguments via `rdi`, `rsi`, `rdx`, `rcx`, `r8`, `r9` +- Integer/pointer return values via `rax` +- Callee saved registers `rbx`, `rbp`, `r12` – `r15` + +## Reference +- [johannst x86_64 notes](https://johannst.github.io/notes/arch/x86_64.html) + diff --git a/lib/arch/x86_64/asm.h b/lib/arch/x86_64/asm.h new file mode 100644 index 0000000..962ff1b --- /dev/null +++ b/lib/arch/x86_64/asm.h @@ -0,0 +1,4 @@ +/* Copyright (c) 2020 Johannes Stoelp */ + +extern "C" void thread_create(); +extern "C" void yield(const void* new_stack, void* const* old_stack); diff --git a/lib/arch/x86_64/thread_create.s b/lib/arch/x86_64/thread_create.s new file mode 100644 index 0000000..37c368c --- /dev/null +++ b/lib/arch/x86_64/thread_create.s @@ -0,0 +1,20 @@ +# Copyright (c) 2020 Johannes Stoelp + + .intel_syntax noprefix + .section .text, "ax", @progbits + + # extern "C" void thread_create(); + .global thread_create + .type thread_create, @function +thread_create: + .cfi_startproc + mov rdi, qword ptr [rsp+0x8] + mov rsi, qword ptr [rsp] + + call rsi + + # FIXME: no return from thread after user fn finished. +1: + jmp 1b + .cfi_endproc + .size thread_create, .-thread_create diff --git a/lib/arch/x86_64/yield.s b/lib/arch/x86_64/yield.s new file mode 100644 index 0000000..d40bcd7 --- /dev/null +++ b/lib/arch/x86_64/yield.s @@ -0,0 +1,44 @@ +# Copyright (c) 2020 Johannes Stoelp + + .intel_syntax noprefix + .section .text, "ax", @progbits + + # extern "C" void yield(const void* new_stack, void* const* old_stack); + # ^^^^^^^^^ ^^^^^^^^^ + # rdi rsi + .global yield + .type yield, @function +yield: + .cfi_startproc + // prologue + push rbp + mov rbp, rsp + + // push callee saved registers + push rbx + push rbp + push r12 + push r13 + push r14 + push r15 + + // arg0: rdi holds new stack + // arg1: rsi holds addr to location current stack must be saved + mov [rsi], rsp # save current stack ptr + mov rsp, rdi # switch to new stack ptr + + // pop callee saved registers + pop r15 + pop r14 + pop r13 + pop r12 + pop rbp + pop rbx + + // epilogue + mov rsp, rbp + pop rbp + + ret + .cfi_endproc + .size yield, .-yield |