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 Descriptor
s 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 Entry
s to
this table. Note that some Descriptor
s may take up 2 Entry
s.
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 registers
Implementations§
Source§impl<const MAX: usize> GlobalDescriptorTable<MAX>
impl<const MAX: usize> GlobalDescriptorTable<MAX>
Sourcepub const fn from_raw_entries(slice: &[u64]) -> Self
pub const fn from_raw_entries(slice: &[u64]) -> Self
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
entires 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
MAX
entries - 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 Entry
s to the table.
Panics if the GDT doesn’t have enough free entries.
Sourcepub fn load(&'static self)
pub fn load(&'static self)
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)
pub unsafe fn load_unsafe(&self)
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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more