5c12ca662c6d95ef19e5c008d84e2427c3e1bde0
[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_allocseq(NULL, NULL);
35     devrand->ops.read = __rand_rd;
36     devrand->ops.read_page = __rand_rd_pg;
37
38     device_register(devrand, &devdef->class, "rand");
39
40     return 0;
41 }
42
43 static struct device_def devrandx86_def = {
44     .name = "x86 On-Chip RNG",
45     .class = DEVCLASS(DEVIF_SOC, DEVFN_CHAR, DEV_RNG),
46     .init = pdev_randdev_init};
47 EXPORT_DEVICE(randdev, &devrandx86_def, load_onboot);