From 46f4837cc75c46bbf4920b45c5a1560a87e7651e Mon Sep 17 00:00:00 2001 From: MeexReay Date: Fri, 31 Jan 2025 15:05:41 +0300 Subject: [PATCH] progress in irqs --- Cargo.lock | 37 +++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 1 + src/kernel/acpi.rs | 0 src/kernel/irq.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/mod.rs | 17 ++++-------- src/kernel/ps2.rs | 8 ++---- src/kernel/util.rs | 1 - src/lib.rs | 5 +++- 9 files changed, 120 insertions(+), 18 deletions(-) delete mode 100644 src/kernel/acpi.rs create mode 100644 src/kernel/irq.rs diff --git a/Cargo.lock b/Cargo.lock index 8790503..22149bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,25 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + [[package]] name = "mxrox" version = "0.1.0" dependencies = [ "no-std-compat 0.4.1", "stable-vec", + "x86_64", ] [[package]] @@ -22,6 +35,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + [[package]] name = "stable-vec" version = "0.4.1" @@ -30,3 +49,21 @@ checksum = "d1dff32a2ce087283bec878419027cebd888760d8760b2941ad0843531dc9ec8" dependencies = [ "no-std-compat 0.2.0", ] + +[[package]] +name = "volatile" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" + +[[package]] +name = "x86_64" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae" +dependencies = [ + "bit_field", + "bitflags", + "rustversion", + "volatile", +] diff --git a/Cargo.toml b/Cargo.toml index 960f4a7..6f1a565 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" [dependencies] stable-vec = "0.4.1" no-std-compat = {version = "0.4.1", features = ["alloc"]} +x86_64 = {version = "0.15.2", features = ["instructions"]} [profile.dev] panic = "abort" diff --git a/README.md b/README.md index 6f46f33..0342f3e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Internet resources where I found most information about OS dev - https://gitea.bedohswe.eu.org/bedohswe/bootsector_tictactoe/ - https://wiki.osdev.org/PS/2 - https://os.phil-opp.com/ +- https://wiki.osdev.org/Interrupts_Tutorial ### Contributing diff --git a/src/kernel/acpi.rs b/src/kernel/acpi.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/kernel/irq.rs b/src/kernel/irq.rs new file mode 100644 index 0000000..9352c56 --- /dev/null +++ b/src/kernel/irq.rs @@ -0,0 +1,68 @@ +use core::ptr::write_volatile; +use core::arch::asm; + +const APIC_BASE: *mut u32 = 0xFEE00000 as *mut u32; + +const APIC_EOI: *mut u32 = unsafe { APIC_BASE.add(0x0B0) }; +const APIC_SVR: *mut u32 = unsafe { APIC_BASE.add(0x0F0) }; + +#[repr(C, packed)] +#[derive(Clone, Copy)] +pub struct IdtEntry { + offset_low: u16, + selector: u16, + ist: u8, + type_attr: u8, + offset_middle: u16 +} + +pub const IDT_SIZE: usize = 256; + +#[repr(C)] +pub struct Idt { + entries: [IdtEntry; IDT_SIZE], +} + +static mut IDT: Idt = Idt { + entries: [IdtEntry { + offset_low: 0, + selector: 0, + ist: 0, + type_attr: 0, + offset_middle: 0, + }; IDT_SIZE], +}; + +fn register_idt(handler: u32) { + unsafe { + let entry = &mut IDT.entries[32]; + + entry.offset_low = handler as u16; + entry.selector = 0x08; + entry.ist = 0; + entry.type_attr = 0x8E; + entry.offset_middle = (handler >> 16) as u16; + } +} + +unsafe fn load_idt() { + asm!("lidt [{}]", "sti", in(reg) &IDT as *const _); +} + +unsafe fn enable_apic() { + let sv_reg = APIC_SVR as *mut u32; + let mut value = 0x100 | 0x1; + write_volatile(sv_reg, value); +} + +unsafe fn send_eoi() { + let eoi_reg = APIC_EOI as *mut u32; + write_volatile(eoi_reg, 0); +} + +pub fn init_apic() { + unsafe { + load_idt(); + enable_apic(); + } +} \ No newline at end of file diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 820ee22..50e231d 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,5 +1,6 @@ +use irq::init_apic; use heap::init_heap; -use ps2::read_ps2_status; +use ps2::init_ps2; use vga::{ fill_with_color, put_string, @@ -8,11 +9,10 @@ use vga::{ VGA_COLOR_LIGHT_MAGENTA, VGA_COLOR_RED }; -use no_std_compat::string::ToString; mod vga; mod ps2; -mod acpi; +mod irq; mod thread; mod heap; mod util; @@ -24,15 +24,10 @@ pub fn show_error(message: &str) { pub fn init_kernel() { init_heap(16400, 16384); + init_ps2(); + init_apic(); fill_with_color(VGA_COLOR_BLACK); - loop { - put_string( - 0, 0, - &format!("ps/2 status: 0x{:x}", read_ps2_status()), - VGA_COLOR_BLACK, - VGA_COLOR_LIGHT_MAGENTA - ); - } + loop {} } \ No newline at end of file diff --git a/src/kernel/ps2.rs b/src/kernel/ps2.rs index 5aec0ee..cf413f1 100644 --- a/src/kernel/ps2.rs +++ b/src/kernel/ps2.rs @@ -1,7 +1,5 @@ use core::ptr::{read_volatile, write_volatile}; -use no_std_compat::vec::Vec; - const DATA_PORT: *mut u8 = 0x60 as *mut u8; const STATUS_PORT: *mut u8 = 0x64 as *mut u8; @@ -17,11 +15,11 @@ fn send_ps2_command(command: u8) { unsafe { write_volatile(STATUS_PORT, command) } } -pub fn read_ps2_status() -> u8 { +fn read_ps2_status() -> u8 { unsafe { read_volatile(STATUS_PORT) } } -/// returns device type bytes -fn init_ps2_controller() -> Vec { +pub fn init_ps2() { + todo!() } \ No newline at end of file diff --git a/src/kernel/util.rs b/src/kernel/util.rs index 1c2ab74..e69de29 100644 --- a/src/kernel/util.rs +++ b/src/kernel/util.rs @@ -1 +0,0 @@ -use core::str; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e511ba5..621fc08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ #![feature(lang_items)] #![feature(alloc_error_handler)] #![feature(rustc_private)] +#![feature(abi_x86_interrupt)] + #[lang = "eh_personality"] extern "C" fn eh_personality() {} @@ -20,12 +22,13 @@ extern crate alloc; #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { - show_error(info.message().as_str().unwrap_or("mxrox death")); + show_error(info.message().as_str().unwrap_or("Unknown Error")); loop {} } #[alloc_error_handler] fn oom(_layout: core::alloc::Layout) -> ! { + show_error("Out Of Memory"); loop {} }