Skip to content

Commit

Permalink
Merge pull request #1057 from mintsuki/vinix
Browse files Browse the repository at this point in the history
sysdeps/vinix: Reupstream port
  • Loading branch information
mintsuki authored Apr 19, 2024
2 parents c8b7fd6 + 4db030c commit 3bfb8e7
Show file tree
Hide file tree
Showing 67 changed files with 2,417 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
compile-sysdeps:
strategy:
matrix:
sysdeps: [dripos, lemon, aero, ironclad, lyre, keyronex, managarm]
sysdeps: [dripos, lemon, aero, ironclad, lyre, keyronex, managarm, vinix]
name: Compile sysdeps
runs-on: ubuntu-22.04
container:
Expand Down
191 changes: 191 additions & 0 deletions abis/vinix/signal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#ifndef _ABIBITS_SIGNAL_H
#define _ABIBITS_SIGNAL_H

#include <abi-bits/pid_t.h>
#include <abi-bits/uid_t.h>
#include <bits/size_t.h>

union sigval {
int sival_int;
void *sival_ptr;
};

typedef struct {
int si_signo;
int si_code;
int si_errno;
pid_t si_pid;
uid_t si_uid;
void *si_addr;
int si_status;
union sigval si_value;
} siginfo_t;

#ifdef __cplusplus
extern "C" {
#endif

// Argument for signal()
typedef void (*__sighandler) (int);

#define SIG_ERR ((__sighandler)(void *)(-1))
#define SIG_DFL ((__sighandler)(void *)(-2))
#define SIG_IGN ((__sighandler)(void *)(-3))

#define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGBUS 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGSTKFLT 16
#define SIGCHLD 17
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGURG 23
#define SIGXCPU 24
#define SIGXFSZ 25
#define SIGVTALRM 26
#define SIGPROF 27
#define SIGWINCH 28
#define SIGIO 29
#define SIGPOLL SIGIO
#define SIGPWR 30
#define SIGSYS 31
#define SIGRTMIN 32
#define SIGRTMAX 33
#define SIGCANCEL 34

// siginfo->si_info constants
// SIGBUS
#define BUS_ADRALN 1
#define BUS_ADRERR 2
#define BUS_OBJERR 3

// SIGILL
#define ILL_ILLOPC 1
#define ILL_ILLOPN 2
#define ILL_ILLADR 3
#define ILL_ILLTRP 4
#define ILL_PRVOPC 5
#define ILL_PRVREG 6
#define ILL_COPROC 7
#define ILL_BADSTK 8
#define ILL_BADIADDR 9

// SIGSEGV
#define SEGV_MAPERR 1
#define SEGV_ACCERR 2

// TODO: replace this by uint64_t
typedef long sigset_t;

#define SIGUNUSED SIGSYS

// constants for sigprocmask()
#define SIG_BLOCK 1
#define SIG_UNBLOCK 2
#define SIG_SETMASK 3

#define SA_NOCLDSTOP (1 << 0)
#define SA_ONSTACK (1 << 1)
#define SA_RESETHAND (1 << 2)
#define SA_RESTART (1 << 3)
#define SA_SIGINFO (1 << 4)
#define SA_NOCLDWAIT (1 << 5)
#define SA_NODEFER (1 << 6)

#define MINSIGSTKSZ 2048
#define SIGSTKSZ 8192
#define SS_ONSTACK 1
#define SS_DISABLE 2

typedef struct __stack {
void *ss_sp;
size_t ss_size;
int ss_flags;
} stack_t;

// constants for sigev_notify of struct sigevent
#define SIGEV_NONE 1
#define SIGEV_SIGNAL 2
#define SIGEV_THREAD 3

#define SI_ASYNCNL (-60)
#define SI_TKILL (-6)
#define SI_SIGIO (-5)
#define SI_ASYNCIO (-4)
#define SI_MESGQ (-3)
#define SI_TIMER (-2)
#define SI_QUEUE (-1)
#define SI_USER 0
#define SI_KERNEL 128

#define NSIG 65

#define CLD_EXITED 1
#define CLD_KILLED 2
#define CLD_DUMPED 3
#define CLD_TRAPPED 4
#define CLD_STOPPED 5
#define CLD_CONTINUED 6

struct sigevent {
int sigev_notify;
int sigev_signo;
union sigval sigev_value;
void (*sigev_notify_function)(union sigval);
// MISSING: sigev_notify_attributes
};

struct sigaction {
union {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
};
sigset_t sa_mask;
int sa_flags;
};

#if defined(__x86_64__) || defined(__aarch64__)
// TODO: This is wrong for AArch64.

typedef struct {
unsigned long oldmask;
unsigned long gregs[16];
unsigned long pc, pr, sr;
unsigned long gbr, mach, macl;
unsigned long fpregs[16];
unsigned long xfpregs[16];
unsigned int fpscr, fpul, ownedfp;
} mcontext_t;

typedef struct __ucontext {
unsigned long uc_flags;
struct __ucontext *uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
sigset_t uc_sigmask;
} ucontext_t;

#else
#error "Missing architecture specific code."
#endif

#ifdef __cplusplus
}
#endif

#endif // _ABIBITS_SIGNAL_H
12 changes: 12 additions & 0 deletions ci/vinix.cross-file
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[binaries]
c = 'x86_64-linux-mlibc-gcc'
cpp = 'x86_64-linux-mlibc-g++'

[properties]
needs_exe_wrapper = true

[host_machine]
system = 'vinix'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ elif host_machine.system() == 'keyronex'
internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
subdir('sysdeps/keyronex')
elif host_machine.system() == 'vinix'
disable_linux_option = true
rtld_include_dirs += include_directories('sysdeps/vinix/include')
libc_include_dirs += include_directories('sysdeps/vinix/include')
subdir('sysdeps/vinix')
elif host_machine.system() == 'lyre'
disable_linux_option = true
rtld_include_dirs += include_directories('sysdeps/lyre/include')
Expand Down
120 changes: 120 additions & 0 deletions sysdeps/vinix/generic/entry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <stdint.h>
#include <stdlib.h>
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/elf/startup.h>
#include <mlibc/all-sysdeps.hpp>
#include <bits/posix/posix_signal.h>

#include "syscall.h"

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

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);
}

struct GPRState {
uint64_t ds;
uint64_t es;
uint64_t rax;
uint64_t rbx;
uint64_t rcx;
uint64_t rdx;
uint64_t rsi;
uint64_t rdi;
uint64_t rbp;
uint64_t r8;
uint64_t r9;
uint64_t r10;
uint64_t r11;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
uint64_t err;
uint64_t rip;
uint64_t cs;
uint64_t rflags;
uint64_t rsp;
uint64_t ss;
};

namespace mlibc {
int sys_sigentry(void *sigentry) {
__syscall_ret ret = __syscall(27, sigentry);
if (ret.errno != 0)
return ret.errno;
return 0;
}

[[noreturn]] int sys_sigreturn(void *context, sigset_t old_mask) {
__syscall(30, context, old_mask);
__builtin_unreachable();
}
}

static void __mlibc_sigentry(int which, siginfo_t *siginfo,
void (*sa)(int, siginfo_t *, void *),
GPRState *ret_context, sigset_t prev_mask) {

/*
size_t *base_ptr = (size_t *)ret_context->rbp;
mlibc::infoLogger() << "Stacktrace:" << frg::endlog;
mlibc::infoLogger() << " [" << (void *)ret_context->rip << "]" << frg::endlog;
for (;;) {
size_t old_bp = base_ptr[0];
size_t ret_addr = base_ptr[1];
if (!ret_addr)
break;
size_t off;
mlibc::infoLogger() << " [" << (void *)ret_addr << "]" << frg::endlog;
if (!old_bp)
break;
base_ptr = (size_t *)old_bp;
}
*/

switch ((uintptr_t)sa) {
// DFL
case (uintptr_t)(-2):
mlibc::infoLogger() << "mlibc: Unhandled signal " << which << frg::endlog;
mlibc::sys_exit(128 + which);
// IGN
case (uintptr_t)(-3):
break;
default:
sa(which, siginfo, NULL);
break;
}

mlibc::sys_sigreturn(ret_context, prev_mask);

__builtin_unreachable();
}

extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
mlibc::sys_sigentry((void *)__mlibc_sigentry);

__dlapi_enter(entry_stack);
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
exit(result);
}
Loading

0 comments on commit 3bfb8e7

Please sign in to comment.