Actually, we can protect the GDT behind a proper mutex

This commit is contained in:
apio 2022-11-01 12:19:48 +01:00
parent f29161fd0b
commit 21c5ef0822

View File

@ -6,13 +6,11 @@ use x86_64::registers::segmentation::{CS, SS, Segment, SegmentSelector};
use x86_64::PrivilegeLevel; use x86_64::PrivilegeLevel;
use spin::Mutex; use spin::Mutex;
static mut GDT: GlobalDescriptorTable = GlobalDescriptorTable::new(); static GDT: Mutex<GlobalDescriptorTable> = Mutex::new(GlobalDescriptorTable::new());
static GDT_LOCK: Mutex<u64> = Mutex::new(0); // we cannot lock the GDT directly as it must stay in the same place in memory and that behaves weirdly with mutexes
fn add_entry(descriptor: Descriptor) fn add_entry(descriptor: Descriptor)
{ {
GDT_LOCK.lock(); GDT.lock().add_entry(descriptor);
unsafe { GDT.add_entry(descriptor); }
} }
pub fn init() pub fn init()
@ -26,9 +24,8 @@ pub fn init()
pub fn load_and_reload_segment_registers() pub fn load_and_reload_segment_registers()
{ {
GDT_LOCK.lock();
unsafe { unsafe {
GDT.load(); GDT.lock().load_unsafe();
CS::set_reg(SegmentSelector::new(1, PrivilegeLevel::Ring0)); CS::set_reg(SegmentSelector::new(1, PrivilegeLevel::Ring0));
SS::set_reg(SegmentSelector::new(2, PrivilegeLevel::Ring0)); SS::set_reg(SegmentSelector::new(2, PrivilegeLevel::Ring0));
} }