-
Notifications
You must be signed in to change notification settings - Fork 33
/
gdt.rs
65 lines (58 loc) · 2.24 KB
/
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
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
use lazy_static::lazy_static;
use x86_64::VirtAddr;
use x86_64::instructions::segmentation::{CS, DS, Segment};
use x86_64::instructions::tables::load_tss;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
const STACK_SIZE: usize = 1024 * 8;
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
pub const PAGE_FAULT_IST_INDEX: u16 = 1;
pub const GENERAL_PROTECTION_FAULT_IST_INDEX: u16 = 2;
lazy_static! {
static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new();
tss.privilege_stack_table[0] = {
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
VirtAddr::from_ptr(unsafe { &STACK }) + STACK_SIZE
};
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
VirtAddr::from_ptr(unsafe { &STACK }) + STACK_SIZE
};
tss.interrupt_stack_table[PAGE_FAULT_IST_INDEX as usize] = {
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
VirtAddr::from_ptr(unsafe { &STACK }) + STACK_SIZE
};
tss.interrupt_stack_table[GENERAL_PROTECTION_FAULT_IST_INDEX as usize] = {
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
VirtAddr::from_ptr(unsafe { &STACK }) + STACK_SIZE
};
tss
};
}
lazy_static! {
pub static ref GDT: (GlobalDescriptorTable, Selectors) = {
let mut gdt = GlobalDescriptorTable::new();
let tss = gdt.add_entry(Descriptor::tss_segment(&TSS));
let code = gdt.add_entry(Descriptor::kernel_code_segment());
let data = gdt.add_entry(Descriptor::kernel_data_segment());
let user_code = gdt.add_entry(Descriptor::user_code_segment());
let user_data = gdt.add_entry(Descriptor::user_data_segment());
(gdt, Selectors { tss, code, data, user_code, user_data })
};
}
pub struct Selectors {
tss: SegmentSelector,
code: SegmentSelector,
data: SegmentSelector,
pub user_code: SegmentSelector,
pub user_data: SegmentSelector,
}
pub fn init() {
GDT.0.load();
unsafe {
CS::set_reg(GDT.1.code);
DS::set_reg(GDT.1.data);
load_tss(GDT.1.tss);
}
}