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]
# 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
# ableos/src/arch/riscv/virt.lds

View file

@ -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"

View file

@ -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<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 {
println!("Allocating memory");

View file

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

View file

@ -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;

View file

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

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;
pub mod aalloc;
pub mod allocator;
pub mod arch;
pub mod device_interface;