user: implement mlibc as the libc, finally.

It's finally done..

Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
kaguya
2026-05-02 03:31:49 -04:00
parent 2fa39ad85a
commit 9a9b91c940
2387 changed files with 152741 additions and 315 deletions
@@ -0,0 +1,26 @@
#include <stdint.h>
#include <stdlib.h>
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>
#include <sys/syscall.h>
#include <mlibc/debug.hpp>
extern "C" void __dlapi_enter(uintptr_t *);
extern char **environ;
extern "C" void __mlibc_sigret(void) {
int ret, errno;
SYSCALL0(SYSCALL_SIGNAL_RETURN);
mlibc::panicLogger() << "mlibc: failed to exit signal with " << errno << frg::endlog;
__builtin_unreachable();
}
extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
__dlapi_enter(entry_stack);
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,32 @@
#include <sys/syscall.h>
#include <sys/mac.h>
#include <errno.h>
#include <string.h>
extern "C" {
unsigned long get_mac_capabilities(void) {
int ret;
SYSCALL0(SYSCALL_GET_MAC_CAPABILITIES);
return ret;
}
int set_mac_capabilities(unsigned long request) {
int ret;
SYSCALL1(SYSCALL_SET_MAC_CAPABILITIES, request);
return ret;
}
int add_mac_permissions(const char *path, int flags) {
int ret;
SYSCALL3(SYSCALL_ADD_MAC_PERMISSIONS, path, strlen(path), flags);
return ret;
}
int set_mac_enforcement(unsigned long enforcement) {
int ret;
SYSCALL1(SYSCALL_SET_MAC_ENFORCEMENT, enforcement);
return ret;
}
}
@@ -0,0 +1,82 @@
#include <errno.h>
#include <mntent.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <bits/ensure.h>
namespace {
char *internal_buf;
size_t internal_bufsize;
}
#define SENTINEL (char *)&internal_buf
FILE *setmntent(const char *name, const char *mode) {
return fopen(name, mode);
}
struct mntent *getmntent(FILE *f) {
static struct mntent mnt;
return getmntent_r(f, &mnt, SENTINEL, 0);
}
int addmntent(FILE *f, const struct mntent *mnt) {
if(fseek(f, 0, SEEK_END)) {
return 1;
}
return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
mnt->mnt_freq, mnt->mnt_passno) < 0;
}
int endmntent(FILE *f) {
if(f) {
fclose(f);
}
return 1;
}
char *hasmntopt(const struct mntent *mnt, const char *opt) {
return strstr(mnt->mnt_opts, opt);
}
/* Adapted from musl */
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen) {
bool use_internal = (linebuf == SENTINEL);
char source[60];
char target[30];
char fs[30];
char options[30];
int dump;
int pass;
do {
if (use_internal) {
getline(&internal_buf, &internal_bufsize, f);
linebuf = internal_buf;
} else {
fgets(linebuf, buflen, f);
}
if (feof(f) || ferror(f)) {
return 0;
}
if (sscanf(linebuf, "%s %s %s %s %d %d\n", source, target, fs, options,
&dump, &pass) != 6) {
continue;
}
} while(linebuf[0] == '#');
mnt->mnt_fsname = strdup(source);
mnt->mnt_dir = strdup(target);
mnt->mnt_type = strdup(fs);
mnt->mnt_opts = strdup(options);
mnt->mnt_freq = dump;
mnt->mnt_passno = pass;
return mnt;
}
@@ -0,0 +1,20 @@
#include <errno.h>
#include <sys/mount.h>
#include <bits/ensure.h>
#include <sys/syscall.h>
#include <string.h>
int mount(const char *source, const char *target, int type, int flags) {
int ret;
size_t source_len = strlen(source);
size_t target_len = strlen(target);
SYSCALL6(SYSCALL_MOUNT, source, source_len, target, target_len, type, flags);
return ret;
}
int umount(const char *target, int flags) {
int ret;
size_t target_len = strlen(target);
SYSCALL3(SYSCALL_UMOUNT, target, target_len, flags);
return ret;
}
@@ -0,0 +1,9 @@
#include <sys/syscall.h>
#include <errno.h>
#include <sys/ptrace.h>
int ptrace(int request, pid_t pid, void *addr, void *data) {
int ret;
SYSCALL4(SYSCALL_PTRACE, request, pid, addr, data);
return ret;
}
@@ -0,0 +1,9 @@
#include <errno.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
int reboot(int what) {
int ret, errno;
SYSCALL2(SYSCALL_REBOOT, what, 0);
return ret;
}
@@ -0,0 +1,23 @@
#if defined(__x86_64__)
.section .text
.global __mlibc_thread_entry
__mlibc_thread_entry:
pop %rdi
pop %rsi
pop %rdx
call __mlibc_thread_trampoline
#elif (defined(__riscv) && __riscv_xlen == 64)
.section .text
.global __mlibc_thread_entry
__mlibc_thread_entry:
ld a0, 0x0(sp)
ld a1, 0x8(sp)
ld a2, 0x10(sp)
addi sp, sp, 24
call __mlibc_thread_trampoline
#else
#error "Missing architecture specific code."
#endif
.section .note.GNU-stack,"",%progbits
@@ -0,0 +1,52 @@
#include <sys/mman.h>
#include <mlibc/debug.hpp>
#include <errno.h>
#include <mlibc/all-sysdeps.hpp>
#include <bits/ensure.h>
#include <mlibc/tcb.hpp>
#include <mlibc/arch-defs.hpp>
extern "C" void __mlibc_thread_trampoline(void *(*fn)(void *), Tcb *tcb, void *arg) {
while (__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED) == 0) {
mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);
}
tcb->invokeThreadFunc(reinterpret_cast<void *>(fn), arg);
__atomic_store_n(&tcb->didExit, 1, __ATOMIC_RELEASE);
mlibc::sys_futex_wake(&tcb->didExit);
mlibc::sys_thread_exit();
}
#define DEFAULT_STACK 0x20000
namespace mlibc {
int sys_prepare_stack(void **stack, void *entry, void *arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) {
*guard_size = mlibc::page_size;
*stack_size = *stack_size ? *stack_size : DEFAULT_STACK;
if (!*stack) {
*stack_base = mmap(NULL, *stack_size + mlibc::page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (*stack_base == MAP_FAILED) {
return errno;
}
munmap((char *)*stack_base + *stack_size, mlibc::page_size);
} else {
*stack_base = *stack;
}
*stack = (void *)((char *)*stack_base + *stack_size);
void **stack_it = (void **)*stack;
*--stack_it = arg;
*--stack_it = tcb;
*--stack_it = entry;
*stack = (void *)stack_it;
return 0;
}
}