spectre.h 1.0 KB

1234567891011121314151617181920212223242526
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* (Extracted and transcluded from <sHT/nospec.h> */
  4. /* "cmp %1,%2; sbb %0,%0": mask <- (mask - mask) - (pos < length).
  5. (unsigned, so pos, length > SSIZE_MAX is allowed).
  6. Alternatively, one could do a compare, setcc, and decrement, but
  7. that's an instruction longer. */
  8. #define _sHT_index_mask(maskp, pos, length) \
  9. __asm__("cmp %1,%2; sbb %0,%0" : "=&r" (*(maskp)) : "rm" (length), "r" (pos) : "cc")
  10. /* This sequence is what Intel tells us to do, and what
  11. Linux can do. It also has some effect upon memory ordering.
  12. TODO: check its correctness. Linux sometimes does "mfence"
  13. instead, why? Something to do with AMD / Intel differences.
  14. volatile "memory" is for paranoia, to avoid reordering or
  15. or elimination (Linux does this). Although in my tests (GCC 6.3.0,
  16. GCC 8.3.0, Clang 6.0), even without any of these nothing happens
  17. ... */
  18. #define _sHT_speculation_barrier() \
  19. __asm__ volatile("lfence" : : : "memory")