From 33f286000db35fe50639c237caa736deea304585 Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 22 Sep 2020 23:48:09 +0200 Subject: split classes into separate files, add arch specific subdir --- lib/arch/x86_64/README.md | 9 +++++++++ lib/arch/x86_64/asm.h | 4 ++++ lib/arch/x86_64/thread_create.s | 20 +++++++++++++++++++ lib/arch/x86_64/yield.s | 44 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 lib/arch/x86_64/README.md create mode 100644 lib/arch/x86_64/asm.h create mode 100644 lib/arch/x86_64/thread_create.s create mode 100644 lib/arch/x86_64/yield.s (limited to 'lib/arch/x86_64') 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 -- cgit v1.2.3