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,86 @@
#include <bits/ensure.h>
#include <bits/posix/posix_signal.h>
#include <keyronex/syscall.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/debug.hpp>
#include <mlibc/elf/startup.h>
#include <stdint.h>
#include <stdlib.h>
extern char **environ;
namespace mlibc {
int
sys_sigentry(void *sigentry)
{
uintptr_t ret = syscall1(kPXSysSigEntry, (uintptr_t)sigentry, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
[[noreturn]] int
sys_sigreturn(ucontext_t *context)
{
syscall1(kPXSysSigReturn, (uintptr_t)context, NULL);
__builtin_unreachable();
}
}
static void
do_stacktrace(ucontext_t *ctx)
{
size_t *base_ptr = (size_t *)ctx->uc_mcontext.gregs[REG_RBP];
mlibc::infoLogger() << "Stacktrace:" << frg::endlog;
mlibc::infoLogger() << " [" << (void *)ctx->uc_mcontext.gregs[REG_RIP]
<< "]" << frg::endlog;
for (;;) {
size_t old_bp = base_ptr[0];
size_t ret_addr = base_ptr[1];
if (!ret_addr)
break;
mlibc::infoLogger()
<< " [" << (void *)ret_addr << "]" << frg::endlog;
if (!old_bp)
break;
base_ptr = (size_t *)old_bp;
}
}
static void
__mlibc_sigentry(int which, siginfo_t *siginfo, void *handler,
bool is_sigaction, ucontext_t *ret_context)
{
if ((uintptr_t)handler == (uintptr_t)SIG_DFL) {
mlibc::infoLogger()
<< "mlibc: Unhandled signal " << which << frg::endlog;
do_stacktrace(ret_context);
mlibc::sys_exit(128 + which);
} else if ((uintptr_t)handler == (uintptr_t)SIG_IGN) {
/* epsilon */
} else {
if (is_sigaction)
((void (*)(int, siginfo_t *, void *))handler)(which,
siginfo, ret_context);
else
((void (*)(int))handler)(which);
}
mlibc::sys_sigreturn(ret_context);
__builtin_unreachable();
}
extern "C" void
__mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[]))
{
/* communicate the signal handler entry point to the kernel */
mlibc::sys_sigentry((void *)__mlibc_sigentry);
// 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,753 @@
#include <asm/ioctls.h>
#include <bits/ensure.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <frg/logging.hpp>
#include <keyronex/syscall.h>
#include <limits.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/debug.hpp>
#include <stdlib.h>
#define STUB_ONLY \
{ \
__ensure(!"STUB_ONLY function was called"); \
__builtin_unreachable(); \
}
namespace mlibc {
void
sys_libc_log(const char *message)
{
syscall1(kPXSysDebug, (uintptr_t)message, NULL);
}
void
sys_libc_panic()
{
sys_libc_log("\nMLIBC PANIC\n");
for (;;)
;
STUB_ONLY
}
void
sys_exit(int status)
{
syscall1(kPXSysExit, status, NULL);
mlibc::panicLogger() << "sys_exit() returned!" << frg::endlog;
__builtin_unreachable();
}
#ifndef MLIBC_BUILDING_RTLD
int
sys_tcgetattr(int fd, struct termios *attr)
{
int ret;
if (int r = sys_ioctl(fd, TCGETS, attr, &ret) != 0) {
return r;
}
return 0;
}
int
sys_tcsetattr(int fd, int optional_action, const struct termios *attr)
{
int ret;
switch (optional_action) {
case TCSANOW:
optional_action = TCSETS;
break;
case TCSADRAIN:
optional_action = TCSETSW;
break;
case TCSAFLUSH:
optional_action = TCSETSF;
break;
default:
__ensure(!"Unsupported tcsetattr");
}
if (int r = sys_ioctl(fd, optional_action, (void *)attr, &ret) != 0) {
return r;
}
return 0;
}
#endif
int
sys_tcb_set(void *pointer)
{
return syscall1(kPXSysSetFSBase, (uintptr_t)pointer, NULL);
}
int
sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout,
const sigset_t *sigmask, int *num_events)
{
uintptr_t ret = syscall4(kPXSysPPoll, (uintptr_t)fds, (uintptr_t)nfds,
(uintptr_t)timeout, (uintptr_t)sigmask, NULL);
if (int e = sc_error(ret); e)
return e;
*num_events = (ssize_t)ret;
return 0;
}
int
sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events)
{
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
return sys_ppoll(fds, count, timeout < 0 ? NULL : &ts, NULL,
num_events);
}
#ifndef MLIBC_BUILDING_RTLD
int
sys_pselect(int nfds, fd_set *read_set, fd_set *write_set, fd_set *except_set,
const struct timespec *timeout, const sigset_t *sigmask, int *num_events)
{
struct pollfd *fds = (struct pollfd *)malloc(
nfds * sizeof(struct pollfd));
for (int i = 0; i < nfds; i++) {
struct pollfd *fd = &fds[i];
memset(fd, 0, sizeof(struct pollfd));
if (read_set && FD_ISSET(i, read_set))
fd->events |= POLLIN; // TODO: Additional events.
if (write_set && FD_ISSET(i, write_set))
fd->events |= POLLOUT; // TODO: Additional events.
if (except_set && FD_ISSET(i, except_set))
fd->events |= POLLPRI;
if (!fd->events) {
fd->fd = -1;
continue;
}
fd->fd = i;
}
int e = sys_ppoll(fds, nfds, timeout, sigmask, num_events);
if (e != 0) {
free(fds);
return e;
}
fd_set res_read_set;
fd_set res_write_set;
fd_set res_except_set;
FD_ZERO(&res_read_set);
FD_ZERO(&res_write_set);
FD_ZERO(&res_except_set);
for (int i = 0; i < nfds; i++) {
struct pollfd *fd = &fds[i];
if (read_set && FD_ISSET(i, read_set) &&
fd->revents & (POLLIN | POLLERR | POLLHUP)) {
FD_SET(i, &res_read_set);
}
if (write_set && FD_ISSET(i, write_set) &&
fd->revents & (POLLOUT | POLLERR | POLLHUP)) {
FD_SET(i, &res_write_set);
}
if (except_set && FD_ISSET(i, except_set) &&
fd->revents & POLLPRI) {
FD_SET(i, &res_except_set);
}
}
free(fds);
if (read_set)
memcpy(read_set, &res_read_set, sizeof(fd_set));
if (write_set)
memcpy(write_set, &res_write_set, sizeof(fd_set));
if (except_set)
memcpy(except_set, &res_except_set, sizeof(fd_set));
return 0;
}
#endif
int
sys_fcntl(int fd, int request, va_list args, int *result)
{
auto ret = syscall3(kPXSysFCntl, fd, request, va_arg(args, uint64_t),
NULL);
if (int e = sc_error(ret); e)
return e;
*result = ret;
return 0;
}
int
sys_futex_wait(int *pointer, int expected, const struct timespec *time)
{
auto ret = syscall3(kPXSysFutexWait, (uintptr_t)pointer, expected,
(uintptr_t)time, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_futex_wake(int *pointer)
{
auto ret = syscall1(kPXSysFutexWake, (uintptr_t)pointer, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
#ifndef MLIBC_BUILDING_RTLD
int
sys_ioctl(int fd, unsigned long request, void *arg, int *result)
{
uintptr_t r = syscall3(kPXSysIOCtl, fd, request, (uintptr_t)arg, NULL);
if (int e = sc_error(r); e)
return e;
*result = r;
return 0;
}
#endif
int
sys_isatty(int fd)
{
uintptr_t ret = syscall1(kPXSysIsATTY, fd, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
#ifndef MLIBC_BUILDING_RTLD
int
sys_getcwd(char *buffer, size_t size)
{
uintptr_t ret = syscall2(kPXSysGetCWD, (uintptr_t)buffer, size, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
#endif
int
sys_dup(int fd, int flags, int *newfd)
{
uintptr_t ret = syscall2(kPXSysDup, fd, flags, NULL);
if (int e = sc_error(ret); e) {
return e;
}
*newfd = ret;
return 0;
}
int
sys_dup2(int fd, int flags, int newfd)
{
uintptr_t ret = syscall3(kPXSysDup3, fd, newfd, flags, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
int
sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd)
{
uintptr_t r = syscall4(kPXSysOpenAt, dirfd, (uintptr_t)path,
(uintptr_t)flags, (uintptr_t)mode, NULL);
if (int e = sc_error(r); e)
return e;
*fd = (int)r;
return 0;
}
int
sys_open(const char *path, int flags, mode_t mode, int *fd)
{
return sys_openat(AT_FDCWD, path, flags, mode, fd);
}
int
sys_open_dir(const char *path, int *handle)
{
return sys_open(path, O_DIRECTORY, 0, handle);
}
int
sys_read_entries(int fd, void *buffer, size_t max_size, size_t *bytes_read)
{
uintptr_t r = syscall3(kPXSysReadDir, fd, (uintptr_t)buffer, max_size,
NULL);
if (int e = sc_error(r); e)
return e;
*bytes_read = r;
return 0;
}
int
sys_close(int fd)
{
return (int)syscall1(kPXSysClose, fd, NULL);
}
int
sys_link(const char *old_path, const char *new_path)
{
uintptr_t ret = syscall2(kPXSysLink, (uintptr_t)old_path,
(uintptr_t)new_path, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_seek(int fd, off_t offset, int whence, off_t *new_offset)
{
uintptr_t ret = syscall3(kPXSysSeek, fd, offset, whence, NULL);
if (int e = sc_error(ret); e)
return e;
*new_offset = (ssize_t)ret;
return 0;
}
int
sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read)
{
uintptr_t ret = syscall3(kPXSysRead, fd, (uintptr_t)buf,
(uintptr_t)count, NULL);
if (int e = sc_error(ret); e)
return e;
*bytes_read = (ssize_t)ret;
return 0;
}
int
sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written)
{
uintptr_t ret = syscall3(kPXSysWrite, fd, (uintptr_t)buf,
(uintptr_t)count, NULL);
if (int e = sc_error(ret); e)
return e;
*bytes_written = (ssize_t)ret;
return 0;
}
int
sys_readlink(const char *path, void *buffer, size_t max_size, ssize_t *length)
{
uintptr_t ret = syscall3(kPXSysReadLink, (uintptr_t)path,
(uintptr_t)buffer, (uintptr_t)max_size, NULL);
if (int e = sc_error(ret); e)
return e;
*length = (ssize_t)ret;
return 0;
}
int
sys_pipe(int *fds, int flags)
{
uintptr_t ret = syscall2(kPXSysPipe, (uintptr_t)fds, flags, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_unlinkat(int fd, const char *path, int flags)
{
uintptr_t ret = syscall3(kPXSysUnlinkAt, fd, (uintptr_t)path, flags,
NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset,
void **window)
{
uintptr_t r = syscall6(kPXSysMmap, (uintptr_t)hint, size, prot, flags,
fd, offset, NULL);
if (int e = sc_error(r); e)
return e;
*window = (void *)r;
return 0;
}
int
sys_vm_unmap(void *pointer, size_t size)
{
uintptr_t r = syscall2(kPXSysMunmap, (uintptr_t)pointer, size, NULL);
if (int e = sc_error(r); e)
return e;
return 0;
}
int
sys_vm_protect(void *pointer, size_t size, int prot)
{
mlibc::infoLogger() << "mlibc: sys_vm_protect(" << pointer << ", "
<< size << ", " << prot << "); stub!\n"
<< frg::endlog;
return 0;
}
int
sys_anon_allocate(size_t size, void **pointer)
{
return sys_vm_map(NULL, size, PROT_EXEC | PROT_READ | PROT_WRITE,
MAP_ANONYMOUS, -1, 0, pointer);
}
int
sys_anon_free(void *pointer, size_t size)
{
return sys_vm_unmap(pointer, size);
}
pid_t
sys_getpid()
{
return syscall0(kPXSysGetPID, NULL);
}
pid_t
sys_getppid()
{
return syscall0(kPXSysGetPPID, NULL);
}
uid_t
sys_getuid()
{
return 0;
}
uid_t
sys_geteuid()
{
return 0;
}
gid_t
sys_getgid()
{
return 0;
}
int
sys_getsid(pid_t pid, pid_t *sid)
{
auto ret = syscall1(kPXSysGetSID, pid, NULL);
if (int e = sc_error(ret); e)
return e;
*sid = (pid_t)(ret);
return 0;
}
int
sys_setgid(gid_t gid)
{
(void)gid;
return 0;
}
int
sys_getpgid(pid_t pid, pid_t *out)
{
auto ret = syscall1(kPXSysGetPGID, pid, NULL);
if (int e = sc_error(ret); e)
return e;
*out = (pid_t)(ret);
return 0;
}
int
sys_setpgid(pid_t pid, pid_t pgid)
{
auto ret = syscall2(kPXSysSetPGID, pid, pgid, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_setsid(pid_t *sid)
{
auto ret = syscall0(kPXSysSetSID, NULL);
if (int e = sc_error(ret); e)
return e;
*sid = (pid_t)ret;
return 0;
}
gid_t
sys_getegid()
{
mlibc::infoLogger() << "mlibc: " << __func__ << " is a stub!\n"
<< frg::endlog;
return 0;
}
pid_t
sys_gettid()
{
return syscall0(kPXSysGetTID, NULL);
}
int
sys_clock_get(int clock, time_t *secs, long *nanos)
{
auto ret = syscall1(kPXSysClockGet, clock, NULL);
*secs = ret / 1000000000;
*nanos = ret % 1000000000;
return 0;
}
int
sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
struct stat *statbuf)
{
uintptr_t r;
enum posix_stat_kind kind;
switch (fsfdt) {
case fsfd_target::fd:
kind = kPXStatKindFD;
break;
case fsfd_target::path:
kind = kPXStatKindCWD;
break;
case fsfd_target::fd_path:
kind = kPXStatKindAt;
break;
default:
__ensure(!"stat: Invalid fsfdt");
__builtin_unreachable();
}
r = syscall5(kPXSysStat, kind, fd, (uintptr_t)path, flags,
(uintptr_t)statbuf, NULL);
if (int e = sc_error(r); e)
return e;
return 0;
}
int
sys_statfs(const char *path, struct statfs *buf)
{
uintptr_t ret = syscall2(kPXSysStatFS, (uintptr_t)path, (uintptr_t)buf,
NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_statvfs(const char *path, struct statvfs *buf)
{
struct statfs sb;
uintptr_t ret = syscall2(kPXSysStatFS, (uintptr_t)path, (uintptr_t)&sb,
NULL);
if (int e = sc_error(ret); e)
return e;
buf->f_bsize = sb.f_bsize;
buf->f_frsize = sb.f_frsize;
buf->f_blocks = sb.f_blocks;
buf->f_bfree = sb.f_bfree;
buf->f_bavail = sb.f_bavail;
buf->f_files = sb.f_files;
buf->f_ffree = sb.f_ffree;
buf->f_favail = sb.f_ffree;
buf->f_fsid = sb.f_fsid.__val[1] | (uint64_t)sb.f_fsid.__val[0] << 32;
buf->f_flag = sb.f_flags;
buf->f_namemax = sb.f_namelen;
return 0;
}
int
sys_faccessat(int dirfd, const char *pathname, int mode, int flags)
{
(void)flags;
struct stat buf;
if (int r = sys_stat(dirfd == AT_FDCWD ? fsfd_target::path :
fsfd_target::fd_path,
dirfd, pathname, mode & AT_SYMLINK_FOLLOW, &buf)) {
return r;
}
return 0;
}
int
sys_access(const char *path, int mode)
{
return sys_faccessat(AT_FDCWD, path, mode, 0);
}
int
sys_fork(pid_t *child)
{
uintptr_t ret = syscall0(kPXSysFork, NULL);
if (int e = sc_error(ret); e)
return e;
*child = (int)ret;
return 0;
}
int
sys_execve(const char *path, char *const argv[], char *const envp[])
{
uintptr_t ret = syscall3(kPXSysExecVE, (uintptr_t)path, (uintptr_t)argv,
(uintptr_t)envp, NULL);
if (int e = sc_error(ret); e)
return e;
mlibc::panicLogger() << "execve returned! " << ret << frg::endlog;
__builtin_unreachable();
}
int
sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru,
pid_t *ret_pid)
{
(void)ru;
uintptr_t ret = syscall3(kPXSysWaitPID, pid, (uintptr_t)status, flags,
NULL);
if (int e = sc_error(ret); e)
return e;
*ret_pid = (pid_t)ret;
return 0;
}
#ifndef MLIBC_BUILDING_RTLD
int
sys_sleep(time_t *sec, long *nanosec)
{
auto ret = syscall1(kPXSysSleep, *sec * 1000000000 + *nanosec, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_uname(struct utsname *buf)
{
uintptr_t ret = syscall1(kPXSysUTSName, (uintptr_t)buf, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_gethostname(char *buf, size_t bufsize)
{
struct utsname uname_buf;
if (auto e = sys_uname(&uname_buf); e)
return e;
auto node_len = strlen(uname_buf.nodename);
if (node_len >= bufsize)
return ENAMETOOLONG;
memcpy(buf, uname_buf.nodename, node_len);
buf[node_len] = '\0';
return 0;
}
int
sys_fsync(int)
{
mlibc::infoLogger() << "mlibc: fsync is a stub" << frg::endlog;
return 0;
}
int
sys_getentropy(void *buffer, size_t length)
{
/* todo: improve lmao */
mlibc::infoLogger() << "mlibc: getentropy is a stub" << frg::endlog;
memset(buffer, 123, length);
return 0;
}
#endif
int
sys_mkdir(const char *path, mode_t mode)
{
return sys_mkdirat(AT_FDCWD, path, mode);
}
int
sys_mkdirat(int dirfd, const char *path, mode_t mode)
{
uintptr_t ret = syscall3(kPXSysMkDirAt, dirfd, (uintptr_t)path, mode,
NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_chdir(const char *path)
{
uintptr_t ret = syscall1(kPXSysChDir, (uintptr_t)path, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_umask(mode_t mode, mode_t *old)
{
uintptr_t ret = syscall1(kPXSysUMask, mode, NULL);
if (int e = sc_error(ret); e)
return e;
*old = (mode_t)ret;
return 0;
}
int
sys_rename(const char *old_path, const char *new_path)
{
return sys_renameat(AT_FDCWD, old_path, AT_FDCWD, new_path);
}
int
sys_renameat(int old_dirfd, const char *old_path, int new_dirfd,
const char *new_path)
{
auto ret = syscall4(kPXSysRenameAt, old_dirfd, (uintptr_t)old_path,
new_dirfd, (uintptr_t)new_path, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
} // namespace mlibc
@@ -0,0 +1,41 @@
#include <sys/types.h>
#include <keyronex/syscall.h>
#include <mlibc/ansi-sysdeps.hpp>
#include <mlibc/debug.hpp>
#include <mlibc/posix-sysdeps.hpp>
#include <mlibc/linux-sysdeps.hpp>
namespace mlibc {
int
sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, int timeout,
const sigset_t *sigmask, int *raised)
{
uintptr_t ret = syscall5(kPXSysEPollWait, epfd, (uintptr_t)ev, n,
timeout, (uintptr_t)sigmask, NULL);
if (int e = sc_error(ret); e)
return e;
*raised = ret;
return 0;
}
int
sys_epoll_create(int flags, int *fd)
{
uintptr_t ret = syscall1(kPXSysEPollCreate, flags, NULL);
if (int e = sc_error(ret); e)
return e;
*fd = ret;
return 0;
}
int
sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev)
{
uintptr_t ret = syscall4(kPXSysEPollCtl, epfd, mode, fd, (uintptr_t)ev,
NULL);
return sc_error(ret);
}
}
@@ -0,0 +1,66 @@
#include <sys/types.h>
#include <keyronex/syscall.h>
#include <mlibc/ansi-sysdeps.hpp>
#include <mlibc/posix-sysdeps.hpp>
#include <mlibc/debug.hpp>
namespace mlibc {
int
sys_sigprocmask(int how, const sigset_t *__restrict set,
sigset_t *__restrict retrieve)
{
auto ret = syscall3(kPXSysSigMask, how, (uintptr_t)set,
(uintptr_t)retrieve, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
int
sys_sigaction(int signal, const struct sigaction *__restrict action,
struct sigaction *__restrict oldAction)
{
auto ret = syscall3(kPXSysSigAction, signal, (uintptr_t)action,
(uintptr_t)oldAction, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
int
sys_kill(int pid, int signal)
{
if (signal == 0) {
mlibc::infoLogger() << "Sending signal 0! Allowing" << frg::endlog;
return 0;
}
auto ret = syscall2(kPXSysSigSend, pid, signal, NULL);
if (int e = sc_error(ret); e) {
return e;
}
return 0;
}
int
sys_sigsuspend(const sigset_t *set)
{
auto ret = syscall1(kPXSysSigSuspend, (uintptr_t)set, NULL);
if (int e = sc_error(ret); e) {
return e;
}
mlibc::panicLogger()
<< "Unexpected zero return from sigsuspend()" << frg::endlog;
return 0;
}
}
@@ -0,0 +1,119 @@
#include <sys/errno.h>
#include <keyronex/syscall.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/allocator.hpp>
#include <mlibc/debug.hpp>
#define log_unimplemented() \
mlibc::infoLogger() << "mlibc: " << __PRETTY_FUNCTION__ \
<< " is a stub!" << frg::endlog;
namespace mlibc {
int
sys_socket(int family, int type, int protocol, int *fd)
{
auto ret = syscall3(kPXSysSocket, family, type, protocol, NULL);
if (int e = sc_error(ret); e)
return e;
*fd = ret;
return 0;
}
int
sys_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
{
auto ret = syscall3(kPXSysBind, fd, (uintptr_t)addr, addrlen, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
{
auto ret = syscall3(kPXSysConnect, fd, (uintptr_t)addr, addrlen, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_listen(int fd, int backlog)
{
auto ret = syscall2(kPXSysListen, fd, backlog, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_accept(int fd, int *newfd, struct sockaddr *addr, socklen_t *addrlen,
int flags)
{
auto ret = syscall4(kPXSysAccept, fd, (uintptr_t)addr,
(uintptr_t)addrlen, flags, NULL);
if (int e = sc_error(ret); e)
return e;
*newfd = ret;
return 0;
}
int
sys_msg_send(int fd, const struct msghdr *msg, int flags, ssize_t *length)
{
auto ret = syscall3(kPXSysSendMsg, fd, (uintptr_t)msg, flags, NULL);
if (int e = sc_error(ret); e)
return e;
*length = ret;
return 0;
}
int
sys_msg_recv(int fd, struct msghdr *msg, int flags, ssize_t *length)
{
auto ret = syscall3(kPXSysRecvMsg, fd, (uintptr_t)msg, flags, NULL);
if (int e = sc_error(ret); e)
return e;
*length = ret;
return 0;
}
int
sys_socketpair(int domain, int type_and_flags, int proto, int *fds)
{
auto ret = syscall4(kPXSysSocketPair, domain, type_and_flags, proto,
(uintptr_t)fds, NULL);
if (int e = sc_error(ret); e)
return e;
return 0;
}
int
sys_getsockopt(int fd, int layer, int number, void *__restrict buffer,
socklen_t *__restrict size)
{
(void)fd;
(void)layer;
(void)number;
(void)buffer;
(void)size;
log_unimplemented();
return ENOSYS;
}
int
sys_setsockopt(int fd, int layer, int number, const void *buffer,
socklen_t size)
{
(void)fd;
(void)layer;
(void)number;
(void)buffer;
(void)size;
log_unimplemented();
return ENOSYS;
}
}
@@ -0,0 +1,9 @@
.section .text
.global __mlibc_thread_entry
__mlibc_thread_entry:
pop %rdi
pop %rsi
pop %rdx
call __mlibc_thread_trampoline
.section .note.GNU-stack,"",%progbits
@@ -0,0 +1,80 @@
#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 <keyronex/syscall.h>
extern "C" void __mlibc_thread_trampoline(void *(*fn)(void *), Tcb *tcb, void *arg) {
if (mlibc::sys_tcb_set(tcb)) {
__ensure(!"failed to set tcb for new thread");
}
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 0x400000
namespace mlibc {
extern "C" void __mlibc_thread_entry();
int sys_clone(void *tcb, pid_t *tid_out, void *stack) {
(void)tcb;
auto ret = syscall2(kPXSysForkThread, (uintptr_t)__mlibc_thread_entry, (uintptr_t)stack, NULL);
if (int e = sc_error(ret); e)
return e;
*tid_out = ret;
return 0;
}
int sys_prepare_stack(void **stack, void *entry, void *arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) {
// TODO guard
mlibc::infoLogger() << "mlibc: sys_prepare_stack() does not setup a guard!" << frg::endlog;
*guard_size = 0;
*stack_size = *stack_size ? *stack_size : DEFAULT_STACK;
if (!*stack) {
*stack_base = mmap(NULL, *stack_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (*stack_base == MAP_FAILED) {
return errno;
}
} 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;
}
void sys_thread_exit() {
mlibc::panicLogger() << "mlibc: sys_thread_exit unimplemented!" << frg::endlog;
__builtin_unreachable();
}
}