Skip to content

Commit 2fbf90c

Browse files
gdt: Add a limit function for building a DescriptorTablePointer (#413)
Co-authored-by: Thomas Epperson <thomas.epperson@snapon.com>
1 parent ad4d90d commit 2fbf90c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/structures/gdt.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,21 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
244244
index
245245
}
246246

247+
/// Returns the value of the limit for a gdt pointer. It is one less than the number of bytes of the table.
248+
pub const fn limit(&self) -> u16 {
249+
use core::mem::size_of;
250+
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
251+
// will not underflow or overflow.
252+
(self.len * size_of::<u64>() - 1) as u16
253+
}
254+
247255
/// Creates the descriptor pointer for this table. This pointer can only be
248256
/// safely used if the table is never modified or destroyed while in use.
249257
#[cfg(feature = "instructions")]
250258
fn pointer(&self) -> super::DescriptorTablePointer {
251-
use core::mem::size_of;
252259
super::DescriptorTablePointer {
253260
base: crate::VirtAddr::new(self.table.as_ptr() as u64),
254-
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
255-
// will not underflow or overflow.
256-
limit: (self.len * size_of::<u64>() - 1) as u16,
261+
limit: self.limit(),
257262
}
258263
}
259264
}

0 commit comments

Comments
 (0)