summaryrefslogtreecommitdiff
path: root/src/kernel/random.c
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@bpcspace.com>2021-08-24 14:09:29 -0500
committerBrett Weiland <brett_weiland@bpcspace.com>2021-08-24 14:09:29 -0500
commit9b22a6965579ea1867aea291d910c96f386b518b (patch)
treed06dbb9c4708f1cc713bcb115b32ff9bce4cf9b9 /src/kernel/random.c
parentbad4b0e9bdfee336bfc1c23761408279eaec1558 (diff)
major backup 8.24.21
Diffstat (limited to 'src/kernel/random.c')
-rw-r--r--src/kernel/random.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/kernel/random.c b/src/kernel/random.c
new file mode 100644
index 0000000..404223a
--- /dev/null
+++ b/src/kernel/random.c
@@ -0,0 +1,36 @@
+#include <stdbool.h>
+#include <cpuid.h>
+#include <printf.h>
+#include <stdint.h>
+
+static bool hw_random = false;
+static unsigned long int seed = -1;
+
+void randinit() {
+ unsigned int unused, eax, ecx;
+ eax = 0;
+ ecx = 0;
+ __get_cpuid(1, &eax, &unused, &ecx, &unused);
+ hw_random = (ecx >> 30) & 1;
+ printf("Kernel random source: %s.\n", (hw_random) ? "rdrand" : "pseudo");
+}
+
+//As of now, this function isn't nessesarily meant to be fast.
+unsigned int randint() {
+ uint64_t random_long = 0;
+ if(hw_random) {
+ asm("rand:\n"
+ "rdrand %0\n"
+ "jc finished\n"
+ "pause\n"
+ "jmp rand\n"
+ "finished:" :"=r"(random_long)::"rax" );
+ }
+ else {
+ //TODO FUCKING ENTROPY!!!!!!!111
+ seed = 1103515245 * seed + 12345;
+ return(unsigned int)(seed / 65536) % 32768;
+ }
+ return 0;
+}
+