From 7ae51a4849e498925c20dfad14a456bb42cdf6e8 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Wed, 27 Oct 2021 23:44:01 -0300 Subject: [PATCH] Save x87 state on context switch --- Kernel/asm/interrupts.asm | 56 ++++++++++++++++++++++++--------------- Kernel/utils/scheduler.c | 33 ++++++++++++++++++----- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/Kernel/asm/interrupts.asm b/Kernel/asm/interrupts.asm index 06760ff..28c1f21 100755 --- a/Kernel/asm/interrupts.asm +++ b/Kernel/asm/interrupts.asm @@ -171,12 +171,15 @@ picSlaveMask: _irq00Handler: pushState - ; mov rsi, rsp - ; and rsp, -16 - ; sub rsp, 108 - ; fsave [rsp] - ; push rsi - + mov rsi, rsp + and rsp, -16 + sub rsp, 108 + fsave [rsp] + and rsp, -16 + sub rsp, 512 + fxsave [rsp] + push rsi + ; push rax ; call getFPUaddress ; fsave [rax] @@ -207,12 +210,15 @@ _irq00Handler: ; fxrstor [rax] ; pop rax - ; mov rax, rsp - ; pop rsp - ; and rsp, -16 - ; sub rsp, 108 - ; frstor [rsp] - ; mov rsp, rax + pop rsp + mov rax, rsp + and rsp, -16 + sub rsp, 108 + frstor [rsp] + and rsp, -16 + sub rsp, 512 + fxrstor [rsp] + mov rsp, rax popState iretq @@ -300,20 +306,26 @@ _initialize_stack_frame: pushState - ; mov rsi, rsp - ; and rsp, -16 - ; sub rsp, 108 - ; fsave [rsp] - ; push rsi + mov rsi, rsp + and rsp, -16 + sub rsp, 108 + fsave [rsp] + and rsp, -16 + sub rsp, 512 + fxsave [rsp] + push rsi + mov rax, rsp ; fsave [bytesForFPU] - ; push rax - ; mov dword [auxi], 1 - ; call getFPUaddress - ; call getSSEaddress - ; pop rax ; fxsave [bytesForSSEAligned] + ; push rax + ; call getFPUaddress + ; fsave [rax] + ; call getSSEaddress + ; fxsave [rax] + ; pop rax + ; fsave [r8] ; fxsave [r9] diff --git a/Kernel/utils/scheduler.c b/Kernel/utils/scheduler.c index 2d4eb5c..b453ad4 100644 --- a/Kernel/utils/scheduler.c +++ b/Kernel/utils/scheduler.c @@ -2,10 +2,23 @@ #define IDLE_PID 1 // void _initialize_stack_frame(void *, void *, int, char**, void **, void **); -void _initialize_stack_frame(void *, void *, int, char**); +// void _initialize_stack_frame(void *, void *, int, char**, void *, void *); +uint64_t * _initialize_stack_frame(void *, void *, int, char**); enum states {READY = 0, BLOCKED}; +// typedef long Align; + +// union header { +// struct { +// void * fpuBytes; +// void * sseBytes; +// } s; +// Align x; +// }; + +// typedef union header Header; + typedef struct processCDT { struct processCDT * next; char * name; @@ -18,12 +31,12 @@ typedef struct processCDT { char foreground; enum states state; int * fd; + // Header * bytes; + // Header * sse; // void * sseBytes; // void * fpuBytes; } processCDT; -// typedef long Align; - // typedef union processCDT { // struct { // struct p * next; @@ -104,7 +117,7 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * processADT process = pvPortMalloc(sizeof(processCDT)); uint64_t * auxi = pvPortMalloc(STACK_SIZE); uint64_t * rbp = STACK_SIZE + auxi; - uint64_t * rsp = rbp - 20; //22 + uint64_t * rsp = rbp - 20; // 20 //22 char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2; @@ -127,11 +140,15 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * process->state = READY; process->fd = fd; process->next = NULL; + // process->bytes->s.fpuBytes = pvPortMalloc(108); + // process->bytes->s.sseBytes = pvPortMalloc(512); // process->sseBytes = pvPortMalloc(64); // process->fpuBytes = pvPortMalloc(14); // _initialize_stack_frame(fn, rbp, argc, argv, &(process->fpuBytes), &(process->sseBytes)); - _initialize_stack_frame(fn, rbp, argc, argv); + // _initialize_stack_frame(fn, rbp, argc, argv, &(process->bytes->s.sseBytes), &(process->bytes->s.fpuBytes)); + // _initialize_stack_frame(fn, rbp, argc, argv, process->bytes->s.sseBytes, process->bytes->s.fpuBytes); + process->rsp = _initialize_stack_frame(fn, rbp, argc, argv); if (firstReady == NULL) firstReady = process; @@ -143,11 +160,13 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * } // void * getSSEaddress() { -// return currentProcess->sseBytes; +// // return currentProcess->sseBytes; +// return currentProcess->bytes->s.sseBytes; // } // void * getFPUaddress() { -// return currentProcess->fpuBytes; +// // return currentProcess->fpuBytes; +// return currentProcess->bytes->s.fpuBytes; // } void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) {