mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Reset source code to master again
(in order to follow the post and test the exact code from it)
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
use bit_field::BitField;
|
||||
use x86::bits64::task::TaskStateSegment;
|
||||
use x86::shared::segmentation::SegmentSelector;
|
||||
use x86::shared::PrivilegeLevel;
|
||||
|
||||
pub struct Gdt {
|
||||
table: [u64; 8],
|
||||
current_offset: usize,
|
||||
}
|
||||
|
||||
impl Gdt {
|
||||
pub fn new() -> Gdt {
|
||||
Gdt {
|
||||
table: [0; 8],
|
||||
current_offset: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, value: u64) -> usize {
|
||||
if self.current_offset < self.table.len() {
|
||||
let offset = self.current_offset;
|
||||
self.table[offset] = value;
|
||||
self.current_offset += 1;
|
||||
offset
|
||||
} else {
|
||||
panic!("GDT full");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector {
|
||||
let index = match entry {
|
||||
Descriptor::UserSegment(value) => self.push(value),
|
||||
Descriptor::SystemSegment(value_low, value_high) => {
|
||||
let index = self.push(value_low);
|
||||
self.push(value_high);
|
||||
index
|
||||
}
|
||||
};
|
||||
SegmentSelector::new(index as u16, PrivilegeLevel::Ring0)
|
||||
}
|
||||
|
||||
pub fn load(&'static self) {
|
||||
use x86::shared::dtables::{DescriptorTablePointer, lgdt};
|
||||
use core::mem::size_of;
|
||||
|
||||
let ptr = DescriptorTablePointer {
|
||||
base: self.table.as_ptr() as *const ::x86::shared::segmentation::SegmentDescriptor,
|
||||
limit: (self.table.len() * size_of::<u64>() - 1) as u16,
|
||||
};
|
||||
|
||||
unsafe { lgdt(&ptr) };
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Descriptor {
|
||||
UserSegment(u64),
|
||||
SystemSegment(u64, u64),
|
||||
}
|
||||
|
||||
impl Descriptor {
|
||||
pub fn kernel_code_segment() -> Descriptor {
|
||||
let flags = USER_SEGMENT | PRESENT | EXECUTABLE | LONG_MODE;
|
||||
Descriptor::UserSegment(flags.bits())
|
||||
}
|
||||
|
||||
pub fn tss_segment(tss: &'static TaskStateSegment) -> Descriptor {
|
||||
use core::mem::size_of;
|
||||
|
||||
let ptr = tss as *const _ as u64;
|
||||
|
||||
let mut low = PRESENT.bits();
|
||||
low.set_range(0..16, (size_of::<TaskStateSegment>() - 1) as u64);
|
||||
low.set_range(16..40, ptr.get_range(0..24));
|
||||
low.set_range(40..44, 0b1001); // type: available 64-bit tss
|
||||
|
||||
let mut high = 0;
|
||||
high.set_range(0..32, ptr.get_range(32..64));
|
||||
|
||||
Descriptor::SystemSegment(low, high)
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
flags DescriptorFlags: u64 {
|
||||
const CONFORMING = 1 << 42,
|
||||
const EXECUTABLE = 1 << 43,
|
||||
const USER_SEGMENT = 1 << 44,
|
||||
const PRESENT = 1 << 47,
|
||||
const LONG_MODE = 1 << 53,
|
||||
}
|
||||
}
|
||||
@@ -102,14 +102,14 @@ impl EntryOptions {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn set_privilege_level(&mut self, dpl: u8) -> &mut Self {
|
||||
self.0.set_range(13..15, dpl.into());
|
||||
pub fn set_privilege_level(&mut self, dpl: u16) -> &mut Self {
|
||||
self.0.set_range(13..15, dpl);
|
||||
self
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn set_stack_index(&mut self, index: u8) -> &mut Self {
|
||||
self.0.set_range(0..3, (index + 1).into());
|
||||
pub fn set_stack_index(&mut self, index: u16) -> &mut Self {
|
||||
self.0.set_range(0..3, index);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use spin::Once;
|
||||
use memory::MemoryController;
|
||||
use x86::bits64::task::TaskStateSegment;
|
||||
|
||||
mod idt;
|
||||
mod gdt;
|
||||
|
||||
macro_rules! save_scratch_registers {
|
||||
() => {
|
||||
@@ -98,46 +93,13 @@ lazy_static! {
|
||||
idt.set_handler(0, handler!(divide_by_zero_handler));
|
||||
idt.set_handler(3, handler!(breakpoint_handler));
|
||||
idt.set_handler(6, handler!(invalid_opcode_handler));
|
||||
idt.set_handler(8, handler_with_error_code!(double_fault_handler))
|
||||
.set_stack_index(DOUBLE_FAULT_IST_INDEX as u8);
|
||||
idt.set_handler(14, handler_with_error_code!(page_fault_handler));
|
||||
|
||||
idt
|
||||
};
|
||||
}
|
||||
|
||||
static TSS: Once<TaskStateSegment> = Once::new();
|
||||
static GDT: Once<gdt::Gdt> = Once::new();
|
||||
const DOUBLE_FAULT_IST_INDEX: usize = 0;
|
||||
|
||||
pub fn init(memory_controller: &mut MemoryController) {
|
||||
use x86::shared::segmentation::{SegmentSelector, set_cs};
|
||||
use x86::shared::task::load_tr;
|
||||
|
||||
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.ist[DOUBLE_FAULT_IST_INDEX] = double_fault_stack.top() as u64;
|
||||
tss
|
||||
});
|
||||
|
||||
let mut code_selector = SegmentSelector::empty();
|
||||
let mut tss_selector = SegmentSelector::empty();
|
||||
let gdt = GDT.call_once(|| {
|
||||
let mut gdt = gdt::Gdt::new();
|
||||
tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
|
||||
code_selector = gdt.add_entry(gdt::Descriptor::kernel_code_segment());
|
||||
gdt
|
||||
});
|
||||
gdt.load();
|
||||
|
||||
unsafe {
|
||||
set_cs(code_selector);
|
||||
load_tr(tss_selector);
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
@@ -188,8 +150,3 @@ extern "C" fn page_fault_handler(stack_frame: &ExceptionStackFrame, error_code:
|
||||
stack_frame);
|
||||
loop {}
|
||||
}
|
||||
|
||||
extern "C" fn double_fault_handler(stack_frame: &ExceptionStackFrame, _error_code: u64) {
|
||||
println!("\nEXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
||||
loop {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user