From 6ff65eee417ad428b96606ad6569383edcf8423c Mon Sep 17 00:00:00 2001 From: koniifer Date: Wed, 23 Oct 2024 21:22:28 +0100 Subject: [PATCH] kaboom --- kernel/src/arch/x86_64/mod.rs | 1 + .../holeybytes/kernel_services/mem_serve.rs | 6 +- sysdata/libraries/horizon_api/src/lib.hb | 3 +- sysdata/libraries/render/src/software.hb | 10 ++- sysdata/libraries/stn/src/buffer.hb | 8 +-- sysdata/libraries/stn/src/memory.hb | 66 +++++++++++-------- sysdata/programs/horizon/src/main.hb | 3 +- sysdata/programs/ps2_driver/src/main.hb | 5 +- sysdata/programs/serial_driver/src/main.hb | 2 +- .../programs/serial_driver_test/src/main.hb | 2 +- sysdata/system_config.toml | 9 +-- 11 files changed, 64 insertions(+), 51 deletions(-) diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index c45a7e2f7..d0270ff9b 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -251,6 +251,7 @@ unsafe extern "C" fn start() -> ! { /// Spin loop pub fn spin_loop() -> ! { loop { + core::hint::spin_loop(); x86_64::instructions::hlt() } } diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs index 29c22820e..743440e9f 100644 --- a/kernel/src/holeybytes/kernel_services/mem_serve.rs +++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs @@ -75,16 +75,14 @@ pub fn memory_msg_handler( match msg_type { 0 => unsafe { let page_count = msg_vec[1]; - let mptr_raw: [u8; 8] = msg_vec[2..10].try_into().unwrap(); - let mptr: u64 = u64::from_le_bytes(mptr_raw); - - log::debug!("Allocating {} pages @ {:x}", page_count, mptr); let ptr = alloc(Layout::from_size_align_unchecked( page_count as usize * 4096, 8, )); + log::debug!("Allocating {} pages @ {:x}", page_count, ptr as u64); + vm.registers[1] = hbvm::value::Value(ptr as u64); log::debug!("Kernel ptr: {:x}", ptr as u64); }, diff --git a/sysdata/libraries/horizon_api/src/lib.hb b/sysdata/libraries/horizon_api/src/lib.hb index be58f081b..e8d24601f 100644 --- a/sysdata/libraries/horizon_api/src/lib.hb +++ b/sysdata/libraries/horizon_api/src/lib.hb @@ -25,6 +25,7 @@ create_window := fn(channel: int): ^render.Surface { if windowing_system_buffer == 0 { return 0 } else { + // ! bad able, stop using string messages :ragey: // msg := "\{01}\0" // msg_length := 2 @@ -33,7 +34,7 @@ create_window := fn(channel: int): ^render.Surface { x := 0 loop if x > 1000 break else x += 1 - ret := buffer.recv(windowing_system_buffer, mem_buf, 4096) + ret := buffer.recv([u8; 4096], windowing_system_buffer, mem_buf) if ret == 0 { log.info("No messages\0") } diff --git a/sysdata/libraries/render/src/software.hb b/sysdata/libraries/render/src/software.hb index 038bbea7b..7f577d376 100644 --- a/sysdata/libraries/render/src/software.hb +++ b/sysdata/libraries/render/src/software.hb @@ -30,9 +30,9 @@ clone_surface := fn(surface: ^Surface): Surface { return new } -free_surface := fn(surface: ^Surface): void { - // todo: depends on stn.memory.free - return +// ! is broken, check memory.free function +free_surface := fn(surface: Surface): void { + return @inline(memory.free, Color, surface.buf, @intcast(surface.width * surface.height), false) } framebuffer := @as(^Color, idk) @@ -53,6 +53,10 @@ clear := fn(surface: Surface, color: Color): void { } sync := fn(surface: Surface): void { + // vague safety + if surface.buf == framebuffer { + return + } return @inline(memory.copy, Color, surface.buf, framebuffer, @bitcast(surface.width * surface.height)) } diff --git a/sysdata/libraries/stn/src/buffer.hb b/sysdata/libraries/stn/src/buffer.hb index 23d7e67e6..23041dc11 100644 --- a/sysdata/libraries/stn/src/buffer.hb +++ b/sysdata/libraries/stn/src/buffer.hb @@ -1,11 +1,11 @@ string := @use("string.hb") -recv := fn(buffer_id: int, memory_map_location: ^u8, length: int): ^u8 { - return @eca(4, buffer_id, memory_map_location, length) +recv := fn($Expr: type, buffer_id: int, memory_map_location: ^u8): ^Expr { + return @eca(4, buffer_id, memory_map_location, @sizeof(Expr)) } -write := fn(msg: ^u8, buffer_id: int, length: int): void { - return @eca(3, buffer_id, msg, length) +write := fn($Expr: type, msg: ^Expr, buffer_id: int): void { + return @eca(3, buffer_id, msg, @sizeof(Expr)) } BufferMsg := packed struct {operation: u8, msg: ^u8, msg_len: uint} diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb index b51c5ad34..6022d5ae4 100644 --- a/sysdata/libraries/stn/src/memory.hb +++ b/sysdata/libraries/stn/src/memory.hb @@ -1,65 +1,79 @@ PAGE_SIZE := 4096 MAX_ALLOC := 0xFF +MAX_FREE := 0xFF + +calc_pages := fn($Expr: type, num: int): int { + return 1 + @bitcast(@sizeof(Expr)) * num / PAGE_SIZE +} alloc := fn($Expr: type, num: int): ^Expr { - pages := 1 + @bitcast(@sizeof(Expr)) * num / PAGE_SIZE + pages := @inline(calc_pages, Expr, num) if pages <= MAX_ALLOC { - return @bitcast(@inline(request_page, pages)) + return @bitcast(request_page(@intcast(pages))) } - ptr := @inline(request_page, 0xFF) + ptr := request_page(0xFF) remaining := pages - MAX_ALLOC loop if remaining <= 0 break else { if remaining < MAX_ALLOC { - request_page(remaining) + request_page(@intcast(remaining)) } else { - request_page(MAX_ALLOC) + request_page(@intcast(MAX_ALLOC)) } remaining -= MAX_ALLOC } return @bitcast(ptr) } -free := fn($Expr: type, ptr: ^Expr, num: int): void { - // todo +// ! is broked, somebody fix please :( +free := fn($Expr: type, ptr: ^Expr, num: int, nullify: bool): void { + if nullify { + null := @as(u8, 0) + set(u8, &null, @bitcast(ptr), @bitcast(num) * @bitcast(PAGE_SIZE)) + } + pages := @inline(calc_pages, Expr, num) + if pages <= MAX_FREE { + return release_page(@bitcast(ptr), @intcast(pages)) + } + page_ptr := @as(^[u8; PAGE_SIZE], @bitcast(ptr)) + 1 + remaining := pages - MAX_FREE + loop if remaining <= 0 break else { + if remaining < MAX_FREE { + release_page(@bitcast(page_ptr), @intcast(remaining)) + } else { + release_page(@bitcast(page_ptr), @intcast(MAX_FREE)) + } + remaining -= MAX_FREE + page_ptr += 1 + } return } -request_page := fn(page_count: u8): ^u8 { - msg := "\{00}\{01}xxxxxxxx\0" - msg_page_count := msg + 1; - *msg_page_count = page_count - return @eca(3, 2, msg, 12) +RqPageMsg := packed struct {a: u8, count: u8} +request_page := fn(count: u8): ^u8 { + return @eca(3, 2, &RqPageMsg.(0, count), @sizeof(RqPageMsg)) } -release_page := fn(ptr: ^u8, page_count: u8): void { - msg := "\{01}\{00}xxxxxxxx\0" - - msg_page_count := msg + 1; - *msg_page_count = page_count - - msg_ptr := @as(^^u8, @bitcast(msg + 2)); - *msg_ptr = ptr - - return @eca(3, 2, msg, 12) +RlPageMsg := packed struct {a: u8, count: u8, ptr: ^u8} +release_page := fn(ptr: ^u8, count: u8): void { + return @eca(3, 2, &RlPageMsg.(1, count, ptr), @sizeof(RlPageMsg)) } OutbMsg := packed struct {a: u8, b: u8, addr: u16, value: u8} -InbMsg := packed struct {a: u8, b: u8, addr: u16} -OutlMsg := packed struct {a: u8, b: u8, addr: u16, value: u32} -InlMsg := packed struct {a: u8, b: u8, addr: u16} - outb := fn(addr: u16, value: u8): void { return @eca(3, 3, &OutbMsg.(1, 0, addr, value), @sizeof(OutbMsg)) } +InbMsg := packed struct {a: u8, b: u8, addr: u16} inb := fn(addr: u16): u8 { return @eca(3, 3, &InbMsg.(0, 0, addr), @sizeof(InbMsg)) } +OutlMsg := packed struct {a: u8, b: u8, addr: u16, value: u32} outl := fn(addr: u16, value: u32): void { return @eca(3, 3, &OutlMsg.(1, 2, addr, value), @sizeof(OutlMsg)) } +InlMsg := packed struct {a: u8, b: u8, addr: u16} inl := fn(addr: u16): u32 { return @eca(3, 3, &InlMsg.(0, 2, addr), @sizeof(InlMsg)) } diff --git a/sysdata/programs/horizon/src/main.hb b/sysdata/programs/horizon/src/main.hb index 95e5cfbab..171606b87 100644 --- a/sysdata/programs/horizon/src/main.hb +++ b/sysdata/programs/horizon/src/main.hb @@ -18,7 +18,6 @@ Window := struct { main := fn(): int { win_buff := buffer.create("XHorizon\0") - // BUG: Backbuffering is disabled screen := render.init(true) // Clear the screen to black. @@ -41,7 +40,7 @@ main := fn(): int { // TODO: Read the window buffer here { - ret := buffer.recv(win_buff, mem_buf, 4096) + ret := buffer.recv([u8; 4096], win_buff, mem_buf) if ret == 0 { log.info("No messages\0") } diff --git a/sysdata/programs/ps2_driver/src/main.hb b/sysdata/programs/ps2_driver/src/main.hb index 26be5025d..bdd72f561 100644 --- a/sysdata/programs/ps2_driver/src/main.hb +++ b/sysdata/programs/ps2_driver/src/main.hb @@ -1,4 +1,4 @@ -.{memory, log, string, buffer} := @use("../../../libraries/stn/src/lib.hb") +.{memory, log, buffer} := @use("../../../libraries/stn/src/lib.hb") send_byte := fn(byte: u8): u8 { memory.outb(96, byte) @@ -15,7 +15,6 @@ main := fn(): int { if send_byte(244) == 250 { log.info("Enabled scanning\0") } - ptr := memory.request_page(1) prev_input := 250 loop { input := memory.inb(96) @@ -23,7 +22,7 @@ main := fn(): int { continue } prev_input = input - buffer.write(&input, buf, 1) + buffer.write(u8, &input, buf) } return 0 } \ No newline at end of file diff --git a/sysdata/programs/serial_driver/src/main.hb b/sysdata/programs/serial_driver/src/main.hb index 9270463f5..0173b67f9 100644 --- a/sysdata/programs/serial_driver/src/main.hb +++ b/sysdata/programs/serial_driver/src/main.hb @@ -25,7 +25,7 @@ main := fn(): int { mem := memory.request_page(1) loop { - ptr := @eca(4, a, mem, 0x1000) + ptr := @as(^u8, @eca(4, a, mem, 0x1000)) if ptr == 0 { serial_println("No message\0") } diff --git a/sysdata/programs/serial_driver_test/src/main.hb b/sysdata/programs/serial_driver_test/src/main.hb index d0998bcf7..d3011c332 100644 --- a/sysdata/programs/serial_driver_test/src/main.hb +++ b/sysdata/programs/serial_driver_test/src/main.hb @@ -6,7 +6,7 @@ log_info := fn(): void { } else { msg := "XABC\0" msg_length := @inline(string.length, msg) - @eca(3, a, msg, msg_length) + @as(void, @eca(3, a, msg, msg_length)) } return diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index e5deef95e..f5562824b 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -25,15 +25,15 @@ resolution = "1024x768x24" # [boot.limine.ableos.modules.tests] # path = "boot:///tests.hbf" -# [boot.limine.ableos.modules.serial_driver] -# path = "boot:///serial_driver.hbf" - # [boot.limine.ableos.modules.diskio_driver] # path = "boot:///diskio_driver.hbf" [boot.limine.ableos.modules.render_example] path = "boot:///render_example.hbf" +# [boot.limine.ableos.modules.serial_driver] +# path = "boot:///serial_driver.hbf" + # [boot.limine.ableos.modules.serial_driver_test] # path = "boot:///serial_driver_test.hbf" @@ -57,6 +57,3 @@ path = "boot:///render_example.hbf" # [boot.limine.ableos.modules.pumpkin_print] # path = "boot:///pumpkin_print.hbf" - -# [boot.limine.ableos.modules.tetris] -# path = "boot:///tetris.hbf"