Kernel: Only save/load sse state when it is used

There is no need to save and load sse state on every interrupt. Instead
we can use CR0.TS to make threads trigger an interrupt when they use sse
instructions. This can be used to only save and load sse state when
needed.

Processor now keeps track of its current "sse thread" and the scheduler
either enabled or disabled sse based on which thread it is starting up.
When a thread dies, it checks if it was the current sse thread to avoid
use after free bugs. When load balancing, processor has to save the
thread's sse state before sending it to a new processor (if it was the
current sse thread). This ensures thread's sse state will be correct
when the new processor ends up loading it.
This commit is contained in:
2026-01-11 03:06:39 +02:00
parent 35c97e2ff8
commit 4af9699b22
7 changed files with 89 additions and 46 deletions

View File

@@ -29,11 +29,6 @@ namespace Kernel
return Thread::current().yield_registers().sp;
}
extern "C" void load_thread_sse()
{
Thread::current().load_sse();
}
static pid_t s_next_tid = 1;
alignas(16) static uint8_t s_default_sse_storage[512];
@@ -51,6 +46,11 @@ namespace Kernel
return;
}
const auto state = Processor::get_interrupt_state();
Processor::set_interrupt_state(InterruptState::Disabled);
Processor::enable_sse();
const uint32_t mxcsr = 0x1F80;
asm volatile(
"finit;"
@@ -66,6 +66,10 @@ namespace Kernel
: [mxcsr]"m"(mxcsr)
);
Processor::disable_sse();
Processor::set_interrupt_state(state);
s_default_sse_storage_initialized = true;
}
@@ -261,6 +265,12 @@ namespace Kernel
Thread::~Thread()
{
if (Processor::get_current_sse_thread() == this)
{
Processor::set_current_sse_thread(nullptr);
Processor::disable_sse();
}
if (m_delete_process)
{
ASSERT(m_process);