scheduler: Fix page table switch for scheduler

The userspace process may map pages where the kernel has allocated data and cause a crash.

This patch fixes this issue by having a separate IRQ handler which sets the kernel page table at the start of SchedulerInterruptHandler() and restores it in SchedulerHandlerStub() function.
This commit is contained in:
EnderIce2
2024-11-17 03:11:20 +02:00
parent 079055082a
commit 86a119ea51
9 changed files with 161 additions and 21 deletions

View File

@ -641,6 +641,38 @@ namespace CPU
uint64_t ss; /* Stack Segment */
};
struct SchedulerFrame
{
uint64_t ppt; /* Process Page Table */
uint64_t opt; /* Original Page Table */
uint64_t r15; /* General purpose */
uint64_t r14; /* General purpose */
uint64_t r13; /* General purpose */
uint64_t r12; /* General purpose */
uint64_t r11; /* General purpose */
uint64_t r10; /* General purpose */
uint64_t r9; /* General purpose */
uint64_t r8; /* General purpose */
uint64_t rbp; /* Base Pointer (meant for stack frames) */
uint64_t rdi; /* Destination index for string operations */
uint64_t rsi; /* Source index for string operations */
uint64_t rdx; /* Data (commonly extends the A register) */
uint64_t rcx; /* Counter */
uint64_t rbx; /* Base */
uint64_t rax; /* Accumulator */
uint64_t InterruptNumber; /* Interrupt Number */
uint64_t ErrorCode; /* Error code */
uint64_t rip; /* Instruction Pointer */
uint64_t cs; /* Code Segment */
RFLAGS rflags; /* Register Flags */
uint64_t rsp; /* Stack Pointer */
uint64_t ss; /* Stack Segment */
};
struct ExceptionFrame
{
uint64_t cr0; /* Control Register 0 (system control) */
@ -1026,6 +1058,7 @@ namespace CPU
* @note This is for x86_64
*/
typedef x64::TrapFrame TrapFrame;
typedef x64::SchedulerFrame SchedulerFrame;
typedef x64::ExceptionFrame ExceptionFrame;
#elif defined(a32)
/**
@ -1034,6 +1067,7 @@ namespace CPU
* @note This is for x86_32
*/
typedef x32::TrapFrame TrapFrame;
typedef x32::SchedulerFrame SchedulerFrame;
typedef x32::ExceptionFrame ExceptionFrame;
#elif defined(aa64)
/**
@ -1042,6 +1076,7 @@ namespace CPU
* @note This is for aarch64
*/
typedef aarch64::TrapFrame TrapFrame;
typedef aarch64::SchedulerFrame SchedulerFrame;
typedef aarch64::TrapFrame ExceptionFrame;
#endif
}

View File

@ -78,6 +78,7 @@ namespace Interrupts
public:
virtual void OnInterruptReceived(CPU::TrapFrame *Frame);
virtual void OnInterruptReceived(CPU::SchedulerFrame *Frame);
};
}

View File

@ -142,8 +142,8 @@ namespace Tasking::Scheduler
void WakeUpThreads();
void CleanupTerminated();
void Schedule(CPU::TrapFrame *Frame);
void OnInterruptReceived(CPU::TrapFrame *Frame) final;
void Schedule(CPU::SchedulerFrame *Frame);
void OnInterruptReceived(CPU::SchedulerFrame *Frame) final;
Custom(Task *ctx);
virtual ~Custom();

View File

@ -312,11 +312,11 @@ namespace Tasking
{
#ifdef a64
CPU::x64::FXState fx;
CPU::x64::TrapFrame tf;
CPU::x64::SchedulerFrame tf;
uintptr_t GSBase, FSBase, ShadowGSBase;
#else
CPU::x32::FXState fx;
CPU::x32::TrapFrame tf;
CPU::x32::SchedulerFrame tf;
uintptr_t GSBase, FSBase;
#endif
sigset_t SignalMask;
@ -422,7 +422,7 @@ namespace Tasking
int AddSignal(Signals sig, union sigval val = {0}, pid_t tid = -1);
int RemoveSignal(Signals sig);
bool HandleSignal(CPU::TrapFrame *tf, void *thread);
bool HandleSignal(CPU::SchedulerFrame *tf, void *thread);
void RestoreHandleSignal(SyscallsFrame *tf, void *thread);
int SetAction(Signals sig, const SignalAction *act);

View File

@ -356,10 +356,10 @@ namespace Tasking
/* CPU state */
#if defined(a64)
CPU::x64::TrapFrame Registers{};
CPU::x64::SchedulerFrame Registers{};
uintptr_t ShadowGSBase, GSBase, FSBase;
#elif defined(a32)
CPU::x32::TrapFrame Registers{};
CPU::x32::SchedulerFrame Registers{};
uintptr_t ShadowGSBase, GSBase, FSBase;
#elif defined(aa64)
uintptr_t Registers; // TODO