feat: serial device interfacing
[lunaix-os.git] / lunaix-os / hal / rng / rngx86.c
1 #include <lunaix/device.h>
2 #include <lunaix/mm/page.h>
3
4 static inline void
5 rng_fill(void* data, size_t len)
6 {
7     asm volatile("1:\n"
8                  "rdrand %%eax\n"
9                  "movl %%eax, (%0)\n"
10                  "addl $4, %%eax\n"
11                  "subl $4, %1\n"
12                  "jnz 1b" ::"r"((ptr_t)data),
13                  "r"((len & ~0x3))
14                  : "%eax");
15 }
16
17 static int
18 __rand_rd_pg(struct device* dev, void* buf, size_t offset)
19 {
20     rng_fill(buf, PG_SIZE);
21     return PG_SIZE;
22 }
23
24 static int
25 __rand_rd(struct device* dev, void* buf, size_t offset, size_t len)
26 {
27     rng_fill(buf, len);
28     return len;
29 }
30
31 int
32 pdev_randdev_init(struct device_def* devdef)
33 {
34     struct device* devrand = device_addseq(NULL, NULL, "rand");
35     devrand->ops.read = __rand_rd;
36     devrand->ops.read_page = __rand_rd_pg;
37
38     return 0;
39 }
40
41 static struct device_def devrandx86_def = {
42     .name = "x86 On-Chip RNG",
43     .class = DEVCLASS(DEVIF_SOC, DEVFN_CHAR, DEV_RNG, 0),
44     .init = pdev_randdev_init
45 };
46 EXPORT_DEVICE(randdev, &devrandx86_def, load_earlystage);