Enable SSE

This commit is contained in:
Thomas Lovén 2022-01-22 22:26:03 +01:00
parent b154aed94d
commit e5556e16a7
6 changed files with 46 additions and 1 deletions

View File

@ -27,4 +27,5 @@ void cpu_init() {
apic_init(); apic_init();
ioapic_init(); ioapic_init();
timer_init(); timer_init();
sse_init();
} }

30
src/kernel/cpu/sse.S Normal file
View File

@ -0,0 +1,30 @@
.intel_syntax noprefix
.global sse_init
sse_init:
//; Enable bits 9 and 10 in cr4
//; 9: Enable SSE instructions
//; 10: Enable SSE exceptions
mov rax, cr4
or rax, 1<<9 | 1<<10
mov cr4, rax
//; Set bit 1 and unset bit 2 in cr0
//; 1: Coprocessor monitoring
//; 2: Coprocessor emulation
mov rax, cr0
or rax, 1<<1
and rax, ~(1<<2)
mov cr0, rax
ret
.global sse_save
sse_save:
fxsave [rdi]
ret
.global sse_restore
sse_restore:
fxrstor [rdi]
ret

View File

@ -99,3 +99,8 @@ void ioapic_init();
// cpu/timer.c // cpu/timer.c
void timer_init(); void timer_init();
// cpu/sse.S
void sse_init();
void sse_save(void *addr);
void sse_restore(void *addr);

View File

@ -7,6 +7,7 @@ struct process {
uint64_t state; uint64_t state;
uint64_t P4; uint64_t P4;
struct process *q_next; struct process *q_next;
void *sse;
uint8_t stack[]; uint8_t stack[];
}; };

View File

@ -1,6 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <proc.h> #include <proc.h>
#include <memory.h> #include <memory.h>
#include <stdlib.h>
struct swtch_stack { struct swtch_stack {
uint64_t RBP; uint64_t RBP;
@ -24,6 +25,10 @@ struct process *new_process(void (*function)(void)) {
p->q_next = 0; p->q_next = 0;
p->P4 = new_P4(); p->P4 = new_P4();
// Storage for SSE registers must be 16 byte aligned
void *sse = malloc(512+15);
p->sse = (void *) (((uintptr_t)sse + 15) & ~0xF);
struct swtch_stack *stck = p->stack_ptr; struct swtch_stack *stck = p->stack_ptr;
stck->RBP = (uint64_t)&stck->RBP2; stck->RBP = (uint64_t)&stck->RBP2;
stck->ret = (uint64_t)function; stck->ret = (uint64_t)function;

View File

@ -32,8 +32,11 @@ void scheduler() {
current_proc = new; current_proc = new;
write_cr3(new->P4); write_cr3(new->P4);
sse_restore(current_proc->sse);
switch_stack(&scheduler_proc->stack_ptr, &new->stack_ptr); switch_stack(&scheduler_proc->stack_ptr, &new->stack_ptr);
sse_save(current_proc->sse);
scheduler_insert(current_proc); scheduler_insert(current_proc);
current_proc = 0; current_proc = 0;
} }