1
0
Fork 0
forked from AbleOS/ableos

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)
This commit is contained in:
koniifer 2024-10-14 18:54:53 +01:00
parent 6fa1c829fd
commit 1eee33ce8b
11 changed files with 96 additions and 156 deletions

View file

@ -24,6 +24,74 @@ fn alloc_page(vm: &mut Vm, _mem_addr: u64, _length: usize) -> Result<(), MemoryS
Ok(()) 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)] #[inline(always)]
pub fn memory_msg_handler( pub fn memory_msg_handler(
vm: &mut Vm, vm: &mut Vm,
@ -83,130 +151,21 @@ pub fn memory_msg_handler(
let page_count = msg_vec[1]; let page_count = msg_vec[1];
log::debug!(" {} pages", page_count); log::debug!(" {} pages", page_count);
} }
// trash but fast memcpy
4 => unsafe { 4 => unsafe {
let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize; 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 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 dest = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *mut u8;
let mut src_ptr = src; memcpy(dest, src, count);
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);
}
}, },
// trash but fast memset
5 => unsafe { 5 => unsafe {
let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize; 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 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 = 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; memset(dest, src, 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);
}
}
}, },
_ => { _ => {
log::debug!("Unknown memory service message type: {}", msg_type); log::debug!("Unknown memory service message type: {}", msg_type);

View file

@ -1 +0,0 @@
# dt_api

View file

@ -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)
}

View file

@ -1,5 +1,4 @@
.{math, memory} := @use("../../stn/src/lib.hb"); .{math, memory, dt} := @use("../../stn/src/lib.hb");
.{dt_get} := @use("../../dt_api/src/lib.hb");
.{Color, Image} := @use("lib.hb"); .{Color, Image} := @use("lib.hb");
.{Vec2} := math .{Vec2} := math
@ -16,12 +15,12 @@ Context := struct {
} }
init := fn(): void { init := fn(): void {
width := dt_get(int, "framebuffer/fb0/width\0") width := dt.get(int, "framebuffer/fb0/width\0")
height := dt_get(int, "framebuffer/fb0/height\0") height := dt.get(int, "framebuffer/fb0/height\0")
pixels := width * height pixels := width * height
back_buffer := memory.alloc(Color, pixels * @bitcast(@sizeof(Color))) back_buffer := memory.alloc(Color, pixels * @bitcast(@sizeof(Color)))
ctx = Context.{ ctx = Context.{
fb: dt_get(^Color, "framebuffer/fb0/ptr\0"), fb: dt.get(^Color, "framebuffer/fb0/ptr\0"),
bb: back_buffer, bb: back_buffer,
buf: back_buffer, buf: back_buffer,
width, width,

View file

@ -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))
}

View file

@ -1,9 +1,9 @@
acs := @use("acs.hb") acs := @use("acs.hb")
string := @use("string.hb") string := @use("string.hb")
log := @use("log.hb") log := @use("log.hb")
memory := @use("memory.hb") memory := @use("memory.hb")
buffer := @use("buffer.hb") buffer := @use("buffer.hb")
math := @use("math.hb") math := @use("math.hb")
random := @use("random.hb") random := @use("random.hb")
file := @use("file_io.hb") file := @use("file_io.hb")
dt := @use("dt.hb")

View file

@ -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)) 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 { 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))
} }

View file

@ -1,14 +1,13 @@
dt_api := @use("../../../libraries/dt_api/src/lib.hb"); .{dt} := @use("../../../libraries/stn/src/lib.hb")
.{dt_get} := dt_api
main := fn(): int { main := fn(): int {
dt_api.dt_get("framebuffer/fb0/width\0") dt.get(int, "framebuffer/fb0/width\0")
dt_api.dt_get("cpu/cpu0/architecture\0") dt.get(int, "cpu/cpu0/architecture\0")
// Checking if the first detected serial port is memory mapped or port mapped // Checking if the first detected serial port is memory mapped or port mapped
// 0 -> memory mapped // 0 -> memory mapped
// 1 -> port mapped // 1 -> port mapped
dt_get("serial_ports/sp0/mapping\0") dt.get(int, "serial_ports/sp0/mapping\0")
return 0 return 0
} }

View file

@ -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
}

View file

@ -2,9 +2,7 @@ stn := @use("../../../libraries/stn/src/lib.hb");
.{string, memory, buffer, log} := stn .{string, memory, buffer, log} := stn
pci := @use("../../../libraries/pci/src/lib.hb"); pci := @use("../../../libraries/pci/src/lib.hb");
.{PCIAddress, get_ids, config_read32} := pci; .{PCIAddress, get_ids, config_read32} := pci
.{dt_get} := @use("../../../libraries/dt_api/src/lib.hb")
reg := @use("./reg.hb") reg := @use("./reg.hb")

View file

@ -3,11 +3,12 @@
default_entry = 1 default_entry = 1
timeout = 0 timeout = 0
verbose = false verbose = false
# interface_resolution = "1920x1080x24"
interface_resolution = "1024x768x24" interface_resolution = "1024x768x24"
# interface_resolution = "640x480x32" # interface_resolution = "640x480x24"
# Terminal related settings # Terminal related settings
term_wallpaper = "boot:///background.bmp" # term_wallpaper = "boot:///background.bmp"
# term_wallpaper = "boot:///empty-background.bmp" term_wallpaper = "boot:///empty-background.bmp"
term_background = "008080" term_background = "008080"
[boot.limine.ableos] [boot.limine.ableos]
@ -15,8 +16,9 @@ comment = "Default AbleOS boot entry."
protocol = "limine" protocol = "limine"
kernel_path = "boot:///kernel_${ARCH}" kernel_path = "boot:///kernel_${ARCH}"
kernel_cmdline = "" kernel_cmdline = ""
# resolution = "640x480x32" # resolution = "1920x1080x24"
resolution = "1024x768x24" resolution = "1024x768x24"
# resolution = "640x480x24"
[boot.limine.ableos.modules] [boot.limine.ableos.modules]