1 /*
2  * lib/bitmap.c
3  * Helper functions for bitmap.h.
4  *
5  * This source code is licensed under the GNU General Public License,
6  * Version 2.  See the file COPYING for more details.
7  */
8 #include <xen/types.h>
9 #include <xen/errno.h>
10 #include <xen/bitmap.h>
11 #include <xen/bitops.h>
12 #include <asm/byteorder.h>
13 
14 /*
15  * bitmaps provide an array of bits, implemented using an an
16  * array of unsigned longs.  The number of valid bits in a
17  * given bitmap does _not_ need to be an exact multiple of
18  * BITS_PER_LONG.
19  *
20  * The possible unused bits in the last, partially used word
21  * of a bitmap are 'don't care'.  The implementation makes
22  * no particular effort to keep them zero.  It ensures that
23  * their value will not affect the results of any operation.
24  * The bitmap operations that return Boolean (bitmap_empty,
25  * for example) or scalar (bitmap_weight, for example) results
26  * carefully filter out these unused bits from impacting their
27  * results.
28  *
29  * These operations actually hold to a slightly stronger rule:
30  * if you don't input any bitmaps to these ops that have some
31  * unused bits set, then they won't output any set unused bits
32  * in output bitmaps.
33  *
34  * The byte ordering of bitmaps is more natural on little
35  * endian architectures.  See the big-endian headers
36  * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
37  * for the best explanations of this ordering.
38  */
39 
40 /*
41  * If a bitmap has a number of bits which is not a multiple of 8 then
42  * the last few bits of the last byte of the bitmap can be
43  * unexpectedly set which can confuse consumers (e.g. in the tools)
44  * who also round up their loops to 8 bits. Ensure we clear those left
45  * over bits so as to prevent surprises.
46  */
clamp_last_byte(uint8_t * bp,unsigned int nbits)47 static void clamp_last_byte(uint8_t *bp, unsigned int nbits)
48 {
49 	unsigned int remainder = nbits % 8;
50 
51 	if (remainder)
52 		bp[nbits/8] &= (1U << remainder) - 1;
53 }
54 
__bitmap_empty(const unsigned long * bitmap,int bits)55 int __bitmap_empty(const unsigned long *bitmap, int bits)
56 {
57 	int k, lim = bits/BITS_PER_LONG;
58 	for (k = 0; k < lim; ++k)
59 		if (bitmap[k])
60 			return 0;
61 
62 	if (bits % BITS_PER_LONG)
63 		if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
64 			return 0;
65 
66 	return 1;
67 }
68 EXPORT_SYMBOL(__bitmap_empty);
69 
__bitmap_full(const unsigned long * bitmap,int bits)70 int __bitmap_full(const unsigned long *bitmap, int bits)
71 {
72 	int k, lim = bits/BITS_PER_LONG;
73 	for (k = 0; k < lim; ++k)
74 		if (~bitmap[k])
75 			return 0;
76 
77 	if (bits % BITS_PER_LONG)
78 		if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
79 			return 0;
80 
81 	return 1;
82 }
83 EXPORT_SYMBOL(__bitmap_full);
84 
__bitmap_equal(const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)85 int __bitmap_equal(const unsigned long *bitmap1,
86 		const unsigned long *bitmap2, int bits)
87 {
88 	int k, lim = bits/BITS_PER_LONG;
89 	for (k = 0; k < lim; ++k)
90 		if (bitmap1[k] != bitmap2[k])
91 			return 0;
92 
93 	if (bits % BITS_PER_LONG)
94 		if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
95 			return 0;
96 
97 	return 1;
98 }
99 EXPORT_SYMBOL(__bitmap_equal);
100 
__bitmap_complement(unsigned long * dst,const unsigned long * src,int bits)101 void __bitmap_complement(unsigned long *dst, const unsigned long *src, int bits)
102 {
103 	int k, lim = bits/BITS_PER_LONG;
104 	for (k = 0; k < lim; ++k)
105 		dst[k] = ~src[k];
106 
107 	if (bits % BITS_PER_LONG)
108 		dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
109 }
110 EXPORT_SYMBOL(__bitmap_complement);
111 
112 /*
113  * __bitmap_shift_right - logical right shift of the bits in a bitmap
114  *   @dst - destination bitmap
115  *   @src - source bitmap
116  *   @nbits - shift by this many bits
117  *   @bits - bitmap size, in bits
118  *
119  * Shifting right (dividing) means moving bits in the MS -> LS bit
120  * direction.  Zeros are fed into the vacated MS positions and the
121  * LS bits shifted off the bottom are lost.
122  */
__bitmap_shift_right(unsigned long * dst,const unsigned long * src,int shift,int bits)123 void __bitmap_shift_right(unsigned long *dst,
124 			const unsigned long *src, int shift, int bits)
125 {
126 	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
127 	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
128 	unsigned long mask = (1UL << left) - 1;
129 	for (k = 0; off + k < lim; ++k) {
130 		unsigned long upper, lower;
131 
132 		/*
133 		 * If shift is not word aligned, take lower rem bits of
134 		 * word above and make them the top rem bits of result.
135 		 */
136 		if (!rem || off + k + 1 >= lim)
137 			upper = 0;
138 		else {
139 			upper = src[off + k + 1];
140 			if (off + k + 1 == lim - 1 && left)
141 				upper &= mask;
142 		}
143 		lower = src[off + k];
144 		if (left && off + k == lim - 1)
145 			lower &= mask;
146 		dst[k] = rem
147 		         ? (upper << (BITS_PER_LONG - rem)) | (lower >> rem)
148 		         : lower;
149 		if (left && k == lim - 1)
150 			dst[k] &= mask;
151 	}
152 	if (off)
153 		memset(&dst[lim - off], 0, off*sizeof(unsigned long));
154 }
155 EXPORT_SYMBOL(__bitmap_shift_right);
156 
157 
158 /*
159  * __bitmap_shift_left - logical left shift of the bits in a bitmap
160  *   @dst - destination bitmap
161  *   @src - source bitmap
162  *   @nbits - shift by this many bits
163  *   @bits - bitmap size, in bits
164  *
165  * Shifting left (multiplying) means moving bits in the LS -> MS
166  * direction.  Zeros are fed into the vacated LS bit positions
167  * and those MS bits shifted off the top are lost.
168  */
169 
__bitmap_shift_left(unsigned long * dst,const unsigned long * src,int shift,int bits)170 void __bitmap_shift_left(unsigned long *dst,
171 			const unsigned long *src, int shift, int bits)
172 {
173 	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
174 	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
175 	for (k = lim - off - 1; k >= 0; --k) {
176 		unsigned long upper, lower;
177 
178 		/*
179 		 * If shift is not word aligned, take upper rem bits of
180 		 * word below and make them the bottom rem bits of result.
181 		 */
182 		if (rem && k > 0)
183 			lower = src[k - 1];
184 		else
185 			lower = 0;
186 		upper = src[k];
187 		if (left && k == lim - 1)
188 			upper &= (1UL << left) - 1;
189 		dst[k + off] = rem ? (lower >> (BITS_PER_LONG - rem))
190 		                      | (upper << rem)
191 		                   : upper;
192 		if (left && k + off == lim - 1)
193 			dst[k + off] &= (1UL << left) - 1;
194 	}
195 	if (off)
196 		memset(dst, 0, off*sizeof(unsigned long));
197 }
198 EXPORT_SYMBOL(__bitmap_shift_left);
199 
__bitmap_and(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)200 void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
201 				const unsigned long *bitmap2, int bits)
202 {
203 	int k;
204 	int nr = BITS_TO_LONGS(bits);
205 
206 	for (k = 0; k < nr; k++)
207 		dst[k] = bitmap1[k] & bitmap2[k];
208 }
209 EXPORT_SYMBOL(__bitmap_and);
210 
__bitmap_or(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)211 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
212 				const unsigned long *bitmap2, int bits)
213 {
214 	int k;
215 	int nr = BITS_TO_LONGS(bits);
216 
217 	for (k = 0; k < nr; k++)
218 		dst[k] = bitmap1[k] | bitmap2[k];
219 }
220 EXPORT_SYMBOL(__bitmap_or);
221 
__bitmap_xor(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)222 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
223 				const unsigned long *bitmap2, int bits)
224 {
225 	int k;
226 	int nr = BITS_TO_LONGS(bits);
227 
228 	for (k = 0; k < nr; k++)
229 		dst[k] = bitmap1[k] ^ bitmap2[k];
230 }
231 EXPORT_SYMBOL(__bitmap_xor);
232 
__bitmap_andnot(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)233 void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
234 				const unsigned long *bitmap2, int bits)
235 {
236 	int k;
237 	int nr = BITS_TO_LONGS(bits);
238 
239 	for (k = 0; k < nr; k++)
240 		dst[k] = bitmap1[k] & ~bitmap2[k];
241 }
242 EXPORT_SYMBOL(__bitmap_andnot);
243 
__bitmap_intersects(const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)244 int __bitmap_intersects(const unsigned long *bitmap1,
245 				const unsigned long *bitmap2, int bits)
246 {
247 	int k, lim = bits/BITS_PER_LONG;
248 	for (k = 0; k < lim; ++k)
249 		if (bitmap1[k] & bitmap2[k])
250 			return 1;
251 
252 	if (bits % BITS_PER_LONG)
253 		if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
254 			return 1;
255 	return 0;
256 }
257 EXPORT_SYMBOL(__bitmap_intersects);
258 
__bitmap_subset(const unsigned long * bitmap1,const unsigned long * bitmap2,int bits)259 int __bitmap_subset(const unsigned long *bitmap1,
260 				const unsigned long *bitmap2, int bits)
261 {
262 	int k, lim = bits/BITS_PER_LONG;
263 	for (k = 0; k < lim; ++k)
264 		if (bitmap1[k] & ~bitmap2[k])
265 			return 0;
266 
267 	if (bits % BITS_PER_LONG)
268 		if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
269 			return 0;
270 	return 1;
271 }
272 EXPORT_SYMBOL(__bitmap_subset);
273 
274 #if BITS_PER_LONG == 32
__bitmap_weight(const unsigned long * bitmap,int bits)275 int __bitmap_weight(const unsigned long *bitmap, int bits)
276 {
277 	int k, w = 0, lim = bits/BITS_PER_LONG;
278 
279 	for (k = 0; k < lim; k++)
280 		w += hweight32(bitmap[k]);
281 
282 	if (bits % BITS_PER_LONG)
283 		w += hweight32(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
284 
285 	return w;
286 }
287 #else
__bitmap_weight(const unsigned long * bitmap,int bits)288 int __bitmap_weight(const unsigned long *bitmap, int bits)
289 {
290 	int k, w = 0, lim = bits/BITS_PER_LONG;
291 
292 	for (k = 0; k < lim; k++)
293 		w += hweight64(bitmap[k]);
294 
295 	if (bits % BITS_PER_LONG)
296 		w += hweight64(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
297 
298 	return w;
299 }
300 #endif
301 EXPORT_SYMBOL(__bitmap_weight);
302 
303 /*
304  * Bitmap printing & parsing functions: first version by Bill Irwin,
305  * second version by Paul Jackson, third by Joe Korty.
306  */
307 
308 #define CHUNKSZ				32
309 #define nbits_to_hold_value(val)	fls(val)
310 #define roundup_power2(val,modulus)	(((val) + (modulus) - 1) & ~((modulus) - 1))
311 #define unhex(c)			(isdigit(c) ? (c - '0') : (toupper(c) - 'A' + 10))
312 #define BASEDEC 10		/* fancier cpuset lists input in decimal */
313 
314 /**
315  * bitmap_scnprintf - convert bitmap to an ASCII hex string.
316  * @buf: byte buffer into which string is placed
317  * @buflen: reserved size of @buf, in bytes
318  * @maskp: pointer to bitmap to convert
319  * @nmaskbits: size of bitmap, in bits
320  *
321  * Exactly @nmaskbits bits are displayed.  Hex digits are grouped into
322  * comma-separated sets of eight digits per set.
323  */
bitmap_scnprintf(char * buf,unsigned int buflen,const unsigned long * maskp,int nmaskbits)324 int bitmap_scnprintf(char *buf, unsigned int buflen,
325 	const unsigned long *maskp, int nmaskbits)
326 {
327 	int i, word, bit, len = 0;
328 	unsigned long val;
329 	const char *sep = "";
330 	int chunksz;
331 	u32 chunkmask;
332 
333 	chunksz = nmaskbits & (CHUNKSZ - 1);
334 	if (chunksz == 0)
335 		chunksz = CHUNKSZ;
336 
337 	i = roundup_power2(nmaskbits, CHUNKSZ) - CHUNKSZ;
338 	for (; i >= 0; i -= CHUNKSZ) {
339 		chunkmask = ((1ULL << chunksz) - 1);
340 		word = i / BITS_PER_LONG;
341 		bit = i % BITS_PER_LONG;
342 		val = (maskp[word] >> bit) & chunkmask;
343 		len += scnprintf(buf+len, buflen-len, "%s%0*lx", sep,
344 			(chunksz+3)/4, val);
345 		chunksz = CHUNKSZ;
346 		sep = ",";
347 	}
348 	return len;
349 }
350 EXPORT_SYMBOL(bitmap_scnprintf);
351 
352 /*
353  * bscnl_emit(buf, buflen, rbot, rtop, bp)
354  *
355  * Helper routine for bitmap_scnlistprintf().  Write decimal number
356  * or range to buf, suppressing output past buf+buflen, with optional
357  * comma-prefix.  Return len of what would be written to buf, if it
358  * all fit.
359  */
bscnl_emit(char * buf,int buflen,int rbot,int rtop,int len)360 static inline int bscnl_emit(char *buf, int buflen, int rbot, int rtop, int len)
361 {
362 	if (len > 0)
363 		len += scnprintf(buf + len, buflen - len, ",");
364 	if (rbot == rtop)
365 		len += scnprintf(buf + len, buflen - len, "%d", rbot);
366 	else
367 		len += scnprintf(buf + len, buflen - len, "%d-%d", rbot, rtop);
368 	return len;
369 }
370 
371 /**
372  * bitmap_scnlistprintf - convert bitmap to list format ASCII string
373  * @buf: byte buffer into which string is placed
374  * @buflen: reserved size of @buf, in bytes
375  * @maskp: pointer to bitmap to convert
376  * @nmaskbits: size of bitmap, in bits
377  *
378  * Output format is a comma-separated list of decimal numbers and
379  * ranges.  Consecutively set bits are shown as two hyphen-separated
380  * decimal numbers, the smallest and largest bit numbers set in
381  * the range.  Output format is compatible with the format
382  * accepted as input by bitmap_parselist().
383  *
384  * The return value is the number of characters which were output,
385  * excluding the trailing '\0'.
386  */
bitmap_scnlistprintf(char * buf,unsigned int buflen,const unsigned long * maskp,int nmaskbits)387 int bitmap_scnlistprintf(char *buf, unsigned int buflen,
388 	const unsigned long *maskp, int nmaskbits)
389 {
390 	int len = 0;
391 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
392 	int cur, rbot, rtop;
393 
394 	rbot = cur = find_first_bit(maskp, nmaskbits);
395 	while (cur < nmaskbits) {
396 		rtop = cur;
397 		cur = find_next_bit(maskp, nmaskbits, cur+1);
398 		if (cur >= nmaskbits || cur > rtop + 1) {
399 			len = bscnl_emit(buf, buflen, rbot, rtop, len);
400 			rbot = cur;
401 		}
402 	}
403 	if (!len && buflen)
404 		*buf = 0;
405 	return len;
406 }
407 EXPORT_SYMBOL(bitmap_scnlistprintf);
408 
409 /**
410  *	bitmap_find_free_region - find a contiguous aligned mem region
411  *	@bitmap: an array of unsigned longs corresponding to the bitmap
412  *	@bits: number of bits in the bitmap
413  *	@order: region size to find (size is actually 1<<order)
414  *
415  * This is used to allocate a memory region from a bitmap.  The idea is
416  * that the region has to be 1<<order sized and 1<<order aligned (this
417  * makes the search algorithm much faster).
418  *
419  * The region is marked as set bits in the bitmap if a free one is
420  * found.
421  *
422  * Returns either beginning of region or negative error
423  */
bitmap_find_free_region(unsigned long * bitmap,int bits,int order)424 int bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
425 {
426 	unsigned long mask;
427 	int pages = 1 << order;
428 	int i;
429 
430 	if(pages > BITS_PER_LONG)
431 		return -EINVAL;
432 
433 	/* make a mask of the order */
434 	mask = (1ul << (pages - 1));
435 	mask += mask - 1;
436 
437 	/* run up the bitmap pages bits at a time */
438 	for (i = 0; i < bits; i += pages) {
439 		int index = i/BITS_PER_LONG;
440 		int offset = i - (index * BITS_PER_LONG);
441 		if((bitmap[index] & (mask << offset)) == 0) {
442 			/* set region in bimap */
443 			bitmap[index] |= (mask << offset);
444 			return i;
445 		}
446 	}
447 	return -ENOMEM;
448 }
449 EXPORT_SYMBOL(bitmap_find_free_region);
450 
451 /**
452  *	bitmap_release_region - release allocated bitmap region
453  *	@bitmap: a pointer to the bitmap
454  *	@pos: the beginning of the region
455  *	@order: the order of the bits to release (number is 1<<order)
456  *
457  * This is the complement to __bitmap_find_free_region and releases
458  * the found region (by clearing it in the bitmap).
459  */
bitmap_release_region(unsigned long * bitmap,int pos,int order)460 void bitmap_release_region(unsigned long *bitmap, int pos, int order)
461 {
462 	int pages = 1 << order;
463 	unsigned long mask = (1ul << (pages - 1));
464 	int index = pos/BITS_PER_LONG;
465 	int offset = pos - (index * BITS_PER_LONG);
466 	mask += mask - 1;
467 	bitmap[index] &= ~(mask << offset);
468 }
469 EXPORT_SYMBOL(bitmap_release_region);
470 
bitmap_allocate_region(unsigned long * bitmap,int pos,int order)471 int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
472 {
473 	int pages = 1 << order;
474 	unsigned long mask = (1ul << (pages - 1));
475 	int index = pos/BITS_PER_LONG;
476 	int offset = pos - (index * BITS_PER_LONG);
477 
478 	/* We don't do regions of pages > BITS_PER_LONG.  The
479 	 * algorithm would be a simple look for multiple zeros in the
480 	 * array, but there's no driver today that needs this.  If you
481 	 * trip this BUG(), you get to code it... */
482 	BUG_ON(pages > BITS_PER_LONG);
483 	mask += mask - 1;
484 	if (bitmap[index] & (mask << offset))
485 		return -EBUSY;
486 	bitmap[index] |= (mask << offset);
487 	return 0;
488 }
489 EXPORT_SYMBOL(bitmap_allocate_region);
490 
491 #ifdef __BIG_ENDIAN
492 
bitmap_long_to_byte(uint8_t * bp,const unsigned long * lp,int nbits)493 void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
494 {
495 	unsigned long l;
496 	int i, j, b;
497 
498 	for (i = 0, b = 0; nbits > 0; i++, b += sizeof(l)) {
499 		l = lp[i];
500 		for (j = 0; (j < sizeof(l)) && (nbits > 0); j++) {
501 			bp[b+j] = l;
502 			l >>= 8;
503 			nbits -= 8;
504 		}
505 	}
506 	clamp_last_byte(bp, nbits);
507 }
508 
bitmap_byte_to_long(unsigned long * lp,const uint8_t * bp,int nbits)509 void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
510 {
511 	unsigned long l;
512 	int i, j, b;
513 
514 	for (i = 0, b = 0; nbits > 0; i++, b += sizeof(l)) {
515 		l = 0;
516 		for (j = 0; (j < sizeof(l)) && (nbits > 0); j++) {
517 			l |= (unsigned long)bp[b+j] << (j*8);
518 			nbits -= 8;
519 		}
520 		lp[i] = l;
521 	}
522 }
523 
524 #elif defined(__LITTLE_ENDIAN)
525 
bitmap_long_to_byte(uint8_t * bp,const unsigned long * lp,int nbits)526 void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
527 {
528 	memcpy(bp, lp, (nbits+7)/8);
529 	clamp_last_byte(bp, nbits);
530 }
531 
bitmap_byte_to_long(unsigned long * lp,const uint8_t * bp,int nbits)532 void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
533 {
534 	/* We may need to pad the final longword with zeroes. */
535 	if (nbits & (BITS_PER_LONG-1))
536 		lp[BITS_TO_LONGS(nbits)-1] = 0;
537 	memcpy(lp, bp, (nbits+7)/8);
538 }
539 
540 #endif
541