Compare commits

...

134 Commits

Author SHA1 Message Date
Philipp Oppermann
c407c26224 Run cargo fmt 2025-03-27 16:13:18 +01:00
Philipp Oppermann
5b088bffeb Update stack_overflow test to use unsafe no_mangle attribute
Required since Rust 2024 edition
2025-03-27 15:51:13 +01:00
Philipp Oppermann
e9ca6b83c4 Merge branch 'post-05' into post-06 2025-03-27 15:50:40 +01:00
Philipp Oppermann
3d94346f43 Merge branch 'post-05' into post-06 2025-03-27 15:13:48 +01:00
Philipp Oppermann
caa829db01 Merge branch 'post-05' into post-06 2025-03-24 11:09:24 +01:00
Philipp Oppermann
90d1551665 Merge branch 'post-05' into post-06 2025-02-10 12:36:07 +01:00
Philipp Oppermann
e9f9035f89 Merge branch 'post-05' into post-06 2025-02-10 11:16:21 +01:00
Philipp Oppermann
aa9d0fff74 Code: Use new &raw const operator instead of unsafe {& _} 2025-01-15 19:58:23 +01:00
Philipp Oppermann
61fc3c65bc Merge branch 'post-05' into post-06 2024-04-30 15:39:16 +02:00
Philipp Oppermann
27888e99b5 Merge branch 'post-05' into post-06 2024-02-16 15:59:40 +01:00
Philipp Oppermann
26222f866f Merge branch 'post-05' into post-06 2024-02-16 13:22:06 +01:00
Philipp Oppermann
d61a13bbad Merge branch 'post-05' into post-06 2024-02-16 13:14:09 +01:00
Philipp Oppermann
8b7d009ba5 Merge branch 'post-05' into post-06 2024-02-16 12:39:56 +01:00
Philipp Oppermann
431a956388 Merge branch 'post-05' into post-06 2024-01-28 11:45:38 +01:00
Philipp Oppermann
698ccaf6d7 Merge branch 'post-05' into post-06 2022-09-25 11:32:07 +02:00
ruhuang
b72c457481 Update post-06 to set CS register with non-deprecated function (#1099)
Set CS register with non-deprecated function
2022-04-08 10:05:21 +02:00
Philipp Oppermann
4a684a06b1 Merge branch 'post-05' into post-06 2022-01-18 10:06:44 +01:00
Philipp Oppermann
a9d6066fb7 Merge branch 'post-05' into post-06 2021-12-22 09:27:53 +01:00
Philipp Oppermann
8b6a53973b Merge branch 'post-05' into post-06 2021-12-21 13:56:10 +01:00
Philipp Oppermann
f0ed4260e1 Merge branch 'post-05' into post-06 2021-08-22 14:44:44 +02:00
Philipp Oppermann
6c97986b38 Merge branch 'post-05' into post-06 2021-05-21 19:56:04 +02:00
Philipp Oppermann
1990d09e80 Merge branch 'post-05' into post-06 2021-05-20 14:33:10 +02:00
Philipp Oppermann
d55fa1ee24 Adjust post-06 to take InterruptStackFrame by value 2021-05-17 11:46:24 +02:00
Philipp Oppermann
5ae0f13fa4 Merge branch 'post-05' into post-06 2021-05-17 11:44:50 +02:00
Philipp Oppermann
91f75922ca Merge branch 'post-05' into post-06 2021-05-17 11:41:30 +02:00
Philipp Oppermann
a7943b8d05 Merge branch 'post-05' into post-06 2021-05-17 11:30:02 +02:00
Philipp Oppermann
69c22017ce Merge branch 'post-05' into post-06 2021-05-17 11:29:23 +02:00
Philipp Oppermann
9bc237ac70 Merge branch 'post-05' into post-06 2021-03-10 09:23:02 +01:00
Philipp Oppermann
4815290f89 Merge branch 'post-05' into post-06 2021-03-10 09:15:58 +01:00
Philipp Oppermann
79a05fb802 Merge branch 'post-05' into post-06 2021-03-10 09:13:39 +01:00
Philipp Oppermann
b74317b35a Merge branch 'post-05' into post-06 2021-03-10 08:53:42 +01:00
Philipp Oppermann
4f9ec9427c Merge branch 'post-05' into post-06 2021-02-02 11:06:37 +01:00
Philipp Oppermann
01701384f0 Merge branch 'post-05' into post-06 2021-02-02 10:52:06 +01:00
Philipp Oppermann
ab691c47d7 Merge branch 'post-05' into post-06 2020-11-22 13:46:58 +01:00
Philipp Oppermann
31c0412d0d Merge branch 'post-05' into post-06 2020-10-02 23:35:26 +02:00
Philipp Oppermann
f7198d2b00 Merge branch 'post-05' into post-06 2020-09-29 12:22:06 +02:00
Philipp Oppermann
de8d1cb632 Merge branch 'post-05' into post-06 2020-09-29 12:19:54 +02:00
Philipp Oppermann
b471610fa3 Merge branch 'post-05' into post-06 2020-09-24 10:27:12 +02:00
Philipp Oppermann
9a7d089cf7 Merge branch 'post-05' into post-06 2020-09-24 09:09:39 +02:00
Philipp Oppermann
7056923ef7 Merge branch 'post-05' into post-06 2020-08-16 18:03:09 +02:00
Philipp Oppermann
817e36c064 Increase double fault stack size 2020-08-16 17:57:43 +02:00
Philipp Oppermann
18bc113726 Merge branch 'post-05' into post-06 2020-07-17 15:57:35 +02:00
Philipp Oppermann
70458b6f6f Merge branch 'post-05' into post-06 2020-07-17 11:48:35 +02:00
Philipp Oppermann
51f8e413d5 Merge branch 'post-05' into post-06 2020-07-17 11:28:04 +02:00
Philipp Oppermann
1876a78058 Merge branch 'post-05' into post-06 2020-07-17 11:23:28 +02:00
Philipp Oppermann
0dd32e2680 Merge pull request #818 from phil-opp/post-06-prevent-tail-recursion
Do a volatile read in stack_overflow test to avoid tail recursion
2020-06-08 12:29:58 +02:00
Philipp Oppermann
5735cb8ec0 Do a volatile read in stack_overflow test to avoid tail recursion
This prevents the compiler from transforming the tail recursive function into a loop, which does not lead to a stack overflow. It also avoids the LLVM bug where functions with side-effect-free endless loops are removed. Thus, the test now also works in `--release` mode.
2020-06-08 12:17:06 +02:00
Philipp Oppermann
12d94fd2e9 Adjust stack_overflow test print message 2020-06-08 12:01:04 +02:00
Philipp Oppermann
8c10bbb096 Merge branch 'post-05' into post-06 2020-06-08 11:57:12 +02:00
Philipp Oppermann
f9446883e2 Merge branch 'post-05' into post-06 2020-05-20 14:56:19 +02:00
Philipp Oppermann
2bf84f60e9 Merge branch 'post-05' into post-06 2020-05-20 14:25:56 +02:00
Philipp Oppermann
bfed1f44e2 Merge branch 'post-05' into post-06 2020-05-20 14:06:06 +02:00
Philipp Oppermann
8a4f3ab2a8 Merge branch 'post-05' into post-06 2020-03-08 14:39:37 +01:00
Philipp Oppermann
dbe1353fd2 Merge branch 'post-05' into post-06 2020-03-08 14:27:42 +01:00
Philipp Oppermann
16afb268f8 Merge branch 'post-05' into post-06 2020-02-26 12:46:19 +01:00
Philipp Oppermann
c211412ce2 Update Github Actions badge for post-06 2019-12-12 09:30:51 +01:00
Philipp Oppermann
93d3de6feb Merge branch 'post-05' into post-06 2019-12-12 09:30:37 +01:00
Philipp Oppermann
651c629ffd Merge branch 'post-05' into post-06 2019-12-12 09:23:30 +01:00
Philipp Oppermann
8d8a7e0973 Merge branch 'post-05' into post-06 2019-12-10 17:31:24 +01:00
Philipp Oppermann
423b6e49cc Merge pull request #702 from phil-opp/post-06-fix
Make double fault handlers diverging
2019-12-10 17:17:00 +01:00
Philipp Oppermann
d1cf8000a3 Make double fault handlers diverging
Required by x86_64 0.8.0.
2019-12-10 17:02:46 +01:00
Philipp Oppermann
16c499f966 Merge branch 'post-05' into post-06 2019-12-10 16:55:41 +01:00
Philipp Oppermann
9d6ba0d31e Merge branch 'post-05' into post-06 2019-11-28 14:06:28 +01:00
Philipp Oppermann
590585da33 Merge branch 'post-05' into post-06 2019-11-25 13:24:09 +01:00
Philipp Oppermann
a604e1fa14 Merge branch 'post-06' of github.com:phil-opp/blog_os into post-06 2019-11-22 16:04:09 +01:00
Philipp Oppermann
05c3a17c49 Merge branch 'post-05' into post-06 2019-11-22 15:57:18 +01:00
Philipp Oppermann
3470636653 Use panic! instead of println! + loop in double fault handler (#687)
This avoids an endless loop when a double fault occurs while running `cargo xtest`.
2019-11-22 15:56:56 +01:00
Philipp Oppermann
1a1ce5120f Merge branch 'post-05' into post-06 2019-10-08 19:44:38 +02:00
Philipp Oppermann
78fdba7606 Merge branch 'post-05' into post-06 2019-09-25 13:20:13 +02:00
Philipp Oppermann
8d6915beba Merge branch 'post-05' into post-06 2019-09-13 17:43:25 +02:00
Philipp Oppermann
0eaaa3ff2b Merge branch 'post-05' into post-06 2019-09-13 10:53:50 +02:00
Philipp Oppermann
086304c9c4 Merge branch 'post-05' into post-06 2019-09-11 13:18:19 +02:00
Philipp Oppermann
2e27b91220 Merge branch 'post-05' into post-06 2019-09-11 11:21:15 +02:00
Philipp Oppermann
cb77074476 Merge branch 'post-05' into post-06 2019-09-11 10:58:02 +02:00
Philipp Oppermann
d0afb95b50 Merge branch 'post-05' into post-06 2019-09-11 10:32:57 +02:00
Philipp Oppermann
1b0378f87e Merge branch 'post-05' into post-06 2019-09-11 10:12:46 +02:00
Philipp Oppermann
b91f71784d Merge branch 'post-05' into post-06 2019-09-11 10:11:25 +02:00
Philipp Oppermann
6316a0e1df Merge branch 'post-05' into post-06 2019-09-11 10:02:41 +02:00
Philipp Oppermann
38cfb5e324 Merge branch 'post-05' into post-06 2019-09-10 11:12:54 +02:00
Philipp Oppermann
a9921e9448 Merge branch 'post-05' into post-06 2019-09-10 10:21:14 +02:00
Philipp Oppermann
9770d481f2 Merge branch 'post-05' into post-06 2019-08-07 12:40:04 +02:00
Philipp Oppermann
7baa3b0155 Merge branch 'post-05' into post-06 2019-07-22 10:36:23 +02:00
Philipp Oppermann
08bf4405a9 Merge branch 'post-05' into post-06 2019-07-18 10:17:05 +02:00
Philipp Oppermann
9d5b5f33f1 Merge branch 'post-05' into post-06 2019-06-23 18:07:34 +02:00
Philipp Oppermann
9b4b810bd3 Merge branch 'post-05' into post-06 2019-06-17 17:28:32 +02:00
Philipp Oppermann
7a45083975 Merge branch 'post-05' into post-06 2019-05-23 12:39:35 +02:00
Philipp Oppermann
01040fc643 Merge branch 'post-05' into post-06 2019-05-09 14:59:55 +02:00
Philipp Oppermann
5a68816b45 Merge branch 'post-05' into post-06 2019-05-03 18:36:45 +02:00
Philipp Oppermann
111c3e1029 Comment out stack_overflow() call to keep cargo xtest working 2019-04-26 15:48:59 +02:00
Philipp Oppermann
70d981e179 Simplify stack_overflow test 2019-04-26 15:14:38 +02:00
Philipp Oppermann
ee3d2219ab Disable test harness for stack_overflow test 2019-04-26 15:14:38 +02:00
Philipp Oppermann
c5a39e0902 Add a stack overflow integration test 2019-04-26 15:14:38 +02:00
Philipp Oppermann
4774893a93 Call gdt::init from blog_os::init 2019-04-26 15:14:38 +02:00
Philipp Oppermann
3c59321b54 Remove old bootimage test integration test 2019-04-26 15:14:38 +02:00
Philipp Oppermann
f5345db3e6 Update post number for Readme badge 2019-04-26 15:11:49 +02:00
Philipp Oppermann
94622ebfd5 Merge branch 'post-05-new' into post-06-new-rebased 2019-04-26 15:11:24 +02:00
Philipp Oppermann
82014268da Merge branch 'post-05-new' into post-06-new-rebased 2019-04-26 15:05:55 +02:00
Philipp Oppermann
e0d68fee24 Merge branch 'post-06' into post-07 2019-04-25 11:08:11 +02:00
Philipp Oppermann
6f623dc7b4 Merge branch 'post-06' into post-07 2019-04-11 15:32:21 +02:00
Philipp Oppermann
dc1f862177 Merge branch 'post-06' into post-07 2019-04-06 17:35:17 +02:00
Philipp Oppermann
fdf0fe617b Merge branch 'post-06' into post-07 2019-04-06 17:31:13 +02:00
Philipp Oppermann
23fcb9f20b Merge branch 'post-06' into post-07 2019-04-06 16:49:01 +02:00
Philipp Oppermann
ffb76038bc Merge branch 'post-06' into post-07
# Conflicts:
#	README.md
2019-04-03 10:46:46 +02:00
Philipp Oppermann
f42461b99b Merge branch 'post-06' into post-07 2019-03-26 13:26:56 +01:00
Philipp Oppermann
701823742e Merge branch 'post-06' into post-07 2019-03-14 11:18:22 +01:00
Philipp Oppermann
b533bf6596 Merge branch 'post-06' into post-07 2019-03-12 17:48:11 +01:00
Philipp Oppermann
1ed76411f7 Fix remaining use of ExceptionStackFrame in test 2019-03-09 14:21:03 +01:00
Philipp Oppermann
8e5b6a3e3f Update post-07 code for changes in x86_64 0.5.0 2019-03-09 12:34:49 +01:00
Philipp Oppermann
be0ed3a54d Merge branch 'post-06' into post-07 2019-03-09 12:32:03 +01:00
Philipp Oppermann
f5b6094521 Merge branch 'post-06' into post-07 2019-02-25 16:37:30 +01:00
Philipp Oppermann
38bd9a65b6 Merge branch 'post-06' into post-07 2019-02-07 18:46:32 +01:00
Philipp Oppermann
b3e94470e4 Merge branch 'post-06' into post-07 2019-02-07 16:15:10 +01:00
Philipp Oppermann
7b01e9c862 Merge branch 'post-06' into post-07 2019-02-05 15:23:49 +01:00
Philipp Oppermann
5452e0f455 Merge branch 'post-06' into post-07 2019-02-05 14:59:15 +01:00
Philipp Oppermann
202902f30d Merge branch 'post-06' into post-07 2019-02-05 10:46:08 +01:00
Philipp Oppermann
3bb30fd1e8 Merge branch 'post-06' into post-07 2019-02-05 10:45:12 +01:00
Philipp Oppermann
41e36b4afa Merge branch 'post-06' into post-07 2019-01-29 12:16:54 +01:00
Philipp Oppermann
f76ec1ae32 Only set double fault handler once 2019-01-28 11:50:59 +01:00
Philipp Oppermann
efe92e5004 Merge branch 'post-06' into post-07 2019-01-28 11:44:14 +01:00
Philipp Oppermann
233943310c Update Readme for Double Faults post 2019-01-27 17:12:21 +01:00
Philipp Oppermann
ef6d69f82b Merge branch 'post-06' into post-07 2019-01-27 17:11:38 +01:00
Philipp Oppermann
637b517f47 Merge branch 'post-06' into post-07 2019-01-27 16:33:54 +01:00
Philipp Oppermann
5802c97938 Merge branch 'z_post_06' into z_post_07 2019-01-27 15:46:59 +01:00
Philipp Oppermann
29ee6b15dd Merge branch 'z_post_06' into z_post_07 2019-01-27 14:28:55 +01:00
Philipp Oppermann
90ad333e90 Merge branch 'z_post_06' into z_post_07 2019-01-25 14:46:29 +01:00
Philipp Oppermann
afe8dc3dcf Add stack overflow integration test 2019-01-25 14:45:02 +01:00
Philipp Oppermann
8bb0187f35 Run rustfmt 2019-01-25 14:44:42 +01:00
Philipp Oppermann
d8edfccca4 Set the stack_index for the double fault handler 2019-01-25 14:01:29 +01:00
Philipp Oppermann
ff25a7d86c Reload code and TSS selector registers 2019-01-25 13:59:09 +01:00
Philipp Oppermann
b5202e26a1 Load the GDT 2019-01-25 13:57:40 +01:00
Philipp Oppermann
ea1b031fd3 Create a static GDT 2019-01-25 13:57:08 +01:00
Philipp Oppermann
db4e879c34 Create a new GDT module and a static TSS 2019-01-25 13:56:40 +01:00
Philipp Oppermann
7fd29c9cbe Trigger a double fault through a stack overflow -> triple fault 2019-01-25 13:55:42 +01:00
Philipp Oppermann
218cb9399e Add a double fault handler and test it 2019-01-25 13:54:44 +01:00
7 changed files with 139 additions and 7 deletions

View File

@@ -8,6 +8,10 @@ edition = "2024"
name = "should_panic"
harness = false
[[test]]
name = "stack_overflow"
harness = false
[dependencies]
bootloader = "0.9"
volatile = "0.2.6"

View File

@@ -1,10 +1,10 @@
# Blog OS (CPU Exceptions)
# Blog OS (Double Faults)
[![Build Status](https://github.com/phil-opp/blog_os/workflows/Code/badge.svg?branch=post-05)](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-05)
[![Build Status](https://github.com/phil-opp/blog_os/workflows/Code/badge.svg?branch=post-06)](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.**

52
src/gdt.rs Normal file
View File

@@ -0,0 +1,52 @@
use lazy_static::lazy_static;
use x86_64::VirtAddr;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
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::{CS, Segment};
use x86_64::instructions::tables::load_tss;
GDT.0.load();
unsafe {
CS::set_reg(GDT.1.code_selector);
load_tss(GDT.1.tss_selector);
}
}

View File

@@ -1,4 +1,4 @@
use crate::println;
use crate::{gdt, println};
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
@@ -6,6 +6,11 @@ lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
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
};
}
@@ -18,6 +23,13 @@ extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
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]
fn test_breakpoint_exception() {
// invoke a breakpoint exception

View File

@@ -7,11 +7,13 @@
use core::panic::PanicInfo;
pub mod gdt;
pub mod interrupts;
pub mod serial;
pub mod vga_buffer;
pub fn init() {
gdt::init();
interrupts::init_idt();
}
pub trait Testable {

View File

@@ -13,8 +13,12 @@ pub extern "C" fn _start() -> ! {
blog_os::init();
// invoke a breakpoint exception
x86_64::instructions::interrupts::int3();
fn stack_overflow() {
stack_overflow(); // for each recursion, the return address is pushed
}
// uncomment line below to trigger a stack overflow
// stack_overflow();
#[cfg(test)]
test_main();

58
tests/stack_overflow.rs Normal file
View File

@@ -0,0 +1,58 @@
#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println};
use core::panic::PanicInfo;
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
#[unsafe(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)
}