Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ elif host_machine.system() == 'nyaux'
rtld_include_dirs += include_directories('sysdeps/nyaux/include')
libc_include_dirs += include_directories('sysdeps/nyaux/include')
subdir('sysdeps/nyaux')
elif host_machine.system() == 'cryptix'
rtld_include_dirs += include_directories('sysdeps/cryptix/include')
libc_include_dirs += include_directories('sysdeps/cryptix/include')

#internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
internal_conf.set10('MLIBC_MMAP_ALLOCATE_DSO', true)
internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
subdir('sysdeps/cryptix')
else
error('No sysdeps defined for OS: ' + host_machine.system())
endif
Expand Down
1 change: 1 addition & 0 deletions sysdeps/cryptix/arch/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir(host_machine.cpu_family())
7 changes: 7 additions & 0 deletions sysdeps/cryptix/arch/x86_64/crt0.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.section .text
.global _start
_start:
mov %rsp, %rdi
mov $main, %rsi
call __mlibc_entry
.section .note.GNU-stack,"",%progbitst
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use GNU_STACK_NOTE from the mlibc-asm/helpers.h header.

10 changes: 10 additions & 0 deletions sysdeps/cryptix/arch/x86_64/crti.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.section .init
.global _init
_init:
push %rax

.section .fini
.global _fini
_fini:
push %rax
.section .note.GNU-stack,"",%progbits
8 changes: 8 additions & 0 deletions sysdeps/cryptix/arch/x86_64/crtn.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.section .init
pop %rax
ret

.section .fini
pop %rax
ret
.section .note.GNU-stack,"",%progbits
37 changes: 37 additions & 0 deletions sysdeps/cryptix/arch/x86_64/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
if get_option('posix_option').allowed()
libc_sources += files(
'thread_entry.S'
)
endif

if not headers_only
crt = custom_target(
'crt0',
build_by_default: true,
command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'],
input: '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: '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: 'crtn.S',
output: 'crtn.o',
install: true,
install_dir: get_option('libdir'),
)
endif
23 changes: 23 additions & 0 deletions sysdeps/cryptix/arch/x86_64/thread_entry.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.section .text
.global __mlibc_spawn_thread
.type __mlibc_spawn_thread, "function"
__mlibc_spawn_thread:
xor %eax, %eax
/* The rest of the args are already in the right registers,
* only need to fixup rcx to r10
*/
mov %rcx, %r10
mov $56, %al
syscall
test %eax, %eax
jnz 1f
xor %ebp, %ebp
pop %rdi
pop %rsi
call __mlibc_enter_thread
hlt
1:
ret
.section .note.GNU-stack,"",%progbits


63 changes: 63 additions & 0 deletions sysdeps/cryptix/entry/entry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <cryptix/syscall.h>
#include <mlibc/elf/startup.h>

#include <mlibc/debug.hpp>
#include <mlibc/posix-sysdeps.hpp>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

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

extern "C" {
unsigned int __mlibc_dev_major(unsigned long long int __dev) {
return ((__dev >> 8) & 0xfff) | ((unsigned int)(__dev >> 32) & ~0xfff);
}

unsigned int __mlibc_dev_minor(unsigned long long int __dev) {
return (__dev & 0xff) | ((unsigned int)(__dev >> 12) & ~0xff);
}

unsigned long long int __mlibc_dev_makedev(unsigned int __major, unsigned int __minor) {
return (
(__minor & 0xff) | ((__major & 0xfff) << 8)
| (((unsigned long long int)(__minor & ~0xff)) << 12)
| (((unsigned long long int)(__major & ~0xfff)) << 32)
);
}
}

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;
namespace mlibc {

int sys_anon_allocate(size_t size, void **pointer);
}

LibraryGuard::LibraryGuard() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here instead of just using the init_libc constructor?

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

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

auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
exit(result);
}
4 changes: 4 additions & 0 deletions sysdeps/cryptix/entry/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
libc_sources += files(
'entry.cpp',
'thread.cpp',
)
66 changes: 66 additions & 0 deletions sysdeps/cryptix/entry/thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <bits/ensure.h>
#include <errno.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/thread.hpp>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>

namespace mlibc {
extern "C" void __mlibc_enter_thread(void *entry, void *user_arg) {
auto tcb = mlibc::get_current_tcb();

while (!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);
tcb->invokeThreadFunc(entry, user_arg);

__atomic_store_n(&tcb->didExit, 1, __ATOMIC_RELEASE);
mlibc::sys_futex_wake(&tcb->didExit);
mlibc::sys_thread_exit();
}

static constexpr size_t default_stacksize = 0x200000; // 2 mib
int sys_prepare_stack(
void **stack,
void *entry,
void *user_arg,
void *tcb,
size_t *stack_size,
size_t *guard_size,
void **stack_base
) {
(void)tcb;
if (!*stack_size)
*stack_size = default_stacksize;

uintptr_t map;
if (*stack) {
map = reinterpret_cast<uintptr_t>(*stack);
*guard_size = 0;
} else {
sys_vm_map(
nullptr,
*stack_size + *guard_size,
PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0,
reinterpret_cast<void **>(&map)
);
if (reinterpret_cast<void *>(map) == MAP_FAILED)
return EAGAIN;
int ret = sys_vm_protect(
reinterpret_cast<void *>(map + *guard_size), *stack_size, PROT_READ | PROT_WRITE
);
if (ret)
return EAGAIN;
}

*stack_base = reinterpret_cast<void *>(map);
auto sp = reinterpret_cast<uintptr_t *>(map + *guard_size + *stack_size);
*--sp = reinterpret_cast<uintptr_t>(user_arg);
*--sp = reinterpret_cast<uintptr_t>(entry);
*stack = reinterpret_cast<void *>(sp);
return 0;
}
} // namespace mlibc
41 changes: 41 additions & 0 deletions sysdeps/cryptix/generic/reboot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/ensure.h>
#include <cryptix/reboot.hpp>
#include <cryptix/syscall.h>
#include <errno.h>
#include <sys/reboot.h>

using namespace cryptix;

RebootCmd cryptix_reboot_cmd(unsigned int what) {
switch (what) {
case 0x01234567:
return RebootCmd::eRestart;
case 0xCDEF0123:
return RebootCmd::eHalt;
case 0x4321FEDC:
return RebootCmd::ePowerOff;
case 0xA1B2C3D4:
return RebootCmd::eRestart2;
case 0xD000FCE2:
return RebootCmd::eSuspend;
case 0x45584543:
return RebootCmd::eKexec;

default:
break;
}

return RebootCmd::eUndefined;
}

int reboot(int what) {
auto cmd = cryptix_reboot_cmd(what);

auto ret = Syscall(SYS_REBOOT, cmd);
if (auto e = syscall_error(ret); e) {
errno = e;
return -1;
}

return 0;
}
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/access.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/auxv.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/blkcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/blksize_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/clockid_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/dev_t.h
3 changes: 3 additions & 0 deletions sysdeps/cryptix/include/abi-bits/do
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it shouldn't be checked in - we already have a helper script.


ln -s ../../../../abis/linux/$1.h $1.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/epoll.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/errno.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/fcntl.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/fsblkcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/fsfilcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/gid_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/in.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/ino_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/inotify.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/ioctls.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/ipc.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/limits.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/mode_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/mqueue.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/msg.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/nlink_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/packet.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/pid_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/poll.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/ptrace.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/random.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/reboot.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/resource.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/riscv-hwprobe.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/rlim_t.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/seek-whence.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/shm.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/sigevent.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/signal.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/sigval.h
1 change: 1 addition & 0 deletions sysdeps/cryptix/include/abi-bits/socket.h
Loading