1f3804203SDan Williams // SPDX-License-Identifier: GPL-2.0
2f3804203SDan Williams // Copyright(c) 2018 Linus Torvalds. All rights reserved.
3f3804203SDan Williams // Copyright(c) 2018 Alexei Starovoitov. All rights reserved.
4f3804203SDan Williams // Copyright(c) 2018 Intel Corporation. All rights reserved.
5f3804203SDan Williams
6f3804203SDan Williams #ifndef _LINUX_NOSPEC_H
7f3804203SDan Williams #define _LINUX_NOSPEC_H
8002dff36SWill Deacon
9002dff36SWill Deacon #include <linux/compiler.h>
10eb6174f6SDan Williams #include <asm/barrier.h>
11f3804203SDan Williams
127bbf1373SKees Cook struct task_struct;
137bbf1373SKees Cook
14*74e19ef0SDave Hansen #ifndef barrier_nospec
15*74e19ef0SDave Hansen # define barrier_nospec() do { } while (0)
16*74e19ef0SDave Hansen #endif
17*74e19ef0SDave Hansen
18f3804203SDan Williams /**
19f3804203SDan Williams * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise
20f3804203SDan Williams * @index: array element index
21f3804203SDan Williams * @size: number of elements in array
22f3804203SDan Williams *
23f3804203SDan Williams * When @index is out of bounds (@index >= @size), the sign bit will be
24f3804203SDan Williams * set. Extend the sign bit to all bits and invert, giving a result of
25f3804203SDan Williams * zero for an out of bounds index, or ~0 if within bounds [0, @size).
26f3804203SDan Williams */
27f3804203SDan Williams #ifndef array_index_mask_nospec
array_index_mask_nospec(unsigned long index,unsigned long size)28f3804203SDan Williams static inline unsigned long array_index_mask_nospec(unsigned long index,
29f3804203SDan Williams unsigned long size)
30f3804203SDan Williams {
31f3804203SDan Williams /*
328fa80c50SWill Deacon * Always calculate and emit the mask even if the compiler
338fa80c50SWill Deacon * thinks the mask is not needed. The compiler does not take
348fa80c50SWill Deacon * into account the value of @index under speculation.
358fa80c50SWill Deacon */
368fa80c50SWill Deacon OPTIMIZER_HIDE_VAR(index);
378fa80c50SWill Deacon return ~(long)(index | (size - 1UL - index)) >> (BITS_PER_LONG - 1);
388fa80c50SWill Deacon }
398fa80c50SWill Deacon #endif
408fa80c50SWill Deacon
418fa80c50SWill Deacon /*
42f3804203SDan Williams * array_index_nospec - sanitize an array index after a bounds check
43f3804203SDan Williams *
44f3804203SDan Williams * For a code sequence like:
45f3804203SDan Williams *
46f3804203SDan Williams * if (index < size) {
47f3804203SDan Williams * index = array_index_nospec(index, size);
48f3804203SDan Williams * val = array[index];
49f3804203SDan Williams * }
50f3804203SDan Williams *
51f3804203SDan Williams * ...if the CPU speculates past the bounds check then
52f3804203SDan Williams * array_index_nospec() will clamp the index within the range of [0,
53f3804203SDan Williams * size).
54f3804203SDan Williams */
55f3804203SDan Williams #define array_index_nospec(index, size) \
56f3804203SDan Williams ({ \
57f3804203SDan Williams typeof(index) _i = (index); \
58f3804203SDan Williams typeof(size) _s = (size); \
591d91c1d2SDan Williams unsigned long _mask = array_index_mask_nospec(_i, _s); \
60f3804203SDan Williams \
61f3804203SDan Williams BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \
62f3804203SDan Williams BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \
63f3804203SDan Williams \
64b98c6a16SRasmus Villemoes (typeof(_i)) (_i & _mask); \
65f3804203SDan Williams })
66b617cfc8SThomas Gleixner
67b617cfc8SThomas Gleixner /* Speculation control prctl */
687bbf1373SKees Cook int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);
697bbf1373SKees Cook int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
707bbf1373SKees Cook unsigned long ctrl);
718bf37d8cSThomas Gleixner /* Speculation control for seccomp enforced mitigation */
728bf37d8cSThomas Gleixner void arch_seccomp_spec_mitigate(struct task_struct *task);
73b617cfc8SThomas Gleixner
74f3804203SDan Williams #endif /* _LINUX_NOSPEC_H */
75