mirror of
https://github.com/hexagonal-sun/moss-kernel.git
synced 2025-12-23 22:47:55 -05:00
syscalls: sys_getrand: implement
Implement sys_getrand
This commit is contained in:
committed by
Matthew Leach
parent
5f2c2491b8
commit
8038a6c4e2
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -250,6 +250,7 @@ dependencies = [
|
||||
"num-traits",
|
||||
"object",
|
||||
"paste",
|
||||
"rand",
|
||||
"ringbuf",
|
||||
"static_cell",
|
||||
"thiserror",
|
||||
|
||||
@@ -26,6 +26,7 @@ num-traits = { version = "0.2", default-features = false }
|
||||
ringbuf = { version = "0.4.8", default-features = false, features = ["alloc"] }
|
||||
bitflags = "2.9.1"
|
||||
futures = { version = "0.3.31", default-features = false, features = ["alloc"] }
|
||||
rand = { version = "0.9.2", default-features = false, features = ["small_rng"] }
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
|
||||
@@ -262,7 +262,7 @@
|
||||
| 0x113 (275) | sched_getattr | (pid_t pid, struct sched_attr *uattr, unsigned int usize, unsigned int flags) | __arm64_sys_sched_getattr | false |
|
||||
| 0x114 (276) | renameat2 | (int olddfd, const char *oldname, int newdfd, const char *newname, unsigned int flags) | __arm64_sys_renameat2 | false |
|
||||
| 0x115 (277) | seccomp | (unsigned int op, unsigned int flags, void *uargs) | __arm64_sys_seccomp | false |
|
||||
| 0x116 (278) | getrandom | (char *ubuf, size_t len, unsigned int flags) | __arm64_sys_getrandom | false |
|
||||
| 0x116 (278) | getrandom | (char *ubuf, size_t len, unsigned int flags) | __arm64_sys_getrandom | true |
|
||||
| 0x117 (279) | memfd_create | (const char *uname, unsigned int flags) | __arm64_sys_memfd_create | false |
|
||||
| 0x118 (280) | bpf | (int cmd, union bpf_attr *uattr, unsigned int size) | __arm64_sys_bpf | false |
|
||||
| 0x119 (281) | execveat | (int fd, const char *filename, const char *const *argv, const char *const *envp, int flags) | __arm64_sys_execveat | false |
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::kernel::power::sys_reboot;
|
||||
use crate::kernel::rand::sys_getrandom;
|
||||
use crate::{
|
||||
arch::{Arch, ArchImpl},
|
||||
clock::{gettime::sys_clock_gettime, timeofday::sys_gettimeofday},
|
||||
@@ -265,6 +266,7 @@ pub async fn handle_syscall() {
|
||||
)
|
||||
.await
|
||||
}
|
||||
0x116 => sys_getrandom(TUA::from_value(arg1 as _), arg2 as _, arg3 as _).await,
|
||||
0x125 => Err(KernelError::NotSupported),
|
||||
0x1b7 => {
|
||||
sys_faccessat2(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod kpipe;
|
||||
pub mod power;
|
||||
pub mod rand;
|
||||
pub mod uname;
|
||||
|
||||
39
src/kernel/rand.rs
Normal file
39
src/kernel/rand.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
// TODO: generate a pool of entropy.
|
||||
|
||||
use crate::{
|
||||
drivers::timer::uptime,
|
||||
memory::uaccess::copy_to_user_slice,
|
||||
sync::{OnceLock, SpinLock},
|
||||
};
|
||||
use alloc::vec::Vec;
|
||||
use libkernel::error::Result;
|
||||
use libkernel::memory::address::TUA;
|
||||
use rand::{RngCore, SeedableRng, rngs::SmallRng};
|
||||
|
||||
pub async fn sys_getrandom(ubuf: TUA<u8>, size: isize, _flags: u32) -> Result<usize> {
|
||||
let buf = {
|
||||
let mut rng = ENTROPY_POOL
|
||||
.get_or_init(|| {
|
||||
let now = uptime();
|
||||
|
||||
SpinLock::new(SmallRng::seed_from_u64(
|
||||
(now.as_micros() & 0xffffffff_ffffffff) as u64,
|
||||
))
|
||||
})
|
||||
.lock_save_irq();
|
||||
|
||||
let mut buf = Vec::with_capacity(size as usize);
|
||||
|
||||
for _ in 0..size {
|
||||
buf.push((rng.next_u32() & 0xff) as u8);
|
||||
}
|
||||
|
||||
buf
|
||||
};
|
||||
|
||||
copy_to_user_slice(&buf, ubuf.to_untyped()).await?;
|
||||
|
||||
Ok(size as _)
|
||||
}
|
||||
|
||||
static ENTROPY_POOL: OnceLock<SpinLock<SmallRng>> = OnceLock::new();
|
||||
Reference in New Issue
Block a user