Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
rand.c
Go to the documentation of this file.
1#include "hal/rand/rand.h"
2#include "hal/acpi/hpet.h"
3#include "hal/cpu/cpuid.h"
4#include "init/init.h"
5#include "libk/serial.h"
6
8
9#define RAND_MAX 0xFFFFFFFF
10
11INIT(Rand) {
12 uint32_t ecx, unused;
13 cpuid(1, 0, &unused, &unused, &ecx, &unused);
14 random_available = (ecx >> 30) & 1;
15 LOG_INFO("Rand ", "random is available %d", random_available);
16}
17
19 if (random_available) {
20 uint32_t rand = 0;
21 asm volatile("rdrand %0" : "=r"(rand));
22 return rand & RAND_MAX;
23 } else if (vxHPETIsAvailable()) {
24 // fallback using HPET if available
26 return rand;
27 }
28 return 0;
29}
void cpuid(uint32_t leaf, uint32_t subleaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
Definition cpuid.c:4
boolean_t vxHPETIsAvailable()
Definition hpet.c:14
uint64_t vxHPETGetMainCount()
Definition hpet.c:82
#define INIT(fn)
Definition init.h:26
#define RAND_MAX
Definition rand.c:9
static boolean_t random_available
Definition rand.c:7
uint32_t vxRand()
Definition rand.c:18
#define LOG_INFO(mod, fmt,...)
Definition serial.h:20
unsigned int uint32_t
Definition type.h:19
uint8_t boolean_t
Definition type.h:89