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,2 @@
---
DisableFormat: true
@@ -0,0 +1,9 @@
.section .text
.global _start
_start:
mov $main, %rdi
call __mlibc_entry
.section .note.GNU-stack,"",%progbits
@@ -0,0 +1,14 @@
#include <stdint.h>
#include <stdlib.h>
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>
extern char **environ;
extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[])) {
// TODO: call __dlapi_enter, otherwise static builds will break (see Linux sysdeps)
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
@@ -0,0 +1,298 @@
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/thread-entry.hpp>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
namespace mlibc {
void sys_libc_log(const char *message) {
unsigned long res;
asm volatile ("syscall" : "=a"(res)
: "a"(50), "D"(message)
: "rcx", "r11", "rdx");
}
void sys_libc_panic() {
mlibc::infoLogger() << "\e[31mmlibc: panic!" << frg::endlog;
asm volatile ("syscall" :
: "a"(12), "D"(1)
: "rcx", "r11", "rdx");
}
int sys_tcb_set(void *pointer) {
int res;
asm volatile ("syscall" : "=a"(res)
: "a"(300), "D"(pointer)
: "rcx", "r11", "rdx");
return res;
}
int sys_anon_allocate(size_t size, void **pointer) {
void *ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(9), "D"(0), "S"(size)
: "rcx", "r11");
if (!ret)
return sys_errno;
*pointer = ret;
return 0;
}
int sys_anon_free(void *pointer, size_t size) {
int unused_return;
int sys_errno;
asm volatile ("syscall"
: "=a"(unused_return), "=d"(sys_errno)
: "a"(11), "D"(pointer), "S"(size)
: "rcx", "r11");
if (unused_return)
return sys_errno;
return 0;
}
#ifndef MLIBC_BUILDING_RTLD
void sys_exit(int status) {
asm volatile ("syscall" :
: "a"(12), "D"(status)
: "rcx", "r11", "rdx");
}
#endif
#ifndef MLIBC_BUILDING_RTLD
int sys_clock_get(int clock, time_t *secs, long *nanos) {
return 0;
}
#endif
int sys_open(const char *path, int flags, mode_t mode, int *fd) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(2), "D"(path), "S"(flags), "d"(0)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*fd = ret;
return 0;
}
int sys_close(int fd) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(3), "D"(fd)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
return 0;
}
int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
ssize_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(0), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*bytes_read = ret;
return 0;
}
#ifndef MLIBC_BUILDING_RTLD
int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
ssize_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(1), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*bytes_written = ret;
return 0;
}
#endif
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
off_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(8), "D"(fd), "S"(offset), "d"(whence)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*new_offset = ret;
return 0;
}
int sys_vm_map(void *hint, size_t size, int prot, int flags,
int fd, off_t offset, void **window) {
__ensure(flags & MAP_ANONYMOUS);
void *ret;
int sys_errno;
// mlibc::infoLogger() << "calling sys_vm_map with size: " << size << frg::endlog;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(9), "D"(hint), "S"(size)
: "rcx", "r11");
if (!ret)
return sys_errno;
*window = ret;
return 0;
}
int sys_vm_unmap(void *pointer, size_t size) {
return sys_anon_free(pointer, size);
}
int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
uint64_t err;
asm volatile ("syscall"
: "=d"(err)
: "a"(66), "D"(pointer), "S"(expected)
: "rcx", "r11");
if (err) {
return -1;
}
return 0;
}
int sys_futex_wake(int *pointer) {
uint64_t err;
asm volatile ("syscall"
: "=d"(err)
: "a"(65), "D"(pointer)
: "rcx", "r11");
if (err) {
return -1;
}
return 0;
}
// All remaining functions are disabled in ldso.
#ifndef MLIBC_BUILDING_RTLD
int sys_clone(void *tcb, pid_t *tid_out, void *stack) {
int tid;
asm volatile ("syscall"
: "=a"(tid)
: "a"(67), "D"(__mlibc_start_thread), "S"(stack), "d"(tcb)
: "rcx", "r11");
if (tid_out)
*tid_out = tid;
return 0;
}
void sys_thread_exit() {
asm volatile ("syscall"
:
: "a"(68)
: "rcx", "r11");
__builtin_trap();
}
int sys_sleep(time_t *secs, long *nanos) {
long ms = (*nanos / 1000000) + (*secs * 1000);
asm volatile ("syscall"
:
: "a"(6), "D"(ms)
: "rcx", "r11");
*secs = 0;
*nanos = 0;
return 0;
}
int sys_fork(pid_t *child) {
pid_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(57)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*child = ret;
return 0;
}
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(59), "D"(path), "S"(argv), "d"(envp)
: "rcx", "r11");
if (sys_errno != 0)
return sys_errno;
return 0;
}
pid_t sys_getpid() {
pid_t pid;
asm volatile ("syscall" : "=a"(pid)
: "a"(5)
: "rcx", "r11", "rdx");
return pid;
}
pid_t sys_getppid() {
pid_t ppid;
asm volatile ("syscall" : "=a"(ppid)
: "a"(14)
: "rcx", "r11", "rdx");
return ppid;
}
#endif // MLIBC_BUILDING_RTLD
} // namespace mlibc
@@ -0,0 +1,53 @@
#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");
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);
}
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,11 @@
.section .text
.global __mlibc_start_thread
__mlibc_start_thread:
pop %rdi
pop %rsi
pop %rdx
call __mlibc_enter_thread
.section .note.GNU-stack,"",%progbits
@@ -0,0 +1 @@
../../../../abis/dripos/access.h
@@ -0,0 +1 @@
../../../../abis/dripos/auxv.h
@@ -0,0 +1 @@
../../../../abis/dripos/blkcnt_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/blksize_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/clockid_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/dev_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/epoll.h
@@ -0,0 +1 @@
../../../../abis/dripos/errno.h
@@ -0,0 +1 @@
../../../../abis/dripos/fcntl.h
@@ -0,0 +1 @@
../../../../abis/linux/fsblkcnt_t.h
@@ -0,0 +1 @@
../../../../abis/linux/fsfilcnt_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/gid_t.h
+1
View File
@@ -0,0 +1 @@
../../../../abis/dripos/in.h
@@ -0,0 +1 @@
../../../../abis/dripos/ino_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/inotify.h
@@ -0,0 +1 @@
../../../../abis/linux/ioctls.h
+1
View File
@@ -0,0 +1 @@
../../../../abis/dripos/ipc.h
@@ -0,0 +1 @@
../../../../abis/dripos/limits.h
@@ -0,0 +1 @@
../../../../abis/dripos/mode_t.h
@@ -0,0 +1 @@
../../../../abis/linux/mqueue.h
+1
View File
@@ -0,0 +1 @@
../../../../abis/linux/msg.h
@@ -0,0 +1 @@
../../../../abis/dripos/nlink_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/packet.h
@@ -0,0 +1 @@
../../../../abis/dripos/pid_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/poll.h
@@ -0,0 +1 @@
../../../../abis/dripos/ptrace.h
@@ -0,0 +1 @@
../../../../abis/dripos/resource.h
@@ -0,0 +1 @@
../../../../abis/dripos/rlim_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/seek-whence.h
+1
View File
@@ -0,0 +1 @@
../../../../abis/dripos/shm.h
@@ -0,0 +1 @@
../../../../abis/dripos/sigevent.h
@@ -0,0 +1 @@
../../../../abis/dripos/signal.h
@@ -0,0 +1 @@
../../../../abis/linux/sigval.h
@@ -0,0 +1 @@
../../../../abis/dripos/socket.h
@@ -0,0 +1 @@
../../../../abis/linux/socklen_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/stat.h
@@ -0,0 +1 @@
../../../../abis/linux/statfs.h
@@ -0,0 +1 @@
../../../../abis/linux/statvfs.h
@@ -0,0 +1 @@
../../../../abis/linux/statx.h
@@ -0,0 +1 @@
../../../../abis/linux/suseconds_t.h
@@ -0,0 +1 @@
../../../../abis/dripos/termios.h
@@ -0,0 +1 @@
../../../../abis/linux/time.h
@@ -0,0 +1 @@
../../../../abis/dripos/uid_t.h
@@ -0,0 +1 @@
../../../../abis/linux/utmp-defines.h
@@ -0,0 +1 @@
../../../../abis/linux/utmpx.h
@@ -0,0 +1 @@
../../../../abis/dripos/utsname.h
@@ -0,0 +1 @@
../../../../abis/dripos/vm-flags.h
@@ -0,0 +1 @@
../../../../abis/dripos/wait.h
@@ -0,0 +1 @@
../../../../abis/linux/xattr.h
@@ -0,0 +1,10 @@
#pragma once
#include <mlibc/tcb.hpp>
extern "C" void __mlibc_start_thread(void);
extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb);
namespace mlibc {
void *prepare_stack(void *entry, void *user_arg, void *tcb);
}
@@ -0,0 +1,84 @@
sysdep_supported_options = {
'posix': true,
'linux': false,
'glibc': true,
'bsd': true,
}
rtld_sources += files(
'generic/generic.cpp'
)
libc_sources += files(
'generic/entry.cpp',
'generic/generic.cpp',
'generic/thread.cpp',
'generic/thread_entry.S'
)
if not no_headers
install_headers(
'include/abi-bits/auxv.h',
'include/abi-bits/seek-whence.h',
'include/abi-bits/vm-flags.h',
'include/abi-bits/errno.h',
'include/abi-bits/fcntl.h',
'include/abi-bits/in.h',
'include/abi-bits/resource.h',
'include/abi-bits/stat.h',
'include/abi-bits/statx.h',
'include/abi-bits/signal.h',
'include/abi-bits/socket.h',
'include/abi-bits/termios.h',
'include/abi-bits/blkcnt_t.h',
'include/abi-bits/blksize_t.h',
'include/abi-bits/dev_t.h',
'include/abi-bits/gid_t.h',
'include/abi-bits/ino_t.h',
'include/abi-bits/mode_t.h',
'include/abi-bits/nlink_t.h',
'include/abi-bits/pid_t.h',
'include/abi-bits/uid_t.h',
'include/abi-bits/access.h',
'include/abi-bits/wait.h',
'include/abi-bits/limits.h',
'include/abi-bits/utsname.h',
'include/abi-bits/ptrace.h',
'include/abi-bits/poll.h',
'include/abi-bits/epoll.h',
'include/abi-bits/packet.h',
'include/abi-bits/inotify.h',
'include/abi-bits/clockid_t.h',
'include/abi-bits/ipc.h',
'include/abi-bits/shm.h',
'include/abi-bits/mqueue.h',
'include/abi-bits/suseconds_t.h',
'include/abi-bits/fsfilcnt_t.h',
'include/abi-bits/fsblkcnt_t.h',
'include/abi-bits/socklen_t.h',
'include/abi-bits/statfs.h',
'include/abi-bits/statvfs.h',
'include/abi-bits/ioctls.h',
'include/abi-bits/xattr.h',
'include/abi-bits/msg.h',
'include/abi-bits/rlim_t.h',
'include/abi-bits/sigval.h',
'include/abi-bits/sigevent.h',
'include/abi-bits/utmpx.h',
'include/abi-bits/utmp-defines.h',
subdir: 'abi-bits',
follow_symlinks: true
)
endif
if not headers_only
crt = custom_target('crt1',
build_by_default: true,
command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'],
input: 'crt-x86_64/crt1.S',
output: 'crt1.o',
install: true,
install_dir: get_option('libdir')
)
endif