user: implement mlibc as the libc, finally.
It's finally done.. Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <mlibc/tcb.hpp>
|
||||
#include <mlibc/thread.hpp>
|
||||
#include <mlibc-config.h>
|
||||
#include <utility>
|
||||
|
||||
#include <sys/syscall.h>
|
||||
#include <bits/syscall.h>
|
||||
|
||||
using sc_word_t = __sc_word_t;
|
||||
|
||||
extern "C" {
|
||||
extern sc_word_t __mlibc_do_asm_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4, sc_word_t arg5, sc_word_t arg6);
|
||||
|
||||
extern void __mlibc_do_cancel();
|
||||
}
|
||||
|
||||
namespace mlibc {
|
||||
// C++ wrappers for the extern "C" functions.
|
||||
inline sc_word_t do_nargs_syscall(int sc) {
|
||||
return __do_syscall0(sc);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1) {
|
||||
return __do_syscall1(sc, arg1);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1, sc_word_t arg2) {
|
||||
return __do_syscall2(sc, arg1, arg2);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3) {
|
||||
return __do_syscall3(sc, arg1, arg2, arg3);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4) {
|
||||
return __do_syscall4(sc, arg1, arg2, arg3, arg4);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4, sc_word_t arg5) {
|
||||
return __do_syscall5(sc, arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
inline sc_word_t do_nargs_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4, sc_word_t arg5, sc_word_t arg6) {
|
||||
return __do_syscall6(sc, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, 0, 0, 0, 0, 0);
|
||||
}
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, arg2, 0, 0, 0, 0);
|
||||
}
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, arg2, arg3, 0, 0, 0);
|
||||
}
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, arg2, arg3, arg4, 0, 0);
|
||||
}
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4, sc_word_t arg5) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, arg2, arg3, arg4, arg5, 0);
|
||||
}
|
||||
inline sc_word_t do_nargs_cp_syscall(int sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
|
||||
sc_word_t arg4, sc_word_t arg5, sc_word_t arg6) {
|
||||
return __mlibc_do_asm_cp_syscall(sc, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
|
||||
// Type-safe syscall result type.
|
||||
enum class sc_result_t : sc_word_t { };
|
||||
|
||||
// Cast to the argument type of the extern "C" functions.
|
||||
inline sc_word_t sc_cast(long x) { return x; }
|
||||
inline sc_word_t sc_cast(const void *x) { return reinterpret_cast<sc_word_t>(x); }
|
||||
|
||||
template<typename... T>
|
||||
sc_result_t do_syscall(int sc, T... args) {
|
||||
return static_cast<sc_result_t>(do_nargs_syscall(sc, sc_cast(args)...));
|
||||
}
|
||||
|
||||
inline int sc_error(sc_result_t ret) {
|
||||
auto v = static_cast<sc_word_t>(ret);
|
||||
if(static_cast<unsigned long>(v) > -4096UL)
|
||||
return -v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
sc_result_t do_cp_syscall(int sc, T... args) {
|
||||
#if __MLIBC_POSIX_OPTION && !MLIBC_BUILDING_RTLD
|
||||
auto result = static_cast<sc_result_t>(do_nargs_cp_syscall(sc, sc_cast(args)...));
|
||||
if (int e = sc_error(result); e) {
|
||||
auto tcb = reinterpret_cast<Tcb*>(get_current_tcb());
|
||||
if (tcb_cancelled(tcb->cancelBits) && e == EINTR) {
|
||||
__mlibc_do_cancel();
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
return do_syscall(sc, std::forward<T>(args)...);
|
||||
#endif // __MLIBC_POSIX_OPTION || !MLIBC_BUILDING_RTLD
|
||||
}
|
||||
// Cast from the syscall result type.
|
||||
template<typename T>
|
||||
T sc_int_result(sc_result_t ret) {
|
||||
auto v = static_cast<sc_word_t>(ret);
|
||||
return v;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *sc_ptr_result(sc_result_t ret) {
|
||||
auto v = static_cast<sc_word_t>(ret);
|
||||
return reinterpret_cast<T *>(v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/elf/startup.h>
|
||||
#include <sys/auxv.h>
|
||||
|
||||
extern "C" void __dlapi_enter(uintptr_t *);
|
||||
|
||||
extern char **environ;
|
||||
|
||||
size_t __hwcap;
|
||||
|
||||
extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
|
||||
__dlapi_enter(entry_stack);
|
||||
__hwcap = getauxval(AT_HWCAP);
|
||||
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
|
||||
exit(result);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,60 @@
|
||||
#include <mlibc/thread-entry.hpp>
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/thread.hpp>
|
||||
#include <bits/ensure.h>
|
||||
#include <sys/mman.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
|
||||
extern "C" void __mlibc_enter_thread(void *entry, void *user_arg) {
|
||||
// The linux kernel already sets the TCB in sys_clone().
|
||||
auto tcb = mlibc::get_current_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);
|
||||
|
||||
tcb->invokeThreadFunc(entry, user_arg);
|
||||
|
||||
__atomic_store_n(&tcb->didExit, 1, __ATOMIC_RELEASE);
|
||||
mlibc::sys_futex_wake(&tcb->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, void **stack_base) {
|
||||
(void)tcb;
|
||||
if (!*stack_size)
|
||||
*stack_size = default_stacksize;
|
||||
|
||||
uintptr_t map;
|
||||
if (*stack) {
|
||||
map = reinterpret_cast<uintptr_t>(*stack);
|
||||
*guard_size = 0;
|
||||
} else {
|
||||
map = reinterpret_cast<uintptr_t>(
|
||||
mmap(nullptr, *stack_size + *guard_size,
|
||||
PROT_NONE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
|
||||
);
|
||||
if (reinterpret_cast<void*>(map) == MAP_FAILED)
|
||||
return EAGAIN;
|
||||
int ret = mprotect(reinterpret_cast<void*>(map + *guard_size), *stack_size,
|
||||
PROT_READ | PROT_WRITE);
|
||||
if(ret)
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
*stack_base = reinterpret_cast<void*>(map);
|
||||
auto sp = reinterpret_cast<uintptr_t*>(map + *guard_size + *stack_size);
|
||||
*--sp = reinterpret_cast<uintptr_t>(user_arg);
|
||||
*--sp = reinterpret_cast<uintptr_t>(entry);
|
||||
*stack = reinterpret_cast<void*>(sp);
|
||||
return 0;
|
||||
}
|
||||
} // namespace mlibc
|
||||
Reference in New Issue
Block a user