pub struct GlobalDescriptorTable<const MAX: usize = 8> { /* private fields */ }Expand description
A 64-bit mode global descriptor table (GDT).
In 64-bit mode, segmentation is not supported. The GDT is used nonetheless, for example for switching between user and kernel mode or for loading a TSS.
The GDT has a fixed maximum size given by the MAX const generic parameter.
Overflowing this limit by adding too many Descriptors via
GlobalDescriptorTable::append will panic.
You do not need to add a null segment descriptor yourself - this is already done
internally. This means you can add up to MAX - 1 additional Entrys to
this table. Note that some Descriptors may take up 2 Entrys.
Data segment registers in ring 0 can be loaded with the null segment selector. When running in
ring 3, the ss register must point to a valid data segment which can be obtained through the
Descriptor::user_data_segment() function. Code segments must
be valid and non-null at all times and can be obtained through the
Descriptor::kernel_code_segment() and
Descriptor::user_code_segment() in rings 0 and 3
respectively.
For more info, see:
x86 Instruction Reference for mov,
Intel Manual,
AMD Manual
§Example
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor};
let mut gdt = GlobalDescriptorTable::new();
gdt.append(Descriptor::kernel_code_segment());
gdt.append(Descriptor::user_code_segment());
gdt.append(Descriptor::user_data_segment());
// Add entry for TSS, call gdt.load() then update segment registersImplementations§
Source§impl GlobalDescriptorTable
impl GlobalDescriptorTable
Sourcepub const fn new() -> GlobalDescriptorTable
pub const fn new() -> GlobalDescriptorTable
Creates an empty GDT with the default length of 8.
Source§impl<const MAX: usize> GlobalDescriptorTable<MAX>
impl<const MAX: usize> GlobalDescriptorTable<MAX>
Sourcepub const fn empty() -> GlobalDescriptorTable<MAX>
pub const fn empty() -> GlobalDescriptorTable<MAX>
Creates an empty GDT which can hold MAX number of Entrys.
Sourcepub const fn from_raw_entries(slice: &[u64]) -> GlobalDescriptorTable<MAX>
pub const fn from_raw_entries(slice: &[u64]) -> GlobalDescriptorTable<MAX>
Forms a GDT from a slice of u64.
This method allows for creation of a GDT with malformed or invalid
entries. However, it is safe because loading a GDT with invalid
entries doesn’t do anything until those entries are used. For example,
[CS::set_reg] and load_tss
are both unsafe for this reason.
Panics if:
- the provided slice has more than
MAXentries - the provided slice is empty
- the first entry is not zero
Sourcepub const fn append(&mut self, entry: Descriptor) -> SegmentSelector
pub const fn append(&mut self, entry: Descriptor) -> SegmentSelector
Appends the given segment descriptor to the GDT, returning the segment selector.
Note that depending on the type of the Descriptor this may append
either one or two new Entrys to the table.
Panics if the GDT doesn’t have enough free entries.
Sourcepub fn load(&'static self)
Available on x86-64 and crate feature instructions only.
pub fn load(&'static self)
instructions only.Loads the GDT in the CPU using the lgdt instruction. This does not alter any of the
segment registers; you must (re)load them yourself using the appropriate
functions:
[SS::set_reg()] and [CS::set_reg()].
Sourcepub unsafe fn load_unsafe(&self)
Available on x86-64 and crate feature instructions only.
pub unsafe fn load_unsafe(&self)
instructions only.Loads the GDT in the CPU using the lgdt instruction. This does not alter any of the
segment registers; you must (re)load them yourself using the appropriate
functions:
[SS::set_reg()] and [CS::set_reg()].
§Safety
Unlike load this function will not impose a static lifetime constraint
this means its up to the user to ensure that there will be no modifications
after loading and that the GDT will live for as long as it’s loaded.
Trait Implementations§
Source§impl<const MAX: usize> Clone for GlobalDescriptorTable<MAX>
impl<const MAX: usize> Clone for GlobalDescriptorTable<MAX>
Source§fn clone(&self) -> GlobalDescriptorTable<MAX>
fn clone(&self) -> GlobalDescriptorTable<MAX>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more