1
0
Fork 0
forked from AbleOS/ableos

allocator work

This commit is contained in:
able 2022-06-22 13:59:24 -05:00
parent 236949682e
commit 5f9923272d
9 changed files with 235 additions and 38 deletions

View file

@ -1,6 +1,6 @@
[build] [build]
# target = "riscv64gc-unknown-none-elf" target = "riscv64gc-unknown-none-elf"
target = "json_targets/x86_64-ableos.json" # target = "json_targets/x86_64-ableos.json"
[unstable] [unstable]
build-std = ["core", "compiler_builtins", "alloc"] build-std = ["core", "compiler_builtins", "alloc"]

View file

@ -12,23 +12,25 @@ run-args = [
"--nodefaults", "--nodefaults",
"-cpu", "-cpu",
"Broadwell-v3", "Broadwell-v3",
"-m",
"4G",
"-serial", "-serial",
"stdio", "stdio",
"-smp", "-smp",
"cores=2", "cores=2",
"-soundhw",
"-soundhw", "pcspk", "pcspk",
"-device", "VGA", "-device",
"VGA",
# "-device", "virtio-gpu-pci", # "-device", "virtio-gpu-pci",
# "-machine", "pcspk-audiodev=0", # "-machine", "pcspk-audiodev=0",
"-qmp", "-qmp",
"unix:../qmp-sock,server,nowait" "unix:../qmp-sock,server,nowait",
] ]
@ -57,7 +59,6 @@ embedded-graphics="*"
pc-keyboard = "0.5" pc-keyboard = "0.5"
[dependencies.logos] [dependencies.logos]
version = "0.12" version = "0.12"
default-features = false default-features = false
@ -121,7 +122,6 @@ version = "*"
git = "https://git.ablecorp.us:443/able/externc-libm.git" git = "https://git.ablecorp.us:443/able/externc-libm.git"
[target.'cfg(target_arch = "riscv")'.dependencies] [target.'cfg(target_arch = "riscv")'.dependencies]
riscv = "*" riscv = "*"

View file

@ -1,10 +1,63 @@
//! The allocator to be implemented by ableOS //! 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 alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut; use core::{fmt::Display, ptr::null_mut};
pub struct AAloc; const HEAP_START: usize = 600_000_000;
unsafe impl GlobalAlloc for AAloc { 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<MemoryRegion>; 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 { unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
println!("Allocating memory"); println!("Allocating memory");

View file

@ -1,12 +1,12 @@
mod aalloc; pub mod aalloc;
pub use aalloc::*;
use linked_list_allocator::LockedHeap;
pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_MULTIPLIER: usize = 100000; pub const HEAP_MULTIPLIER: usize = 100000;
pub const HEAP_BASE: usize = 100; pub const HEAP_BASE: usize = 100;
pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER; pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER;
/*
#[global_allocator] #[global_allocator]
pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
@ -14,3 +14,6 @@ pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
panic!("allocation error: {:?}", layout) panic!("allocation error: {:?}", layout)
} }
*/

View file

@ -64,6 +64,9 @@ pub mod virtio;
pub mod wasm; pub mod wasm;
pub mod wasm_jumploader; pub mod wasm_jumploader;
pub mod allocator;
// pub use allocator as aalloc;
mod unicode_utils; mod unicode_utils;
pub mod vga_e; pub mod vga_e;

View file

@ -1,3 +1,4 @@
// use crate::aalloc::aalloc;
use crate::arch::generate_process_pass; use crate::arch::generate_process_pass;
use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2};
use crate::devices::pci; use crate::devices::pci;
@ -5,7 +6,7 @@ use crate::filesystem::FILE_SYSTEM;
use crate::rhai_shell::shell; use crate::rhai_shell::shell;
use crate::rhai_shell::KEYBUFF; use crate::rhai_shell::KEYBUFF;
use crate::wasm_jumploader::run_program; use crate::wasm_jumploader::run_program;
use crate::{SCREEN_BUFFER}; use crate::SCREEN_BUFFER;
use acpi::{AcpiTables, PlatformInfo}; use acpi::{AcpiTables, PlatformInfo};
use cpuio::inb; use cpuio::inb;
use cpuio::outb; use cpuio::outb;
@ -110,8 +111,7 @@ pub fn scratchpad() {
debug!("end the graphics"); debug!("end the graphics");
// */ // */
// kernel::aalloc::AAlloc::intialize();
real_shell(); real_shell();
} }

125
kernel/src/aalloc/aalloc.rs Normal file
View file

@ -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<MemoryRegion>; 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")
}
}

12
kernel/src/aalloc/mod.rs Normal file
View file

@ -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)
}
*/

View file

@ -8,6 +8,7 @@
extern crate alloc; extern crate alloc;
pub mod aalloc;
pub mod allocator; pub mod allocator;
pub mod arch; pub mod arch;
pub mod device_interface; pub mod device_interface;