diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 11:49:20 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 11:49:20 +0800 |
| commit | 922c9685e9e905a4e0f4acb6ce49e2471485bf8d (patch) | |
| tree | 8a5fae77d8b0d97a5c88336656a5a0573ec9c5c7 | |
| parent | 365b581ca5c37305f9583cf08d0c25c4426f47e2 (diff) | |
init: mm
just some ground work and newtype pattern
| -rw-r--r-- | Cargo.lock | 92 | ||||
| -rw-r--r-- | kernel/Cargo.toml | 3 | ||||
| -rw-r--r-- | kernel/src/lib.rs | 2 | ||||
| -rw-r--r-- | kernel/src/mm.rs | 100 | ||||
| -rw-r--r-- | report.typ | 10 |
5 files changed, 206 insertions, 1 deletions
@@ -3,12 +3,35 @@ version = 4 [[package]] +name = "acpi" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b763acdc1d85c36d61acf97a59938f23202d0e8efe45e8759de10c02db242744" +dependencies = [ + "bit_field", + "bitflags 2.13.1", + "byteorder", + "log", + "pci_types", + "spinning_top", +] + +[[package]] name = "anyhow" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] +name = "atomic" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89cbf775b137e9b968e67227ef7f775587cde3fd31b0d8599dbd0f598a48340" +dependencies = [ + "bytemuck", +] + +[[package]] name = "base16ct" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -30,6 +53,12 @@ dependencies = [ ] [[package]] +name = "bit_field" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4b40c7323adcfc0a41c4b88143ed58346ff65a288fc144329c5c45e05d70c6" + +[[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -97,6 +126,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" [[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + +[[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -327,7 +362,10 @@ dependencies = [ name = "kernel" version = "0.1.0" dependencies = [ + "acpi", + "atomic", "bootloader_api", + "linkme", "uart_16550", ] @@ -338,6 +376,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" [[package]] +name = "linkme" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3045e122bd98aef8ec3ad58ce84f0791f64e70163d1a02710af4aa11a4d54cc5" +dependencies = [ + "linkme-impl", +] + +[[package]] +name = "linkme-impl" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77060ebe535362c3da75682cd17b0431017b6e7c5661e714fc69a7ad017d1301" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] name = "linux-raw-sys" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -350,6 +408,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955be5d0ca0465caf127165acb47964f911e2bc26073e865deb8be7189302faf" [[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] name = "log" version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -405,6 +472,16 @@ dependencies = [ ] [[package]] +name = "pci_types" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c2a105c657261a938ff68ee231c199a3d80eef33976004829de761ef5b1a9b" +dependencies = [ + "bit_field", + "bitflags 2.13.1", +] + +[[package]] name = "percent-encoding" version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -515,6 +592,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" [[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] name = "serde" version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -590,6 +673,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] +name = "spinning_top" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96d2d1d716fb500937168cc09353ffdc7a012be8475ac7308e1bdf0e3923300" +dependencies = [ + "lock_api", +] + +[[package]] name = "subtle" version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index fda2a4a..e1e5f5a 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -4,5 +4,8 @@ version = "0.1.0" edition = "2024" [dependencies] +acpi = "6.1.1" +atomic = "0.6.1" bootloader_api = { git = "https://github.com/rust-osdev/bootloader" } +linkme = "0.3.37" uart_16550 = "0.8.0" diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 2832ab2..ed7bd16 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,4 +1,6 @@ #![no_std] +pub mod init; pub mod io; +pub mod mm; pub mod pm; diff --git a/kernel/src/mm.rs b/kernel/src/mm.rs new file mode 100644 index 0000000..0a2c5ad --- /dev/null +++ b/kernel/src/mm.rs @@ -0,0 +1,100 @@ +use core::ops::Deref; + +use atomic::Atomic; + +static KERNEL_VIRTUAL_BASE: Atomic<PhysicalMemoryAddress> = + Atomic::new(PhysicalMemoryAddress(0xFFFFFFFF80000000)); + +#[derive(Clone, Copy)] +pub struct VirtualMemoryAddress(pub usize); + +impl Deref for VirtualMemoryAddress { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From<usize> for VirtualMemoryAddress { + fn from(value: usize) -> Self { + VirtualMemoryAddress(value) + } +} + +impl From<VirtualMemoryAddress> for usize { + fn from(value: VirtualMemoryAddress) -> Self { + value.0 + } +} + +impl From<PhysicalMemoryAddress> for VirtualMemoryAddress { + fn from(value: PhysicalMemoryAddress) -> Self { + VirtualMemoryAddress(value.0) + } +} + +#[derive(Clone, Copy)] +pub struct PhysicalMemoryAddress(pub usize); + +impl Deref for PhysicalMemoryAddress { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From<usize> for PhysicalMemoryAddress { + fn from(value: usize) -> Self { + PhysicalMemoryAddress(value) + } +} + +impl From<PhysicalMemoryAddress> for usize { + fn from(value: PhysicalMemoryAddress) -> Self { + value.0 + } +} + +impl From<VirtualMemoryAddress> for PhysicalMemoryAddress { + fn from(value: VirtualMemoryAddress) -> Self { + PhysicalMemoryAddress(value.0) + } +} + +#[derive(Clone, Copy)] +pub enum MemoryAddress { + Virtual(VirtualMemoryAddress), + Physical(PhysicalMemoryAddress), +} + +impl From<VirtualMemoryAddress> for MemoryAddress { + fn from(value: VirtualMemoryAddress) -> Self { + MemoryAddress::Virtual(value) + } +} + +impl From<PhysicalMemoryAddress> for MemoryAddress { + fn from(value: PhysicalMemoryAddress) -> Self { + MemoryAddress::Physical(value) + } +} + +impl From<MemoryAddress> for usize { + fn from(value: MemoryAddress) -> Self { + match value { + MemoryAddress::Virtual(v) => *v, + MemoryAddress::Physical(p) => *p, + } + } +} + +impl From<MemoryAddress> for VirtualMemoryAddress { + fn from(value: MemoryAddress) -> Self { + match value { + MemoryAddress::Virtual(v) => v, + MemoryAddress::Physical(p) => VirtualMemoryAddress(p.0), + } + } +} @@ -1,6 +1,6 @@ i got inspiration from this #link("https://os.phil-opp.com")[blog] -booatloader selection: +== booatloader selection i could just use qemu skipping to kernel but i wanted to explore about bootloaders too since im making a os, but i dont wanna spend all my time just doing repetitive stuff and dealing with legacy stuff two realistic options only: #link("https://crates.io/crates/bootloader_api")[bootloader_api] which is what the aforementioned blog uses @@ -11,6 +11,14 @@ rejected because: - due to that it's tightly coupled with it #link("https://crates.io/crates/limine")[limine] - cons: no documentation +=== changed to bootloader api as it's more idiomatic and appearently has better documentation == spinlock thanks ("https://www.reddit.com/r/cpp/comments/1w6us70/optimizing_a_spinlock/")[this reddit post] and subsequently ("https://david.alvarezrosa.com/posts/optimizing-a-spin-lock/")[this blog] making me realize this was even a possibility + +== mm +- using Higher Half Direct Map provided by bootloader api crate +- custom new type for virtual and physical memory for type safety + +== panic & backtrace +thanks ("https://eli.thegreenplace.net/2011/02/07/how-debuggers-work-part-3-debugging-information")[Eli] teaching basic concepts and ("https://wiki.osdev.org/Stack_Trace")[osdev] for a practical example |
