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,24 @@
|
||||
#include <abi-bits/signal.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "context-offsets.h"
|
||||
|
||||
// offsets int ucontext_t as used in signals.S
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext) == UCONTEXT_GREGS_OFFSET);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R8]) == UCONTEXT_OFFSET_R8);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R9]) == UCONTEXT_OFFSET_R9);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R10]) == UCONTEXT_OFFSET_R10);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R11]) == UCONTEXT_OFFSET_R11);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R12]) == UCONTEXT_OFFSET_R12);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R13]) == UCONTEXT_OFFSET_R13);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R14]) == UCONTEXT_OFFSET_R14);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_R15]) == UCONTEXT_OFFSET_R15);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RDI]) == UCONTEXT_OFFSET_RDI);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RSI]) == UCONTEXT_OFFSET_RSI);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RBP]) == UCONTEXT_OFFSET_RBP);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RBX]) == UCONTEXT_OFFSET_RBX);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RDX]) == UCONTEXT_OFFSET_RDX);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RAX]) == UCONTEXT_OFFSET_RAX);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RCX]) == UCONTEXT_OFFSET_RCX);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RSP]) == UCONTEXT_OFFSET_RSP);
|
||||
static_assert(offsetof(ucontext_t, uc_mcontext.gregs[REG_RIP]) == UCONTEXT_OFFSET_RIP);
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#define UCONTEXT_GREGS_OFFSET 40
|
||||
|
||||
#define UCONTEXT_OFFSET_R8 (UCONTEXT_GREGS_OFFSET + 0)
|
||||
#define UCONTEXT_OFFSET_R9 (UCONTEXT_GREGS_OFFSET + 8)
|
||||
#define UCONTEXT_OFFSET_R10 (UCONTEXT_GREGS_OFFSET + 16)
|
||||
#define UCONTEXT_OFFSET_R11 (UCONTEXT_GREGS_OFFSET + 24)
|
||||
#define UCONTEXT_OFFSET_R12 (UCONTEXT_GREGS_OFFSET + 32)
|
||||
#define UCONTEXT_OFFSET_R13 (UCONTEXT_GREGS_OFFSET + 40)
|
||||
#define UCONTEXT_OFFSET_R14 (UCONTEXT_GREGS_OFFSET + 48)
|
||||
#define UCONTEXT_OFFSET_R15 (UCONTEXT_GREGS_OFFSET + 56)
|
||||
#define UCONTEXT_OFFSET_RDI (UCONTEXT_GREGS_OFFSET + 64)
|
||||
#define UCONTEXT_OFFSET_RSI (UCONTEXT_GREGS_OFFSET + 72)
|
||||
#define UCONTEXT_OFFSET_RBP (UCONTEXT_GREGS_OFFSET + 80)
|
||||
#define UCONTEXT_OFFSET_RBX (UCONTEXT_GREGS_OFFSET + 88)
|
||||
#define UCONTEXT_OFFSET_RDX (UCONTEXT_GREGS_OFFSET + 96)
|
||||
#define UCONTEXT_OFFSET_RAX (UCONTEXT_GREGS_OFFSET + 104)
|
||||
#define UCONTEXT_OFFSET_RCX (UCONTEXT_GREGS_OFFSET + 112)
|
||||
#define UCONTEXT_OFFSET_RSP (UCONTEXT_GREGS_OFFSET + 120)
|
||||
#define UCONTEXT_OFFSET_RIP (UCONTEXT_GREGS_OFFSET + 128)
|
||||
@@ -0,0 +1,8 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov %rsp, %rdi
|
||||
lea main(%rip), %rsi
|
||||
call __mlibc_entry
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov %rsp, %rdi
|
||||
mov $main, %rsi
|
||||
call __mlibc_entry
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
.ident "x86_64-managarm-mlibc crti"
|
||||
|
||||
.section .init
|
||||
.globl _init
|
||||
.type _init,@function
|
||||
_init:
|
||||
push %rax
|
||||
|
||||
.section .fini
|
||||
.globl _fini
|
||||
.type _fini,@function
|
||||
_fini:
|
||||
push %rax
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,11 @@
|
||||
.ident "x86_64-managarm-mlibc crtn"
|
||||
|
||||
.section .init
|
||||
pop %rax
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
pop %rax
|
||||
ret
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "mlibc-asm/dwarf-helpers.h"
|
||||
#include "mlibc-asm/helpers.h"
|
||||
#include "context-offsets.h"
|
||||
|
||||
.section .text
|
||||
.cfi_startproc
|
||||
.cfi_signal_frame
|
||||
cfi_set_cfa_to_ptr_with_offset DWARF_REG_RSP, UCONTEXT_OFFSET_RSP
|
||||
cfi_set_prev_reg_value DWARF_REG_R8, DWARF_REG_RSP, UCONTEXT_OFFSET_R8
|
||||
cfi_set_prev_reg_value DWARF_REG_R9, DWARF_REG_RSP, UCONTEXT_OFFSET_R9
|
||||
cfi_set_prev_reg_value DWARF_REG_R10, DWARF_REG_RSP, UCONTEXT_OFFSET_R10
|
||||
cfi_set_prev_reg_value DWARF_REG_R11, DWARF_REG_RSP, UCONTEXT_OFFSET_R11
|
||||
cfi_set_prev_reg_value DWARF_REG_R12, DWARF_REG_RSP, UCONTEXT_OFFSET_R12
|
||||
cfi_set_prev_reg_value DWARF_REG_R13, DWARF_REG_RSP, UCONTEXT_OFFSET_R13
|
||||
cfi_set_prev_reg_value DWARF_REG_R14, DWARF_REG_RSP, UCONTEXT_OFFSET_R14
|
||||
cfi_set_prev_reg_value DWARF_REG_R15, DWARF_REG_RSP, UCONTEXT_OFFSET_R15
|
||||
cfi_set_prev_reg_value DWARF_REG_RDI, DWARF_REG_RSP, UCONTEXT_OFFSET_RDI
|
||||
cfi_set_prev_reg_value DWARF_REG_RSI, DWARF_REG_RSP, UCONTEXT_OFFSET_RSI
|
||||
cfi_set_prev_reg_value DWARF_REG_RBP, DWARF_REG_RSP, UCONTEXT_OFFSET_RBP
|
||||
cfi_set_prev_reg_value DWARF_REG_RBX, DWARF_REG_RSP, UCONTEXT_OFFSET_RBX
|
||||
cfi_set_prev_reg_value DWARF_REG_RDX, DWARF_REG_RSP, UCONTEXT_OFFSET_RDX
|
||||
cfi_set_prev_reg_value DWARF_REG_RAX, DWARF_REG_RSP, UCONTEXT_OFFSET_RAX
|
||||
cfi_set_prev_reg_value DWARF_REG_RCX, DWARF_REG_RSP, UCONTEXT_OFFSET_RCX
|
||||
cfi_set_prev_reg_value DWARF_REG_RETURN_ADDRESS, DWARF_REG_RSP, UCONTEXT_OFFSET_RIP
|
||||
nop
|
||||
|
||||
PROC_START_NOCFI(__mlibc_signal_restore)
|
||||
mov $0x80000006, %rdi
|
||||
syscall
|
||||
ud2
|
||||
PROC_END(__mlibc_signal_restore)
|
||||
|
||||
GNU_STACK_NOTE()
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <bits/ensure.h>
|
||||
#include <errno.h>
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/tcb.hpp>
|
||||
#include <mlibc/thread-entry.hpp>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/mman.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");
|
||||
|
||||
tcb->invokeThreadFunc(entry, user_arg);
|
||||
|
||||
auto self = reinterpret_cast<Tcb *>(tcb);
|
||||
|
||||
__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,
|
||||
void **stack_base
|
||||
) {
|
||||
if (!*stack_size)
|
||||
*stack_size = default_stacksize;
|
||||
*guard_size = 0;
|
||||
|
||||
if (*stack) {
|
||||
*stack_base = *stack;
|
||||
} else {
|
||||
*stack_base =
|
||||
mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (*stack_base == MAP_FAILED) {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
uintptr_t *sp =
|
||||
reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(*stack_base) + *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
|
||||
@@ -0,0 +1,22 @@
|
||||
.section .text
|
||||
.global __mlibc_start_thread
|
||||
.type __mlibc_start_thread, "function"
|
||||
.cfi_startproc
|
||||
__mlibc_start_thread:
|
||||
.cfi_undefined %rip
|
||||
.cfi_undefined %rbp
|
||||
|
||||
pop %rdi
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_undefined %rdi
|
||||
pop %rsi
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_undefined %rsi
|
||||
pop %rdx
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_undefined %rdx
|
||||
call __mlibc_enter_thread
|
||||
ud2
|
||||
.cfi_endproc
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
Reference in New Issue
Block a user