libcommunism
Userspace cooperative threading library
AAPCS.cpp
Go to the documentation of this file.
1 
5 #include "Common.h"
6 #include "CothreadPrivate.h"
7 
8 #include "AAPCS.S"
9 
10 #include <cstddef>
11 #include <stdexcept>
12 
13 using namespace libcommunism;
14 using namespace libcommunism::internal;
15 
16 extern "C" void Aarch64AapcsEntryStub();
17 
29 void *Aarch64::AllocStack(const size_t bytes) {
30  void* buf{ nullptr };
31  int err{ -1 };
32 
33  err = posix_memalign(&buf, kStackAlignment, bytes);
34  if (err) {
35  throw std::runtime_error("posix_memalign() failed");
36  }
37 
38  return buf;
39 }
40 
48 void Aarch64::DeallocStack(void* stack) {
49  free(stack);
50 }
51 
62 void Aarch64::Prepare(Aarch64 *thread, const Entry &entry) {
63  static_assert(offsetof(Aarch64, stackTop) == COTHREAD_OFF_CONTEXT_TOP, "cothread stack top is invalid");
64 
65  // build the context structure we pass to our "fake" entry point
66  auto info = new CallInfo{entry};
67  if(!info) throw std::runtime_error("Failed to allocate call info");
68 
69  // build up the stack frame
70  auto &stack = thread->stack;
71  const auto stackBottom = reinterpret_cast<uintptr_t>(stack.data()) +
72  (stack.size() * sizeof(uintptr_t));
73  auto context = reinterpret_cast<uintptr_t *>(stack.data());
74 
75  context[0] = stackBottom; // sp
76  context[1] = reinterpret_cast<uintptr_t>(Aarch64AapcsEntryStub); // x30/lr
77  context[2] = reinterpret_cast<uintptr_t>(info); // x19
78  context[12] = stackBottom; //x29/fp
79 
80  // we use the `stackTop` ptr to point to the context area
81  thread->stackTop = context;
82 }
void Aarch64AapcsEntryStub()
Architecture specific methods for working with cothreads on 64 bit ARM machines.
Definition: Common.h:18
static constexpr const size_t kStackAlignment
Definition: Common.h:80
Implementation details (including architecture/platform specific code) for the library.
Definition: Common.h:11
Main namespace for the libcommunism library.
Definition: Common.h:11
std::span< uintptr_t > stack
Stack used by this cothread, if any.
Definition: CothreadImpl.h:88