This repository was archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
172 lines (145 loc) · 4.8 KB
/
mod.rs
File metadata and controls
172 lines (145 loc) · 4.8 KB
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
// TODO: Figure out why moving the exception handlers to another module causes the keyboard to
// seemingly break.
use memory::MemoryController;
use x86_64::structures::tss::TaskStateSegment;
use x86_64::structures::idt::{Idt, ExceptionStackFrame, PageFaultErrorCode};
use spin::Once;
use device::pic::PICS;
use device::keyboard::read_char;
use utils::disable_interrupts_and_then;
mod gdt;
const DOUBLE_FAULT_IST_INDEX: usize = 0;
lazy_static! {
static ref IDT: Idt = {
let mut idt = Idt::new();
idt.divide_by_zero.set_handler_fn(divide_by_zero_handler);
idt.breakpoint.set_handler_fn(breakpoint_handler);
idt.invalid_opcode.set_handler_fn(invalid_opcode_handler);
idt.page_fault.set_handler_fn(page_fault_handler);
idt.general_protection_fault.set_handler_fn(gpf_handler);
unsafe {
idt.double_fault.set_handler_fn(double_fault_handler)
.set_stack_index(DOUBLE_FAULT_IST_INDEX as u16);
}
idt.interrupts[0].set_handler_fn(timer_handler);
idt.interrupts[1].set_handler_fn(keyboard_handler);
idt
};
}
static TSS: Once<TaskStateSegment> = Once::new();
static GDT: Once<gdt::Gdt> = Once::new();
pub fn init(memory_controller: &mut MemoryController) {
use x86_64::structures::gdt::SegmentSelector;
use x86_64::instructions::segmentation::set_cs;
use x86_64::instructions::tables::load_tss;
use x86_64::VirtualAddress;
let double_fault_stack = memory_controller
.alloc_stack(1)
.expect("could not allocate double fault stack");
let tss = TSS.call_once(|| {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] =
VirtualAddress(double_fault_stack.top());
//TODO allocate privelege stacks.
tss
});
let mut code_selector = SegmentSelector(0);
let mut tss_selector = SegmentSelector(0);
let gdt = GDT.call_once(|| {
let mut gdt = gdt::Gdt::new();
code_selector = gdt.add_entry(gdt::Descriptor::kernel_code_segment());
tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
gdt
});
gdt.load();
println!("[ OK ] GDT.");
unsafe {
// reload code segment register
set_cs(code_selector);
// load TSS
load_tss(tss_selector);
}
IDT.load();
println!("[ OK ] IDT.")
}
//IRQs.
pub extern "x86-interrupt" fn timer_handler(_stack_frame: &mut ExceptionStackFrame) {
use core::sync::atomic::Ordering;
use device::pit::PIT_TICKS;
use task::{SCHEDULER, Scheduling};
unsafe { PICS.lock().notify_end_of_interrupt(0x20) };
if PIT_TICKS.fetch_add(1, Ordering::SeqCst) >= 10 {
PIT_TICKS.store(0, Ordering::SeqCst);
unsafe {
disable_interrupts_and_then(|| {
SCHEDULER.resched();
});
}
}
}
pub extern "x86-interrupt" fn keyboard_handler(_stack_frame: &mut ExceptionStackFrame) {
disable_interrupts_and_then(|| {
if let Some(c) = read_char() {
print!("{}", c);
}
unsafe { PICS.lock().notify_end_of_interrupt(0x21) };
});
}
pub extern "x86-interrupt" fn divide_by_zero_handler(stack_frame: &mut ExceptionStackFrame) {
disable_interrupts_and_then(|| {
println!("\nEXCEPTION: DIVIDE BY ZERO\n{:#?}", stack_frame);
loop {}
});
}
pub extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut ExceptionStackFrame) {
println!(
"\nEXCEPTION: BREAKPOINT at {:#x}\n{:#?}",
stack_frame.instruction_pointer,
stack_frame
);
}
pub extern "x86-interrupt" fn invalid_opcode_handler(stack_frame: &mut ExceptionStackFrame) {
disable_interrupts_and_then(|| {
println!(
"\nEXCEPTION: INVALID OPCODE at {:#x}\n{:#?}",
stack_frame.instruction_pointer,
stack_frame
);
loop {}
});
}
pub extern "x86-interrupt" fn page_fault_handler(
stack_frame: &mut ExceptionStackFrame,
error_code: PageFaultErrorCode,
) {
disable_interrupts_and_then(|| {
use x86_64::registers::control_regs;
println!(
"\nEXCEPTION: PAGE FAULT while accessing {:#x}\nerror code: \
{:?}\n{:#?}",
control_regs::cr2(),
error_code,
stack_frame
);
loop {}
});
}
pub extern "x86-interrupt" fn double_fault_handler(
stack_frame: &mut ExceptionStackFrame,
_error_code: u64,
) {
disable_interrupts_and_then(|| {
println!("\nEXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
loop {}
});
}
pub extern "x86-interrupt" fn gpf_handler(
stack_frame: &mut ExceptionStackFrame,
_error_code: u64,
)
{
disable_interrupts_and_then(|| {
println!("\nEXCEPTION: GPF\n{:#?}", stack_frame);
loop {}
});
}