build-rename.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use std::cell::RefCell;
  2. use rustsbi::{HartMask, RustSBI};
  3. use sbi_spec::{
  4. binary::{Physical, SbiRet},
  5. dbcn::EID_DBCN,
  6. rfnc::EID_RFNC,
  7. spi::EID_SPI,
  8. time::EID_TIME,
  9. };
  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(10));
  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, _: rustsbi::HartMask) -> SbiRet {
  85. SbiRet::success(4)
  86. }
  87. fn remote_sfence_vma(&self, _: rustsbi::HartMask, _: usize, _: usize) -> SbiRet {
  88. SbiRet::success(5)
  89. }
  90. fn remote_sfence_vma_asid(&self, _: rustsbi::HartMask, _: usize, _: usize, _: usize) -> SbiRet {
  91. SbiRet::success(6)
  92. }
  93. }
  94. struct DummyEnvInfo;
  95. impl rustsbi::EnvInfo for DummyEnvInfo {
  96. fn mvendorid(&self) -> usize {
  97. unimplemented!()
  98. }
  99. fn marchid(&self) -> usize {
  100. unimplemented!()
  101. }
  102. fn mimpid(&self) -> usize {
  103. unimplemented!()
  104. }
  105. }
  106. struct RealEnvInfo;
  107. impl rustsbi::EnvInfo for RealEnvInfo {
  108. fn mvendorid(&self) -> usize {
  109. 7
  110. }
  111. fn marchid(&self) -> usize {
  112. 8
  113. }
  114. fn mimpid(&self) -> usize {
  115. 9
  116. }
  117. }
  118. struct MockClint {
  119. time: RefCell<u64>,
  120. }
  121. impl rustsbi::Timer for MockClint {
  122. fn set_timer(&self, stime_value: u64) {
  123. self.time.replace(stime_value);
  124. }
  125. }
  126. impl rustsbi::Ipi for MockClint {
  127. fn send_ipi(&self, _: HartMask) -> SbiRet {
  128. SbiRet::success(10)
  129. }
  130. }