Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
added alloc and implemented the allocate function for the page alloca…
Browse files Browse the repository at this point in the history
…tor. Still need to implement deallocate for the page allocator and the entire subpage allocator prior to implementing the overall kernel allocator to enable the use of alloc.
  • Loading branch information
mdpatelcsecon committed Oct 5, 2024
1 parent 5a2697a commit e20612b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}
7 changes: 0 additions & 7 deletions charlotte_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion charlotte_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ panic = "abort"
panic = "abort"

[build-dependencies]
cc = "*"
walkdir = "*"

[dependencies]
Expand Down
15 changes: 8 additions & 7 deletions charlotte_core/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ pub static LOGGER: Lazy<TicketMutex<Logger>> = Lazy::new(|| {
})
});

pub enum MemType {
KernelReadWrite,
KernelReadOnly,
KernelReadExecute,
}

pub trait MemoryMap {
type Error;
type Flags;

fn get_flags(mem_type: MemType) -> Self::Flags;

/// Loads the page map into the logical processor.
unsafe fn load(&self) -> Result<(), Self::Error>;

Expand Down Expand Up @@ -120,13 +128,6 @@ pub enum HwTimerMode {
Recurrent,
}

#[derive(Debug, Copy, Clone)]
pub struct PagingParams {
pub page_size: UAddr,
pub page_shift: UAddr,
pub page_mask: UAddr,
}

#[derive(Debug, Copy, Clone)]
pub struct IsaParams {
pub paging: PagingParams,
Expand Down
24 changes: 24 additions & 0 deletions charlotte_core/src/arch/x86_64/memory/page_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod page_table;

use page_table::page_table_entry::PteFlags;
use page_table::PageTable;

use super::Error;
Expand Down Expand Up @@ -215,6 +216,29 @@ impl MemoryMap for PageMap {
type Error = Error;
type Flags = u64;

fn get_flags(mem_type: crate::arch::MemType) -> Self::Flags {
match mem_type {

Check warning on line 220 in charlotte_core/src/arch/x86_64/memory/page_map/mod.rs

View workflow job for this annotation

GitHub Actions / style-check / check (x86_64-unknown-none)

Diff in /home/runner/work/charlotte-core/charlotte-core/charlotte_core/src/arch/x86_64/memory/page_map/mod.rs
crate::arch::MemType::KernelReadWrite => {
PteFlags::PRESENT as u64
| PteFlags::WRITABLE as u64
| PteFlags::NO_EXECUTE as u64
| PteFlags::GLOBAL as u64
| PteFlags::WriteThrough as u64
},
crate::arch::MemType::KernelReadOnly => {
PteFlags::PRESENT as u64
| PteFlags::NO_EXECUTE as u64
| PteFlags::GLOBAL as u64
| PteFlags::WriteThrough as u64
},
crate::arch::MemType::KernelReadExecute => {
PteFlags::PRESENT as u64
| PteFlags::GLOBAL as u64
| PteFlags::WriteThrough as u64
},
}
}

/// Loads the page map into the logical processor.
unsafe fn load(&self) -> Result<(), Self::Error> {
if self.get_pcid() != 0 {
Expand Down
90 changes: 21 additions & 69 deletions charlotte_core/src/memory/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//! The kernel allocator provides a single unified interface for allocating and deallocating memory
//! in the kernel's address space.
use alloc::alloc::{GlobalAlloc, Layout};
use core::mem::size_of;
use core::num::NonZeroUsize;
use core::ops::Deref;
use core::ptr::NonNull;

Check warning on line 10 in charlotte_core/src/memory/allocator/mod.rs

View workflow job for this annotation

GitHub Actions / style-check / check (x86_64-unknown-none)

Diff in /home/runner/work/charlotte-core/charlotte-core/charlotte_core/src/memory/allocator/mod.rs
use spin::lazy::Lazy;

use crate::arch::x86_64;
use crate::arch::{self, Api, ArchApi, MemoryMap};
use crate::arch::{self, Api, ArchApi, MemoryMap, Api::MemoryMap};
use crate::bootinfo::KernelAddressRequest;
use crate::bootinfo::KERNEL_ADDRESS_REQUEST;
use crate::memory::address::*;
Expand All @@ -27,81 +28,32 @@ static KERNEL_HEAP_END: Lazy<VirtualAddress> = Lazy::new(|| {
VirtualAddress::try_from(kaddr_response.virtual_base()).unwrap()
});

static KERNEL_HEAP_SIZE: Lazy<usize> =
Lazy::new(|| ((*KERNEL_HEAP_END).bits() - (*KERNEL_HEAP_START).bits()) as usize);
/// The kernel allocator.
/// This allocator is used to allocate memory in the kernel's address space.
pub struct Allocator;

/// The starting size of the free region list in pages.
/// This is the minimum size of the free region list.
/// The free region list will be reallocated if it grows beyond this size.
const FREE_LIST_MIN_PAGES: usize = 16;

enum Error {
NullPtrNotAllowed,
VmmError(<<ArchApi as arch::Api>::MemoryMap as arch::MemoryMap>::Error),
PmmError(pmm::Error),
}

impl From<<<ArchApi as arch::Api>::MemoryMap as arch::MemoryMap>::Error> for Error {
fn from(e: <<ArchApi as arch::Api>::MemoryMap as arch::MemoryMap>::Error) -> Self {
Error::VmmError(e)
impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
todo!()
}
}

impl From<pmm::Error> for Error {
fn from(e: pmm::Error) -> Self {
Error::PmmError(e)
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
todo!()
}
}

/// A struct that describes a region of memory located directly behind it in the kernel heap.
struct AllocRegion {
base: VirtualAddress,
size: usize,
}
pub struct PageAllocator;

/// The kernel heap allocator.
pub struct KernelAllocator {
heap_base: VirtualAddress,
heap_size: usize,
free_list_base: NonNull<AllocRegion>,
free_list_n_pages: usize,
}

impl KernelAllocator {
/// Attempts to create a new kernel heap allocator.
///
/// # Returns
/// * `Ok(Self)` if the allocator was successfully created.
/// * `Err(Error)` if the allocator could not be created due to a null KERNEL_HEAP_START value.
fn try_new() -> Result<Self, Error> {
if let Some(free_list_ptr) = NonNull::new(<*mut AllocRegion>::from(*KERNEL_HEAP_START)) {
Ok(Self {
heap_base: *KERNEL_HEAP_START + (4 * PAGE_SIZE),
heap_size: *KERNEL_HEAP_SIZE,
free_list_base: free_list_ptr,
free_list_n_pages: FREE_LIST_MIN_PAGES,
})
} else {
Err(Error::NullPtrNotAllowed)
}
impl PageAllocator {
pub fn allocate(size: NonZeroUsize, alignment: NonZeroUsize) -> Result<VirtualAddress, Error> {

Check warning on line 48 in charlotte_core/src/memory/allocator/mod.rs

View workflow job for this annotation

GitHub Actions / style-check / check (x86_64-unknown-none)

Diff in /home/runner/work/charlotte-core/charlotte-core/charlotte_core/src/memory/allocator/mod.rs
// find a virtual address range to map the pages to
let vbase: VirtualAddress = Api::MemoryMap.find_free_range(size, alignment)?;
Api::MemoryMap.map_pages(vbase, size, Api::MemoryMap::get_flags(crate::arch::MemType::KernelReadWrite))?;
Ok(vbase)
}

/// Allocates non-contiguous page frames and maps them to a contiguous virtual address range.
///
/// # Arguments
/// * `base` - The base virtual address to map the pages to
/// * `n_frames` - The number of page frames to allocate and map
fn map_pages_at(base: VirtualAddress, n_frames: usize) -> Result<(), Error> {
let mut pfa = PHYSICAL_FRAME_ALLOCATOR.lock();
let mut frame = PhysicalAddress::try_from(0).unwrap();

for i in 0..n_frames as u64 {
frame = pfa.allocate()?;
let vaddr = base + (i * PAGE_SIZE);
// Implement ISA agnostic flag sets to allow mapping of pages for kernel and userspace
// in an ISA independent way.
todo!("Implement ISA independent page mapping");
}
Ok(())
pub fn deallocate(paddr: PhysicalAddress, size: NonZeroUsize) -> Result<(), Error> {
todo!()

Check warning on line 56 in charlotte_core/src/memory/allocator/mod.rs

View workflow job for this annotation

GitHub Actions / style-check / check (x86_64-unknown-none)

Diff in /home/runner/work/charlotte-core/charlotte-core/charlotte_core/src/memory/allocator/mod.rs
}
}

0 comments on commit e20612b

Please sign in to comment.