lib.rs 1.6 KB

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