From 0b0184de760cfc27155658d71e75eb4d3b1e53b8 Mon Sep 17 00:00:00 2001 From: Matheus Date: Wed, 10 May 2023 21:20:31 -0300 Subject: [PATCH] kernel: archctl syscall --- kernel-src/arch/x86-64/archctl.c | 28 +++++++++++++++++++++ kernel-src/arch/x86-64/syscall.asm | 4 ++- patches/mlibc/jinx-working-patch.patch | 35 +++++++++++++++++++++----- 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 kernel-src/arch/x86-64/archctl.c diff --git a/kernel-src/arch/x86-64/archctl.c b/kernel-src/arch/x86-64/archctl.c new file mode 100644 index 00000000..26ba5f3e --- /dev/null +++ b/kernel-src/arch/x86-64/archctl.c @@ -0,0 +1,28 @@ +#include +#include +#include + +#define ARCH_CTL_GSBASE 0 +#define ARCH_CTL_FSBASE 1 + +syscallret_t syscall_archctl(context_t *context, int func, void *arg) { + syscallret_t ret = { + .errno = 0, + .ret = 0 + }; + + switch (func) { + case ARCH_CTL_GSBASE: + // kernelgsbase because it will be switched out to user + wrmsr(MSR_KERNELGSBASE, (uint64_t)arg); + break; + case ARCH_CTL_FSBASE: + wrmsr(MSR_FSBASE, (uint64_t)arg); + break; + default: + ret.errno = EINVAL; + break; + } + + return ret; +} diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 99ba7391..1046d6a6 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -5,6 +5,7 @@ extern syscall_openat extern syscall_read extern syscall_seek extern syscall_close +extern syscall_archctl syscalltab: dq syscall_print dq syscall_mmap @@ -12,7 +13,8 @@ dq syscall_openat dq syscall_read dq syscall_seek dq syscall_close -syscallcount equ 6 +dq syscall_archctl +syscallcount equ 7 section .text global arch_syscall_entry ; on entry: diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index e25f861d..fce6eb03 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -71,7 +71,27 @@ index 0000000..0a0a4a0 + diff --git mlibc-workdir/sysdeps/astral/generic/astral.cpp mlibc-workdir/sysdeps/astral/generic/astral.cpp new file mode 100644 -index 0000000..e69de29 +index 0000000..7d5b1e3 +--- /dev/null ++++ mlibc-workdir/sysdeps/astral/generic/astral.cpp +@@ -0,0 +1,17 @@ ++#include ++#include ++ ++#define ARCH_CTL_GSBASE 0 ++#define ARCH_CTL_FSBASE 1 ++ ++#ifndef MLIBC_BUILDING_RTDL ++ ++int syscall_archctl(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 diff --git mlibc-workdir/sysdeps/astral/generic/entry.cpp mlibc-workdir/sysdeps/astral/generic/entry.cpp new file mode 100644 index 0000000..e5b087a @@ -113,10 +133,10 @@ index 0000000..e5b087a + diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp new file mode 100644 -index 0000000..0f88fe5 +index 0000000..1bf8c55 --- /dev/null +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp -@@ -0,0 +1,152 @@ +@@ -0,0 +1,154 @@ +#include +#include +#include @@ -197,7 +217,8 @@ index 0000000..0f88fe5 + } + + int sys_tcb_set(void *pointer) { -+ return ENOSYS; ++ long r; ++ return syscall(SYSCALL_ARCHCTL, &r, ARCH_CTL_FSBASE, (uint64_t)pointer); + } + + int sys_futex_wait(int *pointer, int expected, const struct timespec *time) { @@ -236,6 +257,7 @@ index 0000000..0f88fe5 + int sys_write(int fd, const void *buff, size_t count, ssize_t *bytes_written) { + return ENOSYS; + } ++ + 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); @@ -288,10 +310,10 @@ index 0000000..990eca0 +#endif diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h new file mode 100644 -index 0000000..bb2e40d +index 0000000..2d4b960 --- /dev/null +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h -@@ -0,0 +1,29 @@ +@@ -0,0 +1,30 @@ +#ifndef _SYSCALL_H_INCLUDE +#define _SYSCALL_H_INCLUDE + @@ -301,6 +323,7 @@ index 0000000..bb2e40d +#define SYSCALL_READ 3 +#define SYSCALL_SEEK 4 +#define SYSCALL_CLOSE 5 ++#define SYSCALL_ARCHCTL 6 + +#include +#include