| 1 | /* |
| 2 | * Bitmap Module |
| 3 | * |
| 4 | * Stolen from linux/src/lib/bitmap.c |
| 5 | * |
| 6 | * Copyright (C) 2010 Corentin Chary |
| 7 | * |
| 8 | * This source code is licensed under the GNU General Public License, |
| 9 | * Version 2. |
| 10 | */ |
| 11 | |
| 12 | #include "qemu/osdep.h" |
| 13 | #include "qemu/bitops.h" |
| 14 | #include "qemu/bitmap.h" |
| 15 | #include "qemu/bswap.h" |
| 16 | #include "qemu/atomic.h" |
| 17 | |
| 18 | /* |
| 19 | * bitmaps provide an array of bits, implemented using an |
| 20 | * array of unsigned longs. The number of valid bits in a |
| 21 | * given bitmap does _not_ need to be an exact multiple of |
| 22 | * BITS_PER_LONG. |
| 23 | * |
| 24 | * The possible unused bits in the last, partially used word |
| 25 | * of a bitmap are 'don't care'. The implementation makes |
| 26 | * no particular effort to keep them zero. It ensures that |
| 27 | * their value will not affect the results of any operation. |
| 28 | * The bitmap operations that return Boolean (bitmap_empty, |
| 29 | * for example) or scalar (bitmap_weight, for example) results |
| 30 | * carefully filter out these unused bits from impacting their |
| 31 | * results. |
| 32 | * |
| 33 | * These operations actually hold to a slightly stronger rule: |
| 34 | * if you don't input any bitmaps to these ops that have some |
| 35 | * unused bits set, then they won't output any set unused bits |
| 36 | * in output bitmaps. |
| 37 | * |
| 38 | * The byte ordering of bitmaps is more natural on little |
| 39 | * endian architectures. |
| 40 | */ |
| 41 | |
| 42 | int slow_bitmap_empty(const unsigned long *bitmap, long bits) |
| 43 | { |
| 44 | long k, lim = bits/BITS_PER_LONG; |
| 45 | |
| 46 | for (k = 0; k < lim; ++k) { |
| 47 | if (bitmap[k]) { |
| 48 | return 0; |
| 49 | } |
| 50 | } |
| 51 | if (bits % BITS_PER_LONG) { |
| 52 | if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) { |
| 53 | return 0; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | int slow_bitmap_full(const unsigned long *bitmap, long bits) |
| 61 | { |
| 62 | long k, lim = bits/BITS_PER_LONG; |
| 63 | |
| 64 | for (k = 0; k < lim; ++k) { |
| 65 | if (~bitmap[k]) { |
| 66 | return 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (bits % BITS_PER_LONG) { |
| 71 | if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) { |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | int slow_bitmap_equal(const unsigned long *bitmap1, |
| 80 | const unsigned long *bitmap2, long bits) |
| 81 | { |
| 82 | long k, lim = bits/BITS_PER_LONG; |
| 83 | |
| 84 | for (k = 0; k < lim; ++k) { |
| 85 | if (bitmap1[k] != bitmap2[k]) { |
| 86 | return 0; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (bits % BITS_PER_LONG) { |
| 91 | if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) { |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | void slow_bitmap_complement(unsigned long *dst, const unsigned long *src, |
| 100 | long bits) |
| 101 | { |
| 102 | long k, lim = bits/BITS_PER_LONG; |
| 103 | |
| 104 | for (k = 0; k < lim; ++k) { |
| 105 | dst[k] = ~src[k]; |
| 106 | } |
| 107 | |
| 108 | if (bits % BITS_PER_LONG) { |
| 109 | dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
| 114 | const unsigned long *bitmap2, long bits) |
| 115 | { |
| 116 | long k; |
| 117 | long nr = BITS_TO_LONGS(bits); |
| 118 | unsigned long result = 0; |
| 119 | |
| 120 | for (k = 0; k < nr; k++) { |
| 121 | result |= (dst[k] = bitmap1[k] & bitmap2[k]); |
| 122 | } |
| 123 | return result != 0; |
| 124 | } |
| 125 | |
| 126 | void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
| 127 | const unsigned long *bitmap2, long bits) |
| 128 | { |
| 129 | long k; |
| 130 | long nr = BITS_TO_LONGS(bits); |
| 131 | |
| 132 | for (k = 0; k < nr; k++) { |
| 133 | dst[k] = bitmap1[k] | bitmap2[k]; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, |
| 138 | const unsigned long *bitmap2, long bits) |
| 139 | { |
| 140 | long k; |
| 141 | long nr = BITS_TO_LONGS(bits); |
| 142 | |
| 143 | for (k = 0; k < nr; k++) { |
| 144 | dst[k] = bitmap1[k] ^ bitmap2[k]; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, |
| 149 | const unsigned long *bitmap2, long bits) |
| 150 | { |
| 151 | long k; |
| 152 | long nr = BITS_TO_LONGS(bits); |
| 153 | unsigned long result = 0; |
| 154 | |
| 155 | for (k = 0; k < nr; k++) { |
| 156 | result |= (dst[k] = bitmap1[k] & ~bitmap2[k]); |
| 157 | } |
| 158 | return result != 0; |
| 159 | } |
| 160 | |
| 161 | void bitmap_set(unsigned long *map, long start, long nr) |
| 162 | { |
| 163 | unsigned long *p = map + BIT_WORD(start); |
| 164 | const long size = start + nr; |
| 165 | int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 166 | unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); |
| 167 | |
| 168 | assert(start >= 0 && nr >= 0); |
| 169 | |
| 170 | while (nr - bits_to_set >= 0) { |
| 171 | *p |= mask_to_set; |
| 172 | nr -= bits_to_set; |
| 173 | bits_to_set = BITS_PER_LONG; |
| 174 | mask_to_set = ~0UL; |
| 175 | p++; |
| 176 | } |
| 177 | if (nr) { |
| 178 | mask_to_set &= BITMAP_LAST_WORD_MASK(size); |
| 179 | *p |= mask_to_set; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void bitmap_set_atomic(unsigned long *map, long start, long nr) |
| 184 | { |
| 185 | unsigned long *p = map + BIT_WORD(start); |
| 186 | const long size = start + nr; |
| 187 | int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 188 | unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); |
| 189 | |
| 190 | assert(start >= 0 && nr >= 0); |
| 191 | |
| 192 | /* First word */ |
| 193 | if (nr - bits_to_set > 0) { |
| 194 | qatomic_or(p, mask_to_set); |
| 195 | nr -= bits_to_set; |
| 196 | bits_to_set = BITS_PER_LONG; |
| 197 | mask_to_set = ~0UL; |
| 198 | p++; |
| 199 | } |
| 200 | |
| 201 | /* Full words */ |
| 202 | if (bits_to_set == BITS_PER_LONG) { |
| 203 | while (nr >= BITS_PER_LONG) { |
| 204 | *p = ~0UL; |
| 205 | nr -= BITS_PER_LONG; |
| 206 | p++; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* Last word */ |
| 211 | if (nr) { |
| 212 | mask_to_set &= BITMAP_LAST_WORD_MASK(size); |
| 213 | qatomic_or(p, mask_to_set); |
| 214 | } else { |
| 215 | /* If we avoided the full barrier in qatomic_or(), issue a |
| 216 | * barrier to account for the assignments in the while loop. |
| 217 | */ |
| 218 | smp_mb(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void bitmap_clear(unsigned long *map, long start, long nr) |
| 223 | { |
| 224 | unsigned long *p = map + BIT_WORD(start); |
| 225 | const long size = start + nr; |
| 226 | int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 227 | unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); |
| 228 | |
| 229 | assert(start >= 0 && nr >= 0); |
| 230 | |
| 231 | while (nr - bits_to_clear >= 0) { |
| 232 | *p &= ~mask_to_clear; |
| 233 | nr -= bits_to_clear; |
| 234 | bits_to_clear = BITS_PER_LONG; |
| 235 | mask_to_clear = ~0UL; |
| 236 | p++; |
| 237 | } |
| 238 | if (nr) { |
| 239 | mask_to_clear &= BITMAP_LAST_WORD_MASK(size); |
| 240 | *p &= ~mask_to_clear; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool bitmap_test_and_clear(unsigned long *map, long start, long nr) |
| 245 | { |
| 246 | unsigned long *p = map + BIT_WORD(start); |
| 247 | const long size = start + nr; |
| 248 | int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 249 | unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); |
| 250 | bool dirty = false; |
| 251 | |
| 252 | assert(start >= 0 && nr >= 0); |
| 253 | |
| 254 | /* First word */ |
| 255 | if (nr - bits_to_clear > 0) { |
| 256 | if ((*p) & mask_to_clear) { |
| 257 | dirty = true; |
| 258 | } |
| 259 | *p &= ~mask_to_clear; |
| 260 | nr -= bits_to_clear; |
| 261 | bits_to_clear = BITS_PER_LONG; |
| 262 | p++; |
| 263 | } |
| 264 | |
| 265 | /* Full words */ |
| 266 | if (bits_to_clear == BITS_PER_LONG) { |
| 267 | while (nr >= BITS_PER_LONG) { |
| 268 | if (*p) { |
| 269 | dirty = true; |
| 270 | *p = 0; |
| 271 | } |
| 272 | nr -= BITS_PER_LONG; |
| 273 | p++; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Last word */ |
| 278 | if (nr) { |
| 279 | mask_to_clear &= BITMAP_LAST_WORD_MASK(size); |
| 280 | if ((*p) & mask_to_clear) { |
| 281 | dirty = true; |
| 282 | } |
| 283 | *p &= ~mask_to_clear; |
| 284 | } |
| 285 | |
| 286 | return dirty; |
| 287 | } |
| 288 | |
| 289 | bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr) |
| 290 | { |
| 291 | unsigned long *p = map + BIT_WORD(start); |
| 292 | const long size = start + nr; |
| 293 | int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 294 | unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); |
| 295 | unsigned long dirty = 0; |
| 296 | unsigned long old_bits; |
| 297 | |
| 298 | assert(start >= 0 && nr >= 0); |
| 299 | |
| 300 | /* First word */ |
| 301 | if (nr - bits_to_clear > 0) { |
| 302 | old_bits = qatomic_fetch_and(p, ~mask_to_clear); |
| 303 | dirty |= old_bits & mask_to_clear; |
| 304 | nr -= bits_to_clear; |
| 305 | bits_to_clear = BITS_PER_LONG; |
| 306 | mask_to_clear = ~0UL; |
| 307 | p++; |
| 308 | } |
| 309 | |
| 310 | /* Full words */ |
| 311 | if (bits_to_clear == BITS_PER_LONG) { |
| 312 | while (nr >= BITS_PER_LONG) { |
| 313 | if (*p) { |
| 314 | old_bits = qatomic_xchg(p, 0); |
| 315 | dirty |= old_bits; |
| 316 | } |
| 317 | nr -= BITS_PER_LONG; |
| 318 | p++; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* Last word */ |
| 323 | if (nr) { |
| 324 | mask_to_clear &= BITMAP_LAST_WORD_MASK(size); |
| 325 | old_bits = qatomic_fetch_and(p, ~mask_to_clear); |
| 326 | dirty |= old_bits & mask_to_clear; |
| 327 | } else { |
| 328 | if (!dirty) { |
| 329 | smp_mb(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return dirty != 0; |
| 334 | } |
| 335 | |
| 336 | void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src, |
| 337 | long nr) |
| 338 | { |
| 339 | while (nr > 0) { |
| 340 | *dst = qatomic_xchg(src, 0); |
| 341 | dst++; |
| 342 | src++; |
| 343 | nr -= BITS_PER_LONG; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
| 348 | |
| 349 | /** |
| 350 | * bitmap_find_next_zero_area - find a contiguous aligned zero area |
| 351 | * @map: The address to base the search on |
| 352 | * @size: The bitmap size in bits |
| 353 | * @start: The bitnumber to start searching at |
| 354 | * @nr: The number of zeroed bits we're looking for |
| 355 | * @align_mask: Alignment mask for zero area |
| 356 | * |
| 357 | * The @align_mask should be one less than a power of 2; the effect is that |
| 358 | * the bit offset of all zero areas this function finds is multiples of that |
| 359 | * power of 2. A @align_mask of 0 means no alignment is required. |
| 360 | */ |
| 361 | unsigned long bitmap_find_next_zero_area(unsigned long *map, |
| 362 | unsigned long size, |
| 363 | unsigned long start, |
| 364 | unsigned long nr, |
| 365 | unsigned long align_mask) |
| 366 | { |
| 367 | unsigned long index, end, i; |
| 368 | again: |
| 369 | index = find_next_zero_bit(map, size, start); |
| 370 | |
| 371 | /* Align allocation */ |
| 372 | index = ALIGN_MASK(index, align_mask); |
| 373 | |
| 374 | end = index + nr; |
| 375 | if (end > size) { |
| 376 | return end; |
| 377 | } |
| 378 | i = find_next_bit(map, end, index); |
| 379 | if (i < end) { |
| 380 | start = i + 1; |
| 381 | goto again; |
| 382 | } |
| 383 | return index; |
| 384 | } |
| 385 | |
| 386 | int slow_bitmap_intersects(const unsigned long *bitmap1, |
| 387 | const unsigned long *bitmap2, long bits) |
| 388 | { |
| 389 | long k, lim = bits/BITS_PER_LONG; |
| 390 | |
| 391 | for (k = 0; k < lim; ++k) { |
| 392 | if (bitmap1[k] & bitmap2[k]) { |
| 393 | return 1; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (bits % BITS_PER_LONG) { |
| 398 | if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) { |
| 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | long slow_bitmap_count_one(const unsigned long *bitmap, long nbits) |
| 406 | { |
| 407 | long k, lim = nbits / BITS_PER_LONG, result = 0; |
| 408 | |
| 409 | for (k = 0; k < lim; k++) { |
| 410 | result += ctpopl(bitmap[k]); |
| 411 | } |
| 412 | |
| 413 | if (nbits % BITS_PER_LONG) { |
| 414 | result += ctpopl(bitmap[k] & BITMAP_LAST_WORD_MASK(nbits)); |
| 415 | } |
| 416 | |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | static void bitmap_to_from_le(unsigned long *dst, |
| 421 | const unsigned long *src, long nbits) |
| 422 | { |
| 423 | long len = BITS_TO_LONGS(nbits); |
| 424 | |
| 425 | #if HOST_BIG_ENDIAN |
| 426 | long index; |
| 427 | |
| 428 | for (index = 0; index < len; index++) { |
| 429 | # if HOST_LONG_BITS == 64 |
| 430 | dst[index] = bswap64(src[index]); |
| 431 | # else |
| 432 | dst[index] = bswap32(src[index]); |
| 433 | # endif |
| 434 | } |
| 435 | #else |
| 436 | memcpy(dst, src, len * sizeof(unsigned long)); |
| 437 | #endif |
| 438 | } |
| 439 | |
| 440 | void bitmap_from_le(unsigned long *dst, const unsigned long *src, |
| 441 | long nbits) |
| 442 | { |
| 443 | bitmap_to_from_le(dst, src, nbits); |
| 444 | } |
| 445 | |
| 446 | void bitmap_to_le(unsigned long *dst, const unsigned long *src, |
| 447 | long nbits) |
| 448 | { |
| 449 | bitmap_to_from_le(dst, src, nbits); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Copy "src" bitmap with a positive offset and put it into the "dst" |
| 454 | * bitmap. The caller needs to make sure the bitmap size of "src" |
| 455 | * is bigger than (shift + nbits). |
| 456 | */ |
| 457 | void bitmap_copy_with_src_offset(unsigned long *dst, const unsigned long *src, |
| 458 | unsigned long shift, unsigned long nbits) |
| 459 | { |
| 460 | unsigned long left_mask, right_mask, last_mask; |
| 461 | |
| 462 | /* Proper shift src pointer to the first word to copy from */ |
| 463 | src += BIT_WORD(shift); |
| 464 | shift %= BITS_PER_LONG; |
| 465 | |
| 466 | if (!shift) { |
| 467 | /* Fast path */ |
| 468 | bitmap_copy(dst, src, nbits); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | right_mask = (1ul << shift) - 1; |
| 473 | left_mask = ~right_mask; |
| 474 | |
| 475 | while (nbits >= BITS_PER_LONG) { |
| 476 | *dst = (*src & left_mask) >> shift; |
| 477 | *dst |= (src[1] & right_mask) << (BITS_PER_LONG - shift); |
| 478 | dst++; |
| 479 | src++; |
| 480 | nbits -= BITS_PER_LONG; |
| 481 | } |
| 482 | |
| 483 | if (nbits > BITS_PER_LONG - shift) { |
| 484 | *dst = (*src & left_mask) >> shift; |
| 485 | nbits -= BITS_PER_LONG - shift; |
| 486 | last_mask = (1ul << nbits) - 1; |
| 487 | *dst |= (src[1] & last_mask) << (BITS_PER_LONG - shift); |
| 488 | } else if (nbits) { |
| 489 | last_mask = (1ul << nbits) - 1; |
| 490 | *dst = (*src >> shift) & last_mask; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Copy "src" bitmap into the "dst" bitmap with an offset in the |
| 496 | * "dst". The caller needs to make sure the bitmap size of "dst" is |
| 497 | * bigger than (shift + nbits). |
| 498 | */ |
| 499 | void bitmap_copy_with_dst_offset(unsigned long *dst, const unsigned long *src, |
| 500 | unsigned long shift, unsigned long nbits) |
| 501 | { |
| 502 | unsigned long left_mask, right_mask, last_mask; |
| 503 | |
| 504 | /* Proper shift dst pointer to the first word to copy from */ |
| 505 | dst += BIT_WORD(shift); |
| 506 | shift %= BITS_PER_LONG; |
| 507 | |
| 508 | if (!shift) { |
| 509 | /* Fast path */ |
| 510 | bitmap_copy(dst, src, nbits); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | right_mask = (1ul << (BITS_PER_LONG - shift)) - 1; |
| 515 | left_mask = ~right_mask; |
| 516 | |
| 517 | *dst &= (1ul << shift) - 1; |
| 518 | while (nbits >= BITS_PER_LONG) { |
| 519 | *dst |= (*src & right_mask) << shift; |
| 520 | dst[1] = (*src & left_mask) >> (BITS_PER_LONG - shift); |
| 521 | dst++; |
| 522 | src++; |
| 523 | nbits -= BITS_PER_LONG; |
| 524 | } |
| 525 | |
| 526 | if (nbits > BITS_PER_LONG - shift) { |
| 527 | *dst |= (*src & right_mask) << shift; |
| 528 | nbits -= BITS_PER_LONG - shift; |
| 529 | last_mask = ((1ul << nbits) - 1) << (BITS_PER_LONG - shift); |
| 530 | dst[1] = (*src & last_mask) >> (BITS_PER_LONG - shift); |
| 531 | } else if (nbits) { |
| 532 | last_mask = (1ul << nbits) - 1; |
| 533 | *dst |= (*src & last_mask) << shift; |
| 534 | } |
| 535 | } |