build-full.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. use rustsbi::RustSBI;
  2. use sbi_spec::{
  3. binary::{HartMask, Physical, SbiRet, SharedPtr},
  4. nacl::shmem_size::NATIVE,
  5. };
  6. #[derive(RustSBI)]
  7. struct FullyImplemented {
  8. console: DummyConsole,
  9. cppc: DummyCppc,
  10. hsm: DummyHsm,
  11. ipi: DummyIpi,
  12. nacl: DummyNacl,
  13. pmu: DummyPmu,
  14. reset: DummyReset,
  15. fence: DummyFence,
  16. sta: DummySta,
  17. susp: DummySusp,
  18. timer: DummyTimer,
  19. info: DummyEnvInfo,
  20. }
  21. #[derive(RustSBI)]
  22. struct AlternateName {
  23. dbcn: DummyConsole,
  24. cppc: DummyCppc,
  25. hsm: DummyHsm,
  26. ipi: DummyIpi,
  27. nacl: DummyNacl,
  28. pmu: DummyPmu,
  29. srst: DummyReset,
  30. rfnc: DummyFence,
  31. sta: DummySta,
  32. susp: DummySusp,
  33. time: DummyTimer,
  34. info: DummyEnvInfo,
  35. }
  36. #[derive(RustSBI)]
  37. struct TupleStruct(
  38. #[rustsbi(dbcn)] DummyConsole,
  39. #[rustsbi(cppc)] DummyCppc,
  40. #[rustsbi(hsm)] DummyHsm,
  41. #[rustsbi(ipi)] DummyIpi,
  42. #[rustsbi(nacl)] DummyNacl,
  43. #[rustsbi(pmu)] DummyPmu,
  44. #[rustsbi(srst)] DummyReset,
  45. #[rustsbi(rfnc)] DummyFence,
  46. #[rustsbi(sta)] DummySta,
  47. #[rustsbi(susp)] DummySusp,
  48. #[rustsbi(time)] DummyTimer,
  49. #[rustsbi(info)] DummyEnvInfo,
  50. );
  51. #[cfg(feature = "machine")]
  52. #[derive(RustSBI)]
  53. struct UnitStruct;
  54. #[test]
  55. fn rustsbi_impl_id() {
  56. let sbi = FullyImplemented {
  57. console: DummyConsole,
  58. cppc: DummyCppc,
  59. hsm: DummyHsm,
  60. ipi: DummyIpi,
  61. nacl: DummyNacl,
  62. pmu: DummyPmu,
  63. reset: DummyReset,
  64. fence: DummyFence,
  65. sta: DummySta,
  66. susp: DummySusp,
  67. timer: DummyTimer,
  68. info: DummyEnvInfo,
  69. };
  70. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  71. assert_eq!(sbi.handle_ecall(0x535441, 0, [0; 6]), SbiRet::success(33));
  72. assert_eq!(sbi.handle_ecall(0x53555350, 0, [0; 6]), SbiRet::success(34));
  73. // assert_eq!(sbi.handle_ecall(0x54494D45, 0, [0; 6]), SbiRet::success(35));
  74. assert_eq!(sbi.handle_ecall(0x10, 4, [0; 6]), SbiRet::success(36));
  75. assert_eq!(sbi.handle_ecall(0x10, 5, [0; 6]), SbiRet::success(37));
  76. assert_eq!(sbi.handle_ecall(0x10, 6, [0; 6]), SbiRet::success(38));
  77. let sbi = AlternateName {
  78. dbcn: DummyConsole,
  79. cppc: DummyCppc,
  80. hsm: DummyHsm,
  81. ipi: DummyIpi,
  82. nacl: DummyNacl,
  83. pmu: DummyPmu,
  84. srst: DummyReset,
  85. rfnc: DummyFence,
  86. sta: DummySta,
  87. susp: DummySusp,
  88. time: DummyTimer,
  89. info: DummyEnvInfo,
  90. };
  91. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  92. let sbi = TupleStruct(
  93. DummyConsole,
  94. DummyCppc,
  95. DummyHsm,
  96. DummyIpi,
  97. DummyNacl,
  98. DummyPmu,
  99. DummyReset,
  100. DummyFence,
  101. DummySta,
  102. DummySusp,
  103. DummyTimer,
  104. DummyEnvInfo,
  105. );
  106. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  107. }
  108. #[cfg(feature = "machine")]
  109. #[test]
  110. fn unit_struct() {
  111. let sbi = UnitStruct;
  112. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  113. }
  114. #[test]
  115. fn extension_impl() {
  116. let sbi = FullyImplemented {
  117. console: DummyConsole,
  118. cppc: DummyCppc,
  119. hsm: DummyHsm,
  120. ipi: DummyIpi,
  121. nacl: DummyNacl,
  122. pmu: DummyPmu,
  123. reset: DummyReset,
  124. fence: DummyFence,
  125. sta: DummySta,
  126. susp: DummySusp,
  127. timer: DummyTimer,
  128. info: DummyEnvInfo,
  129. };
  130. assert_eq!(
  131. sbi.handle_ecall(0x4442434E, 0x0, [0; 6]).error,
  132. -1isize as _
  133. );
  134. }
  135. struct DummyConsole;
  136. impl rustsbi::Console for DummyConsole {
  137. fn write(&self, _: Physical<&[u8]>) -> SbiRet {
  138. // special return value for test cases
  139. SbiRet::failed()
  140. }
  141. fn read(&self, _: Physical<&mut [u8]>) -> SbiRet {
  142. unimplemented!()
  143. }
  144. fn write_byte(&self, _: u8) -> SbiRet {
  145. unimplemented!()
  146. }
  147. }
  148. struct DummyCppc;
  149. impl rustsbi::Cppc for DummyCppc {
  150. fn probe(&self, _: u32) -> SbiRet {
  151. unimplemented!()
  152. }
  153. fn read(&self, _: u32) -> SbiRet {
  154. unimplemented!()
  155. }
  156. fn read_hi(&self, _: u32) -> SbiRet {
  157. unimplemented!()
  158. }
  159. fn write(&self, _: u32, _: u64) -> SbiRet {
  160. unimplemented!()
  161. }
  162. }
  163. struct DummyHsm;
  164. impl rustsbi::Hsm for DummyHsm {
  165. fn hart_start(&self, _: usize, _: usize, _: usize) -> SbiRet {
  166. unimplemented!()
  167. }
  168. fn hart_stop(&self) -> SbiRet {
  169. unimplemented!()
  170. }
  171. fn hart_get_status(&self, _: usize) -> SbiRet {
  172. unimplemented!()
  173. }
  174. fn hart_suspend(&self, _: u32, _: usize, _: usize) -> SbiRet {
  175. unimplemented!()
  176. }
  177. }
  178. struct DummyIpi;
  179. impl rustsbi::Ipi for DummyIpi {
  180. fn send_ipi(&self, _: HartMask) -> SbiRet {
  181. unimplemented!()
  182. }
  183. }
  184. struct DummyNacl;
  185. impl rustsbi::Nacl for DummyNacl {
  186. fn probe_feature(&self, _: u32) -> SbiRet {
  187. unimplemented!()
  188. }
  189. fn set_shmem(&self, _: SharedPtr<[u8; NATIVE]>, _: usize) -> SbiRet {
  190. unimplemented!()
  191. }
  192. fn sync_csr(&self, _: usize) -> SbiRet {
  193. unimplemented!()
  194. }
  195. fn sync_hfence(&self, _: usize) -> SbiRet {
  196. unimplemented!()
  197. }
  198. fn sync_sret(&self) -> SbiRet {
  199. unimplemented!()
  200. }
  201. }
  202. struct DummyPmu;
  203. impl rustsbi::Pmu for DummyPmu {
  204. fn num_counters(&self) -> usize {
  205. unimplemented!()
  206. }
  207. fn counter_get_info(&self, _: usize) -> SbiRet {
  208. unimplemented!()
  209. }
  210. fn counter_config_matching(&self, _: usize, _: usize, _: usize, _: usize, _: u64) -> SbiRet {
  211. unimplemented!()
  212. }
  213. fn counter_start(&self, _: usize, _: usize, _: usize, _: u64) -> SbiRet {
  214. unimplemented!()
  215. }
  216. fn counter_stop(&self, _: usize, _: usize, _: usize) -> SbiRet {
  217. unimplemented!()
  218. }
  219. fn counter_fw_read(&self, _: usize) -> SbiRet {
  220. unimplemented!()
  221. }
  222. fn counter_fw_read_hi(&self, _: usize) -> SbiRet {
  223. unimplemented!()
  224. }
  225. }
  226. struct DummyReset;
  227. impl rustsbi::Reset for DummyReset {
  228. fn system_reset(&self, _: u32, _: u32) -> SbiRet {
  229. unimplemented!()
  230. }
  231. }
  232. struct DummyFence;
  233. impl rustsbi::Fence for DummyFence {
  234. fn remote_fence_i(&self, _: HartMask) -> SbiRet {
  235. unimplemented!()
  236. }
  237. fn remote_sfence_vma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  238. unimplemented!()
  239. }
  240. fn remote_sfence_vma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  241. unimplemented!()
  242. }
  243. fn remote_hfence_gvma_vmid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  244. unimplemented!()
  245. }
  246. fn remote_hfence_gvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  247. unimplemented!()
  248. }
  249. fn remote_hfence_vvma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  250. unimplemented!()
  251. }
  252. fn remote_hfence_vvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  253. unimplemented!()
  254. }
  255. }
  256. struct DummySta;
  257. impl rustsbi::Sta for DummySta {
  258. fn set_shmem(&self, _: SharedPtr<[u8; 64]>, _: usize) -> SbiRet {
  259. SbiRet::success(33)
  260. }
  261. }
  262. struct DummySusp;
  263. impl rustsbi::Susp for DummySusp {
  264. fn system_suspend(&self, _: u32, _: usize, _: usize) -> SbiRet {
  265. SbiRet::success(34)
  266. }
  267. }
  268. struct DummyTimer;
  269. impl rustsbi::Timer for DummyTimer {
  270. fn set_timer(&self, _: u64) {
  271. todo!()
  272. }
  273. }
  274. struct DummyEnvInfo;
  275. impl rustsbi::EnvInfo for DummyEnvInfo {
  276. fn mvendorid(&self) -> usize {
  277. 36
  278. }
  279. fn marchid(&self) -> usize {
  280. 37
  281. }
  282. fn mimpid(&self) -> usize {
  283. 38
  284. }
  285. }