From 12ee3a9b87c2a01adce5cb965ebeb58ceb54b22d Mon Sep 17 00:00:00 2001 From: koniifer <> Date: Fri, 30 Aug 2024 16:22:13 +0100 Subject: [PATCH] fix random implement hardware_random_u32 implement math.max, alter math.min remove unneeded dependencies from fb_driver --- kernel/src/arch/x86_64/mod.rs | 27 ++++++++++++++++++++++++++ kernel/src/holeybytes/ecah.rs | 2 +- sysdata/libraries/stn/src/math.hb | 11 +++++++++-- sysdata/libraries/stn/src/random.hb | 4 ++-- sysdata/programs/fb_driver/src/main.hb | 3 +-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index a67a7ed0d..7437b1e4e 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -226,6 +226,33 @@ pub fn hardware_random_u64() -> u64 { } } +pub fn hardware_random_u32() -> u32 { + use {log::trace, rdrand::RdRand}; + let gen = RdRand::new(); + match gen { + Ok(gen) => { + let ret = gen.try_next_u32().unwrap(); + trace!("Random {}", ret); + return ret; + } + Err(err) => { + warn!("RDRand not supported."); + // Try rdseed + let gen = RdSeed::new(); + match gen { + Ok(gen) => { + let ret = gen.try_next_u32().unwrap(); + trace!("Random {}", ret); + return ret; + } + Err(err) => { + panic!("Neither RDRand or RDSeed are supported") + } + } + } + } +} + pub fn get_edid() {} pub fn register_dump() { diff --git a/kernel/src/holeybytes/ecah.rs b/kernel/src/holeybytes/ecah.rs index 5da4a8de3..516ae2dd6 100644 --- a/kernel/src/holeybytes/ecah.rs +++ b/kernel/src/holeybytes/ecah.rs @@ -133,7 +133,7 @@ pub fn handler(vm: &mut Vm) { } // source of rng 4 => { - vm.registers[1] = hbvm::value::Value(crate::arch::hardware_random_u64()); + vm.registers[1] = hbvm::value::Value(crate::arch::hardware_random_u32() as u64); } buffer_id => { let mut buffs = IPC_BUFFERS.lock(); diff --git a/sysdata/libraries/stn/src/math.hb b/sysdata/libraries/stn/src/math.hb index 2fd1d0207..94ee78a30 100644 --- a/sysdata/libraries/stn/src/math.hb +++ b/sysdata/libraries/stn/src/math.hb @@ -1,7 +1,14 @@ +SIZEOF_INT := 31 + abs := fn(x: int): int { - mask := x >> 31 + mask := x >> SIZEOF_INT return (x ^ mask) - mask } min := fn(a: int, b: int): int { - return b + (a - b & a - b >> 31) + c := a - b + return b + (c & c >> SIZEOF_INT) +} +max := fn(a: int, b: int): int { + c := a - b + return a - (c & c >> SIZEOF_INT) } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/random.hb b/sysdata/libraries/stn/src/random.hb index df1f29771..dbff80a5b 100644 --- a/sysdata/libraries/stn/src/random.hb +++ b/sysdata/libraries/stn/src/random.hb @@ -1,5 +1,5 @@ -uint_64 := fn(min: uint, max: uint): uint { - rng := @eca(uint, 3, 4) +integer := fn(min: int, max: int): int { + rng := @eca(int, 3, 4) if min != 0 | max != 0 { return rng % (max - min + 1) + min diff --git a/sysdata/programs/fb_driver/src/main.hb b/sysdata/programs/fb_driver/src/main.hb index fafa1e7b9..8893c76bf 100644 --- a/sysdata/programs/fb_driver/src/main.hb +++ b/sysdata/programs/fb_driver/src/main.hb @@ -1,5 +1,4 @@ -.{log, memory, string, buffer} := @use("../../../libraries/stn/src/lib.hb"); -.{front_buffer_ptr, screenidx, ColorBGRA, Point} := @use("./lib.hb") +.{log, memory, string, buffer} := @use("../../../libraries/stn/src/lib.hb") main := fn(): int { buffer_id := buffer.create("XGraphics\0")