user: implement mlibc as the libc, finally.
It's finally done.. Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
---
|
||||
DisableFormat: true
|
||||
@@ -0,0 +1,10 @@
|
||||
.section .text
|
||||
|
||||
.global _start
|
||||
_start:
|
||||
mov $main, %rdi
|
||||
call __mlibc_entry
|
||||
|
||||
.size _start, . - _start
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
.section .init
|
||||
.global _init
|
||||
_init:
|
||||
push %rax
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
_fini:
|
||||
push %rax
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,8 @@
|
||||
.section .init
|
||||
pop %rax
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
pop %rax
|
||||
ret
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,368 @@
|
||||
#include <abi-bits/pid_t.h>
|
||||
#include <aero/syscall.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <errno.h>
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/debug.hpp>
|
||||
#include <mlibc/thread-entry.hpp>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <frg/vector.hpp>
|
||||
#include <mlibc/allocator.hpp>
|
||||
|
||||
#define ARCH_SET_GS 0x1001
|
||||
#define ARCH_SET_FS 0x1002
|
||||
#define ARCH_GET_FS 0x1003
|
||||
#define ARCH_GET_GS 0x1004
|
||||
|
||||
struct Slice {
|
||||
void *ptr;
|
||||
uint64_t len;
|
||||
};
|
||||
|
||||
/// Helper function to construct a slice vector from the provided argument
|
||||
/// array. A slice basically consists of a pointer to the data and the length of
|
||||
/// it.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```cc
|
||||
/// auto slice = create_slice({ "hello", "world" });
|
||||
/// ```
|
||||
///
|
||||
/// The `slice` will look like the following:
|
||||
///
|
||||
/// ```cc
|
||||
/// vector<Slice>(
|
||||
/// Slice { .ptr: hello_ptr, .size: hello_size },
|
||||
/// Slice { .ptr: world_ptr, .size: world_size }
|
||||
/// )
|
||||
/// ```
|
||||
static frg::vector<Slice, MemoryAllocator> create_slice(char *const arg[]) {
|
||||
if (arg == nullptr) {
|
||||
return frg::vector<Slice, MemoryAllocator>{getAllocator()};
|
||||
}
|
||||
|
||||
// Find out the length of arg:
|
||||
size_t len = 0;
|
||||
|
||||
while (arg[len] != nullptr) {
|
||||
len += 1;
|
||||
}
|
||||
|
||||
frg::vector<Slice, MemoryAllocator> params{getAllocator()};
|
||||
params.resize(len);
|
||||
|
||||
// Construct the slice vector:
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
params[i].ptr = (void *)arg[i];
|
||||
params[i].len = strlen(arg[i]);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
namespace mlibc {
|
||||
int sys_uname(struct utsname *buf) {
|
||||
auto result = syscall(SYS_UNAME, buf);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
|
||||
// auto result = syscall(SYS_FUTEX_WAIT, pointer, expected, time);
|
||||
//
|
||||
// if (result < 0) {
|
||||
// return -result;
|
||||
// }
|
||||
//
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_futex_wake(int *pointer) {
|
||||
// auto result = syscall(SYS_FUTEX_WAKE, pointer);
|
||||
//
|
||||
// if (result < 0) {
|
||||
// return -result;
|
||||
// }
|
||||
//
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_tcb_set(void *pointer) {
|
||||
auto result = syscall(SYS_ARCH_PRCTL, ARCH_SET_FS, (uint64_t)pointer);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd,
|
||||
off_t offset, void **window) {
|
||||
auto result = syscall(SYS_MMAP, hint, size, prot, flags, fd, offset);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*window = (void *)result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_vm_unmap(void *address, size_t size) {
|
||||
return syscall(SYS_MUNMAP, address, size);
|
||||
}
|
||||
|
||||
int sys_vm_protect(void *pointer, size_t size, int prot) {
|
||||
auto res = syscall(SYS_MPROTECT, pointer, size, prot);
|
||||
if (res < 0)
|
||||
return -res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_anon_allocate(size_t size, void **pointer) {
|
||||
return sys_vm_map(nullptr, size, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, pointer);
|
||||
}
|
||||
|
||||
int sys_anon_free(void *pointer, size_t size) {
|
||||
return sys_vm_unmap(pointer, size);
|
||||
}
|
||||
|
||||
void sys_libc_panic() {
|
||||
mlibc::infoLogger() << "libc_panic: panicked at 'unknown'" << frg::endlog;
|
||||
__ensure(!syscall(SYS_BACKTRACE));
|
||||
|
||||
sys_exit(1);
|
||||
}
|
||||
|
||||
void sys_libc_log(const char *msg) { syscall(SYS_LOG, msg, strlen(msg)); }
|
||||
|
||||
void sys_exit(int status) {
|
||||
syscall(SYS_EXIT, status);
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
|
||||
pid_t sys_getpid() {
|
||||
auto result = syscall(SYS_GETPID);
|
||||
__ensure(result >= 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
pid_t sys_getppid() {
|
||||
auto result = syscall(SYS_GETPPID);
|
||||
__ensure(result != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int sys_kill(int pid, int sig) {
|
||||
auto result = syscall(SYS_KILL, pid, sig);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_clock_get(int clock, time_t *secs, long *nanos) {
|
||||
struct timespec ts;
|
||||
auto result = syscall(SYS_GETTIME, clock, &ts);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*secs = ts.tv_sec;
|
||||
*nanos = ts.tv_nsec;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getcwd(char *buffer, size_t size) {
|
||||
auto result = syscall(SYS_GETCWD, buffer, size);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_chdir(const char *path) {
|
||||
auto result = syscall(SYS_CHDIR, path, strlen(path));
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_gethostname(char *buffer, size_t bufsize) {
|
||||
auto result = syscall(SYS_GETHOSTNAME, buffer, bufsize);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_sleep(time_t *sec, long *nanosec) {
|
||||
struct timespec ts = {.tv_sec = *sec, .tv_nsec = *nanosec};
|
||||
|
||||
auto result = syscall(SYS_SLEEP, &ts);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
pid_t sys_getpgid(pid_t pid, pid_t *pgid) {
|
||||
auto ret = syscall(SYS_GETPGID, pid);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
*pgid = ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setpgid(pid_t pid, pid_t pgid) {
|
||||
auto ret = syscall(SYS_SETPGID, pid, pgid);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uid_t sys_getuid() {
|
||||
mlibc::infoLogger() << "mlibc: sys_setuid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uid_t sys_geteuid() {
|
||||
mlibc::infoLogger() << "mlibc: sys_seteuid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setsid(pid_t *sid) {
|
||||
auto ret = syscall(SYS_SETSID);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
*sid = ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_seteuid(uid_t euid) UNIMPLEMENTED("sys_seteuid")
|
||||
|
||||
gid_t sys_getgid() {
|
||||
mlibc::infoLogger() << "mlibc: sys_setgid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
gid_t sys_getegid() {
|
||||
mlibc::infoLogger() << "mlibc: sys_getegid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setgid(gid_t gid) {
|
||||
mlibc::infoLogger() << "mlibc: sys_setgid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setegid(gid_t egid) {
|
||||
mlibc::infoLogger() << "mlibc: sys_setegid is a stub" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sys_yield() {
|
||||
mlibc::infoLogger() << "mlibc: sys_yield is a stub" << frg::endlog;
|
||||
__ensure(!syscall(SYS_BACKTRACE));
|
||||
}
|
||||
|
||||
int sys_clone(void *tcb, pid_t *tid_out, void *stack) {
|
||||
auto result = syscall(SYS_CLONE, (uintptr_t)__mlibc_start_thread, stack);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*tid_out = (pid_t)result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_thread_setname(void *tcb, const char *name) {
|
||||
mlibc::infoLogger() << "The name of this thread is " << name << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sys_thread_exit() {
|
||||
syscall(SYS_EXIT);
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru,
|
||||
pid_t *ret_pid) {
|
||||
if (ru) {
|
||||
mlibc::infoLogger()
|
||||
<< "mlibc: struct rusage in sys_waitpid is unsupported"
|
||||
<< frg::endlog;
|
||||
return ENOSYS;
|
||||
}
|
||||
|
||||
auto result = syscall(SYS_WAITPID, pid, status, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*ret_pid = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_fork(pid_t *child) {
|
||||
auto result = syscall(SYS_FORK);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*child = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
|
||||
auto envv_slice = create_slice(envp);
|
||||
auto argv_slice = create_slice(argv);
|
||||
|
||||
auto path_ptr = (uintptr_t)path;
|
||||
auto path_len = strlen(path);
|
||||
|
||||
auto result =
|
||||
syscall(SYS_EXEC, path_ptr, path_len, argv_slice.data(),
|
||||
argv_slice.size(), envv_slice.data(), envv_slice.size());
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
// int sys_getentropy(void *buffer, size_t length)
|
||||
// UNIMPLEMENTED("sys_getentropy")
|
||||
|
||||
#endif
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/elf/startup.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern "C" uintptr_t *__dlapi_entrystack();
|
||||
|
||||
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,494 @@
|
||||
#include "mlibc/fsfd_target.hpp"
|
||||
#include <aero/syscall.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/debug.hpp>
|
||||
|
||||
namespace mlibc {
|
||||
int sys_write(int fd, const void *buffer, size_t count, ssize_t *written) {
|
||||
auto result = syscall(SYS_WRITE, fd, buffer, count);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*written = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
|
||||
auto result = syscall(SYS_READ, fd, buf, count);
|
||||
|
||||
if (result < 0) {
|
||||
*bytes_read = 0;
|
||||
return -result;
|
||||
}
|
||||
|
||||
*bytes_read = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_fsync(int) {
|
||||
mlibc::infoLogger() << "\e[35mmlibc: fsync is a stub\e[39m" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_fdatasync(int) {
|
||||
mlibc::infoLogger() << "\e[35mmlibc: fdatasync() is a no-op\e[39m" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
int sys_pwrite(int fd, const void *buffer, size_t count, off_t off,
|
||||
ssize_t *written) UNIMPLEMENTED("sys_pwrite")
|
||||
|
||||
// clang-format off
|
||||
int sys_pread(int fd, void *buf, size_t count,
|
||||
off_t off, ssize_t *bytes_read) UNIMPLEMENTED("sys_pread")
|
||||
|
||||
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
|
||||
auto result = syscall(SYS_SEEK, fd, offset, whence);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*new_offset = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_open(const char *filename, int flags, mode_t mode, int *fd) {
|
||||
auto result = syscall(SYS_OPEN, 0, filename, strlen(filename), flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*fd = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_close(int fd) {
|
||||
auto result = syscall(SYS_CLOSE, fd);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||
auto ret = syscall(SYS_ACCESS, dirfd, pathname, strlen(pathname), mode, flags);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_access(const char *filename, int mode) {
|
||||
return sys_faccessat(AT_FDCWD, filename, mode, 0);
|
||||
}
|
||||
|
||||
int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
|
||||
struct stat *statbuf) {
|
||||
switch (fsfdt) {
|
||||
case fsfd_target::path:
|
||||
fd = AT_FDCWD;
|
||||
break;
|
||||
|
||||
case fsfd_target::fd:
|
||||
flags |= AT_EMPTY_PATH;
|
||||
|
||||
case fsfd_target::fd_path:
|
||||
break;
|
||||
|
||||
default:
|
||||
__ensure(!"Invalid fsfd_target");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
auto ret = syscall(SYS_FSTAT, fd, path, strlen(path), flags, statbuf);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
|
||||
auto sys_res = syscall(SYS_IOCTL, fd, request, arg);
|
||||
|
||||
if (sys_res < 0) {
|
||||
return -sys_res;
|
||||
}
|
||||
|
||||
if (result)
|
||||
*result = sys_res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_isatty(int fd) {
|
||||
// NOTE: The easiest way to check if a file descriptor is a TTY is to
|
||||
// do an ioctl of TIOCGWINSZ on it and see if it succeeds :^)
|
||||
struct winsize ws;
|
||||
int result;
|
||||
|
||||
if (!sys_ioctl(fd, TIOCGWINSZ, &ws, &result)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ENOTTY;
|
||||
}
|
||||
|
||||
int sys_tcgetattr(int fd, struct termios *attr) {
|
||||
int result;
|
||||
|
||||
if (int e = sys_ioctl(fd, TCGETS, (void *)attr, &result); e)
|
||||
return e;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_tcsetattr(int fd, int optional_action, const struct termios *attr) {
|
||||
int req;
|
||||
|
||||
switch (optional_action) {
|
||||
case TCSANOW: req = TCSETS; break;
|
||||
case TCSADRAIN: req = TCSETSW; break;
|
||||
case TCSAFLUSH: req = TCSETSF; break;
|
||||
default: return EINVAL;
|
||||
}
|
||||
|
||||
if (int e = sys_ioctl(fd, req, (void *)attr, NULL); e)
|
||||
return e;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_mkdir(const char *path, mode_t) {
|
||||
auto result = syscall(SYS_MKDIR, path, strlen(path));
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_link(const char *srcpath, const char *destpath) {
|
||||
auto result =
|
||||
syscall(SYS_LINK, srcpath, strlen(srcpath), destpath, strlen(destpath));
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_rmdir(const char *path) {
|
||||
return sys_unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
|
||||
}
|
||||
|
||||
int sys_unlinkat(int fd, const char *path, int flags) {
|
||||
auto ret = syscall(SYS_UNLINK, fd, path, strlen(path), flags);
|
||||
if (int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_symlink(const char *target_path, const char *link_path) {
|
||||
return sys_symlinkat(target_path, AT_FDCWD, link_path);
|
||||
}
|
||||
|
||||
int sys_symlinkat(const char *target_path, int dirfd, const char *link_path) {
|
||||
auto ret = syscall(SYS_SYMLINK_AT, dirfd, target_path, strlen(target_path), link_path, strlen(link_path));
|
||||
if (int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct aero_dir_entry {
|
||||
size_t inode;
|
||||
size_t offset;
|
||||
size_t reclen;
|
||||
size_t filetyp;
|
||||
char name[];
|
||||
} __attribute__((__packed__));
|
||||
|
||||
int sys_read_entries(int handle, void *buffer, size_t max_size,
|
||||
size_t *bytes_read) {
|
||||
auto result = syscall(SYS_GETDENTS, handle, buffer, max_size);
|
||||
|
||||
// Check if we got an error.
|
||||
if (result < 0) {
|
||||
*bytes_read = 0;
|
||||
return -result;
|
||||
}
|
||||
|
||||
// Nothing to read.
|
||||
if (result == 0) {
|
||||
*bytes_read = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto entry = (struct aero_dir_entry *)buffer;
|
||||
|
||||
struct dirent dirent = {
|
||||
.d_ino = static_cast<ino_t>(entry->inode),
|
||||
.d_off = static_cast<off_t>(entry->offset),
|
||||
.d_reclen = static_cast<unsigned short>(entry->reclen),
|
||||
.d_type = static_cast<unsigned char>(entry->filetyp),
|
||||
};
|
||||
|
||||
// The reclen is the size of the dirent struct, plus the size of the name.
|
||||
auto name_size = entry->reclen - sizeof(struct aero_dir_entry);
|
||||
__ensure(name_size < 255);
|
||||
|
||||
memcpy(&dirent.d_name, entry->name, name_size);
|
||||
*bytes_read = entry->reclen;
|
||||
|
||||
memcpy(buffer, &dirent, sizeof(struct dirent));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_open_dir(const char *path, int *handle) {
|
||||
return sys_open(path, O_DIRECTORY, 0, handle);
|
||||
}
|
||||
|
||||
int sys_rename(const char *path, const char *new_path) {
|
||||
auto result =
|
||||
syscall(SYS_RENAME, path, strlen(path), new_path, strlen(new_path));
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_readlink(const char *path, void *buffer, size_t max_size,
|
||||
ssize_t *length) {
|
||||
auto result = syscall(SYS_READ_LINK, path, strlen(path), buffer, max_size);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*length = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_dup(int fd, int flags, int *newfd) {
|
||||
auto result = syscall(SYS_DUP, fd, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*newfd = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_dup2(int fd, int flags, int newfd) {
|
||||
auto result = syscall(SYS_DUP2, fd, newfd, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_fcntl(int fd, int request, va_list args, int *result_value) {
|
||||
auto result = syscall(SYS_FCNTL, fd, request, va_arg(args, uint64_t));
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*result_value = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// int sys_chmod(const char *pathname, mode_t mode) UNIMPLEMENTED("sys_chmod")
|
||||
|
||||
int sys_pipe(int *fds, int flags) {
|
||||
auto result = syscall(SYS_PIPE, fds, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// epoll API syscalls:
|
||||
int sys_epoll_create(int flags, int *fd) {
|
||||
auto result = syscall(SYS_EPOLL_CREATE, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*fd = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) {
|
||||
auto result = syscall(SYS_EPOLL_CTL, epfd, mode, fd, ev);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, int timeout,
|
||||
const sigset_t *sigmask, int *raised) {
|
||||
auto result = syscall(SYS_EPOLL_PWAIT, epfd, ev, n, timeout, sigmask);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*raised = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_eventfd_create(unsigned int initval, int flags, int *fd) {
|
||||
auto result = syscall(SYS_EVENT_FD, initval, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*fd = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout,
|
||||
const sigset_t *sigmask, int *num_events) {
|
||||
auto result = syscall(SYS_POLL, fds, nfds, timeout, sigmask);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*num_events = result;
|
||||
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, &ts, NULL, num_events);
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
#include <stdio.h>
|
||||
int sys_ptsname(int fd, char *buffer, size_t length) {
|
||||
int index;
|
||||
if (int e = sys_ioctl(fd, TIOCGPTN, &index, NULL); e)
|
||||
return e;
|
||||
if ((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) {
|
||||
return ERANGE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_pselect(int num_fds, fd_set *read_set, fd_set *write_set,
|
||||
fd_set *except_set, const struct timespec *timeout,
|
||||
const sigset_t *sigmask, int *num_events) {
|
||||
int fd = epoll_create1(0);
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
|
||||
for (int k = 0; k < FD_SETSIZE; k++) {
|
||||
struct epoll_event ev;
|
||||
memset(&ev, 0, sizeof(struct epoll_event));
|
||||
|
||||
if (read_set && FD_ISSET(k, read_set))
|
||||
ev.events |= EPOLLIN;
|
||||
if (write_set && FD_ISSET(k, write_set))
|
||||
ev.events |= EPOLLOUT;
|
||||
if (except_set && FD_ISSET(k, except_set))
|
||||
ev.events |= EPOLLPRI;
|
||||
|
||||
if (!ev.events)
|
||||
continue;
|
||||
|
||||
ev.data.u32 = k;
|
||||
if (epoll_ctl(fd, EPOLL_CTL_ADD, k, &ev))
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct epoll_event evnts[16];
|
||||
int n = epoll_pwait(
|
||||
fd, evnts, 16,
|
||||
timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 100) : -1,
|
||||
sigmask);
|
||||
|
||||
if (n == -1)
|
||||
return -1;
|
||||
|
||||
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);
|
||||
|
||||
int m = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int k = evnts[i].data.u32;
|
||||
|
||||
if (read_set && FD_ISSET(k, read_set) &&
|
||||
evnts[i].events & (EPOLLIN | EPOLLERR | EPOLLHUP)) {
|
||||
FD_SET(k, &res_read_set);
|
||||
m++;
|
||||
}
|
||||
|
||||
if (write_set && FD_ISSET(k, write_set) &&
|
||||
evnts[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) {
|
||||
FD_SET(k, &res_write_set);
|
||||
m++;
|
||||
}
|
||||
|
||||
if (except_set && FD_ISSET(k, except_set) &&
|
||||
evnts[i].events & EPOLLPRI) {
|
||||
FD_SET(k, &res_except_set);
|
||||
m++;
|
||||
}
|
||||
}
|
||||
|
||||
if (close(fd))
|
||||
__ensure("mlibc::pselect: close() failed on epoll file");
|
||||
|
||||
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));
|
||||
|
||||
*num_events = m;
|
||||
return 0;
|
||||
}
|
||||
#endif // #ifndef MLIBC_BUILDING_RTLD
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .text
|
||||
.global __mlibc_signal_restore
|
||||
|
||||
__mlibc_signal_restore:
|
||||
mov $39, %rax
|
||||
syscall
|
||||
ud2
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <mlibc/ansi-sysdeps.hpp>
|
||||
#include <mlibc/debug.hpp>
|
||||
|
||||
#include <aero/syscall.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define LOG_SIGACTION_INSTALL 0
|
||||
|
||||
extern "C" void __mlibc_signal_restore(); // defined in `signals.S`
|
||||
|
||||
namespace mlibc {
|
||||
int sys_sigaction(int how, const struct sigaction *__restrict action,
|
||||
struct sigaction *__restrict old_action) {
|
||||
#if LOG_SIGACTION_INSTALL
|
||||
mlibc::infoLogger() << "sys_sigaction: signal " << how << frg::endlog;
|
||||
mlibc::infoLogger() << "sys_sigaction: size: " << sizeof(*action)
|
||||
<< frg::endlog;
|
||||
|
||||
if (action != NULL) {
|
||||
mlibc::infoLogger() << "sys_sigaction: handler "
|
||||
<< (int64_t)action->sa_handler << frg::endlog;
|
||||
mlibc::infoLogger() << "sys_sigaction: action "
|
||||
<< (int64_t)action->sa_sigaction << frg::endlog;
|
||||
mlibc::infoLogger() << "sys_sigaction: flags "
|
||||
<< (int64_t)action->sa_flags << frg::endlog;
|
||||
}
|
||||
|
||||
mlibc::infoLogger() << frg::endlog;
|
||||
#endif
|
||||
|
||||
auto sigreturn = (sc_word_t)__mlibc_signal_restore;
|
||||
auto ret = syscall(SYS_SIGACTION, how, (sc_word_t)action, sigreturn,
|
||||
(sc_word_t)old_action);
|
||||
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_sigprocmask(int how, const sigset_t *__restrict set,
|
||||
sigset_t *__restrict retrieve) {
|
||||
|
||||
auto ret = syscall(SYS_SIGPROCMASK, how, set, retrieve);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,266 @@
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/thread-entry.hpp>
|
||||
#include <mlibc/debug.hpp>
|
||||
#include <abi-bits/in.h>
|
||||
#include <abi-bits/errno.h>
|
||||
|
||||
#include <aero/syscall.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
namespace {
|
||||
|
||||
int fcntl_helper(int fd, int request, int *result, ...) {
|
||||
va_list args;
|
||||
va_start(args, result);
|
||||
if(!mlibc::sys_fcntl) {
|
||||
return ENOSYS;
|
||||
}
|
||||
int ret = mlibc::sys_fcntl(fd, request, args, result);
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace mlibc {
|
||||
int sys_socket(int family, int type, int protocol, int *fd) {
|
||||
auto result = syscall(SYS_SOCKET, family, type, protocol);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*fd = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
|
||||
auto result = syscall(SYS_BIND, fd, addr_ptr, (sc_word_t)addr_length);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_connect(int fd, const struct sockaddr *addr_ptr,
|
||||
socklen_t addr_length) {
|
||||
auto result = syscall(SYS_CONNECT, fd, addr_ptr, (sc_word_t)addr_length);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_listen(int fd, int backlog) {
|
||||
auto result = syscall(SYS_LISTEN, fd, backlog);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_accept(int sockfd, int *newfd, struct sockaddr *addr_ptr,
|
||||
socklen_t *addr_length, int flags) {
|
||||
auto result = syscall(SYS_ACCEPT, sockfd, addr_ptr, addr_length);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*newfd = result;
|
||||
|
||||
if(flags & SOCK_NONBLOCK) {
|
||||
int fcntl_ret = 0;
|
||||
fcntl_helper(*newfd, F_GETFL, &fcntl_ret);
|
||||
fcntl_helper(*newfd, F_SETFL, &fcntl_ret, fcntl_ret | O_NONBLOCK);
|
||||
}
|
||||
|
||||
if(flags & SOCK_CLOEXEC) {
|
||||
int fcntl_ret = 0;
|
||||
fcntl_helper(*newfd, F_GETFD, &fcntl_ret);
|
||||
fcntl_helper(*newfd, F_SETFD, &fcntl_ret, fcntl_ret | FD_CLOEXEC);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_msg_send(int fd, const struct msghdr *hdr, int flags, ssize_t *length) {
|
||||
auto result = syscall(SYS_SOCK_SEND, fd, hdr, flags);
|
||||
if (result < 0)
|
||||
return -result;
|
||||
|
||||
*length = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_msg_recv(int sockfd, struct msghdr *msg_hdr, int flags,
|
||||
ssize_t *length) {
|
||||
auto result = syscall(SYS_SOCK_RECV, sockfd, msg_hdr, flags);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
*length = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) {
|
||||
auto result = syscall(SYS_SOCKET_PAIR, domain, type_and_flags, proto, fds);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getsockopt(int fd, int layer, int number, void *__restrict buffer,
|
||||
socklen_t *__restrict size) {
|
||||
(void)fd;
|
||||
(void)size;
|
||||
if (layer == SOL_SOCKET && number == SO_PEERCRED) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET "
|
||||
"and SO_PEERCRED is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
*(int *)buffer = 0;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_SNDBUF) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET "
|
||||
"and SO_SNDBUF is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
*(int *)buffer = 4096;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_TYPE) {
|
||||
mlibc::infoLogger()
|
||||
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_TYPE is "
|
||||
"unimplemented, hardcoding SOCK_STREAM\e[39m"
|
||||
<< frg::endlog;
|
||||
*(int *)buffer = SOCK_STREAM;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_ERROR) {
|
||||
mlibc::infoLogger()
|
||||
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ERROR is "
|
||||
"unimplemented, hardcoding 0\e[39m"
|
||||
<< frg::endlog;
|
||||
*(int *)buffer = 0;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) {
|
||||
mlibc::infoLogger()
|
||||
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and "
|
||||
"SO_KEEPALIVE is unimplemented, hardcoding 0\e[39m"
|
||||
<< frg::endlog;
|
||||
*(int *)buffer = 0;
|
||||
return 0;
|
||||
} else {
|
||||
mlibc::panicLogger()
|
||||
<< "\e[31mmlibc: Unexpected getsockopt() call, layer: " << layer
|
||||
<< " number: " << number << "\e[39m" << frg::endlog;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setsockopt(int fd, int layer, int number, const void *buffer,
|
||||
socklen_t size) {
|
||||
(void)fd;
|
||||
(void)buffer;
|
||||
(void)size;
|
||||
|
||||
if (layer == SOL_SOCKET && number == SO_PASSCRED) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_PASSCRED) is not "
|
||||
"implemented correctly\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_ATTACH_FILTER) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_ATTACH_FILTER) is "
|
||||
"not implemented correctly\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_RCVBUFFORCE) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_RCVBUFFORCE) is not "
|
||||
"implemented correctly\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_SNDBUF) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
|
||||
"and SO_SNDBUF is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
|
||||
"and SO_KEEPALIVE is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == SOL_SOCKET && number == SO_REUSEADDR) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
|
||||
"and SO_REUSEADDR is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else if (layer == AF_NETLINK && number == SO_ACCEPTCONN) {
|
||||
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with AF_NETLINK "
|
||||
"and SO_ACCEPTCONN is unimplemented\e[39m"
|
||||
<< frg::endlog;
|
||||
return 0;
|
||||
} else {
|
||||
mlibc::infoLogger()
|
||||
<< "\e[31mmlibc: Unexpected setsockopt() call, layer: " << layer
|
||||
<< " number: " << number << "\e[39m" << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
|
||||
auto ret = syscall(SYS_GETPEERNAME, fd, addr_ptr, &max_addr_length);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
*actual_length = max_addr_length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
|
||||
auto ret = syscall(SYS_GETSOCKNAME, fd, addr_ptr, &max_addr_length);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
*actual_length = max_addr_length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_shutdown(int sockfd, int how) {
|
||||
auto ret = syscall(SYS_SOCK_SHUTDOWN, sockfd, how);
|
||||
if(int e = sc_error(ret); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_if_nametoindex(const char *name, unsigned int *ret) {
|
||||
int fd = 0;
|
||||
int r = sys_socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd);
|
||||
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
struct ifreq ifr;
|
||||
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
||||
|
||||
r = sys_ioctl(fd, SIOCGIFINDEX, &ifr, NULL);
|
||||
close(fd);
|
||||
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
*ret = ifr.ifr_ifindex;
|
||||
return 0;
|
||||
}
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <mlibc/thread-entry.hpp>
|
||||
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/tcb.hpp>
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.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 = 0x1000000;
|
||||
|
||||
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,10 @@
|
||||
.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,25 @@
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <aero/syscall.h>
|
||||
|
||||
namespace mlibc {
|
||||
int sys_setitimer(int which, const struct itimerval *new_value,
|
||||
struct itimerval *old_value) {
|
||||
auto result = syscall(SYS_SETITIMER, which, new_value, old_value);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getitimer(int which, struct itimerval *curr_value) {
|
||||
auto result = syscall(SYS_GETITIMER, which, curr_value);
|
||||
|
||||
if (result < 0) {
|
||||
return -result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/access.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/auxv.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/blkcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/blksize_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/clockid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/dev_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/epoll.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/errno.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fcntl.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fsblkcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fsfilcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/gid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/in.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/ino_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/inotify.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/ioctls.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/ipc.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/limits.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/mode_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/mqueue.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/msg.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/nlink_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/packet.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/pid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/poll.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/ptrace.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/random.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/reboot.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/resource.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/rlim_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/seek-whence.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/shm.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/sigevent.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/signal.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/sigval.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/socket.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/socklen_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/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/linux/termios.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/time.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/uid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/utmp-defines.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/utmpx.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/utsname.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/aero/vm-flags.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/wait.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/xattr.h
|
||||
@@ -0,0 +1,237 @@
|
||||
#ifndef SYSCALL_H
|
||||
#define SYSCALL_H
|
||||
|
||||
#include <bits/ensure.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define SYS_READ 0
|
||||
#define SYS_WRITE 1
|
||||
#define SYS_OPEN 2
|
||||
#define SYS_CLOSE 3
|
||||
#define SYS_SHUTDOWN 4
|
||||
#define SYS_EXIT 5
|
||||
#define SYS_FORK 6
|
||||
#define SYS_REBOOT 7
|
||||
#define SYS_MMAP 8
|
||||
#define SYS_MUNMAP 9
|
||||
#define SYS_ARCH_PRCTL 10
|
||||
#define SYS_GETDENTS 11
|
||||
#define SYS_GETCWD 12
|
||||
#define SYS_CHDIR 13
|
||||
#define SYS_MKDIR 14
|
||||
#define SYS_MKDIR_AT 15
|
||||
#define SYS_RMDIR 16
|
||||
#define SYS_EXEC 17
|
||||
#define SYS_LOG 18
|
||||
#define SYS_UNAME 19
|
||||
#define SYS_WAITPID 20
|
||||
#define SYS_IOCTL 21
|
||||
#define SYS_GETPID 22
|
||||
#define SYS_SOCKET 23
|
||||
#define SYS_CONNECT 24
|
||||
#define SYS_BIND 25
|
||||
#define SYS_LISTEN 26
|
||||
#define SYS_ACCEPT 27
|
||||
#define SYS_SEEK 28
|
||||
#define SYS_GETTID 29
|
||||
#define SYS_GETTIME 30
|
||||
#define SYS_SLEEP 31
|
||||
#define SYS_ACCESS 32
|
||||
#define SYS_PIPE 33
|
||||
#define SYS_UNLINK 34
|
||||
#define SYS_GETHOSTNAME 35
|
||||
#define SYS_SETHOSTNAME 36
|
||||
#define SYS_INFO 37
|
||||
#define SYS_CLONE 38
|
||||
#define SYS_SIGRETURN 39
|
||||
#define SYS_SIGACTION 40
|
||||
#define SYS_SIGPROCMASK 41
|
||||
#define SYS_DUP 42
|
||||
#define SYS_FCNTL 43
|
||||
#define SYS_DUP2 44
|
||||
#define SYS_IPC_SEND 45
|
||||
#define SYS_IPC_RECV 46
|
||||
#define SYS_DISCOVER_ROOT 47
|
||||
#define SYS_BECOME_ROOT 48
|
||||
#define SYS_STAT 49
|
||||
#define SYS_FSTAT 50
|
||||
#define SYS_READ_LINK 51
|
||||
#define SYS_EPOLL_CREATE 52
|
||||
#define SYS_EPOLL_PWAIT 53
|
||||
#define SYS_EPOLL_CTL 54
|
||||
#define SYS_EVENT_FD 55
|
||||
#define SYS_KILL 56
|
||||
#define SYS_FUTEX_WAIT 57
|
||||
#define SYS_FUTEX_WAKE 58
|
||||
#define SYS_LINK 59
|
||||
#define SYS_BACKTRACE 60
|
||||
#define SYS_POLL 61
|
||||
#define SYS_EXIT_THREAD 62
|
||||
#define SYS_SOCK_RECV 63
|
||||
#define SYS_SETITIMER 64
|
||||
#define SYS_GETITIMER 65
|
||||
#define SYS_GETPPID 66
|
||||
#define SYS_SOCKET_PAIR 67
|
||||
#define SYS_RENAME 68
|
||||
#define SYS_MPROTECT 69
|
||||
#define SYS_SOCK_SEND 70
|
||||
#define SYS_TRACE 71
|
||||
#define SYS_SETPGID 72
|
||||
#define SYS_SETSID 73
|
||||
#define SYS_GETPGID 74
|
||||
#define SYS_SOCK_SHUTDOWN 75
|
||||
#define SYS_GETPEERNAME 76
|
||||
#define SYS_GETSOCKNAME 77
|
||||
#define SYS_DEBUG 78
|
||||
#define SYS_SETSOCKOPT 79
|
||||
#define SYS_GETSOCKOPT 80
|
||||
#define SYS_SYMLINK_AT 81
|
||||
|
||||
/* Invalid syscall used to trigger a log error in the kernel (as a hint) */
|
||||
/* so, that we can implement the syscall in the kernel. */
|
||||
#define UNIMPLEMENTED(FUNCTION_NAME) \
|
||||
{ \
|
||||
sys_libc_log("Unimplemented syscall: " FUNCTION_NAME); \
|
||||
sys_exit(1); \
|
||||
__builtin_unreachable(); \
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
using sc_word_t = long;
|
||||
|
||||
static sc_word_t syscall0(int sc) {
|
||||
sc_word_t ret;
|
||||
asm volatile("syscall" : "=a"(ret) : "a"(sc) : "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall1(int sc, sc_word_t arg1) {
|
||||
sc_word_t ret;
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall2(int sc, sc_word_t arg1, sc_word_t arg2) {
|
||||
sc_word_t ret;
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1), "S"(arg2)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall3(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3) {
|
||||
sc_word_t ret;
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1), "S"(arg2), "d"(arg3)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall4(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4) {
|
||||
sc_word_t ret;
|
||||
|
||||
register sc_word_t arg4_reg asm("r10") = arg4;
|
||||
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1), "S"(arg2), "d"(arg3), "r"(arg4_reg)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall5(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4, sc_word_t arg5) {
|
||||
sc_word_t ret;
|
||||
|
||||
register sc_word_t arg4_reg asm("r10") = arg4;
|
||||
register sc_word_t arg5_reg asm("r8") = arg5;
|
||||
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1), "S"(arg2), "d"(arg3), "r"(arg4_reg),
|
||||
"r"(arg5_reg)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static sc_word_t syscall6(int sc, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4, sc_word_t arg5,
|
||||
sc_word_t arg6) {
|
||||
sc_word_t ret;
|
||||
|
||||
register sc_word_t arg4_reg asm("r10") = arg4;
|
||||
register sc_word_t arg5_reg asm("r8") = arg5;
|
||||
register sc_word_t arg6_reg asm("r9") = arg6;
|
||||
|
||||
asm volatile("syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(sc), "D"(arg1), "S"(arg2), "d"(arg3), "r"(arg4_reg),
|
||||
"r"(arg5_reg), "r"(arg6_reg)
|
||||
: "rcx", "r11", "memory");
|
||||
return ret;
|
||||
}
|
||||
} /* extern "C" */
|
||||
|
||||
/* Cast to the argument type of the extern "C" functions. */
|
||||
__attribute__((__always_inline__)) inline sc_word_t sc_cast(long x) { return x; }
|
||||
__attribute__((__always_inline__)) inline sc_word_t sc_cast(const void *x) {
|
||||
return reinterpret_cast<sc_word_t>(x);
|
||||
}
|
||||
|
||||
/* C++ wrappers for the extern "C" functions. */
|
||||
__attribute__((__always_inline__)) static inline long _syscall(int call) {
|
||||
return syscall0(call);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long _syscall(int call,
|
||||
sc_word_t arg0) {
|
||||
return syscall1(call, arg0);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long
|
||||
_syscall(int call, sc_word_t arg0, sc_word_t arg1) {
|
||||
return syscall2(call, arg0, arg1);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long
|
||||
_syscall(int call, sc_word_t arg0, sc_word_t arg1, sc_word_t arg2) {
|
||||
return syscall3(call, arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long
|
||||
_syscall(int call, sc_word_t arg0, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3) {
|
||||
return syscall4(call, arg0, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long
|
||||
_syscall(int call, sc_word_t arg0, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4) {
|
||||
return syscall5(call, arg0, arg1, arg2, arg3, arg4);
|
||||
}
|
||||
|
||||
__attribute__((__always_inline__)) static inline long
|
||||
_syscall(int call, sc_word_t arg0, sc_word_t arg1, sc_word_t arg2,
|
||||
sc_word_t arg3, sc_word_t arg4, sc_word_t arg5) {
|
||||
return syscall6(call, arg0, arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
__attribute__((__always_inline__)) static inline long syscall(sc_word_t call,
|
||||
T... args) {
|
||||
return _syscall(call, sc_cast(args)...);
|
||||
}
|
||||
|
||||
inline int sc_error(long ret) {
|
||||
if (ret < 0)
|
||||
return -ret;
|
||||
return 0;
|
||||
}
|
||||
#endif /* SYSCALL_H */
|
||||
@@ -0,0 +1,11 @@
|
||||
#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 {
|
||||
int prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size,
|
||||
size_t *guard_size);
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,118 @@
|
||||
sysdep_supported_options = {
|
||||
'posix': true,
|
||||
'linux': true,
|
||||
'glibc': true,
|
||||
'bsd': true,
|
||||
}
|
||||
|
||||
rtld_dso_sources += files(
|
||||
'generic/aero.cpp',
|
||||
'generic/filesystem.cpp',
|
||||
)
|
||||
|
||||
libc_sources += files(
|
||||
'generic/aero.cpp',
|
||||
'generic/entry.cpp',
|
||||
'generic/thread_entry.S',
|
||||
'generic/thread.cpp',
|
||||
'generic/filesystem.cpp',
|
||||
'generic/sockets.cpp',
|
||||
'generic/signals.cpp',
|
||||
'generic/time.cpp',
|
||||
)
|
||||
|
||||
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/signal.h',
|
||||
'include/abi-bits/stat.h',
|
||||
'include/abi-bits/statx.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/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/random.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
|
||||
)
|
||||
install_headers(
|
||||
'include/aero/syscall.h',
|
||||
subdir: 'aero'
|
||||
)
|
||||
endif
|
||||
|
||||
if not headers_only
|
||||
crt = custom_target('crt0',
|
||||
build_by_default: true,
|
||||
command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'],
|
||||
input: 'crt-x86_64/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: 'crt-x86_64/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: 'crt-x86_64/crtn.S',
|
||||
output: 'crtn.o',
|
||||
install: true,
|
||||
install_dir: get_option('libdir')
|
||||
)
|
||||
endif
|
||||
|
||||
if host_machine.cpu_family() == 'x86_64'
|
||||
libc_sources += files('generic/signals.S')
|
||||
else
|
||||
error('Unknown architecture')
|
||||
endif
|
||||
@@ -0,0 +1,2 @@
|
||||
---
|
||||
DisableFormat: true
|
||||
@@ -0,0 +1,7 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
lea main(%rip), %rdi
|
||||
mov %rsp, %rsi
|
||||
call __mlibc_entry
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,11 @@
|
||||
.section .init
|
||||
.global _init
|
||||
_init:
|
||||
push %rax
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
_fini:
|
||||
push %rax
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .init
|
||||
pop %rax
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
pop %rax
|
||||
ret
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <astral/syscall.h>
|
||||
#include <astral/archctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
|
||||
int arch_ctl(int func, void *arg) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_ARCHCTL, &ret, func, (uint64_t)arg);
|
||||
if(error)
|
||||
errno = error;
|
||||
return error ? -1 : ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/elf/startup.h>
|
||||
|
||||
extern "C" void __dlapi_enter(uintptr_t *);
|
||||
|
||||
extern char **environ;
|
||||
|
||||
extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[]), uintptr_t *entry_stack) {
|
||||
__dlapi_enter(entry_stack);
|
||||
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
|
||||
exit(result);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,916 @@
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/debug.hpp>
|
||||
#include <mlibc/all-sysdeps.hpp>
|
||||
#include <errno.h>
|
||||
#include <astral/syscall.h>
|
||||
#include <astral/archctl.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <asm/ioctls.h>
|
||||
#include <poll.h>
|
||||
#include <sys/select.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
namespace mlibc {
|
||||
int sys_setgroups(size_t size, const gid_t *list) {
|
||||
(void)size;
|
||||
(void)list;
|
||||
// TODO unstub
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getgroups(size_t size, gid_t *list, int *ret) {
|
||||
(void)size;
|
||||
(void)list;
|
||||
(void)ret;
|
||||
// TODO unstub
|
||||
*ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getsockopt(int fd, int layer, int number,
|
||||
void *__restrict buffer, socklen_t *__restrict size) {
|
||||
(void)size;
|
||||
(void)buffer;
|
||||
// TODO unstub
|
||||
mlibc::infoLogger() << "getsockopt: " << fd << " " << layer << " " << number << frg::endlog;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_inet_configured(bool *ipv4, bool *ipv6) {
|
||||
// there is no ipv6 support in the kernel currently and no way of checking for configured interfaces
|
||||
*ipv4 = true;
|
||||
*ipv6 = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getcpu(int *cpu) {
|
||||
long ret;
|
||||
// can never fail
|
||||
syscall(SYSCALL_GETCPU, &ret);
|
||||
*cpu = ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_flock(int fd, int options) {
|
||||
long r;
|
||||
return syscall(SYSCALL_FLOCK, &r, fd, options);
|
||||
}
|
||||
|
||||
int sys_nice(int nice, int *ret) {
|
||||
long r = -1;
|
||||
long e = syscall(SYSCALL_NICE, &r, nice);
|
||||
*ret = r;
|
||||
return e;
|
||||
}
|
||||
|
||||
int sys_shutdown(int sockfd, int how) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SHUTDOWN, &ret, sockfd, how);
|
||||
}
|
||||
|
||||
int sys_tgkill(int pid, int tid, int sig) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_KILLTHREAD, &ret, pid, tid, sig);
|
||||
}
|
||||
|
||||
int sys_sigpending(sigset_t *set) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SIGPENDING, &ret, (uint64_t)set);
|
||||
}
|
||||
|
||||
int sys_sigtimedwait(const sigset_t *__restrict set, siginfo_t *__restrict info, const struct timespec *__restrict timeout, int *out_signal) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_SIGTIMEDWAIT, &ret, (uint64_t)set, (uint64_t)info, (uint64_t)timeout);
|
||||
*out_signal = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_sigsuspend(const sigset_t *set) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SIGSUSPEND, &ret, (uint64_t)set);
|
||||
}
|
||||
|
||||
int sys_vm_protect(void *pointer, size_t size, int prot) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_MPROTECT, &ret, (uint64_t)pointer, size, prot);
|
||||
}
|
||||
|
||||
int sys_setuid(uid_t id) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETUID, &ret, id);
|
||||
}
|
||||
|
||||
int sys_setgid(gid_t id) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETGID, &ret, id);
|
||||
}
|
||||
|
||||
int sys_seteuid(uid_t id) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETEUID, &ret, id);
|
||||
}
|
||||
|
||||
int sys_setegid(gid_t id) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETEGID, &ret, id);
|
||||
}
|
||||
|
||||
uid_t sys_getuid() {
|
||||
uid_t r, e, s;
|
||||
sys_getresuid(&r, &e, &s);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
uid_t sys_geteuid() {
|
||||
uid_t r, e, s;
|
||||
sys_getresuid(&r, &e, &s);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
gid_t sys_getgid() {
|
||||
gid_t r, e, s;
|
||||
sys_getresgid(&r, &e, &s);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
gid_t sys_getegid() {
|
||||
gid_t r, e, s;
|
||||
sys_getresgid(&r, &e, &s);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
int sys_setresuid(uid_t _ruid, uid_t _euid, uid_t _suid) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETRESUID, &ret, _ruid, _euid, _suid);
|
||||
}
|
||||
|
||||
int sys_setresgid(gid_t _rgid, gid_t _egid, gid_t _sgid) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETRESGID, &ret, _rgid, _egid, _sgid);
|
||||
}
|
||||
|
||||
int sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_GETRESUID, &ret, (uint64_t)ruid, (uint64_t)euid, (uint64_t)suid);
|
||||
}
|
||||
|
||||
int sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_GETRESGID, &ret, (uint64_t)rgid, (uint64_t)egid, (uint64_t)sgid);
|
||||
}
|
||||
|
||||
int sys_mknodat(int dirfd, const char *path, int mode, int dev) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_MKNODAT, &ret, dirfd, (uint64_t)path, mode, dev);
|
||||
}
|
||||
|
||||
int sys_mkfifoat(int dirfd, const char *path, mode_t mode) {
|
||||
return sys_mknodat(dirfd, path, S_IFIFO | mode, 0);
|
||||
}
|
||||
|
||||
int sys_rmdir(const char *path) {
|
||||
return sys_unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
|
||||
}
|
||||
|
||||
int sys_pread(int fd, void *buf, size_t n, off_t off, ssize_t *bytes_read) {
|
||||
long readc;
|
||||
long error = syscall(SYSCALL_PREAD, &readc, fd, (uint64_t)buf, n, off);
|
||||
*bytes_read = readc;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_pwrite(int fd, const void *buf, size_t n, off_t off, ssize_t *bytes_written) {
|
||||
long writec;
|
||||
long error = syscall(SYSCALL_PWRITE, &writec, fd, (uint64_t)buf, n, off);
|
||||
*bytes_written = writec;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_getrlimit(int resource, struct rlimit *limit) {
|
||||
switch(resource) {
|
||||
case RLIMIT_NOFILE:
|
||||
limit->rlim_cur = RLIM_INFINITY;
|
||||
limit->rlim_max = RLIM_INFINITY;
|
||||
return 0;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
|
||||
typedef struct {
|
||||
ino_t d_ino;
|
||||
off_t d_off;
|
||||
unsigned short d_reclen;
|
||||
unsigned char d_type;
|
||||
char d_name[1024];
|
||||
} dent_t;
|
||||
|
||||
#endif
|
||||
|
||||
int sys_pause() {
|
||||
long ret;
|
||||
return syscall(SYSCALL_PAUSE, &ret);
|
||||
}
|
||||
|
||||
int sys_chroot(const char *path) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_CHROOT, &ret, (uint64_t)path);
|
||||
}
|
||||
|
||||
int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
|
||||
long ret;
|
||||
int len = max_addr_length;
|
||||
long error = syscall(SYSCALL_GETPEERNAME, &ret, fd, (uint64_t)addr_ptr, (uint64_t)&len);
|
||||
*actual_length = len;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
|
||||
long ret;
|
||||
int len = max_addr_length;
|
||||
long error = syscall(SYSCALL_GETSOCKNAME, &ret, fd, (uint64_t)addr_ptr, (uint64_t)&len);
|
||||
*actual_length = len;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) {
|
||||
long ret = 0;
|
||||
long err = syscall(SYSCALL_SOCKETPAIR, &ret, domain, type_and_flags, proto);
|
||||
if(err)
|
||||
return err;
|
||||
|
||||
fds[0] = ret & 0xffffffff;
|
||||
fds[1] = (ret >> 32) & 0xffffffff;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_getitimer(int which, struct itimerval *curr_value) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_GETITIMER, &ret, which, (uint64_t)curr_value);
|
||||
}
|
||||
|
||||
int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETITIMER, &ret, which, (uint64_t)new_value, (uint64_t)old_value);
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
#define TTY_IOCTL_NAME 0x771101141113l
|
||||
#define TTY_NAME_MAX 32
|
||||
#define TTY_PREFIX "/dev/"
|
||||
|
||||
|
||||
int sys_ttyname(int fd, char * buffer, size_t size) {
|
||||
size_t prefixLen = strlen(TTY_PREFIX);
|
||||
if(size < TTY_NAME_MAX + prefixLen) {
|
||||
mlibc::panicLogger() << "ttyname size too small" << frg::endlog;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
strcpy(buffer, TTY_PREFIX);
|
||||
|
||||
int res;
|
||||
return sys_ioctl(fd, TTY_IOCTL_NAME, (void *)(buffer + prefixLen), &res);
|
||||
}
|
||||
#endif
|
||||
|
||||
int sys_fsync(int fd) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FSYNC, &ret, fd);
|
||||
}
|
||||
|
||||
int sys_fdatasync(int fd) {
|
||||
// TODO proper datasync syscall
|
||||
return sys_fsync(fd);
|
||||
}
|
||||
|
||||
pid_t sys_getppid() {
|
||||
long ret;
|
||||
syscall(SYSCALL_GETPPID, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sys_getsid(pid_t pid, pid_t *pgid) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_GETSID, &ret, pid);
|
||||
*pgid = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_getpgid(pid_t pid, pid_t *pgid) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_GETPGID, &ret, pid);
|
||||
*pgid = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_gethostname(char *buffer, size_t bufsize) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_HOSTNAME, &ret, NULL, 0, (uint64_t)buffer, bufsize);
|
||||
}
|
||||
|
||||
int sys_sethostname(const char *buffer, size_t bufsize) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_HOSTNAME, &ret, (uint64_t)buffer, bufsize, NULL, 0);
|
||||
}
|
||||
|
||||
int sys_uname(struct utsname *buf) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_UNAME, &ret, (uint64_t)buf);
|
||||
}
|
||||
|
||||
void sys_sync() {
|
||||
long ret;
|
||||
syscall(SYSCALL_SYNC, &ret);
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
int sys_getentropy(void *buffer, size_t length) {
|
||||
int fd;
|
||||
int error = sys_open("/dev/urandom", O_RDONLY, 0, &fd);
|
||||
if(error)
|
||||
mlibc::panicLogger() << "/dev/urandom open error " << strerror(error) << frg::endlog;
|
||||
|
||||
ssize_t bytes;
|
||||
error = sys_read(fd, buffer, length, &bytes);
|
||||
if(error) {
|
||||
mlibc::infoLogger() << "/dev/urandom read error " << strerror(error) << frg::endlog;
|
||||
return error;
|
||||
}
|
||||
|
||||
sys_close(fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int sys_kill(int pid, int signal) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_KILL, &ret, pid, signal);
|
||||
}
|
||||
|
||||
int sys_sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict retrieve) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SIGPROCMASK, &ret, how, (uint64_t)set, (uint64_t)retrieve);
|
||||
}
|
||||
|
||||
int sys_sigaltstack(const stack_t *ss, stack_t *oss) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SIGALTSTACK, &ret, (uint64_t)ss, (uint64_t)oss);
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
extern "C" void __mlibc_restorer();
|
||||
|
||||
int sys_sigaction(int sig, const struct sigaction *__restrict act,
|
||||
struct sigaction *__restrict oldact) {
|
||||
long ret;
|
||||
|
||||
struct sigaction newAction;
|
||||
if(act)
|
||||
memcpy(&newAction, act, sizeof(struct sigaction));
|
||||
|
||||
if(act && (newAction.sa_flags & SA_RESTORER) == 0) {
|
||||
newAction.sa_restorer = __mlibc_restorer;
|
||||
newAction.sa_flags |= SA_RESTORER;
|
||||
}
|
||||
|
||||
return syscall(SYSCALL_SIGACTION, &ret, sig, act ? (uint64_t)&newAction : 0, (uint64_t)oldact);
|
||||
}
|
||||
|
||||
int sys_ptsname(int fd, char *buffer, size_t length) {
|
||||
int index;
|
||||
int tmp;
|
||||
if(int e = sys_ioctl(fd, TIOCGPTN, &index, &tmp); e)
|
||||
return e;
|
||||
|
||||
if((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) {
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int sys_setpgid(pid_t pid, pid_t pgid) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETPGID, &ret, pid, pgid);
|
||||
}
|
||||
|
||||
int sys_setsid(pid_t *out) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_SETSID, &ret);
|
||||
*out = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_futex_tid() {
|
||||
long ret;
|
||||
syscall(SYSCALL_GETTID, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pid_t sys_gettid() {
|
||||
long ret;
|
||||
syscall(SYSCALL_GETTID, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
|
||||
[[noreturn]] void sys_thread_exit() {
|
||||
syscall(SYSCALL_THREADEXIT, nullptr);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
extern "C" void __mlibc_thread_entry();
|
||||
|
||||
int sys_clone(void *tcb, pid_t *pid_out, void *stack) {
|
||||
(void)tcb;
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_NEWTHREAD, &ret, (uintptr_t)__mlibc_thread_entry, (uintptr_t)stack);
|
||||
*pid_out = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int sys_listen(int fd, int backlog) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_LISTEN, &ret, fd, backlog);
|
||||
}
|
||||
|
||||
int sys_accept(int fd, int *newfd, struct sockaddr *addr_ptr, socklen_t *addr_length, int flags) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_ACCEPT, &ret, fd, (uint64_t)addr_ptr, (uint64_t)addr_length, flags);
|
||||
*newfd = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_CONNECT, &ret, fd, (uint64_t)addr_ptr, addr_length);
|
||||
}
|
||||
|
||||
int sys_msg_recv(int fd, struct msghdr *hdr, int flags, ssize_t *length) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_RECVMSG, &ret, fd, (uint64_t)hdr, flags);
|
||||
*length = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_setsockopt(int fd, int layer, int number, const void *buffer, socklen_t size) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_SETSOCKOPT, &ret, fd, layer, number, (uint64_t)buffer, size);
|
||||
}
|
||||
|
||||
int sys_msg_send(int fd, const struct msghdr *hdr, int flags, ssize_t *length) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_SENDMSG, &ret, fd, (uint64_t)hdr, flags);
|
||||
*length = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_BIND, &ret, fd, (uint64_t)addr_ptr, addr_length);
|
||||
}
|
||||
|
||||
int sys_socket(int family, int type, int protocol, int *fd) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_SOCKET, &ret, family, type, protocol);
|
||||
*fd = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_renameat(int olddirfd, const char *old_path, int newdirfd, const char *new_path) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_RENAMEAT, &ret, olddirfd, (uint64_t)old_path, newdirfd, (uint64_t)new_path);
|
||||
}
|
||||
|
||||
int sys_rename(const char *path, const char *new_path) {
|
||||
return sys_renameat(AT_FDCWD, path, AT_FDCWD, new_path);
|
||||
}
|
||||
|
||||
int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_UTIMENSAT, &ret, dirfd, (uint64_t)pathname, (uint64_t)times, flags);
|
||||
}
|
||||
|
||||
int sys_fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FCHOWNAT, &ret, dirfd, (uint64_t)pathname, owner, group, flags);
|
||||
}
|
||||
|
||||
int sys_mount(const char *source, const char *target, const char *fstype, unsigned long flags, const void *data) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_MOUNT, &ret, (uint64_t)source, (uint64_t)target, (uint64_t)fstype, flags, (uint64_t)data);
|
||||
}
|
||||
|
||||
int sys_ftruncate(int fd, size_t size) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FTRUNCATE, &ret, fd, size);
|
||||
}
|
||||
|
||||
int sys_sleep(time_t *secs, long *nanos) {
|
||||
struct timespec ts;
|
||||
ts.tv_sec = *secs;
|
||||
ts.tv_nsec = *nanos;
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_NANOSLEEP, &ret, (uintptr_t)&ts, (uintptr_t)&ts);
|
||||
*secs = ts.tv_sec;
|
||||
*nanos = ts.tv_nsec;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_tcgetattr(int fd, struct termios *attr){
|
||||
int res;
|
||||
return sys_ioctl(fd, TCGETS, (void *)attr, &res);
|
||||
}
|
||||
|
||||
int sys_tcsetattr(int fd, int act, const struct termios *attr){
|
||||
(void)act;
|
||||
int res;
|
||||
return sys_ioctl(fd, TCSETS, (void *)attr, &res);
|
||||
}
|
||||
|
||||
int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_POLL, &ret, (uint64_t)fds, count, timeout);
|
||||
*num_events = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask, int *num_events) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_PPOLL, &ret, (uint64_t)fds, nfds, (uint64_t)timeout, (uint64_t)sigmask);
|
||||
*num_events = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
#ifndef MLIBC_BUILDING_RTLD
|
||||
int sys_pselect(int num_fds, fd_set *read_set, fd_set *write_set, fd_set *except_set, const struct timespec *timeout, const sigset_t *sigmask, int *num_events) {
|
||||
pollfd *fds = (pollfd *)malloc(num_fds * sizeof(pollfd));
|
||||
|
||||
if(fds == NULL)
|
||||
return ENOMEM;
|
||||
|
||||
int actual_count = 0;
|
||||
|
||||
for(int fd = 0; fd < num_fds; ++fd) {
|
||||
short events = 0;
|
||||
if(read_set && FD_ISSET(fd, read_set)) {
|
||||
events |= POLLIN;
|
||||
}
|
||||
|
||||
if(write_set && FD_ISSET(fd, write_set)) {
|
||||
events |= POLLOUT;
|
||||
}
|
||||
|
||||
if(except_set && FD_ISSET(fd, except_set)) {
|
||||
events |= POLLPRI;
|
||||
}
|
||||
|
||||
if(events) {
|
||||
fds[actual_count].fd = fd;
|
||||
fds[actual_count].events = events;
|
||||
fds[actual_count].revents = 0;
|
||||
actual_count++;
|
||||
}
|
||||
}
|
||||
|
||||
int num;
|
||||
int err = sys_ppoll(fds, actual_count, timeout, sigmask, &num);
|
||||
|
||||
if(err) {
|
||||
free(fds);
|
||||
return err;
|
||||
}
|
||||
|
||||
#define READ_SET_POLLSTUFF (POLLIN | POLLHUP | POLLERR)
|
||||
#define WRITE_SET_POLLSTUFF (POLLOUT | POLLERR)
|
||||
#define EXCEPT_SET_POLLSTUFF (POLLPRI)
|
||||
|
||||
int return_count = 0;
|
||||
for(int fd = 0; fd < actual_count; ++fd) {
|
||||
int events = fds[fd].events;
|
||||
if((events & POLLIN) && (fds[fd].revents & READ_SET_POLLSTUFF) == 0) {
|
||||
FD_CLR(fds[fd].fd, read_set);
|
||||
events &= ~POLLIN;
|
||||
}
|
||||
|
||||
if((events & POLLOUT) && (fds[fd].revents & WRITE_SET_POLLSTUFF) == 0) {
|
||||
FD_CLR(fds[fd].fd, write_set);
|
||||
events &= ~POLLOUT;
|
||||
}
|
||||
|
||||
if((events & POLLPRI) && (fds[fd].revents & EXCEPT_SET_POLLSTUFF) == 0) {
|
||||
FD_CLR(fds[fd].fd, except_set);
|
||||
events &= ~POLLPRI;
|
||||
}
|
||||
|
||||
if(events)
|
||||
return_count++;
|
||||
}
|
||||
*num_events = return_count;
|
||||
free(fds);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int sys_umask(mode_t mode, mode_t *old) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_UMASK, &ret, mode);
|
||||
*old = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_fchmod(int fd, mode_t mode) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FCHMOD, &ret, fd, mode);
|
||||
}
|
||||
|
||||
int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FCHMODAT, &ret, fd, (uint64_t)pathname, mode, flags);
|
||||
}
|
||||
|
||||
int sys_chmod(const char *pathname, mode_t mode) {
|
||||
return sys_fchmodat(AT_FDCWD, pathname, mode, 0);
|
||||
}
|
||||
|
||||
int sys_readlinkat(int dirfd, const char *path, void *buffer, size_t max_size, ssize_t *length) {
|
||||
long ret;
|
||||
long error = syscall(SYSCALL_READLINKAT, &ret, dirfd, (uint64_t)path, (uint64_t)buffer, max_size);
|
||||
*length = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_readlink(const char *path, void *buffer, size_t max_size, ssize_t *length) {
|
||||
return sys_readlinkat(AT_FDCWD, path, buffer, max_size, length);
|
||||
}
|
||||
|
||||
static int dolink(int oldfd, const char *oldpath, int newfd, const char *newpath, int flags, int type) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_LINKAT, &ret, oldfd, (uint64_t)oldpath, newfd, (uint64_t)newpath, flags, type);
|
||||
}
|
||||
|
||||
int sys_linkat(int olddirfd, const char *old_path, int newdirfd, const char *new_path, int flags) {
|
||||
return dolink(olddirfd, old_path, newdirfd, new_path, flags, 0);
|
||||
}
|
||||
|
||||
int sys_link(const char *old_path, const char *new_path) {
|
||||
return sys_linkat(AT_FDCWD, old_path, AT_FDCWD, new_path, 0);
|
||||
}
|
||||
|
||||
int sys_symlinkat(const char *target_path, int dirfd, const char *link_path) {
|
||||
return dolink(AT_FDCWD, target_path, dirfd, link_path, 0, 1);
|
||||
}
|
||||
|
||||
int sys_symlink(const char *target_path, const char *link_path) {
|
||||
return sys_symlinkat(target_path, AT_FDCWD, link_path);
|
||||
}
|
||||
|
||||
int sys_mkdirat(int dirfd, const char *path, mode_t mode) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_MKDIRAT, &ret, dirfd, (uint64_t)path, mode);
|
||||
}
|
||||
|
||||
int sys_mkdir(const char *path, mode_t mode) {
|
||||
return sys_mkdirat(AT_FDCWD, path, mode);
|
||||
}
|
||||
|
||||
int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_IOCTL, &ret, fd, request, (uint64_t)arg);
|
||||
*result = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_unlinkat(int fd, const char *path, int flags) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_UNLINKAT, &ret, fd, (uint64_t)path, flags);
|
||||
}
|
||||
|
||||
int sys_faccessat(int dirfd, const char *pathname, int mode, int flags){
|
||||
long ret;
|
||||
return syscall(SYSCALL_FACCESSAT, &ret, dirfd, (uint64_t)pathname, mode, flags);
|
||||
}
|
||||
|
||||
int sys_access(const char *path, int mode){
|
||||
return sys_faccessat(AT_FDCWD, path, mode, 0);
|
||||
}
|
||||
|
||||
int sys_pipe(int *fds, int flags) {
|
||||
long ret = 0;
|
||||
long err = syscall(SYSCALL_PIPE2, &ret, flags);
|
||||
if(err)
|
||||
return err;
|
||||
|
||||
fds[0] = ret & 0xffffffff;
|
||||
fds[1] = (ret >> 32) & 0xffffffff;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_chdir(const char *path) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_CHDIR, &ret, (uint64_t)path);
|
||||
}
|
||||
|
||||
int sys_fchdir(int fd) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FCHDIR, &ret, fd);
|
||||
}
|
||||
|
||||
int sys_fcntl(int fd, int request, va_list args, int *result) {
|
||||
long arg = va_arg(args, uint64_t);
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_FCNTL, &ret, fd, request, arg);
|
||||
*result = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_dup(int fd, int flags, int *newfd) {
|
||||
(void)flags;
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_DUP, &ret, fd);
|
||||
*newfd = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_dup2(int fd, int flags, int newfd) {
|
||||
(void)flags;
|
||||
long ret;
|
||||
return syscall(SYSCALL_DUP2, &ret, fd, newfd);
|
||||
}
|
||||
|
||||
int sys_read_entries(int handle, void *buffer, size_t max_size, size_t *bytes_read) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_GETDENTS, &ret, handle, (uint64_t)buffer, max_size);
|
||||
if(err)
|
||||
return err;
|
||||
*bytes_read = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru, pid_t *ret_pid) {
|
||||
(void)ru;
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_WAITPID, &ret, pid, (uint64_t)status, flags);
|
||||
*ret_pid = ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_EXECVE, &ret, (uint64_t)path, (uint64_t)argv, (uint64_t)envp);
|
||||
}
|
||||
|
||||
int sys_fork(pid_t *pid) {
|
||||
long ret = 0;
|
||||
long error = syscall(SYSCALL_FORK, &ret);
|
||||
*pid = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat *statbuf) {
|
||||
long ret;
|
||||
switch (fsfdt) {
|
||||
case fsfd_target::path:
|
||||
return syscall(SYSCALL_FSTATAT, &ret, AT_FDCWD, (uint64_t)path, (uint64_t)statbuf, flags);
|
||||
case fsfd_target::fd:
|
||||
return syscall(SYSCALL_FSTAT, &ret, fd, (uint64_t)statbuf);
|
||||
case fsfd_target::fd_path:
|
||||
return syscall(SYSCALL_FSTATAT, &ret, fd, (uint64_t)path, (uint64_t)statbuf, flags);
|
||||
default:
|
||||
mlibc::infoLogger() << "mlibc: stat: Unknown fsfd_target: " << (int)fsfdt << frg::endlog;
|
||||
return ENOSYS;
|
||||
}
|
||||
}
|
||||
|
||||
pid_t sys_getpid() {
|
||||
long ret;
|
||||
syscall(SYSCALL_GETPID, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sys_libc_log(const char *message) {
|
||||
long ret;
|
||||
syscall(SYSCALL_PRINT, &ret, (uint64_t)message);
|
||||
}
|
||||
|
||||
[[noreturn]] void sys_libc_panic() {
|
||||
sys_libc_log("mlibc: panic");
|
||||
sys_exit(1);
|
||||
}
|
||||
|
||||
[[noreturn]] void sys_exit(int status) {
|
||||
syscall(SYSCALL_EXIT, NULL, status);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int sys_tcb_set(void *pointer) {
|
||||
long r;
|
||||
return syscall(SYSCALL_ARCHCTL, &r, ARCH_CTL_SET_FSBASE, (uint64_t)pointer);
|
||||
}
|
||||
|
||||
#define FUTEX_WAIT 0
|
||||
#define FUTEX_WAKE 1
|
||||
|
||||
int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FUTEX, &ret, (uint64_t)pointer, FUTEX_WAIT, expected, (uint64_t)time);
|
||||
}
|
||||
|
||||
int sys_futex_wake(int *pointer) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_FUTEX, &ret, (uint64_t)pointer, FUTEX_WAKE, INT_MAX, NULL);
|
||||
}
|
||||
|
||||
int sys_anon_allocate(size_t size, void **pointer) {
|
||||
size += 4096 - (size % 4096);
|
||||
return sys_vm_map(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0, pointer);
|
||||
}
|
||||
int sys_anon_free(void *pointer, size_t size) {
|
||||
size += 4096 - (size % 4096);
|
||||
return sys_vm_unmap(pointer, size);
|
||||
}
|
||||
|
||||
int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_OPENAT, &ret, dirfd, (uint64_t)path, flags, mode);
|
||||
if(err)
|
||||
return err;
|
||||
*fd = ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_open(const char *pathname, int flags, mode_t mode, int *fd) {
|
||||
return sys_openat(AT_FDCWD, pathname, flags, mode, fd);
|
||||
};
|
||||
|
||||
int sys_open_dir(const char *path, int *handle) {
|
||||
return sys_open(path, O_DIRECTORY, 0, handle);
|
||||
}
|
||||
|
||||
int sys_read(int fd, void *buff, size_t count, ssize_t *bytes_read) {
|
||||
long readc;
|
||||
long error = syscall(SYSCALL_READ, &readc, fd, (uint64_t)buff, count);
|
||||
*bytes_read = readc;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_write(int fd, const void *buff, size_t count, ssize_t *bytes_written) {
|
||||
long writec;
|
||||
long error = syscall(SYSCALL_WRITE, &writec, fd, (uint64_t)buff, count);
|
||||
*bytes_written = writec;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
|
||||
long ret = 0;
|
||||
long error = syscall(SYSCALL_SEEK, &ret, fd, offset, whence);
|
||||
*new_offset = ret;
|
||||
return error;
|
||||
}
|
||||
|
||||
int sys_close(int fd) {
|
||||
long r;
|
||||
return syscall(SYSCALL_CLOSE, &r, fd);
|
||||
}
|
||||
|
||||
int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset, void **window) {
|
||||
long ret;
|
||||
long err = syscall(SYSCALL_MMAP, &ret, (uint64_t)hint, size, prot, flags, fd, offset);
|
||||
*window = (void *)ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_vm_unmap(void *pointer, size_t size) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_MUNMAP, &ret, (uintptr_t)pointer, size);
|
||||
}
|
||||
|
||||
int sys_isatty(int fd) {
|
||||
long ret;
|
||||
return syscall(SYSCALL_ISATTY, &ret, fd);
|
||||
}
|
||||
|
||||
int sys_clock_get(int clock, time_t *secs, long *nanos) {
|
||||
struct timespec ts;
|
||||
long ret;
|
||||
int err = syscall(SYSCALL_CLOCKGET, &ret, clock, (uint64_t)&ts);
|
||||
*secs = ts.tv_sec;
|
||||
*nanos = ts.tv_nsec;
|
||||
return err;
|
||||
}
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/access.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/auxv.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/blkcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/blksize_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/clockid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/dev_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/epoll.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/errno.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fcntl.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fsblkcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/fsfilcnt_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/gid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/in.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/ino_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/inotify.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/ioctls.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/astral/ipc.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/limits.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/mode_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/mqueue.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/msg.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/nlink_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/packet.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/pid_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/poll.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/ptrace.h
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user