From 5616e0ba03f7ff5c8090e9876f3750b935d90094 Mon Sep 17 00:00:00 2001
From: koniifer <koniifer@proton.me>
Date: Wed, 1 Jan 2025 21:19:08 +0000
Subject: [PATCH] fiddle with memory free & alloc for safety & simplicity
 reasons

---
 Cargo.lock                                    |  34 +++++++++---------
 .../holeybytes/kernel_services/mem_serve.rs   |  21 +++++------
 kernel/src/holeybytes/mem.rs                  |   8 ++---
 sysdata/libraries/stn/src/memory.hb           |  27 +++++---------
 sysdata/programs/diskio_driver/src/main.hb    |   2 +-
 sysdata/programs/test/src/main.hb             |   2 +-
 .../test/src/tests/stn/assets/lily.hbf        | Bin 0 -> 4588 bytes
 .../programs/test/src/tests/stn/process.hb    |  10 ++----
 sysdata/system_config.toml                    |   8 ++---
 9 files changed, 46 insertions(+), 66 deletions(-)
 create mode 100644 sysdata/programs/test/src/tests/stn/assets/lily.hbf

diff --git a/Cargo.lock b/Cargo.lock
index 3924f4a..7f3bc80 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -73,9 +73,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
 
 [[package]]
 name = "cc"
-version = "1.2.5"
+version = "1.2.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
+checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333"
 dependencies = [
  "shlex",
 ]
@@ -213,12 +213,12 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#08fc9d6ab6a8dd539255bf45d892f4b7f08776c5"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#d1bc70892b442376138e854450b0f423302962ef"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#08fc9d6ab6a8dd539255bf45d892f4b7f08776c5"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#d1bc70892b442376138e854450b0f423302962ef"
 dependencies = [
  "hashbrown",
  "hbbytecode",
@@ -229,7 +229,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#08fc9d6ab6a8dd539255bf45d892f4b7f08776c5"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#d1bc70892b442376138e854450b0f423302962ef"
 dependencies = [
  "hbbytecode",
 ]
@@ -521,9 +521,9 @@ dependencies = [
 
 [[package]]
 name = "quote"
-version = "1.0.37"
+version = "1.0.38"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
+checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
 dependencies = [
  "proc-macro2",
 ]
@@ -625,9 +625,9 @@ dependencies = [
 
 [[package]]
 name = "rustversion"
-version = "1.0.18"
+version = "1.0.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
+checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
 
 [[package]]
 name = "sbi"
@@ -649,18 +649,18 @@ checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba"
 
 [[package]]
 name = "serde"
-version = "1.0.216"
+version = "1.0.217"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
+checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
 dependencies = [
  "serde_derive",
 ]
 
 [[package]]
 name = "serde_derive"
-version = "1.0.216"
+version = "1.0.217"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
+checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -726,9 +726,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
 
 [[package]]
 name = "syn"
-version = "2.0.91"
+version = "2.0.93"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
+checksum = "9c786062daee0d6db1132800e623df74274a0a87322d8e183338e01b3d98d058"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -967,9 +967,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
 
 [[package]]
 name = "winnow"
-version = "0.6.20"
+version = "0.6.21"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
+checksum = "e6f5bb5257f2407a5425c6e749bfd9692192a73e70a6060516ac04f889087d68"
 dependencies = [
  "memchr",
 ]
diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs
index 47fa95b..6e39e3a 100644
--- a/kernel/src/holeybytes/kernel_services/mem_serve.rs
+++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs
@@ -48,29 +48,24 @@ pub fn memory_msg_handler(
     let msg_type = msg_vec[0];
     match msg_type {
         0 => unsafe {
-            let page_count = msg_vec[1];
+            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 as usize * 4096,
-                8,
-            ));
+            let ptr = alloc(Layout::from_size_align_unchecked(page_count * 4096, 1));
 
-            log::debug!("Allocating {} pages @ {:x}", page_count, ptr as u64);
+            log::debug!("Allocating {} pages @ {:?}", page_count, ptr);
 
             vm.registers[1] = hbvm::value::Value(ptr as u64);
-            log::debug!("Kernel ptr: {:x}", ptr as u64);
         },
 
         1 => unsafe {
-            let page_count = msg_vec[1];
+            let page_count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap()) as usize;
 
-            let mptr_raw: [u8; 8] = msg_vec[2..10].try_into().unwrap();
-            let mptr: u64 = u64::from_le_bytes(mptr_raw);
-            log::debug!("Deallocating {} pages @ {:x}", page_count, mptr);
+            let mptr = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap()) as *mut u8;
+            log::debug!("Deallocating {} pages @ {:?}", page_count, mptr);
 
             dealloc(
-                mptr as *mut u8,
-                Layout::from_size_align_unchecked(page_count as usize * 4096, 8),
+                mptr,
+                Layout::from_size_align_unchecked(page_count * 4096, 1),
             )
         },
         2 => {
diff --git a/kernel/src/holeybytes/mem.rs b/kernel/src/holeybytes/mem.rs
index 2bd591a..887ef02 100644
--- a/kernel/src/holeybytes/mem.rs
+++ b/kernel/src/holeybytes/mem.rs
@@ -60,9 +60,9 @@ impl hbvm::mem::Memory for Memory {
     }
 
     #[inline(always)]
-    fn log_instr(&mut self, at: Address, regs: &[hbvm::value::Value]) {
-        log::debug!("exec: [{:02x}] {}", at.get(), unsafe {
-            self.logger.display_instr(at, regs)
-        });
+    fn log_instr(&mut self, _at: Address, _regs: &[hbvm::value::Value]) {
+        // log::debug!("exec: [{:02x}] {}", at.get(), unsafe {
+        //     self.logger.display_instr(at, regs)
+        // });
     }
 }
diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb
index b254b6a..c6b818e 100644
--- a/sysdata/libraries/stn/src/memory.hb
+++ b/sysdata/libraries/stn/src/memory.hb
@@ -15,37 +15,26 @@ $dangling := fn($Expr: type): ^Expr {
 }
 
 $calc_pages := fn($Expr: type, num: uint): uint {
-	return 1 + @sizeof(Expr) * num / PAGE_SIZE
+	return (num + PAGE_SIZE - 1) / PAGE_SIZE
 }
 
 // ! will be replaced, don't get attached
 alloc := fn($Expr: type, num: uint): ^Expr {
-	pages := calc_pages(Expr, num)
-	if pages <= MAX_ALLOC {
-		return @bitcast(request_page(@intcast(pages)))
-	}
-	ptr := request_page(MAX_ALLOC)
-	remaining := pages - MAX_ALLOC
-	loop if remaining < MAX_ALLOC break else {
-		_ = request_page(MAX_ALLOC)
-		remaining -= MAX_ALLOC
-	}
-	_ = request_page(@intcast(remaining))
-	return @bitcast(ptr)
+	return @bitcast(request_page(@intcast(calc_pages(Expr, num))))
 }
 
 // ! stub
-$free := fn($Expr: type, ptr: ^Expr, num: uint, nullify: bool): void {
-	return
+$free := fn($Expr: type, ptr: ^Expr, num: uint): void {
+	release_page(@bitcast(ptr), @intcast(calc_pages(Expr, num)))
 }
 
-RqPageMsg := packed struct {a: u8, count: u8}
-$request_page := fn(count: u8): ^u8 {
+RqPageMsg := packed struct {a: u8, count: uint}
+$request_page := fn(count: uint): ^u8 {
 	return @eca(3, 2, &RqPageMsg.(0, count), @sizeof(RqPageMsg))
 }
 
-RlPageMsg := packed struct {a: u8, count: u8, ptr: ^u8}
-$release_page := fn(ptr: ^u8, count: u8): void {
+RlPageMsg := packed struct {a: u8, count: uint, ptr: ^u8}
+$release_page := fn(ptr: ^u8, count: uint): void {
 	return @eca(3, 2, &RlPageMsg.(1, count, ptr), @sizeof(RlPageMsg))
 }
 
diff --git a/sysdata/programs/diskio_driver/src/main.hb b/sysdata/programs/diskio_driver/src/main.hb
index 99ca1a2..2e7214a 100644
--- a/sysdata/programs/diskio_driver/src/main.hb
+++ b/sysdata/programs/diskio_driver/src/main.hb
@@ -48,7 +48,7 @@ identify := fn(drive: Drive): u8 {
 	status := inb(ATA_PRIMARY_COMM_REGSTAT)
 
 	loop if (status & STAT_BSY) == 0 break else {
-		// if DEBUG_PRINT log.printf("(ata: waiting for status) status={}", .(status), .{radix: 16, log: .Warn})
+		if DEBUG_PRINT log.printf("(ata: waiting for status) status={}", .(status), .{radix: 16, log: .Warn})
 		status = inb(ATA_PRIMARY_COMM_REGSTAT)
 	}
 
diff --git a/sysdata/programs/test/src/main.hb b/sysdata/programs/test/src/main.hb
index 4995e0c..3b3bc21 100644
--- a/sysdata/programs/test/src/main.hb
+++ b/sysdata/programs/test/src/main.hb
@@ -3,5 +3,5 @@ serial_driver := @use("./tests/serial_driver.hb")
 
 main := fn(): uint {
 	// return serial_driver.test()
-	return stn.hashers.test()
+	return stn.process.test()
 }
\ No newline at end of file
diff --git a/sysdata/programs/test/src/tests/stn/assets/lily.hbf b/sysdata/programs/test/src/tests/stn/assets/lily.hbf
new file mode 100644
index 0000000000000000000000000000000000000000..f85d5e6914c0d2dc353f7e3a44e47e4e6592aebc
GIT binary patch
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>HpGy`Z4Rk_Ln+67uaa3{_VP=m3Rm{W)8CJ7KivWO^
zfi)2?k62);BHez|*x*l-NZ-jYkx?vrT<%~mOQv!*)lXf;uWH4S$@-nvNoJwUqiJgR
zRw8p+R7EB<>VUQi@jd<!Blv%o;C?KMP(Rng!vvXnRv%J7EEa+IypKSL0FO?<iGp_0
z>=+QoMPX>FQP$FGav`IU>=lUFK_oHA-6Jp!krFhLkwW_p2++r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
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

literal 0
HcmV?d00001

diff --git a/sysdata/programs/test/src/tests/stn/process.hb b/sysdata/programs/test/src/tests/stn/process.hb
index b08a1c4..cf3d22c 100644
--- a/sysdata/programs/test/src/tests/stn/process.hb
+++ b/sysdata/programs/test/src/tests/stn/process.hb
@@ -1,13 +1,9 @@
 .{process, log} := @use("stn")
 
-exe := @embed("./assets/hello_world_and_spin.hbf")
+// src: https://git.ablecorp.us/koniifer/lily
+exe := @embed("./assets/lily.hbf")
 
 test := fn(): uint {
-	loop {
-		log.print(process.spawn(@bitcast(&exe), 356), .{})
-		// spin so we don't spawn 10 quattuordecillion processes
-		i := 0
-		loop if i == 20000000 break else i += 1
-	}
+	process.spawn(@bitcast(&exe), @sizeof(@TypeOf(exe)))
 	return 0
 }
\ No newline at end of file
diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml
index 6e62b14..b5d8d44 100644
--- a/sysdata/system_config.toml
+++ b/sysdata/system_config.toml
@@ -44,14 +44,14 @@ resolution = "1024x768x24"
 # [boot.limine.ableos.modules.ablefetch]
 # path = "boot:///ablefetch.hbf"
 
-[boot.limine.ableos.modules.diskio_driver]
-path = "boot:///diskio_driver.hbf"
+# [boot.limine.ableos.modules.diskio_driver]
+# path = "boot:///diskio_driver.hbf"
 
 # [boot.limine.ableos.modules.angels_halo]
 # path = "boot:///angels_halo.hbf"
 
-# [boot.limine.ableos.modules.test]
-# path = "boot:///test.hbf"
+[boot.limine.ableos.modules.test]
+path = "boot:///test.hbf"
 
 # [boot.limine.ableos.modules.vfsaur]
 # path = "boot:///vfsaur.hbf"