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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Segmentation.

use super::PrivilegeLevel;
use core::arch::asm;

use super::table::{GlobalDescriptorTable, LocalDescriptorTable, SystemTableRegister};
use super::tss::TaskStateSegment;

bitflags::bitflags! {
    /// X86_64's access permission of segment.
    pub struct SegmentAccess: u64 {
        /// Granularity
        const G = 1 << 55;
        /// Default operation size (0 = 16-bit segment; 1 = 32-bit segment)
        const D_B = 1 << 54;
        /// 64-bit code segment (IA-32e mode only)
        const L = 1 << 53;
        /// Available for use by system software
        const AVL = 1 << 52;

        /// Segment present
        const P = 1 << 47;
        /// Descriptor type  (0 = system; 1 = code or data).
        const S = 1 << 44;
        /// Data or Code
        const CODE = 1 << 43;
        /// Expand_down/Conforming.
        const EC = 1 << 42;
        /// Writable/Readable.
        const WR = 1 << 41;
        /// Accessed.
        const A = 1 << 40;
    }
}

impl SegmentAccess {
    const BASE_31_24_SHIFT: u64 = 56;
    const SEG_LIMIT_SHIFT: u64 = 48;
    const DPL_SHIFT: u64 = 45;
    const BASE_23_0_SHIFT: u64 = 16;
}

/// X86_64's Segment Descriptor.
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct SegmentDescriptor(u64);

impl SegmentDescriptor {
    /// Create a null segment.
    #[inline]
    pub const fn null() -> Self {
        Self::new(0, 0, SegmentAccess::empty(), PrivilegeLevel::Ring0)
    }

    /// Create a new segment.
    #[inline]
    pub const fn new(base: u64, limit: u64, access: SegmentAccess, dpl: PrivilegeLevel) -> Self {
        let (limit_15_0, base_23_0, access, dpl, limit_23_16, base_31_24) = (
            limit & 0xffff,
            base & 0xff_ffff,
            access.bits(),
            dpl as u64,
            (limit >> 16) & 0xf,
            (base >> 24) & 0xff,
        );

        Self(
            limit_15_0
                | base_23_0 << SegmentAccess::BASE_23_0_SHIFT
                | access
                | dpl << SegmentAccess::DPL_SHIFT
                | limit_23_16 << SegmentAccess::SEG_LIMIT_SHIFT
                | base_31_24 << SegmentAccess::BASE_31_24_SHIFT,
        )
    }
}

bitflags::bitflags! {
    /// X86_64's access permission of 64bit segment.
    pub struct SegmentAccess64: u64 {
        /// Granularity
        const G = 1 << 55;
        /// Available for use by system software
        const AVL = 1 << 52;

        /// Segment present
        const P = 1 << 47;
        /// Data or Code
        const CODE = 1 << 43;
        /// Expand_down/Conforming.
        const EC = 1 << 42;
        /// Writable/Readable.
        const WR = 1 << 41;
        /// Accessed.
        const A = 1 << 40;
        /// Available 64-bit TSS
        const T64A = 0x9 << Self::TYPE_SHIFT;
        /// Busy 64-bit TSS
        const T64B = 0xB << Self::TYPE_SHIFT;
        /// 64-bit Call Gate
        const CG64 = 0xC << Self::TYPE_SHIFT;
        /// 64-bit Interrupt Gate
        const IG64 = 0xE << Self::TYPE_SHIFT;
        /// 64-bit Trap Gate
        const TG64 = 0xF << Self::TYPE_SHIFT;

    }
}

impl SegmentAccess64 {
    const BASE_31_24_SHIFT: u64 = 56;
    const SEG_LIMIT_SHIFT: u64 = 48;
    const DPL_SHIFT: u64 = 45;
    const BASE_23_0_SHIFT: u64 = 16;
    const TYPE_SHIFT: u64 = 40;
}

/// X86_64's 64bit Segment Descriptor.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SegmentDescriptor64(u64, u64);

impl SegmentDescriptor64 {
    /// Create a null segment.
    #[inline]
    pub const fn null() -> Self {
        Self::new(0, 0, SegmentAccess64::empty(), PrivilegeLevel::Ring0)
    }

    /// Create a new segment.
    #[inline]
    pub const fn new(base: u64, limit: u64, access: SegmentAccess64, dpl: PrivilegeLevel) -> Self {
        let (limit_15_0, base_23_0, access, dpl, limit_23_16, base_31_24, base_63_32, clear) = (
            limit & 0xffff,
            base & 0xff_ffff,
            access.bits(),
            dpl as u64,
            (limit >> 16) & 0xf,
            (base >> 24) & 0xff,
            (base >> 32) & 0xffff_ffff,
            0,
        );

        Self(
            limit_15_0
                | base_23_0 << SegmentAccess64::BASE_23_0_SHIFT
                | access
                | dpl << SegmentAccess64::DPL_SHIFT
                | limit_23_16 << SegmentAccess64::SEG_LIMIT_SHIFT
                | base_31_24 << SegmentAccess64::BASE_31_24_SHIFT,
            base_63_32 | clear << 40,
        )
    }
}

/// X86_64's segment selector.
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct SegmentSelector(u16);

impl core::fmt::Debug for SegmentSelector {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        f.debug_struct("SegmentSelector")
            .field("index", &self.index())
            .field("dpl", &self.dpl())
            .finish()
    }
}

impl SegmentSelector {
    /// Create a new SegmentSelector from the index and dpl.
    #[inline]
    pub const fn new(index: u16, dpl: PrivilegeLevel) -> Self {
        Self((index << 3) | dpl as u16)
    }

    /// Pack the SegmentSelector into a word.
    #[inline]
    pub const fn pack(self) -> u16 {
        self.0
    }

    /// Get index of the SegmentSelector.
    #[inline]
    pub const fn index(self) -> u16 {
        self.0 >> 3
    }

    /// Get dpl of the SegmentSelector.
    #[inline]
    pub const fn dpl(self) -> PrivilegeLevel {
        match self.0 & 3 {
            0 => PrivilegeLevel::Ring0,
            1 => PrivilegeLevel::Ring1,
            2 => PrivilegeLevel::Ring2,
            3 => PrivilegeLevel::Ring3,
            _ => unreachable!(),
        }
    }
}

/// X86_64's Segment Register
pub enum SegmentRegister {
    /// Code Segment.
    Cs,
    /// Data Segment.
    Ds,
    /// Stack Segment.
    Ss,
    /// Extra Segment.
    Es,
    /// Extra Segment (E -> F).
    Fs,
    /// Extra Segment (F -> G).
    Gs,
    /// Task State Segment.
    Tss,
}

impl SegmentRegister {
    /// Load the segment selector into the segment register.
    #[inline(always)]
    pub fn load(&self, ss: SegmentSelector) {
        unsafe {
            match self {
                Self::Cs => {
                    // XXX: unique_id_per asm is not supported yet.
                    // we need to use att syntax as rex.W prefix is not supported.
                    #[inline(never)]
                    unsafe fn do_cs_reload(ss: SegmentSelector) {
                        asm!("push {0}",
                             "movabs $1f, %rax",
                             "push %rax",
                             "lretq",
                             "1:",
                             in(reg) ss.pack() as u64,
                             out("rax") _,
                             options(att_syntax))
                    }

                    do_cs_reload(ss)
                }
                Self::Ds => asm!(
                    "mov ds, {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
                Self::Ss => asm!(
                    "mov ss, {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
                Self::Es => asm!(
                    "mov es, {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
                Self::Fs => asm!(
                    "mov fs, {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
                Self::Gs => asm!(
                    "mov gs, {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
                Self::Tss => asm!(
                    "ltr {:x}",
                    in(reg) ss.pack(),
                    options(nostack, nomem)),
            }
        }
    }
}

/// A table for segment descriptors.
#[repr(C)]
pub struct SegmentTable {
    _null: SegmentDescriptor,
    kernel_code: SegmentDescriptor,
    kernel_data: SegmentDescriptor,
    user_data: SegmentDescriptor,
    user_code: SegmentDescriptor,
    tss: [SegmentDescriptor64; crate::MAX_CPU],
}

/// Types of segment.
#[derive(Copy, Clone, Debug)]
pub enum Segment {
    /// Null segment.
    Null,
    /// Kernel code segment (KC).
    KernelCode,
    /// Kernel data segment (KD).
    KernelData,
    /// User data segment (UD).
    UserData,
    /// User code segment (UC).
    UserCode,
    /// Task-state-struct segment (TSS).
    Tss,
}

const TSS_INIT: (super::tss::TaskStateSegment, usize) = (super::tss::TaskStateSegment::empty(), 0);

#[doc(hidden)]
pub static mut TSS: [(super::tss::TaskStateSegment, usize); crate::MAX_CPU] =
    [TSS_INIT; crate::MAX_CPU];

impl Segment {
    /// Segment selector for Kernel Code.
    pub const KERNEL_CODE_SELECTOR: SegmentSelector =
        SegmentSelector::new(1, PrivilegeLevel::Ring0);
    /// Segment selector for Kernel Data.
    pub const KERNEL_DATA_SELECTOR: SegmentSelector =
        SegmentSelector::new(2, PrivilegeLevel::Ring0);
    /// Segment selector for User Data.
    pub const USER_DATA_SELECTOR: SegmentSelector = SegmentSelector::new(3, PrivilegeLevel::Ring3);
    /// Segment selector for User Code.
    pub const USER_CODE_SELECTOR: SegmentSelector = SegmentSelector::new(4, PrivilegeLevel::Ring3);

    /// Get segment selector from the [`Segment`].
    #[inline]
    pub fn into_selector(self) -> SegmentSelector {
        let cpuid = crate::x86_64::intrinsics::cpuid();
        match self {
            Self::Null => SegmentSelector::new(0, PrivilegeLevel::Ring3),
            Self::KernelCode => Self::KERNEL_CODE_SELECTOR,
            Self::KernelData => Self::KERNEL_DATA_SELECTOR,
            Self::UserData => Self::USER_DATA_SELECTOR,
            Self::UserCode => Self::USER_CODE_SELECTOR,
            Self::Tss => SegmentSelector::new(5 + 2 * cpuid as u16, PrivilegeLevel::Ring0),
        }
    }
}

impl SegmentTable {
    /// Initialize tss segment.
    ///
    /// # SAFETY
    /// Interrupt must be blocked.
    pub(crate) unsafe fn init_tss(&'static mut self) {
        let cpuid = crate::x86_64::intrinsics::cpuid();
        let tss = &mut TSS[cpuid].0;

        (tss as *mut TaskStateSegment)
            .as_mut()
            .unwrap()
            .fill_segment_descriptor(&mut self.tss[cpuid]);

        SegmentRegister::Tss.load(Segment::Tss.into_selector());
    }

    #[inline]
    /// Update the tss.
    ///
    /// # SAFETY
    /// Interrupt must be blocked.
    pub unsafe fn update_tss(v: usize) {
        let cpuid = crate::x86_64::intrinsics::cpuid();
        TSS[cpuid].0.rsp0 = v;
    }

    /// Get current tss address.
    pub unsafe fn current_tss() -> &'static mut TaskStateSegment {
        let cpuid = crate::x86_64::intrinsics::cpuid();
        &mut TSS[cpuid].0
    }

    /// Load this table into global descriptor table and segment registers.
    #[inline]
    pub fn load(&'static self) {
        SystemTableRegister::new(self).load::<GlobalDescriptorTable>();
        SegmentRegister::Gs.load(Segment::UserData.into_selector());
        SegmentRegister::Fs.load(Segment::UserData.into_selector());
        SegmentRegister::Es.load(Segment::Null.into_selector());
        SegmentRegister::Ds.load(Segment::Null.into_selector());
        SegmentRegister::Ss.load(Segment::KernelData.into_selector());
        SegmentRegister::Cs.load(Segment::KernelCode.into_selector());
        LocalDescriptorTable::kill();
    }
}

/// A unique kernel segment table.
pub static mut SEGMENT_TABLE: SegmentTable = SegmentTable {
    _null: SegmentDescriptor::null(),
    kernel_code: SegmentDescriptor::new(
        0,
        0xffffffff,
        SegmentAccess::from_bits_truncate(
            SegmentAccess::P.bits()
            | SegmentAccess::S.bits()
            | SegmentAccess::L.bits()
            | SegmentAccess::G.bits()
            // Code, readable.
            | SegmentAccess::CODE.bits()
            | SegmentAccess::WR.bits(),
        ),
        PrivilegeLevel::Ring0,
    ),
    kernel_data: SegmentDescriptor::new(
        0,
        0xffffffff,
        SegmentAccess::from_bits_truncate(
            SegmentAccess::P.bits()
            | SegmentAccess::S.bits()
            | SegmentAccess::L.bits()
            | SegmentAccess::G.bits()
            // Data, writable.
            | SegmentAccess::WR.bits(),
        ),
        PrivilegeLevel::Ring0,
    ),
    user_data: SegmentDescriptor::new(
        0,
        0xffffffff,
        SegmentAccess::from_bits_truncate(
            SegmentAccess::P.bits()
            | SegmentAccess::S.bits()
            | SegmentAccess::L.bits()
            | SegmentAccess::G.bits()
            // Data, writable.
            | SegmentAccess::WR.bits(),
        ),
        PrivilegeLevel::Ring3,
    ),
    user_code: SegmentDescriptor::new(
        0,
        0xffffffff,
        SegmentAccess::from_bits_truncate(
            SegmentAccess::P.bits()
            | SegmentAccess::S.bits()
            | SegmentAccess::L.bits()
            | SegmentAccess::G.bits()
            // Code, readable.
            | SegmentAccess::CODE.bits()
            | SegmentAccess::WR.bits(),
        ),
        PrivilegeLevel::Ring3,
    ),
    tss: [SegmentDescriptor64::null(); crate::MAX_CPU],
};