diff --git a/.cargo/config.toml b/.cargo/config.toml index 6ce8bc9..6acb164 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,4 +1,3 @@ - [unstable] build-std = ["core", "compiler_builtins", "alloc"] build-std-features = ["compiler-builtins-mem"] diff --git a/Cargo.lock b/Cargo.lock index 410bf9b..8790503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,41 +3,11 @@ version = 4 [[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[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 = "lock_api" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "mxrox_kernel" +name = "mxrox" version = "0.1.0" dependencies = [ - "spin", + "no-std-compat 0.4.1", "stable-vec", - "volatile 0.6.1", - "x86_64", ] [[package]] @@ -47,25 +17,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df270209a7f04d62459240d890ecb792714d5db12c92937823574a09930276b4" [[package]] -name = "rustversion" -version = "1.0.19" +name = "no-std-compat" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] +checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" [[package]] name = "stable-vec" @@ -73,29 +28,5 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d1dff32a2ce087283bec878419027cebd888760d8760b2941ad0843531dc9ec8" dependencies = [ - "no-std-compat", -] - -[[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", + "no-std-compat 0.2.0", ] diff --git a/Cargo.toml b/Cargo.toml index 13f5985..960f4a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,17 @@ +[lib] +crate-type = ["staticlib"] + [package] -name = "mxrox_kernel" +name = "mxrox" version = "0.1.0" edition = "2021" [dependencies] -x86_64 = "0.15.2" -volatile = "0.6.1" stable-vec = "0.4.1" -spin = "0.9" \ No newline at end of file +no-std-compat = {version = "0.4.1", features = ["alloc"]} + +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" \ No newline at end of file diff --git a/Makefile b/Makefile index 390cc75..458098e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: clean run run-iso build_dir +TARGET_DIR = target/x86-unknown-bare_metal/release + build/mxrox.iso: build/kernel.elf build_dir cp 'grub.cfg' build/iso/boot/grub cp '$<' build/iso/boot @@ -8,13 +10,14 @@ build/mxrox.iso: build/kernel.elf build_dir build/boot.o: boot.s build_dir nasm -f elf32 '$<' -o '$@' -build/kernel.elf: linker.ld build/boot.o build/kernel.o - i686-elf-ld -m elf_i386 -o '$@' -T $^ +build/kernel.elf: linker.ld build/boot.o build/libkernel.a + i686-elf-gcc -nostdlib -o $@ -T $^ -build/kernel.o: build_dir +build/libkernel.a: build_dir + rm -rf $(TARGET_DIR) rustup override set nightly cargo build --release - cp target/x86-unknown-bare_metal/release/deps/mxrox_kernel-*.o $@ + cp $(TARGET_DIR)/libmxrox.a build/libkernel.a build_dir: mkdir -p build @@ -25,13 +28,12 @@ build_dir: build: build/mxrox.iso clean: + cargo clean rm -rf build - rm -rf target -# rm -rf Cargo.lock mkdir build run-kernel: build/kernel.elf - qemu-system-i386 -kernel '$<' + qemu-system-i386 -kernel '$<' -m 512M run: build/mxrox.iso - qemu-system-i386 -cdrom '$<' \ No newline at end of file + qemu-system-i386 -cdrom '$<' -m 512M \ No newline at end of file diff --git a/boot.s b/boot.s index 73fb436..c35bfa1 100644 --- a/boot.s +++ b/boot.s @@ -10,7 +10,7 @@ CHECKSUM equ -(MAGIC + MBFLAGS) ; checksum of above, to prove we are multiboot ; search for this signature in the first 8 KiB of the kernel file, aligned at a ; 32-bit boundary. The signature is in its own section so the header can be ; forced to be within the first 8 KiB of the kernel file. -section .multiboot +section .mbheader align 4 dd MAGIC dd MBFLAGS diff --git a/src/kernel/heap.rs b/src/kernel/heap.rs index 85df497..7f8554a 100644 --- a/src/kernel/heap.rs +++ b/src/kernel/heap.rs @@ -36,7 +36,9 @@ impl FreeListAllocator { *self.head.get() = first_node; } +} +unsafe impl GlobalAlloc for FreeListAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { let mut prev: *mut FreeListNode = null_mut(); let mut current = *self.head.get(); @@ -95,16 +97,6 @@ impl FreeListAllocator { #[global_allocator] static ALLOCATOR: FreeListAllocator = FreeListAllocator::new(); -unsafe impl GlobalAlloc for FreeListAllocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - self.alloc(layout) - } - - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - self.dealloc(ptr, layout) - } -} - #[no_mangle] pub extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 { for i in 0..n { diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index ebf65d9..820ee22 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,19 +1,21 @@ use heap::init_heap; -use stable_vec::StableVec; +use ps2::read_ps2_status; use vga::{ fill_with_color, + put_string, put_string_by_index, - VGA_COLOR_BLACK, + VGA_COLOR_BLACK, + VGA_COLOR_LIGHT_MAGENTA, VGA_COLOR_RED }; +use no_std_compat::string::ToString; mod vga; mod ps2; mod acpi; mod thread; mod heap; - -type Vec = StableVec; +mod util; pub fn show_error(message: &str) { fill_with_color(VGA_COLOR_BLACK); @@ -23,5 +25,14 @@ pub fn show_error(message: &str) { pub fn init_kernel() { init_heap(16400, 16384); - show_error("error test"); + 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 + ); + } } \ No newline at end of file diff --git a/src/kernel/ps2.rs b/src/kernel/ps2.rs index 48e3ae1..5aec0ee 100644 --- a/src/kernel/ps2.rs +++ b/src/kernel/ps2.rs @@ -1,6 +1,6 @@ use core::ptr::{read_volatile, write_volatile}; -use super::Vec; +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,7 +17,7 @@ fn send_ps2_command(command: u8) { unsafe { write_volatile(STATUS_PORT, command) } } -fn read_ps2_status() -> u8 { +pub fn read_ps2_status() -> u8 { unsafe { read_volatile(STATUS_PORT) } } diff --git a/src/kernel/util.rs b/src/kernel/util.rs new file mode 100644 index 0000000..1c2ab74 --- /dev/null +++ b/src/kernel/util.rs @@ -0,0 +1 @@ +use core::str; \ No newline at end of file diff --git a/src/main.rs b/src/lib.rs similarity index 70% rename from src/main.rs rename to src/lib.rs index 760e3bf..e511ba5 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ #![no_main] #![feature(lang_items)] +#![feature(alloc_error_handler)] +#![feature(rustc_private)] #[lang = "eh_personality"] extern "C" fn eh_personality() {} @@ -12,6 +14,9 @@ use kernel::{init_kernel, show_error}; mod kernel; +#[macro_use] +extern crate alloc; + #[inline(never)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { @@ -19,6 +24,11 @@ fn panic(info: &PanicInfo) -> ! { loop {} } +#[alloc_error_handler] +fn oom(_layout: core::alloc::Layout) -> ! { + loop {} +} + #[no_mangle] pub extern "C" fn main() -> ! { init_kernel(); diff --git a/x86-unknown-bare_metal.json b/x86-unknown-bare_metal.json index b797a05..8f0d330 100644 --- a/x86-unknown-bare_metal.json +++ b/x86-unknown-bare_metal.json @@ -11,5 +11,9 @@ "linker": "rust-lld", "panic-strategy": "abort", "disable-redzone": true, - "features": "+soft-float,-sse" -} + "features": "+soft-float,-sse", + "relocation-model": "static", + "code-model": "large", + "target-family": "none", + "linker-is-gnu": false +} \ No newline at end of file