nacl.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. use core::mem::size_of;
  2. use spec::binary::{SbiRet, SharedPtr};
  3. /// Nested Acceleration Extension
  4. ///
  5. /// Nested virtualization is the ability of a hypervisor to run another hypervisor
  6. /// as a guest. RISC-V nested virtualization requires an L0 hypervisor (running
  7. /// in hypervisor-mode) to trap-and-emulate the RISC-V H-extension functionality
  8. /// (such as CSR accesses, HFENCE instructions, HLV/HSV instructions,
  9. /// etc.) for the L1 hypervisor (running in virtualized supervisor-mode).
  10. ///
  11. /// The SBI nested acceleration extension defines a shared memory based interface
  12. /// between the SBI implementation (or L0 hypervisor) and the supervisor software
  13. /// (or L1 hypervisor) which allows both to collaboratively reduce traps taken
  14. /// by the L0 hypervisor for emulating RISC-V H-extension functionality. The
  15. /// nested acceleration shared memory allows the L1 hypervisor to batch multiple
  16. /// RISC-V H-extension CSR accesses and HFENCE requests which are then emulated
  17. /// by the L0 hypervisor upon an explicit synchronization SBI call.
  18. ///
  19. /// *NOTE:* The M-mode firmware should not implement the SBI nested acceleration
  20. /// extension if the underlying platform has the RISC-V H-extension implemented
  21. /// in hardware.
  22. pub trait Nacl: Send + Sync {
  23. /// Probe nested acceleration feature.
  24. ///
  25. /// Probe a nested acceleration feature. This is a mandatory function of the
  26. /// SBI nested acceleration extension.
  27. ///
  28. /// # Parameters
  29. ///
  30. /// The `feature_id` parameter specifies the nested acceleration feature to probe.
  31. ///
  32. /// # Return Value
  33. ///
  34. /// This function always returns SBI_SUCCESS in `SbiRet.error`. It returns `0`
  35. /// in `SbiRet.value` if the given `feature_id` is not available, or `1` in
  36. /// `SbiRet.value` if it is available.
  37. fn probe_feature(&self, feature_id: u32) -> SbiRet;
  38. /// Set nested acceleration shared memory.
  39. ///
  40. /// Set and enable the shared memory for nested acceleration on the calling
  41. /// hart.
  42. ///
  43. /// # Parameters
  44. ///
  45. /// If physical address of `shmem` are not all-ones bitwise, then the `shmem` pointer
  46. /// specifies the shared memory physical base address. The physical address of `shmem`
  47. /// MUST be 4096-byte aligned. The size of the shared memory must be 4096 + (XLEN * 128) bytes.
  48. /// The SBI implementation MUST zero the shared memory before returning from the SBI
  49. /// call.
  50. ///
  51. /// If physical address of `shmem` are all-ones bitwise, then the nested acceleration features
  52. /// are disabled.
  53. ///
  54. /// The `flags` parameter is reserved for future use and must be zero.
  55. ///
  56. /// # Return Value
  57. ///
  58. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  59. ///
  60. /// | Return code | Description
  61. /// |:----------------------------|:----------------------------------------------
  62. /// | `SbiRet::success()` | The steal-time shared memory physical base address was set or cleared successfully.
  63. /// | `SbiRet::invalid_param()` | The `flags` parameter is not zero or the `shmem` is not 4096-byte aligned.
  64. /// | `SbiRet::invalid_address()` | The shared memory pointed to by the `shmem` parameter is not writable or does not satisfy other requirements of shared memory physical address range.
  65. // fixme: `shmem` should have length of a constant definition like `SharedPtr<[u8; SHMEM_LEN]>`,
  66. // where `SHMEM_LEN` is defined in `sbi-spec` crate to be `4096 + (XLEN * 128)`.
  67. fn set_shmem(
  68. &self,
  69. shmem: SharedPtr<[u8; size_of::<usize>() * 128 + 4096]>,
  70. flags: usize,
  71. ) -> SbiRet;
  72. /// Synchronize shared memory CSRs.
  73. ///
  74. /// Synchronize CSRs in the nested acceleration shared memory. This is an
  75. /// optional function which is only available if the SBI_NACL_FEAT_SYNC_CSR
  76. /// feature is available.
  77. ///
  78. /// # Parameters
  79. ///
  80. /// The parameter `csr_num` specifies the set of RISC-V H-extension CSRs to be synchronized.
  81. ///
  82. /// If `csr_num` is all-ones bitwise then all RISC-V H-extension CSRs implemented by the SBI
  83. /// implementation (or L0 hypervisor) are synchronized.
  84. ///
  85. /// If `(csr_num & 0x300) == 0x200` and `csr_num < 0x1000` then only a single RISC-V H-extension
  86. /// CSR specified by the `csr_num` parameter is synchronized.
  87. ///
  88. /// # Return Value
  89. ///
  90. /// The possible error codes returned in `SbiRet.error` are shown in the table below:
  91. ///
  92. /// | Return code | Description
  93. /// |:--------------------------|:----------------------------------------------
  94. /// | `SbiRet::success()` | CSRs synchronized successfully.
  95. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_CSR feature is not available.
  96. /// | `SbiRet::invalid_param()` | `csr_num` is not all-ones bitwise and either: `(csr_num & 0x300) != 0x200` or `csr_num >= 0x1000` or `csr_num` is not implemented by the SBI implementation
  97. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  98. fn sync_csr(&self, csr_num: usize) -> SbiRet;
  99. /// Synchronize shared memory HFENCEs.
  100. ///
  101. /// Synchronize HFENCEs in the nested acceleration shared memory. This is an
  102. /// optional function which is only available if the SBI_NACL_FEAT_SYNC_HFENCE
  103. /// feature is available.
  104. ///
  105. /// # Parameters
  106. ///
  107. /// The parameter `entry_index` specifies the set of nested HFENCE entries to be synchronized.
  108. ///
  109. /// If `entry_index` is all-ones bitwise then all nested HFENCE entries are
  110. /// synchronized.
  111. ///
  112. /// If `entry_index < (3840 / XLEN)` then only a single nested HFENCE entry
  113. /// specified by the `entry_index` parameter is synchronized.
  114. ///
  115. /// # Return Value
  116. ///
  117. /// The possible error codes returned in `SbiRet.error` are shown in the table below:
  118. ///
  119. /// | Return code | Description
  120. /// |:--------------------------|:----------------------------------------------
  121. /// | `SbiRet::success()` | HFENCEs synchronized successfully.
  122. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_HFENCE feature is not available.
  123. /// | `SbiRet::invalid_param()` | `entry_index` is not all-ones bitwise and `entry_index >= (3840 / XLEN)`.
  124. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  125. fn sync_hfence(&self, entry_index: usize) -> SbiRet;
  126. /// Synchronize shared memory and emulate SRET.
  127. ///
  128. /// Synchronize CSRs and HFENCEs in the nested acceleration shared memory and
  129. /// emulate the SRET instruction. This is an optional function which is only
  130. /// available if the SBI_NACL_FEAT_SYNC_SRET feature is available.
  131. ///
  132. /// This function is used by supervisor software (or L1 hypervisor) to do
  133. /// a synchronize SRET request and the SBI implementation (or L0 hypervisor)
  134. /// MUST handle it.
  135. ///
  136. /// # Return Value
  137. ///
  138. /// This function does not return upon success and the possible error codes returned in
  139. /// `SbiRet.error` upon failure are shown in the table below:
  140. ///
  141. /// | Return code | Description
  142. /// |:--------------------------|:----------------------------------------------
  143. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_SRET feature is not available.
  144. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  145. fn sync_sret(&self) -> SbiRet;
  146. }
  147. impl<T: Nacl> Nacl for T {
  148. #[inline]
  149. fn probe_feature(&self, feature_id: u32) -> SbiRet {
  150. T::probe_feature(self, feature_id)
  151. }
  152. #[inline]
  153. fn set_shmem(
  154. &self,
  155. shmem: SharedPtr<[u8; size_of::<usize>() * 128 + 4096]>,
  156. flags: usize,
  157. ) -> SbiRet {
  158. T::set_shmem(self, shmem, flags)
  159. }
  160. #[inline]
  161. fn sync_csr(&self, csr_num: usize) -> SbiRet {
  162. T::sync_csr(self, csr_num)
  163. }
  164. #[inline]
  165. fn sync_hfence(&self, entry_index: usize) -> SbiRet {
  166. T::sync_hfence(self, entry_index)
  167. }
  168. #[inline]
  169. fn sync_sret(&self) -> SbiRet {
  170. T::sync_sret(self)
  171. }
  172. }