From 51478e7ccf74cb4aa6b4dd28acb2881ed41cdce5 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Fri, 30 Aug 2024 00:38:15 -0700 Subject: [PATCH] [Denali] Begin porting to rust. --- rust/Cargo.lock | 1 + rust/Cargo.toml | 4 +-- rust/lib/denali/Cargo.toml | 11 ------ rust/sys/denali/Cargo.toml | 21 +++++++++++ rust/{lib => sys}/denali/build.rs | 0 rust/sys/denali/src/ahci/controller.rs | 49 ++++++++++++++++++++++++++ rust/sys/denali/src/ahci/mod.rs | 3 ++ rust/sys/denali/src/bin/denali.rs | 29 +++++++++++++++ rust/{lib => sys}/denali/src/lib.rs | 2 ++ rust/sys/yellowstone/Cargo.toml | 2 +- scripts/build_image.sh | 2 +- scripts/qemu.sh | 2 +- 12 files changed, 110 insertions(+), 16 deletions(-) delete mode 100644 rust/lib/denali/Cargo.toml create mode 100644 rust/sys/denali/Cargo.toml rename rust/{lib => sys}/denali/build.rs (100%) create mode 100644 rust/sys/denali/src/ahci/controller.rs create mode 100644 rust/sys/denali/src/ahci/mod.rs create mode 100644 rust/sys/denali/src/bin/denali.rs rename rust/{lib => sys}/denali/src/lib.rs (84%) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 37a5cd7..c7d4f7b 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -22,6 +22,7 @@ name = "denali" version = "0.1.0" dependencies = [ "mammoth", + "yellowstone-yunq", "yunq", "yunqc", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c357206..258b494 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,7 +1,7 @@ [workspace] -members = [ "lib/denali", - "lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-test", "sys/teton", "sys/yellowstone", "usr/testbed", +members = [ + "lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-test", "sys/denali", "sys/teton", "sys/yellowstone", "usr/testbed", ] resolver = "2" diff --git a/rust/lib/denali/Cargo.toml b/rust/lib/denali/Cargo.toml deleted file mode 100644 index 2993024..0000000 --- a/rust/lib/denali/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "denali" -version = "0.1.0" -edition = "2021" - -[dependencies] -mammoth = { path = "../mammoth" } -yunq = {path = "../yunq"} - -[build-dependencies] -yunqc = {path = "../../../yunq/rust"} diff --git a/rust/sys/denali/Cargo.toml b/rust/sys/denali/Cargo.toml new file mode 100644 index 0000000..6cb7992 --- /dev/null +++ b/rust/sys/denali/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "denali" +version = "0.1.0" +edition = "2021" + +[dependencies] +mammoth = { path = "../../lib/mammoth" } +yunq = {path = "../../lib/yunq"} + +yellowstone-yunq = { path = "../../lib/yellowstone", optional = true } + +[[bin]] +name = "denali" +required-features = ["binary"] + +[build-dependencies] +yunqc = {path = "../../../yunq/rust"} + +[features] +default = ["binary"] +binary = ["dep:yellowstone-yunq"] diff --git a/rust/lib/denali/build.rs b/rust/sys/denali/build.rs similarity index 100% rename from rust/lib/denali/build.rs rename to rust/sys/denali/build.rs diff --git a/rust/sys/denali/src/ahci/controller.rs b/rust/sys/denali/src/ahci/controller.rs new file mode 100644 index 0000000..cf02c60 --- /dev/null +++ b/rust/sys/denali/src/ahci/controller.rs @@ -0,0 +1,49 @@ +use mammoth::mem::MemoryRegion; + +#[repr(C, packed)] +pub struct PciDeviceHeader { + pub vendor_id: u16, + pub device_id: u16, + pub command_reg: u16, + pub status_reg: u16, + pub revision: u8, + pub prog_interface: u8, + pub subclass: u8, + pub class_code: u8, + pub cache_line_size: u8, + pub latency_timer: u8, + pub header_type: u8, + pub bist: u8, + pub bars: [u32; 5], + pub abar: u32, + pub reserved0: u32, + pub subsystem_id: u32, + pub expansion_rom: u16, + pub cap_ptr: u8, + pub reserved1: [u8; 7], + pub interrupt_line: u8, + pub interrupt_pin: u8, + pub min_grant: u8, + pub max_latency: u8, +} + +pub struct AhciController { + pci_memory: MemoryRegion, +} + +impl AhciController { + pub fn new(pci_memory: MemoryRegion) -> Self { + Self { pci_memory } + } + + pub fn pci_header(&self) -> &mut PciDeviceHeader { + unsafe { + self.pci_memory + .mut_slice::() + .as_mut_ptr() + .cast::() + .as_mut() + .unwrap() + } + } +} diff --git a/rust/sys/denali/src/ahci/mod.rs b/rust/sys/denali/src/ahci/mod.rs new file mode 100644 index 0000000..db456be --- /dev/null +++ b/rust/sys/denali/src/ahci/mod.rs @@ -0,0 +1,3 @@ +mod controller; + +pub use controller::AhciController; diff --git a/rust/sys/denali/src/bin/denali.rs b/rust/sys/denali/src/bin/denali.rs new file mode 100644 index 0000000..b685b5d --- /dev/null +++ b/rust/sys/denali/src/bin/denali.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +extern crate alloc; + +use mammoth::{define_entry, zion::z_err_t}; + +use denali::ahci::AhciController; + +define_entry!(); + +#[no_mangle] +extern "C" fn main() -> z_err_t { + mammoth::debug!("IN Denali!"); + + let yellowstone = yellowstone_yunq::from_init_endpoint(); + + let ahci_info = yellowstone + .get_ahci_info() + .expect("Failed to get ahci info"); + + let ahci_controller = AhciController::new( + mammoth::mem::MemoryRegion::from_cap(mammoth::cap::Capability::take(ahci_info.ahci_region)) + .unwrap(), + ); + + mammoth::debug!("AHCI ABAR {:#x}", ahci_controller.pci_header().abar as u64); + 0 +} diff --git a/rust/lib/denali/src/lib.rs b/rust/sys/denali/src/lib.rs similarity index 84% rename from rust/lib/denali/src/lib.rs rename to rust/sys/denali/src/lib.rs index 3cec9d6..ee83829 100644 --- a/rust/lib/denali/src/lib.rs +++ b/rust/sys/denali/src/lib.rs @@ -3,3 +3,5 @@ use core::include; include!(concat!(env!("OUT_DIR"), "/yunq.rs")); + +pub mod ahci; diff --git a/rust/sys/yellowstone/Cargo.toml b/rust/sys/yellowstone/Cargo.toml index 57e52b8..5e165e8 100644 --- a/rust/sys/yellowstone/Cargo.toml +++ b/rust/sys/yellowstone/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] mammoth = { path = "../../lib/mammoth" } -denali = { path = "../../lib/denali" } +denali = { path = "../../sys/denali", default-features = false} victoriafalls = { path = "../../lib/victoriafalls" } voyageurs = { path = "../../lib/voyageurs" } yellowstone-yunq = { path = "../../lib/yellowstone" } diff --git a/scripts/build_image.sh b/scripts/build_image.sh index 826bdd4..f715102 100644 --- a/scripts/build_image.sh +++ b/scripts/build_image.sh @@ -40,7 +40,7 @@ cp ../zion/boot/limine.cfg efi/ cp zion/zion efi/ mkdir -p efi/sys cp ../sysroot/bin/yellowstone efi/sys/yellowstone -cp sys/denali/denali efi/sys/denali +cp ../sysroot/bin/denali efi/sys/denali cp sys/victoriafalls/victoriafalls efi/sys/victoriafalls mkdir -p sysroot diff --git a/scripts/qemu.sh b/scripts/qemu.sh index 0ae6b41..40bf1c7 100755 --- a/scripts/qemu.sh +++ b/scripts/qemu.sh @@ -23,7 +23,7 @@ for BIN in ${DIR}/../rust/usr/*/; do done for BIN in ${DIR}/../rust/sys/*/; do - cargo install --force --path "${BIN}" --root $CARGO_SYS_ROOT + cargo install --all-features --force --path "${BIN}" --root $CARGO_SYS_ROOT done popd