build-rename.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use rustsbi::RustSBI;
  2. use sbi_spec::{
  3. binary::{HartMask, Physical, SbiRet},
  4. dbcn::EID_DBCN,
  5. rfnc::EID_RFNC,
  6. spi::EID_SPI,
  7. time::EID_TIME,
  8. };
  9. use std::cell::RefCell;
  10. #[derive(RustSBI)]
  11. struct AssignOne {
  12. console: DummyConsole,
  13. #[rustsbi(fence)]
  14. some_other_name: DummyFence,
  15. info: DummyEnvInfo,
  16. }
  17. #[derive(RustSBI)]
  18. struct AssignMultiple {
  19. #[rustsbi(ipi, timer)]
  20. clint: MockClint,
  21. info: DummyEnvInfo,
  22. }
  23. #[test]
  24. fn rustsbi_assign_one() {
  25. let sbi = AssignOne {
  26. console: DummyConsole,
  27. some_other_name: DummyFence,
  28. info: DummyEnvInfo,
  29. };
  30. // Both extension return correct values.
  31. assert_eq!(sbi.handle_ecall(EID_DBCN, 0x0, [0; 6]), SbiRet::success(1));
  32. assert_eq!(sbi.handle_ecall(EID_DBCN, 0x1, [0; 6]), SbiRet::success(2));
  33. assert_eq!(sbi.handle_ecall(EID_DBCN, 0x2, [0; 6]), SbiRet::success(3));
  34. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x0, [0; 6]), SbiRet::success(4));
  35. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x1, [0; 6]), SbiRet::success(5));
  36. assert_eq!(sbi.handle_ecall(EID_RFNC, 0x2, [0; 6]), SbiRet::success(6));
  37. // Both extension exists.
  38. assert_ne!(
  39. sbi.handle_ecall(0x10, 0x3, [EID_DBCN, 0, 0, 0, 0, 0]),
  40. SbiRet::success(0)
  41. );
  42. assert_ne!(
  43. sbi.handle_ecall(0x10, 0x3, [EID_RFNC, 0, 0, 0, 0, 0]),
  44. SbiRet::success(0)
  45. );
  46. }
  47. #[test]
  48. fn rustsbi_assign_multiple() {
  49. let sbi = AssignMultiple {
  50. clint: MockClint {
  51. time: RefCell::new(0),
  52. },
  53. info: DummyEnvInfo,
  54. };
  55. assert_eq!(sbi.handle_ecall(EID_SPI, 0x0, [0; 6]), SbiRet::success(14));
  56. #[cfg(target_pointer_width = "64")]
  57. sbi.handle_ecall(EID_TIME, 0x0, [0x1122334455667788, 0, 0, 0, 0, 0]);
  58. #[cfg(target_pointer_width = "32")]
  59. sbi.handle_ecall(EID_TIME, 0x0, [0x11223344, 0x55667788, 0, 0, 0, 0]);
  60. assert_eq!(sbi.clint.time.take(), 0x1122334455667788);
  61. assert_ne!(
  62. sbi.handle_ecall(0x10, 0x3, [EID_SPI, 0, 0, 0, 0, 0]),
  63. SbiRet::success(0)
  64. );
  65. assert_ne!(
  66. sbi.handle_ecall(0x10, 0x3, [EID_TIME, 0, 0, 0, 0, 0]),
  67. SbiRet::success(0)
  68. );
  69. }
  70. struct DummyConsole;
  71. impl rustsbi::Console for DummyConsole {
  72. fn write(&self, _: Physical<&[u8]>) -> SbiRet {
  73. SbiRet::success(1)
  74. }
  75. fn read(&self, _: Physical<&mut [u8]>) -> SbiRet {
  76. SbiRet::success(2)
  77. }
  78. fn write_byte(&self, _: u8) -> SbiRet {
  79. SbiRet::success(3)
  80. }
  81. }
  82. struct DummyFence;
  83. impl rustsbi::Fence for DummyFence {
  84. fn remote_fence_i(&self, _: HartMask) -> SbiRet {
  85. SbiRet::success(4)
  86. }
  87. fn remote_sfence_vma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  88. SbiRet::success(5)
  89. }
  90. fn remote_sfence_vma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  91. SbiRet::success(6)
  92. }
  93. fn remote_hfence_gvma_vmid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  94. SbiRet::success(7)
  95. }
  96. fn remote_hfence_gvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  97. SbiRet::success(8)
  98. }
  99. fn remote_hfence_vvma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  100. SbiRet::success(9)
  101. }
  102. fn remote_hfence_vvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
  103. SbiRet::success(10)
  104. }
  105. }
  106. struct DummyEnvInfo;
  107. impl rustsbi::EnvInfo for DummyEnvInfo {
  108. fn mvendorid(&self) -> usize {
  109. unimplemented!()
  110. }
  111. fn marchid(&self) -> usize {
  112. unimplemented!()
  113. }
  114. fn mimpid(&self) -> usize {
  115. unimplemented!()
  116. }
  117. }
  118. #[allow(unused)] // FIXME: hot fix, use it on unit test in the future.
  119. struct RealEnvInfo;
  120. impl rustsbi::EnvInfo for RealEnvInfo {
  121. fn mvendorid(&self) -> usize {
  122. 11
  123. }
  124. fn marchid(&self) -> usize {
  125. 12
  126. }
  127. fn mimpid(&self) -> usize {
  128. 13
  129. }
  130. }
  131. struct MockClint {
  132. time: RefCell<u64>,
  133. }
  134. impl rustsbi::Timer for MockClint {
  135. fn set_timer(&self, stime_value: u64) {
  136. self.time.replace(stime_value);
  137. }
  138. }
  139. impl rustsbi::Ipi for MockClint {
  140. fn send_ipi(&self, _: HartMask) -> SbiRet {
  141. SbiRet::success(14)
  142. }
  143. }