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