asm.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //! Assembly instructions
  2. macro_rules! instruction {
  3. ($(#[$attr:meta])*, unsafe $fnname:ident, $asm:expr) => (
  4. $(#[$attr])*
  5. #[inline]
  6. pub unsafe fn $fnname() {
  7. match () {
  8. #[cfg(riscv)]
  9. () => core::arch::asm!($asm),
  10. #[cfg(not(riscv))]
  11. () => unimplemented!(),
  12. }
  13. }
  14. );
  15. ($(#[$attr:meta])*, $fnname:ident, $asm:expr) => (
  16. $(#[$attr])*
  17. #[inline]
  18. pub fn $fnname() {
  19. match () {
  20. #[cfg(riscv)]
  21. () => unsafe { core::arch::asm!($asm) },
  22. #[cfg(not(riscv))]
  23. () => unimplemented!(),
  24. }
  25. }
  26. );
  27. }
  28. instruction!(
  29. /// `nop` instruction wrapper
  30. ///
  31. /// The `NOP` instruction does not change any architecturally visible state, except for
  32. /// advancing the pc and incrementing any applicable performance counters.
  33. ///
  34. /// This function generates a no-operation; it's useful to prevent delay loops from being
  35. /// optimized away.
  36. , nop, "nop");
  37. instruction!(
  38. /// `EBREAK` instruction wrapper
  39. ///
  40. /// Generates a breakpoint exception.
  41. , unsafe ebreak, "ebreak");
  42. instruction!(
  43. /// `WFI` instruction wrapper
  44. ///
  45. /// Provides a hint to the implementation that the current hart can be stalled until an interrupt might need servicing.
  46. /// The WFI instruction is just a hint, and a legal implementation is to implement WFI as a NOP.
  47. , unsafe wfi, "wfi");
  48. instruction!(
  49. /// `SFENCE.VMA` instruction wrapper (all address spaces and page table levels)
  50. ///
  51. /// Synchronizes updates to in-memory memory-management data structures with current execution.
  52. /// Instruction execution causes implicit reads and writes to these data structures; however, these implicit references
  53. /// are ordinarily not ordered with respect to loads and stores in the instruction stream.
  54. /// Executing an `SFENCE.VMA` instruction guarantees that any stores in the instruction stream prior to the
  55. /// `SFENCE.VMA` are ordered before all implicit references subsequent to the `SFENCE.VMA`.
  56. , unsafe sfence_vma_all, "sfence.vma");
  57. instruction!(
  58. /// `FENCE` instruction wrapper
  59. ///
  60. /// The FENCE instruction is used to order device I/O and memory accesses as viewed by other RISC-V
  61. /// harts and external devices or coprocessors. Any combination of device input (I), device output
  62. /// (O), memory reads (R), and memory writes (W) may be ordered with respect to any combination
  63. /// of the same. Informally, no other RISC-V hart or external device can observe any operation in the
  64. /// successor set following a FENCE before any operation in the predecessor set preceding the FENCE.
  65. /// Chapter 17 provides a precise description of the RISC-V memory consistency model.
  66. ///
  67. /// The FENCE instruction also orders memory reads and writes made by the hart as observed by
  68. /// memory reads and writes made by an external device. However, FENCE does not order observations
  69. /// of events made by an external device using any other signaling mechanism.
  70. , unsafe fence, "fence");
  71. instruction!(
  72. /// `FENCE.I` instruction wrapper
  73. ///
  74. /// Used to synchronize the instruction and data streams. RISC-V does not guarantee that
  75. /// stores to instruction memory will be made visible to instruction fetches on a
  76. /// RISC-V hart until that hart executes a FENCE.I instruction.
  77. ///
  78. /// A FENCE.I instruction ensures that a subsequent instruction fetch on a RISC-V hart
  79. /// will see any previous data stores already visible to the same RISC-V hart.
  80. /// FENCE.I does not ensure that other RISC-V harts’ instruction fetches will observe the
  81. /// local hart’s stores in a multiprocessor system. To make a store to instruction memory
  82. /// visible to all RISC-V harts, the writing hart also has to execute a data FENCE before
  83. /// requesting that all remote RISC-V harts execute a FENCE.I.
  84. ///
  85. /// The unused fields in the FENCE.I instruction, imm\[11:0\], rs1, and rd, are reserved for
  86. /// finer-grain fences in future extensions. For forward compatibility, base
  87. /// implementations shall ignore these fields, and standard software shall zero these fields.
  88. , unsafe fence_i, "fence.i");
  89. /// `SFENCE.VMA` instruction wrapper
  90. ///
  91. /// Synchronizes updates to in-memory memory-management data structures with current execution.
  92. /// Instruction execution causes implicit reads and writes to these data structures; however, these implicit references
  93. /// are ordinarily not ordered with respect to loads and stores in the instruction stream.
  94. /// Executing an `SFENCE.VMA` instruction guarantees that any stores in the instruction stream prior to the
  95. /// `SFENCE.VMA` are ordered before all implicit references subsequent to the `SFENCE.VMA`.
  96. #[inline]
  97. #[allow(unused_variables)]
  98. pub unsafe fn sfence_vma(asid: usize, addr: usize) {
  99. match () {
  100. #[cfg(riscv)]
  101. () => core::arch::asm!("sfence.vma {0}, {1}", in(reg) addr, in(reg) asid),
  102. #[cfg(not(riscv))]
  103. () => unimplemented!(),
  104. }
  105. }
  106. /// Blocks the program for *at least* `cycles` CPU cycles.
  107. ///
  108. /// This is implemented in assembly so its execution time is independent of the optimization
  109. /// level, however it is dependent on the specific architecture and core configuration.
  110. ///
  111. /// NOTE that the delay can take much longer if interrupts are serviced during its execution
  112. /// and the execution time may vary with other factors. This delay is mainly useful for simple
  113. /// timer-less initialization of peripherals if and only if accurate timing is not essential. In
  114. /// any other case please use a more accurate method to produce a delay.
  115. #[inline]
  116. #[allow(unused_variables)]
  117. pub fn delay(cycles: u32) {
  118. match () {
  119. #[cfg(riscv)]
  120. () => unsafe {
  121. let real_cyc = 1 + cycles / 2;
  122. core::arch::asm!(
  123. "1:",
  124. "addi {0}, {0}, -1",
  125. "bne {0}, zero, 1b",
  126. inout(reg) real_cyc => _,
  127. options(nomem, nostack),
  128. )
  129. },
  130. #[cfg(not(riscv))]
  131. () => unimplemented!(),
  132. }
  133. }