From 32779a05bc89660d92fc9648884b6c8b012a7123 Mon Sep 17 00:00:00 2001 From: shibedrill Date: Tue, 18 Feb 2025 23:49:06 -0500 Subject: [PATCH] Work for kernel params --- Makefile | 28 ++++++++++++++++------------ build.rs | 3 +-- src/arch/aarch64/asm.rs | 7 +++++++ src/arch/aarch64/display.rs | 1 + src/arch/aarch64/mod.rs | 2 ++ src/arch/loongarch64/asm.rs | 7 +++++++ src/arch/loongarch64/display.rs | 1 + src/arch/loongarch64/mod.rs | 2 ++ src/arch/mod.rs | 17 ++++++++++++++++- src/arch/riscv64/asm.rs | 7 +++++++ src/arch/riscv64/display.rs | 1 + src/arch/riscv64/mod.rs | 2 ++ src/arch/x86_64/asm.rs | 7 +++++++ src/arch/x86_64/display.rs | 10 +++------- src/arch/x86_64/mod.rs | 3 ++- src/boot.rs | 7 +++++-- src/lib.rs | 10 ++++++---- src/main.rs | 26 +++++++++++--------------- src/memory.rs | 10 +++++----- src/panic.rs | 3 +-- src/params.rs | 24 ++++++++++++++++++++++++ src/process.rs | 5 ++--- src/resources.rs | 1 + 23 files changed, 130 insertions(+), 54 deletions(-) create mode 100644 src/arch/aarch64/asm.rs create mode 100644 src/arch/aarch64/display.rs create mode 100644 src/arch/aarch64/mod.rs create mode 100644 src/arch/loongarch64/asm.rs create mode 100644 src/arch/loongarch64/display.rs create mode 100644 src/arch/loongarch64/mod.rs create mode 100644 src/arch/riscv64/asm.rs create mode 100644 src/arch/riscv64/display.rs create mode 100644 src/arch/riscv64/mod.rs create mode 100644 src/arch/x86_64/asm.rs create mode 100644 src/params.rs diff --git a/Makefile b/Makefile index 2777346..f2b6402 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,20 @@ TARGET ?= "x86_64-unknown-none" -QEMU ?= "qemu-system-x86_64" +qemu = "qemu-system-x86_64" -all: clean prepare gila iso run +ifeq ($(TARGET),x86_64-unknown-none) + qemu = "qemu-system-x86_64" +else ifeq ($(TARGET),riscv64gc-unknown-none-elf) + qemu = "qemu-system-riscv64" +else ifeq ($(TARGET),aarch64-unknown-none) + qemu = "qemu-system-aarch64" + qemu += "-machine" + qemu += "orangepi-pc" +else ifeq ($(TARGET),loongarch64-unknown-none) + qemu = "qemu-system-loongarch64" +endif + +all: gila iso run # Prepare toolchain prepare: @@ -12,17 +24,9 @@ prepare: # Run the ISO in an emulator. run: build/gila.iso -ifeq ($(TARGET),x86_64-unknown-none) - QEMU = "qemu-system-x86_64" -else ifeq ($(TARGET),riscv64gc-unknown-none-elf) - QEMU = "qemu-system-riscv64" -else ifeq ($(TARGET),aarch64-unknown-none) - QEMU = "qemu-system-aarch64" -else ifeq ($(TARGET),loongarch64-unknown-none) - QEMU = "qemu-system-loongarch64" -endif + echo $(qemu) - $(QEMU) -drive file=build/gila.iso,format=raw,index=0,media=disk + $(qemu) -drive file=build/gila.iso,format=raw,index=0,media=disk # Build the bootable kernel image. .PHONY: build/iso/gila diff --git a/build.rs b/build.rs index 1cd44f2..0705165 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,7 @@ fn main() { - let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); // Tell cargo to pass the linker script to the linker.. println!("cargo:rustc-link-arg=-Tlinker-scripts/linker-{arch}.ld"); // ..and to re-run if it changes. println!("cargo:rerun-if-changed=linker-scripts/linker-{arch}.ld"); -} \ No newline at end of file +} diff --git a/src/arch/aarch64/asm.rs b/src/arch/aarch64/asm.rs new file mode 100644 index 0000000..bdab157 --- /dev/null +++ b/src/arch/aarch64/asm.rs @@ -0,0 +1,7 @@ +use core::arch::asm; + +pub unsafe fn halt() { + unsafe { + asm!("wfi"); + } +} diff --git a/src/arch/aarch64/display.rs b/src/arch/aarch64/display.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/arch/aarch64/display.rs @@ -0,0 +1 @@ + diff --git a/src/arch/aarch64/mod.rs b/src/arch/aarch64/mod.rs new file mode 100644 index 0000000..0bc4595 --- /dev/null +++ b/src/arch/aarch64/mod.rs @@ -0,0 +1,2 @@ +pub mod asm; +pub mod display; diff --git a/src/arch/loongarch64/asm.rs b/src/arch/loongarch64/asm.rs new file mode 100644 index 0000000..fad4d74 --- /dev/null +++ b/src/arch/loongarch64/asm.rs @@ -0,0 +1,7 @@ +use core::arch::asm; + +pub unsafe fn halt() { + unsafe { + asm!("idle 0"); + } +} diff --git a/src/arch/loongarch64/display.rs b/src/arch/loongarch64/display.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/arch/loongarch64/display.rs @@ -0,0 +1 @@ + diff --git a/src/arch/loongarch64/mod.rs b/src/arch/loongarch64/mod.rs new file mode 100644 index 0000000..0bc4595 --- /dev/null +++ b/src/arch/loongarch64/mod.rs @@ -0,0 +1,2 @@ +pub mod asm; +pub mod display; diff --git a/src/arch/mod.rs b/src/arch/mod.rs index 776fad9..a92bb82 100644 --- a/src/arch/mod.rs +++ b/src/arch/mod.rs @@ -1,4 +1,19 @@ #[cfg(target_arch = "x86_64")] pub mod x86_64; #[cfg(target_arch = "x86_64")] -pub use x86_64 as current; \ No newline at end of file +pub use x86_64 as current; + +#[cfg(target_arch = "aarch64")] +pub mod aarch64; +#[cfg(target_arch = "aarch64")] +pub use aarch64 as current; + +#[cfg(target_arch = "riscv64")] +pub mod riscv64; +#[cfg(target_arch = "riscv64")] +pub use riscv64 as current; + +#[cfg(target_arch = "loongarch64")] +pub mod loongarch64; +#[cfg(target_arch = "loongarch64")] +pub use loongarch64 as current; diff --git a/src/arch/riscv64/asm.rs b/src/arch/riscv64/asm.rs new file mode 100644 index 0000000..bdab157 --- /dev/null +++ b/src/arch/riscv64/asm.rs @@ -0,0 +1,7 @@ +use core::arch::asm; + +pub unsafe fn halt() { + unsafe { + asm!("wfi"); + } +} diff --git a/src/arch/riscv64/display.rs b/src/arch/riscv64/display.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/arch/riscv64/display.rs @@ -0,0 +1 @@ + diff --git a/src/arch/riscv64/mod.rs b/src/arch/riscv64/mod.rs new file mode 100644 index 0000000..0bc4595 --- /dev/null +++ b/src/arch/riscv64/mod.rs @@ -0,0 +1,2 @@ +pub mod asm; +pub mod display; diff --git a/src/arch/x86_64/asm.rs b/src/arch/x86_64/asm.rs new file mode 100644 index 0000000..d4d1e6c --- /dev/null +++ b/src/arch/x86_64/asm.rs @@ -0,0 +1,7 @@ +use core::arch::asm; + +pub unsafe fn halt() { + unsafe { + asm!("hlt"); + } +} diff --git a/src/arch/x86_64/display.rs b/src/arch/x86_64/display.rs index 02162e7..7732baf 100644 --- a/src/arch/x86_64/display.rs +++ b/src/arch/x86_64/display.rs @@ -18,13 +18,9 @@ impl TextDisplay { row: 0, } } - pub fn writeline(text: &str, colors: vga::colors::TextModeColor) { + pub fn writeline(text: &str, colors: vga::colors::TextModeColor) {} - } - - pub fn write(text: &str, colors: vga::colors::TextModeColor) { - - } + pub fn write(text: &str, colors: vga::colors::TextModeColor) {} } -pub static TEXT_DISPLAY: TextDisplay = TextDisplay::new(); \ No newline at end of file +pub static TEXT_DISPLAY: TextDisplay = TextDisplay::new(); diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 9fc4ff2..0bc4595 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -1 +1,2 @@ -pub mod display; \ No newline at end of file +pub mod asm; +pub mod display; diff --git a/src/boot.rs b/src/boot.rs index 4def5f8..f8c0e85 100644 --- a/src/boot.rs +++ b/src/boot.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use limine::{request::*, BaseRevision}; +use limine::{BaseRevision, request::*}; #[used] #[unsafe(link_section = ".requests_start_marker")] @@ -14,4 +14,7 @@ static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new(); pub static BASE_REVISION: BaseRevision = limine::BaseRevision::new(); #[used] #[unsafe(link_section = ".requests")] -pub static SMP_REQUEST: SmpRequest = limine::request::SmpRequest::new(); \ No newline at end of file +pub static SMP_REQUEST: SmpRequest = limine::request::SmpRequest::new(); +#[used] +#[unsafe(link_section = ".requests")] +pub static FILE_REQUEST: KernelFileRequest = limine::request::KernelFileRequest::new(); diff --git a/src/lib.rs b/src/lib.rs index b6fa032..862897d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,16 @@ #![no_std] #![feature(allocator_api)] -/// Process representation. -pub mod process; +/// Boot services, handled by the Limine boot protocol. +pub mod boot; /// Memory regions, allocation, flags, and whatnot. pub mod memory; /// Panic handling function logic. pub mod panic; -/// Boot services, handled by the Limine boot protocol. -pub mod boot; +/// Kernel command line parameter structure and enums. +pub mod params; +/// Process representation. +pub mod process; /// Resources accessible from many parts of the code. pub mod resources; diff --git a/src/main.rs b/src/main.rs index 8252d77..74e3505 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,31 @@ #![no_std] #![no_main] - #![feature(allocator_api)] #![allow(dead_code)] #![allow(unused_imports)] -use core::arch::asm; - +use gila::arch::current::*; use gila::boot::*; -use gila::process; use gila::memory; use gila::panic; -use gila::boot; -use gila::arch::current::display; +use gila::params; +use gila::params::get_kernel_params; +use gila::process; +//use gila::arch::current::display; #[unsafe(no_mangle)] -unsafe extern "C" fn main() -> !{ - +unsafe extern "C" fn main() -> ! { assert!(BASE_REVISION.is_supported()); + let kernel_file_response = FILE_REQUEST.get_response().unwrap(); + + let _kernel_parameters = get_kernel_params(kernel_file_response.file().cmdline()); + let _smp_response = SMP_REQUEST.get_response(); loop { unsafe { - #[cfg(target_arch = "x86_64")] - asm!("hlt"); - #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] - asm!("wfi"); - #[cfg(target_arch = "loongarch64")] - asm!("idle 0"); + asm::halt(); } } - } diff --git a/src/memory.rs b/src/memory.rs index 33c5208..128f1a3 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -3,9 +3,9 @@ use flagset::flags; -use talc::*; -use spin; use core::alloc::{Allocator, Layout}; +use spin; +use talc::*; static mut ARENA: [u8; 10000] = [0; 10000]; @@ -14,8 +14,8 @@ static ALLOCATOR: Talck, ClaimOnOom> = Talc::new(unsafe { // if we're in a hosted environment, the Rust runtime may allocate before // main() is called, so we need to initialize the arena automatically ClaimOnOom::new(Span::from_array(core::ptr::addr_of!(ARENA).cast_mut())) -}).lock(); - +}) +.lock(); pub struct MemoryRegion { start_address: usize, @@ -29,4 +29,4 @@ flags! { WRITABLE, EXECUTABLE, } -} \ No newline at end of file +} diff --git a/src/panic.rs b/src/panic.rs index 8041769..ecbf975 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -1,7 +1,6 @@ - use core::panic::*; #[panic_handler] pub fn panic(_info: &PanicInfo) -> ! { loop {} -} \ No newline at end of file +} diff --git a/src/params.rs b/src/params.rs new file mode 100644 index 0000000..350b29f --- /dev/null +++ b/src/params.rs @@ -0,0 +1,24 @@ +extern crate alloc; +use alloc::string::String; +use alloc::vec::Vec; + +pub type KernelParameters = alloc::collections::BTreeMap; + +// This parsing is godawful. +pub fn get_kernel_params(cmdline: &[u8]) -> KernelParameters { + let cmd_str = String::from_utf8_lossy(cmdline).into_owned(); + let args: Vec<&str> = cmd_str.split_ascii_whitespace().collect(); + let mut params: alloc::collections::BTreeMap = + alloc::collections::BTreeMap::new(); + for arg in args { + let split: Vec<&str> = arg.split('=').collect(); + if let Some(first) = split.first() { + if split.len() > 1 { + params.insert(String::from(*first), split[1..].join("=")); + } else { + params.insert(String::from(*first), String::new()); + } + } + } + params +} diff --git a/src/process.rs b/src/process.rs index e1f8d9b..c4b038f 100644 --- a/src/process.rs +++ b/src/process.rs @@ -6,8 +6,8 @@ use crate::memory::MemoryRegion; use lazy_static; extern crate alloc; -use alloc::vec::Vec; use alloc::string::String; +use alloc::vec::Vec; pub struct ProcessTable {} @@ -39,5 +39,4 @@ pub enum ProcessState { WAITING, SLEEPING, SUSPENDED, - -} \ No newline at end of file +} diff --git a/src/resources.rs b/src/resources.rs index e69de29..8b13789 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -0,0 +1 @@ +