This commit is contained in:
2023-10-22 10:44:14 +02:00
parent b94a46cd20
commit a1a0df3f00
5 changed files with 16 additions and 26 deletions

View File

@@ -1,8 +1,8 @@
add_executable(kernel.elf)
target_compile_options(kernel.elf PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-fstack-protector-all -Ofast -mno-sse>)
target_compile_options(kernel.elf PUBLIC $<$<COMPILE_LANGUAGE:C>:-fstack-protector-all -Ofast -mno-sse>)
target_compile_options(kernel.elf PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-fstack-protector-all>)
target_compile_options(kernel.elf PUBLIC $<$<COMPILE_LANGUAGE:C>:-fstack-protector-all>)
add_subdirectory(./arch/)
add_subdirectory(./kernel/)

View File

@@ -58,8 +58,6 @@ typedef struct {
struct task_frame {
uint64_t guard;
char ssestate[512];
uint64_t r15;
uint64_t r14;
uint64_t r13;

View File

@@ -16,6 +16,8 @@
#include "timer.hpp"
#include "tty.hpp"
char temp_fxsave[512] __attribute__((aligned(16)));
void sanity_check_frame(struct task_frame *cur_frame) {
assert2((void *) cur_frame->ip != NULL, "Sanity check");
assert2((void *) cur_frame->sp != NULL, "Sanity check");
@@ -60,6 +62,7 @@ static std::atomic<bool> initialized = false;
static void free_task(struct Task *t) {
kfree(t->stack);
kfree(t->name);
kfree(t->fxsave);
kfree(t);
}
@@ -175,13 +178,16 @@ struct Task *new_ktask(void (*fn)(), const char *name) {
struct Task *newt = static_cast<Task *>(kmalloc(sizeof(struct Task)));
newt->stack = static_cast<uint64_t *>(kmalloc(TASK_SS));
newt->name = static_cast<char *>(kmalloc(strlen(name) + 1));
newt->fxsave = static_cast<char *>(kmalloc(512));
strcpy(name, newt->name);
newt->frame.sp = ((((uintptr_t) newt->stack) + TASK_SS - 1) & (~0xFULL));// Ensure 16byte alignment
newt->frame.ip = (uint64_t) fn;
newt->frame.cs = GDTSEL(gdt_code);
newt->frame.ss = GDTSEL(gdt_data);
for (int i = 0; i < 512; i++) newt->frame.ssestate[i] = 0;
for (int i = 0; i < 512; i++) newt->fxsave[i] = 0;
newt->frame.flags = flags();
newt->frame.guard = IDT_GUARD;
newt->addressSpace = KERN_AddressSpace;
@@ -249,6 +255,7 @@ extern "C" void switch_task(struct task_frame *cur_frame) {
if (RunningTask) {
RunningTask->task->frame = *cur_frame;
memcpy(RunningTask->task->fxsave, temp_fxsave, 512);
if (RunningTask->task->state == TS_RUNNING) {
assert2(RunningTask->next == NULL, "next should be removed from RunningTask!");
append_task_node(&NextTasks, RunningTask);
@@ -315,6 +322,7 @@ extern "C" void switch_task(struct task_frame *cur_frame) {
RunningTask = next;
*cur_frame = RunningTask->task->frame;
memcpy(temp_fxsave, RunningTask->task->fxsave, 512);
sanity_check_frame(cur_frame);
}

View File

@@ -30,6 +30,7 @@ struct Task {
struct task_frame frame;
struct AddressSpace *addressSpace;
uint64_t *stack;
char *fxsave;
char *name;
enum TaskMode mode;
uint64_t sleep_until;

View File

@@ -5,6 +5,8 @@
; OBJECT_DEPENDS also doesn't seem to work...
; =!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!
extern temp_fxsave
; TODO: This is probably not enough
%macro pushaq 0
push rax
@@ -23,18 +25,7 @@
push r14
push r15
; Ensure 16-byte alignment
; This works as last bunch of bits in fxsave state aren't used
; Fxsaved memory needs to be zeroed before!
mov rsi, 0x0000000000000000
times 64 push rsi
mov rsi, rsp
add rsi, 32
mov rdi, 0xFFFFFFFFFFFFFFF0
and rsi, rdi
fxsave64 [rsi]
fxsave64 [temp_fxsave]
mov rdi, 0xdeadbe3fdeadb3ef ; IDT_GUARD
push rdi ; IDT_GUARD
@@ -43,15 +34,7 @@
%macro popaq 0
add rsp, 8 ; remove IDT_GUARD
; Ensure 16-byte alignment
; This works as last bunch of bits in fxsave state aren't used
mov rsi, rsp
add rsi, 32
mov rdi, 0xFFFFFFFFFFFFFFF0
and rsi, rdi
fxrstor64 [rsi]
add rsp, 512
fxrstor64 [temp_fxsave]
pop r15
pop r14