|
| 1 | + |
| 2 | +use crate::virtio::console::{CONSOLE_DEVICE_ID}; |
| 3 | + |
| 4 | + |
| 5 | +use std::borrow::{Borrow, BorrowMut}; |
| 6 | +use std::io::stdout; |
| 7 | +use std::ops::DerefMut; |
| 8 | +use std::sync::{Arc, Mutex}; |
| 9 | +use virtio_console::console; |
| 10 | + |
| 11 | +use virtio_device::{VirtioConfig, VirtioDeviceActions, VirtioDeviceType, VirtioMmioDevice}; |
| 12 | +use crate::virtio::{CommonConfig, Env, SingleFdSignalQueue, QUEUE_MAX_SIZE}; |
| 13 | +use virtio_queue::Queue; |
| 14 | +use vm_device::bus::MmioAddress; |
| 15 | +use vm_device::device_manager::MmioManager; |
| 16 | +use vm_device::{DeviceMmio, MutDeviceMmio}; |
| 17 | +use vm_memory::GuestAddressSpace; |
| 18 | +use crate::virtio::console::queue_handler::QueueHandler; |
| 19 | +use super::inorder_handler::InOrderQueueHandler; |
| 20 | + |
| 21 | +use super::{ConsoleArgs, Error, Result}; |
| 22 | + |
| 23 | +pub struct Console<M: GuestAddressSpace> { |
| 24 | + cfg: CommonConfig<M>, |
| 25 | + // allow_resize: bool, |
| 26 | + // allow_multiport: bool, |
| 27 | + // allow_emerg_write: bool, |
| 28 | +} |
| 29 | + |
| 30 | +impl<M> Console<M> |
| 31 | +where |
| 32 | + M: GuestAddressSpace + Clone + Send + 'static, |
| 33 | +{ |
| 34 | + pub fn new<B>(env: &mut Env<M, B>, args: &ConsoleArgs) -> Result<Arc<Mutex<Self>>> |
| 35 | + where |
| 36 | + // We're using this (more convoluted) bound so we can pass both references and smart |
| 37 | + // pointers such as mutex guards here. |
| 38 | + B: DerefMut, |
| 39 | + B::Target: MmioManager<D = Arc<dyn DeviceMmio + Send + Sync>>, |
| 40 | + { |
| 41 | + let device_features = args.device_features(); |
| 42 | + |
| 43 | + let queues = vec![ |
| 44 | + Queue::new(env.mem.clone(), QUEUE_MAX_SIZE), |
| 45 | + Queue::new(env.mem.clone(), QUEUE_MAX_SIZE), |
| 46 | + ]; |
| 47 | + |
| 48 | + let config_space = Vec::new(); |
| 49 | + let virtio_cfg = VirtioConfig::new(device_features, queues, config_space); |
| 50 | + |
| 51 | + let common_cfg = CommonConfig::new(virtio_cfg, env).map_err(Error::Virtio)?; |
| 52 | + |
| 53 | + let console = Arc::new(Mutex::new(Console { |
| 54 | + cfg: common_cfg, |
| 55 | + // allow_resize: args.allow_resize, |
| 56 | + // allow_multiport: args.allow_multiport, |
| 57 | + // allow_emerg_write: args.allow_emerg_write, |
| 58 | + })); |
| 59 | + |
| 60 | + env.register_mmio_device(console.clone()) |
| 61 | + .map_err(Error::Virtio)?; |
| 62 | + |
| 63 | + Ok(console) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioDeviceType for Console<M> { |
| 68 | + fn device_type(&self) -> u32 { |
| 69 | + CONSOLE_DEVICE_ID |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<M: GuestAddressSpace + Clone + Send + 'static> Borrow<VirtioConfig<M>> for Console<M> { |
| 74 | + fn borrow(&self) -> &VirtioConfig<M> { |
| 75 | + &self.cfg.virtio |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<M: GuestAddressSpace + Clone + Send + 'static> BorrowMut<VirtioConfig<M>> for Console<M> { |
| 80 | + fn borrow_mut(&mut self) -> &mut VirtioConfig<M> { |
| 81 | + &mut self.cfg.virtio |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioDeviceActions for Console<M> { |
| 86 | + type E = Error; |
| 87 | + |
| 88 | + fn activate(&mut self) -> Result<()> { |
| 89 | + |
| 90 | + // let mut features = self.cfg.virtio.driver_features; |
| 91 | + |
| 92 | + let driver_notify = SingleFdSignalQueue { |
| 93 | + irqfd: self.cfg.irqfd.clone(), |
| 94 | + interrupt_status: self.cfg.virtio.interrupt_status.clone(), |
| 95 | + }; |
| 96 | + |
| 97 | + let mut ioevents = self.cfg.prepare_activate().map_err(Error::Virtio)?; |
| 98 | + |
| 99 | + let inner = InOrderQueueHandler { |
| 100 | + driver_notify, |
| 101 | + receiveq: self.cfg.virtio.queues.remove(0), |
| 102 | + transmitq: self.cfg.virtio.queues.remove(0), |
| 103 | + output: stdout(), |
| 104 | + console: console::Console::new(), |
| 105 | + }; |
| 106 | + |
| 107 | + let handler = Arc::new(Mutex::new(QueueHandler { |
| 108 | + inner, |
| 109 | + receiveqfd: ioevents.remove(0), |
| 110 | + transmitqfd: ioevents.remove(0), |
| 111 | + })); |
| 112 | + |
| 113 | + self.cfg.finalize_activate(handler).map_err(Error::Virtio) |
| 114 | + } |
| 115 | + |
| 116 | + fn reset(&mut self) -> Result<()> { |
| 117 | + // Not implemented for now. |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioMmioDevice<M> for Console<M> {} |
| 123 | + |
| 124 | +impl<M: GuestAddressSpace + Clone + Send + 'static> MutDeviceMmio for Console<M> { |
| 125 | + fn mmio_read(&mut self, _base: MmioAddress, offset: u64, data: &mut [u8]) { |
| 126 | + self.read(offset, data); |
| 127 | + } |
| 128 | + |
| 129 | + fn mmio_write(&mut self, _base: MmioAddress, offset: u64, data: &[u8]) { |
| 130 | + self.write(offset, data); |
| 131 | + } |
| 132 | +} |
0 commit comments