brintos

brintos / linux-shallow public Read only

0
0
Text · 2.2 KiB · 9f0af4f Raw
75 lines · c
1// SPDX-License-Identifier: GPL-2.02// 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_H7#define _LINUX_NOSPEC_H8 9#include <linux/compiler.h>10#include <asm/barrier.h>11 12struct task_struct;13 14#ifndef barrier_nospec15# define barrier_nospec() do { } while (0)16#endif17 18/**19 * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise20 * @index: array element index21 * @size: number of elements in array22 *23 * When @index is out of bounds (@index >= @size), the sign bit will be24 * set.  Extend the sign bit to all bits and invert, giving a result of25 * zero for an out of bounds index, or ~0 if within bounds [0, @size).26 */27#ifndef array_index_mask_nospec28static inline unsigned long array_index_mask_nospec(unsigned long index,29						    unsigned long size)30{31	/*32	 * Always calculate and emit the mask even if the compiler33	 * thinks the mask is not needed. The compiler does not take34	 * into account the value of @index under speculation.35	 */36	OPTIMIZER_HIDE_VAR(index);37	return ~(long)(index | (size - 1UL - index)) >> (BITS_PER_LONG - 1);38}39#endif40 41/*42 * array_index_nospec - sanitize an array index after a bounds check43 *44 * For a code sequence like:45 *46 *     if (index < size) {47 *         index = array_index_nospec(index, size);48 *         val = array[index];49 *     }50 *51 * ...if the CPU speculates past the bounds check then52 * array_index_nospec() will clamp the index within the range of [0,53 * size).54 */55#define array_index_nospec(index, size)					\56({									\57	typeof(index) _i = (index);					\58	typeof(size) _s = (size);					\59	unsigned long _mask = array_index_mask_nospec(_i, _s);		\60									\61	BUILD_BUG_ON(sizeof(_i) > sizeof(long));			\62	BUILD_BUG_ON(sizeof(_s) > sizeof(long));			\63									\64	(typeof(_i)) (_i & _mask);					\65})66 67/* Speculation control prctl */68int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);69int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,70			     unsigned long ctrl);71/* Speculation control for seccomp enforced mitigation */72void arch_seccomp_spec_mitigate(struct task_struct *task);73 74#endif /* _LINUX_NOSPEC_H */75