Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sysdeps/managarm: add riscv64 sysdeps #866

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions abis/linux/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ typedef struct __ucontext {
mcontext_t uc_mcontext;
} ucontext_t;

struct sigcontext {
unsigned long int gregs[32];
unsigned long long int fpregs[66] __attribute__ ((__aligned__ (16)));
};

#warning amnogusd

#elif defined (__aarch64__)

typedef struct sigcontext {
Expand Down
2 changes: 2 additions & 0 deletions sysdeps/managarm/generic/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,8 @@ int sys_uname(struct utsname *buf) {
strcpy(buf->machine, "x86_64");
#elif defined (__aarch64__)
strcpy(buf->machine, "aarch64");
#elif defined (__riscv) && __riscv_xlen == 64
strcpy(buf->machine, "riscv64");
#else
# error Unknown architecture
#endif
Expand Down
6 changes: 6 additions & 0 deletions sysdeps/managarm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ elif host_machine.cpu_family() == 'x86_64'
'x86_64/thread_entry.S',
'x86_64/thread.cpp'
)
elif host_machine.cpu_family() == 'riscv64'
libc_sources += files(
'riscv64/signals.S',
'riscv64/thread_entry.S',
'riscv64/thread.cpp'
)
else
error('Unknown architecture')
endif
Expand Down
15 changes: 15 additions & 0 deletions sysdeps/managarm/riscv64/crt-src/crt0.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.section .text
.global _start
_start:
.weak __global_pointer$
.hidden __global_pointer$
.option push
.option norelax
lla gp, __global_pointer$
.option pop

mv a0, sp
la a1, main
call __mlibc_entry
.section .note.GNU-stack,"",%progbits

8 changes: 8 additions & 0 deletions sysdeps/managarm/riscv64/signals.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.section .text
nop // TODO: why is this needed?
.global __mlibc_signal_restore
.type __mlibc_signal_restore, @function
__mlibc_signal_restore:
unimp // TODO
.section .note.GNU-stack,"",%progbits

56 changes: 56 additions & 0 deletions sysdeps/managarm/riscv64/thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <mlibc/thread-entry.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <bits/ensure.h>
#include <sys/mman.h>
#include <stdint.h>
#include <stddef.h>

extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) {
// Wait until our parent sets up the TID.
while(!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);

if(mlibc::sys_tcb_set(tcb))
__ensure(!"sys_tcb_set() failed");

void *(*func)(void *) = reinterpret_cast<void *(*)(void *)>(entry);
auto result = func(user_arg);

auto self = reinterpret_cast<Tcb *>(tcb);

self->returnValue = result;
__atomic_store_n(&self->didExit, 1, __ATOMIC_RELEASE);
mlibc::sys_futex_wake(&self->didExit);

mlibc::sys_thread_exit();
}

namespace mlibc {

static constexpr size_t default_stacksize = 0x200000;

int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size, size_t *guard_size) {
if (!*stack_size)
*stack_size = default_stacksize;
*guard_size = 0;

uintptr_t *sp;
if (*stack) {
sp = reinterpret_cast<uintptr_t *>(*stack);
} else {
sp = reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(
mmap(nullptr, *stack_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
) + *stack_size);
}

*--sp = reinterpret_cast<uintptr_t>(tcb);
*--sp = reinterpret_cast<uintptr_t>(user_arg);
*--sp = reinterpret_cast<uintptr_t>(entry);
*stack = reinterpret_cast<void*>(sp);
return 0;
}

} //namespace mlibc
17 changes: 17 additions & 0 deletions sysdeps/managarm/riscv64/thread_entry.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.section .text
.global __mlibc_start_thread
.type __mlibc_start_thread, "function"
__mlibc_start_thread:
// if this works i am going to eat my dog
ld a0, 0(sp)
ld a1, 8(sp)
ld a2, 16(sp)
addi sp, sp, 24
andi sp, sp, -16
call __mlibc_enter_thread
unimp

.parent:
ret
.section .note.GNU-stack,"",%progbits