From 62f9fa1fd41e0df112d39dc02ab27e8774a81c6a Mon Sep 17 00:00:00 2001
From: koniifer <koniifer@proton.me>
Date: Wed, 8 Jan 2025 01:25:11 +0000
Subject: [PATCH] ups

---
 .../holeybytes/kernel_services/mem_serve.rs   |  42 ++++++++++++++++--
 sysdata/libraries/stn/src/memory.hb           |  22 +++++----
 .../tests/stn/assets/hello_world_and_spin.hb  |   7 ---
 .../tests/stn/assets/hello_world_and_spin.hbf | Bin 273 -> 0 bytes
 .../test/src/tests/stn/assets/lily.axe        | Bin 0 -> 6043 bytes
 .../test/src/tests/stn/assets/lily.hbf        | Bin 4588 -> 0 bytes
 .../programs/test/src/tests/stn/process.hb    |   2 +-
 7 files changed, 53 insertions(+), 20 deletions(-)
 delete mode 100644 sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hb
 delete mode 100644 sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hbf
 create mode 100644 sysdata/programs/test/src/tests/stn/assets/lily.axe
 delete mode 100644 sysdata/programs/test/src/tests/stn/assets/lily.hbf

diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs
index 6e39e3a..ccdedf6 100644
--- a/kernel/src/holeybytes/kernel_services/mem_serve.rs
+++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs
@@ -1,6 +1,6 @@
 use {
     crate::holeybytes::{kernel_services::block_read, Vm},
-    alloc::alloc::{alloc, dealloc},
+    alloc::alloc::{alloc, alloc_zeroed, dealloc, realloc},
     core::alloc::Layout,
     log::{debug, info},
 };
@@ -49,8 +49,12 @@ pub fn memory_msg_handler(
     match msg_type {
         0 => unsafe {
             let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize;
-
-            let ptr = alloc(Layout::from_size_align_unchecked(page_count * 4096, 1));
+            let zeroed = msg_vec[9];
+            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);
 
@@ -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 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);
 
             dealloc(
                 mptr,
-                Layout::from_size_align_unchecked(page_count * 4096, 1),
+                Layout::from_size_align_unchecked(page_count * 4096, 8),
             )
         },
         2 => {
@@ -108,6 +113,35 @@ pub fn memory_msg_handler(
             debug_assert!(dest.addr() & 0xFFFF000000000000 != 0);
             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);
         }
diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb
index c6b818e..abf8554 100644
--- a/sysdata/libraries/stn/src/memory.hb
+++ b/sysdata/libraries/stn/src/memory.hb
@@ -1,6 +1,4 @@
 $PAGE_SIZE := 4096
-$MAX_ALLOC := 0xFF
-$MAX_FREE := 0xFF
 
 $uninit := fn($Expr: type): Expr {
 	return idk
@@ -19,18 +17,22 @@ $calc_pages := fn($Expr: type, num: uint): uint {
 }
 
 // ! will be replaced, don't get attached
-alloc := fn($Expr: type, num: uint): ^Expr {
-	return @bitcast(request_page(@intcast(calc_pages(Expr, num))))
+$alloc := fn($Expr: type, num: uint): ^Expr {
+	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
 $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}
-$request_page := fn(count: uint): ^u8 {
-	return @eca(3, 2, &RqPageMsg.(0, count), @sizeof(RqPageMsg))
+RqPageMsg := packed struct {a: u8, count: uint, zeroed: bool}
+$request_page := fn(count: uint, zeroed: bool): ^u8 {
+	return @eca(3, 2, &RqPageMsg.(0, count, zeroed), @sizeof(RqPageMsg))
 }
 
 RlPageMsg := packed struct {a: u8, count: uint, ptr: ^u8}
@@ -76,4 +78,8 @@ $copy := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void {
 SetMsg := packed struct {a: u8, count: uint, size: uint, src: ^u8, dest: ^u8}
 $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))
+}
+
+$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))
 }
\ No newline at end of file
diff --git a/sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hb b/sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hb
deleted file mode 100644
index e8d4c92..0000000
--- a/sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hb
+++ /dev/null
@@ -1,7 +0,0 @@
-.{log, sleep} := @use("stn")
-
-main := fn(): void {
-	log.info("Hello, World!")
-	// fake interrupt, sleep forever
-	sleep.sleep_until_interrupt(100)
-}
\ No newline at end of file
diff --git a/sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hbf b/sysdata/programs/test/src/tests/stn/assets/hello_world_and_spin.hbf
deleted file mode 100644
index 48b140214b18d8358f18f9d8cfe6dfed8dd9f8e6..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 273
zcmWfgc!>cB&VmRa;Dt~)$q;!44xl8X!M}g^|3iQeFT)|AkOv==0slXkMnB$vf>1UG
zgSP<8SU*0v3PuJ$fq#;4H9!rFaE>o8sthMw#+w&rtT!J_y|(}}RNjqAz>S&DjfLNh
sl{dzNH^rNe4KBmy#>^W7bhaW?B$PoO%3|<H&B@8vQ3%g3%1KcK00VC%p#T5?

diff --git a/sysdata/programs/test/src/tests/stn/assets/lily.axe b/sysdata/programs/test/src/tests/stn/assets/lily.axe
new file mode 100644
index 0000000000000000000000000000000000000000..ff23ae1280245253d00e408a429ff4f5f2eb97b7
GIT binary patch
literal 6043
zcmdT|O^h5z6|SC{T@RM#$3Z~>p}2m!XJ=RZbi`V+;~adUG)ix`G7RzBVuidoiySG6
zBV&j}4puHmQ7(>z5>5#xlvCiAgp*GwN65*CNaP3t@&i#~5bEKpntD~$J7dcjaY!F#
zyQ*H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>+e{0oIbC{kp~K1NZU
zFh!+;N)$}s124{tXOn?|M|lbSDTG&Z8Pr_rw$L4=*ZAIQfIj}v0u;6kfTk+}x)A{4
z&VW0BLS>(~0DTSPz|C19X52(+|AtE@D&+oGFo>=M(3@a{M(_%-^Uf034?6!-0p|@3
z1Tn(o)%*VE2X@~SBRA*58RzhSCB}2W|8q}_n}7R!5aR@Be+X>4Q{NLY-v8^}663W{
z5Tt}wG4MeS!oHa-#brKw&C}p2jv^{P|KF7UCq~)j!rFZ9m)sk5*;&qBGCe`mZ>unz
zuT~h&cnTdk=2oz}8pQs;5S2R|*{}S?0vVt|h3&fqqYArnu>aIkmoM&LJ$Qm&*?)F*
z<B5YOpMLV{!`zKtzCs<C5juZ(<zV52Mt^c}k&mDKrturBHV=<_r<>NnGe7>(CAhhI
InPd8Y004l7g8%>k

literal 0
HcmV?d00001

diff --git a/sysdata/programs/test/src/tests/stn/assets/lily.hbf b/sysdata/programs/test/src/tests/stn/assets/lily.hbf
deleted file mode 100644
index f85d5e6914c0d2dc353f7e3a44e47e4e6592aebc..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 4588
zcmcgvJ&Y7p9RJVEvRR35-s~MjwD8`1>~TOd;+mikbA@@?WSt3@#XBHIO^}4dLXBWy
zVT@YPsj;D?QK<~khDvEnEQuAJlysUHmBcvf@6Eoi;p4<enBwmJ-}`@m@9xijB79#B
z)nB4`@$a60kFC%Ek3rcq7n^ze)hmq%76j{5M@^!Egh~gLqPeM=)?|+Zk}1_`^Q(Ru
zP?<IQOm)`WviCq5XubnQK^7?U=D0@N5cwLtD2TzBS~L?&Jr%|-TpzX9lOmjsc4!b9
zexQ%g&>UM~=k-0*TWx+cNUP1K_8uBt^nnXLaLETQ`@j_+FpOJ~SN)f3K5*Rv8zP%1
zU)-=aQlwjAl#&c{1nLwgoHCr&9>pn0XU_r?Z!!|N+zA)Tu&5&IsDyC+9B@iuB1*H%
z2y*3z7@>bnd;cX`(Xt7*Key>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s(^pGB1Z-vEF6Z}ggfAK+iYum>@~
z)%4!TG3o#Npmu&o@=Ua&yUBCYMacwWj~difDqK^jgtspsIb@`A>Dyav&vj%FVraTT
zuHPwSO>YBtWaxSDTV&{Qs&ZktBSSk&i*m@~YhR390GUsddT3ty8m?<<6<*U&SLZ)n
z)7fHbT?}Y;4w$9uY*UW8)P5ynb{FtVIR_>8T88QpW+R?*(>re4g1PL)dJC2TgMx;l
ztDv_C8&{OdshFnPAgVGih+7%$!+NPE`3#aVJ&oau%lA<jvsXd7SgPK`sXKsk%NH#@
zr>77euV+++_uQ__>SxU-f6V8t-)A4fpI+lAmFL&BqFux6gyvC|a2MIFgti?%Ux1r8
zuc{@x*{K=F2U-|@7{*Ko7e9aE@v>b3`pZkp8_Q~Z?edJ-+@(f!>@#3)R5MS*Dp=M+
zkQioE8&R_hIKgP4QSNTE_p?UmIyBLR6@G5x*W#iM5Bv_#cFHyDl&ePT0j(w@UA27j
z&;U3&0G!(qG-H%dK{FigTYp1<tVvbYq~-!Tz#=EsTJ*+WI%~Go*RTPXhp*zwZx39>
Tx6ZwD=Jf00?3?1<)2HYkfAgyQ

diff --git a/sysdata/programs/test/src/tests/stn/process.hb b/sysdata/programs/test/src/tests/stn/process.hb
index cf3d22c..96b879b 100644
--- a/sysdata/programs/test/src/tests/stn/process.hb
+++ b/sysdata/programs/test/src/tests/stn/process.hb
@@ -1,7 +1,7 @@
 .{process, log} := @use("stn")
 
 // src: https://git.ablecorp.us/koniifer/lily
-exe := @embed("./assets/lily.hbf")
+exe := @embed("./assets/lily.axe")
 
 test := fn(): uint {
 	process.spawn(@bitcast(&exe), @sizeof(@TypeOf(exe)))