1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::arch;
use crate::synch::spinlock::Spinlock;
static PARK_MILLER_LEHMER_SEED: Spinlock<u32> = Spinlock::new(0);
const RAND_MAX: u64 = 2_147_483_647;
fn generate_park_miller_lehmer_random_number() -> u32 {
let mut seed = PARK_MILLER_LEHMER_SEED.lock();
let random = ((u64::from(*seed) * 48271) % RAND_MAX) as u32;
*seed = random;
random
}
fn __sys_rand32() -> Option<u32> {
arch::processor::generate_random_number32()
}
fn __sys_rand64() -> Option<u64> {
arch::processor::generate_random_number64()
}
fn __sys_rand() -> u32 {
generate_park_miller_lehmer_random_number()
}
#[cfg(not(feature = "newlib"))]
#[no_mangle]
pub fn sys_secure_rand32() -> Option<u32> {
kernel_function!(__sys_rand32())
}
#[cfg(not(feature = "newlib"))]
#[no_mangle]
pub fn sys_secure_rand64() -> Option<u64> {
kernel_function!(__sys_rand64())
}
#[no_mangle]
pub extern "C" fn sys_rand() -> u32 {
kernel_function!(__sys_rand())
}
fn __sys_srand(seed: u32) {
*(PARK_MILLER_LEHMER_SEED.lock()) = seed;
}
#[no_mangle]
pub extern "C" fn sys_srand(seed: u32) {
kernel_function!(__sys_srand(seed))
}
pub fn random_init() {
let seed: u32 = arch::processor::get_timestamp() as u32;
*PARK_MILLER_LEHMER_SEED.lock() = seed;
}