rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | //! virtio-rng: random bytes from the host (QEMU reads the host's
//! /dev/urandom). `-device virtio-rng-pci`.
//!
//! The simplest virtio device there is: one queue, and a request is just an
//! empty buffer the device fills with random bytes. We poll for the answer
//! (it takes microseconds) instead of waiting for an interrupt.
//!
//! Why bother? A computer is deterministic; good randomness has to come
//! from outside (hardware noise -- or here, the host). TCP needs
//! unpredictable sequence numbers and ports, or an attacker can guess them
//! and inject packets; programs need random keys and seeds. Without the
//! device we fall back to a generator seeded from the CPU's time stamp
//! counter -- fine for games, *not* for secrets, and we say so in the log.
//! (Real kernels also mix in the CPU's RDRAND and interrupt timings.)
use ;
use cratememory;
use cratePciDevice;
use ;
use ;
use Mutex;
use ;
static RNG: = new;
static HAVE_DEVICE: AtomicBool = new;
/// State of the fallback generator (xorshift64*).
static FALLBACK: AtomicU64 = new;
/// Is this the virtio entropy device? (transitional id 0x1005, modern 0x1044)
/// Is the host's entropy device in use (vs the weak fallback)?
/// Fill `buf` with random bytes. Returns false if the bytes came from the
/// weak fallback generator.
/// One request: up to a page of random bytes. Returns how many we got.
/// A random u64 (for seeds and port numbers).
/// xorshift64* (Marsaglia / Vigna), seeded and stirred with the TSC.
/// Acknowledge a (shared-line) interrupt; we poll, so nothing else to do.
|