1
0
Fork 0
forked from AbleOS/ableos
This commit is contained in:
koniifer 2025-01-08 01:25:11 +00:00
parent 5616e0ba03
commit 62f9fa1fd4
7 changed files with 53 additions and 20 deletions

View file

@ -1,6 +1,6 @@
use { use {
crate::holeybytes::{kernel_services::block_read, Vm}, crate::holeybytes::{kernel_services::block_read, Vm},
alloc::alloc::{alloc, dealloc}, alloc::alloc::{alloc, alloc_zeroed, dealloc, realloc},
core::alloc::Layout, core::alloc::Layout,
log::{debug, info}, log::{debug, info},
}; };
@ -49,8 +49,12 @@ pub fn memory_msg_handler(
match msg_type { match msg_type {
0 => unsafe { 0 => unsafe {
let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize; let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize;
let zeroed = msg_vec[9];
let ptr = alloc(Layout::from_size_align_unchecked(page_count * 4096, 1)); let ptr = if zeroed > 0 {
alloc_zeroed(Layout::from_size_align_unchecked(page_count * 4096, 8))
} else {
alloc(Layout::from_size_align_unchecked(page_count * 4096, 8))
};
log::debug!("Allocating {} pages @ {:?}", page_count, ptr); log::debug!("Allocating {} pages @ {:?}", page_count, ptr);
@ -61,11 +65,12 @@ pub fn memory_msg_handler(
let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize; let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize;
let mptr = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap()) as *mut u8; let mptr = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap()) as *mut u8;
debug_assert!(mptr.addr() & 0xFFFF000000000000 != 0);
log::debug!("Deallocating {} pages @ {:?}", page_count, mptr); log::debug!("Deallocating {} pages @ {:?}", page_count, mptr);
dealloc( dealloc(
mptr, mptr,
Layout::from_size_align_unchecked(page_count * 4096, 1), Layout::from_size_align_unchecked(page_count * 4096, 8),
) )
}, },
2 => { 2 => {
@ -108,6 +113,35 @@ pub fn memory_msg_handler(
debug_assert!(dest.addr() & 0xFFFF000000000000 != 0); debug_assert!(dest.addr() & 0xFFFF000000000000 != 0);
memset(dest, src, count, size); memset(dest, src, count, size);
}, },
6 => unsafe {
let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize;
let src = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap_unchecked()) as *const u8;
let dest = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *mut u8;
debug_assert!(src.addr() & 0xFFFF000000000000 != 0);
debug_assert!(dest.addr() & 0xFFFF000000000000 != 0);
src.copy_to(dest, count);
},
7 => unsafe {
let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize;
let page_count_new = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap()) as usize;
let ptr = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap()) as *mut u8;
debug_assert!(ptr.addr() & 0xFFFF000000000000 != 0);
let ptr_new = realloc(
ptr,
Layout::from_size_align_unchecked(page_count * 4096, 8),
page_count_new * 4096,
);
log::debug!(
"Re-allocating {} (now {}) pages from {:?} to {:?}",
page_count,
page_count_new,
ptr,
ptr_new
);
vm.registers[1] = hbvm::value::Value(ptr_new as u64);
},
_ => { _ => {
log::debug!("Unknown memory service message type: {}", msg_type); log::debug!("Unknown memory service message type: {}", msg_type);
} }

View file

@ -1,6 +1,4 @@
$PAGE_SIZE := 4096 $PAGE_SIZE := 4096
$MAX_ALLOC := 0xFF
$MAX_FREE := 0xFF
$uninit := fn($Expr: type): Expr { $uninit := fn($Expr: type): Expr {
return idk return idk
@ -19,18 +17,22 @@ $calc_pages := fn($Expr: type, num: uint): uint {
} }
// ! will be replaced, don't get attached // ! will be replaced, don't get attached
alloc := fn($Expr: type, num: uint): ^Expr { $alloc := fn($Expr: type, num: uint): ^Expr {
return @bitcast(request_page(@intcast(calc_pages(Expr, num)))) return @bitcast(request_page(calc_pages(Expr, num * @sizeof(Expr)), false))
}
$alloc_zeroed := fn($Expr: type, num: uint): ^Expr {
return @bitcast(request_page(calc_pages(Expr, num * @sizeof(Expr)), true))
} }
// ! stub // ! stub
$free := fn($Expr: type, ptr: ^Expr, num: uint): void { $free := fn($Expr: type, ptr: ^Expr, num: uint): void {
release_page(@bitcast(ptr), @intcast(calc_pages(Expr, num))) release_page(@bitcast(ptr), calc_pages(Expr, num * @sizeof(Expr)))
} }
RqPageMsg := packed struct {a: u8, count: uint} RqPageMsg := packed struct {a: u8, count: uint, zeroed: bool}
$request_page := fn(count: uint): ^u8 { $request_page := fn(count: uint, zeroed: bool): ^u8 {
return @eca(3, 2, &RqPageMsg.(0, count), @sizeof(RqPageMsg)) return @eca(3, 2, &RqPageMsg.(0, count, zeroed), @sizeof(RqPageMsg))
} }
RlPageMsg := packed struct {a: u8, count: uint, ptr: ^u8} RlPageMsg := packed struct {a: u8, count: uint, ptr: ^u8}
@ -77,3 +79,7 @@ SetMsg := packed struct {a: u8, count: uint, size: uint, src: ^u8, dest: ^u8}
$set := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void { $set := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void {
return @eca(3, 2, &SetMsg.(5, count, @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(SetMsg)) return @eca(3, 2, &SetMsg.(5, count, @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(SetMsg))
} }
$move := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void {
return @eca(3, 2, &CopyMsg.(7, count * @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(CopyMsg))
}

View file

@ -1,7 +0,0 @@
.{log, sleep} := @use("stn")
main := fn(): void {
log.info("Hello, World!")
// fake interrupt, sleep forever
sleep.sleep_until_interrupt(100)
}

Binary file not shown.

View file

@ -1,7 +1,7 @@
.{process, log} := @use("stn") .{process, log} := @use("stn")
// src: https://git.ablecorp.us/koniifer/lily // src: https://git.ablecorp.us/koniifer/lily
exe := @embed("./assets/lily.hbf") exe := @embed("./assets/lily.axe")
test := fn(): uint { test := fn(): uint {
process.spawn(@bitcast(&exe), @sizeof(@TypeOf(exe))) process.spawn(@bitcast(&exe), @sizeof(@TypeOf(exe)))