add timer interrupt

This commit is contained in:
mii443
2025-04-13 16:17:59 +09:00
parent 561003d833
commit 89e8047bb1
7 changed files with 97 additions and 17 deletions

26
Cargo.lock generated
View File

@ -41,10 +41,20 @@ version = "0.1.0"
dependencies = [
"bootloader",
"lazy_static",
"pic8259",
"spin 0.5.2",
"uart_16550",
"volatile 0.2.7",
"x86_64",
"x86_64 0.14.13",
]
[[package]]
name = "pic8259"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62d9a86c292b165f757e47e7fd66855def189b2564609bc4203727b27c33db22"
dependencies = [
"x86_64 0.15.2",
]
[[package]]
@ -73,7 +83,7 @@ checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e"
dependencies = [
"bitflags 1.3.2",
"rustversion",
"x86_64",
"x86_64 0.14.13",
]
[[package]]
@ -99,3 +109,15 @@ dependencies = [
"rustversion",
"volatile 0.4.6",
]
[[package]]
name = "x86_64"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
dependencies = [
"bit_field",
"bitflags 2.9.0",
"rustversion",
"volatile 0.4.6",
]

View File

@ -9,6 +9,7 @@ volatile = "0.2.6"
spin = "0.5.2"
x86_64 = "0.14.2"
uart_16550 = "0.2.0"
pic8259 = "0.11.0"
[dependencies.lazy_static]
version = "1.0"

View File

@ -1,7 +1,8 @@
use lazy_static::lazy_static;
use pic8259::ChainedPics;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
use crate::{gdt, println};
use crate::{gdt, print, println};
lazy_static! {
static ref IDT: InterruptDescriptorTable = {
@ -12,6 +13,7 @@ lazy_static! {
.set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
}
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
idt
};
@ -32,6 +34,37 @@ extern "x86-interrupt" fn double_fault_handler(
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
}
pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
pub static PICS: spin::Mutex<ChainedPics> =
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum InterruptIndex {
Timer = PIC_1_OFFSET,
}
impl InterruptIndex {
fn as_u8(self) -> u8 {
self as u8
}
fn as_usize(self) -> usize {
usize::from(self.as_u8())
}
}
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
print!(".");
unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
}
}
#[test_case]
fn test_breakpoint_exception() {
x86_64::instructions::interrupts::int3();

View File

@ -39,7 +39,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
hlt_loop();
}
#[cfg(test)]
@ -47,7 +47,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! {
pub extern "C" fn _start() -> ! {
init();
test_main();
loop {}
hlt_loop();
}
#[cfg(test)]
@ -72,7 +72,17 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
}
}
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}
pub fn init() {
gdt::init();
interrupts::init_idt();
unsafe {
interrupts::PICS.lock().initialize();
}
x86_64::instructions::interrupts::enable();
}

View File

@ -11,7 +11,7 @@ use nel_os::println;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
nel_os::hlt_loop();
}
#[cfg(test)]
@ -29,5 +29,5 @@ pub extern "C" fn _start() -> ! {
#[cfg(test)]
test_main();
loop {}
nel_os::hlt_loop();
}

View File

@ -13,10 +13,14 @@ lazy_static! {
#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL1
.lock()
.write_fmt(args)
.expect("Printing to serial failed");
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
SERIAL1
.lock()
.write_fmt(args)
.expect("Printing to serial failed");
});
}
#[macro_export]

View File

@ -26,7 +26,11 @@ macro_rules! println {
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
WRITER.lock().write_fmt(args).unwrap();
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
WRITER.lock().write_fmt(args).unwrap();
});
}
#[allow(dead_code)]
@ -157,10 +161,16 @@ fn test_println_many() {
#[test_case]
fn test_println_output() {
use core::fmt::Write;
use x86_64::instructions::interrupts;
let s = "Some test string that fits on a single line";
println!("{}", s);
for (i, c) in s.chars().enumerate() {
let screen_char = WRITER.lock().buffer.chars[BUFFER_HEIGHT - 2][i].read();
assert_eq!(char::from(screen_char.ascii_character), c);
}
interrupts::without_interrupts(|| {
let mut writer = WRITER.lock();
writeln!(writer, "\n{}", s).expect("writeln failed");
for (i, c) in s.chars().enumerate() {
let screen_char = writer.buffer.chars[BUFFER_HEIGHT - 2][i].read();
assert_eq!(char::from(screen_char.ascii_character), c);
}
});
}