From 7d247fab3c07c9a667da25511bce8e8850a9a553 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Wed, 29 Jan 2025 01:49:10 +0300 Subject: [PATCH] more modules --- .vscode/settings.json | 13 +++++++++ Cargo.lock | 46 ++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ Makefile | 7 ++--- src/kernel/acpi.rs | 0 src/kernel/mod.rs | 14 +++++++++ src/kernel/ps2.rs | 23 +++++++++++++++ src/kernel/thread.rs | 0 src/{terminal.rs => kernel/vga.rs} | 36 +++++++++++++++-------- src/main.rs | 18 +++++------- 10 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/kernel/acpi.rs create mode 100644 src/kernel/mod.rs create mode 100644 src/kernel/ps2.rs create mode 100644 src/kernel/thread.rs rename src/{terminal.rs => kernel/vga.rs} (60%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7d2f122 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ + + +{ + "rust-analyzer.cargo.allTargets": false, + "rust-analyzer.checkOnSave.allTargets": true, // not sure if this one is needed + "rust-analyzer.checkOnSave.overrideCommand": [ + "./cargo", + "xcheck", + "--json-output", + "--target", + "thumbv7m-none-eabi" + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index d27a482..4d4e0c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,52 @@ # 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_kernel" version = "0.1.0" +dependencies = [ + "volatile 0.6.1", + "x86_64", +] + +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "volatile" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" + +[[package]] +name = "volatile" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af8ca9a5d4debca0633e697c88269395493cebf2e10db21ca2dbde37c1356452" + +[[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 0.4.6", +] diff --git a/Cargo.toml b/Cargo.toml index f592958..71a28c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +x86_64 = "0.15.2" +volatile = "0.6.1" \ No newline at end of file diff --git a/Makefile b/Makefile index 952a12a..cac01d7 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build/mxrox.iso: build/kernel.elf build_dir cp 'grub.cfg' build/iso/boot/grub - cp '$<' build/iso/bootmxrox + cp '$<' build/iso/boot grub-mkrescue -o '$@' build/iso build/boot.o: boot.s build_dir @@ -19,7 +19,6 @@ build/kernel.o: build_dir build_dir: mkdir -p build mkdir -p build/iso - mkdir -p build/iso/lib mkdir -p build/iso/boot mkdir -p build/iso/boot/grub @@ -27,8 +26,8 @@ build: build/mxrox.iso clean: rm -rf build - rm -rf target - rm -rf Cargo.lock +# rm -rf target +# rm -rf Cargo.lock mkdir build run-kernel: build/kernel.elf diff --git a/src/kernel/acpi.rs b/src/kernel/acpi.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs new file mode 100644 index 0000000..e2cec0f --- /dev/null +++ b/src/kernel/mod.rs @@ -0,0 +1,14 @@ +use vga::{fill, fill_with_color, put_char, put_string_by_index, read_char, VgaColor}; + +mod vga; +mod ps2; +mod acpi; +mod thread; + +pub fn show_error(message: &str) { + put_string_by_index(0, message, VgaColor::BLACK, VgaColor::LIGHT_RED); +} + +pub fn start_kernel() { + fill_with_color(VgaColor::BLACK); +} \ No newline at end of file diff --git a/src/kernel/ps2.rs b/src/kernel/ps2.rs new file mode 100644 index 0000000..63f7e79 --- /dev/null +++ b/src/kernel/ps2.rs @@ -0,0 +1,23 @@ +const DATA_PORT: *mut u8 = 0x60 as *mut u8; +const STATUS_PORT: *mut u8 = 0x64 as *mut u8; + +fn write_ps2_data(data: u8) { + todo!() +} + +fn read_ps2_data() -> u8 { + todo!() +} + +fn send_ps2_command(command: u8) { + todo!() +} + +fn read_ps2_status() -> u8 { + todo!() +} + +/// returns device type bytes +fn init_ps2_controller() -> vec![u8; 2] { + todo!() +} \ No newline at end of file diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/terminal.rs b/src/kernel/vga.rs similarity index 60% rename from src/terminal.rs rename to src/kernel/vga.rs index b56c6b5..80ebbb5 100644 --- a/src/terminal.rs +++ b/src/kernel/vga.rs @@ -1,6 +1,7 @@ -pub const TERMINAL_BUFFER: *mut u16 = 0xB8000 as *mut u16; -pub const TERMINAL_WIDTH: usize = 80; -pub const TERMINAL_HEIGHT: usize = 25; +const VGA_BUFFER: *mut u16 = 0xB8000 as *mut u16; + +pub const VGA_WIDTH: usize = 80; +pub const VGA_HEIGHT: usize = 25; pub struct VgaColor(u8); @@ -23,27 +24,38 @@ impl VgaColor { pub const WHITE: u8 = 15; } -pub fn put_char(x: usize, y: usize, c: char, bg: u8, fg: u8) { +pub fn put_char_by_index(index: usize, c: char, bg: u8, fg: u8) { let c16 = c as u16; let color = fg | bg << 4; let color16 = color as u16; - let index = y * TERMINAL_WIDTH + x; unsafe { - *TERMINAL_BUFFER.add(index) = c16 | (color16 << 8) + ptr::write_volatile(VGA_BUFFER.add(index), c16 | (color16 << 8)); + } +} + +pub fn put_char(x: usize, y: usize, c: char, bg: u8, fg: u8) { + put_char_by_index(y * VGA_WIDTH + x, c, bg, fg) +} + +pub fn put_string_by_index(index: usize, text: &str, bg: u8, fg: u8) { + for (i, c) in text.char_indices() { + put_char_by_index(index + i, c, bg, fg); } } pub fn put_string(x: usize, y: usize, text: &str, bg: u8, fg: u8) { - for (i, c) in text.char_indices() { - put_char(x+i, y, c, bg, fg); - } + put_string_by_index(y * VGA_WIDTH + x, text, bg, fg) } pub fn fill(c: char, bg: u8, fg: u8) { - for x in 0..TERMINAL_WIDTH { - for y in 0..TERMINAL_HEIGHT { + for x in 0..VGA_WIDTH { + for y in 0..VGA_HEIGHT { put_char(x, y, c, bg, fg); } } -} \ No newline at end of file +} + +pub fn fill_with_color(color: u8) { + fill(' ', color, 0) +} diff --git a/src/main.rs b/src/main.rs index 8def41f..ce12032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,24 +10,20 @@ use core::panic::PanicInfo; use core::sync::atomic; use core::sync::atomic::Ordering; +use kernel::{start_kernel, show_error}; + +mod kernel; #[inline(never)] #[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop { - atomic::compiler_fence(Ordering::SeqCst); - } +fn panic(info: &PanicInfo) -> ! { + show_error(info.message().as_str().unwrap_or("mxrox death")); + loop {} } -mod terminal; - -use terminal::{fill, put_string, VgaColor}; - #[no_mangle] fn main() -> ! { - fill(' ', VgaColor::BLACK, VgaColor::BLACK); - put_string(0, 0, "Hello World from MxRox!", VgaColor::BLACK, VgaColor::WHITE); - + start_kernel(); loop {} } \ No newline at end of file