From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/sysdeps/keyronex/generic/entry.cpp | 108 +++ lib/mlibc/sysdeps/keyronex/generic/generic.cpp | 753 +++++++++++++++++++++ lib/mlibc/sysdeps/keyronex/generic/linux.cpp | 41 ++ lib/mlibc/sysdeps/keyronex/generic/signal.cpp | 66 ++ lib/mlibc/sysdeps/keyronex/generic/socket.cpp | 119 ++++ lib/mlibc/sysdeps/keyronex/generic/thread.S | 9 + lib/mlibc/sysdeps/keyronex/generic/thread.cpp | 80 +++ .../sysdeps/keyronex/include/abi-bits/access.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/auxv.h | 1 + .../sysdeps/keyronex/include/abi-bits/blkcnt_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/blksize_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/clockid_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/dev_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/epoll.h | 1 + .../sysdeps/keyronex/include/abi-bits/errno.h | 1 + .../sysdeps/keyronex/include/abi-bits/fcntl.h | 1 + .../sysdeps/keyronex/include/abi-bits/fsblkcnt_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/fsfilcnt_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/gid_t.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/in.h | 1 + .../sysdeps/keyronex/include/abi-bits/ino_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/inotify.h | 1 + .../sysdeps/keyronex/include/abi-bits/ioctls.h | 1 + .../sysdeps/keyronex/include/abi-bits/limits.h | 1 + .../sysdeps/keyronex/include/abi-bits/mode_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/mqueue.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/msg.h | 1 + .../sysdeps/keyronex/include/abi-bits/nlink_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/packet.h | 1 + .../sysdeps/keyronex/include/abi-bits/pid_t.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/poll.h | 1 + .../sysdeps/keyronex/include/abi-bits/ptrace.h | 1 + .../sysdeps/keyronex/include/abi-bits/reboot.h | 1 + .../sysdeps/keyronex/include/abi-bits/resource.h | 1 + .../keyronex/include/abi-bits/seek-whence.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/shm.h | 1 + .../sysdeps/keyronex/include/abi-bits/signal.h | 1 + .../sysdeps/keyronex/include/abi-bits/socket.h | 1 + .../sysdeps/keyronex/include/abi-bits/socklen_t.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/stat.h | 1 + .../sysdeps/keyronex/include/abi-bits/statfs.h | 1 + .../sysdeps/keyronex/include/abi-bits/statvfs.h | 1 + .../keyronex/include/abi-bits/suseconds_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/termios.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/time.h | 1 + .../sysdeps/keyronex/include/abi-bits/uid_t.h | 1 + .../sysdeps/keyronex/include/abi-bits/utsname.h | 1 + .../sysdeps/keyronex/include/abi-bits/vm-flags.h | 1 + lib/mlibc/sysdeps/keyronex/include/abi-bits/wait.h | 1 + .../sysdeps/keyronex/include/abi-bits/xattr.h | 1 + .../sysdeps/keyronex/include/keyronex/syscall.h | 213 ++++++ lib/mlibc/sysdeps/keyronex/meson.build | 98 +++ lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crt0.S | 7 + lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crti.S | 11 + lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crtn.S | 9 + 55 files changed, 1557 insertions(+) create mode 100644 lib/mlibc/sysdeps/keyronex/generic/entry.cpp create mode 100644 lib/mlibc/sysdeps/keyronex/generic/generic.cpp create mode 100644 lib/mlibc/sysdeps/keyronex/generic/linux.cpp create mode 100644 lib/mlibc/sysdeps/keyronex/generic/signal.cpp create mode 100644 lib/mlibc/sysdeps/keyronex/generic/socket.cpp create mode 100644 lib/mlibc/sysdeps/keyronex/generic/thread.S create mode 100644 lib/mlibc/sysdeps/keyronex/generic/thread.cpp create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/access.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/auxv.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/blkcnt_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/blksize_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/clockid_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/dev_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/epoll.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/errno.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/fcntl.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/fsblkcnt_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/fsfilcnt_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/gid_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/in.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/ino_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/inotify.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/ioctls.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/limits.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/mode_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/mqueue.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/msg.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/nlink_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/packet.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/pid_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/poll.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/ptrace.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/reboot.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/resource.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/seek-whence.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/shm.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/signal.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/socket.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/socklen_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/stat.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/statfs.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/statvfs.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/suseconds_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/termios.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/time.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/uid_t.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/utsname.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/vm-flags.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/wait.h create mode 120000 lib/mlibc/sysdeps/keyronex/include/abi-bits/xattr.h create mode 100644 lib/mlibc/sysdeps/keyronex/include/keyronex/syscall.h create mode 100644 lib/mlibc/sysdeps/keyronex/meson.build create mode 100644 lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crt0.S create mode 100644 lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crti.S create mode 100644 lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crtn.S (limited to 'lib/mlibc/sysdeps/keyronex') diff --git a/lib/mlibc/sysdeps/keyronex/generic/entry.cpp b/lib/mlibc/sysdeps/keyronex/generic/entry.cpp new file mode 100644 index 0000000..ff15c62 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/entry.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +// defined by the POSIX library +void __mlibc_initLocale(); + +extern "C" uintptr_t *__dlapi_entrystack(); + +extern char **environ; +static mlibc::exec_stack_data __mlibc_stack_data; + +struct LibraryGuard { + LibraryGuard(); +}; + +static LibraryGuard guard; + +LibraryGuard::LibraryGuard() +{ + __mlibc_initLocale(); + + // Parse the exec() stack. + mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data); + mlibc::set_startup_data(__mlibc_stack_data.argc, + __mlibc_stack_data.argv, __mlibc_stack_data.envp); +} + +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_stack_data.argc, __mlibc_stack_data.argv, + environ); + exit(result); +} diff --git a/lib/mlibc/sysdeps/keyronex/generic/generic.cpp b/lib/mlibc/sysdeps/keyronex/generic/generic.cpp new file mode 100644 index 0000000..a3e77fb --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/generic.cpp @@ -0,0 +1,753 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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_RTDL +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_RTDL +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_RTDL +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_RTDL +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_RTDL +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 diff --git a/lib/mlibc/sysdeps/keyronex/generic/linux.cpp b/lib/mlibc/sysdeps/keyronex/generic/linux.cpp new file mode 100644 index 0000000..9b4a4c7 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/linux.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include +#include +#include +#include + +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); +} + +} \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/generic/signal.cpp b/lib/mlibc/sysdeps/keyronex/generic/signal.cpp new file mode 100644 index 0000000..ead3dee --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/signal.cpp @@ -0,0 +1,66 @@ +#include + +#include +#include +#include +#include + +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; +} + +} \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/generic/socket.cpp b/lib/mlibc/sysdeps/keyronex/generic/socket.cpp new file mode 100644 index 0000000..c6453bf --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/socket.cpp @@ -0,0 +1,119 @@ +#include + +#include +#include +#include +#include + +#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; +} + +} diff --git a/lib/mlibc/sysdeps/keyronex/generic/thread.S b/lib/mlibc/sysdeps/keyronex/generic/thread.S new file mode 100644 index 0000000..47ab6a9 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/thread.S @@ -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 diff --git a/lib/mlibc/sysdeps/keyronex/generic/thread.cpp b/lib/mlibc/sysdeps/keyronex/generic/thread.cpp new file mode 100644 index 0000000..cafb74a --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/generic/thread.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +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(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(); + } + + +} diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/access.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/access.h new file mode 120000 index 0000000..cb83931 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/access.h @@ -0,0 +1 @@ +../../../../abis/linux/access.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/auxv.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/auxv.h new file mode 120000 index 0000000..c43f878 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/auxv.h @@ -0,0 +1 @@ +../../../../abis/linux/auxv.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/blkcnt_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/blkcnt_t.h new file mode 120000 index 0000000..0b0ec27 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/blkcnt_t.h @@ -0,0 +1 @@ +../../../../abis/linux/blkcnt_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/blksize_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/blksize_t.h new file mode 120000 index 0000000..7dc8d7c --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/blksize_t.h @@ -0,0 +1 @@ +../../../../abis/linux/blksize_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/clockid_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/clockid_t.h new file mode 120000 index 0000000..6a42da5 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/clockid_t.h @@ -0,0 +1 @@ +../../../../abis/linux/clockid_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/dev_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/dev_t.h new file mode 120000 index 0000000..bca881e --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/dev_t.h @@ -0,0 +1 @@ +../../../../abis/linux/dev_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/epoll.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/epoll.h new file mode 120000 index 0000000..eb4b76d --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/epoll.h @@ -0,0 +1 @@ +../../../../abis/linux/epoll.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/errno.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/errno.h new file mode 120000 index 0000000..6e507de --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/errno.h @@ -0,0 +1 @@ +../../../../abis/linux/errno.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/fcntl.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fcntl.h new file mode 120000 index 0000000..463e2c9 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fcntl.h @@ -0,0 +1 @@ +../../../../abis/linux/fcntl.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsblkcnt_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsblkcnt_t.h new file mode 120000 index 0000000..898dfb2 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsblkcnt_t.h @@ -0,0 +1 @@ +../../../../abis/linux/fsblkcnt_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsfilcnt_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsfilcnt_t.h new file mode 120000 index 0000000..791755c --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/fsfilcnt_t.h @@ -0,0 +1 @@ +../../../../abis/linux/fsfilcnt_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/gid_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/gid_t.h new file mode 120000 index 0000000..abce6d6 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/gid_t.h @@ -0,0 +1 @@ +../../../../abis/linux/gid_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/in.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/in.h new file mode 120000 index 0000000..418d1d5 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/in.h @@ -0,0 +1 @@ +../../../../abis/linux/in.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/ino_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ino_t.h new file mode 120000 index 0000000..4c20aca --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ino_t.h @@ -0,0 +1 @@ +../../../../abis/linux/ino_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/inotify.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/inotify.h new file mode 120000 index 0000000..b5cb282 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/inotify.h @@ -0,0 +1 @@ +../../../../abis/linux/inotify.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/ioctls.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ioctls.h new file mode 120000 index 0000000..595106b --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ioctls.h @@ -0,0 +1 @@ +../../../../abis/linux/ioctls.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/limits.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/limits.h new file mode 120000 index 0000000..6c88db2 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/limits.h @@ -0,0 +1 @@ +../../../../abis/linux/limits.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/mode_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/mode_t.h new file mode 120000 index 0000000..5d78fdf --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/mode_t.h @@ -0,0 +1 @@ +../../../../abis/linux/mode_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/mqueue.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/mqueue.h new file mode 120000 index 0000000..fa87b07 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/mqueue.h @@ -0,0 +1 @@ +../../../../abis/linux/mqueue.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/msg.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/msg.h new file mode 120000 index 0000000..f402b49 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/msg.h @@ -0,0 +1 @@ +../../../../abis/linux/msg.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/nlink_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/nlink_t.h new file mode 120000 index 0000000..bb3b625 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/nlink_t.h @@ -0,0 +1 @@ +../../../../abis/linux/nlink_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/packet.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/packet.h new file mode 120000 index 0000000..998ef1a --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/packet.h @@ -0,0 +1 @@ +../../../../abis/linux/packet.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/pid_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/pid_t.h new file mode 120000 index 0000000..baa90f6 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/pid_t.h @@ -0,0 +1 @@ +../../../../abis/linux/pid_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/poll.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/poll.h new file mode 120000 index 0000000..8ea6a0a --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/poll.h @@ -0,0 +1 @@ +../../../../abis/linux/poll.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/ptrace.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ptrace.h new file mode 120000 index 0000000..b2517b2 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/ptrace.h @@ -0,0 +1 @@ +../../../../abis/linux/ptrace.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/reboot.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/reboot.h new file mode 120000 index 0000000..77013a4 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/reboot.h @@ -0,0 +1 @@ +../../../../abis/linux/reboot.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/resource.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/resource.h new file mode 120000 index 0000000..88d7402 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/resource.h @@ -0,0 +1 @@ +../../../../abis/linux/resource.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/seek-whence.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/seek-whence.h new file mode 120000 index 0000000..df7bccf --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/seek-whence.h @@ -0,0 +1 @@ +../../../../abis/linux/seek-whence.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/shm.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/shm.h new file mode 120000 index 0000000..067d8c4 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/shm.h @@ -0,0 +1 @@ +../../../../abis/linux/shm.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/signal.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/signal.h new file mode 120000 index 0000000..4dcb0b7 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/signal.h @@ -0,0 +1 @@ +../../../../abis/linux/signal.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/socket.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/socket.h new file mode 120000 index 0000000..f1dc016 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/socket.h @@ -0,0 +1 @@ +../../../../abis/linux/socket.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/socklen_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/socklen_t.h new file mode 120000 index 0000000..41f3b11 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/socklen_t.h @@ -0,0 +1 @@ +../../../../abis/linux/socklen_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/stat.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/stat.h new file mode 120000 index 0000000..1f63b41 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/stat.h @@ -0,0 +1 @@ +../../../../abis/linux/stat.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/statfs.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/statfs.h new file mode 120000 index 0000000..e3d202f --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/statfs.h @@ -0,0 +1 @@ +../../../../abis/linux/statfs.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/statvfs.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/statvfs.h new file mode 120000 index 0000000..1fc80c2 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/statvfs.h @@ -0,0 +1 @@ +../../../../abis/linux/statvfs.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/suseconds_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/suseconds_t.h new file mode 120000 index 0000000..9ed6597 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/suseconds_t.h @@ -0,0 +1 @@ +../../../../abis/linux/suseconds_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/termios.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/termios.h new file mode 120000 index 0000000..ee8f0b0 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/termios.h @@ -0,0 +1 @@ +../../../../abis/linux/termios.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/time.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/time.h new file mode 120000 index 0000000..2a02625 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/time.h @@ -0,0 +1 @@ +../../../../abis/linux/time.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/uid_t.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/uid_t.h new file mode 120000 index 0000000..b306777 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/uid_t.h @@ -0,0 +1 @@ +../../../../abis/linux/uid_t.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/utsname.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/utsname.h new file mode 120000 index 0000000..b285754 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/utsname.h @@ -0,0 +1 @@ +../../../../abis/linux/utsname.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/vm-flags.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/vm-flags.h new file mode 120000 index 0000000..bbe258c --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/vm-flags.h @@ -0,0 +1 @@ +../../../../abis/linux/vm-flags.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/wait.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/wait.h new file mode 120000 index 0000000..feb2840 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/wait.h @@ -0,0 +1 @@ +../../../../abis/linux/wait.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/abi-bits/xattr.h b/lib/mlibc/sysdeps/keyronex/include/abi-bits/xattr.h new file mode 120000 index 0000000..66412d7 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/abi-bits/xattr.h @@ -0,0 +1 @@ +../../../../abis/linux/xattr.h \ No newline at end of file diff --git a/lib/mlibc/sysdeps/keyronex/include/keyronex/syscall.h b/lib/mlibc/sysdeps/keyronex/include/keyronex/syscall.h new file mode 100644 index 0000000..9cbafd0 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/include/keyronex/syscall.h @@ -0,0 +1,213 @@ +#ifndef _KEYRONEX__SYSCALL_H +#define _KEYRONEX__SYSCALL_H + +#include + +enum posix_syscall { + /*! debug print */ + kPXSysDebug, + kPXSysMmap, + kPXSysMunmap, + + kPXSysIOCtl, + kPXSysOpenAt, + kPXSysClose, + kPXSysRead, + kPXSysReadLink, + kPXSysWrite, + kPXSysSeek, + kPXSysPPoll, + kPXSysIsATTY, + kPXSysReadDir, + kPXSysStat, + kPXSysUnlinkAt, + kPXSysGetCWD, + kPXSysPipe, + kPXSysDup, + kPXSysDup3, + kPXSysLink, + kPXSysChDir, + kPXSysUMask, + kPXSysMkDirAt, + kPXSysRenameAt, + kPXSysStatFS, + kPXSysFCntl, + + kPXSysSetFSBase, + kPXSysExecVE, + kPXSysExit, + kPXSysFork, + kPXSysWaitPID, + kPXSysGetPID, + kPXSysGetPPID, + kPXSysGetPGID, + kPXSysSetPGID, + kPXSysGetSID, + kPXSysSetSID, + kPXSysGetTID, + + kPXSysSigAction, + kPXSysSigMask, + kPXSysSigSend, + kPXSysSigSuspend, + kPXSysSigTimedWait, + + kPXSysSocket, + kPXSysBind, + kPXSysConnect, + kPXSysListen, + kPXSysAccept, + kPXSysSendMsg, + kPXSysRecvMsg, + kPXSysSocketPair, + kPXSysGetSockOpt, + kPXSysSetSockOpt, + + kPXSysEPollCreate, + kPXSysEPollCtl, + kPXSysEPollWait, + + kPXSysSleep, + kPXSysUTSName, + kPXSysClockGet, + + kPXSysForkThread, + kPXSysFutexWait, + kPXSysFutexWake, + + /*! register signal entry function */ + kPXSysSigEntry, + /*! return from a signal */ + kPXSysSigReturn, +}; + +enum posix_stat_kind { + kPXStatKindFD, + kPXStatKindAt, + kPXStatKindCWD, +}; + +#if defined(__x86_64__) +static inline uintptr_t +syscall0(uintptr_t num, uintptr_t *out) +{ + uintptr_t ret, ret2; + asm volatile("int $0x80" : "=a"(ret), "=D"(ret2) : "a"(num) : "memory"); + if (out) + *out = ret2; + return ret; +} + +static inline uintptr_t +syscall1(uintptr_t num, uintptr_t arg1, uintptr_t *out) +{ + uintptr_t ret, ret2; + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1) + : "memory"); + if (out) + *out = ret2; + return ret; +} + +static inline uintptr_t +syscall2(uintptr_t num, uintptr_t arg1, uintptr_t arg2, uintptr_t *out) +{ + uintptr_t ret, ret2; + + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1), "S"(arg2) + : "memory"); + + if (out) + *out = ret2; + + return ret; +} + +static inline uintptr_t +syscall3(intptr_t num, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, + uintptr_t *out) +{ + uintptr_t ret, ret2; + + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1), "S"(arg2), "d"(arg3) + : "memory"); + + if (out) + *out = ret2; + + return ret; +} + +static inline uintptr_t +syscall4(intptr_t num, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, + uintptr_t arg4, uintptr_t *out) +{ + register uintptr_t r10 asm("r10") = arg4; + uintptr_t ret, ret2; + + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1), "S"(arg2), "d"(arg3), "r"(r10) + : "memory"); + + if (out) + *out = ret2; + + return ret; +} + +static inline uintptr_t +syscall5(uintptr_t num, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, + uintptr_t arg4, uintptr_t arg5, uintptr_t *out) +{ + register uintptr_t r10 asm("r10") = arg4, r8 asm("r8") = arg5; + uintptr_t ret, ret2; + + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1), "S"(arg2), "d"(arg3), "r"(r10), + "r"(r8) + : "memory"); + + if (out) + *out = ret2; + + return ret; +} + +static inline uintptr_t +syscall6(uintptr_t num, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, uintptr_t *out) +{ + register uintptr_t r10 asm("r10") = arg4, r8 asm("r8") = arg5, + r9 asm("r9") = arg6; + uintptr_t ret, ret2; + + asm volatile("int $0x80" + : "=a"(ret), "=D"(ret2) + : "a"(num), "D"(arg1), "S"(arg2), "d"(arg3), "r"(r10), + "r"(r8), "r"(r9) + : "memory"); + + if (out) + *out = ret2; + + return ret; +} + +static inline int +sc_error(uintptr_t ret) +{ + if (ret > -4096UL) + return -ret; + return 0; +} +#endif + +#endif diff --git a/lib/mlibc/sysdeps/keyronex/meson.build b/lib/mlibc/sysdeps/keyronex/meson.build new file mode 100644 index 0000000..42de7a8 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/meson.build @@ -0,0 +1,98 @@ + +rtdl_sources += files( + 'generic/generic.cpp' +) + +libc_sources += files( + 'generic/entry.cpp', + 'generic/generic.cpp', + 'generic/linux.cpp', + 'generic/signal.cpp', + 'generic/socket.cpp', + 'generic/thread.cpp', + 'generic/thread.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/reboot.h', + 'include/abi-bits/resource.h', + 'include/abi-bits/stat.h', + 'include/abi-bits/signal.h', + 'include/abi-bits/socket.h', + 'include/abi-bits/termios.h', + 'include/abi-bits/time.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/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', + subdir: 'abi-bits', + follow_symlinks: true + ) + + install_headers( + 'include/keyronex/syscall.h', + subdir: 'keyronex', + ) +endif + +if not headers_only + crt = custom_target('crt0', + build_by_default: true, + command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'], + input: host_machine.cpu_family() / 'crt-src/crt0.S', + output: 'crt0.o', + install: true, + install_dir: get_option('libdir') + ) + + custom_target('crti', + build_by_default: true, + command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'], + input: host_machine.cpu_family() / 'crt-src/crti.S', + output: 'crti.o', + install: true, + install_dir: get_option('libdir') + ) + + custom_target('crtn', + build_by_default: true, + command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'], + input: host_machine.cpu_family() / 'crt-src/crtn.S', + output: 'crtn.o', + install: true, + install_dir: get_option('libdir') + ) +endif + diff --git a/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crt0.S b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crt0.S new file mode 100644 index 0000000..d16a46f --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crt0.S @@ -0,0 +1,7 @@ +.section .text +.global _start +_start: + mov $main, %rdi + call __mlibc_entry +.section .note.GNU-stack,"",%progbits + diff --git a/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crti.S b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crti.S new file mode 100644 index 0000000..911b078 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crti.S @@ -0,0 +1,11 @@ +.section .init +.global _init +_init: + push %rax + +.section .fini +.global _fini +_fini: + push %rax +.section .note.GNU-stack,"",%progbits + diff --git a/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crtn.S b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crtn.S new file mode 100644 index 0000000..0187e50 --- /dev/null +++ b/lib/mlibc/sysdeps/keyronex/x86_64/crt-src/crtn.S @@ -0,0 +1,9 @@ +.section .init + pop %rax + ret + +.section .fini + pop %rax + ret +.section .note.GNU-stack,"",%progbits + -- cgit v1.2.3