Skip to content

Commit

Permalink
fix screwed up gdt/gdt pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Dec 24, 2022
1 parent b87b70b commit a737b66
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
7 changes: 2 additions & 5 deletions hal-x86_64/src/cpu/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,13 @@ impl Topology {
})
}

pub fn init_boot_processor<const GDT_SIZE: usize>(&mut self, gdt: &mut segment::Gdt<GDT_SIZE>) {
pub fn init_boot_processor(&mut self, gdt: &mut segment::Gdt) {
self.boot_processor.init_processor(gdt);
}
}

impl Processor {
pub(crate) fn init_processor<const GDT_SIZE: usize>(
&mut self,
gdt: &mut segment::Gdt<GDT_SIZE>,
) {
pub(crate) fn init_processor(&mut self, gdt: &mut segment::Gdt) {
tracing::info!(self.id, "initializing processor");
assert!(!self.initialized, "processor already initialized");

Expand Down
42 changes: 16 additions & 26 deletions hal-x86_64/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@ bits::bitfield! {
// it to `usize` in the array, but this requires unstable const generics
// features and i didn't want to mess with it...
#[derive(Clone)]
#[repr(C)]
// rustfmt eats default parameters in const generics for some reason (probably a
// bug...)
#[rustfmt::skip]
pub struct Gdt<const SIZE: usize = 8> {
entries: [Descriptor; SIZE],
tss_descrs: [SystemDescriptor; crate::cpu::topology::MAX_CPUS],
push_tss_at: usize,
pub struct Gdt {
entries: [u64; Self::SIZE as usize],
push_at: usize,
}

Expand Down Expand Up @@ -334,10 +333,8 @@ impl Selector {

// === impl Gdt ===

impl<const SIZE: usize> Gdt<SIZE> {
/// The total size of the GDT, including both the normal GDT descriptors and
/// system (TSS) descriptors.
const TOTAL_SIZE: usize = SIZE + (crate::cpu::topology::MAX_CPUS * 2);
impl Gdt {
const SIZE: u16 = 8 + (crate::cpu::topology::MAX_CPUS as u16 * 2);

/// Sets `self` as the current GDT.
///
Expand Down Expand Up @@ -372,25 +369,17 @@ impl<const SIZE: usize> Gdt<SIZE> {
///
/// `self` must point to a GDT that is valid for the `'static` lifetime.
unsafe fn load_unchecked(&self) {
// Create the descriptor table pointer with *just* the actual table, so
// that the next push index isn't considered a segment descriptor!
let limit: u16 = ((Self::TOTAL_SIZE - 1) * mem::size_of::<Descriptor>())
.try_into()
.expect("GDT size would exceed a u16");
let ptr = cpu::DtablePtr::new_raw(self as *const _ as *const (), limit);
let ptr = cpu::DtablePtr::new_unchecked(&self.entries);
tracing::trace!(?ptr, "loading GDT");
cpu::intrinsics::lgdt(ptr);
tracing::trace!("loaded GDT!");
}

/// Returns a new `Gdt` with all entries zeroed.
pub const fn new() -> Self {
assert!(Self::TOTAL_SIZE <= 65535);
Gdt {
entries: [Descriptor::new(); SIZE],
tss_descrs: [SystemDescriptor::null(); crate::cpu::topology::MAX_CPUS],
entries: [Descriptor::new().bits(); Self::SIZE as usize],
push_at: 1,
push_tss_at: 1,
}
}

Expand All @@ -417,26 +406,27 @@ impl<const SIZE: usize> Gdt<SIZE> {

const fn push(&mut self, entry: Descriptor) -> u16 {
let idx = self.push_at;
self.entries[idx] = entry;
self.entries[idx] = entry.bits();
self.push_at += 1;
idx as u16
}

const fn push_tss(&mut self, entry: SystemDescriptor) -> u16 {
let idx = self.push_tss_at;
self.tss_descrs[idx] = entry;
self.push_tss_at += 1;
let idx = self.push_at;
self.entries[idx] = entry.low;
self.entries[idx + 1] = entry.high;
self.push_at += 2;
idx as u16
}
}

impl<const SIZE: usize> fmt::Debug for Gdt<SIZE> {
impl fmt::Debug for Gdt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Gdt")
.field("capacity", &SIZE)
.field("capacity", &Self::SIZE)
.field("len", &(self.push_at - 1))
.field("entries", &&self.entries[..self.push_at])
.field("tss_descrs", &&self.tss_descrs[..self.push_tss_at])
// .field("entries", &&self.entries[..self.push_at])
// .field("tss_descrs", &&self.tss_descrs[..self.push_tss_at])
.finish()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/segmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mycelium_util::{
sync::{self, spin},
};

pub(super) static GDT: spin::Mutex<Gdt<8>> = spin::Mutex::new(Gdt::new());
pub(super) static GDT: spin::Mutex<Gdt> = spin::Mutex::new(Gdt::new());

#[tracing::instrument(level = tracing::Level::DEBUG)]
pub(super) fn init_gdt() {
Expand Down

0 comments on commit a737b66

Please sign in to comment.