lib.rs 2.0 KB

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