-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use lazy_static::lazy_static; | ||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector}; | ||
use x86_64::structures::tss::TaskStateSegment; | ||
use x86_64::VirtAddr; | ||
|
||
struct Selectors { | ||
code_selector: SegmentSelector, | ||
tss_selector: SegmentSelector, | ||
} | ||
|
||
lazy_static! { | ||
static ref GDT: (GlobalDescriptorTable, Selectors) = { | ||
let mut gdt = GlobalDescriptorTable::new(); | ||
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment()); | ||
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS)); | ||
( | ||
gdt, | ||
Selectors { | ||
code_selector, | ||
tss_selector, | ||
}, | ||
) | ||
}; | ||
} | ||
|
||
pub fn init() { | ||
use x86_64::instructions::segmentation::set_cs; | ||
use x86_64::instructions::tables::load_tss; | ||
|
||
GDT.0.load(); | ||
unsafe { | ||
set_cs(GDT.1.code_selector); | ||
load_tss(GDT.1.tss_selector); | ||
} | ||
} | ||
|
||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0; | ||
|
||
lazy_static! { | ||
static ref TSS: TaskStateSegment = { | ||
let mut tss = TaskStateSegment::new(); | ||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = { | ||
const STACK_SIZE: usize = 4096 * 5; | ||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; | ||
|
||
let stack_start = VirtAddr::from_ptr(unsafe { &STACK }); | ||
let stack_end = stack_start + STACK_SIZE; | ||
stack_end | ||
}; | ||
tss | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#![no_std] | ||
#![no_main] | ||
#![feature(abi_x86_interrupt)] | ||
|
||
use core::panic::PanicInfo; | ||
use k_os::{exit_qemu, serial_print, serial_println, QemuExitCode}; | ||
use lazy_static::lazy_static; | ||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn _start() -> ! { | ||
serial_print!("stack_overflow::stack_overflow...\t"); | ||
|
||
k_os::gdt::init(); | ||
// Use custom IDT to load custom fault handler | ||
init_test_idt(); | ||
|
||
// trigger a stack overflow | ||
stack_overflow(); | ||
|
||
panic!("Execution continued after stack overflow"); | ||
} | ||
|
||
#[allow(unconditional_recursion)] | ||
fn stack_overflow() { | ||
stack_overflow(); // for each recursion, the return address is pushed | ||
volatile::Volatile::new(0).read(); // prevent tail recursion optimizations | ||
} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
k_os::test_panic_handler(info) | ||
} | ||
|
||
// fault handler for test to avoid normal panic | ||
lazy_static! { | ||
static ref TEST_IDT: InterruptDescriptorTable = { | ||
let mut idt = InterruptDescriptorTable::new(); | ||
unsafe { | ||
idt.double_fault | ||
.set_handler_fn(test_double_fault_handler) | ||
.set_stack_index(k_os::gdt::DOUBLE_FAULT_IST_INDEX); | ||
} | ||
|
||
idt | ||
}; | ||
} | ||
|
||
pub fn init_test_idt() { | ||
TEST_IDT.load(); | ||
} | ||
|
||
extern "x86-interrupt" fn test_double_fault_handler( | ||
_stack_frame: &mut InterruptStackFrame, | ||
_error_code: u64, | ||
) -> ! { | ||
serial_println!("[ok]"); | ||
// Exit qemu instead of panic | ||
exit_qemu(QemuExitCode::Success); | ||
loop {} | ||
} |