diff options
| author | Tianhao Wang <wth@riseup.net> | 2024-02-05 14:04:25 +0100 |
|---|---|---|
| committer | Tianhao Wang <shrik3@mailbox.org> | 2024-06-11 15:13:38 +0200 |
| commit | 5e9190ff1a7d35e812b42c5bb9cbb742c2d313fb (patch) | |
| tree | 39fca5cda21500f51488193018a45f65c768d0ad | |
| parent | 959d50d9117a0ed3f280bf7d5bfbffd1fa1a5740 (diff) | |
MM: add modules for memory management
| -rw-r--r-- | src/arch/x86_64/asm/e820.s | 2 | ||||
| -rw-r--r-- | src/arch/x86_64/linker.ld | 1 | ||||
| -rw-r--r-- | src/arch/x86_64/mem.rs | 16 | ||||
| -rw-r--r-- | src/arch/x86_64/mod.rs | 1 | ||||
| -rw-r--r-- | src/defs.rs | 13 | ||||
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | src/mm/mod.rs | 1 | ||||
| -rw-r--r-- | src/mm/pma.rs | 9 |
8 files changed, 44 insertions, 0 deletions
diff --git a/src/arch/x86_64/asm/e820.s b/src/arch/x86_64/asm/e820.s new file mode 100644 index 0000000..59c672a --- /dev/null +++ b/src/arch/x86_64/asm/e820.s @@ -0,0 +1,2 @@ +; getting an E820 memory map, code from osdev wiki. +; this only works in real mode ... how to do it in long mode? diff --git a/src/arch/x86_64/linker.ld b/src/arch/x86_64/linker.ld index 7a03770..ae77deb 100644 --- a/src/arch/x86_64/linker.ld +++ b/src/arch/x86_64/linker.ld @@ -71,4 +71,5 @@ SECTIONS *(".debug_aranges") } */ + PROVIDE (KERNEL_END = .); } diff --git a/src/arch/x86_64/mem.rs b/src/arch/x86_64/mem.rs new file mode 100644 index 0000000..52ccdec --- /dev/null +++ b/src/arch/x86_64/mem.rs @@ -0,0 +1,16 @@ +// to detect available memory to initialize PMA +// https://wiki.osdev.org/Detecting_Memory_(x86) +use core::arch::asm; +use crate::io::*; + +extern "C" { + fn do_e820(start: usize) -> u32; +} + +// Getting an E820 Memory Map -- osdev wiki +pub fn prob_mem_bios(){ + unsafe { + let res = do_e820(0x8000); + println!("res is {}", res) + } +} diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 9429c8f..c3b226e 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -2,3 +2,4 @@ pub mod cpu; pub mod interrupt; pub mod io_port; pub mod misc; +pub mod mem; diff --git a/src/defs.rs b/src/defs.rs new file mode 100644 index 0000000..3c1ff57 --- /dev/null +++ b/src/defs.rs @@ -0,0 +1,13 @@ +pub struct Mem; +impl Mem { + // units + pub const K: usize = 1024; + pub const M: usize = 1024 * Mem::K; + pub const G: usize = 1024 * Mem::M; + // physical memory + pub const PHY_TOP: usize = 128 * Mem::M; // qemu defaults to 128 MiB phy Memory + pub const PAGE_SIZE: usize = 0x1000; + pub const PAGE_SHIFT: usize = 12; + pub const PHY_PAGES: usize = Mem::PHY_TOP >> Mem::PAGE_SHIFT; + pub const PAGE_MASK: u64 = 0xfff; +} @@ -5,6 +5,7 @@ mod arch; mod io; mod machine; +mod defs; use arch::x86_64::interrupt::pic_8259; use arch::x86_64::interrupt::pic_8259::PicDeviceInt; use core::panic::PanicInfo; diff --git a/src/mm/mod.rs b/src/mm/mod.rs new file mode 100644 index 0000000..1298d0d --- /dev/null +++ b/src/mm/mod.rs @@ -0,0 +1 @@ +pub mod pma; diff --git a/src/mm/pma.rs b/src/mm/pma.rs new file mode 100644 index 0000000..f9a40ee --- /dev/null +++ b/src/mm/pma.rs @@ -0,0 +1,9 @@ +use core::ffi::c_void; +use crate::defs::Mem; +// this is POC code, it will be ugly +extern "C" { + static KERNEL_END: *const c_void; +} + +// pub struct PageAlloctor; + |
