4
0

dynamic-priority.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. use rustsbi::{HartMask, RustSBI, SbiRet};
  2. use sbi_spec::{rfnc::EID_RFNC, spi::EID_SPI, time::EID_TIME};
  3. #[derive(RustSBI)]
  4. #[rustsbi(dynamic)]
  5. struct MultipleFences {
  6. #[rustsbi(fence)]
  7. fence_one: Option<FenceOne>,
  8. #[rustsbi(fence)]
  9. fence_two: Option<FenceTwo>,
  10. env_info: DummyEnvInfo,
  11. }
  12. #[test]
  13. fn priority_multiple_fences() {
  14. let sbi = MultipleFences {
  15. fence_one: Some(FenceOne),
  16. fence_two: None,
  17. env_info: DummyEnvInfo,
  18. };
  19. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x0, [0; 6]), SbiRet::success(1));
  20. let sbi = MultipleFences {
  21. fence_one: None,
  22. fence_two: Some(FenceTwo),
  23. env_info: DummyEnvInfo,
  24. };
  25. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x0, [0; 6]), SbiRet::success(4));
  26. let sbi = MultipleFences {
  27. fence_one: Some(FenceOne),
  28. fence_two: Some(FenceTwo),
  29. env_info: DummyEnvInfo,
  30. };
  31. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x0, [0; 6]), SbiRet::success(1));
  32. let sbi = MultipleFences {
  33. fence_one: None,
  34. fence_two: None,
  35. env_info: DummyEnvInfo,
  36. };
  37. assert_eq!(
  38. sbi.handle_ecall(EID_RFNC, 0x0, [0; 6]),
  39. SbiRet::not_supported()
  40. );
  41. }
  42. #[derive(RustSBI)]
  43. #[rustsbi(dynamic)]
  44. struct AssignMultiple {
  45. #[rustsbi(timer)]
  46. aclint_1: Option<AclintOne>,
  47. #[rustsbi(ipi, timer)]
  48. aclint_2: Option<AclintTwo>,
  49. #[rustsbi(ipi)]
  50. aclint_3: Option<AclintThree>,
  51. env_info: DummyEnvInfo,
  52. }
  53. #[test]
  54. fn priority_assign_multiple() {
  55. let sbi = AssignMultiple {
  56. aclint_1: Some(AclintOne::default()),
  57. aclint_2: Some(AclintTwo::default()),
  58. aclint_3: Some(AclintThree::default()),
  59. env_info: DummyEnvInfo,
  60. };
  61. let sbi_ret = sbi.handle_ecall(EID_TIME, 0x0, [1, 0, 0, 0, 0, 0]);
  62. assert_eq!(sbi_ret, SbiRet::success(0));
  63. assert_eq!(sbi.aclint_1.as_ref().unwrap().stimer_value(), 1);
  64. assert_eq!(sbi.aclint_2.as_ref().unwrap().stimer_value(), 0);
  65. let sbi_ret = sbi.handle_ecall(EID_SPI, 0x0, [0; 6]);
  66. assert_eq!(sbi_ret, SbiRet::success(7));
  67. let sbi = AssignMultiple {
  68. aclint_1: None,
  69. aclint_2: Some(AclintTwo::default()),
  70. aclint_3: Some(AclintThree::default()),
  71. env_info: DummyEnvInfo,
  72. };
  73. let sbi_ret = sbi.handle_ecall(EID_TIME, 0x0, [2, 0, 0, 0, 0, 0]);
  74. assert_eq!(sbi_ret, SbiRet::success(0));
  75. assert_eq!(sbi.aclint_2.as_ref().unwrap().stimer_value(), 2);
  76. let sbi_ret = sbi.handle_ecall(EID_SPI, 0x0, [0; 6]);
  77. assert_eq!(sbi_ret, SbiRet::success(7));
  78. let sbi = AssignMultiple {
  79. aclint_1: None,
  80. aclint_2: None,
  81. aclint_3: Some(AclintThree::default()),
  82. env_info: DummyEnvInfo,
  83. };
  84. let sbi_ret = sbi.handle_ecall(EID_TIME, 0x0, [3, 0, 0, 0, 0, 0]);
  85. assert_eq!(sbi_ret, SbiRet::not_supported());
  86. let sbi_ret = sbi.handle_ecall(EID_SPI, 0x0, [0; 6]);
  87. assert_eq!(sbi_ret, SbiRet::success(8));
  88. let sbi = AssignMultiple {
  89. aclint_1: None,
  90. aclint_2: None,
  91. aclint_3: None,
  92. env_info: DummyEnvInfo,
  93. };
  94. let sbi_ret = sbi.handle_ecall(EID_TIME, 0x0, [4, 0, 0, 0, 0, 0]);
  95. assert_eq!(sbi_ret, SbiRet::not_supported());
  96. let sbi_ret = sbi.handle_ecall(EID_SPI, 0x0, [0; 6]);
  97. assert_eq!(sbi_ret, SbiRet::not_supported());
  98. }
  99. struct FenceOne;
  100. impl rustsbi::Fence for FenceOne {
  101. fn remote_fence_i(&self, _: rustsbi::HartMask) -> SbiRet {
  102. SbiRet::success(1)
  103. }
  104. fn remote_sfence_vma(&self, _: rustsbi::HartMask, _: usize, _: usize) -> SbiRet {
  105. SbiRet::success(2)
  106. }
  107. fn remote_sfence_vma_asid(&self, _: rustsbi::HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  108. SbiRet::success(3)
  109. }
  110. }
  111. struct FenceTwo;
  112. impl rustsbi::Fence for FenceTwo {
  113. fn remote_fence_i(&self, _: rustsbi::HartMask) -> SbiRet {
  114. SbiRet::success(4)
  115. }
  116. fn remote_sfence_vma(&self, _: rustsbi::HartMask, _: usize, _: usize) -> SbiRet {
  117. SbiRet::success(5)
  118. }
  119. fn remote_sfence_vma_asid(&self, _: rustsbi::HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  120. SbiRet::success(6)
  121. }
  122. }
  123. #[derive(Default)]
  124. struct AclintOne {
  125. inner: core::cell::RefCell<u64>,
  126. }
  127. impl AclintOne {
  128. fn stimer_value(&self) -> u64 {
  129. *self.inner.borrow()
  130. }
  131. }
  132. impl rustsbi::Timer for AclintOne {
  133. fn set_timer(&self, stime_value: u64) {
  134. let _ = self.inner.replace(stime_value);
  135. }
  136. }
  137. #[derive(Default)]
  138. struct AclintTwo {
  139. inner: core::cell::RefCell<u64>,
  140. }
  141. impl AclintTwo {
  142. fn stimer_value(&self) -> u64 {
  143. *self.inner.borrow()
  144. }
  145. }
  146. impl rustsbi::Timer for AclintTwo {
  147. fn set_timer(&self, stime_value: u64) {
  148. let _ = self.inner.replace(stime_value);
  149. }
  150. }
  151. impl rustsbi::Ipi for AclintTwo {
  152. fn send_ipi(&self, _: HartMask) -> SbiRet {
  153. SbiRet::success(7)
  154. }
  155. }
  156. #[derive(Default)]
  157. struct AclintThree;
  158. impl rustsbi::Ipi for AclintThree {
  159. fn send_ipi(&self, _: HartMask) -> SbiRet {
  160. SbiRet::success(8)
  161. }
  162. }
  163. struct DummyEnvInfo;
  164. impl rustsbi::EnvInfo for DummyEnvInfo {
  165. fn mvendorid(&self) -> usize {
  166. 36
  167. }
  168. fn marchid(&self) -> usize {
  169. 37
  170. }
  171. fn mimpid(&self) -> usize {
  172. 38
  173. }
  174. }