ecall.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //! 这个模块将会处理所有的SBI调用陷入
  2. // 你应该在riscv-rt或其它中断处理函数里,调用这个模块的内容
  3. mod base;
  4. mod hsm;
  5. mod ipi;
  6. mod legacy;
  7. mod pmu;
  8. mod rfence;
  9. mod srst;
  10. mod timer;
  11. pub const EXTENSION_BASE: usize = 0x10;
  12. pub const EXTENSION_TIMER: usize = 0x54494D45;
  13. pub const EXTENSION_IPI: usize = 0x735049;
  14. pub const EXTENSION_RFENCE: usize = 0x52464E43;
  15. pub const EXTENSION_HSM: usize = 0x48534D;
  16. pub const EXTENSION_SRST: usize = 0x53525354;
  17. pub const EXTENSION_PMU: usize = 0x504D55;
  18. const LEGACY_SET_TIMER: usize = 0x0;
  19. const LEGACY_CONSOLE_PUTCHAR: usize = 0x01;
  20. const LEGACY_CONSOLE_GETCHAR: usize = 0x02;
  21. // const LEGACY_CLEAR_IPI: usize = 0x03;
  22. const LEGACY_SEND_IPI: usize = 0x04;
  23. // const LEGACY_REMOTE_FENCE_I: usize = 0x05;
  24. // const LEGACY_REMOTE_SFENCE_VMA: usize = 0x06;
  25. // const LEGACY_REMOTE_SFENCE_VMA_ASID: usize = 0x07;
  26. const LEGACY_SHUTDOWN: usize = 0x08;
  27. /// Supervisor environment call handler function
  28. ///
  29. /// This function is used by platform runtime to handle environment call `ecall` instruction.
  30. ///
  31. /// You should call this function in your runtime's exception handler.
  32. /// If the incoming exception is caused by supervisor `ecall`,
  33. /// call this function with parameters extracted from trap frame.
  34. /// After this function returns, store the return value into `a0` and `a1` parameters.
  35. ///
  36. /// This function also adapts to the legacy functions.
  37. /// If the supervisor called any of legacy function, the `a0` and `a1` parameter
  38. /// is transferred to error and value field of `SbiRet` respectively.
  39. /// In this case, implementations should always store the result into `a0` and `a1` in
  40. /// any environment call functions including legacy functions.
  41. ///
  42. /// # Example
  43. ///
  44. /// A typical usage:
  45. ///
  46. /// ```no_run
  47. /// #[exception]
  48. /// fn handle_exception(ctx: &mut TrapFrame) {
  49. /// if mcause::read().cause() == Trap::Exception(Exception::SupervisorEnvCall) {
  50. /// let params = [ctx.a0, ctx.a1, ctx.a2, ctx.a3, ctx.a4, ctx.a5];
  51. /// let ans = rustsbi::ecall(ctx.a7, ctx.a6, params);
  52. /// ctx.a0 = ans.error;
  53. /// ctx.a1 = ans.value;
  54. /// mepc::write(mepc::read().wrapping_add(4));
  55. /// }
  56. /// // other conditions..
  57. /// }
  58. /// ```
  59. ///
  60. /// Do not forget to advance `mepc` by 4 after an ecall is handled.
  61. /// This skips the `ecall` instruction itself which is 4-byte long in all conditions.
  62. #[inline]
  63. pub fn handle_ecall(extension: usize, function: usize, param: [usize; 6]) -> SbiRet {
  64. match extension {
  65. EXTENSION_RFENCE => {
  66. rfence::handle_ecall_rfence(function, param[0], param[1], param[2], param[3], param[4])
  67. }
  68. EXTENSION_TIMER => match () {
  69. #[cfg(target_pointer_width = "64")]
  70. () => timer::handle_ecall_timer_64(function, param[0]),
  71. #[cfg(target_pointer_width = "32")]
  72. () => timer::handle_ecall_timer_32(function, param[0], param[1]),
  73. },
  74. EXTENSION_IPI => ipi::handle_ecall_ipi(function, param[0], param[1]),
  75. EXTENSION_BASE => base::handle_ecall_base(function, param[0]),
  76. EXTENSION_HSM => hsm::handle_ecall_hsm(function, param[0], param[1], param[2]),
  77. EXTENSION_SRST => srst::handle_ecall_srst(function, param[0], param[1]),
  78. EXTENSION_PMU => match () {
  79. #[cfg(target_pointer_width = "64")]
  80. () => {
  81. pmu::handle_ecall_pmu_64(function, param[0], param[1], param[2], param[3], param[4])
  82. }
  83. #[cfg(target_pointer_width = "32")]
  84. () => pmu::handle_ecall_pmu_32(
  85. function, param[0], param[1], param[2], param[3], param[4], param[5],
  86. ),
  87. },
  88. LEGACY_SET_TIMER => match () {
  89. #[cfg(target_pointer_width = "64")]
  90. () => legacy::set_timer_64(param[0]),
  91. #[cfg(target_pointer_width = "32")]
  92. () => legacy::set_timer_32(param[0], param[1]),
  93. }
  94. .legacy_void(param[0], param[1]),
  95. LEGACY_CONSOLE_PUTCHAR => legacy::console_putchar(param[0]).legacy_void(param[0], param[1]),
  96. LEGACY_CONSOLE_GETCHAR => legacy::console_getchar().legacy_return(param[1]),
  97. LEGACY_SEND_IPI => legacy::send_ipi(param[0]).legacy_void(param[0], param[1]),
  98. LEGACY_SHUTDOWN => legacy::shutdown().legacy_void(param[0], param[1]),
  99. _ => SbiRet::not_supported(),
  100. }
  101. }
  102. /// Call result returned by SBI
  103. ///
  104. /// After `handle_ecall` finished, you should save returned `error` in `a0`, and `value` in `a1`.
  105. #[repr(C)] // ensure that return value follows RISC-V SBI calling convention
  106. pub struct SbiRet {
  107. /// Error number
  108. pub error: usize,
  109. /// Result value
  110. pub value: usize,
  111. }
  112. const SBI_SUCCESS: usize = 0;
  113. const SBI_ERR_FAILED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-1));
  114. const SBI_ERR_NOT_SUPPORTED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-2));
  115. const SBI_ERR_INVALID_PARAM: usize = usize::from_ne_bytes(isize::to_ne_bytes(-3));
  116. // const SBI_ERR_DENIED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-4));
  117. const SBI_ERR_INVALID_ADDRESS: usize = usize::from_ne_bytes(isize::to_ne_bytes(-5));
  118. const SBI_ERR_ALREADY_AVAILABLE: usize = usize::from_ne_bytes(isize::to_ne_bytes(-6));
  119. const SBI_ERR_ALREADY_STARTED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-7));
  120. const SBI_ERR_ALREADY_STOPPED: usize = usize::from_ne_bytes(isize::to_ne_bytes(-8));
  121. impl SbiRet {
  122. /// Return success SBI state with given value.
  123. pub fn ok(value: usize) -> SbiRet {
  124. SbiRet {
  125. error: SBI_SUCCESS,
  126. value,
  127. }
  128. }
  129. /// The SBI call request failed for unknown reasons.
  130. pub fn failed() -> SbiRet {
  131. SbiRet {
  132. error: SBI_ERR_FAILED,
  133. value: 0,
  134. }
  135. }
  136. /// SBI call failed due to not supported by target ISA, operation type not supported,
  137. /// or target operation type not implemented on purpose.
  138. pub fn not_supported() -> SbiRet {
  139. SbiRet {
  140. error: SBI_ERR_NOT_SUPPORTED,
  141. value: 0,
  142. }
  143. }
  144. /// SBI call failed due to invalid hart mask parameter, invalid target hart id, invalid operation type
  145. /// or invalid resource index.
  146. pub fn invalid_param() -> SbiRet {
  147. SbiRet {
  148. error: SBI_ERR_INVALID_PARAM,
  149. value: 0,
  150. }
  151. }
  152. /// SBI call failed for invalid mask start address, not a valid physical address parameter,
  153. /// or the target address is prohibited by PMP to run in supervisor mode.
  154. pub fn invalid_address() -> SbiRet {
  155. SbiRet {
  156. error: SBI_ERR_INVALID_ADDRESS,
  157. value: 0,
  158. }
  159. }
  160. /// SBI call failed for the target resource is already available, e.g. the target hart is already
  161. /// started when caller still request it to start.
  162. pub fn already_available() -> SbiRet {
  163. SbiRet {
  164. error: SBI_ERR_ALREADY_AVAILABLE,
  165. value: 0,
  166. }
  167. }
  168. /// SBI call failed for the target resource is already started, e.g. target performance counter is started.
  169. pub fn already_started() -> SbiRet {
  170. SbiRet {
  171. error: SBI_ERR_ALREADY_STARTED,
  172. value: 0,
  173. }
  174. }
  175. /// SBI call failed for the target resource is already stopped, e.g. target performance counter is stopped.
  176. pub fn already_stopped() -> SbiRet {
  177. SbiRet {
  178. error: SBI_ERR_ALREADY_STOPPED,
  179. value: 0,
  180. }
  181. }
  182. pub(crate) fn legacy_ok(legacy_value: usize) -> SbiRet {
  183. SbiRet {
  184. error: legacy_value,
  185. value: 0,
  186. }
  187. }
  188. // only used for legacy where a0, a1 return value is not modified
  189. pub(crate) fn legacy_void(self, a0: usize, a1: usize) -> SbiRet {
  190. SbiRet {
  191. error: a0,
  192. value: a1,
  193. }
  194. }
  195. pub(crate) fn legacy_return(self, a1: usize) -> SbiRet {
  196. SbiRet {
  197. error: self.error,
  198. value: a1,
  199. }
  200. }
  201. }