Skip to content

Commit

Permalink
Merge pull request #38 from maestro-os/module
Browse files Browse the repository at this point in the history
Refactor modules management
  • Loading branch information
llenotre authored Oct 7, 2024
2 parents b79bb42 + 35a6717 commit e7ebdfa
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 90 deletions.
5 changes: 4 additions & 1 deletion kernel/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[unstable]
build-std = ["core", "alloc"]
build-std = ["core"]

[target.x86]
runner = "scripts/qemu.sh"

[build]
# Set default target
target = "arch/x86/x86.json"
rustflags = [
"-Zexport-executable-symbols"
]
6 changes: 1 addition & 5 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ unexpected_cfgs = { level = "warn", check-cfg = [

[profile.release]
panic = "abort"
rustflags = [
"-Zexport-executable-symbols"
]

[profile.dev]
rustflags = [
"-Zexport-executable-symbols",
"-Cforce-frame-pointers=yes"
"-Cforce-frame-pointers=yes"
]
2 changes: 1 addition & 1 deletion kernel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cargo run --release # Release mode
The following command runs unit tests in QEMU:

```sh
cargo test
cargo test --lib
```


Expand Down
5 changes: 2 additions & 3 deletions kernel/src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@
#![no_std]
#![no_main]
#![feature(adt_const_params)]
#![feature(alloc_layout_extra)]
#![feature(allocator_api)]
#![feature(allow_internal_unstable)]
#![feature(array_chunks)]
#![feature(core_intrinsics)]
#![feature(custom_test_frameworks)]
#![feature(exposed_provenance)]
#![feature(lang_items)]
#![feature(once_cell_try)]
#![feature(pointer_is_aligned_to)]
#![feature(ptr_metadata)]
#![feature(strict_provenance)]
#![feature(trait_upcasting)]
#![feature(exposed_provenance)]
#![deny(fuzzy_provenance_casts)]
#![deny(missing_docs)]
#![deny(warnings)]
Expand All @@ -49,8 +50,6 @@
#![test_runner(crate::selftest::runner)]
#![reexport_test_harness_main = "kernel_selftest"]

extern crate alloc;

pub mod acpi;
pub mod cmdline;
pub mod cpu;
Expand Down
67 changes: 30 additions & 37 deletions kernel/src/memory/malloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* Maestro. If not, see <https://www.gnu.org/licenses/>.
*/

//! This module implements the global memory allocator for kernelside operations.
//!
//! The allocator is accessible through [`alloc::alloc::Global`].
//! Implementation of the global memory allocator for kernelspace operations.
mod block;
mod chunk;
Expand All @@ -27,11 +25,12 @@ use crate::{memory, memory::malloc::ptr::NonNull};
use block::Block;
use chunk::Chunk;
use core::{
alloc::{AllocError, GlobalAlloc, Layout},
alloc::{AllocError, Layout},
cmp::Ordering,
intrinsics::unlikely,
num::NonZeroUsize,
ptr,
ptr::{drop_in_place, null_mut},
ptr::drop_in_place,
};
use utils::{errno::AllocResult, lock::IntMutex};

Expand Down Expand Up @@ -121,43 +120,37 @@ unsafe fn free(mut ptr: NonNull<u8>) {
super::trace::sample("malloc", super::trace::SampleOp::Free, ptr.as_ptr() as _, 0);
}

/// The global allocator for the kernel.
struct Malloc;

unsafe impl GlobalAlloc for Malloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let Some(size) = NonZeroUsize::new(layout.size()) else {
return null_mut();
};
alloc(size).map(|p| p.as_ptr()).unwrap_or(null_mut())
}
#[no_mangle]
unsafe fn __alloc(layout: Layout) -> AllocResult<NonNull<[u8]>> {
let Some(size) = NonZeroUsize::new(layout.size()) else {
return Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0));
};
let ptr = alloc(size)?;
Ok(NonNull::slice_from_raw_parts(ptr, size.get()))
}

unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
let Some(ptr) = NonNull::new(ptr) else {
return;
};
free(ptr);
}
#[no_mangle]
unsafe fn __realloc(
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> AllocResult<NonNull<[u8]>> {
let Some(new_size) = NonZeroUsize::new(new_layout.size()) else {
__dealloc(ptr, old_layout);
return Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0));
};
let ptr = realloc(ptr, new_size)?;
Ok(NonNull::slice_from_raw_parts(ptr, new_size.get()))
}

unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {
let Some(ptr) = NonNull::new(ptr) else {
return null_mut();
};
match NonZeroUsize::new(new_size) {
Some(new_size) => realloc(ptr, new_size)
.map(|p| p.as_ptr())
.unwrap_or(null_mut()),
None => {
free(ptr);
null_mut()
}
}
#[no_mangle]
unsafe fn __dealloc(ptr: NonNull<u8>, layout: Layout) {
if unlikely(layout.size() == 0) {
return;
}
free(ptr);
}

#[global_allocator]
static ALLOCATOR: Malloc = Malloc;

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions mod/template/.cargo/config.toml

This file was deleted.

1 change: 0 additions & 1 deletion mod/template/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion mod/template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cargo-features = ["profile-rustflags"]

[package]
name = "module_name"
name = "hello"
version = "0.1.0"
edition = "2021"

Expand Down
6 changes: 5 additions & 1 deletion mod/template/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#![no_std]
#![no_main]

// hello module, version 1.0.0
// Do not include kernel symbols in the module
#[no_link]
extern crate kernel;

// Declare the module, with its dependencies
kernel::module!([]);

/// Called on module load
Expand Down
13 changes: 6 additions & 7 deletions utils/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
//! The `Box` structure allows to hold an object on the heap and handles its
//! memory properly.
use crate::{errno::AllocResult, AllocError, TryClone};
use alloc::alloc::Global;
use crate::{errno::AllocResult, AllocError, TryClone, __alloc, __dealloc};
use core::{
alloc::{Allocator, Layout},
alloc::Layout,
borrow::{Borrow, BorrowMut},
fmt,
marker::Unsize,
Expand All @@ -49,11 +48,11 @@ impl<T> Box<T> {
pub fn new(value: T) -> AllocResult<Box<T>> {
let layout = Layout::for_value(&value);
let ptr = if layout.size() > 0 {
let ptr = Global.allocate(layout)?.cast();
unsafe {
let ptr = __alloc(layout)?.cast();
ptr.write(value);
ptr
}
ptr
} else {
// Prevent double drop
mem::forget(value);
Expand All @@ -69,7 +68,7 @@ impl<T> Box<T> {
let layout = Layout::for_value(&*self);
unsafe {
let t = self.ptr.read();
Global.deallocate(self.ptr.cast(), layout);
__dealloc(self.ptr.cast(), layout);
mem::forget(self);
t
}
Expand Down Expand Up @@ -183,7 +182,7 @@ impl<T: ?Sized> Drop for Box<T> {
let inner = self.ptr.as_mut();
let layout = Layout::for_value(inner);
drop_in_place(inner);
Global.deallocate(self.ptr.cast(), layout);
__dealloc(self.ptr.cast(), layout);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions utils/src/collections/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
use crate::{
errno::{AllocResult, CollectResult},
AllocError, TryClone,
AllocError, TryClone, __alloc, __dealloc,
};
use alloc::alloc::Global;
use core::{
alloc::{Allocator, Layout},
alloc::Layout,
borrow::Borrow,
cell::UnsafeCell,
cmp::Ordering,
Expand Down Expand Up @@ -78,7 +77,8 @@ struct Node<K, V> {
#[inline]
unsafe fn drop_node<K, V>(ptr: NonNull<Node<K, V>>) -> (K, V) {
let node = ptr.read();
Global.deallocate(ptr.cast(), Layout::new::<Node<K, V>>());
let layout = Layout::new::<Node<K, V>>();
__dealloc(ptr.cast(), layout);
let Node::<K, V> {
key,
value,
Expand Down Expand Up @@ -107,11 +107,12 @@ impl<K, V> Node<K, V> {
key,
value,
};
let ptr = Global.allocate(Layout::new::<Self>())?.cast();
let layout = Layout::new::<Self>();
unsafe {
let ptr = __alloc(layout)?.cast();
ptr.write(s);
Ok(ptr)
}
Ok(ptr)
}

/// Tells whether the node is red.
Expand Down
9 changes: 4 additions & 5 deletions utils/src/collections/hashmap/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

//! Inner table implementation for hashmaps.
use crate::errno::AllocResult;
use alloc::alloc::Global;
use crate::{__alloc, __dealloc, errno::AllocResult};
use core::{
alloc::{Allocator, Layout},
alloc::Layout,
borrow::Borrow,
intrinsics::{likely, unlikely},
iter::FusedIterator,
Expand Down Expand Up @@ -56,7 +55,7 @@ pub fn init_data<K, V>(capacity: usize) -> AllocResult<NonNull<u8>> {
let (size, ctrl_off) = buff_size::<K, V>(capacity);
unsafe {
let layout = Layout::from_size_align_unchecked(size, ALIGN);
let mut data = Global.allocate(layout)?;
let mut data = __alloc(layout)?;
data.as_mut()[ctrl_off..].fill(CTRL_EMPTY);
Ok(data.cast())
}
Expand Down Expand Up @@ -297,7 +296,7 @@ impl<K, V> Drop for RawTable<K, V> {
let size = buff_size::<K, V>(self.capacity).0;
unsafe {
let layout = Layout::from_size_align_unchecked(size, ALIGN);
Global.deallocate(self.data, layout)
__dealloc(self.data, layout)
}
}
}
23 changes: 13 additions & 10 deletions utils/src/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
//! A dynamically-resizable array of elements.
use crate::{
__alloc, __dealloc, __realloc,
errno::{AllocResult, CollectResult},
TryClone,
};
use alloc::alloc::Global;
use core::{
alloc::{AllocError, Allocator, Layout},
alloc::{AllocError, Layout},
cmp::max,
fmt,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -60,14 +60,17 @@ impl<T> RawVec<T> {
self.free();
return Ok(());
}
let old_layout = Layout::array::<T>(self.capacity).unwrap();
let new_layout = Layout::array::<T>(capacity).map_err(|_| AllocError)?;
let new = if self.capacity > 0 {
let old_layout = Layout::array::<T>(self.capacity).unwrap();
// SAFETY: memory is rewritten when the object is placed into the vector
unsafe { Global.grow(self.data.cast(), old_layout, new_layout)? }.cast()
} else {
Global.allocate(new_layout)?.cast()
};
let new = unsafe {
if self.capacity > 0 {
// SAFETY: memory is rewritten when the object is placed into the vector
__realloc(self.data.cast(), old_layout, new_layout)?
} else {
__alloc(new_layout)?
}
}
.cast();
self.data = new;
self.capacity = capacity;
Ok(())
Expand All @@ -81,7 +84,7 @@ impl<T> RawVec<T> {
let layout = Layout::array::<T>(self.capacity).unwrap();
// SAFETY: the underlying data is no longer used
unsafe {
Global.deallocate(self.data.cast(), layout);
__dealloc(self.data.cast(), layout);
}
self.capacity = 0;
}
Expand Down
Loading

0 comments on commit e7ebdfa

Please sign in to comment.