Skip to content

Commit

Permalink
std: create a heap for user programs automatically!
Browse files Browse the repository at this point in the history
This completes our journey to having user programs looking almost completely
normal! I am pretty amazed that this hack works so successfully, but not
complaining.

Lots of work still to do in terms of making Poplar primitives nicer to work
with, but this is a good start!
  • Loading branch information
IsaacWoods committed Sep 25, 2023
1 parent e2d55d3 commit 79da376
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
2 changes: 2 additions & 0 deletions lib/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ authors = ["Isaac Woods"]
edition = "2021"

[dependencies]
poplar = { path = "../poplar" }
linked_list_allocator = "0.10.5"
19 changes: 18 additions & 1 deletion lib/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern crate alloc;

use core::panic::PanicInfo;
use linked_list_allocator::LockedHeap;

/*
* Public re-exports. Most of this is copied from real `std`, plus our `poplar` library.
Expand Down Expand Up @@ -65,16 +66,31 @@ pub mod prelude {
}
}

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();

#[no_mangle]
unsafe extern "C" fn _start() -> ! {
extern "C" {
fn main(argc: isize, argv: *const *const u8) -> isize;
}

// Initialize the heap
const HEAP_START: usize = 0x600000000;
const HEAP_SIZE: usize = 0x4000;
let heap_memory_object =
poplar::syscall::create_memory_object(HEAP_START, HEAP_SIZE, true, false, 0x0 as *mut usize).unwrap();
unsafe {
poplar::syscall::map_memory_object(&heap_memory_object, &poplar::ZERO_HANDLE, None, 0x0 as *mut usize)
.unwrap();
ALLOCATOR.lock().init(HEAP_START as *mut u8, HEAP_SIZE);
}

main(0, core::ptr::null());

poplar::syscall::early_log("Returned from main. Looping.").unwrap();
loop {
// TODO: yield here idk
poplar::syscall::yield_to_kernel();
// TODO: actually this should call an exit system call or something
}
}
Expand All @@ -88,5 +104,6 @@ fn lang_start<T>(main: fn() -> T, _argc: isize, _argv: *const *const u8, _sigpip
#[panic_handler]
pub fn handle_panic(info: &PanicInfo) -> ! {
// TODO: print a panic message
let _ = poplar::syscall::early_log("Panicked!");
loop {}
}
31 changes: 21 additions & 10 deletions user/Cargo.lock

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

2 changes: 0 additions & 2 deletions user/simple_fb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ edition = "2021"

[dependencies]
std = { path = "../../lib/std" }
poplar = { path = "../../lib/poplar", features = ["can_alloc"] }
linked_list_allocator = "0.10.2"
log = "0.4"
gfxconsole = { path = "../../lib/gfxconsole" }
ptah = { path = "../../lib/ptah" }
40 changes: 10 additions & 30 deletions user/simple_fb/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
extern crate alloc;

use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use core::mem::MaybeUninit;
use gfxconsole::{Bgr32, Format, Framebuffer, Pixel};
use linked_list_allocator::LockedHeap;
use log::info;
use poplar::{
caps::{CapabilitiesRepr, CAP_EARLY_LOGGING, CAP_GET_FRAMEBUFFER, CAP_PADDING, CAP_SERVICE_USER},
channel::Channel,
early_logger::EarlyLogger,
syscall::{self, FramebufferInfo, PixelFormat},
};
use ptah::{Deserialize, Serialize};
use std::{
mem::MaybeUninit,
poplar::{
caps::{CapabilitiesRepr, CAP_EARLY_LOGGING, CAP_GET_FRAMEBUFFER, CAP_PADDING, CAP_SERVICE_USER},
channel::Channel,
early_logger::EarlyLogger,
syscall::{self, FramebufferInfo, PixelFormat},
},
};

#[derive(Debug, Serialize, Deserialize)]
struct TestMessage {
id: usize,
message: String,
}

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();

pub fn main() {
syscall::early_log("Hello from FB").unwrap();
// Initialise the heap
const HEAP_START: usize = 0x600000000;
const HEAP_SIZE: usize = 0x4000;
let heap_memory_object =
syscall::create_memory_object(HEAP_START, HEAP_SIZE, true, false, 0x0 as *mut usize).unwrap();
unsafe {
syscall::map_memory_object(&heap_memory_object, &poplar::ZERO_HANDLE, None, 0x0 as *mut usize).unwrap();
ALLOCATOR.lock().init(HEAP_START as *mut u8, HEAP_SIZE);
}

log::set_logger(&EarlyLogger).unwrap();
log::set_max_level(log::LevelFilter::Trace);
info!("Simple framebuffer driver is running!");
Expand Down Expand Up @@ -96,7 +76,7 @@ fn make_framebuffer() -> Framebuffer<Bgr32> {
unsafe {
syscall::map_memory_object(
&framebuffer_handle,
&poplar::ZERO_HANDLE,
&std::poplar::ZERO_HANDLE,
Some(FRAMEBUFFER_ADDRESS),
0x0 as *mut _,
)
Expand Down

0 comments on commit 79da376

Please sign in to comment.