xref: /linux/kernel/bpf/tnum.c (revision 0a91336e287ca2557fead5221d2c79e0effd034e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* tnum: tracked (or tristate) numbers
3  *
4  * A tnum tracks knowledge about the bits of a value.  Each bit can be either
5  * known (0 or 1), or unknown (x).  Arithmetic operations on tnums will
6  * propagate the unknown bits such that the tnum result represents all the
7  * possible results for possible values of the operands.
8  */
9 #include <linux/kernel.h>
10 #include <linux/tnum.h>
11 
12 #define TNUM(_v, _m)	(struct tnum){.value = _v, .mask = _m}
13 /* A completely unknown value */
14 const struct tnum tnum_unknown = { .value = 0, .mask = -1 };
15 
tnum_const(u64 value)16 struct tnum tnum_const(u64 value)
17 {
18 	return TNUM(value, 0);
19 }
20 
tnum_range(u64 min,u64 max)21 struct tnum tnum_range(u64 min, u64 max)
22 {
23 	u64 chi = min ^ max, delta;
24 	u8 bits = fls64(chi);
25 
26 	/* special case, needed because 1ULL << 64 is undefined */
27 	if (bits > 63)
28 		return tnum_unknown;
29 	/* e.g. if chi = 4, bits = 3, delta = (1<<3) - 1 = 7.
30 	 * if chi = 0, bits = 0, delta = (1<<0) - 1 = 0, so we return
31 	 *  constant min (since min == max).
32 	 */
33 	delta = (1ULL << bits) - 1;
34 	return TNUM(min & ~delta, delta);
35 }
36 
tnum_lshift(struct tnum a,u8 shift)37 struct tnum tnum_lshift(struct tnum a, u8 shift)
38 {
39 	return TNUM(a.value << shift, a.mask << shift);
40 }
41 
tnum_rshift(struct tnum a,u8 shift)42 struct tnum tnum_rshift(struct tnum a, u8 shift)
43 {
44 	return TNUM(a.value >> shift, a.mask >> shift);
45 }
46 
tnum_arshift(struct tnum a,u8 min_shift,u8 insn_bitness)47 struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness)
48 {
49 	/* if a.value is negative, arithmetic shifting by minimum shift
50 	 * will have larger negative offset compared to more shifting.
51 	 * If a.value is nonnegative, arithmetic shifting by minimum shift
52 	 * will have larger positive offset compare to more shifting.
53 	 */
54 	if (insn_bitness == 32)
55 		return TNUM((u32)(((s32)a.value) >> min_shift),
56 			    (u32)(((s32)a.mask)  >> min_shift));
57 	else
58 		return TNUM((s64)a.value >> min_shift,
59 			    (s64)a.mask  >> min_shift);
60 }
61 
tnum_add(struct tnum a,struct tnum b)62 struct tnum tnum_add(struct tnum a, struct tnum b)
63 {
64 	u64 sm, sv, sigma, chi, mu;
65 
66 	sm = a.mask + b.mask;
67 	sv = a.value + b.value;
68 	sigma = sm + sv;
69 	chi = sigma ^ sv;
70 	mu = chi | a.mask | b.mask;
71 	return TNUM(sv & ~mu, mu);
72 }
73 
tnum_sub(struct tnum a,struct tnum b)74 struct tnum tnum_sub(struct tnum a, struct tnum b)
75 {
76 	u64 dv, alpha, beta, chi, mu;
77 
78 	dv = a.value - b.value;
79 	alpha = dv + a.mask;
80 	beta = dv - b.mask;
81 	chi = alpha ^ beta;
82 	mu = chi | a.mask | b.mask;
83 	return TNUM(dv & ~mu, mu);
84 }
85 
tnum_neg(struct tnum a)86 struct tnum tnum_neg(struct tnum a)
87 {
88 	return tnum_sub(TNUM(0, 0), a);
89 }
90 
tnum_and(struct tnum a,struct tnum b)91 struct tnum tnum_and(struct tnum a, struct tnum b)
92 {
93 	u64 alpha, beta, v;
94 
95 	alpha = a.value | a.mask;
96 	beta = b.value | b.mask;
97 	v = a.value & b.value;
98 	return TNUM(v, alpha & beta & ~v);
99 }
100 
tnum_or(struct tnum a,struct tnum b)101 struct tnum tnum_or(struct tnum a, struct tnum b)
102 {
103 	u64 v, mu;
104 
105 	v = a.value | b.value;
106 	mu = a.mask | b.mask;
107 	return TNUM(v, mu & ~v);
108 }
109 
tnum_xor(struct tnum a,struct tnum b)110 struct tnum tnum_xor(struct tnum a, struct tnum b)
111 {
112 	u64 v, mu;
113 
114 	v = a.value ^ b.value;
115 	mu = a.mask | b.mask;
116 	return TNUM(v & ~mu, mu);
117 }
118 
119 /* Generate partial products by multiplying each bit in the multiplier (tnum a)
120  * with the multiplicand (tnum b), and add the partial products after
121  * appropriately bit-shifting them. Instead of directly performing tnum addition
122  * on the generated partial products, equivalenty, decompose each partial
123  * product into two tnums, consisting of the value-sum (acc_v) and the
124  * mask-sum (acc_m) and then perform tnum addition on them. The following paper
125  * explains the algorithm in more detail: https://arxiv.org/abs/2105.05398.
126  */
tnum_mul(struct tnum a,struct tnum b)127 struct tnum tnum_mul(struct tnum a, struct tnum b)
128 {
129 	u64 acc_v = a.value * b.value;
130 	struct tnum acc_m = TNUM(0, 0);
131 
132 	while (a.value || a.mask) {
133 		/* LSB of tnum a is a certain 1 */
134 		if (a.value & 1)
135 			acc_m = tnum_add(acc_m, TNUM(0, b.mask));
136 		/* LSB of tnum a is uncertain */
137 		else if (a.mask & 1)
138 			acc_m = tnum_add(acc_m, TNUM(0, b.value | b.mask));
139 		/* Note: no case for LSB is certain 0 */
140 		a = tnum_rshift(a, 1);
141 		b = tnum_lshift(b, 1);
142 	}
143 	return tnum_add(TNUM(acc_v, 0), acc_m);
144 }
145 
146 /* Note that if a and b disagree - i.e. one has a 'known 1' where the other has
147  * a 'known 0' - this will return a 'known 1' for that bit.
148  */
tnum_intersect(struct tnum a,struct tnum b)149 struct tnum tnum_intersect(struct tnum a, struct tnum b)
150 {
151 	u64 v, mu;
152 
153 	v = a.value | b.value;
154 	mu = a.mask & b.mask;
155 	return TNUM(v & ~mu, mu);
156 }
157 
tnum_cast(struct tnum a,u8 size)158 struct tnum tnum_cast(struct tnum a, u8 size)
159 {
160 	a.value &= (1ULL << (size * 8)) - 1;
161 	a.mask &= (1ULL << (size * 8)) - 1;
162 	return a;
163 }
164 
tnum_is_aligned(struct tnum a,u64 size)165 bool tnum_is_aligned(struct tnum a, u64 size)
166 {
167 	if (!size)
168 		return true;
169 	return !((a.value | a.mask) & (size - 1));
170 }
171 
tnum_in(struct tnum a,struct tnum b)172 bool tnum_in(struct tnum a, struct tnum b)
173 {
174 	if (b.mask & ~a.mask)
175 		return false;
176 	b.value &= ~a.mask;
177 	return a.value == b.value;
178 }
179 
tnum_sbin(char * str,size_t size,struct tnum a)180 int tnum_sbin(char *str, size_t size, struct tnum a)
181 {
182 	size_t n;
183 
184 	for (n = 64; n; n--) {
185 		if (n < size) {
186 			if (a.mask & 1)
187 				str[n - 1] = 'x';
188 			else if (a.value & 1)
189 				str[n - 1] = '1';
190 			else
191 				str[n - 1] = '0';
192 		}
193 		a.mask >>= 1;
194 		a.value >>= 1;
195 	}
196 	str[min(size - 1, (size_t)64)] = 0;
197 	return 64;
198 }
199 
tnum_subreg(struct tnum a)200 struct tnum tnum_subreg(struct tnum a)
201 {
202 	return tnum_cast(a, 4);
203 }
204 
tnum_clear_subreg(struct tnum a)205 struct tnum tnum_clear_subreg(struct tnum a)
206 {
207 	return tnum_lshift(tnum_rshift(a, 32), 32);
208 }
209 
tnum_with_subreg(struct tnum reg,struct tnum subreg)210 struct tnum tnum_with_subreg(struct tnum reg, struct tnum subreg)
211 {
212 	return tnum_or(tnum_clear_subreg(reg), tnum_subreg(subreg));
213 }
214 
tnum_const_subreg(struct tnum a,u32 value)215 struct tnum tnum_const_subreg(struct tnum a, u32 value)
216 {
217 	return tnum_with_subreg(a, tnum_const(value));
218 }
219