lib.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //! VirtIO guest drivers.
  2. #![no_std]
  3. #![deny(unused_must_use, missing_docs)]
  4. #![allow(clippy::identity_op)]
  5. #![allow(dead_code)]
  6. extern crate alloc;
  7. mod blk;
  8. mod console;
  9. mod gpu;
  10. mod hal;
  11. mod input;
  12. mod net;
  13. mod queue;
  14. mod transport;
  15. pub use self::blk::{BlkResp, RespStatus, VirtIOBlk};
  16. pub use self::console::VirtIOConsole;
  17. pub use self::gpu::VirtIOGpu;
  18. pub use self::hal::{Hal, PhysAddr, VirtAddr};
  19. pub use self::input::{InputConfigSelect, InputEvent, VirtIOInput};
  20. pub use self::net::VirtIONet;
  21. use self::queue::VirtQueue;
  22. pub use self::transport::mmio::LegacyMmioTransport;
  23. pub use self::transport::{DeviceStatus, DeviceType, Transport};
  24. use core::mem::size_of;
  25. use hal::*;
  26. /// The page size in bytes supported by the library (4 KiB).
  27. const PAGE_SIZE: usize = 0x1000;
  28. /// The type returned by driver methods.
  29. pub type Result<T = ()> = core::result::Result<T, Error>;
  30. /// The error type of VirtIO drivers.
  31. #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  32. pub enum Error {
  33. /// The buffer is too small.
  34. BufferTooSmall,
  35. /// The device is not ready.
  36. NotReady,
  37. /// The queue is already in use.
  38. AlreadyUsed,
  39. /// Invalid parameter.
  40. InvalidParam,
  41. /// Failed to alloc DMA memory.
  42. DmaError,
  43. /// I/O Error
  44. IoError,
  45. }
  46. /// Align `size` up to a page.
  47. fn align_up(size: usize) -> usize {
  48. (size + PAGE_SIZE) & !(PAGE_SIZE - 1)
  49. }
  50. /// The number of pages required to store `size` bytes, rounded up to a whole number of pages.
  51. fn pages(size: usize) -> usize {
  52. (size + PAGE_SIZE - 1) / PAGE_SIZE
  53. }
  54. /// Convert a struct into a byte buffer.
  55. unsafe trait AsBuf: Sized {
  56. fn as_buf(&self) -> &[u8] {
  57. unsafe { core::slice::from_raw_parts(self as *const _ as _, size_of::<Self>()) }
  58. }
  59. fn as_buf_mut(&mut self) -> &mut [u8] {
  60. unsafe { core::slice::from_raw_parts_mut(self as *mut _ as _, size_of::<Self>()) }
  61. }
  62. }