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 246 247 | //! virtio-gpu, 2D only: a framebuffer the host shows on its screen.
//! `-vga none -device virtio-vga` (virtio-gpu plus the old VGA modes, so
//! the BIOS and our VGA text console keep working until we switch over).
//!
//! The GPU doesn't scan out of our memory directly. The protocol has
//! *resources* (images living on the host) and *backing* (guest pages
//! holding the pixels). Setting up a framebuffer, every step one command on
//! the control queue:
//!
//! 1. GET_DISPLAY_INFO which outputs ("scanouts") exist, what size
//! 2. RESOURCE_CREATE_2D a 1024x768 image, format B8G8R8X8, id 1
//! 3. RESOURCE_ATTACH_BACKING its pixels live in these guest pages
//! 4. SET_SCANOUT show resource 1 on scanout 0
//!
//! Then, whenever we've drawn something:
//!
//! 5. TRANSFER_TO_HOST_2D copy a rectangle from our pages to the host
//! 6. RESOURCE_FLUSH and update the screen there
//!
//! Each command is a request buffer (a 24-byte header + arguments) and a
//! response buffer the device writes (a header whose type says OK or which
//! error). We wait for each answer by polling -- it's a function call into
//! QEMU, microseconds.
//!
//! 3D (virgl) would add contexts, 3D resources and a command stream of
//! Gallium/OpenGL commands that Mesa in the guest produces and
//! virglrenderer on the host executes. That's a lot of protocol; 2D is the
//! foundation it builds on (3D still ends in SET_SCANOUT + RESOURCE_FLUSH).
use ;
use cratememory;
use cratePciDevice;
use ;
use OnceCell;
use Mutex;
use ;
/// The framebuffer size we ask for (the device's preferred size is only
/// logged: fixed is predictable for tests and screenshots).
pub const WIDTH: u32 = 1024;
pub const HEIGHT: u32 = 768;
// Command types (virtio spec 5.7.6.7) and response types.
pub const CMD_GET_DISPLAY_INFO: u32 = 0x0100;
pub const CMD_RESOURCE_CREATE_2D: u32 = 0x0101;
pub const CMD_SET_SCANOUT: u32 = 0x0103;
pub const CMD_RESOURCE_FLUSH: u32 = 0x0104;
pub const CMD_TRANSFER_TO_HOST_2D: u32 = 0x0105;
pub const CMD_RESOURCE_ATTACH_BACKING: u32 = 0x0106;
pub const RESP_OK_NODATA: u32 = 0x1100;
pub const RESP_OK_DISPLAY_INFO: u32 = 0x1101;
pub const RESP_ERR_INVALID_RESOURCE_ID: u32 = 0x1203;
/// Pixel format: bytes B, G, R, unused -- i.e. a little-endian u32 is
/// 0x00RRGGBB, the usual way to write colours.
const FORMAT_B8G8R8X8: u32 = 2;
const RESOURCE_ID: u32 = 1;
/// Where the pixels are. Pixel (x, y) is the u32 at `base + y * stride + x`.
static GPU: = new;
static FB: = uninit;
/// The host's preferred display size, as reported by GET_DISPLAY_INFO.
static PREFERRED: = new;
/// virtio-gpu: modern-only, id 0x1040 + 16. (virtio-vga is the same id
/// with the VGA display class.)
/// The framebuffer, once the GPU is set up.
/// Send one raw command and return the response type (0 = no answer).
/// Exposed for the tests, which check that errors come back as errors.
/// Push the rectangle (x, y, w, h) of the framebuffer to the screen:
/// TRANSFER_TO_HOST_2D + RESOURCE_FLUSH, both queued before we wait.
/// Clipped to the framebuffer; returns false if the GPU said no.
/// The host's preferred display size (from GET_DISPLAY_INFO).
/// Acknowledge interrupts (e.g. "display config changed" when the QEMU
/// window is resized); we poll, so nothing else to do.
|