Skip to content

Commit

Permalink
attempt to fix wrong GDT pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Dec 24, 2022
1 parent a076452 commit b87b70b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions hal-x86_64/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ impl DtablePtr {
pub(crate) unsafe fn new_unchecked<T>(t: &T) -> Self {
let limit = (mem::size_of::<T>() - 1) as u16;
let base = t as *const _ as *const ();
Self { limit, base }
}

pub(crate) unsafe fn new_raw(base: *const (), limit: u16) -> Self {
Self { limit, base }
}
}
Expand Down
13 changes: 10 additions & 3 deletions hal-x86_64/src/segment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// x86 memory segmentation structures.
use crate::{cpu, task};
use core::{arch::asm, mem};
use core::{arch::asm, convert::TryInto, mem};
use mycelium_util::{
bits::{self, Pack64, Packing64, Pair64},
fmt,
Expand Down Expand Up @@ -335,6 +335,10 @@ 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);

/// Sets `self` as the current GDT.
///
/// This method is safe, because the `'static` bound on `self` ensures that
Expand Down Expand Up @@ -370,15 +374,18 @@ impl<const SIZE: usize> Gdt<SIZE> {
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 ptr = cpu::DtablePtr::new_unchecked(&self.entries);
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);
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!(SIZE + (crate::cpu::topology::MAX_CPUS * 2) <= 65535);
assert!(Self::TOTAL_SIZE <= 65535);
Gdt {
entries: [Descriptor::new(); SIZE],
tss_descrs: [SystemDescriptor::null(); crate::cpu::topology::MAX_CPUS],
Expand Down

0 comments on commit b87b70b

Please sign in to comment.