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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | //! virtio-console: a second terminal, `hvc0` ("hypervisor console", the
//! name Linux gives it). QEMU connects it to anything a *chardev* can be:
//!
//! -device virtio-serial-pci -device virtconsole,chardev=hvc
//! -chardev socket,id=hvc,path=target/hvc0.sock,server=on,wait=off
//! (then on the host: socat - UNIX-CONNECT:target/hvc0.sock)
//! -chardev file,id=hvc,path=out.txt,input-path=in.txt (tests)
//!
//! Two queues: 0 = receive (host -> us), 1 = transmit (us -> host). (With
//! the MULTIPORT feature there'd be more ports and a control queue; we
//! don't ask for it, so it's exactly one port, which QEMU opens for us.)
//!
//! * **Receiving**: we keep 8 empty 512-byte buffers in the receive queue.
//! When the host sends bytes the device fills one, puts it on the used
//! ring and raises an interrupt; we copy the bytes into INPUT and put the
//! buffer straight back. Readers of /dev/hvc0 wait on INPUT.
//! * **Sending**: 8 buffers too. A write copies into a free one and kicks
//! the device. If all 8 are still in flight (host not reading), we wait a
//! few milliseconds and then *drop* the output: kernel logging must never
//! hang because nobody is listening.
//!
//! Every kernel log line (`serial_print!`) also goes here -- except while
//! a program has /dev/hvc0 open (e.g. a shell runs on it via `getty`), so
//! that session isn't interleaved with log noise.
use ;
use cratememory;
use cratePciDevice;
use crateWaitQueue;
use ;
use OnceCell;
use ;
use ArrayQueue;
use Mutex;
use ;
const SLOTS: u16 = 8; // buffers per direction
const SLOT_SIZE: usize = 512; // 8 x 512 = one page per direction
static HVC: = new;
static PRESENT: AtomicBool = new;
/// Bytes received from the host, not read yet.
static INPUT: = uninit;
static INPUT_READY: WaitQueue = new;
/// How many open /dev/hvc0 files there are (kernel log mirroring pauses
/// while there are any).
pub static OPEN_COUNT: AtomicUsize = new;
/// Bytes we had to drop because the host wasn't reading.
pub static DROPPED: AtomicUsize = new;
/// virtio-serial: transitional id 0x1003, modern 0x1043.
*mut u8
/// Move received bytes from the used ring into INPUT and recycle the
/// buffers. Called from the interrupt handler, and by readers (in case the
/// interrupt line isn't one we handle).
/// Send bytes to the host. Never blocks for long: drops what doesn't fit
/// within ~10 ms. Returns how many bytes were sent.
/// Kernel log mirror (called by serial.rs for every `serial_print!`).
/// Read received bytes into `buf`; blocks until at least one is there.
/// Bytes waiting in the input buffer (tests).
/// Read all buffered input without blocking (tests).
|