Skip to content

Commit

Permalink
gdt: Add a limit function for building a DescriptorTablePointer (#413)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Epperson <[email protected]>
  • Loading branch information
uglyoldbob and tepperson2 authored Mar 26, 2024
1 parent ad4d90d commit 2fbf90c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/structures/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,21 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
index
}

/// Returns the value of the limit for a gdt pointer. It is one less than the number of bytes of the table.
pub const fn limit(&self) -> u16 {
use core::mem::size_of;
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
// will not underflow or overflow.
(self.len * size_of::<u64>() - 1) as u16
}

/// Creates the descriptor pointer for this table. This pointer can only be
/// safely used if the table is never modified or destroyed while in use.
#[cfg(feature = "instructions")]
fn pointer(&self) -> super::DescriptorTablePointer {
use core::mem::size_of;
super::DescriptorTablePointer {
base: crate::VirtAddr::new(self.table.as_ptr() as u64),
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
// will not underflow or overflow.
limit: (self.len * size_of::<u64>() - 1) as u16,
limit: self.limit(),
}
}
}
Expand Down

0 comments on commit 2fbf90c

Please sign in to comment.