rstubs/
gdt.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! This module defines the GDT and its segments.
use crate::arch::gdt::{Descriptor, GlobalDescriptorTable, Ring, SegmentSelector};
use crate::arch::DescriptorTablePointer;

/// The global descriptor table
pub static mut GDT: GlobalDescriptorTable<3> = GlobalDescriptorTable([
    // First one is usually null
    Descriptor::new(),
    // Kernel code
    Descriptor::segment(0..=u32::MAX, true, Ring::System),
    // Kernel data
    Descriptor::segment(0..=u32::MAX, false, Ring::System),
    // TODO: BSB B1 - Add user code, user data and TSS segments
]);

/// Fat pointer to the GDT, used by the boot asm code.
pub static GDT_PTR: DescriptorTablePointer = unsafe { GDT.pointer() };

#[allow(unused)]
pub enum Segment {
    KernelCode = 1,
    KernelData = 2,
    UserCode = 3,
    UserData = 4,
    TaskState = 5,
}

impl Segment {
    pub fn selector(self) -> SegmentSelector {
        let user = matches!(self, Self::UserCode | Self::UserData);
        unsafe { GDT.selector(self as _, user) }
    }
}

/// Initialize the GDT and TSS
pub fn init() {
    todo!("BSB B1 - Initialize and set the TSS");
}