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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Framebuffer device.
//!
//! Provides interfaces for reading, mapping, and managing the format of the framebuffer.
//!
//! The function [`framebuffer`] is used to create two structs:
//! [`Framebuffer`], which is used to map an area of memory for the guest to
//! write to, and [`FramebufferAccess`], which is transformed into a [`View`]
//! to allow the VNC server to read from that memory.
//! In HvLite, the Framebuffer is used to create a [`FramebufferDevice`]
//! and a [`FramebufferLocalControl`] which share state using an inner mutex.
//! The latter implements [`FramebufferControl`] which provides the necessary
//! interfaces for a video device to control the framebuffer.
//! In Underhill, the format sender is extracted from the framebuffer and used
//! to create a different struct that implements the same trait.
//!
//! This is separate from the synthetic device because its lifetime is separate
//! from that of the synthetic video VMBus channel.

#![warn(missing_docs)]

use anyhow::Context;
use chipset_device::ChipsetDevice;
use guestmem::GuestMemory;
use guestmem::MappableGuestMemory;
use guestmem::MemoryMapper;
use inspect::Inspect;
use inspect::InspectMut;
use memory_range::MemoryRange;
use mesh::payload::Protobuf;
use mesh::MeshPayload;
use parking_lot::Mutex;
use sparse_mmap::Mappable;
use sparse_mmap::SparseMapping;
use std::convert::Infallible;
use std::io;
use std::sync::Arc;
use video_core::FramebufferControl;
use video_core::FramebufferFormat;
use video_core::ResolvedFramebuffer;
use video_core::SharedFramebufferHandle;
use vm_resource::kind::FramebufferHandleKind;
use vm_resource::ResolveResource;
use vmcore::device_state::ChangeDeviceState;
use vmcore::save_restore::RestoreError;
use vmcore::save_restore::SaveError;
use vmcore::save_restore::SaveRestore;
use vmcore::save_restore::SavedStateRoot;

fn default_framebuffer_format() -> FramebufferFormat {
    FramebufferFormat {
        width: 1024,
        height: 768,
        bytes_per_line: 1024 * 4,
        offset: 0,
    }
}

/// The framebuffer size. In the future, this will be variable.
// TODO: Make framebuffer size variable. See DetermineSynthVideoVramSize() in OS repo
pub const FRAMEBUFFER_SIZE: usize = 8 * 1024 * 1024; // 8 MB

/// Creates a framebuffer and an object that can be used to read from it.
/// The framebuffer should be allocated prior to calling this function.
///
/// * `vram`:   [`Mappable`] (`OwnedFd`/`OwnedHandle`) that can be mapped into guest memory
///             and a `SparseMapping` for the VNC server to read from. In HvLite, this
///             is created by `alloc_shared_memory`. In Underhill, this is `/dev/mshv_vtl_low`.
///
/// * `len`:    The amount of memory that was allocated for the framebuffer.
///
/// * `offset`: The `file_offset` that should be used when reading the framebuffer.
///             In HvLite, this should be 0. In Underhill, this is the GPA of the
///             VTL2 framebuffer mapping.
pub fn framebuffer(
    vram: Mappable,
    len: usize,
    offset: u64,
) -> io::Result<(Framebuffer, FramebufferAccess)> {
    assert_eq!(
        len, FRAMEBUFFER_SIZE,
        "no framebuffer size flexibility for now"
    );

    let (send, recv) = mesh::channel();

    let fb = Framebuffer {
        vram: vram.try_clone()?,
        len,
        format_send: send,
    };
    let access = FramebufferAccess {
        vram,
        len,
        format_recv: recv,
        offset,
    };
    Ok((fb, access))
}

/// The video framebuffer to be provided to the device.
#[derive(Debug, MeshPayload)]
pub struct Framebuffer {
    vram: Mappable,
    len: usize,
    format_send: mesh::Sender<FramebufferFormat>,
}

impl Framebuffer {
    /// Get the size of the framebuffer
    pub fn len(&self) -> usize {
        self.len
    }

    /// Extract format sender, consuming the framebuffer
    pub fn format_send(self) -> mesh::Sender<FramebufferFormat> {
        self.format_send
    }
}

/// An accessor for the framebuffer. Can be sent cross-process via mesh.
#[derive(Debug, MeshPayload)]
pub struct FramebufferAccess {
    vram: Mappable,
    len: usize,
    format_recv: mesh::Receiver<FramebufferFormat>,
    offset: u64,
}

impl FramebufferAccess {
    /// Maps the framebuffer view.
    pub fn view(self) -> io::Result<View> {
        let mapping = SparseMapping::new(self.len)?;
        mapping.map_file(0, self.len, &self.vram, self.offset, false)?;
        Ok(View {
            mapping,
            format_recv: self.format_recv,
            format: None,
            vram: self.vram,
            len: self.len,
            offset: self.offset,
        })
    }
}

/// A mapped view of the framebuffer.
#[derive(Debug)]
pub struct View {
    mapping: SparseMapping,
    format_recv: mesh::Receiver<FramebufferFormat>,
    format: Option<FramebufferFormat>,
    vram: Mappable,
    len: usize,
    offset: u64,
}

impl View {
    /// Reads a line within the framebuffer.
    pub fn read_line(&mut self, line: u16, data: &mut [u8]) {
        if let Some(format) = &self.format {
            if let Some(offset) = (line as usize)
                .checked_mul(format.bytes_per_line)
                .and_then(|x| x.checked_add(format.offset))
            {
                let len = std::cmp::min(data.len(), format.width * 4);
                let _ = self.mapping.read_at(offset, &mut data[..len]);
                return;
            }
        }
        data.fill(0);
    }

    /// Returns the current resolution.
    pub fn resolution(&mut self) -> (u16, u16) {
        // Get any framebuffer updates.
        //
        // FUTURE-use a channel/port type that throws away all but the last
        // message to avoid possible high memory use.
        while let Ok(format) = self.format_recv.try_recv() {
            self.format = Some(format);
        }
        if let Some(format) = &self.format {
            (format.width as u16, format.height as u16)
        } else {
            (1, 1)
        }
    }

    /// Gets the framebuffer access back.
    pub fn access(self) -> FramebufferAccess {
        // Put the current format at the head of the channel.
        let (send, recv) = mesh::channel();
        if let Some(format) = self.format {
            send.send(format);
        }
        send.bridge(self.format_recv);
        FramebufferAccess {
            vram: self.vram,
            len: self.len,
            format_recv: recv,
            offset: self.offset,
        }
    }
}

/// A chipset device for the framebuffer.
#[derive(InspectMut)]
pub struct FramebufferDevice {
    #[inspect(flatten)]
    inner: Arc<Mutex<FramebufferInner>>,
    #[inspect(hex)]
    len: usize,
}

/// Used to control a framebuffer running in the same process
#[derive(Clone)]
pub struct FramebufferLocalControl {
    inner: Arc<Mutex<FramebufferInner>>,
    len: usize,
}

#[derive(Inspect)]
struct FramebufferInner {
    #[inspect(skip)]
    _mem_fixed: Option<Box<dyn MappableGuestMemory>>,
    #[inspect(skip)]
    framebuffer: Option<Framebuffer>,
    mapping_state: Option<MappingState>,
    format: FramebufferFormat,
    #[inspect(skip)]
    mapper: Box<dyn MemoryMapper>,
}

#[derive(Inspect)]
struct MappingState {
    gpa: u64,
    subrange: MemoryRange,
    #[inspect(skip)]
    mem: Box<dyn MappableGuestMemory>,
}

/// Saved state.
#[derive(Debug, Clone, Protobuf, SavedStateRoot)]
#[mesh(package = "framebuffer")]
pub struct SavedState {
    #[mesh(1)]
    mapping: Option<SavedMappingState>,
    #[mesh(2)]
    format: FramebufferFormat,
}

#[derive(Debug, Clone, Protobuf)]
#[mesh(package = "framebuffer")]
struct SavedMappingState {
    #[mesh(1)]
    gpa: u64,
    #[mesh(2)]
    subrange: MemoryRange,
}

impl FramebufferDevice {
    /// Creates a new framebuffer device from the specified framebuffer
    /// using the given mapper. Optionally creates a second mapping that does
    /// not move once the VM is started. This can be used fo VTL2 to read from.
    pub fn new(
        mapper: Box<dyn MemoryMapper>,
        framebuffer: Framebuffer,
        framebuffer_gpa_base_fixed: Option<u64>,
    ) -> anyhow::Result<Self> {
        let len = framebuffer.len;

        // If the framebuffer is going to be used by VTL2, make a secondary mapping
        // for the VNC server to read that doesn't move once the vm is started.
        // This allows VTL2 to avoid remapping its framebuffer view if VTL0 moves it.
        let mem_fixed = if let Some(gpa) = framebuffer_gpa_base_fixed {
            let (mut mem, region) = mapper
                .new_region(len, "framebuffer-vtl2".to_owned())
                .context("failed to create vtl2 framebuffer memory region")?;

            region
                .map(0, &framebuffer.vram, 0, len, true)
                .context("failed to map vtl2 framebuffer memory region")?;

            mem.map_to_guest(gpa, true)?;
            Some(mem)
        } else {
            None
        };

        // Send the initial framebuffer format.
        let format = default_framebuffer_format();
        framebuffer.format_send.send(format);

        Ok(Self {
            inner: Arc::new(Mutex::new(FramebufferInner {
                _mem_fixed: mem_fixed,
                mapping_state: None,
                format,
                framebuffer: Some(framebuffer),
                mapper,
            })),
            len,
        })
    }

    /// Gets the inner framebuffer back.
    pub fn into_framebuffer(self) -> Framebuffer {
        self.inner.lock().framebuffer.take().unwrap()
    }

    /// Gets the control plane for the framebuffer.
    pub fn control(&self) -> FramebufferLocalControl {
        FramebufferLocalControl {
            inner: self.inner.clone(),
            len: self.len,
        }
    }
}

impl ChangeDeviceState for FramebufferDevice {
    fn start(&mut self) {}

    async fn stop(&mut self) {}

    async fn reset(&mut self) {
        let mut inner = self.inner.lock();
        inner.mapping_state = Default::default();
        if let Some(mut state) = inner.mapping_state.take() {
            state.mem.unmap_from_guest();
        }

        // TODO: clear VRAM
    }
}

impl ChipsetDevice for FramebufferDevice {}

impl SaveRestore for FramebufferDevice {
    type SavedState = SavedState;

    fn save(&mut self) -> Result<Self::SavedState, SaveError> {
        let inner = self.inner.lock();
        let mapping = inner.mapping_state.as_ref().map(
            |MappingState {
                 gpa,
                 subrange,
                 mem: _,
             }| SavedMappingState {
                gpa: *gpa,
                subrange: *subrange,
            },
        );
        Ok(SavedState {
            format: inner.format,
            mapping,
        })
    }

    fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
        let SavedState { mapping, format } = state;
        let mut inner = self.inner.lock();
        let inner = &mut *inner;
        inner.format = format;
        if let Some(mapping) = mapping {
            inner
                .map(mapping.gpa, Some(mapping.subrange))
                .context("failed to map VRAM to guest")
                .map_err(RestoreError::Other)?;
        }

        inner
            .framebuffer
            .as_mut()
            .unwrap()
            .format_send
            .send(inner.format);
        Ok(())
    }
}

impl FramebufferInner {
    fn map(&mut self, gpa: u64, framebuffer_range: Option<MemoryRange>) -> anyhow::Result<()> {
        if let Some(mut state) = self.mapping_state.take() {
            state.mem.unmap_from_guest();
        }

        let Some(framebuffer) = &self.framebuffer else {
            return Ok(());
        };

        let framebuffer_range =
            framebuffer_range.unwrap_or_else(|| MemoryRange::new(0..framebuffer.len as u64));

        let (mut mem, region) = self
            .mapper
            .new_region(framebuffer_range.len() as usize, "framebuffer".to_owned())
            .context("failed to create framebuffer region")?;

        region
            .map(
                0,
                &framebuffer.vram,
                framebuffer_range.start(),
                framebuffer_range.len() as usize,
                true,
            )
            .context("failed to map framebuffer memory")?;

        mem.map_to_guest(gpa, true)
            .context("failed to map VRAM to guest")?;
        self.mapping_state = Some(MappingState {
            gpa,
            subrange: framebuffer_range,
            mem,
        });

        tracing::debug!("Mapped VRAM to guest at address {:#x}", gpa);

        Ok(())
    }
}

impl FramebufferLocalControl {
    /// Maps the framebuffer to the guest at the specified GPA.
    ///
    /// `framebuffer_range` is an optional subrange of the framebuffer to map.
    pub fn map(&mut self, gpa: u64, framebuffer_range: Option<MemoryRange>) {
        if let Err(err) = self.inner.lock().map(gpa, framebuffer_range) {
            tracing::error!(
                gpa,
                error = err.as_ref() as &dyn std::error::Error,
                "failed to map framebuffer to guest"
            );
        }
    }

    /// Unmaps the framebuffer from the guest.
    pub fn unmap(&mut self) {
        let mut inner = self.inner.lock();
        if let Some(mut state) = inner.mapping_state.take() {
            state.mem.unmap_from_guest();
        }
    }

    /// Returns the size of the framebuffer in bytes.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Updates the framebuffer format.
    pub fn set_format(&mut self, format: FramebufferFormat) {
        let mut inner = self.inner.lock();
        let inner = &mut *inner;

        if inner.format != format {
            inner.format = format;
            if let Some(framebuffer) = &mut inner.framebuffer {
                framebuffer.format_send.send(inner.format);
            }
        }
    }

    /// Gets a `GuestMemory` object that can be used to access the framebuffer
    /// memory.
    pub fn memory(&self) -> io::Result<GuestMemory> {
        let inner = self.inner.lock();
        let framebuffer = inner
            .framebuffer
            .as_ref()
            .expect("framebuffer is still active");
        let mapping = SparseMapping::new(framebuffer.len())?;
        mapping.map_file(0, framebuffer.len(), &framebuffer.vram, 0, true)?;
        Ok(GuestMemory::new("framebuffer", mapping))
    }
}

// On the host the mapping is done immediately, but we still use the async trait
// so the video device doesn't have to be aware of the underlying implementation.
#[async_trait::async_trait]
impl FramebufferControl for FramebufferLocalControl {
    async fn map(&mut self, gpa: u64) {
        self.map(gpa, None);
    }
    async fn unmap(&mut self) {
        self.unmap();
    }
    async fn set_format(&mut self, format: FramebufferFormat) {
        self.set_format(format);
    }
}

impl ResolveResource<FramebufferHandleKind, SharedFramebufferHandle> for FramebufferLocalControl {
    type Output = ResolvedFramebuffer;
    type Error = Infallible;

    fn resolve(
        &self,
        _resource: SharedFramebufferHandle,
        _input: (),
    ) -> Result<Self::Output, Self::Error> {
        Ok(self.clone().into())
    }
}