nospec.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright(c) 2018 Linus Torvalds. All rights reserved.
  3. // Copyright(c) 2018 Alexei Starovoitov. All rights reserved.
  4. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  5. #ifndef _LINUX_NOSPEC_H
  6. #define _LINUX_NOSPEC_H
  7. #include <linux/compiler.h>
  8. #include <asm/barrier.h>
  9. struct task_struct;
  10. #ifndef barrier_nospec
  11. # define barrier_nospec() do { } while (0)
  12. #endif
  13. /**
  14. * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise
  15. * @index: array element index
  16. * @size: number of elements in array
  17. *
  18. * When @index is out of bounds (@index >= @size), the sign bit will be
  19. * set. Extend the sign bit to all bits and invert, giving a result of
  20. * zero for an out of bounds index, or ~0 if within bounds [0, @size).
  21. */
  22. #ifndef array_index_mask_nospec
  23. static inline unsigned long array_index_mask_nospec(unsigned long index,
  24. unsigned long size)
  25. {
  26. /*
  27. * Always calculate and emit the mask even if the compiler
  28. * thinks the mask is not needed. The compiler does not take
  29. * into account the value of @index under speculation.
  30. */
  31. OPTIMIZER_HIDE_VAR(index);
  32. return ~(long)(index | (size - 1UL - index)) >> (BITS_PER_LONG - 1);
  33. }
  34. #endif
  35. /*
  36. * array_index_nospec - sanitize an array index after a bounds check
  37. *
  38. * For a code sequence like:
  39. *
  40. * if (index < size) {
  41. * index = array_index_nospec(index, size);
  42. * val = array[index];
  43. * }
  44. *
  45. * ...if the CPU speculates past the bounds check then
  46. * array_index_nospec() will clamp the index within the range of [0,
  47. * size).
  48. */
  49. #define array_index_nospec(index, size) \
  50. ({ \
  51. typeof(index) _i = (index); \
  52. typeof(size) _s = (size); \
  53. unsigned long _mask = array_index_mask_nospec(_i, _s); \
  54. \
  55. BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \
  56. BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \
  57. \
  58. (typeof(_i)) (_i & _mask); \
  59. })
  60. /* Speculation control prctl */
  61. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);
  62. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  63. unsigned long ctrl);
  64. /* Speculation control for seccomp enforced mitigation */
  65. void arch_seccomp_spec_mitigate(struct task_struct *task);
  66. #endif /* _LINUX_NOSPEC_H */