console.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use crate::memory_range::Physical;
  2. use spec::binary::SbiRet;
  3. /// Debug Console Extension
  4. ///
  5. /// The debug console extension defines a generic mechanism for debugging
  6. /// and boot-time early prints from supervisor-mode software.
  7. ///
  8. /// Kernel developers should switch to driver based console implementation
  9. /// instead of using this extension to prevent possible race conditions between
  10. /// the firmware and the kernel when drivers are ready.
  11. ///
  12. /// If the underlying physical console has extra bits for error checking
  13. /// (or correction) then these extra bits should be handled by the SBI
  14. /// implementation.
  15. ///
  16. /// *NOTE:* It is recommended that bytes sent/received using the debug
  17. /// console extension follow UTF-8 character encoding.
  18. pub trait Console {
  19. /// Write bytes to the debug console from input memory.
  20. ///
  21. /// # Parameters
  22. ///
  23. /// The `bytes` parameter specifies the input memory, including its length
  24. /// and memory physical base address (both lower and upper bits).
  25. ///
  26. /// # Non-blocking function
  27. ///
  28. /// This is a non-blocking SBI call and it may do partial/no writes if
  29. /// the debug console is not able to accept more bytes.
  30. ///
  31. /// # Return value
  32. ///
  33. /// The number of bytes written is returned in `SbiRet.value` and the
  34. /// possible return error codes returned in `SbiRet.error` are shown in
  35. /// the table below:
  36. ///
  37. /// | Return code | Description
  38. /// |:--------------------------|:----------------------------------------------
  39. /// | `SbiRet::success()` | Bytes written successfully.
  40. /// | `SbiRet::invalid_param()` | The memory pointed to by `bytes` does not satisfy the [requirements](struct.Physical.html#Requirements) described in shared memory physical address range.
  41. /// | `SbiRet::failed()` | Failed to write due to I/O errors.
  42. fn write(&self, bytes: Physical<&[u8]>) -> SbiRet;
  43. /// Read bytes from the debug console into an output memory.
  44. ///
  45. /// # Parameters
  46. ///
  47. /// The `bytes` parameter specifies the output memory, including the maximum
  48. /// bytes which can be written, and its memory physical base address
  49. /// (both lower and upper bits).
  50. ///
  51. /// # Non-blocking function
  52. ///
  53. /// This is a non-blocking SBI call and it will not write anything
  54. /// into the output memory if there are no bytes to be read in the
  55. /// debug console.
  56. ///
  57. /// # Return value
  58. ///
  59. /// The number of bytes read is returned in `SbiRet.value` and the
  60. /// possible return error codes returned in `SbiRet.error` are shown in
  61. /// the table below:
  62. ///
  63. /// | Return code | Description
  64. /// |:--------------------------|:----------------------------------------------
  65. /// | `SbiRet::success()` | Bytes read successfully.
  66. /// | `SbiRet::invalid_param()` | The memory pointed to by `bytes` does not satisfy the [requirements](struct.Physical.html#Requirements) described in shared memory physical address range.
  67. /// | `SbiRet::failed()` | Failed to read due to I/O errors.
  68. fn read(&self, bytes: Physical<&mut [u8]>) -> SbiRet;
  69. /// Write a single byte to the debug console.
  70. ///
  71. /// # Blocking function
  72. ///
  73. /// This is a blocking SBI call and it will only return after writing
  74. /// the specified byte to the debug console. It will also return, with
  75. /// `SbiRet::failed()`, if there are I/O errors.
  76. /// # Return value
  77. ///
  78. /// The `SbiRet.value` is set to zero and the possible return error
  79. /// codes returned in `SbiRet.error` are shown in the table below:
  80. ///
  81. /// | Return code | Description
  82. /// |:--------------------------|:----------------------------------------------
  83. /// | `SbiRet::success()` | Byte written successfully.
  84. /// | `SbiRet::failed()` | Failed to write the byte due to I/O errors.
  85. fn write_byte(&self, byte: u8) -> SbiRet;
  86. }
  87. impl<T: Console> Console for &T {
  88. #[inline]
  89. fn write(&self, bytes: Physical<&[u8]>) -> SbiRet {
  90. T::write(self, bytes)
  91. }
  92. #[inline]
  93. fn read(&self, bytes: Physical<&mut [u8]>) -> SbiRet {
  94. T::read(self, bytes)
  95. }
  96. #[inline]
  97. fn write_byte(&self, byte: u8) -> SbiRet {
  98. T::write_byte(self, byte)
  99. }
  100. }