nacl.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //! Chapter 15. Nested Acceleration Extension (EID #0x4E41434C "NACL")
  2. use crate::binary::{sbi_call_0, sbi_call_1, sbi_call_3};
  3. use sbi_spec::{
  4. binary::{SbiRet, SharedPtr},
  5. nacl::{shmem_size, EID_NACL, PROBE_FEATURE, SET_SHMEM, SYNC_CSR, SYNC_HFENCE, SYNC_SRET},
  6. };
  7. /// Probe a nested acceleration feature.
  8. ///
  9. /// This is a mandatory function of the SBI nested acceleration extension.
  10. ///
  11. /// # Parameters
  12. ///
  13. /// The `feature_id` parameter specifies the nested acceleration feature to probe.
  14. /// Possible feature IDs are defined in the table below:
  15. ///
  16. /// # Return value
  17. ///
  18. /// This function always returns `SbiRet::success()` in `SbiRet.error`.
  19. /// It returns 0 in `SbiRet.value` if the given `feature_id` is not available,
  20. /// or 1 in `SbiRet.value` if it is available.
  21. ///
  22. /// This function is defined in RISC-V SBI Specification chapter 15.5.
  23. #[inline]
  24. pub fn nacl_probe_feature(feature_id: u32) -> SbiRet {
  25. sbi_call_1(EID_NACL, PROBE_FEATURE, feature_id as _)
  26. }
  27. /// Set and enable the shared memory for nested acceleration on the calling hart.
  28. ///
  29. /// This is a mandatory function of the SBI nested acceleration extension.
  30. ///
  31. /// # Parameters
  32. ///
  33. /// If `shmem` parameter is not all-ones bitwise, then `shmem` specifies the shared
  34. /// memory physical base address. `shmem` MUST be 4096 bytes (i.e., page) aligned, and
  35. /// the size of the shared memory must be `4096 + (XLEN * 128)` bytes.
  36. ///
  37. /// If `shmem` parameter is all-ones bitwise, then the nested acceleration features
  38. /// are disabled.
  39. ///
  40. /// The `flags` parameter is reserved for future use and must be zero.
  41. ///
  42. /// The possible error codes returned in `SbiRet.error` are shown in the table below:
  43. ///
  44. /// | Error code | Description
  45. /// |:----------------------------|:---------------------------------
  46. /// | `SbiRet::success()` | Shared memory was set or cleared successfully.
  47. /// | `SbiRet::invalid_param()` | The `flags` parameter is not zero or or the `shmem` parameter is not 4096 bytes aligned.
  48. /// | `SbiRet::invalid_address()` | The shared memory pointed to by the `shmem` parameters does not satisfy the requirements.
  49. ///
  50. /// This function is defined in RISC-V SBI Specification chapter 15.6.
  51. #[inline]
  52. pub fn nacl_set_shmem(shmem: SharedPtr<[u8; shmem_size::NATIVE]>, flags: usize) -> SbiRet {
  53. sbi_call_3(
  54. EID_NACL,
  55. SET_SHMEM,
  56. shmem.phys_addr_lo(),
  57. shmem.phys_addr_hi(),
  58. flags,
  59. )
  60. }
  61. /// Synchronize CSRs in the nested acceleration shared memory.
  62. ///
  63. /// This is an optional function that is only available if the SBI_NACL_FEAT_SYNC_CSR feature is available.
  64. ///
  65. /// # Parameters
  66. ///
  67. /// The parameter `csr_num` specifies the set of RISC-V H-extension CSRs to be synchronized.
  68. ///
  69. /// If `csr_num` is all-ones bitwise, then all RISC-V H-extension CSRs implemented by the SBI implementation (or L0 hypervisor) are synchronized.
  70. ///
  71. /// If `(csr_num & 0x300) == 0x200` and `csr_num < 0x1000` then only a single
  72. /// RISC-V H-extension CSR specified by the csr_num parameter is synchronized.
  73. ///
  74. /// # Return value
  75. ///
  76. /// The possible error codes returned in `SbiRet.error` are shown in the table below:
  77. ///
  78. /// | Error code | Description
  79. /// |:--------------------------|:---------------------------------
  80. /// | `SbiRet::success()` | CSRs synchronized successfully.
  81. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_CSR feature is not available.
  82. /// | `SbiRet::invalid_param()` | `csr_num` is not all-ones bitwise and either: <br> * `(csr_num & 0x300) != 0x200` or <br> * `csr_num >= 0x1000` or <br> * `csr_num` is not implemented by the SBI implementation
  83. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  84. ///
  85. /// This function is defined in RISC-V SBI Specification chapter 15.7.
  86. #[inline]
  87. pub fn nacl_sync_csr(csr_num: usize) -> SbiRet {
  88. sbi_call_1(EID_NACL, SYNC_CSR, csr_num)
  89. }
  90. /// Synchronize HFENCEs in the nested acceleration shared memory.
  91. ///
  92. /// This is an optional function that is only available if the SBI_NACL_FEAT_SYNC_HFENCE feature is available.
  93. ///
  94. /// # Parameters
  95. ///
  96. /// The parameter `entry_index` specifies the set of nested HFENCE entries to be synchronized.
  97. ///
  98. /// If `entry_index` is all-ones bitwise, then all nested HFENCE entries are synchronized.
  99. ///
  100. /// If `entry_index < (3840 / XLEN)` then only a single nested HFENCE entry specified by the `entry_index` parameter is synchronized
  101. ///
  102. /// # Return value
  103. ///
  104. /// The possible error codes returned in `SbiRet.error` are shown in the table below:
  105. ///
  106. /// | Error code | Description
  107. /// |:--------------------------|:---------------------------------
  108. /// | `SbiRet::success()` | HFENCEs synchronized successfully.
  109. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_HFENCE feature is not available.
  110. /// | `SbiRet::invalid_param()` | `entry_index` is not all-ones bitwise and `entry_index >= (3840 / XLEN)`.
  111. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  112. ///
  113. /// This function is defined in RISC-V SBI Specification chapter 15.8.
  114. #[inline]
  115. pub fn nacl_sync_hfence(entry_index: usize) -> SbiRet {
  116. sbi_call_1(EID_NACL, SYNC_HFENCE, entry_index)
  117. }
  118. /// Synchronize CSRs and HFENCEs in the NACL shared memory and emulate the SRET instruction.
  119. ///
  120. /// This is an optional function that is only available if the SBI_NACL_FEAT_SYNC_SRET feature is available.
  121. ///
  122. /// This function is used by supervisor software (or L1 hypervisor) to do a synchronizing SRET request,
  123. /// and the SBI implementation (or L0 hypervisor) MUST handle it.
  124. ///
  125. /// # Return value
  126. ///
  127. /// This function does not return upon success, and the possible error codes
  128. /// returned in `SbiRet.error` upon failure are shown in the table below:
  129. ///
  130. /// | Error code | Description
  131. /// |:--------------------------|:------------
  132. /// | `SbiRet::no_shmem()` | Nested acceleration shared memory not available.
  133. /// | `SbiRet::not_supported()` | SBI_NACL_FEAT_SYNC_SRET feature is not available.
  134. ///
  135. /// This function is defined in RISC-V SBI Specification chapter 15.9.
  136. #[inline]
  137. pub fn nacl_sync_sret() -> SbiRet {
  138. sbi_call_0(EID_NACL, SYNC_SRET)
  139. }