mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Compare commits
129 Commits
post-05
...
90d1551665
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90d1551665 | ||
|
|
e9f9035f89 | ||
|
|
aa9d0fff74 | ||
|
|
61fc3c65bc | ||
|
|
27888e99b5 | ||
|
|
26222f866f | ||
|
|
d61a13bbad | ||
|
|
8b7d009ba5 | ||
|
|
431a956388 | ||
|
|
698ccaf6d7 | ||
|
|
b72c457481 | ||
|
|
4a684a06b1 | ||
|
|
a9d6066fb7 | ||
|
|
8b6a53973b | ||
|
|
f0ed4260e1 | ||
|
|
6c97986b38 | ||
|
|
1990d09e80 | ||
|
|
d55fa1ee24 | ||
|
|
5ae0f13fa4 | ||
|
|
91f75922ca | ||
|
|
a7943b8d05 | ||
|
|
69c22017ce | ||
|
|
9bc237ac70 | ||
|
|
4815290f89 | ||
|
|
79a05fb802 | ||
|
|
b74317b35a | ||
|
|
4f9ec9427c | ||
|
|
01701384f0 | ||
|
|
ab691c47d7 | ||
|
|
31c0412d0d | ||
|
|
f7198d2b00 | ||
|
|
de8d1cb632 | ||
|
|
b471610fa3 | ||
|
|
9a7d089cf7 | ||
|
|
7056923ef7 | ||
|
|
817e36c064 | ||
|
|
18bc113726 | ||
|
|
70458b6f6f | ||
|
|
51f8e413d5 | ||
|
|
1876a78058 | ||
|
|
0dd32e2680 | ||
|
|
5735cb8ec0 | ||
|
|
12d94fd2e9 | ||
|
|
8c10bbb096 | ||
|
|
f9446883e2 | ||
|
|
2bf84f60e9 | ||
|
|
bfed1f44e2 | ||
|
|
8a4f3ab2a8 | ||
|
|
dbe1353fd2 | ||
|
|
16afb268f8 | ||
|
|
c211412ce2 | ||
|
|
93d3de6feb | ||
|
|
651c629ffd | ||
|
|
8d8a7e0973 | ||
|
|
423b6e49cc | ||
|
|
d1cf8000a3 | ||
|
|
16c499f966 | ||
|
|
9d6ba0d31e | ||
|
|
590585da33 | ||
|
|
a604e1fa14 | ||
|
|
05c3a17c49 | ||
|
|
3470636653 | ||
|
|
1a1ce5120f | ||
|
|
78fdba7606 | ||
|
|
8d6915beba | ||
|
|
0eaaa3ff2b | ||
|
|
086304c9c4 | ||
|
|
2e27b91220 | ||
|
|
cb77074476 | ||
|
|
d0afb95b50 | ||
|
|
1b0378f87e | ||
|
|
b91f71784d | ||
|
|
6316a0e1df | ||
|
|
38cfb5e324 | ||
|
|
a9921e9448 | ||
|
|
9770d481f2 | ||
|
|
7baa3b0155 | ||
|
|
08bf4405a9 | ||
|
|
9d5b5f33f1 | ||
|
|
9b4b810bd3 | ||
|
|
7a45083975 | ||
|
|
01040fc643 | ||
|
|
5a68816b45 | ||
|
|
111c3e1029 | ||
|
|
70d981e179 | ||
|
|
ee3d2219ab | ||
|
|
c5a39e0902 | ||
|
|
4774893a93 | ||
|
|
3c59321b54 | ||
|
|
f5345db3e6 | ||
|
|
94622ebfd5 | ||
|
|
82014268da | ||
|
|
e0d68fee24 | ||
|
|
6f623dc7b4 | ||
|
|
dc1f862177 | ||
|
|
fdf0fe617b | ||
|
|
23fcb9f20b | ||
|
|
ffb76038bc | ||
|
|
f42461b99b | ||
|
|
701823742e | ||
|
|
b533bf6596 | ||
|
|
1ed76411f7 | ||
|
|
8e5b6a3e3f | ||
|
|
be0ed3a54d | ||
|
|
f5b6094521 | ||
|
|
38bd9a65b6 | ||
|
|
b3e94470e4 | ||
|
|
7b01e9c862 | ||
|
|
5452e0f455 | ||
|
|
202902f30d | ||
|
|
3bb30fd1e8 | ||
|
|
41e36b4afa | ||
|
|
f76ec1ae32 | ||
|
|
efe92e5004 | ||
|
|
233943310c | ||
|
|
ef6d69f82b | ||
|
|
637b517f47 | ||
|
|
5802c97938 | ||
|
|
29ee6b15dd | ||
|
|
90ad333e90 | ||
|
|
afe8dc3dcf | ||
|
|
8bb0187f35 | ||
|
|
d8edfccca4 | ||
|
|
ff25a7d86c | ||
|
|
b5202e26a1 | ||
|
|
ea1b031fd3 | ||
|
|
db4e879c34 | ||
|
|
7fd29c9cbe | ||
|
|
218cb9399e |
58
Cargo.lock
generated
58
Cargo.lock
generated
@@ -4,21 +4,15 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bit_field"
|
name = "bit_field"
|
||||||
version = "0.10.2"
|
version = "0.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
|
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bitflags"
|
|
||||||
version = "2.9.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6a65b545ab31d687cff52899d4890855fec459eb6afe0da6417b8a18da87aa29"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "blog_os"
|
name = "blog_os"
|
||||||
@@ -26,7 +20,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bootloader",
|
"bootloader",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"spin 0.5.2",
|
"spin",
|
||||||
"uart_16550",
|
"uart_16550",
|
||||||
"volatile 0.2.7",
|
"volatile 0.2.7",
|
||||||
"x86_64",
|
"x86_64",
|
||||||
@@ -34,45 +28,32 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bootloader"
|
name = "bootloader"
|
||||||
version = "0.9.33"
|
version = "0.9.30"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7bdfddac270bbdd45903296bc1caf29a7fdce6b326aaf0bbab7f04c5f98b7447"
|
checksum = "8a9c8b93781debeb5bc44a12adc4be812aa9feb659d60eeafcd7e9bedb549561"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"spin 0.9.8",
|
"spin",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustversion"
|
|
||||||
version = "1.0.22"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spin"
|
name = "spin"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "spin"
|
|
||||||
version = "0.9.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uart_16550"
|
name = "uart_16550"
|
||||||
version = "0.2.19"
|
version = "0.2.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e"
|
checksum = "503a6c0e6d82daa87985e662d120c0176b09587c92a68db22781b28ae95405dd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags",
|
||||||
"rustversion",
|
|
||||||
"x86_64",
|
"x86_64",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -84,18 +65,17 @@ checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "volatile"
|
name = "volatile"
|
||||||
version = "0.4.6"
|
version = "0.4.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
|
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "x86_64"
|
name = "x86_64"
|
||||||
version = "0.14.13"
|
version = "0.14.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c101112411baafbb4bf8d33e4c4a80ab5b02d74d2612331c61e8192fc9710491"
|
checksum = "fb611915c917c6296d11e23f71ff1ecfe49c5766daba92cd3df52df6b58285b6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bit_field",
|
"bit_field",
|
||||||
"bitflags 2.9.2",
|
"bitflags",
|
||||||
"rustversion",
|
"volatile 0.4.4",
|
||||||
"volatile 0.4.6",
|
|
||||||
]
|
]
|
||||||
|
|||||||
19
Cargo.toml
19
Cargo.toml
@@ -2,12 +2,16 @@
|
|||||||
name = "blog_os"
|
name = "blog_os"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
||||||
edition = "2024"
|
edition = "2018"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "should_panic"
|
name = "should_panic"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "stack_overflow"
|
||||||
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bootloader = "0.9"
|
bootloader = "0.9"
|
||||||
volatile = "0.2.6"
|
volatile = "0.2.6"
|
||||||
@@ -19,19 +23,10 @@ uart_16550 = "0.2.0"
|
|||||||
version = "1.0"
|
version = "1.0"
|
||||||
features = ["spin_no_std"]
|
features = ["spin_no_std"]
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "blog_os"
|
|
||||||
test = true
|
|
||||||
bench = false
|
|
||||||
|
|
||||||
|
|
||||||
[package.metadata.bootimage]
|
[package.metadata.bootimage]
|
||||||
test-args = [
|
test-args = [
|
||||||
"-device",
|
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||||
"isa-debug-exit,iobase=0xf4,iosize=0x04",
|
"-display", "none"
|
||||||
"-serial",
|
|
||||||
"stdio",
|
|
||||||
"-display",
|
|
||||||
"none",
|
|
||||||
]
|
]
|
||||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Blog OS (CPU Exceptions)
|
# Blog OS (Double Faults)
|
||||||
|
|
||||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-05)
|
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-06)
|
||||||
|
|
||||||
This repository contains the source code for the [CPU Exceptions][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
This repository contains the source code for the [Double Faults][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||||
|
|
||||||
[post]: https://os.phil-opp.com/cpu-exceptions/
|
[post]: https://os.phil-opp.com/double-fault-exceptions/
|
||||||
|
|
||||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||||
|
|
||||||
|
|||||||
52
src/gdt.rs
Normal file
52
src/gdt.rs
Normal file
@@ -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;
|
||||||
|
|
||||||
|
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(&raw const STACK);
|
||||||
|
let stack_end = stack_start + STACK_SIZE;
|
||||||
|
stack_end
|
||||||
|
};
|
||||||
|
tss
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Selectors {
|
||||||
|
code_selector: SegmentSelector,
|
||||||
|
tss_selector: SegmentSelector,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
use x86_64::instructions::segmentation::{Segment, CS};
|
||||||
|
use x86_64::instructions::tables::load_tss;
|
||||||
|
|
||||||
|
GDT.0.load();
|
||||||
|
unsafe {
|
||||||
|
CS::set_reg(GDT.1.code_selector);
|
||||||
|
load_tss(GDT.1.tss_selector);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::println;
|
use crate::{gdt, println};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
||||||
|
|
||||||
@@ -6,6 +6,11 @@ lazy_static! {
|
|||||||
static ref IDT: InterruptDescriptorTable = {
|
static ref IDT: InterruptDescriptorTable = {
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
let mut idt = InterruptDescriptorTable::new();
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||||
|
unsafe {
|
||||||
|
idt.double_fault
|
||||||
|
.set_handler_fn(double_fault_handler)
|
||||||
|
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
||||||
|
}
|
||||||
idt
|
idt
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -18,6 +23,13 @@ extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
|||||||
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn double_fault_handler(
|
||||||
|
stack_frame: InterruptStackFrame,
|
||||||
|
_error_code: u64,
|
||||||
|
) -> ! {
|
||||||
|
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
||||||
|
}
|
||||||
|
|
||||||
#[test_case]
|
#[test_case]
|
||||||
fn test_breakpoint_exception() {
|
fn test_breakpoint_exception() {
|
||||||
// invoke a breakpoint exception
|
// invoke a breakpoint exception
|
||||||
|
|||||||
@@ -7,11 +7,13 @@
|
|||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
pub mod vga_buffer;
|
pub mod vga_buffer;
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
|
gdt::init();
|
||||||
interrupts::init_idt();
|
interrupts::init_idt();
|
||||||
}
|
}
|
||||||
pub trait Testable {
|
pub trait Testable {
|
||||||
@@ -62,7 +64,7 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
|
|||||||
|
|
||||||
/// Entry point for `cargo xtest`
|
/// Entry point for `cargo xtest`
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[unsafe(no_mangle)]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
init();
|
init();
|
||||||
test_main();
|
test_main();
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -7,14 +7,18 @@
|
|||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
blog_os::init();
|
blog_os::init();
|
||||||
|
|
||||||
// invoke a breakpoint exception
|
fn stack_overflow() {
|
||||||
x86_64::instructions::interrupts::int3();
|
stack_overflow(); // for each recursion, the return address is pushed
|
||||||
|
}
|
||||||
|
|
||||||
|
// uncomment line below to trigger a stack overflow
|
||||||
|
// stack_overflow();
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
test_main();
|
test_main();
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[unsafe(no_mangle)] // don't mangle the name of this function
|
#[no_mangle] // don't mangle the name of this function
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
test_main();
|
test_main();
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println};
|
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
should_fail();
|
should_fail();
|
||||||
serial_println!("[test did not panic]");
|
serial_println!("[test did not panic]");
|
||||||
|
|||||||
58
tests/stack_overflow.rs
Normal file
58
tests/stack_overflow.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(abi_x86_interrupt)]
|
||||||
|
|
||||||
|
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
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");
|
||||||
|
|
||||||
|
blog_os::gdt::init();
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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(blog_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: InterruptStackFrame,
|
||||||
|
_error_code: u64,
|
||||||
|
) -> ! {
|
||||||
|
serial_println!("[ok]");
|
||||||
|
exit_qemu(QemuExitCode::Success);
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
blog_os::test_panic_handler(info)
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
|
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
|
||||||
"arch": "x86_64",
|
"arch": "x86_64",
|
||||||
"target-endian": "little",
|
"target-endian": "little",
|
||||||
"target-pointer-width": 64,
|
"target-pointer-width": "64",
|
||||||
"target-c-int-width": 32,
|
"target-c-int-width": "32",
|
||||||
"os": "none",
|
"os": "none",
|
||||||
"executables": true,
|
"executables": true,
|
||||||
"linker-flavor": "ld.lld",
|
"linker-flavor": "ld.lld",
|
||||||
|
|||||||
Reference in New Issue
Block a user