asm.rs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //! Assembly instructions
  2. macro_rules! instruction {
  3. ($(#[$attr:meta])*, $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. }
  16. instruction!(
  17. /// `nop` instruction wrapper
  18. ///
  19. /// Generates a no-operation. Useful to prevent delay loops from being optimized away.
  20. , nop, "nop");
  21. instruction!(
  22. /// `EBREAK` instruction wrapper
  23. ///
  24. /// Generates a breakpoint exception.
  25. , ebreak, "ebreak");
  26. instruction!(
  27. /// `WFI` instruction wrapper
  28. ///
  29. /// Provides a hint to the implementation that the current hart can be stalled until an interrupt might need servicing.
  30. /// The WFI instruction is just a hint, and a legal implementation is to implement WFI as a NOP.
  31. , wfi, "wfi");
  32. instruction!(
  33. /// `SFENCE.VMA` instruction wrapper (all address spaces and page table levels)
  34. ///
  35. /// Synchronizes updates to in-memory memory-management data structures with current execution.
  36. /// Instruction execution causes implicit reads and writes to these data structures; however, these implicit references
  37. /// are ordinarily not ordered with respect to loads and stores in the instruction stream.
  38. /// Executing an `SFENCE.VMA` instruction guarantees that any stores in the instruction stream prior to the
  39. /// `SFENCE.VMA` are ordered before all implicit references subsequent to the `SFENCE.VMA`.
  40. , sfence_vma_all, "sfence.vma");
  41. /// `SFENCE.VMA` instruction wrapper
  42. ///
  43. /// Synchronizes updates to in-memory memory-management data structures with current execution.
  44. /// Instruction execution causes implicit reads and writes to these data structures; however, these implicit references
  45. /// are ordinarily not ordered with respect to loads and stores in the instruction stream.
  46. /// Executing an `SFENCE.VMA` instruction guarantees that any stores in the instruction stream prior to the
  47. /// `SFENCE.VMA` are ordered before all implicit references subsequent to the `SFENCE.VMA`.
  48. #[inline]
  49. #[allow(unused_variables)]
  50. pub unsafe fn sfence_vma(asid: usize, addr: usize) {
  51. match () {
  52. #[cfg(riscv)]
  53. () => core::arch::asm!("sfence.vma {0}, {1}", in(reg) addr, in(reg) asid),
  54. #[cfg(not(riscv))]
  55. () => unimplemented!(),
  56. }
  57. }
  58. /// Blocks the program for *at least* `cycles` CPU cycles.
  59. ///
  60. /// This is implemented in assembly so its execution time is independent of the optimization
  61. /// level, however it is dependent on the specific architecture and core configuration.
  62. ///
  63. /// NOTE that the delay can take much longer if interrupts are serviced during its execution
  64. /// and the execution time may vary with other factors. This delay is mainly useful for simple
  65. /// timer-less initialization of peripherals if and only if accurate timing is not essential. In
  66. /// any other case please use a more accurate method to produce a delay.
  67. #[inline]
  68. #[allow(unused_variables)]
  69. pub unsafe fn delay(cycles: u32) {
  70. match () {
  71. #[cfg(riscv)]
  72. () => {
  73. let real_cyc = 1 + cycles / 2;
  74. core::arch::asm!(
  75. "1:",
  76. "addi {0}, {0}, -1",
  77. "bne {0}, zero, 1b",
  78. inout(reg) real_cyc => _,
  79. options(nomem, nostack),
  80. )
  81. }
  82. #[cfg(not(riscv))]
  83. () => unimplemented!(),
  84. }
  85. }