From 5f9923272d9d4d79d2a44b8ac8ec873399e1495d Mon Sep 17 00:00:00 2001 From: able Date: Wed, 22 Jun 2022 13:59:24 -0500 Subject: [PATCH] allocator work --- ableos/.cargo/config.toml | 6 +- ableos/Cargo.toml | 52 +++++++------- ableos/src/allocator/aalloc.rs | 59 +++++++++++++++- ableos/src/allocator/mod.rs | 9 ++- ableos/src/lib.rs | 3 + ableos/src/scratchpad.rs | 6 +- kernel/src/aalloc/aalloc.rs | 125 +++++++++++++++++++++++++++++++++ kernel/src/aalloc/mod.rs | 12 ++++ kernel/src/lib.rs | 1 + 9 files changed, 235 insertions(+), 38 deletions(-) create mode 100644 kernel/src/aalloc/aalloc.rs create mode 100644 kernel/src/aalloc/mod.rs diff --git a/ableos/.cargo/config.toml b/ableos/.cargo/config.toml index 79f7f7a..bc932bb 100644 --- a/ableos/.cargo/config.toml +++ b/ableos/.cargo/config.toml @@ -1,6 +1,6 @@ [build] -# target = "riscv64gc-unknown-none-elf" -target = "json_targets/x86_64-ableos.json" +target = "riscv64gc-unknown-none-elf" +# target = "json_targets/x86_64-ableos.json" [unstable] build-std = ["core", "compiler_builtins", "alloc"] @@ -12,4 +12,4 @@ runner = "bootimage runner" [target.riscv64gc-unknown-none-elf] rustflags = "-C link-arg=-Tableos/src/arch/riscv/virt.lds" -# ableos/src/arch/riscv/virt.lds \ No newline at end of file +# ableos/src/arch/riscv/virt.lds diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index db9a1aa..b660d1e 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -9,34 +9,36 @@ panic = "abort" [package.metadata.bootimage] run-args = [ - "--nodefaults", - "-cpu", - "Broadwell-v3", - - "-serial", - "stdio", - "-smp", - "cores=2", + "--nodefaults", + "-cpu", + "Broadwell-v3", + "-m", + "4G", + "-serial", + "stdio", + "-smp", + "cores=2", + "-soundhw", + "pcspk", + "-device", + "VGA", + # "-device", "virtio-gpu-pci", - "-soundhw", "pcspk", -"-device", "VGA", -# "-device", "virtio-gpu-pci", - -# "-machine", "pcspk-audiodev=0", + # "-machine", "pcspk-audiodev=0", - "-qmp", - "unix:../qmp-sock,server,nowait" + "-qmp", + "unix:../qmp-sock,server,nowait", ] test-args = [ - "-device", - "isa-debug-exit,iobase=0xf4,iosize=0x04", - "-serial", - "stdio", + "-device", + "isa-debug-exit,iobase=0xf4,iosize=0x04", + "-serial", + "stdio", ] [dependencies] @@ -50,14 +52,13 @@ picorand = "0.1.0" watson = "0.4" genfs = "0.1.0" rhai = "1.6.0" -libwasm = {git="https://git.ablecorp.us:443/able/libwasm.git"} +libwasm = { git = "https://git.ablecorp.us:443/able/libwasm.git" } axel = { git = "https://git.ablecorp.us/able/aos_userland" } versioning = { git = "https://git.ablecorp.us/able/aos_userland" } -embedded-graphics="*" +embedded-graphics = "*" pc-keyboard = "0.5" - [dependencies.logos] version = "0.12" default-features = false @@ -121,9 +122,8 @@ version = "*" git = "https://git.ablecorp.us:443/able/externc-libm.git" - [target.'cfg(target_arch = "riscv")'.dependencies] -riscv="*" +riscv = "*" [target.'cfg(target_arch = "x86_64")'.dependencies] bootloader = { version = "0.9.8", features = ["map_physical_memory"] } @@ -132,6 +132,6 @@ pic8259 = "0.10.1" uart_16550 = "0.2.0" volatile = "0.2.6" x86_64 = "*" -pc-beeper = {git = "https://github.com/AbleOS/pc-beeper"} -vga = "*" +pc-beeper = { git = "https://github.com/AbleOS/pc-beeper" } +vga = "*" acpi = "4.1.0" diff --git a/ableos/src/allocator/aalloc.rs b/ableos/src/allocator/aalloc.rs index 7c1b887..7534983 100644 --- a/ableos/src/allocator/aalloc.rs +++ b/ableos/src/allocator/aalloc.rs @@ -1,10 +1,63 @@ //! The allocator to be implemented by ableOS +//! +//! NOTE: All memory regions are taken from https://wiki.osdev.org/Memory_Map_(x86) use alloc::alloc::{GlobalAlloc, Layout}; -use core::ptr::null_mut; +use core::{fmt::Display, ptr::null_mut}; -pub struct AAloc; -unsafe impl GlobalAlloc for AAloc { +const HEAP_START: usize = 600_000_000; +const BLOCK_SIZE: usize = 1024; +const BLOCK_COUNT: usize = 512; +#[derive(Debug, Clone, Copy)] +pub struct MemoryRegion { + start: usize, + end: usize, +} +#[derive(Debug, Clone, Copy)] +pub struct AAlloc { + current_region: usize, + memory_regions: [Option; 512], +} + +impl AAlloc { + pub fn add_region(&mut self, mem: MemoryRegion) { + self.memory_regions[self.current_region] = Some(mem); + self.current_region += 1; + } + + pub fn intialize() { + info!("Heap Start: {}", HEAP_START); + info!("Heap Size: {}", BLOCK_SIZE * BLOCK_COUNT); + info!("Heap End: {}", HEAP_START + BLOCK_SIZE * BLOCK_COUNT); + + let mut aalloc = AAlloc { + current_region: 0, + memory_regions: [None; 512], + }; + + aalloc.add_region(MemoryRegion { + start: 0x00100000, + end: 0x00EFFFFF, + }); + debug!("{}", aalloc); + } +} + +impl Display for AAlloc { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + write!(f, "AAlloc {{\n\tcurrent_region: {},\n", self.current_region)?; + + for x in 0..self.current_region { + if let Some(region) = self.memory_regions[x] { + write!(f, "\tRegion {}: {:?}\n", x, region)?; + } + } + write!(f, "}}")?; + Ok(()) + } +} + +unsafe impl GlobalAlloc for AAlloc { unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { println!("Allocating memory"); diff --git a/ableos/src/allocator/mod.rs b/ableos/src/allocator/mod.rs index 00e263d..9f809fa 100644 --- a/ableos/src/allocator/mod.rs +++ b/ableos/src/allocator/mod.rs @@ -1,12 +1,12 @@ -mod aalloc; - -use linked_list_allocator::LockedHeap; +pub mod aalloc; +pub use aalloc::*; pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_MULTIPLIER: usize = 100000; pub const HEAP_BASE: usize = 100; pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER; +/* #[global_allocator] pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); @@ -14,3 +14,6 @@ pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { panic!("allocation error: {:?}", layout) } + + +*/ diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 49c5830..29788bb 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -64,6 +64,9 @@ pub mod virtio; pub mod wasm; pub mod wasm_jumploader; +pub mod allocator; + +// pub use allocator as aalloc; mod unicode_utils; pub mod vga_e; diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index be88957..ca2ce06 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,3 +1,4 @@ +// use crate::aalloc::aalloc; use crate::arch::generate_process_pass; use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; use crate::devices::pci; @@ -5,7 +6,7 @@ use crate::filesystem::FILE_SYSTEM; use crate::rhai_shell::shell; use crate::rhai_shell::KEYBUFF; use crate::wasm_jumploader::run_program; -use crate::{SCREEN_BUFFER}; +use crate::SCREEN_BUFFER; use acpi::{AcpiTables, PlatformInfo}; use cpuio::inb; use cpuio::outb; @@ -110,8 +111,7 @@ pub fn scratchpad() { debug!("end the graphics"); // */ - - + // kernel::aalloc::AAlloc::intialize(); real_shell(); } diff --git a/kernel/src/aalloc/aalloc.rs b/kernel/src/aalloc/aalloc.rs new file mode 100644 index 0000000..8499449 --- /dev/null +++ b/kernel/src/aalloc/aalloc.rs @@ -0,0 +1,125 @@ +//! The allocator to be implemented by ableOS +//! +//! NOTE: All memory regions are taken from https://wiki.osdev.org/Memory_Map_(x86) + +#![allow(missing_docs)] + +use alloc::alloc::{GlobalAlloc, Layout}; +use core::{ + fmt::Display, + ptr::{self, null_mut}, +}; +use log::{debug, info, trace}; + +// const HEAP_START: usize = 600_000_000; +const HEAP_START: usize = 0x00100000; +const BLOCK_SIZE: usize = 1024; +const BLOCK_COUNT: usize = 512; + +#[derive(Debug, Clone, Copy)] +pub struct MemoryRegion { + start: usize, + end: usize, +} +impl Display for MemoryRegion { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + writeln!( + f, + "MemoryRegion {{ start: {}, end: {}, size: {} bytes}}", + self.start, + self.end, + self.end - self.start + ) + } +} +impl MemoryRegion { + pub fn new(start: usize, end: usize) -> MemoryRegion { + MemoryRegion { start, end } + } + pub fn test_region(&self) -> bool { + unsafe { + let mutptr = self.start as *mut u8; + + core::ptr::write(mutptr, 0xFF); + // trace!("{}", core::ptr::read(mutptr)); + } + + true + } +} + +#[derive(Debug, Clone, Copy)] +pub struct AAlloc { + current_region: usize, + memory_regions: [Option; 512], +} + +impl AAlloc { + fn test_regions(&self) { + for x in 0..self.current_region { + if let Some(region) = self.memory_regions[x] { + debug!("Region {}: {:?}", x, region); + } + } + } + + pub fn add_region(&mut self, mem: MemoryRegion) { + self.memory_regions[self.current_region] = Some(mem); + self.current_region += 1; + } + + pub fn intialize() { + info!("Heap Start: {}", HEAP_START); + info!("Heap Size: {}", BLOCK_SIZE * BLOCK_COUNT); + info!("Heap End: {}", HEAP_START + BLOCK_SIZE * BLOCK_COUNT); + + let mut aalloc = AAlloc { + current_region: 0, + memory_regions: [None; 512], + }; + + // BS MEMORY REGION + aalloc.add_region(MemoryRegion::new(HEAP_START, HEAP_START + 10)); + + aalloc.add_region(MemoryRegion::new(0x00007E00, 0x0007FFFF)); + + aalloc.add_region(MemoryRegion::new(0x00100000, 0x00EFFFFF)); + + // ISA Memory Hole + aalloc.add_region(MemoryRegion::new(0x00F00000, 0x00FFFFFF)); + + aalloc.add_region(MemoryRegion::new(0x0000000100000000, 0x0000000100000000)); + + aalloc.memory_regions[0].unwrap().test_region(); + debug!("{}", aalloc); + } +} + +impl Display for AAlloc { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + write!(f, "AAlloc {{\n\tcurrent_region: {},\n", self.current_region)?; + + for x in 0..self.current_region { + if let Some(region) = self.memory_regions[x] { + write!(f, "\tRegion {}: {}", x, region)?; + } + } + write!(f, "}}")?; + Ok(()) + } +} + +unsafe impl GlobalAlloc for AAlloc { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + info!("Allocating memory"); + + info!("{}", _layout.size()); + info!("{}", _layout.align()); + + null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { + panic!("dealloc should be never called") + } +} diff --git a/kernel/src/aalloc/mod.rs b/kernel/src/aalloc/mod.rs new file mode 100644 index 0000000..74ba352 --- /dev/null +++ b/kernel/src/aalloc/mod.rs @@ -0,0 +1,12 @@ +#![allow(missing_docs)] + +pub mod aalloc; +pub use aalloc::*; +/* +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) +} + + +*/ diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 5ee5a82..3cbadca 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -8,6 +8,7 @@ extern crate alloc; +pub mod aalloc; pub mod allocator; pub mod arch; pub mod device_interface;