1 #include <lunaix/device.h>
2 #include <lunaix/mm/page.h>
4 #include <klibc/string.h>
7 rng_fill(void* data, size_t len)
14 "jnz 1b" ::"r"((ptr_t)data),
20 __rand_rd_pg(struct device* dev, void* buf, size_t offset)
22 rng_fill(buf, PG_SIZE);
27 __rand_rd(struct device* dev, void* buf, size_t offset, size_t len)
29 if (unlikely(len < 4)) {
31 rng_fill(&tmp_buf, 4);
32 memcpy(buf, &tmp_buf, len);
40 pdev_randdev_init(struct device_def* devdef)
42 // FIXME add check on cpuid for presence of rdrand
43 struct device* devrand = device_allocseq(NULL, NULL);
44 devrand->ops.read = __rand_rd;
45 devrand->ops.read_page = __rand_rd_pg;
47 register_device(devrand, &devdef->class, "rand");
52 static struct device_def devrandx86_def = {
53 .name = "x86 On-Chip RNG",
54 .class = DEVCLASS(DEVIF_SOC, DEVFN_CHAR, DEV_RNG),
55 .init = pdev_randdev_init};
56 EXPORT_DEVICE(randdev, &devrandx86_def, load_onboot);