lib.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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::pci;
  28. pub use self::transport::{DeviceStatus, DeviceType, Transport};
  29. use core::mem::size_of;
  30. use hal::*;
  31. /// The page size in bytes supported by the library (4 KiB).
  32. pub const PAGE_SIZE: usize = 0x1000;
  33. /// The type returned by driver methods.
  34. pub type Result<T = ()> = core::result::Result<T, Error>;
  35. /// The error type of VirtIO drivers.
  36. #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  37. pub enum Error {
  38. /// The buffer is too small.
  39. BufferTooSmall,
  40. /// The device is not ready.
  41. NotReady,
  42. /// The queue is already in use.
  43. AlreadyUsed,
  44. /// Invalid parameter.
  45. InvalidParam,
  46. /// Failed to alloc DMA memory.
  47. DmaError,
  48. /// I/O Error
  49. IoError,
  50. }
  51. /// Align `size` up to a page.
  52. fn align_up(size: usize) -> usize {
  53. (size + PAGE_SIZE) & !(PAGE_SIZE - 1)
  54. }
  55. /// The number of pages required to store `size` bytes, rounded up to a whole number of pages.
  56. fn pages(size: usize) -> usize {
  57. (size + PAGE_SIZE - 1) / PAGE_SIZE
  58. }
  59. /// Convert a struct into a byte buffer.
  60. unsafe trait AsBuf: Sized {
  61. fn as_buf(&self) -> &[u8] {
  62. unsafe { core::slice::from_raw_parts(self as *const _ as _, size_of::<Self>()) }
  63. }
  64. fn as_buf_mut(&mut self) -> &mut [u8] {
  65. unsafe { core::slice::from_raw_parts_mut(self as *mut _ as _, size_of::<Self>()) }
  66. }
  67. }