From 1eee33ce8b280e53a75fac56b83ba0bdd8c4ad08 Mon Sep 17 00:00:00 2001 From: koniifer Date: Mon, 14 Oct 2024 18:54:53 +0100 Subject: [PATCH] fix icky pointer misalignment move dt_api to stn for ease of use do away with now redundant strobe example (colour example is basically strobe now) --- .../holeybytes/kernel_services/mem_serve.rs | 185 +++++++----------- sysdata/libraries/dt_api/README.md | 1 - sysdata/libraries/dt_api/src/lib.hb | 6 - sysdata/libraries/render/src/software.hb | 9 +- sysdata/libraries/stn/src/dt.hb | 5 + sysdata/libraries/stn/src/lib.hb | 4 +- sysdata/libraries/stn/src/memory.hb | 4 +- sysdata/programs/dt_buffer_test/src/main.hb | 9 +- .../render_example/src/examples/strobe.hb | 15 -- sysdata/programs/svga_driver/src/device.hb | 4 +- sysdata/system_config.toml | 10 +- 11 files changed, 96 insertions(+), 156 deletions(-) delete mode 100644 sysdata/libraries/dt_api/README.md delete mode 100644 sysdata/libraries/dt_api/src/lib.hb create mode 100644 sysdata/libraries/stn/src/dt.hb delete mode 100644 sysdata/programs/render_example/src/examples/strobe.hb diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs index 2c01f0d..f08a81f 100644 --- a/kernel/src/holeybytes/kernel_services/mem_serve.rs +++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs @@ -24,6 +24,74 @@ fn alloc_page(vm: &mut Vm, _mem_addr: u64, _length: usize) -> Result<(), MemoryS Ok(()) } +#[inline(always)] +unsafe fn memcpy(mut dest: *mut u8, mut src: *const u8, mut count: usize) { + if count < 16 { + src.copy_to_nonoverlapping(dest, count); + return; + } + + // let dest_misalignment = dest as usize & 7; + // if dest_misalignment != 0 { + // let align_bytes = 8 - dest_misalignment; + // src.copy_to_nonoverlapping(dest, align_bytes); + // dest = dest.add(align_bytes); + // src = src.add(align_bytes); + // count -= align_bytes; + // } + + while count >= 8 { + if (src as usize) & 7 == 0 && (dest as usize) & 7 == 0 { + *(dest as *mut u64) = *(src as *const u64); + } else { + src.copy_to_nonoverlapping(dest, 8); + } + dest = dest.add(8); + src = src.add(8); + count -= 8; + } + + if count > 0 { + src.copy_to_nonoverlapping(dest, count); + } +} + +#[inline(always)] +unsafe fn memset(mut dest: *mut u8, src: *const u8, count: usize, size: usize) { + let mut remaining = count * size; + + if remaining < 16 { + src.copy_to_nonoverlapping(dest, remaining); + return; + } + + let mut buffer = [0u8; 64]; + let mut buffer_size = size; + src.copy_to_nonoverlapping(buffer.as_mut_ptr(), size); + + while buffer_size * 2 <= 64 { + buffer + .as_mut_ptr() + .copy_to_nonoverlapping(buffer.as_mut_ptr().add(buffer_size), buffer_size); + buffer_size *= 2; + } + + let buffer_ptr = buffer.as_ptr() as *const u64; + while remaining >= 8 { + if (dest as usize) & 7 == 0 { + *(dest as *mut u64) = *buffer_ptr; + } else { + buffer.as_ptr().copy_to_nonoverlapping(dest, 8); + } + dest = dest.add(8); + remaining -= 8; + } + + if remaining > 0 { + buffer.as_ptr().copy_to_nonoverlapping(dest, remaining); + } +} + #[inline(always)] pub fn memory_msg_handler( vm: &mut Vm, @@ -83,130 +151,21 @@ pub fn memory_msg_handler( let page_count = msg_vec[1]; log::debug!(" {} pages", page_count); } - // trash but fast memcpy 4 => 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; - let mut src_ptr = src; - let mut dest_ptr = dest; - let mut remaining = count; - - while (dest_ptr as usize) & 7 != 0 && remaining > 0 { - *dest_ptr = *src_ptr; - src_ptr = src_ptr.add(1); - dest_ptr = dest_ptr.add(1); - remaining -= 1; - } - - let mut src_ptr_64 = src_ptr as *const u64; - let mut dest_ptr_64 = dest_ptr as *mut u64; - while remaining >= 64 { - let (s1, s2, s3, s4, s5, s6, s7, s8) = ( - *src_ptr_64, - *src_ptr_64.add(1), - *src_ptr_64.add(2), - *src_ptr_64.add(3), - *src_ptr_64.add(4), - *src_ptr_64.add(5), - *src_ptr_64.add(6), - *src_ptr_64.add(7), - ); - *dest_ptr_64 = s1; - *dest_ptr_64.add(1) = s2; - *dest_ptr_64.add(2) = s3; - *dest_ptr_64.add(3) = s4; - *dest_ptr_64.add(4) = s5; - *dest_ptr_64.add(5) = s6; - *dest_ptr_64.add(6) = s7; - *dest_ptr_64.add(7) = s8; - src_ptr_64 = src_ptr_64.add(8); - dest_ptr_64 = dest_ptr_64.add(8); - remaining -= 64; - } - - while remaining >= 8 { - *dest_ptr_64 = *src_ptr_64; - src_ptr_64 = src_ptr_64.add(1); - dest_ptr_64 = dest_ptr_64.add(1); - remaining -= 8; - } - - src_ptr = src_ptr_64 as *const u8; - dest_ptr = dest_ptr_64 as *mut u8; - for _ in 0..remaining { - *dest_ptr = *src_ptr; - src_ptr = src_ptr.add(1); - dest_ptr = dest_ptr.add(1); - } + memcpy(dest, src, count); }, - - // trash but fast memset 5 => unsafe { let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize; let size = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap_unchecked()) as usize; - let dest = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *mut u8; let src = - u64::from_le_bytes(msg_vec[25..33].try_into().unwrap_unchecked()) as *const u8; + u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *const u8; + let dest = u64::from_le_bytes(msg_vec[25..33].try_into().unwrap_unchecked()) as *mut u8; - let total_size = count * size; - - if total_size > 32 { - let mut pattern_512 = [0u8; 64]; - for i in 0..64 { - pattern_512[i] = *src.add(i % size); - } - let pattern_512_ptr = pattern_512.as_ptr() as *const u64; - - let mut dest_ptr = dest; - let mut remaining = total_size; - - while (dest_ptr as usize) & 7 != 0 && remaining > 0 { - *dest_ptr = *src; - dest_ptr = dest_ptr.add(1); - remaining -= 1; - } - - let mut dest_ptr_64 = dest_ptr as *mut u64; - while remaining >= 64 { - let (p1, p2, p3, p4, p5, p6, p7, p8) = ( - *pattern_512_ptr, - *pattern_512_ptr.add(1), - *pattern_512_ptr.add(2), - *pattern_512_ptr.add(3), - *pattern_512_ptr.add(4), - *pattern_512_ptr.add(5), - *pattern_512_ptr.add(6), - *pattern_512_ptr.add(7), - ); - *dest_ptr_64 = p1; - *dest_ptr_64.add(1) = p2; - *dest_ptr_64.add(2) = p3; - *dest_ptr_64.add(3) = p4; - *dest_ptr_64.add(4) = p5; - *dest_ptr_64.add(5) = p6; - *dest_ptr_64.add(6) = p7; - *dest_ptr_64.add(7) = p8; - dest_ptr_64 = dest_ptr_64.add(8); - remaining -= 64; - } - - while remaining >= 8 { - *dest_ptr_64 = *pattern_512_ptr; - dest_ptr_64 = dest_ptr_64.add(1); - remaining -= 8; - } - - dest_ptr = dest_ptr_64 as *mut u8; - for i in 0..remaining { - *dest_ptr.add(i) = *src.add(i % size); - } - } else { - for i in 0..total_size { - *dest.add(i) = *src.add(i % size); - } - } + memset(dest, src, count, size); }, _ => { log::debug!("Unknown memory service message type: {}", msg_type); diff --git a/sysdata/libraries/dt_api/README.md b/sysdata/libraries/dt_api/README.md deleted file mode 100644 index 61d4547..0000000 --- a/sysdata/libraries/dt_api/README.md +++ /dev/null @@ -1 +0,0 @@ -# dt_api \ No newline at end of file diff --git a/sysdata/libraries/dt_api/src/lib.hb b/sysdata/libraries/dt_api/src/lib.hb deleted file mode 100644 index 9159c83..0000000 --- a/sysdata/libraries/dt_api/src/lib.hb +++ /dev/null @@ -1,6 +0,0 @@ -.{string} := @use("../../stn/src/lib.hb") - -dt_get := fn($Expr: type, query: ^u8): Expr { - length := string.length(query) - return @eca(3, 5, query, length) -} \ No newline at end of file diff --git a/sysdata/libraries/render/src/software.hb b/sysdata/libraries/render/src/software.hb index 32c1c1d..aa83562 100644 --- a/sysdata/libraries/render/src/software.hb +++ b/sysdata/libraries/render/src/software.hb @@ -1,5 +1,4 @@ -.{math, memory} := @use("../../stn/src/lib.hb"); -.{dt_get} := @use("../../dt_api/src/lib.hb"); +.{math, memory, dt} := @use("../../stn/src/lib.hb"); .{Color, Image} := @use("lib.hb"); .{Vec2} := math @@ -16,12 +15,12 @@ Context := struct { } init := fn(): void { - width := dt_get(int, "framebuffer/fb0/width\0") - height := dt_get(int, "framebuffer/fb0/height\0") + width := dt.get(int, "framebuffer/fb0/width\0") + height := dt.get(int, "framebuffer/fb0/height\0") pixels := width * height back_buffer := memory.alloc(Color, pixels * @bitcast(@sizeof(Color))) ctx = Context.{ - fb: dt_get(^Color, "framebuffer/fb0/ptr\0"), + fb: dt.get(^Color, "framebuffer/fb0/ptr\0"), bb: back_buffer, buf: back_buffer, width, diff --git a/sysdata/libraries/stn/src/dt.hb b/sysdata/libraries/stn/src/dt.hb new file mode 100644 index 0000000..160d537 --- /dev/null +++ b/sysdata/libraries/stn/src/dt.hb @@ -0,0 +1,5 @@ +.{string} := @use("../../stn/src/lib.hb") + +get := fn($Expr: type, query: ^u8): Expr { + return @eca(3, 5, query, @inline(string.length, query)) +} \ No newline at end of file diff --git a/sysdata/libraries/stn/src/lib.hb b/sysdata/libraries/stn/src/lib.hb index 09ff85d..6c67d45 100644 --- a/sysdata/libraries/stn/src/lib.hb +++ b/sysdata/libraries/stn/src/lib.hb @@ -1,9 +1,9 @@ acs := @use("acs.hb") - string := @use("string.hb") log := @use("log.hb") memory := @use("memory.hb") buffer := @use("buffer.hb") math := @use("math.hb") random := @use("random.hb") -file := @use("file_io.hb") \ No newline at end of file +file := @use("file_io.hb") +dt := @use("dt.hb") \ No newline at end of file diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb index 21a0387..9167b32 100644 --- a/sysdata/libraries/stn/src/memory.hb +++ b/sysdata/libraries/stn/src/memory.hb @@ -64,7 +64,7 @@ copy := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void { return @eca(3, 2, &CopyMsg.(4, count * @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(CopyMsg)) } -SetMsg := packed struct {a: u8, count: uint, size: uint, dest: ^u8, src: ^u8} +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(dest), @bitcast(src)), @sizeof(SetMsg)) + return @eca(3, 2, &SetMsg.(5, count, @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(SetMsg)) } \ No newline at end of file diff --git a/sysdata/programs/dt_buffer_test/src/main.hb b/sysdata/programs/dt_buffer_test/src/main.hb index 4d4d14c..ce42ef0 100644 --- a/sysdata/programs/dt_buffer_test/src/main.hb +++ b/sysdata/programs/dt_buffer_test/src/main.hb @@ -1,14 +1,13 @@ -dt_api := @use("../../../libraries/dt_api/src/lib.hb"); -.{dt_get} := dt_api +.{dt} := @use("../../../libraries/stn/src/lib.hb") main := fn(): int { - dt_api.dt_get("framebuffer/fb0/width\0") - dt_api.dt_get("cpu/cpu0/architecture\0") + dt.get(int, "framebuffer/fb0/width\0") + dt.get(int, "cpu/cpu0/architecture\0") // Checking if the first detected serial port is memory mapped or port mapped // 0 -> memory mapped // 1 -> port mapped - dt_get("serial_ports/sp0/mapping\0") + dt.get(int, "serial_ports/sp0/mapping\0") return 0 } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/examples/strobe.hb b/sysdata/programs/render_example/src/examples/strobe.hb deleted file mode 100644 index 4681cc5..0000000 --- a/sysdata/programs/render_example/src/examples/strobe.hb +++ /dev/null @@ -1,15 +0,0 @@ -render := @use("../../../../libraries/render/src/lib.hb") - -/* expected result: (EPILEPSY WARNING) - the screen rapidly flashes red then black */ - -example := fn(): void { - render.init() - loop { - render.clear(render.red) - render.sync() - render.clear(render.yellow) - render.sync() - } - return -} \ No newline at end of file diff --git a/sysdata/programs/svga_driver/src/device.hb b/sysdata/programs/svga_driver/src/device.hb index daf98e4..741d9e4 100644 --- a/sysdata/programs/svga_driver/src/device.hb +++ b/sysdata/programs/svga_driver/src/device.hb @@ -2,9 +2,7 @@ stn := @use("../../../libraries/stn/src/lib.hb"); .{string, memory, buffer, log} := stn pci := @use("../../../libraries/pci/src/lib.hb"); -.{PCIAddress, get_ids, config_read32} := pci; - -.{dt_get} := @use("../../../libraries/dt_api/src/lib.hb") +.{PCIAddress, get_ids, config_read32} := pci reg := @use("./reg.hb") diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index b05a036..1792d68 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -3,11 +3,12 @@ default_entry = 1 timeout = 0 verbose = false +# interface_resolution = "1920x1080x24" interface_resolution = "1024x768x24" -# interface_resolution = "640x480x32" +# interface_resolution = "640x480x24" # Terminal related settings -term_wallpaper = "boot:///background.bmp" -# term_wallpaper = "boot:///empty-background.bmp" +# term_wallpaper = "boot:///background.bmp" +term_wallpaper = "boot:///empty-background.bmp" term_background = "008080" [boot.limine.ableos] @@ -15,8 +16,9 @@ comment = "Default AbleOS boot entry." protocol = "limine" kernel_path = "boot:///kernel_${ARCH}" kernel_cmdline = "" -# resolution = "640x480x32" +# resolution = "1920x1080x24" resolution = "1024x768x24" +# resolution = "640x480x24" [boot.limine.ableos.modules]