mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. pub mod mmio;
  2. use crate::{PhysAddr, PAGE_SIZE};
  3. use bitflags::bitflags;
  4. /// A VirtIO transport layer.
  5. pub trait Transport {
  6. /// Reads device features.
  7. fn read_device_features(&mut self) -> u64;
  8. /// Writes device features.
  9. fn write_driver_features(&mut self, driver_features: u64);
  10. /// Gets the max size of queue.
  11. fn max_queue_size(&self) -> u32;
  12. /// Notifies the given queue on the device.
  13. fn notify(&mut self, queue: u32);
  14. /// Sets the device status.
  15. fn set_status(&mut self, status: DeviceStatus);
  16. /// Sets the guest page size.
  17. fn set_guest_page_size(&mut self, guest_page_size: u32);
  18. /// Sets up the given queue.
  19. fn queue_set(
  20. &mut self,
  21. queue: u32,
  22. size: u32,
  23. descriptors: PhysAddr,
  24. driver_area: PhysAddr,
  25. device_area: PhysAddr,
  26. );
  27. /// Returns whether the queue is in use, i.e. has a nonzero PFN or is marked as ready.
  28. fn queue_used(&mut self, queue: u32) -> bool;
  29. /// Acknowledges an interrupt.
  30. ///
  31. /// Returns true on success.
  32. fn ack_interrupt(&mut self) -> bool;
  33. /// Begins initializing the device.
  34. ///
  35. /// Ref: virtio 3.1.1 Device Initialization
  36. fn begin_init(&mut self, negotiate_features: impl FnOnce(u64) -> u64) {
  37. self.set_status(DeviceStatus::ACKNOWLEDGE);
  38. self.set_status(DeviceStatus::DRIVER);
  39. let features = self.read_device_features();
  40. self.write_driver_features(negotiate_features(features));
  41. self.set_status(DeviceStatus::FEATURES_OK);
  42. self.set_guest_page_size(PAGE_SIZE as u32);
  43. }
  44. /// Finishes initializing the device.
  45. fn finish_init(&mut self) {
  46. self.set_status(DeviceStatus::DRIVER_OK);
  47. }
  48. /// Gets the pointer to the config space.
  49. fn config_space(&self) -> *mut u64;
  50. }
  51. bitflags! {
  52. /// The device status field.
  53. pub struct DeviceStatus: u32 {
  54. /// Indicates that the guest OS has found the device and recognized it
  55. /// as a valid virtio device.
  56. const ACKNOWLEDGE = 1;
  57. /// Indicates that the guest OS knows how to drive the device.
  58. const DRIVER = 2;
  59. /// Indicates that something went wrong in the guest, and it has given
  60. /// up on the device. This could be an internal error, or the driver
  61. /// didn’t like the device for some reason, or even a fatal error
  62. /// during device operation.
  63. const FAILED = 128;
  64. /// Indicates that the driver has acknowledged all the features it
  65. /// understands, and feature negotiation is complete.
  66. const FEATURES_OK = 8;
  67. /// Indicates that the driver is set up and ready to drive the device.
  68. const DRIVER_OK = 4;
  69. /// Indicates that the device has experienced an error from which it
  70. /// can’t recover.
  71. const DEVICE_NEEDS_RESET = 64;
  72. }
  73. }
  74. /// Types of virtio devices.
  75. #[repr(u8)]
  76. #[derive(Debug, Eq, PartialEq)]
  77. #[allow(missing_docs)]
  78. pub enum DeviceType {
  79. Invalid = 0,
  80. Network = 1,
  81. Block = 2,
  82. Console = 3,
  83. EntropySource = 4,
  84. MemoryBallooning = 5,
  85. IoMemory = 6,
  86. Rpmsg = 7,
  87. ScsiHost = 8,
  88. _9P = 9,
  89. Mac80211 = 10,
  90. RprocSerial = 11,
  91. VirtioCAIF = 12,
  92. MemoryBalloon = 13,
  93. GPU = 16,
  94. Timer = 17,
  95. Input = 18,
  96. Socket = 19,
  97. Crypto = 20,
  98. SignalDistributionModule = 21,
  99. Pstore = 22,
  100. IOMMU = 23,
  101. Memory = 24,
  102. }