| 1 | /* tnum: tracked (or tristate) numbers | 
|---|
| 2 | * | 
|---|
| 3 | * A tnum tracks knowledge about the bits of a value.  Each bit can be either | 
|---|
| 4 | * known (0 or 1), or unknown (x).  Arithmetic operations on tnums will | 
|---|
| 5 | * propagate the unknown bits such that the tnum result represents all the | 
|---|
| 6 | * possible results for possible values of the operands. | 
|---|
| 7 | */ | 
|---|
| 8 |  | 
|---|
| 9 | #ifndef _LINUX_TNUM_H | 
|---|
| 10 | #define _LINUX_TNUM_H | 
|---|
| 11 |  | 
|---|
| 12 | #include <linux/types.h> | 
|---|
| 13 |  | 
|---|
| 14 | struct tnum { | 
|---|
| 15 | u64 value; | 
|---|
| 16 | u64 mask; | 
|---|
| 17 | }; | 
|---|
| 18 |  | 
|---|
| 19 | /* Constructors */ | 
|---|
| 20 | /* Represent a known constant as a tnum. */ | 
|---|
| 21 | struct tnum tnum_const(u64 value); | 
|---|
| 22 | /* A completely unknown value */ | 
|---|
| 23 | extern const struct tnum tnum_unknown; | 
|---|
| 24 | /* An unknown value that is a superset of @min <= value <= @max. | 
|---|
| 25 | * | 
|---|
| 26 | * Could include values outside the range of [@min, @max]. | 
|---|
| 27 | * For example tnum_range(0, 2) is represented by {0, 1, 2, *3*}, | 
|---|
| 28 | * rather than the intended set of {0, 1, 2}. | 
|---|
| 29 | */ | 
|---|
| 30 | struct tnum tnum_range(u64 min, u64 max); | 
|---|
| 31 |  | 
|---|
| 32 | /* Arithmetic and logical ops */ | 
|---|
| 33 | /* Shift a tnum left (by a fixed shift) */ | 
|---|
| 34 | struct tnum tnum_lshift(struct tnum a, u8 shift); | 
|---|
| 35 | /* Shift (rsh) a tnum right (by a fixed shift) */ | 
|---|
| 36 | struct tnum tnum_rshift(struct tnum a, u8 shift); | 
|---|
| 37 | /* Shift (arsh) a tnum right (by a fixed min_shift) */ | 
|---|
| 38 | struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness); | 
|---|
| 39 | /* Add two tnums, return @a + @b */ | 
|---|
| 40 | struct tnum tnum_add(struct tnum a, struct tnum b); | 
|---|
| 41 | /* Subtract two tnums, return @a - @b */ | 
|---|
| 42 | struct tnum tnum_sub(struct tnum a, struct tnum b); | 
|---|
| 43 | /* Neg of a tnum, return  0 - @a */ | 
|---|
| 44 | struct tnum tnum_neg(struct tnum a); | 
|---|
| 45 | /* Bitwise-AND, return @a & @b */ | 
|---|
| 46 | struct tnum tnum_and(struct tnum a, struct tnum b); | 
|---|
| 47 | /* Bitwise-OR, return @a | @b */ | 
|---|
| 48 | struct tnum tnum_or(struct tnum a, struct tnum b); | 
|---|
| 49 | /* Bitwise-XOR, return @a ^ @b */ | 
|---|
| 50 | struct tnum tnum_xor(struct tnum a, struct tnum b); | 
|---|
| 51 | /* Multiply two tnums, return @a * @b */ | 
|---|
| 52 | struct tnum tnum_mul(struct tnum a, struct tnum b); | 
|---|
| 53 |  | 
|---|
| 54 | /* Return true if the known bits of both tnums have the same value */ | 
|---|
| 55 | bool tnum_overlap(struct tnum a, struct tnum b); | 
|---|
| 56 |  | 
|---|
| 57 | /* Return a tnum representing numbers satisfying both @a and @b */ | 
|---|
| 58 | struct tnum tnum_intersect(struct tnum a, struct tnum b); | 
|---|
| 59 |  | 
|---|
| 60 | /* Returns a tnum representing numbers satisfying either @a or @b */ | 
|---|
| 61 | struct tnum tnum_union(struct tnum t1, struct tnum t2); | 
|---|
| 62 |  | 
|---|
| 63 | /* Return @a with all but the lowest @size bytes cleared */ | 
|---|
| 64 | struct tnum tnum_cast(struct tnum a, u8 size); | 
|---|
| 65 |  | 
|---|
| 66 | /* Returns true if @a is a known constant */ | 
|---|
| 67 | static inline bool tnum_is_const(struct tnum a) | 
|---|
| 68 | { | 
|---|
| 69 | return !a.mask; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | /* Returns true if @a == tnum_const(@b) */ | 
|---|
| 73 | static inline bool tnum_equals_const(struct tnum a, u64 b) | 
|---|
| 74 | { | 
|---|
| 75 | return tnum_is_const(a) && a.value == b; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /* Returns true if @a is completely unknown */ | 
|---|
| 79 | static inline bool tnum_is_unknown(struct tnum a) | 
|---|
| 80 | { | 
|---|
| 81 | return !~a.mask; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | /* Returns true if @a is known to be a multiple of @size. | 
|---|
| 85 | * @size must be a power of two. | 
|---|
| 86 | */ | 
|---|
| 87 | bool tnum_is_aligned(struct tnum a, u64 size); | 
|---|
| 88 |  | 
|---|
| 89 | /* Returns true if @b represents a subset of @a. | 
|---|
| 90 | * | 
|---|
| 91 | * Note that using tnum_range() as @a requires extra cautions as tnum_in() may | 
|---|
| 92 | * return true unexpectedly due to tnum limited ability to represent tight | 
|---|
| 93 | * range, e.g. | 
|---|
| 94 | * | 
|---|
| 95 | *   tnum_in(tnum_range(0, 2), tnum_const(3)) == true | 
|---|
| 96 | * | 
|---|
| 97 | * As a rule of thumb, if @a is explicitly coded rather than coming from | 
|---|
| 98 | * reg->var_off, it should be in form of tnum_const(), tnum_range(0, 2**n - 1), | 
|---|
| 99 | * or tnum_range(2**n, 2**(n+1) - 1). | 
|---|
| 100 | */ | 
|---|
| 101 | bool tnum_in(struct tnum a, struct tnum b); | 
|---|
| 102 |  | 
|---|
| 103 | /* Formatting functions.  These have snprintf-like semantics: they will write | 
|---|
| 104 | * up to @size bytes (including the terminating NUL byte), and return the number | 
|---|
| 105 | * of bytes (excluding the terminating NUL) which would have been written had | 
|---|
| 106 | * sufficient space been available.  (Thus tnum_sbin always returns 64.) | 
|---|
| 107 | */ | 
|---|
| 108 | /* Format a tnum as a pair of hex numbers (value; mask) */ | 
|---|
| 109 | int tnum_strn(char *str, size_t size, struct tnum a); | 
|---|
| 110 | /* Format a tnum as tristate binary expansion */ | 
|---|
| 111 | int tnum_sbin(char *str, size_t size, struct tnum a); | 
|---|
| 112 |  | 
|---|
| 113 | /* Returns the 32-bit subreg */ | 
|---|
| 114 | struct tnum tnum_subreg(struct tnum a); | 
|---|
| 115 | /* Returns the tnum with the lower 32-bit subreg cleared */ | 
|---|
| 116 | struct tnum tnum_clear_subreg(struct tnum a); | 
|---|
| 117 | /* Returns the tnum with the lower 32-bit subreg in *reg* set to the lower | 
|---|
| 118 | * 32-bit subreg in *subreg* | 
|---|
| 119 | */ | 
|---|
| 120 | struct tnum tnum_with_subreg(struct tnum reg, struct tnum subreg); | 
|---|
| 121 | /* Returns the tnum with the lower 32-bit subreg set to value */ | 
|---|
| 122 | struct tnum tnum_const_subreg(struct tnum a, u32 value); | 
|---|
| 123 | /* Returns true if 32-bit subreg @a is a known constant*/ | 
|---|
| 124 | static inline bool tnum_subreg_is_const(struct tnum a) | 
|---|
| 125 | { | 
|---|
| 126 | return !(tnum_subreg(a)).mask; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | #endif /* _LINUX_TNUM_H */ | 
|---|
| 130 |  | 
|---|