diff --git a/Cargo.toml b/Cargo.toml index 71bd8cea..cff0c8c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,7 @@ members = [ "crates/lldebug", "crates/serial", "crates/lldebug-macro", - "crates/hw", - "crates/hw-macro", + "crates/bits-macro", "crates/util", "crates/elf", "crates/mem", @@ -52,8 +51,7 @@ bootgfx = { path = "crates/bootgfx" } serial = { path = "crates/serial" } lldebug = { path = "crates/lldebug" } lldebug-macro = { path = "crates/lldebug-macro" } -hw = { path = "crates/hw" } -hw-macro = { path = "crates/hw-macro" } +bits-macro = { path = "crates/bits-macro" } util = { path = "crates/util" } elf = { path = "crates/elf" } mem = { path = "crates/mem" } diff --git a/crates/arch/Cargo.toml b/crates/arch/Cargo.toml index 9d4d3fc2..e1e15b2f 100644 --- a/crates/arch/Cargo.toml +++ b/crates/arch/Cargo.toml @@ -8,7 +8,6 @@ documentation.workspace = true [dependencies] bits = {workspace = true} -hw = {workspace = true} util = {workspace = true} arch-macro = {workspace = true} lldebug = {workspace = true} diff --git a/crates/arch/src/gdt.rs b/crates/arch/src/gdt.rs index 3353d6d2..b70d5dfb 100644 --- a/crates/arch/src/gdt.rs +++ b/crates/arch/src/gdt.rs @@ -24,7 +24,6 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA */ use core::arch::asm; -use hw::make_hw; #[repr(C)] pub struct GlobalDescriptorTable([u64; TABLE_SIZE]); @@ -68,7 +67,7 @@ impl GdtPointer { } } -#[make_hw( +#[bits::bits( field(RW, 0..16, segment_limit_lo), field(RW, 16..32, base_address_lo), field(RW, 32..40, base_address_mi), @@ -89,7 +88,7 @@ impl GdtPointer { #[derive(Clone, Copy)] pub struct DataSegmentDesc(u64); -#[make_hw( +#[bits::bits( field(RW, 0..16, segment_limit_lo), field(RW, 16..32, base_address_lo), field(RW, 32..40, base_address_mi), @@ -144,7 +143,7 @@ impl SegmentEntry for DataSegmentDesc { } } -#[make_hw( +#[bits::bits( field(RW, 0..16, segment_limit_lo), field(RW, 16..32, base_address_lo), field(RW, 32..40, base_address_mi), diff --git a/crates/arch/src/idt64.rs b/crates/arch/src/idt64.rs index 0e00a00d..e6158e65 100644 --- a/crates/arch/src/idt64.rs +++ b/crates/arch/src/idt64.rs @@ -29,7 +29,6 @@ use crate::{ }; pub use arch_macro::interrupt; use core::fmt::Debug; -use hw::make_hw; #[derive(Clone, Copy, Debug)] pub enum GateKind { @@ -74,7 +73,7 @@ impl IdtPointer { } } -#[make_hw( +#[bits::bits( field(RW, 0..=15, offset_1), field(RW, 16..=31, segment_selector), field(RW, 32..=34, pub ist), diff --git a/crates/arch/src/paging64.rs b/crates/arch/src/paging64.rs index c5e66e91..08433ff7 100644 --- a/crates/arch/src/paging64.rs +++ b/crates/arch/src/paging64.rs @@ -24,14 +24,12 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA */ use core::{fmt::Display, marker::PhantomPinned}; - -use hw::make_hw; use util::consts::PAGE_4K; /// The max 'bits' of physical memory the system supports. pub const MAX_PHY_MEMORY_WIDTH: usize = 48; -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -62,7 +60,7 @@ impl PageEntry4K { } } -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -103,7 +101,7 @@ impl PageEntry2M { } } -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -160,7 +158,7 @@ impl PageEntry1G { /// It would be a good idea to verify that all 'bit' or options set in this entry does exactly /// what you intend it to do before loading it. Page tables can cause the entire system to become /// unstable if mapped wrong -- **this is very important.** -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -213,7 +211,7 @@ impl PageEntryLvl2 { /// It would be a good idea to verify that all 'bit' or options set in this entry does exactly /// what you intend it to do before loading it. Page tables can cause the entire system to become /// unstable if mapped wrong -- **this is very important.** -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -266,7 +264,7 @@ impl PageEntryLvl3 { /// It would be a good idea to verify that all 'bit' or options set in this entry does exactly /// what you intend it to do before loading it. Page tables can cause the entire system to become /// unstable if mapped wrong -- **this is very important.** -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), @@ -319,7 +317,7 @@ impl PageEntryLvl4 { /// It would be a good idea to verify that all 'bit' or options set in this entry does exactly /// what you intend it to do before loading it. Page tables can cause the entire system to become /// unstable if mapped wrong -- **this is very important.** -#[make_hw( +#[bits::bits( field(RW, 0, pub present), field(RW, 1, pub read_write), field(RW, 2, pub user_access), diff --git a/crates/arch/src/registers.rs b/crates/arch/src/registers.rs index dcdae4f2..0191658f 100644 --- a/crates/arch/src/registers.rs +++ b/crates/arch/src/registers.rs @@ -23,8 +23,6 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -use hw::make_hw; - use crate::CpuPrivilege; #[repr(C)] @@ -195,7 +193,7 @@ impl SegmentRegisters { } } -#[make_hw( +#[bits::bits( field(RW, 0, pub protected_mode), field(RW, 1, pub monitor_co_processor), field(RW, 2, pub x87_fpu_emulation), @@ -306,7 +304,7 @@ pub mod cr2 { } } -#[make_hw( +#[bits::bits( field(RW, 3, pub page_level_write_through), field(RW, 4, pub page_level_cache_disable), field(RWNS, 12..=63, pub page_directory_base_register) @@ -360,7 +358,7 @@ pub mod cr3 { } } -#[make_hw( +#[bits::bits( field(RW, 0, pub v8086), field(RW, 1, pub protected_mode_virtual_interrupts), field(RW, 2, pub time_stamp_disable), @@ -434,7 +432,7 @@ pub mod cr4 { } } -#[make_hw( +#[bits::bits( field(RO, 0, pub carry), field(RO, 2, pub parity), field(RO, 4, pub auxiliary), @@ -515,7 +513,7 @@ pub unsafe fn write_msr(msr_number: u32, value: u64) { ) } -#[make_hw( +#[bits::bits( field(RW, 0, pub syscall_extensions), field(RW, 8, pub long_mode_enable), field(RW, 10, pub long_mode_active), @@ -542,13 +540,13 @@ pub mod ia32_efer { pub mod amd_syscall { use crate::CpuPrivilege; - use super::{make_hw, read_msr, write_msr, Segment}; + use super::{read_msr, write_msr, Segment}; const STAR: u32 = 0xC0000081; const LSTAR: u32 = 0xC0000082; const CSTAR: u32 = 0xC0000083; const SFMASK: u32 = 0xC0000084; - #[make_hw( + #[bits::bits( field(RW, 32..48, pub syscall_target_code_segment), field(RW, 48..64, pub sysret_target_code_segment), )] diff --git a/crates/hw-macro/Cargo.toml b/crates/bits-macro/Cargo.toml similarity index 93% rename from crates/hw-macro/Cargo.toml rename to crates/bits-macro/Cargo.toml index 747e656f..156aa159 100644 --- a/crates/hw-macro/Cargo.toml +++ b/crates/bits-macro/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "hw-macro" +name = "bits-macro" edition = "2021" version.workspace = true authors.workspace = true diff --git a/crates/hw-macro/src/lib.rs b/crates/bits-macro/src/lib.rs similarity index 97% rename from crates/hw-macro/src/lib.rs rename to crates/bits-macro/src/lib.rs index 7d8fb735..e1c404b0 100644 --- a/crates/hw-macro/src/lib.rs +++ b/crates/bits-macro/src/lib.rs @@ -35,7 +35,7 @@ pub(crate) mod make_hw_parse; pub(crate) mod provider_parse; #[proc_macro_attribute] -pub fn make_hw(args: TokenStream, input: TokenStream) -> TokenStream { +pub fn bits(args: TokenStream, input: TokenStream) -> TokenStream { let macro_fields = parse_macro_input!(args as make_hw_parse::MakeHwMacroInput); // Parse the Struct/Module diff --git a/crates/hw-macro/src/macro_gen.rs b/crates/bits-macro/src/macro_gen.rs similarity index 100% rename from crates/hw-macro/src/macro_gen.rs rename to crates/bits-macro/src/macro_gen.rs diff --git a/crates/hw-macro/src/make_hw_parse.rs b/crates/bits-macro/src/make_hw_parse.rs similarity index 100% rename from crates/hw-macro/src/make_hw_parse.rs rename to crates/bits-macro/src/make_hw_parse.rs diff --git a/crates/hw-macro/src/provider_parse.rs b/crates/bits-macro/src/provider_parse.rs similarity index 100% rename from crates/hw-macro/src/provider_parse.rs rename to crates/bits-macro/src/provider_parse.rs diff --git a/crates/bits/Cargo.toml b/crates/bits/Cargo.toml index 45975707..98c49373 100644 --- a/crates/bits/Cargo.toml +++ b/crates/bits/Cargo.toml @@ -7,3 +7,4 @@ description.workspace = true documentation.workspace = true [dependencies] +bits-macro = { workspace = true} diff --git a/crates/bits/src/lib.rs b/crates/bits/src/lib.rs index 6be97542..72881bb9 100644 --- a/crates/bits/src/lib.rs +++ b/crates/bits/src/lib.rs @@ -24,6 +24,8 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA */ #![no_std] + +pub use bits_macro::*; use core::ops::RangeBounds; /// # Bit Manipulation diff --git a/crates/hw/Cargo.toml b/crates/hw/Cargo.toml deleted file mode 100644 index 3b82793b..00000000 --- a/crates/hw/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "hw" -edition = "2021" -version.workspace = true -authors.workspace = true -description.workspace = true -documentation.workspace = true - -[dependencies] -hw-macro = {workspace = true} - -[dev-dependencies] -trybuild = "1.0" diff --git a/crates/hw/src/lib.rs b/crates/hw/src/lib.rs deleted file mode 100644 index ef65d3b2..00000000 --- a/crates/hw/src/lib.rs +++ /dev/null @@ -1,28 +0,0 @@ -/* - ____ __ __ _ __ - / __ \__ _____ ____ / /___ ____ _ / / (_) / -/ /_/ / // / _ `/ _ \/ __/ // / ' \ / /__/ / _ \ -\___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/ /____/_/_.__/ - Part of the Quantum OS Project - -Copyright 2024 Gavin Kellam - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and -associated documentation files (the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, publish, distribute, -sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial -portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT -NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT -OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - -#![no_std] - -pub use hw_macro::*; diff --git a/crates/hw/tests/mod/mod.rs b/crates/hw/tests/mod/mod.rs deleted file mode 100644 index c6124e84..00000000 --- a/crates/hw/tests/mod/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -use hw::hw_device; - -hw_device! { - mod test { - fn read() -> u32 { - 0 - } - - fn write(input: u32) { - println!("Got Input = {}", input); - } - } -} - -fn main() {} diff --git a/crates/hw/tests/mod/unsafe_fn.rs b/crates/hw/tests/mod/unsafe_fn.rs deleted file mode 100644 index 171f2f0d..00000000 --- a/crates/hw/tests/mod/unsafe_fn.rs +++ /dev/null @@ -1,19 +0,0 @@ -use hw::hw_device; - -hw_device! { - mod test { - pub unsafe fn read() -> u32 { - todo!() - } - - pub unsafe fn write(_value: u32) { - todo!() - } - } - - - #[field(RW, 12, test)] - pub dingus, -} - -fn main() {} diff --git a/crates/hw/tests/one/one.rs b/crates/hw/tests/one/one.rs deleted file mode 100644 index 99f4bd20..00000000 --- a/crates/hw/tests/one/one.rs +++ /dev/null @@ -1,5 +0,0 @@ -use hw::hw_device; - -hw_device! {} - -fn main() {} diff --git a/crates/hw/tests/struct/struct.rs b/crates/hw/tests/struct/struct.rs deleted file mode 100644 index cc7cb144..00000000 --- a/crates/hw/tests/struct/struct.rs +++ /dev/null @@ -1,13 +0,0 @@ -use hw::hw_device; - -hw_device! { - pub struct StructTest(u32); - - #[field(RW, 0..2, StructTest)] - pub multi, - - #[field(RW, 10, StructTest)] - pub single, -} - -fn main() {} diff --git a/crates/hw/tests/tests.rs b/crates/hw/tests/tests.rs deleted file mode 100644 index f9d3f9dc..00000000 --- a/crates/hw/tests/tests.rs +++ /dev/null @@ -1,152 +0,0 @@ -mod test { - use hw::make_hw; - - // #[test] - // fn compile_one_case() { - // let t = trybuild::TestCases::new(); - // t.pass("tests/one/*.rs"); - // } - - // #[test] - // fn compile_module_case() { - // let t = trybuild::TestCases::new(); - // t.pass("tests/mod/*.rs"); - // } - - // #[test] - // fn compile_struct_case() { - // let t = trybuild::TestCases::new(); - // t.pass("tests/struct/*.rs"); - // } - - #[test] - fn ensure_single_bit_set() { - use hw::make_hw; - - #[make_hw( - /// First Bit Field - field(RW, 0, first_bit), - /// Second Bit Field - field(RW, 1, second_bit), - )] - #[derive(Clone, Copy)] - struct ExampleStruct(u8); - - let mut ex = ExampleStruct(0); - - assert_eq!(ex.0, 0); - assert_eq!(ex.is_first_bit_set(), false); - assert_eq!(ex.is_second_bit_set(), false); - - // Manually set the two flags - ex.0 = 0b11; - - assert_eq!(ex.is_first_bit_set(), true); - assert_eq!(ex.is_second_bit_set(), true); - - // Set first bit - ex.0 = 0; - ex.set_first_bit_flag(true); - assert_eq!(ex.0, 0b01); - assert_eq!(ex.is_first_bit_set(), true); - assert_eq!(ex.is_second_bit_set(), false); - - // Set second bit - ex.0 = 0; - ex.set_second_bit_flag(true); - assert_eq!(ex.0, 0b10); - assert_eq!(ex.is_first_bit_set(), false); - assert_eq!(ex.is_second_bit_set(), true); - - // Unset first bit, making zero - ex.0 = 0b01; - ex.set_first_bit_flag(false); - assert_eq!(ex.0, 0b00); - assert_eq!(ex.is_first_bit_set(), false); - assert_eq!(ex.is_second_bit_set(), false); - - // Unset first bit, leaving second bit - ex.0 = 0b11; - ex.set_first_bit_flag(false); - assert_eq!(ex.0, 0b10); - assert_eq!(ex.is_first_bit_set(), false); - assert_eq!(ex.is_second_bit_set(), true); - - // Unset second bit, making zero - ex.0 = 0b10; - ex.set_second_bit_flag(false); - assert_eq!(ex.0, 0b00); - assert_eq!(ex.is_first_bit_set(), false); - assert_eq!(ex.is_second_bit_set(), false); - - // Unset second bit, leaving first bit - ex.0 = 0b11; - ex.set_second_bit_flag(false); - assert_eq!(ex.0, 0b01); - assert_eq!(ex.is_first_bit_set(), true); - assert_eq!(ex.is_second_bit_set(), false); - } - - #[test] - fn ensure_multi_bit_set() { - #[make_hw( - /// First Bit Field - field(RW, 0..2, first), - /// Second Bit Field - field(RW, 2..5, second), - )] - #[derive(Clone, Copy)] - struct ExampleMultiStruct(u8); - - let mut ex = ExampleMultiStruct(0); - - // All zero - assert_eq!(ex.0, 0b00); - assert_eq!(ex.get_first(), 0); - assert_eq!(ex.get_second(), 0); - - // Only first set - ex.0 = 0b11; - assert_eq!(ex.get_first(), 0b11); - assert_eq!(ex.get_second(), 0); - - // Only set second - ex.0 = 0b11100; - assert_eq!(ex.get_first(), 0b0); - assert_eq!(ex.get_second(), 0b111); - - // Set None - ex.0 = 0b11100000; - assert_eq!(ex.get_first(), 0b0); - assert_eq!(ex.get_second(), 0b0); - - // Check first - for i in 0..=3 { - ex.0 = 0b0; - ex.set_first(i); - - assert_eq!(ex.0, i); - assert_eq!(ex.get_first(), i); - assert_eq!(ex.get_second(), 0b0); - } - - // Check Second - for i in 0..=7 { - ex.0 = 0b0; - ex.set_second(i); - - assert_eq!(ex.0 >> 2, i); - assert_eq!(ex.get_first(), 0); - assert_eq!(ex.get_second(), i); - } - - // Check all bits zeroed - ex.0 = u8::MAX; - ex.set_first(0); - ex.set_second(0); - - assert_eq!(ex.0, 0b11100000); - assert_eq!(ex.get_first(), 0); - assert_eq!(ex.get_second(), 0); - } -} diff --git a/crates/mem/Cargo.toml b/crates/mem/Cargo.toml index 3516ab95..9bde80ea 100644 --- a/crates/mem/Cargo.toml +++ b/crates/mem/Cargo.toml @@ -11,7 +11,7 @@ lldebug = {workspace = true} util = {workspace = true} boolvec = {workspace = true, optional = true} arch = {workspace = true} -hw = {workspace = true} +bits = {workspace = true} spin = "0.9.8" [features] diff --git a/crates/mem/src/paging.rs b/crates/mem/src/paging.rs index f319f781..dc4242c7 100644 --- a/crates/mem/src/paging.rs +++ b/crates/mem/src/paging.rs @@ -42,7 +42,6 @@ use arch::{ }, registers::cr3, }; -use hw::make_hw; use lldebug::logln; use spin::RwLock; use util::consts::PAGE_4K; @@ -826,7 +825,7 @@ impl Virt2PhysMapping { } /// Options for mapping a page -#[make_hw( +#[bits::bits( /// If there is already a mapped page here, override it field(RW, 0, pub overwrite), /// If you are mapping a page entry with permissive options (say the USER bit) @@ -850,7 +849,7 @@ impl Virt2PhysMapping { pub struct VmOptions(usize); /// Permissions for mapping a page -#[make_hw( +#[bits::bits( /// Make execute is possible field(RW, 0, pub exec), /// Make reading possible