Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userspace allocator #353

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default-run = "moros"
default = ["video"]
video = []
serial = []
userspace = []

[dependencies]
acpi = "4.1.0"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ user-rust:
basename -s .rs src/bin/*.rs | xargs -I {} \
touch dsk/bin/{}
basename -s .rs src/bin/*.rs | xargs -I {} \
cargo rustc --release --bin {} -- \
cargo rustc --no-default-features --features userspace --release --bin {} -- \
-C relocation-model=static
basename -s .rs src/bin/*.rs | xargs -I {} \
cp target/x86_64-moros/release/{} dsk/bin/{}
Expand Down
Binary file modified dsk/bin/hello
Binary file not shown.
Binary file modified dsk/bin/sleep
Binary file not shown.
5 changes: 5 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#[macro_export]
macro_rules! entry_point {
($path:path) => {
use linked_list_allocator::LockedHeap;

#[cfg_attr(feature = "userspace", global_allocator)]
pub static ALLOCATOR: LockedHeap = LockedHeap::empty();

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
$crate::api::syscall::write(1, b"An exception occured!\n");
Expand Down
26 changes: 23 additions & 3 deletions src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
#![no_std]
#![no_main]

use moros::api::syscall;
extern crate alloc;

use moros::entry_point;
use moros::api::syscall;

use alloc::string::String;

entry_point!(main);

fn main(_args: &[&str]) -> usize {
fn main(args: &[&str]) -> usize {
syscall::write(1, b"Hello, World!\n");
0
let heap_start = args.as_ptr() as usize + (1 << 16);
let heap_size = 1 << 16;
if ALLOCATOR.is_locked() {
syscall::write(1, b"Allocator is locked\n");
} else {
syscall::write(1, b"Allocator is not locked\n");
}
if let Some(mut alloc) = ALLOCATOR.try_lock() {
syscall::write(1, b"Got a lock on the allocator\n");
unsafe { alloc.init(heap_start, heap_size) };
let s = String::from("Allocated string");
syscall::write(1, s.as_bytes());
0
} else {
syscall::write(1, b"Could not get a lock on the allocator\n");
1
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

extern crate alloc;

use moros::{sys, usr, print, println};

use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use moros::{sys, usr, print, println};

entry_point!(main);

Expand Down
6 changes: 3 additions & 3 deletions src/sys/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use x86_64::structures::paging::mapper::MapToError;
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB};
use x86_64::VirtAddr;

pub const HEAP_START: usize = 0x4444_4444_0000;
#[cfg_attr(not(feature = "userspace"), global_allocator)]
pub static ALLOCATOR: LockedHeap = LockedHeap::empty();

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub const HEAP_START: usize = 0x4444_4444_0000;

pub fn init_heap(mapper: &mut impl Mapper<Size4KiB>, frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Result<(), MapToError<Size4KiB>> {
// Use half of the memory for the heap, caped to 16 MB because the allocator is too slow
Expand Down
2 changes: 2 additions & 0 deletions src/sys/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ impl Process {
let args_ptr = ptr_from_addr(args_ptr as u64) as usize;
let args: &[&str] = unsafe { core::slice::from_raw_parts(args_ptr as *const &str, args_len) };
let heap_addr = self.code_addr + (self.stack_addr - self.code_addr) / 2;
sys::allocator::alloc_pages(heap_addr + (1 << 16), 3 << 16);

let mut ptr = heap_addr;
let vec: Vec<&str> = args.iter().map(|arg| {
let src_len = arg.len();
Expand Down