lib.rs 1.6 KB

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