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,10 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov x0, sp
|
||||
adr x1, main
|
||||
|
||||
bl __mlibc_entry
|
||||
brk #0
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov x0, sp
|
||||
adrp x1, main
|
||||
add x1, x1, :lo12:main
|
||||
bl __mlibc_entry
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,13 @@
|
||||
.section .init
|
||||
.global _init
|
||||
_init:
|
||||
stp x29, x30, [sp, -16]!
|
||||
mov x29, sp
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
_fini:
|
||||
stp x29, x30, [sp, -16]!
|
||||
mov x29, sp
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,7 @@
|
||||
.section .init
|
||||
ldp x29, x30, [sp], #16
|
||||
|
||||
.section .fini
|
||||
ldp x29, x30, [sp], #16
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .text
|
||||
.global __mlibc_start_thread
|
||||
__mlibc_start_thread:
|
||||
ldr x0, [sp], #8
|
||||
ldr x1, [sp], #8
|
||||
ldr x2, [sp], #8
|
||||
bl __mlibc_enter_thread
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/elf/startup.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern "C" void __dlapi_enter(uintptr_t *);
|
||||
|
||||
extern char **environ;
|
||||
|
||||
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::entry_stack.argc, mlibc::entry_stack.argv, environ);
|
||||
exit(result);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
#include <abi-bits/fcntl.h>
|
||||
#include <abi-bits/pid_t.h>
|
||||
#include <menix/archctl.hpp>
|
||||
#include <menix/syscall.hpp>
|
||||
#include <mlibc/internal-sysdeps.hpp>
|
||||
#include <mlibc/tcb.hpp>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
namespace mlibc {
|
||||
|
||||
void sys_libc_log(const char *message) {
|
||||
menix_syscall(SYSCALL_WRITE, 1, (size_t)message, strlen(message));
|
||||
menix_syscall(SYSCALL_WRITE, 1, (size_t)"\n", 1);
|
||||
}
|
||||
|
||||
[[noreturn]] void sys_libc_panic() {
|
||||
sys_libc_log("mlibc panic!");
|
||||
menix_syscall(SYSCALL_EXIT, 1);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int sys_tcb_set(void *pointer) {
|
||||
#if defined(__x86_64__)
|
||||
return menix_syscall(SYSCALL_ARCHCTL, ARCHCTL_SET_FSBASE, (size_t)pointer).error;
|
||||
#elif defined(__aarch64__)
|
||||
uintptr_t thread_data = reinterpret_cast<uintptr_t>(pointer) + sizeof(Tcb) - 0x10;
|
||||
asm volatile("msr tpidr_el0, %0" ::"r"(thread_data));
|
||||
return 0;
|
||||
#elif defined(__riscv)
|
||||
uintptr_t thread_data = reinterpret_cast<uintptr_t>(pointer) + sizeof(Tcb);
|
||||
asm volatile("mv tp, %0" ::"r"(thread_data));
|
||||
return 0;
|
||||
#elif defined(__loongarch64)
|
||||
uintptr_t thread_data = reinterpret_cast<uintptr_t>(pointer) + sizeof(Tcb);
|
||||
asm volatile("move $tp, %0" ::"r"(thread_data));
|
||||
return 0;
|
||||
#else
|
||||
#error "Unsupported architecture!"
|
||||
#endif
|
||||
}
|
||||
|
||||
int sys_futex_tid() { return menix_syscall(SYSCALL_GETTID).value; }
|
||||
|
||||
int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
|
||||
return menix_syscall(SYSCALL_FUTEX_WAIT, (size_t)pointer, expected, (size_t)time).error;
|
||||
}
|
||||
|
||||
int sys_futex_wake(int *pointer) {
|
||||
return menix_syscall(SYSCALL_FUTEX_WAKE, (size_t)pointer).error;
|
||||
}
|
||||
|
||||
int sys_anon_allocate(size_t size, void **pointer) {
|
||||
auto r = menix_syscall(
|
||||
SYSCALL_MMAP, 0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0
|
||||
);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*pointer = (void *)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_anon_free(void *pointer, size_t size) {
|
||||
return menix_syscall(SYSCALL_MUNMAP, (size_t)pointer, size).error;
|
||||
}
|
||||
|
||||
int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) {
|
||||
auto r = menix_syscall(SYSCALL_OPENAT, dirfd, (size_t)path, flags, mode);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*fd = (int)r.value;
|
||||
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_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
|
||||
auto r = menix_syscall(SYSCALL_READ, fd, (size_t)buf, count);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*bytes_read = (ssize_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
|
||||
auto r = menix_syscall(SYSCALL_SEEK, fd, offset, whence);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*new_offset = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_close(int fd) { return menix_syscall(SYSCALL_CLOSE, fd).error; }
|
||||
|
||||
int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat *statbuf) {
|
||||
switch (fsfdt) {
|
||||
case fsfd_target::path:
|
||||
return menix_syscall(SYSCALL_FSTATAT, AT_FDCWD, (size_t)path, (size_t)statbuf, flags)
|
||||
.error;
|
||||
case fsfd_target::fd_path:
|
||||
return menix_syscall(SYSCALL_FSTATAT, fd, (size_t)path, (size_t)statbuf, flags).error;
|
||||
case fsfd_target::fd:
|
||||
return menix_syscall(SYSCALL_FSTAT, fd, (size_t)statbuf).error;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset, void **window) {
|
||||
auto r = menix_syscall(SYSCALL_MMAP, (size_t)hint, size, prot, flags, fd, offset);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*window = (void *)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_vm_unmap(void *pointer, size_t size) {
|
||||
return menix_syscall(SYSCALL_MUNMAP, (size_t)pointer, size).error;
|
||||
}
|
||||
|
||||
int sys_vm_protect(void *pointer, size_t size, int prot) {
|
||||
return menix_syscall(SYSCALL_MPROTECT, (size_t)pointer, size, prot).error;
|
||||
}
|
||||
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <abi-bits/pid_t.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <bits/file.h>
|
||||
#include <menix/power.hpp>
|
||||
#include <menix/syscall.hpp>
|
||||
#include <mlibc/linux-sysdeps.hpp>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace mlibc {
|
||||
|
||||
int sys_mount(
|
||||
const char *source,
|
||||
const char *target,
|
||||
const char *fstype,
|
||||
unsigned long flags,
|
||||
const void *data
|
||||
) {
|
||||
return menix_syscall(
|
||||
SYSCALL_MOUNT, (size_t)source, (size_t)target, (size_t)fstype, flags, (size_t)data
|
||||
)
|
||||
.error;
|
||||
}
|
||||
|
||||
int sys_umount2(const char *target, int flags) {
|
||||
return menix_syscall(SYSCALL_UMOUNT, (size_t)target, flags).error;
|
||||
}
|
||||
|
||||
int sys_ptrace(long req, pid_t pid, void *addr, void *data, long *out) {
|
||||
auto r = menix_syscall(SYSCALL_PTRACE, req, pid, (size_t)addr, (size_t)data);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*out = (long)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_klogctl(int type, char *bufp, int len, int *out) {
|
||||
auto r = menix_syscall(SYSCALL_SYSLOG, type, (size_t)bufp, len);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*out = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_reboot(int cmd) {
|
||||
return menix_syscall(SYSCALL_REBOOT, MENIX_REBOOT_MAGIC1, MENIX_REBOOT_MAGIC2, cmd).error;
|
||||
}
|
||||
|
||||
int sys_getcpu(int *cpu) { return menix_syscall(SYSCALL_GETCPU, (size_t)cpu, 0, 0).error; }
|
||||
|
||||
int sys_sysinfo(struct sysinfo *info) { return menix_syscall(SYSCALL_SYSINFO, (size_t)info).error; }
|
||||
|
||||
int sys_swapon(const char *path, int flags) {
|
||||
return menix_syscall(SYSCALL_SWAPON, (size_t)path, flags).error;
|
||||
}
|
||||
|
||||
int sys_swapoff(const char *path) { return menix_syscall(SYSCALL_SWAPOFF, (size_t)path).error; }
|
||||
|
||||
} // namespace mlibc
|
||||
@@ -0,0 +1,767 @@
|
||||
#include <abi-bits/limits.h>
|
||||
#include <abi-bits/pid_t.h>
|
||||
#include <asm/ioctls.h>
|
||||
#include <errno.h>
|
||||
#include <frg/logging.hpp>
|
||||
#include <menix/power.hpp>
|
||||
#include <menix/syscall.hpp>
|
||||
#include <mlibc/debug.hpp>
|
||||
#include <mlibc/posix-sysdeps.hpp>
|
||||
#include <mlibc/tcb.hpp>
|
||||
#include <net/if.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace mlibc {
|
||||
|
||||
[[noreturn]] void sys_exit(int status) {
|
||||
menix_syscall(SYSCALL_EXIT, status);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
[[noreturn]] void sys_thread_exit() {
|
||||
menix_syscall(SYSCALL_THREAD_EXIT, 0);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int sys_clock_get(int clock, time_t *secs, long *nanos) {
|
||||
struct timespec ts;
|
||||
auto r = menix_syscall(SYSCALL_CLOCK_GET, clock, (size_t)&ts);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*secs = ts.tv_sec;
|
||||
*nanos = ts.tv_nsec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_clock_getres(int clock, time_t *secs, long *nanos) {
|
||||
struct timespec ts;
|
||||
auto r = menix_syscall(SYSCALL_CLOCK_GETRES, clock, (size_t)&ts);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*secs = ts.tv_sec;
|
||||
*nanos = ts.tv_nsec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_flock(int fd, int options) {
|
||||
auto r = menix_syscall(SYSCALL_FLOCK, fd, options);
|
||||
return r.error;
|
||||
}
|
||||
|
||||
int sys_open_dir(const char *path, int *handle) { return sys_open(path, O_RDONLY | O_DIRECTORY, 0600, handle); }
|
||||
|
||||
int sys_read_entries(int handle, void *buffer, size_t max_size, size_t *bytes_read) {
|
||||
auto r = menix_syscall(SYSCALL_GETDENTS, handle, (size_t)buffer, (size_t)max_size);
|
||||
if (r.error) {
|
||||
return r.error;
|
||||
}
|
||||
*bytes_read = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
|
||||
auto r = menix_syscall(SYSCALL_WRITE, fd, (size_t)buf, count);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*bytes_written = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_pread(int fd, void *buf, size_t n, off_t off, ssize_t *bytes_read) {
|
||||
auto r = menix_syscall(SYSCALL_PREAD, fd, (size_t)buf, n, off);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*bytes_read = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_pwrite(int fd, const void *buf, size_t n, off_t off, ssize_t *bytes_read) {
|
||||
auto r = menix_syscall(SYSCALL_PWRITE, fd, (size_t)buf, n, off);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*bytes_read = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_access(const char *path, int mode) { return sys_faccessat(AT_FDCWD, path, mode, 0); }
|
||||
|
||||
int sys_faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||
return menix_syscall(SYSCALL_FACCESSAT, dirfd, (size_t)pathname, mode, flags).error;
|
||||
}
|
||||
|
||||
int sys_dup(int fd, int flags, int *newfd) {
|
||||
auto r = menix_syscall(SYSCALL_DUP, fd, flags);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*newfd = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_dup2(int fd, int flags, int newfd) {
|
||||
// dup2 is roughly equal to dup3.
|
||||
return menix_syscall(SYSCALL_DUP3, fd, newfd, flags).error;
|
||||
}
|
||||
|
||||
int sys_isatty(int fd) {
|
||||
struct winsize ws;
|
||||
// If we're a TTY then this call will succeed.
|
||||
if (!sys_ioctl(fd, TIOCGWINSZ, &ws, 0))
|
||||
return 0;
|
||||
|
||||
return ENOTTY;
|
||||
}
|
||||
|
||||
int sys_statvfs(const char *path, struct statvfs *out) {
|
||||
return menix_syscall(SYSCALL_STATVFS, (size_t)path, (size_t)out).error;
|
||||
}
|
||||
|
||||
int sys_fstatvfs(int fd, struct statvfs *out) {
|
||||
return menix_syscall(SYSCALL_FSTATVFS, fd, (size_t)out).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);
|
||||
}
|
||||
|
||||
int sys_readlinkat(int dirfd, const char *path, void *buffer, size_t max_size, ssize_t *length) {
|
||||
auto r =
|
||||
menix_syscall(SYSCALL_READLINKAT, dirfd, (size_t)path, (size_t)buffer, (size_t)max_size);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*length = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_rmdir(const char *path) {
|
||||
return menix_syscall(SYSCALL_RMDIRAT, AT_FDCWD, (size_t)path).error;
|
||||
}
|
||||
|
||||
int sys_ftruncate(int fd, size_t size) { return menix_syscall(SYSCALL_FTRUNCATE, fd, size).error; }
|
||||
|
||||
int sys_fallocate(int fd, off_t offset, size_t size) {
|
||||
return menix_syscall(SYSCALL_FALLOCATE, fd, offset, size).error;
|
||||
}
|
||||
|
||||
int sys_unlinkat(int fd, const char *path, int flags) {
|
||||
return menix_syscall(SYSCALL_UNLINKAT, fd, (size_t)path, flags).error;
|
||||
}
|
||||
|
||||
int sys_socket(int family, int type, int protocol, int *fd) {
|
||||
auto r = menix_syscall(SYSCALL_SOCKET, family, type, protocol);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*fd = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_msg_send(int fd, const struct msghdr *hdr, int flags, ssize_t *length) {
|
||||
auto r = menix_syscall(SYSCALL_SENDMSG, fd, (size_t)hdr, flags);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*length = (ssize_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t sys_sendto(
|
||||
int fd,
|
||||
const void *buffer,
|
||||
size_t size,
|
||||
int flags,
|
||||
const struct sockaddr *sock_addr,
|
||||
socklen_t addr_length,
|
||||
ssize_t *length
|
||||
) {
|
||||
auto r = menix_syscall(
|
||||
SYSCALL_SENDTO, fd, (size_t)buffer, size, flags, (size_t)sock_addr, addr_length
|
||||
);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*length = (ssize_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_msg_recv(int fd, struct msghdr *hdr, int flags, ssize_t *length) {
|
||||
auto r = menix_syscall(SYSCALL_RECVMSG, fd, (size_t)hdr, flags);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*length = (ssize_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t sys_recvfrom(
|
||||
int fd,
|
||||
void *buffer,
|
||||
size_t size,
|
||||
int flags,
|
||||
struct sockaddr *sock_addr,
|
||||
socklen_t *addr_length,
|
||||
ssize_t *length
|
||||
) {
|
||||
auto r = menix_syscall(
|
||||
SYSCALL_RECVFROM, fd, (size_t)buffer, size, flags, (size_t)sock_addr, (size_t)addr_length
|
||||
);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*length = (ssize_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_listen(int fd, int backlog) { return menix_syscall(SYSCALL_LISTEN, fd, backlog).error; }
|
||||
|
||||
gid_t sys_getgid() { return menix_syscall(SYSCALL_GETGID).value; }
|
||||
gid_t sys_getegid() { return menix_syscall(SYSCALL_GETEGID).value; }
|
||||
uid_t sys_getuid() { return menix_syscall(SYSCALL_GETUID).value; }
|
||||
uid_t sys_geteuid() { return menix_syscall(SYSCALL_GETEUID).value; }
|
||||
pid_t sys_getpid() { return menix_syscall(SYSCALL_GETPID).value; }
|
||||
pid_t sys_gettid() { return menix_syscall(SYSCALL_GETTID).value; }
|
||||
pid_t sys_getppid() { return menix_syscall(SYSCALL_GETPPID).value; }
|
||||
|
||||
int sys_getpgid(pid_t pid, pid_t *pgid) {
|
||||
auto r = menix_syscall(SYSCALL_GETPGID, pid);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*pgid = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getsid(pid_t pid, pid_t *sid) {
|
||||
auto r = menix_syscall(SYSCALL_GETSID, pid);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*sid = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setpgid(pid_t pid, pid_t pgid) { return menix_syscall(SYSCALL_SETPGID, pid, pgid).error; }
|
||||
int sys_setuid(uid_t uid) { return menix_syscall(SYSCALL_SETUID, uid).error; }
|
||||
int sys_seteuid(uid_t euid) { return menix_syscall(SYSCALL_SETEUID, euid).error; }
|
||||
int sys_setgid(gid_t gid) { return menix_syscall(SYSCALL_SETGID, gid).error; }
|
||||
int sys_setegid(gid_t egid) { return menix_syscall(SYSCALL_SETEGID, egid).error; }
|
||||
|
||||
int sys_getgroups(size_t size, gid_t *list, int *ret) {
|
||||
auto r = menix_syscall(SYSCALL_GETGROUPS, size, (size_t)list);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*ret = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setgroups(size_t size, const gid_t *list) {
|
||||
return menix_syscall(SYSCALL_SETGROUPS, size, (size_t)list).error;
|
||||
}
|
||||
|
||||
int sys_sleep(time_t *secs, long *nanos) {
|
||||
struct timespec req = {.tv_sec = *secs, .tv_nsec = *nanos};
|
||||
struct timespec rem = {};
|
||||
auto r = menix_syscall(SYSCALL_SLEEP, (size_t)&req, (size_t)&rem);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*secs = rem.tv_sec;
|
||||
*nanos = rem.tv_nsec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sys_yield() { menix_syscall(SYSCALL_YIELD); }
|
||||
|
||||
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;
|
||||
}
|
||||
again:
|
||||
auto r = menix_syscall(SYSCALL_WAITPID, pid, (size_t)status, flags);
|
||||
if (r.error) {
|
||||
if (r.error == EINTR)
|
||||
goto again;
|
||||
return r.error;
|
||||
}
|
||||
*ret_pid = (pid_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_fork(pid_t *child) {
|
||||
auto r = menix_syscall(SYSCALL_FORK);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*child = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
|
||||
return menix_syscall(SYSCALL_EXECVE, (size_t)path, (size_t)argv, (size_t)envp).error;
|
||||
}
|
||||
|
||||
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
|
||||
) {
|
||||
auto r = menix_syscall(
|
||||
SYSCALL_PSELECT,
|
||||
num_fds,
|
||||
(size_t)read_set,
|
||||
(size_t)write_set,
|
||||
(size_t)except_set,
|
||||
(size_t)timeout,
|
||||
(size_t)sigmask
|
||||
);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*num_events = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_getrusage(int scope, struct rusage *usage) {
|
||||
return menix_syscall(SYSCALL_GETRUSAGE, scope, (size_t)usage).error;
|
||||
}
|
||||
|
||||
int sys_getrlimit(int resource, struct rlimit *limit) {
|
||||
return menix_syscall(SYSCALL_GETRLIMIT, resource, (size_t)limit).error;
|
||||
}
|
||||
|
||||
int sys_setrlimit(int resource, const struct rlimit *limit) {
|
||||
return menix_syscall(SYSCALL_SETRLIMIT, resource, (size_t)limit).error;
|
||||
}
|
||||
|
||||
int sys_getpriority(int which, id_t who, int *value) {
|
||||
auto r = menix_syscall(SYSCALL_GETPRIORITY, which, who);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*value = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_setpriority(int which, id_t who, int prio) {
|
||||
return menix_syscall(SYSCALL_GETPRIORITY, which, who, prio).error;
|
||||
}
|
||||
|
||||
int sys_getparam(pid_t pid, struct sched_param *param) {
|
||||
return menix_syscall(SYSCALL_SCHED_GETPARAM, pid, (size_t)param).error;
|
||||
}
|
||||
|
||||
int sys_setparam(pid_t pid, const struct sched_param *param) {
|
||||
return menix_syscall(SYSCALL_SCHED_GETPARAM, pid, (size_t)param).error;
|
||||
}
|
||||
|
||||
int sys_getcwd(char *buffer, size_t size) {
|
||||
return menix_syscall(SYSCALL_GETCWD, (size_t)buffer, size).error;
|
||||
}
|
||||
|
||||
int sys_chdir(const char *path) { return menix_syscall(SYSCALL_CHDIR, (size_t)path).error; }
|
||||
|
||||
int sys_fchdir(int fd) { return menix_syscall(SYSCALL_FCHDIR, fd).error; };
|
||||
|
||||
int sys_chroot(const char *path) { return menix_syscall(SYSCALL_CHROOT, (size_t)path).error; }
|
||||
|
||||
int sys_mkdir(const char *path, mode_t mode) { return sys_mkdirat(AT_FDCWD, path, mode); }
|
||||
|
||||
int sys_mkdirat(int dirfd, const char *path, mode_t mode) {
|
||||
return menix_syscall(SYSCALL_MKDIRAT, dirfd, (size_t)path, mode).error;
|
||||
}
|
||||
|
||||
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_linkat(int olddirfd, const char *old_path, int newdirfd, const char *new_path, int flags) {
|
||||
return menix_syscall(
|
||||
SYSCALL_LINKAT, olddirfd, (size_t)old_path, newdirfd, (size_t)new_path, flags
|
||||
)
|
||||
.error;
|
||||
}
|
||||
|
||||
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) {
|
||||
return menix_syscall(SYSCALL_SYMLINKAT, (size_t)target_path, dirfd, (size_t)link_path).error;
|
||||
}
|
||||
|
||||
int sys_rename(const char *path, const char *new_path) {
|
||||
return sys_renameat(AT_FDCWD, path, AT_FDCWD, new_path);
|
||||
}
|
||||
|
||||
int sys_renameat(int olddirfd, const char *old_path, int newdirfd, const char *new_path) {
|
||||
return menix_syscall(SYSCALL_RENAMEAT, olddirfd, (size_t)old_path, newdirfd, (size_t)new_path)
|
||||
.error;
|
||||
}
|
||||
|
||||
int sys_fcntl(int fd, int request, va_list args, int *result) {
|
||||
auto r = menix_syscall(SYSCALL_FCNTL, fd, request, va_arg(args, size_t));
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*result = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_ttyname(int fd, char *buf, size_t size) {
|
||||
if (size >= NAME_MAX) {
|
||||
mlibc::panicLogger() << "ttyname size too small" << frg::endlog;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int res;
|
||||
return sys_ioctl(fd, TIOCGNAME, (void *)buf, &res);
|
||||
}
|
||||
|
||||
void sys_sync() { menix_syscall(SYSCALL_SYNC); }
|
||||
|
||||
int sys_fsync(int fd) { return menix_syscall(SYSCALL_FSYNC, fd).error; }
|
||||
|
||||
int sys_fdatasync(int fd) { return menix_syscall(SYSCALL_FDATASYNC, fd).error; }
|
||||
|
||||
int sys_chmod(const char *pathname, mode_t mode) {
|
||||
return sys_fchmodat(AT_FDCWD, pathname, mode, 0);
|
||||
}
|
||||
|
||||
int sys_fchmod(int fd, mode_t mode) { return menix_syscall(SYSCALL_FCHMOD, fd, mode).error; }
|
||||
|
||||
int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) {
|
||||
return menix_syscall(SYSCALL_FCHMOD, fd, (size_t)pathname, mode, flags).error;
|
||||
}
|
||||
|
||||
int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
|
||||
return menix_syscall(SYSCALL_UTIMENSAT, dirfd, (size_t)pathname, (size_t)times, flags).error;
|
||||
}
|
||||
|
||||
int sys_setsid(pid_t *sid) {
|
||||
auto r = menix_syscall(SYSCALL_SETSID);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*sid = (pid_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_tcgetattr(int fd, struct termios *attr) {
|
||||
int ret;
|
||||
return sys_ioctl(fd, TCGETS, attr, &ret);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int ret;
|
||||
return sys_ioctl(fd, req, (void *)attr, &ret);
|
||||
}
|
||||
|
||||
int sys_pipe(int *fds, int flags) { return menix_syscall(SYSCALL_PIPE, (size_t)fds, flags).error; }
|
||||
|
||||
int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) {
|
||||
auto r = menix_syscall(SYSCALL_SOCKETPAIR, domain, type_and_flags, proto);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*fds = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_ppoll(
|
||||
struct pollfd *fds,
|
||||
nfds_t count,
|
||||
const struct timespec *timeout,
|
||||
const sigset_t *sigmask,
|
||||
int *num_events
|
||||
) {
|
||||
auto r = menix_syscall(SYSCALL_PPOLL, (size_t)fds, count, (size_t)timeout, (size_t)sigmask);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*num_events = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
|
||||
struct timespec ts;
|
||||
ts.tv_sec = timeout / 1000;
|
||||
ts.tv_nsec = (timeout % 1000) * 1000000;
|
||||
|
||||
return sys_ppoll(fds, count, timeout != -1 ? &ts : NULL, NULL, num_events);
|
||||
}
|
||||
|
||||
int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
|
||||
auto r = menix_syscall(SYSCALL_IOCTL, fd, request, (size_t)arg);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
if (result)
|
||||
*result = r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sys_getsockopt(int fd, int layer, int number, void *__restrict buffer, socklen_t *__restrict size) {
|
||||
return menix_syscall(SYSCALL_GETSOCKOPT, fd, layer, number, (size_t)buffer, (size_t)size).error;
|
||||
}
|
||||
|
||||
int sys_setsockopt(int fd, int layer, int number, const void *buffer, socklen_t size) {
|
||||
return menix_syscall(SYSCALL_SETSOCKOPT, fd, layer, number, (size_t)buffer, size).error;
|
||||
}
|
||||
|
||||
int sys_shutdown(int sockfd, int how) { return menix_syscall(SYSCALL_SHUTDOWN, sockfd, how).error; }
|
||||
|
||||
int sys_sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict retrieve) {
|
||||
return menix_syscall(SYSCALL_SIGPROCMASK, how, (size_t)set, (size_t)retrieve).error;
|
||||
}
|
||||
|
||||
int
|
||||
sys_sigaction(int sig, const struct sigaction *__restrict act, struct sigaction *__restrict oact) {
|
||||
return menix_syscall(SYSCALL_SIGACTION, sig, (size_t)act, (size_t)oact).error;
|
||||
}
|
||||
|
||||
int sys_sigtimedwait(
|
||||
const sigset_t *__restrict set,
|
||||
siginfo_t *__restrict info,
|
||||
const struct timespec *__restrict timeout,
|
||||
int *out_signal
|
||||
) {
|
||||
auto r = menix_syscall(SYSCALL_SIGTIMEDWAIT, (size_t)set, (size_t)info, (size_t)timeout);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*out_signal = (int)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_kill(int pid, int signal) { return menix_syscall(SYSCALL_KILL, pid, signal).error; }
|
||||
|
||||
int sys_accept(int fd, int *newfd, struct sockaddr *addr_ptr, socklen_t *addr_length, int flags) {
|
||||
return menix_syscall(
|
||||
SYSCALL_ACCEPT, fd, (size_t)newfd, (size_t)addr_ptr, (size_t)addr_length, flags
|
||||
)
|
||||
.error;
|
||||
}
|
||||
|
||||
int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
|
||||
return menix_syscall(SYSCALL_BIND, fd, (size_t)addr_ptr, addr_length).error;
|
||||
}
|
||||
|
||||
int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
|
||||
return menix_syscall(SYSCALL_CONNECT, fd, (size_t)addr_ptr, addr_length).error;
|
||||
}
|
||||
|
||||
int sys_sockname(
|
||||
int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length
|
||||
) {
|
||||
auto r = menix_syscall(SYSCALL_GETSOCKNAME, fd, (size_t)addr_ptr, (size_t)&max_addr_length);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*actual_length = max_addr_length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_peername(
|
||||
int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length
|
||||
) {
|
||||
auto r = menix_syscall(SYSCALL_GETPEERNAME, fd, (size_t)addr_ptr, (size_t)&max_addr_length);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*actual_length = (socklen_t)max_addr_length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_gethostname(char *buffer, size_t bufsize) {
|
||||
struct utsname name;
|
||||
int i = sys_uname(&name);
|
||||
if (i)
|
||||
return i;
|
||||
if (bufsize >= sizeof(name.nodename))
|
||||
bufsize = sizeof(name.nodename) - 1;
|
||||
memcpy(buffer, name.nodename, bufsize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_sethostname(const char *buffer, size_t bufsize) {
|
||||
struct utsname name = {};
|
||||
if (bufsize >= sizeof(name.nodename))
|
||||
bufsize = sizeof(name.nodename) - 1;
|
||||
memcpy(name.nodename, buffer, bufsize);
|
||||
return menix_syscall(SYSCALL_SETUNAME, (size_t)&name).error;
|
||||
}
|
||||
|
||||
int sys_mkfifoat(int dirfd, const char *path, mode_t mode) {
|
||||
return sys_mknodat(dirfd, path, S_IFIFO | mode, 0);
|
||||
}
|
||||
|
||||
int sys_getentropy(void *buffer, size_t length) {
|
||||
return menix_syscall(SYSCALL_GETENTROPY, (size_t)buffer, length).error;
|
||||
}
|
||||
|
||||
int sys_mknodat(int dirfd, const char *path, int mode, int dev) {
|
||||
return menix_syscall(SYSCALL_MKNODAT, dirfd, (size_t)path, mode, dev).error;
|
||||
}
|
||||
|
||||
int sys_umask(mode_t mode, mode_t *old) {
|
||||
auto r = menix_syscall(SYSCALL_UMASK, mode);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*old = (mode_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_tgkill(int pid, int tid, int sig) {
|
||||
return menix_syscall(SYSCALL_THREAD_KILL, pid, tid, sig).error;
|
||||
}
|
||||
|
||||
int sys_fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) {
|
||||
return menix_syscall(SYSCALL_FCHOWNAT, dirfd, (size_t)pathname, owner, group, flags).error;
|
||||
}
|
||||
|
||||
int sys_sigaltstack(const stack_t *ss, stack_t *oss) {
|
||||
return menix_syscall(SYSCALL_SIGALTSTACK, (size_t)ss, (size_t)oss).error;
|
||||
}
|
||||
|
||||
int sys_sigsuspend(const sigset_t *set) {
|
||||
menix_syscall(SYSCALL_SIGSUSPEND, (size_t)set);
|
||||
return EINTR;
|
||||
}
|
||||
|
||||
int sys_sigpending(sigset_t *set) { return menix_syscall(SYSCALL_SIGPENDING, (size_t)set).error; }
|
||||
|
||||
int sys_madvise(void *addr, size_t length, int advice) {
|
||||
return menix_syscall(SYSCALL_MADVISE, (size_t)addr, length, advice).error;
|
||||
}
|
||||
|
||||
int sys_getitimer(int which, struct itimerval *curr_value) {
|
||||
return menix_syscall(SYSCALL_ITIMER_GET, which, (size_t)curr_value).error;
|
||||
}
|
||||
|
||||
int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) {
|
||||
return menix_syscall(SYSCALL_ITIMER_SET, which, (size_t)new_value, (size_t)old_value).error;
|
||||
}
|
||||
|
||||
int sys_timer_create(clockid_t clk, struct sigevent *__restrict evp, timer_t *__restrict res) {
|
||||
return menix_syscall(SYSCALL_TIMER_CREATE, (size_t)clk, (size_t)evp, (size_t)res).error;
|
||||
}
|
||||
|
||||
int sys_timer_settime(
|
||||
timer_t t, int flags, const struct itimerspec *__restrict val, struct itimerspec *__restrict old
|
||||
) {
|
||||
return menix_syscall(SYSCALL_TIMER_SET, (size_t)t, flags, (size_t)val, (size_t)old).error;
|
||||
}
|
||||
|
||||
int sys_timer_delete(timer_t t) { return menix_syscall(SYSCALL_TIMER_DELETE, (size_t)t).error; }
|
||||
|
||||
int sys_uname(struct utsname *buf) {
|
||||
auto r = menix_syscall(SYSCALL_GETUNAME, (size_t)buf);
|
||||
return r.error;
|
||||
}
|
||||
|
||||
int sys_setresuid(uid_t ruid, uid_t euid, uid_t suid) {
|
||||
return menix_syscall(SYSCALL_SETRESUID, ruid, euid, suid).error;
|
||||
}
|
||||
|
||||
int sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid) {
|
||||
return menix_syscall(SYSCALL_SETRESGID, rgid, egid, sgid).error;
|
||||
}
|
||||
|
||||
int sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid) {
|
||||
return menix_syscall(SYSCALL_GETRESUID, (size_t)ruid, (size_t)euid, (size_t)suid).error;
|
||||
}
|
||||
|
||||
int sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid) {
|
||||
return menix_syscall(SYSCALL_GETRESGID, (size_t)rgid, (size_t)egid, (size_t)sgid).error;
|
||||
}
|
||||
|
||||
int sys_setreuid(uid_t ruid, uid_t euid) {
|
||||
return menix_syscall(SYSCALL_SETREUID, ruid, euid).error;
|
||||
}
|
||||
|
||||
int sys_setregid(gid_t rgid, gid_t egid) {
|
||||
return menix_syscall(SYSCALL_SETREGID, rgid, egid).error;
|
||||
}
|
||||
|
||||
int sys_unlockpt(int fd) {
|
||||
int unlock = 0;
|
||||
if (int e = sys_ioctl(fd, TIOCSPTLCK, &unlock, NULL); e)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_thread_getname(void *tcb, char *name, size_t size) {
|
||||
auto t = reinterpret_cast<Tcb *>(tcb);
|
||||
return menix_syscall(SYSCALL_THREAD_GETNAME, t->tid, (size_t)name, size).error;
|
||||
}
|
||||
|
||||
int sys_thread_setname(void *tcb, const char *name) {
|
||||
auto t = reinterpret_cast<Tcb *>(tcb);
|
||||
return menix_syscall(SYSCALL_THREAD_SETNAME, t->tid, (size_t)name).error;
|
||||
}
|
||||
|
||||
int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
|
||||
return menix_syscall(SYSCALL_WAITID, idtype, id, (size_t)info, options).error;
|
||||
}
|
||||
|
||||
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_if_indextoname(unsigned int index, char *name) {
|
||||
int fd = 0;
|
||||
int r = sys_socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd);
|
||||
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
struct ifreq ifr;
|
||||
ifr.ifr_ifindex = index;
|
||||
|
||||
int ret = sys_ioctl(fd, SIOCGIFNAME, &ifr, NULL);
|
||||
close(fd);
|
||||
|
||||
if (ret) {
|
||||
if (ret == ENODEV)
|
||||
return ENXIO;
|
||||
return ret;
|
||||
}
|
||||
|
||||
strncpy(name, ifr.ifr_name, IF_NAMESIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_if_nametoindex(const char *name, unsigned int *ret) {
|
||||
int fd = 0;
|
||||
int r = sys_socket(AF_UNIX, 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,80 @@
|
||||
#include <errno.h>
|
||||
#include <menix/syscall.hpp>
|
||||
#include <mlibc/ansi-sysdeps.hpp>
|
||||
#include <mlibc/arch-defs.hpp>
|
||||
#include <mlibc/tcb.hpp>
|
||||
#include <sys/mman.h>
|
||||
|
||||
extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) {
|
||||
while (__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED) == 0) {
|
||||
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();
|
||||
}
|
||||
|
||||
// Defined in <arch>/thread.S
|
||||
extern "C" void __mlibc_start_thread();
|
||||
|
||||
#define DEFAULT_STACK 0x200000
|
||||
|
||||
namespace mlibc {
|
||||
|
||||
int sys_clone(void *tcb, pid_t *pid_out, void *stack) {
|
||||
(void)tcb;
|
||||
auto r = menix_syscall(SYSCALL_THREAD_CREATE, (size_t)__mlibc_start_thread, (size_t)stack);
|
||||
if (r.error)
|
||||
return r.error;
|
||||
*pid_out = (pid_t)r.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sys_prepare_stack(
|
||||
void **stack,
|
||||
void *entry,
|
||||
void *arg,
|
||||
void *tcb,
|
||||
size_t *stack_size,
|
||||
size_t *guard_size,
|
||||
void **stack_base
|
||||
) {
|
||||
*guard_size = mlibc::page_size;
|
||||
|
||||
*stack_size = *stack_size ? *stack_size : DEFAULT_STACK;
|
||||
|
||||
if (!*stack) {
|
||||
*stack_base = mmap(
|
||||
NULL,
|
||||
*stack_size + mlibc::page_size,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE,
|
||||
-1,
|
||||
0
|
||||
);
|
||||
if (*stack_base == MAP_FAILED) {
|
||||
return errno;
|
||||
}
|
||||
munmap((char *)*stack_base + *stack_size, mlibc::page_size);
|
||||
} else {
|
||||
*stack_base = *stack;
|
||||
}
|
||||
|
||||
*stack = (void *)((char *)*stack_base + *stack_size);
|
||||
|
||||
void **stack_it = (void **)*stack;
|
||||
|
||||
*--stack_it = tcb; // a2
|
||||
*--stack_it = arg; // a1
|
||||
*--stack_it = entry; // a0
|
||||
|
||||
*stack = (void *)stack_it;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // 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/menix/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/linux/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
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/random.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/menix/reboot.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/resource.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/riscv-hwprobe.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/rlim_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/seek-whence.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/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/linux/socket.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/socklen_t.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/menix/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/linux/time.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/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/linux/vm-flags.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/vt.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/wait.h
|
||||
@@ -0,0 +1 @@
|
||||
../../../../abis/linux/xattr.h
|
||||
@@ -0,0 +1,94 @@
|
||||
#ifndef _ASM_IOCTLS_H
|
||||
#define _ASM_IOCTLS_H
|
||||
|
||||
#define RTC_RD_TIME 1
|
||||
#define RTC_SET_TIME 2
|
||||
#define FIOQSIZE 0x5460
|
||||
#define TCGETS 0x5401
|
||||
#define TCSETS 0x5402
|
||||
#define TCSETSW 0x5403
|
||||
#define TCSETSF 0x5404
|
||||
#define TCGETA 0x5405
|
||||
#define TCSETA 0x5406
|
||||
#define TCSETAW 0x5407
|
||||
#define TCSETAF 0x5408
|
||||
#define TCSBRK 0x5409
|
||||
#define TCXONC 0x540A
|
||||
#define TCFLSH 0x540B
|
||||
#define TIOCEXCL 0x540C
|
||||
#define TIOCNXCL 0x540D
|
||||
#define TIOCSCTTY 0x540E
|
||||
#define TIOCGPGRP 0x540F
|
||||
#define TIOCSPGRP 0x5410
|
||||
#define TIOCOUTQ 0x5411
|
||||
#define TIOCSTI 0x5412
|
||||
#define TIOCGWINSZ 0x5413
|
||||
#define TIOCSWINSZ 0x5414
|
||||
#define TIOCMGET 0x5415
|
||||
#define TIOCMBIS 0x5416
|
||||
#define TIOCMBIC 0x5417
|
||||
#define TIOCMSET 0x5418
|
||||
#define TIOCGSOFTCAR 0x5419
|
||||
#define TIOCSSOFTCAR 0x541A
|
||||
#define FIONREAD 0x541B
|
||||
#define TIOCINQ FIONREAD
|
||||
#define TIOCLINUX 0x541C
|
||||
#define TIOCCONS 0x541D
|
||||
#define TIOCGSERIAL 0x541E
|
||||
#define TIOCSSERIAL 0x541F
|
||||
#define TIOCPKT 0x5420
|
||||
#define FIONBIO 0x5421
|
||||
#define TIOCNOTTY 0x5422
|
||||
#define TIOCSETD 0x5423
|
||||
#define TIOCGETD 0x5424
|
||||
#define TCSBRKP 0x5425
|
||||
#define TIOCSBRK 0x5427
|
||||
#define TIOCCBRK 0x5428
|
||||
#define TIOCGSID 0x5429
|
||||
#define TIOCGNAME 0x5470
|
||||
#define TCGETS2 3
|
||||
#define TCSETS2 3
|
||||
#define TCSETSW2 3
|
||||
#define TCSETSF2 3
|
||||
#define TIOCGRS485 0x542E
|
||||
#define TIOCSRS485 0x542F
|
||||
#define TIOCGPTN 3
|
||||
#define TIOCSPTLCK 3
|
||||
#define TIOCGDEV 3
|
||||
#define TCGETX 0x5432
|
||||
#define TCSETX 0x5433
|
||||
#define TCSETXF 0x5434
|
||||
#define TCSETXW 0x5435
|
||||
#define TIOCSIG 0x36
|
||||
#define TIOCVHANGUP 0x5437
|
||||
#define TIOCGPKT 3
|
||||
#define TIOCGPTLCK 3
|
||||
#define TIOCGEXCL 3
|
||||
#define TIOCGPTPEER 3
|
||||
#define TIOCGISO7816 3
|
||||
#define TIOCSISO7816 3
|
||||
#define FIONCLEX 0x5450
|
||||
#define FIOCLEX 0x5451
|
||||
#define FIOASYNC 0x5452
|
||||
#define TIOCSERCONFIG 0x5453
|
||||
#define TIOCSERGWILD 0x5454
|
||||
#define TIOCSERSWILD 0x5455
|
||||
#define TIOCGLCKTRMIOS 0x5456
|
||||
#define TIOCSLCKTRMIOS 0x5457
|
||||
#define TIOCSERGSTRUCT 0x5458
|
||||
#define TIOCSERGETLSR 0x5459
|
||||
#define TIOCSERGETMULTI 0x545A
|
||||
#define TIOCSERSETMULTI 0x545B
|
||||
#define TIOCMIWAIT 0x545C
|
||||
#define TIOCGICOUNT 0x545D
|
||||
#define TIOCPKT_DATA 0
|
||||
#define TIOCPKT_FLUSHREAD 1
|
||||
#define TIOCPKT_FLUSHWRITE 2
|
||||
#define TIOCPKT_STOP 4
|
||||
#define TIOCPKT_START 8
|
||||
#define TIOCPKT_NOSTOP 16
|
||||
#define TIOCPKT_DOSTOP 32
|
||||
#define TIOCPKT_IOCTL 64
|
||||
#define TIOCSER_TEMT 0x01
|
||||
|
||||
#endif /* _ASM_IOCTLS_H */
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef _MENIX_ARCHCTL_HPP
|
||||
#define _MENIX_ARCHCTL_HPP
|
||||
|
||||
#if defined(__x86_64__)
|
||||
#define ARCHCTL_SET_FSBASE 0
|
||||
#endif
|
||||
|
||||
#endif /* _MENIX_ARCHCTL_HPP */
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _MENIX_POWER_HPP
|
||||
#define _MENIX_POWER_HPP
|
||||
|
||||
#define MENIX_REBOOT_MAGIC1 0xDEADBEEF
|
||||
#define MENIX_REBOOT_MAGIC2 0x1F2E3D4C
|
||||
|
||||
#endif /* _MENIX_POWER_HPP */
|
||||
@@ -0,0 +1,225 @@
|
||||
#ifndef _MENIX_SYSCALL_HPP
|
||||
#define _MENIX_SYSCALL_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define SYSCALL_EXIT 0
|
||||
#define SYSCALL_SYSLOG 1
|
||||
#define SYSCALL_GETUNAME 2
|
||||
#define SYSCALL_SETUNAME 3
|
||||
#define SYSCALL_ARCHCTL 4
|
||||
#define SYSCALL_REBOOT 5
|
||||
#define SYSCALL_MMAP 6
|
||||
#define SYSCALL_MUNMAP 7
|
||||
#define SYSCALL_MPROTECT 8
|
||||
#define SYSCALL_MADVISE 9
|
||||
#define SYSCALL_SIGPROCMASK 10
|
||||
#define SYSCALL_SIGSUSPEND 11
|
||||
#define SYSCALL_SIGPENDING 12
|
||||
#define SYSCALL_SIGACTION 13
|
||||
#define SYSCALL_SIGTIMEDWAIT 14
|
||||
#define SYSCALL_SIGALTSTACK 15
|
||||
#define SYSCALL_EXECVE 16
|
||||
#define SYSCALL_FORK 17
|
||||
#define SYSCALL_KILL 18
|
||||
#define SYSCALL_GETTID 19
|
||||
#define SYSCALL_GETPID 20
|
||||
#define SYSCALL_GETPPID 21
|
||||
#define SYSCALL_WAITID 22
|
||||
#define SYSCALL_WAITPID 23
|
||||
#define SYSCALL_READ 24
|
||||
#define SYSCALL_PREAD 25
|
||||
#define SYSCALL_WRITE 26
|
||||
#define SYSCALL_PWRITE 27
|
||||
#define SYSCALL_SEEK 28
|
||||
#define SYSCALL_IOCTL 29
|
||||
#define SYSCALL_OPENAT 30
|
||||
#define SYSCALL_CLOSE 31
|
||||
#define SYSCALL_FSTAT 32
|
||||
#define SYSCALL_FSTATAT 33
|
||||
#define SYSCALL_STATVFS 34
|
||||
#define SYSCALL_FSTATVFS 35
|
||||
#define SYSCALL_FACCESSAT 36
|
||||
#define SYSCALL_FCNTL 37
|
||||
#define SYSCALL_FTRUNCATE 38
|
||||
#define SYSCALL_FALLOCATE 39
|
||||
#define SYSCALL_UTIMENSAT 40
|
||||
#define SYSCALL_PSELECT 41
|
||||
#define SYSCALL_MKNODAT 42
|
||||
#define SYSCALL_READDIR 43
|
||||
#define SYSCALL_GETCWD 44
|
||||
#define SYSCALL_CHDIR 45
|
||||
#define SYSCALL_FCHDIR 46
|
||||
#define SYSCALL_MKDIRAT 47
|
||||
#define SYSCALL_RMDIRAT 48
|
||||
#define SYSCALL_GETDENTS 49
|
||||
#define SYSCALL_RENAMEAT 50
|
||||
#define SYSCALL_FCHMOD 51
|
||||
#define SYSCALL_FCHMODAT 52
|
||||
#define SYSCALL_FCHOWNAT 53
|
||||
#define SYSCALL_LINKAT 54
|
||||
#define SYSCALL_SYMLINKAT 55
|
||||
#define SYSCALL_UNLINKAT 56
|
||||
#define SYSCALL_READLINKAT 57
|
||||
#define SYSCALL_FLOCK 58
|
||||
#define SYSCALL_PPOLL 59
|
||||
#define SYSCALL_DUP 60
|
||||
#define SYSCALL_DUP3 61
|
||||
#define SYSCALL_SYNC 62
|
||||
#define SYSCALL_FSYNC 63
|
||||
#define SYSCALL_FDATASYNC 64
|
||||
#define SYSCALL_GETGROUPS 65
|
||||
#define SYSCALL_SETGROUPS 66
|
||||
#define SYSCALL_GETSID 67
|
||||
#define SYSCALL_SETSID 68
|
||||
#define SYSCALL_SETUID 69
|
||||
#define SYSCALL_GETUID 70
|
||||
#define SYSCALL_SETGID 71
|
||||
#define SYSCALL_GETGID 72
|
||||
#define SYSCALL_GETEUID 73
|
||||
#define SYSCALL_SETEUID 74
|
||||
#define SYSCALL_GETEGID 75
|
||||
#define SYSCALL_SETEGID 76
|
||||
#define SYSCALL_GETPGID 77
|
||||
#define SYSCALL_SETPGID 78
|
||||
#define SYSCALL_GETRESUID 79
|
||||
#define SYSCALL_SETRESUID 80
|
||||
#define SYSCALL_GETRESGID 81
|
||||
#define SYSCALL_SETRESGID 82
|
||||
#define SYSCALL_SETREUID 83
|
||||
#define SYSCALL_SETREGID 84
|
||||
#define SYSCALL_UMASK 85
|
||||
#define SYSCALL_PIPE 86
|
||||
#define SYSCALL_FUTEX_WAIT 87
|
||||
#define SYSCALL_FUTEX_WAKE 88
|
||||
#define SYSCALL_THREAD_CREATE 89
|
||||
#define SYSCALL_THREAD_KILL 90
|
||||
#define SYSCALL_THREAD_EXIT 91
|
||||
#define SYSCALL_THREAD_SETNAME 92
|
||||
#define SYSCALL_THREAD_GETNAME 93
|
||||
#define SYSCALL_TIMER_CREATE 94
|
||||
#define SYSCALL_TIMER_SET 95
|
||||
#define SYSCALL_TIMER_DELETE 96
|
||||
#define SYSCALL_ITIMER_GET 97
|
||||
#define SYSCALL_ITIMER_SET 98
|
||||
#define SYSCALL_CLOCK_GET 99
|
||||
#define SYSCALL_CLOCK_GETRES 100
|
||||
#define SYSCALL_SLEEP 101
|
||||
#define SYSCALL_YIELD 102
|
||||
#define SYSCALL_CHROOT 103
|
||||
#define SYSCALL_MOUNT 104
|
||||
#define SYSCALL_UMOUNT 105
|
||||
#define SYSCALL_SWAPON 106
|
||||
#define SYSCALL_SWAPOFF 107
|
||||
#define SYSCALL_SOCKET 108
|
||||
#define SYSCALL_SOCKETPAIR 109
|
||||
#define SYSCALL_SHUTDOWN 110
|
||||
#define SYSCALL_BIND 111
|
||||
#define SYSCALL_CONNECT 112
|
||||
#define SYSCALL_ACCEPT 113
|
||||
#define SYSCALL_LISTEN 114
|
||||
#define SYSCALL_GETPEERNAME 115
|
||||
#define SYSCALL_GETSOCKNAME 116
|
||||
#define SYSCALL_GETSOCKOPT 117
|
||||
#define SYSCALL_SETSOCKOPT 118
|
||||
#define SYSCALL_SENDMSG 119
|
||||
#define SYSCALL_SENDTO 120
|
||||
#define SYSCALL_RECVMSG 121
|
||||
#define SYSCALL_RECVFROM 122
|
||||
#define SYSCALL_GETENTROPY 123
|
||||
#define SYSCALL_GETRUSAGE 124
|
||||
#define SYSCALL_GETRLIMIT 125
|
||||
#define SYSCALL_SETRLIMIT 126
|
||||
#define SYSCALL_GETPRIORITY 127
|
||||
#define SYSCALL_SETPRIORITY 128
|
||||
#define SYSCALL_SCHED_GETPARAM 129
|
||||
#define SYSCALL_SCHED_SETPARAM 130
|
||||
#define SYSCALL_GETCPU 131
|
||||
#define SYSCALL_SYSINFO 132
|
||||
#define SYSCALL_PTRACE 133
|
||||
|
||||
struct syscall_result {
|
||||
size_t value;
|
||||
size_t error;
|
||||
};
|
||||
static_assert(sizeof(syscall_result) == 16);
|
||||
|
||||
#ifndef __MLIBC_ABI_ONLY
|
||||
|
||||
[[gnu::always_inline]]
|
||||
inline syscall_result menix_syscall(
|
||||
size_t num,
|
||||
size_t a0 = 0,
|
||||
size_t a1 = 0,
|
||||
size_t a2 = 0,
|
||||
size_t a3 = 0,
|
||||
size_t a4 = 0,
|
||||
size_t a5 = 0
|
||||
) {
|
||||
syscall_result r;
|
||||
#if defined(__x86_64__)
|
||||
register size_t r3 asm("r10") = a3;
|
||||
register size_t r4 asm("r8") = a4;
|
||||
register size_t r5 asm("r9") = a5;
|
||||
|
||||
asm volatile("syscall"
|
||||
: "=a"(r.value), "=d"(r.error)
|
||||
: "a"(num), "D"(a0), "S"(a1), "d"(a2), "r"(r3), "r"(r4), "r"(r5)
|
||||
: "memory", "rcx", "r11");
|
||||
#elif defined(__aarch64__)
|
||||
register size_t snum asm("x8") = num;
|
||||
register size_t value asm("x0");
|
||||
register size_t error asm("x1");
|
||||
register size_t r0 asm("x0") = a0;
|
||||
register size_t r1 asm("x1") = a1;
|
||||
register size_t r2 asm("x2") = a2;
|
||||
register size_t r3 asm("x3") = a3;
|
||||
register size_t r4 asm("x4") = a4;
|
||||
register size_t r5 asm("x5") = a5;
|
||||
asm volatile("svc 0"
|
||||
: "=r"(value), "=r"(error)
|
||||
: "r"(snum), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)
|
||||
: "memory");
|
||||
r.value = value;
|
||||
r.error = error;
|
||||
#elif defined(__riscv) && (__riscv_xlen == 64)
|
||||
register size_t snum asm("a7") = num;
|
||||
register size_t value asm("a0");
|
||||
register size_t error asm("a1");
|
||||
register size_t r0 asm("a0") = a0;
|
||||
register size_t r1 asm("a1") = a1;
|
||||
register size_t r2 asm("a2") = a2;
|
||||
register size_t r3 asm("a3") = a3;
|
||||
register size_t r4 asm("a4") = a4;
|
||||
register size_t r5 asm("a5") = a5;
|
||||
asm volatile("ecall"
|
||||
: "=r"(value), "=r"(error)
|
||||
: "r"(snum), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)
|
||||
: "memory");
|
||||
r.value = value;
|
||||
r.error = error;
|
||||
#elif defined(__loongarch64)
|
||||
register size_t snum asm("a7") = num;
|
||||
register size_t value asm("a0");
|
||||
register size_t error asm("a1");
|
||||
register size_t r0 asm("a0") = a0;
|
||||
register size_t r1 asm("a1") = a1;
|
||||
register size_t r2 asm("a2") = a2;
|
||||
register size_t r3 asm("a3") = a3;
|
||||
register size_t r4 asm("a4") = a4;
|
||||
register size_t r5 asm("a5") = a5;
|
||||
asm volatile("syscall 0"
|
||||
: "=r"(value), "=r"(error)
|
||||
: "r"(snum), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)
|
||||
: "memory");
|
||||
r.value = value;
|
||||
r.error = error;
|
||||
#else
|
||||
#error "Unsupported architecture!"
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
#endif /* !__MLIBC_ABI_ONLY */
|
||||
|
||||
#endif /* _MENIX_SYSCALL_HPP */
|
||||
@@ -0,0 +1,10 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
move $fp, $zero
|
||||
move $a0, $sp
|
||||
la $a1, main
|
||||
b %plt(__mlibc_entry)
|
||||
break 0
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,10 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
move $fp, $zero
|
||||
move $a0, $sp
|
||||
la $a1, main
|
||||
b __mlibc_entry
|
||||
break 0
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,13 @@
|
||||
.section .init
|
||||
.global _init
|
||||
_init:
|
||||
addi.d $sp, $sp, -16
|
||||
st.d $ra, $sp, 8
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
_fini:
|
||||
addi.d $sp, $sp, -16
|
||||
st.d $ra, $sp, 8
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .init
|
||||
ld.d $ra, $sp, 8
|
||||
addi.d $sp, $sp, 16
|
||||
|
||||
.section .fini
|
||||
ld.d $ra, $sp, 8
|
||||
addi.d $sp, $sp, 16
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,12 @@
|
||||
.section .text
|
||||
.global __mlibc_start_thread
|
||||
__mlibc_start_thread:
|
||||
ld.d $a0, $sp, 0
|
||||
ld.d $a1, $sp, 8
|
||||
ld.d $a2, $sp, 16
|
||||
addi.d $sp, $sp, 16
|
||||
bstrins.d $sp, $sp, 3, 0
|
||||
b __mlibc_enter_thread
|
||||
break 0
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,97 @@
|
||||
sysdep_supported_options = {
|
||||
'posix': true,
|
||||
'linux': true,
|
||||
'glibc': true,
|
||||
'bsd': true,
|
||||
}
|
||||
|
||||
rtld_sources += files(
|
||||
'generic/internal.cpp',
|
||||
)
|
||||
|
||||
libc_sources += files(
|
||||
'generic/entry.cpp',
|
||||
'generic/internal.cpp',
|
||||
'generic/posix.cpp',
|
||||
'generic/thread.cpp',
|
||||
'generic/linux.cpp',
|
||||
host_machine.cpu_family() / 'thread.S',
|
||||
)
|
||||
|
||||
if not no_headers
|
||||
install_headers(
|
||||
'include/abi-bits/auxv.h',
|
||||
'include/abi-bits/seek-whence.h',
|
||||
'include/abi-bits/vm-flags.h',
|
||||
'include/abi-bits/errno.h',
|
||||
'include/abi-bits/fcntl.h',
|
||||
'include/abi-bits/in.h',
|
||||
'include/abi-bits/stat.h',
|
||||
'include/abi-bits/statx.h',
|
||||
'include/abi-bits/signal.h',
|
||||
'include/abi-bits/reboot.h',
|
||||
'include/abi-bits/resource.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/vt.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',
|
||||
'include/abi-bits/riscv-hwprobe.h',
|
||||
subdir: 'abi-bits',
|
||||
follow_symlinks: true,
|
||||
)
|
||||
endif
|
||||
|
||||
if not headers_only
|
||||
crtstuff = ['crt1', 'Scrt1', 'crti', 'crtn']
|
||||
foreach crtthing : crtstuff
|
||||
crtf = crtthing + '.S'
|
||||
crt_src = files(host_machine.cpu_family() / crtf)
|
||||
crt = custom_target(
|
||||
crtthing,
|
||||
build_by_default: true,
|
||||
command: c_compiler.cmd_array() + ['-c', '-o', '@OUTPUT@', '@INPUT@'],
|
||||
input: crt_src,
|
||||
output: crtthing + '.o',
|
||||
install: true,
|
||||
install_dir: get_option('libdir'),
|
||||
)
|
||||
endforeach
|
||||
endif
|
||||
@@ -0,0 +1,30 @@
|
||||
.weak __global_pointer$
|
||||
.hidden __global_pointer$
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
# Load gp.
|
||||
.option push
|
||||
.option norelax
|
||||
lla gp, __global_pointer$
|
||||
.option pop
|
||||
|
||||
mv a0, sp
|
||||
la a1, main
|
||||
call __mlibc_entry@plt
|
||||
unimp
|
||||
|
||||
# Load gp from .preinit_array since it may be used by the executable's .init_array.
|
||||
# We still load it in _start to account for static binaries. This matches glibc's behavior.
|
||||
load_gp:
|
||||
.option push
|
||||
.option norelax
|
||||
lla gp, __global_pointer$
|
||||
.option pop
|
||||
ret
|
||||
|
||||
.section .preinit_array,"aw"
|
||||
.dword load_gp
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,30 @@
|
||||
.weak __global_pointer$
|
||||
.hidden __global_pointer$
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
# Load gp.
|
||||
.option push
|
||||
.option norelax
|
||||
lla gp, __global_pointer$
|
||||
.option pop
|
||||
|
||||
mv a0, sp
|
||||
la a1, main
|
||||
call __mlibc_entry
|
||||
unimp
|
||||
|
||||
# Load gp from .preinit_array since it may be used by the executable's .init_array.
|
||||
# We still load it in _start to account for static binaries. This matches glibc's behavior.
|
||||
load_gp:
|
||||
.option push
|
||||
.option norelax
|
||||
lla gp, __global_pointer$
|
||||
.option pop
|
||||
ret
|
||||
|
||||
.section .preinit_array,"aw"
|
||||
.dword load_gp
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,14 @@
|
||||
.section .init
|
||||
.global _init
|
||||
_init:
|
||||
addi sp, sp, -16
|
||||
sd ra, 8(sp)
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
.type _fini, @function
|
||||
_fini:
|
||||
addi sp, sp, -16
|
||||
sd ra, 8(sp)
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,9 @@
|
||||
.section .init
|
||||
ld ra, 8(sp)
|
||||
addi sp, sp, 16
|
||||
|
||||
.section .fini
|
||||
ld ra, 8(sp)
|
||||
addi sp, sp, 16
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,12 @@
|
||||
.section .text
|
||||
.global __mlibc_start_thread
|
||||
__mlibc_start_thread:
|
||||
ld a0, 0(sp)
|
||||
ld a1, 8(sp)
|
||||
ld a2, 16(sp)
|
||||
addi sp, sp, 16
|
||||
andi sp, sp, -16
|
||||
call __mlibc_enter_thread
|
||||
unimp
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,8 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov %rsp, %rdi
|
||||
mov main@GOTPCREL(%rip), %rsi
|
||||
call *__mlibc_entry@GOTPCREL(%rip)
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,8 @@
|
||||
.section .text
|
||||
.global _start
|
||||
_start:
|
||||
mov %rsp, %rdi
|
||||
mov $main, %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,9 @@
|
||||
.section .text
|
||||
.global __mlibc_start_thread
|
||||
__mlibc_start_thread:
|
||||
pop %rdi
|
||||
pop %rsi
|
||||
pop %rdx
|
||||
call __mlibc_enter_thread
|
||||
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
Reference in New Issue
Block a user