sysdeps/squiid: Add initial sysdeps implementation

This commit is contained in:
Christoph Heiss 2021-01-04 00:13:04 +01:00 committed by Christoph Heiss
parent a9df70b558
commit 6bc1b760a3
Signed by: c8h4
GPG key ID: BDF5332444F0D447
32 changed files with 554 additions and 0 deletions

View file

@ -0,0 +1,6 @@
.section .text
.global _start
_start:
movq $main, %rdi
callq __mlibc_entry

View file

@ -0,0 +1,31 @@
#include <stdint.h>
#include <stdlib.h>
#include <mlibc/elf/startup.h>
// defined by the POSIX library
void __mlibc_initLocale();
extern "C" uintptr_t *__dlapi_entrystack();
extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;
struct LibraryGuard {
LibraryGuard();
};
static LibraryGuard guard;
LibraryGuard::LibraryGuard() {
__mlibc_initLocale();
// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}
extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[])) {
int result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
exit(result);
}

View file

@ -0,0 +1,231 @@
#include <mlibc/all-sysdeps.hpp>
#include <string.h>
#include <bits/ensure.h>
#include <unistd.h>
#include <squiid/syscall_wrappers_x86_64.h>
#include <squiid/mmap.h>
#include <squiid/process.h>
#include <squiid/stat.h>
#define STUB_ONLY { __ensure(!"STUB_ONLY function was called"); __builtin_unreachable(); }
namespace mlibc {
void sys_libc_log(const char *msg) {
ssize_t written;
sys_write(STDERR_FILENO, msg, strlen(msg), &written);
sys_write(STDERR_FILENO, "\n", 1, &written);
}
void sys_libc_panic() {
sys_libc_log("mlibc panic!\n");
__builtin_trap();
}
int sys_tcb_set(void *pointer) {
return __sq_syscall2(SQ_SYS_prctl, SQ_PR_ARCH_X86_64_SET_FS, (__sq_u64)pointer);
}
int sys_futex_wait(int *pointer, int expected) {
return __sq_syscall3(SQ_SYS_futex, SQ_FUTEX_WAIT, (__sq_u64)pointer, expected);
}
int sys_futex_wake(int *pointer) {
return __sq_syscall3(SQ_SYS_futex, SQ_FUTEX_WAKE, (__sq_u64)pointer, 0);
}
int sys_close(int fd) {
return __sq_syscall1(SQ_SYS_close, fd);
}
int sys_open(const char *path, int flags, int *fd) {
/* TODO: convert flags */
int ret = __sq_syscall3(SQ_SYS_open, (__sq_u64)path, flags, 0);
if (ret < 0) {
return ret;
}
*fd = ret;
return 0;
}
int sys_read(int fd, void *buffer, size_t size, ssize_t *bytes_read) {
ssize_t ret = __sq_syscall3(SQ_SYS_read, fd, (__sq_u64)buffer, size);
if (ret < 0) {
return ret;
}
*bytes_read = ret;
return 0;
}
int sys_write(int fd, const void *buffer, size_t size, ssize_t *bytes_written) {
ssize_t ret = __sq_syscall3(SQ_SYS_write, fd, (__sq_u64)buffer, size);
if (ret < 0) {
return ret;
}
*bytes_written = ret;
return 0;
}
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
switch (whence) {
case SEEK_SET:
whence = SQ_SEEK_SET;
break;
case SEEK_CUR:
whence = SQ_SEEK_RELATIVE;
break;
case SEEK_END:
__ensure(!"SEEK_END not implemented yet");
break;
default:
__ensure(!"Unknown seek-whence value");
break;
}
ssize_t ret = __sq_syscall3(SQ_SYS_seek, fd, (__sq_u64)offset, whence);
if (ret < 0) {
return ret;
}
*new_offset = ret;
return 0;
}
int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd,
off_t offset, void **window
) {
/* our SQ_PROT_* are the same as defined in abis/mlibc/vm-flags.h,
* so no need to convert them
*/
/* this flag is not supported yet */
__ensure(!(flags & MAP_SHARED));
int sq_flags = 0;
if (flags & MAP_FIXED) {
sq_flags |= SQ_MMAP_FIXED;
}
if (flags & MAP_ANONYMOUS) {
sq_flags |= SQ_MMAP_ANONYMOUS;
}
if (flags & MAP_PRIVATE) {
/* for now is implicit */
}
ssize_t ret = __sq_syscall6(SQ_SYS_mmap, fd, (__sq_u64)hint, offset, size, prot, sq_flags);
if (ret < 0) {
return ret;
}
*window = reinterpret_cast<void*>(ret);
return 0;
}
int sys_vm_unmap(void *pointer, size_t size) {
return __sq_syscall6(SQ_SYS_mmap, SQ_MMAP_FD_NONE, (__sq_u64)pointer,
0, size, 0, SQ_MMAP_UNMAP | SQ_MMAP_FIXED);
}
int sys_anon_allocate(size_t size, void **pointer) {
ssize_t ret = __sq_syscall6(SQ_SYS_mmap, SQ_MMAP_FD_NONE, 0, 0,
(__sq_u32)size, SQ_PROT_READ | SQ_PROT_WRITE,
SQ_MMAP_ANONYMOUS | SQ_MMAP_ADJUST_HEAP);
if (ret < 0) {
return ret;
}
*pointer = reinterpret_cast<void*>(ret);
return 0;
}
int sys_anon_free(void *pointer, size_t size) {
return sys_vm_unmap(pointer, size);
}
#ifndef MLIBC_BUILDING_RTDL
int sys_fork(pid_t *child) {
int ret = __sq_syscall1(SQ_SYS_clone, 0);
if (ret < 0) {
return ret;
}
*child = ret;
return 0;
}
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
return __sq_syscall3(SQ_SYS_execve, (__sq_u64)path, (__sq_u64)argv, (__sq_u64)envp);
}
void sys_exit(int code) {
__sq_syscall1(SQ_SYS_exit, code);
__builtin_trap();
}
pid_t sys_getpid() {
return __sq_syscall0(SQ_SYS_getpid);
}
int sys_clock_get(int clock, time_t *secs, long *nanos) {
*secs = 0;
*nanos = 0;
return 0;
}
int sys_isatty(int fd) {
// TODO: not supported yet
return 0;
}
int sys_ttyname(int fd, char *buf, size_t size) {
// TODO: not supported yet
strncpy(buf, "/dev/console", size);
return 0;
}
uid_t sys_getuid()
{
// TODO: not supported yet
return 0;
}
gid_t sys_getgid()
{
// TODO: not supported yet
return 0;
}
uid_t sys_geteuid()
{
// TODO: not supported yet
return sys_getuid();
}
gid_t sys_getegid()
{
// TODO: not supported yet
return sys_getgid();
}
#endif // !MLIBC_BUILDING_RTDL
} // namespace mlibc

View file

@ -0,0 +1 @@
../../../../abis/mlibc/abi.h

View file

@ -0,0 +1 @@
../../../../abis/squiid/auxv.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/blkcnt_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/blksize_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/dev_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/errno.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/fcntl.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/gid_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/in.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/ino_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/mode_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/nlink_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/pid_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/resource.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/seek-whence.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/signal.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/socket.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/stat.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/termios.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/time.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/uid_t.h

View file

@ -0,0 +1 @@
../../../../abis/mlibc/vm-flags.h

View file

@ -0,0 +1,12 @@
#ifndef __SQUIID_USER_AUXV_H__
#define __SQUIID_USER_AUXV_H__
#define SQ_AT_NULL 0 /* end of auxv */
#define SQ_AT_PHDR 1 /* program headers */
#define SQ_AT_PHENT 2 /* size of program header entry */
#define SQ_AT_PHNUM 3 /* number of program headers */
#define SQ_AT_ENTRY 4 /* entry pointer */
#define SQ_AT_EXECFN 5 /* pointer to exec pathname */
#define SQ_AT_RANDOM 6 /* pointer to random 16-byte value */
#endif /* __SQUIID_USER_AUXV_H__ */

View file

@ -0,0 +1,19 @@
#ifndef __SQUIID_USER_MMAP_H__
#define __SQUIID_USER_MMAP_H__
/* SQ_SYS_mmap fd */
#define SQ_MMAP_FD_NONE -1
/* SQ_SYS_mmap protection */
#define SQ_PROT_NONE 0
#define SQ_PROT_READ 0x1
#define SQ_PROT_WRITE 0x2
#define SQ_PROT_EXEC 0x4
/* SQ_SYS_mmap flags */
#define SQ_MMAP_ANONYMOUS 0x1
#define SQ_MMAP_ADJUST_HEAP 0x2
#define SQ_MMAP_UNMAP 0x4
#define SQ_MMAP_FIXED 0x8
#endif /* __SQUIID_USER_MMAP_H__ */

View file

@ -0,0 +1,17 @@
#ifndef __SQUIID_USER_PROCESS_H__
#define __SQUIID_USER_PROCESS_H__
/* SQ_SYS_clone flags */
#define SQ_CLONE_THREAD 0x1
/* SQ_SYS_prctl operations */
#define __SQ_PR_ARCH_BASE 0x400
#define SQ_PR_ARCH_X86_64_SET_FS __SQ_PR_ARCH_BASE
#define SQ_PR_ARCH_X86_64_SET_GS (__SQ_PR_ARCH_BASE+1)
/* SYS_futex operations */
#define SQ_FUTEX_WAIT 1
#define SQ_FUTEX_WAKE 2
#endif /* __SQUIID_USER_PROCESS_H__ */

View file

@ -0,0 +1,38 @@
#ifndef __SQUIID_USER_STAT_H__
#define __SQUIID_USER_STAT_H__
#include "syscalls.h"
/* open() flags */
#define SQ_OPEN_WRITEONLY 0x02
#define SQ_OPEN_READWRITE 0x04
#define SQ_OPEN_APPEND 0x08
#define SQ_OPEN_CREATE 0x10
#define SQ_OPEN_EXCLUSIVE 0x20
#define SQ_OPEN_TRUNCATE 0x40
#define SQ_OPEN_NOATIME 0x80
/* seek() flags */
#define SQ_SEEK_SET 1
#define SQ_SEEK_RELATIVE 2
/* sys_dirent type */
#define SQ_DT_REG 0
#define SQ_DT_DIR 1
#define SQ_DT_CHR 2
#define SQ_DT_BLK 3
#define _SQ_PATH_MAX 1024
#ifndef __ASSEMBLER__
struct sq_sys_dirent {
__sq_u32 type;
__sq_u32 name_len;
__sq_u8 name[0];
};
#endif /* __ASSEMBLER__ */
#endif /* __SQUIID_USER_STAT_H__ */

View file

@ -0,0 +1,86 @@
#ifndef __SQUIID_USER_SYSCALL_WRAPPERS_H__
#define __SQUIID_USER_SYSCALL_WRAPPERS_H__
#include "syscalls.h"
#define __sq_always_inline __attribute__((always_inline)) inline
static __sq_always_inline __sq_u64 __sq_syscall0(__sq_u64 nr)
{
__sq_u64 result;
asm volatile (
"syscall"
: "=a"(result)
: "a"(nr)
: "cc", "memory", "rcx", "r11", "rdi", "rsi", "rdx", "r10", "r8", "r9"
);
return result;
}
static __sq_always_inline __sq_u64 __sq_syscall1(__sq_u64 nr, __sq_u64 arg1)
{
__sq_u64 result;
asm volatile (
"syscall"
: "=a"(result)
: "a"(nr), "D"(arg1)
: "cc", "memory", "rcx", "r11", "rsi", "rdx", "r10", "r8", "r9"
);
return result;
}
static __sq_always_inline __sq_u64 __sq_syscall2(__sq_u64 nr, __sq_u64 arg1, __sq_u64 arg2)
{
__sq_u64 result;
asm volatile (
"syscall"
: "=a"(result)
: "a"(nr), "D"(arg1), "S"(arg2)
: "cc", "memory", "rcx", "r11", "rdx", "r10", "r8", "r9"
);
return result;
}
static __sq_always_inline __sq_u64 __sq_syscall3(
__sq_u64 nr, __sq_u64 arg1, __sq_u64 arg2, __sq_u64 arg3
) {
__sq_u64 result;
asm volatile (
"syscall"
: "=a"(result)
: "a"(nr), "D"(arg1), "S"(arg2), "d"(arg3)
: "cc", "memory", "rcx", "r11", "r10", "r8", "r9"
);
return result;
}
static __sq_always_inline __sq_u64 __sq_syscall6(
__sq_u64 nr,
__sq_u64 arg1, __sq_u64 arg2, __sq_u64 arg3,
__sq_u64 arg4, __sq_u64 arg5, __sq_u64 arg6
) {
__sq_u64 result;
register __sq_u64 r10 asm("r10") = arg4;
register __sq_u64 r8 asm("r8") = arg5;
register __sq_u64 r9 asm("r9") = arg6;
asm volatile (
"syscall"
: "=a"(result)
: "a"(nr), "D"(arg1), "S"(arg2), "d"(arg3), "r"(r10), "r"(r8), "r"(r9)
: "cc", "memory", "rcx", "r11"
);
return result;
}
#endif /* __SQUIID_USER_SYSCALL_WRAPPERS_H__ */

View file

@ -0,0 +1,33 @@
#ifndef __SQUIID_USER_SYSCALLS_H__
#define __SQUIID_USER_SYSCALLS_H__
#define SQ_SYS_open 0
#define SQ_SYS_close 1
#define SQ_SYS_read 2
#define SQ_SYS_write 3
#define SQ_SYS_seek 4
#define SQ_SYS_exit 5
#define SQ_SYS_execve 6
#define SQ_SYS_clone 7
#define SQ_SYS_getpid 8
#define SQ_SYS_mmap 9
#define SQ_SYS_prctl 10
#define SQ_SYS_futex 11
#define __SQ_SYS_LAST SQ_SYS_futex
#define __SQ_SYS_NUM (__SQ_SYS_LAST+1)
#ifndef __ASSEMBLER__
typedef signed char __sq_s8;
typedef unsigned char __sq_u8;
typedef signed short __sq_s16;
typedef unsigned short __sq_u16;
typedef signed int __sq_s32;
typedef unsigned int __sq_u32;
typedef signed long long __sq_s64;
typedef unsigned long long __sq_u64;
#endif /* __ASSEMBLER__ */
#endif /* __SQUIID_USER_SYSCALLS_H__ */

View file

@ -0,0 +1,59 @@
if not static
rtdl_sources += files(
'generic/syscalls.cpp',
)
endif
libc_sources += files(
'generic/entry.cpp',
'generic/syscalls.cpp',
)
if not no_headers
install_headers(
'include/abi-bits/abi.h',
'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/stat.h',
'include/abi-bits/signal.h',
'include/abi-bits/socket.h',
'include/abi-bits/termios.h',
'include/abi-bits/time.h',
'include/abi-bits/blkcnt_t.h',
'include/abi-bits/blksize_t.h',
'include/abi-bits/dev_t.h',
'include/abi-bits/gid_t.h',
'include/abi-bits/ino_t.h',
'include/abi-bits/mode_t.h',
'include/abi-bits/nlink_t.h',
'include/abi-bits/pid_t.h',
'include/abi-bits/uid_t.h',
subdir: 'abi-bits'
)
install_headers(
'include/squiid/auxv.h',
'include/squiid/mmap.h',
'include/squiid/process.h',
'include/squiid/stat.h',
'include/squiid/syscalls.h',
'include/squiid/syscall_wrappers_x86_64.h',
subdir: 'squiid'
)
endif
if not headers_only
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')
)
endif