kernel: add xsave support

This commit is contained in:
monkuous
2025-11-15 23:20:05 +01:00
parent 4024c42af1
commit d301ec6955
8 changed files with 190 additions and 61 deletions
+15 -14
View File
@@ -540,7 +540,7 @@ bool signal_check(struct thread_t *thread, context_t *context, bool syscall, uin
// get where in memory to put the frame
#if ARCH_SIGNAL_STACK_GROWS_DOWNWARDS == 1
void *stack = altstack ? (void *)((uintptr_t)altstack + thread->signals.stack.size) : (void *)CTX_SP(context);
stack = (void *)((((uintptr_t)stack - ARCH_SIGNAL_REDZONE_SIZE - sizeof(sigframe_t)) & ~0xfl) - 0x8);
stack = (void *)((((uintptr_t)stack - ARCH_SIGNAL_REDZONE_SIZE - ARCH_SIGFRAME_SIZE) & ~0xfl) - 0x8);
#else
#error unsupported
#endif
@@ -553,35 +553,36 @@ bool signal_check(struct thread_t *thread, context_t *context, bool syscall, uin
}
// configure stack frame
sigframe_t sigframe;
sigframe.restorer = action->restorer;
sigframe_t *sigframe = __builtin_alloca(ARCH_SIGFRAME_SIZE);
arch_sigframe_init(sigframe);
sigframe->restorer = action->restorer;
if (altstack) {
memcpy(&sigframe.oldstack, &thread->signals.stack, sizeof(stack_t));
memcpy(&sigframe->oldstack, &thread->signals.stack, sizeof(stack_t));
memset(&thread->signals.stack, 0, sizeof(stack_t));
} else {
memset(&sigframe.oldstack, 0, sizeof(stack_t));
memset(&sigframe->oldstack, 0, sizeof(stack_t));
}
if (thread->signals.hasreturnmask) {
// if we were waiting in signal_suspend, push the old mask into the return frame
// of the first signal and set it to not act this way the next check
memcpy(&sigframe.oldmask, &thread->signals.returnmask, sizeof(sigset_t));
memcpy(&sigframe->oldmask, &thread->signals.returnmask, sizeof(sigset_t));
thread->signals.hasreturnmask = false;
} else {
// else, just push the current mask
memcpy(&sigframe.oldmask, &thread->signals.mask, sizeof(sigset_t));
memcpy(&sigframe->oldmask, &thread->signals.mask, sizeof(sigset_t));
}
memcpy(&sigframe.context, context, sizeof(context_t));
memcpy(&sigframe.extracontext, &thread->extracontext, sizeof(extracontext_t));
memcpy(&sigframe->context, context, sizeof(context_t));
arch_extracontext_copy(&sigframe->extracontext, &thread->extracontext);
arch_sigframe_prepare_mcontext(stack, &sigframe);
arch_sigframe_prepare_mcontext(stack, sigframe);
memset(&sigframe.siginfo, 0, sizeof(siginfo_t));
memset(&sigframe->siginfo, 0, sizeof(siginfo_t));
if (signal == SIGSEGV)
sigframe.siginfo.__si_fields.__sigfault.si_addr = CTX_TRAP_ADDR(context);
sigframe->siginfo.__si_fields.__sigfault.si_addr = CTX_TRAP_ADDR(context);
if (usercopy_touser(stack, &sigframe, sizeof(sigframe_t))) {
printf("signal: bad user stack %p (altstack %p) handling signal %d trapno %lu\n", stack, altstack, signal, sigframe.mcontext.gregs[MCONTEXT_REG_TRAPNO]);
if (usercopy_touser(stack, sigframe, ARCH_SIGFRAME_SIZE)) {
printf("signal: bad user stack %p (altstack %p) handling signal %d trapno %lu\n", stack, altstack, signal, sigframe->mcontext.gregs[MCONTEXT_REG_TRAPNO]);
THREAD_LEAVE(thread);
PROCESS_LEAVE(proc);
interrupt_set(true);
+9 -14
View File
@@ -9,31 +9,26 @@
// this expects CTX_SP to be pointing to the signal frame
__attribute__((noreturn)) void syscall_sigreturn(context_t *context) {
sigframe_t sigframe;
int error = usercopy_fromuser(&sigframe, (void *)CTX_SP(context), sizeof(sigframe_t));
if (error || ARCH_CONTEXT_ISUSER(&sigframe.context) == false) {
sigframe_t *sigframe = __builtin_alloca(ARCH_SIGFRAME_SIZE);
int error = usercopy_fromuser(sigframe, (void *)CTX_SP(context), ARCH_SIGFRAME_SIZE);
if (error || ARCH_CONTEXT_ISUSER(&sigframe->context) == false) {
printf("syscall_sigreturn: bad return stack or bad return information\n");
proc_terminate(SIGSEGV);
}
interrupt_set(false);
signal_altstack(current_thread(), &sigframe.oldstack, NULL);
signal_changemask(current_thread(), SIG_SETMASK, &sigframe.oldmask, NULL);
signal_altstack(current_thread(), &sigframe->oldstack, NULL);
signal_changemask(current_thread(), SIG_SETMASK, &sigframe->oldmask, NULL);
arch_sigframe_get_context_from_mcontext(&sigframe);
arch_sigframe_get_context_from_mcontext(sigframe);
__assert(ARCH_CONTEXT_ISUSER(&sigframe.context));
__assert(ARCH_CONTEXT_ISUSER(&sigframe->context));
#ifdef __x86_64__
// XXX is this behavior correct? ucontext doesnt seem to save it on linux and so it will not be saved
sigframe.extracontext.gsbase = rdmsr(MSR_KERNELGSBASE);
sigframe.extracontext.fsbase = rdmsr(MSR_FSBASE);
#endif
memcpy(&current_thread()->extracontext, &sigframe.extracontext, sizeof(extracontext_t));
arch_extracontext_copy(&current_thread()->extracontext, &sigframe->extracontext);
ARCH_CONTEXT_THREADLOAD(current_thread(), context);
interrupt_set(true);
arch_context_switch(&sigframe.context);
arch_context_switch(&sigframe->context);
// if we were to return normally, CTX_RET and CTX_ERRNO would be corrupted,
// so we will manually call arch_context_switch, which will jump to the right userland code
+8 -1
View File
@@ -2,6 +2,7 @@
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <logging.h>
#include <arch/context.h>
static scache_t *thread_cache;
@@ -17,8 +18,14 @@ thread_t *sched_newthread(void *ip, size_t kstacksize, int nice, proc_t *proc, v
memset(thread, 0, sizeof(thread_t));
if (arch_extracontext_init(&thread->extracontext)) {
slab_free(thread_cache, thread);
return NULL;
}
thread->kernelstack = vmm_map(NULL, kstacksize, VMM_FLAGS_ALLOCATE, ARCH_MMU_FLAGS_WRITE | ARCH_MMU_FLAGS_READ | ARCH_MMU_FLAGS_NOEXEC, NULL);
if (thread->kernelstack == NULL) {
arch_extracontext_free(&thread->extracontext);
slab_free(thread_cache, thread);
return NULL;
}
@@ -37,7 +44,6 @@ thread_t *sched_newthread(void *ip, size_t kstacksize, int nice, proc_t *proc, v
}
CTX_INIT(&thread->context, proc != NULL, true);
CTX_XINIT(&thread->extracontext, proc != NULL);
CTX_SP(&thread->context) = proc ? (ctxreg_t)ustack : (ctxreg_t)thread->kernelstacktop;
CTX_IP(&thread->context) = (ctxreg_t)ip;
SPINLOCK_INIT(thread->sleeplock);
@@ -52,6 +58,7 @@ thread_t *sched_newthread(void *ip, size_t kstacksize, int nice, proc_t *proc, v
void sched_destroythread(thread_t *thread) {
vmm_unmap(thread->kernelstack, thread->kernelstacksize, 0);
arch_extracontext_free(&thread->extracontext);
slab_free(thread_cache, thread);
}